package androidx.emoji2.text;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import com.google.android.gms.common.api.Api;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/* loaded from: classes.dex */
public class e {

    /* renamed from: o, reason: collision with root package name */
    private static final Object f4237o = new Object();

    /* renamed from: p, reason: collision with root package name */
    private static final Object f4238p = new Object();

    /* renamed from: q, reason: collision with root package name */
    private static volatile e f4239q;

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

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

    /* renamed from: f, reason: collision with root package name */
    final h f4245f;

    /* renamed from: g, reason: collision with root package name */
    private final j f4246g;

    /* renamed from: h, reason: collision with root package name */
    final boolean f4247h;

    /* renamed from: i, reason: collision with root package name */
    final boolean f4248i;

    /* renamed from: j, reason: collision with root package name */
    final int[] f4249j;

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

    /* renamed from: l, reason: collision with root package name */
    private final int f4251l;

    /* renamed from: m, reason: collision with root package name */
    private final int f4252m;

    /* renamed from: n, reason: collision with root package name */
    private final InterfaceC0111e f4253n;

    /* renamed from: a, reason: collision with root package name */
    private final ReadWriteLock f4240a = new ReentrantReadWriteLock();

    /* renamed from: c, reason: collision with root package name */
    private volatile int f4242c = 3;

    /* renamed from: d, reason: collision with root package name */
    private final Handler f4243d = new Handler(Looper.getMainLooper());

    /* loaded from: classes.dex */
    private static final class a extends b {

        /* renamed from: b, reason: collision with root package name */
        private volatile androidx.emoji2.text.h f4254b;

        /* renamed from: c, reason: collision with root package name */
        private volatile m f4255c;

        /* renamed from: androidx.emoji2.text.e$a$a, reason: collision with other inner class name */
        /* loaded from: classes.dex */
        class C0110a extends i {
            C0110a() {
            }

            @Override // androidx.emoji2.text.e.i
            public void a(Throwable th2) {
                a.this.f4257a.p(th2);
            }

            @Override // androidx.emoji2.text.e.i
            public void b(m mVar) {
                a.this.f(mVar);
            }
        }

        a(e eVar) {
            super(eVar);
        }

        @Override // androidx.emoji2.text.e.b
        int a(CharSequence charSequence, int i10) {
            return this.f4254b.b(charSequence, i10);
        }

        @Override // androidx.emoji2.text.e.b
        int b(CharSequence charSequence, int i10) {
            return this.f4254b.c(charSequence, i10);
        }

        @Override // androidx.emoji2.text.e.b
        void c() {
            try {
                this.f4257a.f4245f.a(new C0110a());
            } catch (Throwable th2) {
                this.f4257a.p(th2);
            }
        }

        @Override // androidx.emoji2.text.e.b
        CharSequence d(CharSequence charSequence, int i10, int i11, int i12, boolean z10) {
            return this.f4254b.j(charSequence, i10, i11, i12, z10);
        }

        @Override // androidx.emoji2.text.e.b
        void e(EditorInfo editorInfo) {
            editorInfo.extras.putInt("android.support.text.emoji.emojiCompat_metadataVersion", this.f4255c.e());
            editorInfo.extras.putBoolean("android.support.text.emoji.emojiCompat_replaceAll", this.f4257a.f4247h);
        }

        void f(m mVar) {
            if (mVar == null) {
                this.f4257a.p(new IllegalArgumentException("metadataRepo cannot be null"));
                return;
            }
            this.f4255c = mVar;
            m mVar2 = this.f4255c;
            j jVar = this.f4257a.f4246g;
            InterfaceC0111e interfaceC0111e = this.f4257a.f4253n;
            e eVar = this.f4257a;
            this.f4254b = new androidx.emoji2.text.h(mVar2, jVar, interfaceC0111e, eVar.f4248i, eVar.f4249j, androidx.emoji2.text.g.a());
            this.f4257a.q();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class b {

        /* renamed from: a, reason: collision with root package name */
        final e f4257a;

        b(e eVar) {
            this.f4257a = eVar;
        }

        abstract int a(CharSequence charSequence, int i10);

        abstract int b(CharSequence charSequence, int i10);

        abstract void c();

        abstract CharSequence d(CharSequence charSequence, int i10, int i11, int i12, boolean z10);

        abstract void e(EditorInfo editorInfo);
    }

    /* loaded from: classes.dex */
    public static abstract class c {

        /* renamed from: a, reason: collision with root package name */
        final h f4258a;

        /* renamed from: b, reason: collision with root package name */
        j f4259b;

        /* renamed from: c, reason: collision with root package name */
        boolean f4260c;

        /* renamed from: d, reason: collision with root package name */
        boolean f4261d;

        /* renamed from: e, reason: collision with root package name */
        int[] f4262e;

        /* renamed from: f, reason: collision with root package name */
        Set f4263f;

        /* renamed from: g, reason: collision with root package name */
        boolean f4264g;

        /* renamed from: h, reason: collision with root package name */
        int f4265h = -16711936;

        /* renamed from: i, reason: collision with root package name */
        int f4266i = 0;

        /* renamed from: j, reason: collision with root package name */
        InterfaceC0111e f4267j = new androidx.emoji2.text.d();

        /* JADX INFO: Access modifiers changed from: protected */
        public c(h hVar) {
            s3.i.h(hVar, "metadataLoader cannot be null.");
            this.f4258a = hVar;
        }

        /* JADX INFO: Access modifiers changed from: protected */
        public final h a() {
            return this.f4258a;
        }

        public c b(int i10) {
            this.f4266i = i10;
            return this;
        }
    }

    /* loaded from: classes.dex */
    public static class d implements j {
        @Override // androidx.emoji2.text.e.j
        public androidx.emoji2.text.i a(o oVar) {
            return new p(oVar);
        }
    }

    /* renamed from: androidx.emoji2.text.e$e, reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public interface InterfaceC0111e {
        boolean a(CharSequence charSequence, int i10, int i11, int i12);
    }

    /* loaded from: classes.dex */
    public static abstract class f {
        public void a(Throwable th2) {
        }

        public void b() {
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class g implements Runnable {

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

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

        /* renamed from: r, reason: collision with root package name */
        private final int f4270r;

        g(f fVar, int i10) {
            this(Arrays.asList((f) s3.i.h(fVar, "initCallback cannot be null")), i10, null);
        }

        @Override // java.lang.Runnable
        public void run() {
            int size = this.f4268p.size();
            int i10 = 0;
            if (this.f4270r != 1) {
                while (i10 < size) {
                    ((f) this.f4268p.get(i10)).a(this.f4269q);
                    i10++;
                }
            } else {
                while (i10 < size) {
                    ((f) this.f4268p.get(i10)).b();
                    i10++;
                }
            }
        }

        g(Collection collection, int i10) {
            this(collection, i10, null);
        }

        g(Collection collection, int i10, Throwable th2) {
            s3.i.h(collection, "initCallbacks cannot be null");
            this.f4268p = new ArrayList(collection);
            this.f4270r = i10;
            this.f4269q = th2;
        }
    }

    /* loaded from: classes.dex */
    public interface h {
        void a(i iVar);
    }

    /* loaded from: classes.dex */
    public static abstract class i {
        public abstract void a(Throwable th2);

        public abstract void b(m mVar);
    }

    /* loaded from: classes.dex */
    public interface j {
        androidx.emoji2.text.i a(o oVar);
    }

    private e(c cVar) {
        this.f4247h = cVar.f4260c;
        this.f4248i = cVar.f4261d;
        this.f4249j = cVar.f4262e;
        this.f4250k = cVar.f4264g;
        this.f4251l = cVar.f4265h;
        this.f4245f = cVar.f4258a;
        this.f4252m = cVar.f4266i;
        this.f4253n = cVar.f4267j;
        androidx.collection.b bVar = new androidx.collection.b();
        this.f4241b = bVar;
        j jVar = cVar.f4259b;
        this.f4246g = jVar == null ? new d() : jVar;
        Set set = cVar.f4263f;
        if (set != null && !set.isEmpty()) {
            bVar.addAll(cVar.f4263f);
        }
        this.f4244e = new a(this);
        o();
    }

    public static e c() {
        e eVar;
        synchronized (f4237o) {
            eVar = f4239q;
            s3.i.i(eVar != null, "EmojiCompat is not initialized.\n\nYou must initialize EmojiCompat prior to referencing the EmojiCompat instance.\n\nThe most likely cause of this error is disabling the EmojiCompatInitializer\neither explicitly in AndroidManifest.xml, or by including\nandroidx.emoji2:emoji2-bundled.\n\nAutomatic initialization is typically performed by EmojiCompatInitializer. If\nyou are not expecting to initialize EmojiCompat manually in your application,\nplease check to ensure it has not been removed from your APK's manifest. You can\ndo this in Android Studio using Build > Analyze APK.\n\nIn the APK Analyzer, ensure that the startup entry for\nEmojiCompatInitializer and InitializationProvider is present in\n AndroidManifest.xml. If it is missing or contains tools:node=\"remove\", and you\nintend to use automatic configuration, verify:\n\n  1. Your application does not include emoji2-bundled\n  2. All modules do not contain an exclusion manifest rule for\n     EmojiCompatInitializer or InitializationProvider. For more information\n     about manifest exclusions see the documentation for the androidx startup\n     library.\n\nIf you intend to use emoji2-bundled, please call EmojiCompat.init. You can\nlearn more in the documentation for BundledEmojiCompatConfig.\n\nIf you intended to perform manual configuration, it is recommended that you call\nEmojiCompat.init immediately on application startup.\n\nIf you still cannot resolve this issue, please open a bug with your specific\nconfiguration to help improve error message.");
        }
        return eVar;
    }

    public static boolean h(InputConnection inputConnection, Editable editable, int i10, int i11, boolean z10) {
        return androidx.emoji2.text.h.d(inputConnection, editable, i10, i11, z10);
    }

    public static boolean i(Editable editable, int i10, KeyEvent keyEvent) {
        return androidx.emoji2.text.h.e(editable, i10, keyEvent);
    }

    public static e j(c cVar) {
        e eVar = f4239q;
        if (eVar == null) {
            synchronized (f4237o) {
                eVar = f4239q;
                if (eVar == null) {
                    eVar = new e(cVar);
                    f4239q = eVar;
                }
            }
        }
        return eVar;
    }

    public static boolean k() {
        return f4239q != null;
    }

    private boolean m() {
        return g() == 1;
    }

    private void o() {
        this.f4240a.writeLock().lock();
        try {
            if (this.f4252m == 0) {
                this.f4242c = 0;
            }
            this.f4240a.writeLock().unlock();
            if (g() == 0) {
                this.f4244e.c();
            }
        } catch (Throwable th2) {
            this.f4240a.writeLock().unlock();
            throw th2;
        }
    }

    public int d(CharSequence charSequence, int i10) {
        return this.f4244e.a(charSequence, i10);
    }

    public int e() {
        return this.f4251l;
    }

    public int f(CharSequence charSequence, int i10) {
        return this.f4244e.b(charSequence, i10);
    }

    public int g() {
        this.f4240a.readLock().lock();
        try {
            return this.f4242c;
        } finally {
            this.f4240a.readLock().unlock();
        }
    }

    public boolean l() {
        return this.f4250k;
    }

    public void n() {
        s3.i.i(this.f4252m == 1, "Set metadataLoadStrategy to LOAD_STRATEGY_MANUAL to execute manual loading");
        if (m()) {
            return;
        }
        this.f4240a.writeLock().lock();
        try {
            if (this.f4242c == 0) {
                return;
            }
            this.f4242c = 0;
            this.f4240a.writeLock().unlock();
            this.f4244e.c();
        } finally {
            this.f4240a.writeLock().unlock();
        }
    }

    void p(Throwable th2) {
        ArrayList arrayList = new ArrayList();
        this.f4240a.writeLock().lock();
        try {
            this.f4242c = 2;
            arrayList.addAll(this.f4241b);
            this.f4241b.clear();
            this.f4240a.writeLock().unlock();
            this.f4243d.post(new g(arrayList, this.f4242c, th2));
        } catch (Throwable th3) {
            this.f4240a.writeLock().unlock();
            throw th3;
        }
    }

    void q() {
        ArrayList arrayList = new ArrayList();
        this.f4240a.writeLock().lock();
        try {
            this.f4242c = 1;
            arrayList.addAll(this.f4241b);
            this.f4241b.clear();
            this.f4240a.writeLock().unlock();
            this.f4243d.post(new g(arrayList, this.f4242c));
        } catch (Throwable th2) {
            this.f4240a.writeLock().unlock();
            throw th2;
        }
    }

    public CharSequence r(CharSequence charSequence) {
        return s(charSequence, 0, charSequence == null ? 0 : charSequence.length());
    }

    public CharSequence s(CharSequence charSequence, int i10, int i11) {
        return t(charSequence, i10, i11, Api.BaseClientBuilder.API_PRIORITY_OTHER);
    }

    public CharSequence t(CharSequence charSequence, int i10, int i11, int i12) {
        return u(charSequence, i10, i11, i12, 0);
    }

    public CharSequence u(CharSequence charSequence, int i10, int i11, int i12, int i13) {
        boolean z10;
        s3.i.i(m(), "Not initialized yet");
        s3.i.e(i10, "start cannot be negative");
        s3.i.e(i11, "end cannot be negative");
        s3.i.e(i12, "maxEmojiCount cannot be negative");
        s3.i.b(i10 <= i11, "start should be <= than end");
        if (charSequence == null) {
            return null;
        }
        s3.i.b(i10 <= charSequence.length(), "start should be < than charSequence length");
        s3.i.b(i11 <= charSequence.length(), "end should be < than charSequence length");
        if (charSequence.length() == 0 || i10 == i11) {
            return charSequence;
        }
        if (i13 != 1) {
            z10 = i13 != 2 ? this.f4247h : false;
        } else {
            z10 = true;
        }
        return this.f4244e.d(charSequence, i10, i11, i12, z10);
    }

    public void v(f fVar) {
        s3.i.h(fVar, "initCallback cannot be null");
        this.f4240a.writeLock().lock();
        try {
            if (this.f4242c != 1 && this.f4242c != 2) {
                this.f4241b.add(fVar);
            }
            this.f4243d.post(new g(fVar, this.f4242c));
        } finally {
            this.f4240a.writeLock().unlock();
        }
    }

    public void w(f fVar) {
        s3.i.h(fVar, "initCallback cannot be null");
        this.f4240a.writeLock().lock();
        try {
            this.f4241b.remove(fVar);
        } finally {
            this.f4240a.writeLock().unlock();
        }
    }

    public void x(EditorInfo editorInfo) {
        if (!m() || editorInfo == null) {
            return;
        }
        if (editorInfo.extras == null) {
            editorInfo.extras = new Bundle();
        }
        this.f4244e.e(editorInfo);
    }
}
