package j0;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Build;
import android.os.CancellationSignal;
import android.os.Handler;
import android.os.Trace;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final p f28509a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final androidx.collection.n f28510b;

    static {
        Trace.beginSection(androidx.datastore.preferences.core.c.t("TypefaceCompat static init"));
        int i10 = Build.VERSION.SDK_INT;
        if (i10 >= 29) {
            f28509a = new n();
        } else if (i10 >= 28) {
            f28509a = new m();
        } else if (i10 >= 26) {
            f28509a = new l();
        } else if (k.f28518d != null) {
            f28509a = new k();
        } else {
            f28509a = new j();
        }
        f28510b = new androidx.collection.n(16);
        Trace.endSection();
    }

    public static String a(Resources resources, int i10, String str, int i11, int i12) {
        return resources.getResourcePackageName(i10) + '-' + str + '-' + i11 + '-' + i10 + '-' + i12;
    }

    @NonNull
    public static Typeface create(@NonNull Context context, @Nullable Typeface typeface, int i10) {
        if (context != null) {
            return Typeface.create(typeface, i10);
        }
        throw new IllegalArgumentException("Context cannot be null");
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f557c})
    public static Typeface createFromFontInfo(@NonNull Context context, @Nullable CancellationSignal cancellationSignal, @NonNull n0.k[] kVarArr, int i10) {
        Trace.beginSection(androidx.datastore.preferences.core.c.t("TypefaceCompat.createFromFontInfo"));
        try {
            return f28509a.createFromFontInfo(context, cancellationSignal, kVarArr, i10);
        } finally {
            Trace.endSection();
        }
    }

    @Nullable
    @RequiresApi(29)
    @RestrictTo({RestrictTo.Scope.f555a})
    public static Typeface createFromFontInfoWithFallback(@NonNull Context context, @Nullable CancellationSignal cancellationSignal, @NonNull List<n0.k[]> list, int i10) {
        Trace.beginSection(androidx.datastore.preferences.core.c.t("TypefaceCompat.createFromFontInfoWithFallback"));
        try {
            return f28509a.createFromFontInfoWithFallback(context, cancellationSignal, list, i10);
        } finally {
            Trace.endSection();
        }
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f555a})
    public static Typeface createFromResourcesFamilyXml(@NonNull Context context, @NonNull i0.e eVar, @NonNull Resources resources, int i10, @Nullable String str, int i11, int i12, @Nullable i0.p pVar, @Nullable Handler handler, boolean z10) {
        Typeface typefaceCreateFromFontFamilyFilesResourceEntry;
        List listUnmodifiableList;
        if (eVar instanceof i0.h) {
            i0.h hVar = (i0.h) eVar;
            Typeface systemFontFamily = getSystemFontFamily(hVar.getSystemFontFamilyName());
            if (systemFontFamily != null) {
                if (pVar != null) {
                    pVar.callbackSuccessAsync(systemFontFamily, handler);
                }
                return systemFontFamily;
            }
            boolean z11 = !z10 ? pVar != null : hVar.f26499c != 0;
            int i13 = z10 ? hVar.f26498b : -1;
            Handler handler2 = i0.p.getHandler(handler);
            h hVar2 = new h(pVar);
            n0.e fallbackRequest = hVar.getFallbackRequest();
            n0.e eVar2 = hVar.f26497a;
            if (fallbackRequest != null) {
                Object[] objArr = {eVar2, hVar.getFallbackRequest()};
                ArrayList arrayList = new ArrayList(2);
                for (int i14 = 0; i14 < 2; i14++) {
                    Object obj = objArr[i14];
                    Objects.requireNonNull(obj);
                    arrayList.add(obj);
                }
                listUnmodifiableList = Collections.unmodifiableList(arrayList);
            } else {
                ArrayList arrayList2 = new ArrayList(1);
                Object obj2 = new Object[]{eVar2}[0];
                Objects.requireNonNull(obj2);
                arrayList2.add(obj2);
                listUnmodifiableList = Collections.unmodifiableList(arrayList2);
            }
            typefaceCreateFromFontFamilyFilesResourceEntry = n0.m.requestFont(context, (List<n0.e>) listUnmodifiableList, i12, z11, i13, handler2, hVar2);
        } else {
            typefaceCreateFromFontFamilyFilesResourceEntry = f28509a.createFromFontFamilyFilesResourceEntry(context, (i0.f) eVar, resources, i12);
            if (pVar != null) {
                if (typefaceCreateFromFontFamilyFilesResourceEntry != null) {
                    pVar.callbackSuccessAsync(typefaceCreateFromFontFamilyFilesResourceEntry, handler);
                } else {
                    pVar.callbackFailAsync(-3, handler);
                }
            }
        }
        if (typefaceCreateFromFontFamilyFilesResourceEntry != null) {
            f28510b.c(a(resources, i10, str, i11, i12), typefaceCreateFromFontFamilyFilesResourceEntry);
        }
        return typefaceCreateFromFontFamilyFilesResourceEntry;
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f555a})
    public static Typeface createFromResourcesFontFile(@NonNull Context context, @NonNull Resources resources, int i10, String str, int i11, int i12) {
        Typeface typefaceCreateFromResourcesFontFile = f28509a.createFromResourcesFontFile(context, resources, i10, str, i12);
        if (typefaceCreateFromResourcesFontFile != null) {
            f28510b.c(a(resources, i10, str, i11, i12), typefaceCreateFromResourcesFontFile);
        }
        return typefaceCreateFromResourcesFontFile;
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f555a})
    public static Typeface findFromCache(@NonNull Resources resources, int i10, @Nullable String str, int i11, int i12) {
        return (Typeface) f28510b.b(a(resources, i10, str, i11, i12));
    }

    @Nullable
    private static Typeface getBestFontFromFamily(Context context, Typeface typeface, int i10) {
        p pVar = f28509a;
        i0.f fontFamily = pVar.getFontFamily(typeface);
        if (fontFamily == null) {
            return null;
        }
        return pVar.createFromFontFamilyFilesResourceEntry(context, fontFamily, context.getResources(), i10);
    }

    private static Typeface getSystemFontFamily(@Nullable String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }
        Typeface typefaceCreate = Typeface.create(str, 0);
        Typeface typefaceCreate2 = Typeface.create(Typeface.DEFAULT, 0);
        if (typefaceCreate == null || typefaceCreate.equals(typefaceCreate2)) {
            return null;
        }
        return typefaceCreate;
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f557c})
    @Deprecated
    public static Typeface findFromCache(@NonNull Resources resources, int i10, int i11) {
        return findFromCache(resources, i10, null, 0, i11);
    }

    @NonNull
    public static Typeface create(@NonNull Context context, @Nullable Typeface typeface, @IntRange(from = IjkMediaMeta.AV_CH_FRONT_LEFT, to = 1000) int i10, boolean z10) {
        if (context != null) {
            p0.g.b(i10, 1, 1000, "weight");
            if (typeface == null) {
                typeface = Typeface.DEFAULT;
            }
            return f28509a.b(context, typeface, i10, z10);
        }
        throw new IllegalArgumentException("Context cannot be null");
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f557c})
    @Deprecated
    public static Typeface createFromResourcesFontFile(@NonNull Context context, @NonNull Resources resources, int i10, String str, int i11) {
        return createFromResourcesFontFile(context, resources, i10, str, 0, i11);
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f557c})
    @Deprecated
    public static Typeface createFromResourcesFamilyXml(@NonNull Context context, @NonNull i0.e eVar, @NonNull Resources resources, int i10, int i11, @Nullable i0.p pVar, @Nullable Handler handler, boolean z10) {
        return createFromResourcesFamilyXml(context, eVar, resources, i10, null, 0, i11, pVar, handler, z10);
    }
}
