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

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

    /* renamed from: k, reason: collision with root package name */
    private static final a f4298k = new a();

    /* loaded from: classes.dex */
    public static class a {
        public Typeface a(Context context, g.b bVar) {
            return q3.g.a(context, null, new g.b[]{bVar});
        }

        public g.a b(Context context, q3.e eVar) {
            return q3.g.b(context, null, eVar);
        }

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

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

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

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

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

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

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

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

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

        /* renamed from: h, reason: collision with root package name */
        e.i f4306h;

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

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

        b(Context context, q3.e eVar, a aVar) {
            s3.i.h(context, "Context cannot be null");
            s3.i.h(eVar, "FontRequest cannot be null");
            this.f4299a = context.getApplicationContext();
            this.f4300b = eVar;
            this.f4301c = aVar;
        }

        private void b() {
            synchronized (this.f4302d) {
                this.f4306h = null;
                ContentObserver contentObserver = this.f4307i;
                if (contentObserver != null) {
                    this.f4301c.c(this.f4299a, contentObserver);
                    this.f4307i = null;
                }
                Handler handler = this.f4303e;
                if (handler != null) {
                    handler.removeCallbacks(this.f4308j);
                }
                this.f4303e = null;
                ThreadPoolExecutor threadPoolExecutor = this.f4305g;
                if (threadPoolExecutor != null) {
                    threadPoolExecutor.shutdown();
                }
                this.f4304f = null;
                this.f4305g = null;
            }
        }

        private g.b e() {
            try {
                g.a b10 = this.f4301c.b(this.f4299a, this.f4300b);
                if (b10.c() == 0) {
                    g.b[] b11 = b10.b();
                    if (b11 == null || b11.length == 0) {
                        throw new RuntimeException("fetchFonts failed (empty result)");
                    }
                    return b11[0];
                }
                throw new RuntimeException("fetchFonts failed (" + b10.c() + ")");
            } catch (PackageManager.NameNotFoundException e10) {
                throw new RuntimeException("provider not found", e10);
            }
        }

        @Override // androidx.emoji2.text.e.h
        public void a(e.i iVar) {
            s3.i.h(iVar, "LoaderCallback cannot be null");
            synchronized (this.f4302d) {
                this.f4306h = iVar;
            }
            d();
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public void c() {
            synchronized (this.f4302d) {
                if (this.f4306h == null) {
                    return;
                }
                try {
                    g.b e10 = e();
                    int b10 = e10.b();
                    if (b10 == 2) {
                        synchronized (this.f4302d) {
                        }
                    }
                    if (b10 != 0) {
                        throw new RuntimeException("fetchFonts result is not OK. (" + b10 + ")");
                    }
                    try {
                        androidx.core.os.m.a("EmojiCompat.FontRequestEmojiCompatConfig.buildTypeface");
                        Typeface a10 = this.f4301c.a(this.f4299a, e10);
                        ByteBuffer a11 = androidx.core.graphics.h.a(this.f4299a, null, e10.d());
                        if (a11 == null || a10 == null) {
                            throw new RuntimeException("Unable to open file.");
                        }
                        m b11 = m.b(a10, a11);
                        androidx.core.os.m.b();
                        synchronized (this.f4302d) {
                            e.i iVar = this.f4306h;
                            if (iVar != null) {
                                iVar.b(b11);
                            }
                        }
                        b();
                    } catch (Throwable th2) {
                        androidx.core.os.m.b();
                        throw th2;
                    }
                } catch (Throwable th3) {
                    synchronized (this.f4302d) {
                        e.i iVar2 = this.f4306h;
                        if (iVar2 != null) {
                            iVar2.a(th3);
                        }
                        b();
                    }
                }
            }
        }

        void d() {
            synchronized (this.f4302d) {
                if (this.f4306h == null) {
                    return;
                }
                if (this.f4304f == null) {
                    ThreadPoolExecutor b10 = androidx.emoji2.text.b.b("emojiCompat");
                    this.f4305g = b10;
                    this.f4304f = b10;
                }
                this.f4304f.execute(new Runnable() { // from class: androidx.emoji2.text.k
                    @Override // java.lang.Runnable
                    public final void run() {
                        j.b.this.c();
                    }
                });
            }
        }

        public void f(Executor executor) {
            synchronized (this.f4302d) {
                this.f4304f = executor;
            }
        }
    }

    public j(Context context, q3.e eVar) {
        super(new b(context, eVar, f4298k));
    }

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