package n0;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Trace;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.collection.s;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public abstract class i {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final androidx.collection.n f30742a = new androidx.collection.n(16);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final ThreadPoolExecutor f30743b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final Object f30744c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final s f30745d;

    static {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, 1, 10000, TimeUnit.MILLISECONDS, new LinkedBlockingDeque(), new o());
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        f30743b = threadPoolExecutor;
        f30744c = new Object();
        f30745d = new s(0);
    }

    public static String a(int i10, List list) {
        StringBuilder sb = new StringBuilder();
        for (int i11 = 0; i11 < list.size(); i11++) {
            sb.append(((e) list.get(i11)).f30732e);
            sb.append("-");
            sb.append(i10);
            if (i11 < list.size() - 1) {
                sb.append(";");
            }
        }
        return sb.toString();
    }

    public static h b(String str, Context context, List list, int i10) {
        int i11;
        Trace.beginSection(androidx.datastore.preferences.core.c.t("getFontSync"));
        androidx.collection.n nVar = f30742a;
        try {
            Typeface typeface = (Typeface) nVar.b(str);
            if (typeface != null) {
                return new h(typeface);
            }
            j fontFamilyResult = d.getFontFamilyResult(context, list, null);
            int i12 = fontFamilyResult.f30746a;
            List list2 = fontFamilyResult.f30747b;
            if (i12 != 0) {
                i11 = i12 != 1 ? -3 : -2;
            } else {
                k[] kVarArr = (k[]) list2.get(0);
                if (kVarArr == null || kVarArr.length == 0) {
                    i11 = 1;
                } else {
                    int length = kVarArr.length;
                    int i13 = 0;
                    while (true) {
                        if (i13 >= length) {
                            i11 = 0;
                            break;
                        }
                        int i14 = kVarArr[i13].f30752e;
                        if (i14 == 0) {
                            i13++;
                        } else if (i14 >= 0) {
                            i11 = i14;
                        }
                    }
                }
            }
            if (i11 != 0) {
                return new h(i11);
            }
            Typeface typefaceCreateFromFontInfo = (list2.size() <= 1 || Build.VERSION.SDK_INT < 29) ? j0.i.createFromFontInfo(context, null, (k[]) list2.get(0), i10) : j0.i.createFromFontInfoWithFallback(context, null, list2, i10);
            if (typefaceCreateFromFontInfo == null) {
                return new h(-3);
            }
            nVar.c(str, typefaceCreateFromFontInfo);
            return new h(typefaceCreateFromFontInfo);
        } catch (PackageManager.NameNotFoundException unused) {
            return new h(-1);
        } finally {
            Trace.endSection();
        }
    }

    public static Typeface requestFontAsync(@NonNull Context context, @NonNull List<e> list, int i10, @Nullable Executor executor, @NonNull b bVar) {
        String strA = a(i10, list);
        Typeface typeface = (Typeface) f30742a.b(strA);
        if (typeface != null) {
            bVar.a(new h(typeface));
            return typeface;
        }
        g gVar = new g(bVar, 0);
        synchronized (f30744c) {
            try {
                s sVar = f30745d;
                ArrayList arrayList = (ArrayList) sVar.get(strA);
                if (arrayList != null) {
                    arrayList.add(gVar);
                    return null;
                }
                ArrayList arrayList2 = new ArrayList();
                arrayList2.add(gVar);
                sVar.put(strA, arrayList2);
                f fVar = new f(strA, context, list, i10, 1);
                if (executor == null) {
                    executor = f30743b;
                }
                executor.execute(new a(Looper.myLooper() == null ? new Handler(Looper.getMainLooper()) : new Handler(), fVar, new g(strA, 1), 2));
                return null;
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
