package w2;

import android.R;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.CorrectionInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputContentInfo;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes.dex */
public final class m0 implements InputConnection {

    /* renamed from: a, reason: collision with root package name */
    private final t f40781a;

    /* renamed from: b, reason: collision with root package name */
    private final boolean f40782b;

    /* renamed from: c, reason: collision with root package name */
    private int f40783c;

    /* renamed from: d, reason: collision with root package name */
    private q0 f40784d;

    /* renamed from: e, reason: collision with root package name */
    private int f40785e;

    /* renamed from: f, reason: collision with root package name */
    private boolean f40786f;

    /* renamed from: g, reason: collision with root package name */
    private final List f40787g = new ArrayList();

    /* renamed from: h, reason: collision with root package name */
    private boolean f40788h = true;

    public m0(q0 q0Var, t tVar, boolean z10) {
        this.f40781a = tVar;
        this.f40782b = z10;
        this.f40784d = q0Var;
    }

    private final void b(i iVar) {
        c();
        try {
            this.f40787g.add(iVar);
        } finally {
            d();
        }
    }

    private final boolean c() {
        this.f40783c++;
        return true;
    }

    private final boolean d() {
        List V0;
        int i10 = this.f40783c - 1;
        this.f40783c = i10;
        if (i10 == 0 && (!this.f40787g.isEmpty())) {
            t tVar = this.f40781a;
            V0 = sm.c0.V0(this.f40787g);
            tVar.d(V0);
            this.f40787g.clear();
        }
        return this.f40783c > 0;
    }

    private final void e(int i10) {
        sendKeyEvent(new KeyEvent(0, i10));
        sendKeyEvent(new KeyEvent(1, i10));
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean beginBatchEdit() {
        boolean z10 = this.f40788h;
        return z10 ? c() : z10;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean clearMetaKeyStates(int i10) {
        boolean z10 = this.f40788h;
        if (z10) {
            return false;
        }
        return z10;
    }

    @Override // android.view.inputmethod.InputConnection
    public void closeConnection() {
        this.f40787g.clear();
        this.f40783c = 0;
        this.f40788h = false;
        this.f40781a.e(this);
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean commitCompletion(CompletionInfo completionInfo) {
        boolean z10 = this.f40788h;
        if (z10) {
            return false;
        }
        return z10;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean commitContent(InputContentInfo inputContentInfo, int i10, Bundle bundle) {
        boolean z10 = this.f40788h;
        if (z10) {
            return false;
        }
        return z10;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean commitCorrection(CorrectionInfo correctionInfo) {
        boolean z10 = this.f40788h;
        return z10 ? this.f40782b : z10;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean commitText(CharSequence charSequence, int i10) {
        boolean z10 = this.f40788h;
        if (z10) {
            b(new a(String.valueOf(charSequence), i10));
        }
        return z10;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean deleteSurroundingText(int i10, int i11) {
        boolean z10 = this.f40788h;
        if (!z10) {
            return z10;
        }
        b(new g(i10, i11));
        return true;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean deleteSurroundingTextInCodePoints(int i10, int i11) {
        boolean z10 = this.f40788h;
        if (!z10) {
            return z10;
        }
        b(new h(i10, i11));
        return true;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean endBatchEdit() {
        return d();
    }

    public final void f(q0 q0Var) {
        this.f40784d = q0Var;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean finishComposingText() {
        boolean z10 = this.f40788h;
        if (!z10) {
            return z10;
        }
        b(new n());
        return true;
    }

    public final void g(q0 q0Var, u uVar) {
        if (this.f40788h) {
            f(q0Var);
            if (this.f40786f) {
                uVar.c(this.f40785e, w.a(q0Var));
            }
            q2.m0 f10 = q0Var.f();
            int l10 = f10 != null ? q2.m0.l(f10.r()) : -1;
            q2.m0 f11 = q0Var.f();
            uVar.d(q2.m0.l(q0Var.g()), q2.m0.k(q0Var.g()), l10, f11 != null ? q2.m0.k(f11.r()) : -1);
        }
    }

    @Override // android.view.inputmethod.InputConnection
    public int getCursorCapsMode(int i10) {
        return TextUtils.getCapsMode(this.f40784d.h(), q2.m0.l(this.f40784d.g()), i10);
    }

    @Override // android.view.inputmethod.InputConnection
    public ExtractedText getExtractedText(ExtractedTextRequest extractedTextRequest, int i10) {
        boolean z10 = (i10 & 1) != 0;
        this.f40786f = z10;
        if (z10) {
            this.f40785e = extractedTextRequest != null ? extractedTextRequest.token : 0;
        }
        return w.a(this.f40784d);
    }

    @Override // android.view.inputmethod.InputConnection
    public Handler getHandler() {
        return null;
    }

    @Override // android.view.inputmethod.InputConnection
    public CharSequence getSelectedText(int i10) {
        if (q2.m0.h(this.f40784d.g())) {
            return null;
        }
        return r0.a(this.f40784d).toString();
    }

    @Override // android.view.inputmethod.InputConnection
    public CharSequence getTextAfterCursor(int i10, int i11) {
        return r0.b(this.f40784d, i10).toString();
    }

    @Override // android.view.inputmethod.InputConnection
    public CharSequence getTextBeforeCursor(int i10, int i11) {
        return r0.c(this.f40784d, i10).toString();
    }

    /* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
    @Override // android.view.inputmethod.InputConnection
    public boolean performContextMenuAction(int i10) {
        boolean z10 = this.f40788h;
        if (z10) {
            z10 = false;
            switch (i10) {
                case R.id.selectAll:
                    b(new p0(0, this.f40784d.h().length()));
                    break;
                case R.id.cut:
                    e(277);
                    break;
                case R.id.copy:
                    e(278);
                    break;
                case R.id.paste:
                    e(279);
                    break;
            }
        }
        return z10;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean performEditorAction(int i10) {
        int a10;
        boolean z10 = this.f40788h;
        if (!z10) {
            return z10;
        }
        if (i10 != 0) {
            switch (i10) {
                case 2:
                    a10 = r.f40806b.c();
                    break;
                case 3:
                    a10 = r.f40806b.g();
                    break;
                case 4:
                    a10 = r.f40806b.h();
                    break;
                case 5:
                    a10 = r.f40806b.d();
                    break;
                case 6:
                    a10 = r.f40806b.b();
                    break;
                case 7:
                    a10 = r.f40806b.f();
                    break;
                default:
                    Log.w("RecordingIC", "IME sends unsupported Editor Action: " + i10);
                    a10 = r.f40806b.a();
                    break;
            }
        } else {
            a10 = r.f40806b.a();
        }
        this.f40781a.c(a10);
        return true;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean performPrivateCommand(String str, Bundle bundle) {
        boolean z10 = this.f40788h;
        if (z10) {
            return true;
        }
        return z10;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean reportFullscreenMode(boolean z10) {
        return false;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean requestCursorUpdates(int i10) {
        boolean z10;
        boolean z11;
        boolean z12;
        boolean z13;
        boolean z14 = this.f40788h;
        if (!z14) {
            return z14;
        }
        boolean z15 = false;
        boolean z16 = (i10 & 1) != 0;
        boolean z17 = (i10 & 2) != 0;
        int i11 = Build.VERSION.SDK_INT;
        if (i11 >= 33) {
            boolean z18 = (i10 & 16) != 0;
            boolean z19 = (i10 & 8) != 0;
            boolean z20 = (i10 & 4) != 0;
            if (i11 >= 34 && (i10 & 32) != 0) {
                z15 = true;
            }
            if (z18 || z19 || z20 || z15) {
                z11 = z15;
                z10 = z20;
                z13 = z19;
                z12 = z18;
            } else if (i11 >= 34) {
                z12 = true;
                z13 = true;
                z10 = true;
                z11 = true;
            } else {
                z11 = z15;
                z12 = true;
                z13 = true;
                z10 = true;
            }
        } else {
            z10 = false;
            z11 = false;
            z12 = true;
            z13 = true;
        }
        this.f40781a.b(z16, z17, z12, z13, z10, z11);
        return true;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean sendKeyEvent(KeyEvent keyEvent) {
        boolean z10 = this.f40788h;
        if (!z10) {
            return z10;
        }
        this.f40781a.a(keyEvent);
        return true;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean setComposingRegion(int i10, int i11) {
        boolean z10 = this.f40788h;
        if (z10) {
            b(new n0(i10, i11));
        }
        return z10;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean setComposingText(CharSequence charSequence, int i10) {
        boolean z10 = this.f40788h;
        if (z10) {
            b(new o0(String.valueOf(charSequence), i10));
        }
        return z10;
    }

    @Override // android.view.inputmethod.InputConnection
    public boolean setSelection(int i10, int i11) {
        boolean z10 = this.f40788h;
        if (!z10) {
            return z10;
        }
        b(new p0(i10, i11));
        return true;
    }
}
