package Z0;

import T0.N;
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.BaseInputConnection;
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.lang.ref.WeakReference;
import java.util.ArrayList;
import x4.AbstractC3251q;

/* JADX INFO: loaded from: classes.dex */
public final class u implements InputConnection {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final C0.B f9121a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final boolean f9122b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f9123c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public y f9124d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f9125e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public boolean f9126f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final ArrayList f9127g = new ArrayList();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f9128h = true;

    public u(y yVar, C0.B b5, boolean z6) {
        this.f9121a = b5;
        this.f9122b = z6;
        this.f9124d = yVar;
    }

    public final void a(InterfaceC0866g interfaceC0866g) {
        this.f9123c++;
        try {
            this.f9127g.add(interfaceC0866g);
        } finally {
            b();
        }
    }

    public final boolean b() {
        int i6 = this.f9123c - 1;
        this.f9123c = i6;
        if (i6 == 0) {
            ArrayList arrayList = this.f9127g;
            if (!arrayList.isEmpty()) {
                ((B) this.f9121a.f506b).f9057e.invoke(AbstractC3251q.E0(arrayList));
                arrayList.clear();
            }
        }
        return this.f9123c > 0;
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean beginBatchEdit() {
        boolean z6 = this.f9128h;
        if (!z6) {
            return z6;
        }
        this.f9123c++;
        return true;
    }

    public final void c(int i6) {
        sendKeyEvent(new KeyEvent(0, i6));
        sendKeyEvent(new KeyEvent(1, i6));
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean clearMetaKeyStates(int i6) {
        boolean z6 = this.f9128h;
        if (z6) {
            return false;
        }
        return z6;
    }

    @Override // android.view.inputmethod.InputConnection
    public final void closeConnection() {
        this.f9127g.clear();
        this.f9123c = 0;
        this.f9128h = false;
        B b5 = (B) this.f9121a.f506b;
        int size = b5.f9061i.size();
        for (int i6 = 0; i6 < size; i6++) {
            ArrayList arrayList = b5.f9061i;
            if (kotlin.jvm.internal.o.c(((WeakReference) arrayList.get(i6)).get(), this)) {
                arrayList.remove(i6);
                return;
            }
        }
    }

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

    @Override // android.view.inputmethod.InputConnection
    public final boolean commitContent(InputContentInfo inputContentInfo, int i6, Bundle bundle) {
        boolean z6 = this.f9128h;
        if (z6) {
            return false;
        }
        return z6;
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean commitCorrection(CorrectionInfo correctionInfo) {
        boolean z6 = this.f9128h;
        return z6 ? this.f9122b : z6;
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean commitText(CharSequence charSequence, int i6) {
        boolean z6 = this.f9128h;
        if (z6) {
            a(new C0860a(String.valueOf(charSequence), i6));
        }
        return z6;
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean deleteSurroundingText(int i6, int i7) {
        boolean z6 = this.f9128h;
        if (!z6) {
            return z6;
        }
        a(new C0864e(i6, i7));
        return true;
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean deleteSurroundingTextInCodePoints(int i6, int i7) {
        boolean z6 = this.f9128h;
        if (!z6) {
            return z6;
        }
        a(new C0865f(i6, i7));
        return true;
    }

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

    @Override // android.view.inputmethod.InputConnection
    public final boolean finishComposingText() {
        boolean z6 = this.f9128h;
        if (!z6) {
            return z6;
        }
        a(new i());
        return true;
    }

    @Override // android.view.inputmethod.InputConnection
    public final int getCursorCapsMode(int i6) {
        y yVar = this.f9124d;
        return TextUtils.getCapsMode(yVar.f9136a.f6510b, N.f(yVar.f9137b), i6);
    }

    @Override // android.view.inputmethod.InputConnection
    public final ExtractedText getExtractedText(ExtractedTextRequest extractedTextRequest, int i6) {
        boolean z6 = (i6 & 1) != 0;
        this.f9126f = z6;
        if (z6) {
            this.f9125e = extractedTextRequest != null ? extractedTextRequest.token : 0;
        }
        return W0.e.J(this.f9124d);
    }

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

    @Override // android.view.inputmethod.InputConnection
    public final CharSequence getSelectedText(int i6) {
        if (N.c(this.f9124d.f9137b)) {
            return null;
        }
        return W.b.j0(this.f9124d).f6510b;
    }

    @Override // android.view.inputmethod.InputConnection
    public final CharSequence getTextAfterCursor(int i6, int i7) {
        return W.b.m0(this.f9124d, i6).f6510b;
    }

    @Override // android.view.inputmethod.InputConnection
    public final CharSequence getTextBeforeCursor(int i6, int i7) {
        return W.b.n0(this.f9124d, i6).f6510b;
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean performContextMenuAction(int i6) {
        boolean z6 = this.f9128h;
        if (z6) {
            z6 = false;
            switch (i6) {
                case R.id.selectAll:
                    a(new x(0, this.f9124d.f9136a.f6510b.length()));
                    break;
                case R.id.cut:
                    c(277);
                    return false;
                case R.id.copy:
                    c(278);
                    return false;
                case R.id.paste:
                    c(279);
                    return false;
                default:
                    return false;
            }
        }
        return z6;
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean performEditorAction(int i6) {
        int i7;
        boolean z6 = this.f9128h;
        if (z6) {
            z6 = true;
            if (i6 != 0) {
                switch (i6) {
                    case 2:
                        i7 = 2;
                        break;
                    case 3:
                        i7 = 3;
                        break;
                    case 4:
                        i7 = 4;
                        break;
                    case 5:
                        i7 = 6;
                        break;
                    case 6:
                        i7 = 7;
                        break;
                    case 7:
                        i7 = 5;
                        break;
                    default:
                        Log.w("RecordingIC", "IME sends unsupported Editor Action: " + i6);
                        i7 = 1;
                        break;
                }
                ((B) this.f9121a.f506b).f9058f.invoke(new j(i7));
            } else {
                i7 = 1;
                ((B) this.f9121a.f506b).f9058f.invoke(new j(i7));
            }
        }
        return z6;
    }

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

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

    @Override // android.view.inputmethod.InputConnection
    public final boolean requestCursorUpdates(int i6) {
        boolean z6;
        boolean z7;
        boolean z8;
        boolean z9 = this.f9128h;
        if (!z9) {
            return z9;
        }
        boolean z10 = false;
        boolean z11 = (i6 & 1) != 0;
        boolean z12 = (i6 & 2) != 0;
        int i7 = Build.VERSION.SDK_INT;
        if (i7 >= 33) {
            z6 = (i6 & 16) != 0;
            z7 = (i6 & 8) != 0;
            boolean z13 = (i6 & 4) != 0;
            if (i7 >= 34 && (i6 & 32) != 0) {
                z10 = true;
            }
            if (z6 || z7 || z13 || z10) {
                z8 = z10;
                z10 = z13;
            } else if (i7 >= 34) {
                z8 = true;
                z10 = true;
                z6 = true;
                z7 = true;
            } else {
                z6 = true;
                z7 = true;
                z8 = z10;
                z10 = true;
            }
        } else {
            z6 = true;
            z7 = true;
            z8 = false;
        }
        C0862c c0862c = ((B) this.f9121a.f506b).l;
        synchronized (c0862c.f9085c) {
            try {
                c0862c.f9088f = z6;
                c0862c.f9089g = z7;
                c0862c.f9090h = z10;
                c0862c.f9091i = z8;
                if (z11) {
                    c0862c.f9087e = true;
                    if (c0862c.f9092j != null) {
                        c0862c.a();
                    }
                }
                c0862c.f9086d = z12;
            } catch (Throwable th) {
                throw th;
            }
        }
        return true;
    }

    /* JADX WARN: Type inference failed for: r0v4, types: [java.lang.Object, w4.h] */
    @Override // android.view.inputmethod.InputConnection
    public final boolean sendKeyEvent(KeyEvent keyEvent) {
        boolean z6 = this.f9128h;
        if (!z6) {
            return z6;
        }
        ((BaseInputConnection) ((B) this.f9121a.f506b).f9062j.getValue()).sendKeyEvent(keyEvent);
        return true;
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean setComposingRegion(int i6, int i7) {
        boolean z6 = this.f9128h;
        if (z6) {
            a(new v(i6, i7));
        }
        return z6;
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean setComposingText(CharSequence charSequence, int i6) {
        boolean z6 = this.f9128h;
        if (z6) {
            a(new w(String.valueOf(charSequence), i6));
        }
        return z6;
    }

    @Override // android.view.inputmethod.InputConnection
    public final boolean setSelection(int i6, int i7) {
        boolean z6 = this.f9128h;
        if (!z6) {
            return z6;
        }
        a(new x(i6, i7));
        return true;
    }
}
