package androidx.emoji2.text;

import android.os.Build;
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 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;

/* JADX INFO: compiled from: EmojiCompat.java */
/* JADX INFO: loaded from: classes.dex */
public class e {

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private static final Object f2053n = new Object();

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private static volatile e f2054o;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Set<AbstractC0029e> f2056b;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    final g f2060f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    final boolean f2061g;

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    final int[] f2063i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private final boolean f2064j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private final int f2065k;

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

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private final d f2067m;

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

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

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

    /* JADX INFO: compiled from: EmojiCompat.java */
    private static final class a extends b {

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

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

        /* JADX INFO: renamed from: androidx.emoji2.text.e$a$a, reason: collision with other inner class name */
        /* JADX INFO: compiled from: EmojiCompat.java */
        class C0028a extends h {
            C0028a() {
            }

            @Override // androidx.emoji2.text.e.h
            public void a(Throwable th) {
                a.this.f2071a.m(th);
            }

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

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

        @Override // androidx.emoji2.text.e.b
        void a() {
            try {
                this.f2071a.f2060f.a(new C0028a());
            } catch (Throwable th) {
                this.f2071a.m(th);
            }
        }

        @Override // androidx.emoji2.text.e.b
        CharSequence b(CharSequence charSequence, int i3, int i4, int i5, boolean z2) {
            return this.f2068b.h(charSequence, i3, i4, i5, z2);
        }

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

        void d(m mVar) {
            if (mVar == null) {
                this.f2071a.m(new IllegalArgumentException("metadataRepo cannot be null"));
                return;
            }
            this.f2069c = mVar;
            m mVar2 = this.f2069c;
            i iVar = new i();
            d dVar = this.f2071a.f2067m;
            e eVar = this.f2071a;
            this.f2068b = new androidx.emoji2.text.h(mVar2, iVar, dVar, eVar.f2062h, eVar.f2063i);
            this.f2071a.n();
        }
    }

    /* JADX INFO: compiled from: EmojiCompat.java */
    private static class b {

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

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

        void a() {
            this.f2071a.n();
        }

        CharSequence b(CharSequence charSequence, int i3, int i4, int i5, boolean z2) {
            return charSequence;
        }

        void c(EditorInfo editorInfo) {
        }
    }

    /* JADX INFO: compiled from: EmojiCompat.java */
    public static abstract class c {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final g f2072a;

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

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        boolean f2074c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        int[] f2075d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        Set<AbstractC0029e> f2076e;

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

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        int f2078g = -16711936;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        int f2079h = 0;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        d f2080i = new androidx.emoji2.text.d();

        protected c(g gVar) {
            androidx.core.util.h.g(gVar, "metadataLoader cannot be null.");
            this.f2072a = gVar;
        }

        protected final g a() {
            return this.f2072a;
        }

        public c b(int i3) {
            this.f2079h = i3;
            return this;
        }
    }

    /* JADX INFO: compiled from: EmojiCompat.java */
    public interface d {
        boolean a(CharSequence charSequence, int i3, int i4, int i5);
    }

    /* JADX INFO: renamed from: androidx.emoji2.text.e$e, reason: collision with other inner class name */
    /* JADX INFO: compiled from: EmojiCompat.java */
    public static abstract class AbstractC0029e {
        public void a(Throwable th) {
        }

        public void b() {
        }
    }

    /* JADX INFO: compiled from: EmojiCompat.java */
    private static class f implements Runnable {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private final List<AbstractC0029e> f2081e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private final Throwable f2082f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private final int f2083g;

        f(AbstractC0029e abstractC0029e, int i3) {
            this(Arrays.asList((AbstractC0029e) androidx.core.util.h.g(abstractC0029e, "initCallback cannot be null")), i3, null);
        }

        @Override // java.lang.Runnable
        public void run() {
            int size = this.f2081e.size();
            int i3 = 0;
            if (this.f2083g != 1) {
                while (i3 < size) {
                    this.f2081e.get(i3).a(this.f2082f);
                    i3++;
                }
            } else {
                while (i3 < size) {
                    this.f2081e.get(i3).b();
                    i3++;
                }
            }
        }

        f(Collection<AbstractC0029e> collection, int i3) {
            this(collection, i3, null);
        }

        f(Collection<AbstractC0029e> collection, int i3, Throwable th) {
            androidx.core.util.h.g(collection, "initCallbacks cannot be null");
            this.f2081e = new ArrayList(collection);
            this.f2083g = i3;
            this.f2082f = th;
        }
    }

    /* JADX INFO: compiled from: EmojiCompat.java */
    public interface g {
        void a(h hVar);
    }

    /* JADX INFO: compiled from: EmojiCompat.java */
    public static abstract class h {
        public abstract void a(Throwable th);

        public abstract void b(m mVar);
    }

    /* JADX INFO: compiled from: EmojiCompat.java */
    static class i {
        i() {
        }

        androidx.emoji2.text.i a(androidx.emoji2.text.g gVar) {
            return new o(gVar);
        }
    }

    private e(c cVar) {
        this.f2061g = cVar.f2073b;
        this.f2062h = cVar.f2074c;
        this.f2063i = cVar.f2075d;
        this.f2064j = cVar.f2077f;
        this.f2065k = cVar.f2078g;
        this.f2060f = cVar.f2072a;
        this.f2066l = cVar.f2079h;
        this.f2067m = cVar.f2080i;
        j.b bVar = new j.b();
        this.f2056b = bVar;
        Set<AbstractC0029e> set = cVar.f2076e;
        if (set != null && !set.isEmpty()) {
            bVar.addAll(cVar.f2076e);
        }
        this.f2059e = Build.VERSION.SDK_INT < 19 ? new b(this) : new a(this);
        l();
    }

    public static e b() {
        e eVar;
        synchronized (f2053n) {
            eVar = f2054o;
            androidx.core.util.h.h(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 e(InputConnection inputConnection, Editable editable, int i3, int i4, boolean z2) {
        if (Build.VERSION.SDK_INT >= 19) {
            return androidx.emoji2.text.h.c(inputConnection, editable, i3, i4, z2);
        }
        return false;
    }

    public static boolean f(Editable editable, int i3, KeyEvent keyEvent) {
        if (Build.VERSION.SDK_INT >= 19) {
            return androidx.emoji2.text.h.d(editable, i3, keyEvent);
        }
        return false;
    }

    public static e g(c cVar) {
        e eVar = f2054o;
        if (eVar == null) {
            synchronized (f2053n) {
                eVar = f2054o;
                if (eVar == null) {
                    eVar = new e(cVar);
                    f2054o = eVar;
                }
            }
        }
        return eVar;
    }

    public static boolean h() {
        return f2054o != null;
    }

    private boolean j() {
        return d() == 1;
    }

    private void l() {
        this.f2055a.writeLock().lock();
        try {
            if (this.f2066l == 0) {
                this.f2057c = 0;
            }
            this.f2055a.writeLock().unlock();
            if (d() == 0) {
                this.f2059e.a();
            }
        } catch (Throwable th) {
            this.f2055a.writeLock().unlock();
            throw th;
        }
    }

    public int c() {
        return this.f2065k;
    }

    public int d() {
        this.f2055a.readLock().lock();
        try {
            return this.f2057c;
        } finally {
            this.f2055a.readLock().unlock();
        }
    }

    public boolean i() {
        return this.f2064j;
    }

    public void k() {
        androidx.core.util.h.h(this.f2066l == 1, "Set metadataLoadStrategy to LOAD_STRATEGY_MANUAL to execute manual loading");
        if (j()) {
            return;
        }
        this.f2055a.writeLock().lock();
        try {
            if (this.f2057c == 0) {
                return;
            }
            this.f2057c = 0;
            this.f2055a.writeLock().unlock();
            this.f2059e.a();
        } finally {
            this.f2055a.writeLock().unlock();
        }
    }

    void m(Throwable th) {
        ArrayList arrayList = new ArrayList();
        this.f2055a.writeLock().lock();
        try {
            this.f2057c = 2;
            arrayList.addAll(this.f2056b);
            this.f2056b.clear();
            this.f2055a.writeLock().unlock();
            this.f2058d.post(new f(arrayList, this.f2057c, th));
        } catch (Throwable th2) {
            this.f2055a.writeLock().unlock();
            throw th2;
        }
    }

    void n() {
        ArrayList arrayList = new ArrayList();
        this.f2055a.writeLock().lock();
        try {
            this.f2057c = 1;
            arrayList.addAll(this.f2056b);
            this.f2056b.clear();
            this.f2055a.writeLock().unlock();
            this.f2058d.post(new f(arrayList, this.f2057c));
        } catch (Throwable th) {
            this.f2055a.writeLock().unlock();
            throw th;
        }
    }

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

    public CharSequence p(CharSequence charSequence, int i3, int i4) {
        return q(charSequence, i3, i4, Integer.MAX_VALUE);
    }

    public CharSequence q(CharSequence charSequence, int i3, int i4, int i5) {
        return r(charSequence, i3, i4, i5, 0);
    }

    public CharSequence r(CharSequence charSequence, int i3, int i4, int i5, int i6) {
        boolean z2;
        androidx.core.util.h.h(j(), "Not initialized yet");
        androidx.core.util.h.d(i3, "start cannot be negative");
        androidx.core.util.h.d(i4, "end cannot be negative");
        androidx.core.util.h.d(i5, "maxEmojiCount cannot be negative");
        androidx.core.util.h.a(i3 <= i4, "start should be <= than end");
        if (charSequence == null) {
            return null;
        }
        androidx.core.util.h.a(i3 <= charSequence.length(), "start should be < than charSequence length");
        androidx.core.util.h.a(i4 <= charSequence.length(), "end should be < than charSequence length");
        if (charSequence.length() == 0 || i3 == i4) {
            return charSequence;
        }
        if (i6 != 1) {
            z2 = i6 != 2 ? this.f2061g : false;
        } else {
            z2 = true;
        }
        return this.f2059e.b(charSequence, i3, i4, i5, z2);
    }

    public void s(AbstractC0029e abstractC0029e) {
        androidx.core.util.h.g(abstractC0029e, "initCallback cannot be null");
        this.f2055a.writeLock().lock();
        try {
            if (this.f2057c == 1 || this.f2057c == 2) {
                this.f2058d.post(new f(abstractC0029e, this.f2057c));
            } else {
                this.f2056b.add(abstractC0029e);
            }
        } finally {
            this.f2055a.writeLock().unlock();
        }
    }

    public void t(AbstractC0029e abstractC0029e) {
        androidx.core.util.h.g(abstractC0029e, "initCallback cannot be null");
        this.f2055a.writeLock().lock();
        try {
            this.f2056b.remove(abstractC0029e);
        } finally {
            this.f2055a.writeLock().unlock();
        }
    }

    public void u(EditorInfo editorInfo) {
        if (!j() || editorInfo == null) {
            return;
        }
        if (editorInfo.extras == null) {
            editorInfo.extras = new Bundle();
        }
        this.f2059e.c(editorInfo);
    }
}
