package k4;

import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.widget.EditText;
import androidx.emoji2.text.e;
import com.google.android.gms.common.api.Api;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/* loaded from: classes.dex */
final class g implements TextWatcher {

    /* renamed from: p, reason: collision with root package name */
    private final EditText f25376p;

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

    /* renamed from: r, reason: collision with root package name */
    private e.f f25378r;

    /* renamed from: s, reason: collision with root package name */
    private int f25379s = Api.BaseClientBuilder.API_PRIORITY_OTHER;

    /* renamed from: t, reason: collision with root package name */
    private int f25380t = 0;

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

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class a extends e.f {

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

        a(EditText editText) {
            this.f25382a = new WeakReference(editText);
        }

        @Override // androidx.emoji2.text.e.f
        public void b() {
            super.b();
            g.b((EditText) this.f25382a.get(), 1);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public g(EditText editText, boolean z10) {
        this.f25376p = editText;
        this.f25377q = z10;
    }

    private e.f a() {
        if (this.f25378r == null) {
            this.f25378r = new a(this.f25376p);
        }
        return this.f25378r;
    }

    static void b(EditText editText, int i10) {
        if (i10 == 1 && editText != null && editText.isAttachedToWindow()) {
            Editable editableText = editText.getEditableText();
            int selectionStart = Selection.getSelectionStart(editableText);
            int selectionEnd = Selection.getSelectionEnd(editableText);
            androidx.emoji2.text.e.c().r(editableText);
            d.b(editableText, selectionStart, selectionEnd);
        }
    }

    private boolean d() {
        return (this.f25381u && (this.f25377q || androidx.emoji2.text.e.k())) ? false : true;
    }

    @Override // android.text.TextWatcher
    public void afterTextChanged(Editable editable) {
    }

    @Override // android.text.TextWatcher
    public void beforeTextChanged(CharSequence charSequence, int i10, int i11, int i12) {
    }

    public void c(boolean z10) {
        if (this.f25381u != z10) {
            if (this.f25378r != null) {
                androidx.emoji2.text.e.c().w(this.f25378r);
            }
            this.f25381u = z10;
            if (z10) {
                b(this.f25376p, androidx.emoji2.text.e.c().g());
            }
        }
    }

    @Override // android.text.TextWatcher
    public void onTextChanged(CharSequence charSequence, int i10, int i11, int i12) {
        if (this.f25376p.isInEditMode() || d() || i11 > i12 || !(charSequence instanceof Spannable)) {
            return;
        }
        int g10 = androidx.emoji2.text.e.c().g();
        if (g10 != 0) {
            if (g10 == 1) {
                androidx.emoji2.text.e.c().u((Spannable) charSequence, i10, i10 + i12, this.f25379s, this.f25380t);
                return;
            } else if (g10 != 3) {
                return;
            }
        }
        androidx.emoji2.text.e.c().v(a());
    }
}
