package j0;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.CancellationSignal;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import e.y;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import tv.danmaku.ijk.media.player.IjkEventListener;
import xyz.stream.api.a0;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ConcurrentHashMap f28527a = new ConcurrentHashMap();

    public static Object c(Object[] objArr, int i10, boolean z10, o oVar) {
        Object obj = null;
        int i11 = Integer.MAX_VALUE;
        for (Object obj2 : objArr) {
            int iAbs = (Math.abs(oVar.o(obj2) - i10) * 2) + (oVar.p(obj2) == z10 ? 0 : 1);
            if (obj == null || i11 > iAbs) {
                obj = obj2;
                i11 = iAbs;
            }
        }
        return obj;
    }

    private static long getUniqueKey(@Nullable Typeface typeface) {
        if (typeface == null) {
            return 0L;
        }
        try {
            Field declaredField = Typeface.class.getDeclaredField("native_instance");
            declaredField.setAccessible(true);
            return ((Number) declaredField.get(typeface)).longValue();
        } catch (IllegalAccessException | NoSuchFieldException unused) {
            return 0L;
        }
    }

    public Typeface a(Context context, InputStream inputStream) {
        File tempFile = q.getTempFile(context);
        if (tempFile == null) {
            return null;
        }
        try {
            if (q.b(tempFile, inputStream)) {
                return Typeface.createFromFile(tempFile.getPath());
            }
            return null;
        } catch (RuntimeException unused) {
            return null;
        } finally {
            tempFile.delete();
        }
    }

    public Typeface b(Context context, Typeface typeface, int i10, boolean z10) {
        Typeface typefaceCreateWeightStyle;
        try {
            typefaceCreateWeightStyle = r.createWeightStyle(this, context, typeface, i10, z10);
        } catch (RuntimeException unused) {
            typefaceCreateWeightStyle = null;
        }
        return typefaceCreateWeightStyle == null ? typeface : typefaceCreateWeightStyle;
    }

    @Nullable
    public Typeface createFromFontFamilyFilesResourceEntry(Context context, i0.f fVar, Resources resources, int i10) {
        i0.g gVar = (i0.g) c(fVar.f26490a, (i10 & 1) == 0 ? IjkEventListener.VIDEO_SIZE_CHANGED : 700, (i10 & 2) != 0, new a0(this, 5));
        if (gVar == null) {
            return null;
        }
        Typeface typefaceCreateFromResourcesFontFile = i.createFromResourcesFontFile(context, resources, gVar.f26496f, gVar.f26491a, 0, i10);
        long uniqueKey = getUniqueKey(typefaceCreateFromResourcesFontFile);
        if (uniqueKey != 0) {
            this.f28527a.put(Long.valueOf(uniqueKey), fVar);
        }
        return typefaceCreateFromResourcesFontFile;
    }

    @Nullable
    public Typeface createFromFontInfo(Context context, @Nullable CancellationSignal cancellationSignal, @NonNull n0.k[] kVarArr, int i10) throws Throwable {
        InputStream inputStreamOpenInputStream;
        InputStream inputStream = null;
        if (kVarArr.length < 1) {
            return null;
        }
        try {
            inputStreamOpenInputStream = context.getContentResolver().openInputStream(d(i10, kVarArr).f30748a);
            try {
                Typeface typefaceA = a(context, inputStreamOpenInputStream);
                q.closeQuietly(inputStreamOpenInputStream);
                return typefaceA;
            } catch (IOException unused) {
                q.closeQuietly(inputStreamOpenInputStream);
                return null;
            } catch (Throwable th) {
                th = th;
                inputStream = inputStreamOpenInputStream;
                q.closeQuietly(inputStream);
                throw th;
            }
        } catch (IOException unused2) {
            inputStreamOpenInputStream = null;
        } catch (Throwable th2) {
            th = th2;
        }
    }

    @Nullable
    @RequiresApi(29)
    public Typeface createFromFontInfoWithFallback(@NonNull Context context, @Nullable CancellationSignal cancellationSignal, @NonNull List<n0.k[]> list, int i10) {
        throw new IllegalStateException("createFromFontInfoWithFallback must only be called on API 29+");
    }

    @Nullable
    public Typeface createFromResourcesFontFile(Context context, Resources resources, int i10, String str, int i11) {
        File tempFile = q.getTempFile(context);
        if (tempFile == null) {
            return null;
        }
        try {
            if (q.a(tempFile, resources, i10)) {
                return Typeface.createFromFile(tempFile.getPath());
            }
            return null;
        } catch (RuntimeException unused) {
            return null;
        } finally {
            tempFile.delete();
        }
    }

    public n0.k d(int i10, n0.k[] kVarArr) {
        return (n0.k) c(kVarArr, (i10 & 1) == 0 ? IjkEventListener.VIDEO_SIZE_CHANGED : 700, (i10 & 2) != 0, new y(this));
    }

    @Nullable
    public i0.f getFontFamily(Typeface typeface) {
        long uniqueKey = getUniqueKey(typeface);
        if (uniqueKey == 0) {
            return null;
        }
        return (i0.f) this.f28527a.get(Long.valueOf(uniqueKey));
    }

    @Nullable
    public Typeface createFromFontFamilyFilesResourceEntry(Context context, i0.f fVar, Resources resources, int i10, boolean z10) {
        i0.g gVar = (i0.g) c(fVar.f26490a, i10, z10, new xyz.stream.utils.a0(this));
        if (gVar == null) {
            return null;
        }
        Typeface typefaceCreateFromResourcesFontFile = i.createFromResourcesFontFile(context, resources, gVar.f26496f, gVar.f26491a, 0, 0);
        long uniqueKey = getUniqueKey(typefaceCreateFromResourcesFontFile);
        if (uniqueKey != 0) {
            this.f28527a.put(Long.valueOf(uniqueKey), fVar);
        }
        return typefaceCreateFromResourcesFontFile;
    }
}
