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 androidx.core.provider.g;
import androidx.emoji2.text.e;
import java.nio.ByteBuffer;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/* JADX INFO: compiled from: FontRequestEmojiCompatConfig.java */
/* JADX INFO: loaded from: classes.dex */
public class j extends e.c {

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private static final a f2108j = new a();

    /* JADX INFO: compiled from: FontRequestEmojiCompatConfig.java */
    public static class a {
        public Typeface a(Context context, g.b bVar) throws PackageManager.NameNotFoundException {
            return androidx.core.provider.g.a(context, null, new g.b[]{bVar});
        }

        public g.a b(Context context, androidx.core.provider.e eVar) throws PackageManager.NameNotFoundException {
            return androidx.core.provider.g.b(context, null, eVar);
        }

        public void c(Context context, ContentObserver contentObserver) {
            context.getContentResolver().unregisterContentObserver(contentObserver);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX INFO: compiled from: FontRequestEmojiCompatConfig.java */
    static class b implements e.g {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final Context f2109a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final androidx.core.provider.e f2110b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private final a f2111c;

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

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private Handler f2113e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private Executor f2114f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        private ThreadPoolExecutor f2115g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        e.h f2116h;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        private ContentObserver f2117i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        private Runnable f2118j;

        b(Context context, androidx.core.provider.e eVar, a aVar) {
            androidx.core.util.h.g(context, "Context cannot be null");
            androidx.core.util.h.g(eVar, "FontRequest cannot be null");
            this.f2109a = context.getApplicationContext();
            this.f2110b = eVar;
            this.f2111c = aVar;
        }

        private void b() {
            synchronized (this.f2112d) {
                this.f2116h = null;
                ContentObserver contentObserver = this.f2117i;
                if (contentObserver != null) {
                    this.f2111c.c(this.f2109a, contentObserver);
                    this.f2117i = null;
                }
                Handler handler = this.f2113e;
                if (handler != null) {
                    handler.removeCallbacks(this.f2118j);
                }
                this.f2113e = null;
                ThreadPoolExecutor threadPoolExecutor = this.f2115g;
                if (threadPoolExecutor != null) {
                    threadPoolExecutor.shutdown();
                }
                this.f2114f = null;
                this.f2115g = null;
            }
        }

        private g.b e() {
            try {
                g.a aVarB = this.f2111c.b(this.f2109a, this.f2110b);
                if (aVarB.c() == 0) {
                    g.b[] bVarArrB = aVarB.b();
                    if (bVarArrB == null || bVarArrB.length == 0) {
                        throw new RuntimeException("fetchFonts failed (empty result)");
                    }
                    return bVarArrB[0];
                }
                throw new RuntimeException("fetchFonts failed (" + aVarB.c() + ")");
            } catch (PackageManager.NameNotFoundException e3) {
                throw new RuntimeException("provider not found", e3);
            }
        }

        @Override // androidx.emoji2.text.e.g
        public void a(e.h hVar) {
            androidx.core.util.h.g(hVar, "LoaderCallback cannot be null");
            synchronized (this.f2112d) {
                this.f2116h = hVar;
            }
            d();
        }

        void c() {
            synchronized (this.f2112d) {
                if (this.f2116h == null) {
                    return;
                }
                try {
                    g.b bVarE = e();
                    int iB = bVarE.b();
                    if (iB == 2) {
                        synchronized (this.f2112d) {
                        }
                    }
                    if (iB != 0) {
                        throw new RuntimeException("fetchFonts result is not OK. (" + iB + ")");
                    }
                    try {
                        androidx.core.os.f.a("EmojiCompat.FontRequestEmojiCompatConfig.buildTypeface");
                        Typeface typefaceA = this.f2111c.a(this.f2109a, bVarE);
                        ByteBuffer byteBufferF = androidx.core.graphics.l.f(this.f2109a, null, bVarE.d());
                        if (byteBufferF == null || typefaceA == null) {
                            throw new RuntimeException("Unable to open file.");
                        }
                        m mVarB = m.b(typefaceA, byteBufferF);
                        androidx.core.os.f.b();
                        synchronized (this.f2112d) {
                            e.h hVar = this.f2116h;
                            if (hVar != null) {
                                hVar.b(mVarB);
                            }
                        }
                        b();
                    } catch (Throwable th) {
                        androidx.core.os.f.b();
                        throw th;
                    }
                } catch (Throwable th2) {
                    synchronized (this.f2112d) {
                        e.h hVar2 = this.f2116h;
                        if (hVar2 != null) {
                            hVar2.a(th2);
                        }
                        b();
                    }
                }
            }
        }

        void d() {
            synchronized (this.f2112d) {
                if (this.f2116h == null) {
                    return;
                }
                if (this.f2114f == null) {
                    ThreadPoolExecutor threadPoolExecutorB = androidx.emoji2.text.b.b("emojiCompat");
                    this.f2115g = threadPoolExecutorB;
                    this.f2114f = threadPoolExecutorB;
                }
                this.f2114f.execute(new Runnable() { // from class: androidx.emoji2.text.k
                    @Override // java.lang.Runnable
                    public final void run() {
                        this.f2119e.c();
                    }
                });
            }
        }

        public void f(Executor executor) {
            synchronized (this.f2112d) {
                this.f2114f = executor;
            }
        }
    }

    public j(Context context, androidx.core.provider.e eVar) {
        super(new b(context, eVar, f2108j));
    }

    public j c(Executor executor) {
        ((b) a()).f(executor);
        return this;
    }
}
