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.C0229g;
import androidx.emoji2.text.C0247m;
import java.nio.ByteBuffer;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import p011d0.C0623v;
import p014e0.C0648e;
import p014e0.C0655g;
import p020g0.C0737f;
import p073z.C1904p0;

/* renamed from: androidx.emoji2.text.m */
/* loaded from: classes.dex */
public class C0247m extends C0229g.AbstractC0233c {

    /* renamed from: j */
    public static final C0248a f1123j = new C0248a();

    /* renamed from: androidx.emoji2.text.m$a */
    /* loaded from: classes.dex */
    public static class C0248a {
        /* renamed from: a */
        public Typeface m4339a(Context context, C0655g.C0657b c0657b) {
            return C0655g.m3198a(context, null, new C0655g.C0657b[]{c0657b});
        }

        /* renamed from: b */
        public C0655g.C0656a m4338b(Context context, C0648e c0648e) {
            return C0655g.m3197b(context, null, c0648e);
        }

        /* renamed from: c */
        public void m4337c(Context context, ContentObserver contentObserver) {
            context.getContentResolver().unregisterContentObserver(contentObserver);
        }
    }

    /* renamed from: androidx.emoji2.text.m$b */
    /* loaded from: classes.dex */
    public static class C0249b implements C0229g.InterfaceC0237g {

        /* renamed from: a */
        public final Context f1124a;

        /* renamed from: b */
        public final C0648e f1125b;

        /* renamed from: c */
        public final C0248a f1126c;

        /* renamed from: d */
        public final Object f1127d = new Object();

        /* renamed from: e */
        public Handler f1128e;

        /* renamed from: f */
        public Executor f1129f;

        /* renamed from: g */
        public ThreadPoolExecutor f1130g;

        /* renamed from: h */
        public C0229g.AbstractC0238h f1131h;

        /* renamed from: i */
        public ContentObserver f1132i;

        /* renamed from: j */
        public Runnable f1133j;

        public C0249b(Context context, C0648e c0648e, C0248a c0248a) {
            C0737f.m3032g(context, "Context cannot be null");
            C0737f.m3032g(c0648e, "FontRequest cannot be null");
            this.f1124a = context.getApplicationContext();
            this.f1125b = c0648e;
            this.f1126c = c0248a;
        }

        @Override // androidx.emoji2.text.C0229g.InterfaceC0237g
        /* renamed from: a */
        public void mo4336a(C0229g.AbstractC0238h abstractC0238h) {
            C0737f.m3032g(abstractC0238h, "LoaderCallback cannot be null");
            synchronized (this.f1127d) {
                this.f1131h = abstractC0238h;
            }
            m4333d();
        }

        /* renamed from: b */
        public final void m4335b() {
            synchronized (this.f1127d) {
                this.f1131h = null;
                ContentObserver contentObserver = this.f1132i;
                if (contentObserver != null) {
                    this.f1126c.m4337c(this.f1124a, contentObserver);
                    this.f1132i = null;
                }
                Handler handler = this.f1128e;
                if (handler != null) {
                    handler.removeCallbacks(this.f1133j);
                }
                this.f1128e = null;
                ThreadPoolExecutor threadPoolExecutor = this.f1130g;
                if (threadPoolExecutor != null) {
                    threadPoolExecutor.shutdown();
                }
                this.f1129f = null;
                this.f1130g = null;
            }
        }

        /* renamed from: c */
        public void m4334c() {
            synchronized (this.f1127d) {
                if (this.f1131h == null) {
                    return;
                }
                try {
                    C0655g.C0657b m4332e = m4332e();
                    int m3191b = m4332e.m3191b();
                    if (m3191b == 2) {
                        synchronized (this.f1127d) {
                        }
                    }
                    if (m3191b != 0) {
                        throw new RuntimeException("fetchFonts result is not OK. (" + m3191b + ")");
                    }
                    C0623v.m3248a("EmojiCompat.FontRequestEmojiCompatConfig.buildTypeface");
                    Typeface m4339a = this.f1126c.m4339a(this.f1124a, m4332e);
                    ByteBuffer m75f = C1904p0.m75f(this.f1124a, null, m4332e.m3189d());
                    if (m75f == null || m4339a == null) {
                        throw new RuntimeException("Unable to open file.");
                    }
                    C0255p m4321b = C0255p.m4321b(m4339a, m75f);
                    C0623v.m3247b();
                    synchronized (this.f1127d) {
                        C0229g.AbstractC0238h abstractC0238h = this.f1131h;
                        if (abstractC0238h != null) {
                            abstractC0238h.mo4374b(m4321b);
                        }
                    }
                    m4335b();
                } catch (Throwable th) {
                    synchronized (this.f1127d) {
                        C0229g.AbstractC0238h abstractC0238h2 = this.f1131h;
                        if (abstractC0238h2 != null) {
                            abstractC0238h2.mo4375a(th);
                        }
                        m4335b();
                    }
                }
            }
        }

        /* renamed from: d */
        public void m4333d() {
            synchronized (this.f1127d) {
                if (this.f1131h == null) {
                    return;
                }
                if (this.f1129f == null) {
                    ThreadPoolExecutor m4423b = C0219b.m4423b("emojiCompat");
                    this.f1130g = m4423b;
                    this.f1129f = m4423b;
                }
                this.f1129f.execute(new Runnable() { // from class: androidx.emoji2.text.n
                    @Override // java.lang.Runnable
                    public final void run() {
                        C0247m.C0249b.this.m4334c();
                    }
                });
            }
        }

        /* renamed from: e */
        public final C0655g.C0657b m4332e() {
            try {
                C0655g.C0656a m4338b = this.f1126c.m4338b(this.f1124a, this.f1125b);
                if (m4338b.m3193c() == 0) {
                    C0655g.C0657b[] m3194b = m4338b.m3194b();
                    if (m3194b == null || m3194b.length == 0) {
                        throw new RuntimeException("fetchFonts failed (empty result)");
                    }
                    return m3194b[0];
                }
                throw new RuntimeException("fetchFonts failed (" + m4338b.m3193c() + ")");
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException("provider not found", e);
            }
        }

        /* renamed from: f */
        public void m4331f(Executor executor) {
            synchronized (this.f1127d) {
                this.f1129f = executor;
            }
        }
    }

    public C0247m(Context context, C0648e c0648e) {
        super(new C0249b(context, c0648e, f1123j));
    }

    /* renamed from: c */
    public C0247m m4340c(Executor executor) {
        ((C0249b) m4379a()).m4331f(executor);
        return this;
    }
}