package androidx.emoji2.text;

import android.content.Context;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Trace;
import androidx.emoji2.text.d;
import androidx.emoji2.text.j;
import defpackage.c1a;
import defpackage.eh3;
import defpackage.kf7;
import defpackage.m38;
import defpackage.mh3;
import defpackage.n41;
import defpackage.r0a;
import defpackage.uq9;
import defpackage.va1;
import java.nio.MappedByteBuffer;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public class j extends d.AbstractC0039d {
    public static final b d = new b();

    public static class a extends d {
    }

    @m38
    public static class b {
    }

    public static class c implements d.i {
        public final Context a;
        public final eh3 b;
        public final b c;
        public final Object d = new Object();
        public Handler e;
        public ThreadPoolExecutor f;
        public ThreadPoolExecutor g;
        public d.j h;
        public ContentObserver i;

        public c(Context context, eh3 eh3Var) {
            kf7.c(context, "Context cannot be null");
            this.a = context.getApplicationContext();
            this.b = eh3Var;
            this.c = j.d;
        }

        @Override // androidx.emoji2.text.d.i
        public final void a(d.j jVar) {
            synchronized (this.d) {
                this.h = jVar;
            }
            synchronized (this.d) {
                try {
                    if (this.h == null) {
                        return;
                    }
                    if (this.f == null) {
                        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, 1, 15L, TimeUnit.SECONDS, new LinkedBlockingDeque(), new va1("emojiCompat", 0));
                        threadPoolExecutor.allowCoreThreadTimeOut(true);
                        this.g = threadPoolExecutor;
                        this.f = threadPoolExecutor;
                    }
                    this.f.execute(new Runnable() { // from class: androidx.emoji2.text.k
                        @Override // java.lang.Runnable
                        public final void run() {
                            j.c cVar = this.a;
                            synchronized (cVar.d) {
                                try {
                                    if (cVar.h == null) {
                                        return;
                                    }
                                    try {
                                        mh3.c cVarC = cVar.c();
                                        int i = cVarC.f;
                                        if (i == 2) {
                                            synchronized (cVar.d) {
                                            }
                                        }
                                        if (i != 0) {
                                            throw new RuntimeException("fetchFonts result is not OK. (" + i + ")");
                                        }
                                        try {
                                            int i2 = uq9.a;
                                            Trace.beginSection("EmojiCompat.FontRequestEmojiCompatConfig.buildTypeface");
                                            j.b bVar = cVar.c;
                                            Context context = cVar.a;
                                            bVar.getClass();
                                            Typeface typefaceA = r0a.a(context, new mh3.c[]{cVarC}, 0);
                                            MappedByteBuffer mappedByteBufferE = c1a.e(cVar.a, cVarC.a);
                                            if (mappedByteBufferE == null || typefaceA == null) {
                                                throw new RuntimeException("Unable to open file.");
                                            }
                                            try {
                                                Trace.beginSection("EmojiCompat.MetadataRepo.create");
                                                n nVar = new n(typefaceA, m.a(mappedByteBufferE));
                                                Trace.endSection();
                                                Trace.endSection();
                                                synchronized (cVar.d) {
                                                    try {
                                                        d.j jVar2 = cVar.h;
                                                        if (jVar2 != null) {
                                                            jVar2.b(nVar);
                                                        }
                                                    } finally {
                                                    }
                                                }
                                                cVar.b();
                                            } finally {
                                                int i3 = uq9.a;
                                                Trace.endSection();
                                            }
                                        } catch (Throwable th) {
                                            throw th;
                                        }
                                    } catch (Throwable th2) {
                                        synchronized (cVar.d) {
                                            try {
                                                d.j jVar3 = cVar.h;
                                                if (jVar3 != null) {
                                                    jVar3.a(th2);
                                                }
                                                cVar.b();
                                            } finally {
                                            }
                                        }
                                    }
                                } finally {
                                }
                            }
                        }
                    });
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        public final void b() {
            synchronized (this.d) {
                try {
                    this.h = null;
                    ContentObserver contentObserver = this.i;
                    if (contentObserver != null) {
                        b bVar = this.c;
                        Context context = this.a;
                        bVar.getClass();
                        context.getContentResolver().unregisterContentObserver(contentObserver);
                        this.i = null;
                    }
                    Handler handler = this.e;
                    if (handler != null) {
                        handler.removeCallbacks(null);
                    }
                    this.e = null;
                    ThreadPoolExecutor threadPoolExecutor = this.g;
                    if (threadPoolExecutor != null) {
                        threadPoolExecutor.shutdown();
                    }
                    this.f = null;
                    this.g = null;
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        public final mh3.c c() {
            try {
                b bVar = this.c;
                Context context = this.a;
                eh3 eh3Var = this.b;
                bVar.getClass();
                mh3.b bVarA = mh3.a(context, eh3Var);
                int i = bVarA.a;
                if (i != 0) {
                    throw new RuntimeException(n41.i(i, "fetchFonts failed (", ")"));
                }
                mh3.c[] cVarArr = (mh3.c[]) bVarA.b.get(0);
                if (cVarArr == null || cVarArr.length == 0) {
                    throw new RuntimeException("fetchFonts failed (empty result)");
                }
                return cVarArr[0];
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException("provider not found", e);
            }
        }
    }

    public static abstract class d {
    }
}
