package androidx.emoji2.text;

import android.os.Build;
import android.text.Editable;
import android.text.SpanWatcher;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import p020g0.C0737f;

/* renamed from: androidx.emoji2.text.q */
/* loaded from: classes.dex */
public final class C0257q extends SpannableStringBuilder {

    /* renamed from: d */
    public final Class<?> f1144d;

    /* renamed from: e */
    public final List<C0258a> f1145e;

    /* renamed from: androidx.emoji2.text.q$a */
    /* loaded from: classes.dex */
    public static class C0258a implements TextWatcher, SpanWatcher {

        /* renamed from: d */
        public final Object f1146d;

        /* renamed from: e */
        public final AtomicInteger f1147e = new AtomicInteger(0);

        public C0258a(Object obj) {
            this.f1146d = obj;
        }

        /* renamed from: a */
        public final void m4302a() {
            this.f1147e.incrementAndGet();
        }

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

        /* renamed from: b */
        public final boolean m4301b(Object obj) {
            return obj instanceof AbstractC0246l;
        }

        @Override // android.text.TextWatcher
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            ((TextWatcher) this.f1146d).beforeTextChanged(charSequence, i, i2, i3);
        }

        /* renamed from: c */
        public final void m4300c() {
            this.f1147e.decrementAndGet();
        }

        @Override // android.text.SpanWatcher
        public void onSpanAdded(Spannable spannable, Object obj, int i, int i2) {
            if (this.f1147e.get() <= 0 || !m4301b(obj)) {
                ((SpanWatcher) this.f1146d).onSpanAdded(spannable, obj, i, i2);
            }
        }

        @Override // android.text.SpanWatcher
        public void onSpanChanged(Spannable spannable, Object obj, int i, int i2, int i3, int i4) {
            int i5;
            int i6;
            if (this.f1147e.get() <= 0 || !m4301b(obj)) {
                if (Build.VERSION.SDK_INT < 28) {
                    if (i > i2) {
                        i = 0;
                    }
                    if (i3 > i4) {
                        i5 = i;
                        i6 = 0;
                        ((SpanWatcher) this.f1146d).onSpanChanged(spannable, obj, i5, i2, i6, i4);
                    }
                }
                i5 = i;
                i6 = i3;
                ((SpanWatcher) this.f1146d).onSpanChanged(spannable, obj, i5, i2, i6, i4);
            }
        }

        @Override // android.text.SpanWatcher
        public void onSpanRemoved(Spannable spannable, Object obj, int i, int i2) {
            if (this.f1147e.get() <= 0 || !m4301b(obj)) {
                ((SpanWatcher) this.f1146d).onSpanRemoved(spannable, obj, i, i2);
            }
        }

        @Override // android.text.TextWatcher
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            ((TextWatcher) this.f1146d).onTextChanged(charSequence, i, i2, i3);
        }
    }

    public C0257q(Class<?> cls, CharSequence charSequence) {
        super(charSequence);
        this.f1145e = new ArrayList();
        C0737f.m3032g(cls, "watcherClass cannot be null");
        this.f1144d = cls;
    }

    public C0257q(Class<?> cls, CharSequence charSequence, int i, int i2) {
        super(charSequence, i, i2);
        this.f1145e = new ArrayList();
        C0737f.m3032g(cls, "watcherClass cannot be null");
        this.f1144d = cls;
    }

    /* renamed from: c */
    public static C0257q m4309c(Class<?> cls, CharSequence charSequence) {
        return new C0257q(cls, charSequence);
    }

    /* renamed from: a */
    public void m4311a() {
        m4310b();
    }

    @Override // android.text.SpannableStringBuilder, android.text.Editable, java.lang.Appendable
    public SpannableStringBuilder append(char c) {
        super.append(c);
        return this;
    }

    @Override // android.text.SpannableStringBuilder, android.text.Editable, java.lang.Appendable
    public SpannableStringBuilder append(CharSequence charSequence) {
        super.append(charSequence);
        return this;
    }

    @Override // android.text.SpannableStringBuilder, android.text.Editable, java.lang.Appendable
    public SpannableStringBuilder append(CharSequence charSequence, int i, int i2) {
        super.append(charSequence, i, i2);
        return this;
    }

    @Override // android.text.SpannableStringBuilder
    public SpannableStringBuilder append(CharSequence charSequence, Object obj, int i) {
        super.append(charSequence, obj, i);
        return this;
    }

    /* renamed from: b */
    public final void m4310b() {
        for (int i = 0; i < this.f1145e.size(); i++) {
            this.f1145e.get(i).m4302a();
        }
    }

    /* renamed from: d */
    public void m4308d() {
        m4303i();
        m4307e();
    }

    @Override // android.text.SpannableStringBuilder, android.text.Editable
    public SpannableStringBuilder delete(int i, int i2) {
        super.delete(i, i2);
        return this;
    }

    /* renamed from: e */
    public final void m4307e() {
        for (int i = 0; i < this.f1145e.size(); i++) {
            this.f1145e.get(i).onTextChanged(this, 0, length(), length());
        }
    }

    /* renamed from: f */
    public final C0258a m4306f(Object obj) {
        for (int i = 0; i < this.f1145e.size(); i++) {
            C0258a c0258a = this.f1145e.get(i);
            if (c0258a.f1146d == obj) {
                return c0258a;
            }
        }
        return null;
    }

    /* renamed from: g */
    public final boolean m4305g(Class<?> cls) {
        return this.f1144d == cls;
    }

    @Override // android.text.SpannableStringBuilder, android.text.Spanned
    public int getSpanEnd(Object obj) {
        C0258a m4306f;
        if (m4304h(obj) && (m4306f = m4306f(obj)) != null) {
            obj = m4306f;
        }
        return super.getSpanEnd(obj);
    }

    @Override // android.text.SpannableStringBuilder, android.text.Spanned
    public int getSpanFlags(Object obj) {
        C0258a m4306f;
        if (m4304h(obj) && (m4306f = m4306f(obj)) != null) {
            obj = m4306f;
        }
        return super.getSpanFlags(obj);
    }

    @Override // android.text.SpannableStringBuilder, android.text.Spanned
    public int getSpanStart(Object obj) {
        C0258a m4306f;
        if (m4304h(obj) && (m4306f = m4306f(obj)) != null) {
            obj = m4306f;
        }
        return super.getSpanStart(obj);
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // android.text.SpannableStringBuilder, android.text.Spanned
    public <T> T[] getSpans(int i, int i2, Class<T> cls) {
        if (m4305g(cls)) {
            C0258a[] c0258aArr = (C0258a[]) super.getSpans(i, i2, C0258a.class);
            T[] tArr = (T[]) ((Object[]) Array.newInstance((Class<?>) cls, c0258aArr.length));
            for (int i3 = 0; i3 < c0258aArr.length; i3++) {
                tArr[i3] = c0258aArr[i3].f1146d;
            }
            return tArr;
        }
        return (T[]) super.getSpans(i, i2, cls);
    }

    /* renamed from: h */
    public final boolean m4304h(Object obj) {
        return obj != null && m4305g(obj.getClass());
    }

    /* renamed from: i */
    public final void m4303i() {
        for (int i = 0; i < this.f1145e.size(); i++) {
            this.f1145e.get(i).m4300c();
        }
    }

    @Override // android.text.SpannableStringBuilder, android.text.Editable
    public SpannableStringBuilder insert(int i, CharSequence charSequence) {
        super.insert(i, charSequence);
        return this;
    }

    @Override // android.text.SpannableStringBuilder, android.text.Editable
    public SpannableStringBuilder insert(int i, CharSequence charSequence, int i2, int i3) {
        super.insert(i, charSequence, i2, i3);
        return this;
    }

    @Override // android.text.SpannableStringBuilder, android.text.Spanned
    public int nextSpanTransition(int i, int i2, Class cls) {
        return super.nextSpanTransition(i, i2, (cls == null || m4305g(cls)) ? C0258a.class : C0258a.class);
    }

    @Override // android.text.SpannableStringBuilder, android.text.Spannable
    public void removeSpan(Object obj) {
        C0258a c0258a;
        if (m4304h(obj)) {
            c0258a = m4306f(obj);
            if (c0258a != null) {
                obj = c0258a;
            }
        } else {
            c0258a = null;
        }
        super.removeSpan(obj);
        if (c0258a != null) {
            this.f1145e.remove(c0258a);
        }
    }

    @Override // android.text.SpannableStringBuilder, android.text.Editable
    public SpannableStringBuilder replace(int i, int i2, CharSequence charSequence) {
        m4310b();
        super.replace(i, i2, charSequence);
        m4303i();
        return this;
    }

    @Override // android.text.SpannableStringBuilder, android.text.Editable
    public SpannableStringBuilder replace(int i, int i2, CharSequence charSequence, int i3, int i4) {
        m4310b();
        super.replace(i, i2, charSequence, i3, i4);
        m4303i();
        return this;
    }

    @Override // android.text.SpannableStringBuilder, android.text.Spannable
    public void setSpan(Object obj, int i, int i2, int i3) {
        if (m4304h(obj)) {
            C0258a c0258a = new C0258a(obj);
            this.f1145e.add(c0258a);
            obj = c0258a;
        }
        super.setSpan(obj, i, i2, i3);
    }

    @Override // android.text.SpannableStringBuilder, java.lang.CharSequence
    public CharSequence subSequence(int i, int i2) {
        return new C0257q(this.f1144d, this, i, i2);
    }
}