package X6FRs_4.IMuAHx_6.IFxWkV_5;

import X6FRs_4.IMuAHx_6.X6FRs_4.PGPF6hQ_2.PGPF6hQ_2;
import X6FRs_4.IMuAHx_6.XnFJ5_9.QxJ9d_1;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.CancellationSignal;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentHashMap;
/* compiled from: TypefaceCompatBaseImpl.java */
/* loaded from: classes.dex */
public class FSaI4_7 {
    public ConcurrentHashMap<Long, PGPF6hQ_2.QxJ9d_1> KQgRr5e_0 = new ConcurrentHashMap<>();

    /* compiled from: TypefaceCompatBaseImpl.java */
    /* loaded from: classes.dex */
    public interface PGPF6hQ_2<T> {
        int KQgRr5e_0(T t);

        /* renamed from: KQgRr5e_0  reason: collision with other method in class */
        boolean m277KQgRr5e_0(T t);
    }

    public static <T> T KQgRr5e_0(T[] tArr, int i, PGPF6hQ_2<T> pGPF6hQ_2) {
        int i2 = (i & 1) == 0 ? 400 : 700;
        boolean z = (i & 2) != 0;
        T t = null;
        int i3 = Integer.MAX_VALUE;
        for (T t2 : tArr) {
            int abs = (Math.abs(pGPF6hQ_2.KQgRr5e_0((PGPF6hQ_2<T>) t2) - i2) * 2) + (pGPF6hQ_2.m277KQgRr5e_0((PGPF6hQ_2<T>) t2) == z ? 0 : 1);
            if (t == null || i3 > abs) {
                t = t2;
                i3 = abs;
            }
        }
        return t;
    }

    /* compiled from: TypefaceCompatBaseImpl.java */
    /* loaded from: classes.dex */
    public class KQgRr5e_0 implements PGPF6hQ_2<QxJ9d_1.IFxWkV_5> {
        public KQgRr5e_0(FSaI4_7 fSaI4_7) {
        }

        @Override // X6FRs_4.IMuAHx_6.IFxWkV_5.FSaI4_7.PGPF6hQ_2
        public int KQgRr5e_0(QxJ9d_1.IFxWkV_5 iFxWkV_5) {
            return iFxWkV_5.PGPF6hQ_2();
        }

        @Override // X6FRs_4.IMuAHx_6.IFxWkV_5.FSaI4_7.PGPF6hQ_2
        public boolean KQgRr5e_0(QxJ9d_1.IFxWkV_5 iFxWkV_5) {
            return iFxWkV_5.m347KQgRr5e_0();
        }
    }

    /* compiled from: TypefaceCompatBaseImpl.java */
    /* loaded from: classes.dex */
    public class QxJ9d_1 implements PGPF6hQ_2<PGPF6hQ_2.C0046PGPF6hQ_2> {
        public QxJ9d_1(FSaI4_7 fSaI4_7) {
        }

        @Override // X6FRs_4.IMuAHx_6.IFxWkV_5.FSaI4_7.PGPF6hQ_2
        public int KQgRr5e_0(PGPF6hQ_2.C0046PGPF6hQ_2 c0046PGPF6hQ_2) {
            return c0046PGPF6hQ_2.PGPF6hQ_2();
        }

        @Override // X6FRs_4.IMuAHx_6.IFxWkV_5.FSaI4_7.PGPF6hQ_2
        public boolean KQgRr5e_0(PGPF6hQ_2.C0046PGPF6hQ_2 c0046PGPF6hQ_2) {
            return c0046PGPF6hQ_2.m339KQgRr5e_0();
        }
    }

    public static long KQgRr5e_0(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 e) {
            Log.e("TypefaceCompatBaseImpl", "Could not retrieve font from family.", e);
            return 0L;
        } catch (NoSuchFieldException e2) {
            Log.e("TypefaceCompatBaseImpl", "Could not retrieve font from family.", e2);
            return 0L;
        }
    }

    public QxJ9d_1.IFxWkV_5 KQgRr5e_0(QxJ9d_1.IFxWkV_5[] iFxWkV_5Arr, int i) {
        return (QxJ9d_1.IFxWkV_5) KQgRr5e_0(iFxWkV_5Arr, i, new KQgRr5e_0(this));
    }

    public Typeface KQgRr5e_0(Context context, InputStream inputStream) {
        File KQgRr5e_02 = Xgtqvk_8.KQgRr5e_0(context);
        if (KQgRr5e_02 == null) {
            return null;
        }
        try {
            if (Xgtqvk_8.KQgRr5e_0(KQgRr5e_02, inputStream)) {
                return Typeface.createFromFile(KQgRr5e_02.getPath());
            }
            return null;
        } catch (RuntimeException unused) {
            return null;
        } finally {
            KQgRr5e_02.delete();
        }
    }

    public Typeface KQgRr5e_0(Context context, CancellationSignal cancellationSignal, QxJ9d_1.IFxWkV_5[] iFxWkV_5Arr, int i) {
        InputStream inputStream;
        InputStream inputStream2 = null;
        if (iFxWkV_5Arr.length < 1) {
            return null;
        }
        try {
            inputStream = context.getContentResolver().openInputStream(KQgRr5e_0(iFxWkV_5Arr, i).m346KQgRr5e_0());
        } catch (IOException unused) {
            inputStream = null;
        } catch (Throwable th) {
            th = th;
        }
        try {
            Typeface KQgRr5e_02 = KQgRr5e_0(context, inputStream);
            Xgtqvk_8.KQgRr5e_0(inputStream);
            return KQgRr5e_02;
        } catch (IOException unused2) {
            Xgtqvk_8.KQgRr5e_0(inputStream);
            return null;
        } catch (Throwable th2) {
            th = th2;
            inputStream2 = inputStream;
            Xgtqvk_8.KQgRr5e_0(inputStream2);
            throw th;
        }
    }

    public final PGPF6hQ_2.C0046PGPF6hQ_2 KQgRr5e_0(PGPF6hQ_2.QxJ9d_1 qxJ9d_1, int i) {
        return (PGPF6hQ_2.C0046PGPF6hQ_2) KQgRr5e_0(qxJ9d_1.KQgRr5e_0(), i, new QxJ9d_1(this));
    }

    public Typeface KQgRr5e_0(Context context, PGPF6hQ_2.QxJ9d_1 qxJ9d_1, Resources resources, int i) {
        PGPF6hQ_2.C0046PGPF6hQ_2 KQgRr5e_02 = KQgRr5e_0(qxJ9d_1, i);
        if (KQgRr5e_02 == null) {
            return null;
        }
        Typeface KQgRr5e_03 = X6FRs_4.IMuAHx_6.IFxWkV_5.PGPF6hQ_2.KQgRr5e_0(context, resources, KQgRr5e_02.KQgRr5e_0(), KQgRr5e_02.m338KQgRr5e_0(), i);
        KQgRr5e_0(KQgRr5e_03, qxJ9d_1);
        return KQgRr5e_03;
    }

    public Typeface KQgRr5e_0(Context context, Resources resources, int i, String str, int i2) {
        File KQgRr5e_02 = Xgtqvk_8.KQgRr5e_0(context);
        if (KQgRr5e_02 == null) {
            return null;
        }
        try {
            if (Xgtqvk_8.KQgRr5e_0(KQgRr5e_02, resources, i)) {
                return Typeface.createFromFile(KQgRr5e_02.getPath());
            }
            return null;
        } catch (RuntimeException unused) {
            return null;
        } finally {
            KQgRr5e_02.delete();
        }
    }

    public final void KQgRr5e_0(Typeface typeface, PGPF6hQ_2.QxJ9d_1 qxJ9d_1) {
        long KQgRr5e_02 = KQgRr5e_0(typeface);
        if (KQgRr5e_02 != 0) {
            this.KQgRr5e_0.put(Long.valueOf(KQgRr5e_02), qxJ9d_1);
        }
    }
}
