package androidx.emoji2.text;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.CheckResult;
import androidx.annotation.GuardedBy;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/* JADX INFO: loaded from: classes.dex */
public final class t {

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final Object f2266k = new Object();

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public static final Object f2267l = new Object();

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static volatile boolean f2268m;

    @Nullable
    @GuardedBy("INSTANCE_LOCK")
    private static volatile t sInstance;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ReentrantReadWriteLock f2269a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final androidx.collection.g f2270b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Handler f2272d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final k f2273e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final q f2274f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final m f2275g;

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final int f2277i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final i f2278j;

    @Nullable
    final int[] mEmojiAsDefaultStyleExceptions;

    public t(l lVar) {
        ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
        this.f2269a = reentrantReadWriteLock;
        this.f2271c = 3;
        this.f2276h = lVar.f2242b;
        this.mEmojiAsDefaultStyleExceptions = lVar.mEmojiAsDefaultStyleExceptions;
        this.f2274f = lVar.f2241a;
        int i10 = lVar.f2243c;
        this.f2277i = i10;
        this.f2278j = lVar.f2244d;
        this.f2272d = new Handler(Looper.getMainLooper());
        androidx.collection.g gVar = new androidx.collection.g(0);
        this.f2270b = gVar;
        this.f2275g = new m();
        Set<o> set = lVar.mInitCallbacks;
        if (set != null && !set.isEmpty()) {
            gVar.addAll(lVar.mInitCallbacks);
        }
        k kVar = new k(this);
        this.f2273e = kVar;
        reentrantReadWriteLock.writeLock().lock();
        if (i10 == 0) {
            try {
                this.f2271c = 0;
            } catch (Throwable th) {
                this.f2269a.writeLock().unlock();
                throw th;
            }
        }
        reentrantReadWriteLock.writeLock().unlock();
        if (b() == 0) {
            kVar.t();
        }
    }

    public static t a() {
        t tVar;
        synchronized (f2266k) {
            tVar = sInstance;
            p0.g.checkState(tVar != 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 tVar;
    }

    public static void c(l lVar) {
        if (sInstance == null) {
            synchronized (f2266k) {
                try {
                    if (sInstance == null) {
                        sInstance = new t(lVar);
                    }
                } finally {
                }
            }
        }
    }

    public static boolean d() {
        return sInstance != null;
    }

    @Nullable
    public static t init(@NonNull Context context) {
        return init(context, null);
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f558d})
    public static t reset(@Nullable t tVar) {
        t tVar2;
        synchronized (f2266k) {
            sInstance = tVar;
            tVar2 = sInstance;
        }
        return tVar2;
    }

    public final int b() {
        this.f2269a.readLock().lock();
        try {
            return this.f2271c;
        } finally {
            this.f2269a.readLock().unlock();
        }
    }

    public final void e() {
        p0.g.checkState(this.f2277i == 1, "Set metadataLoadStrategy to LOAD_STRATEGY_MANUAL to execute manual loading");
        if (b() == 1) {
            return;
        }
        this.f2269a.writeLock().lock();
        try {
            if (this.f2271c == 0) {
                return;
            }
            this.f2271c = 0;
            this.f2269a.writeLock().unlock();
            this.f2273e.t();
        } finally {
            this.f2269a.writeLock().unlock();
        }
    }

    public final void f() {
        ArrayList arrayList = new ArrayList();
        this.f2269a.writeLock().lock();
        try {
            this.f2271c = 1;
            arrayList.addAll(this.f2270b);
            this.f2270b.clear();
            this.f2269a.writeLock().unlock();
            this.f2272d.post(new p(arrayList, this.f2271c, null));
        } catch (Throwable th) {
            this.f2269a.writeLock().unlock();
            throw th;
        }
    }

    public final void g(o oVar) {
        p0.g.checkNotNull(oVar, "initCallback cannot be null");
        this.f2269a.writeLock().lock();
        try {
            if (this.f2271c == 1 || this.f2271c == 2) {
                this.f2272d.post(new p(Arrays.asList((o) p0.g.checkNotNull(oVar, "initCallback cannot be null")), this.f2271c, null));
            } else {
                this.f2270b.add(oVar);
            }
            this.f2269a.writeLock().unlock();
        } catch (Throwable th) {
            this.f2269a.writeLock().unlock();
            throw th;
        }
    }

    public void onMetadataLoadFailed(@Nullable Throwable th) {
        ArrayList arrayList = new ArrayList();
        this.f2269a.writeLock().lock();
        try {
            this.f2271c = 2;
            arrayList.addAll(this.f2270b);
            this.f2270b.clear();
            this.f2269a.writeLock().unlock();
            this.f2272d.post(new p(arrayList, this.f2271c, th));
        } catch (Throwable th2) {
            this.f2269a.writeLock().unlock();
            throw th2;
        }
    }

    @Nullable
    @CheckResult
    public CharSequence process(@Nullable CharSequence charSequence, @IntRange(from = 0) int i10, @IntRange(from = 0) int i11, @IntRange(from = 0) int i12, int i13) {
        p0.g.checkState(b() == 1, "Not initialized yet");
        p0.g.checkArgumentNonnegative(i10, "start cannot be negative");
        p0.g.checkArgumentNonnegative(i11, "end cannot be negative");
        p0.g.checkArgumentNonnegative(i12, "maxEmojiCount cannot be negative");
        p0.g.a(i10 <= i11, "start should be <= than end");
        if (charSequence == null) {
            return null;
        }
        p0.g.a(i10 <= charSequence.length(), "start should be < than charSequence length");
        p0.g.a(i11 <= charSequence.length(), "end should be < than charSequence length");
        if (charSequence.length() == 0 || i10 == i11) {
            return charSequence;
        }
        return this.f2273e.u(charSequence, i10, i11, i12, i13 == 1);
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f555a})
    public static t init(@NonNull Context context, @Nullable d dVar) {
        t tVar;
        if (f2268m) {
            return sInstance;
        }
        if (dVar == null) {
            dVar = new d(null);
        }
        l lVarCreate = dVar.create(context);
        synchronized (f2267l) {
            try {
                if (!f2268m) {
                    if (lVarCreate != null) {
                        c(lVarCreate);
                    }
                    f2268m = true;
                }
                tVar = sInstance;
            } catch (Throwable th) {
                throw th;
            }
        }
        return tVar;
    }

    @Nullable
    @CheckResult
    public CharSequence process(@Nullable CharSequence charSequence) {
        return process(charSequence, 0, charSequence == null ? 0 : charSequence.length());
    }

    @Nullable
    @CheckResult
    public CharSequence process(@Nullable CharSequence charSequence, @IntRange(from = 0) int i10, @IntRange(from = 0) int i11) {
        return process(charSequence, i10, i11, Integer.MAX_VALUE);
    }

    @Nullable
    @CheckResult
    public CharSequence process(@Nullable CharSequence charSequence, @IntRange(from = 0) int i10, @IntRange(from = 0) int i11, @IntRange(from = 0) int i12) {
        return process(charSequence, i10, i11, i12, 0);
    }
}
