package defpackage;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.util.Log;
import com.strmr.ps.data.repository.ScreenConfigRepository;
import defpackage.lh3;
import defpackage.mh3;
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;

/* JADX INFO: loaded from: classes.dex */
@m38
class b1a {
    public final ConcurrentHashMap a = new ConcurrentHashMap();

    public interface a<T> {
        boolean a(Object obj);

        int b(Object obj);
    }

    public static Object f(Object[] objArr, int i, a aVar) {
        int i2 = (i & 1) == 0 ? ScreenConfigRepository.MAX_ROWS_PER_PROFILE : 700;
        boolean z = (i & 2) != 0;
        Object obj = null;
        int i3 = Integer.MAX_VALUE;
        for (Object obj2 : objArr) {
            int iAbs = (Math.abs(aVar.b(obj2) - i2) * 2) + (aVar.a(obj2) == z ? 0 : 1);
            if (obj == null || i3 > iAbs) {
                obj = obj2;
                i3 = iAbs;
            }
        }
        return obj;
    }

    public Typeface a(Context context, lh3.d dVar, Resources resources, int i) {
        long jLongValue;
        lh3.e eVar = (lh3.e) f(dVar.a, i, new z0a());
        if (eVar == null) {
            return null;
        }
        Typeface typefaceD = r0a.d(context, resources, eVar.f, eVar.a, 0, i);
        if (typefaceD == null) {
            jLongValue = 0;
        } else {
            try {
                Field declaredField = Typeface.class.getDeclaredField("native_instance");
                declaredField.setAccessible(true);
                jLongValue = ((Number) declaredField.get(typefaceD)).longValue();
            } catch (IllegalAccessException e) {
                Log.e("TypefaceCompatBaseImpl", "Could not retrieve font from family.", e);
                jLongValue = 0;
            } catch (NoSuchFieldException e2) {
                Log.e("TypefaceCompatBaseImpl", "Could not retrieve font from family.", e2);
                jLongValue = 0;
            }
        }
        if (jLongValue != 0) {
            this.a.put(Long.valueOf(jLongValue), dVar);
        }
        return typefaceD;
    }

    public Typeface b(Context context, mh3.c[] cVarArr, int i) throws Throwable {
        InputStream inputStreamOpenInputStream;
        InputStream inputStream = null;
        if (cVarArr.length < 1) {
            return null;
        }
        try {
            inputStreamOpenInputStream = context.getContentResolver().openInputStream(g(cVarArr, i).a);
            try {
                Typeface typefaceD = d(context, inputStreamOpenInputStream);
                c1a.a(inputStreamOpenInputStream);
                return typefaceD;
            } catch (IOException unused) {
                c1a.a(inputStreamOpenInputStream);
                return null;
            } catch (Throwable th) {
                th = th;
                inputStream = inputStreamOpenInputStream;
                c1a.a(inputStream);
                throw th;
            }
        } catch (IOException unused2) {
            inputStreamOpenInputStream = null;
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public Typeface c(Context context, List list, int i) {
        throw new IllegalStateException("createFromFontInfoWithFallback must only be called on API 29+");
    }

    public Typeface d(Context context, InputStream inputStream) {
        File fileD = c1a.d(context);
        if (fileD == null) {
            return null;
        }
        try {
            if (c1a.c(fileD, inputStream)) {
                return Typeface.createFromFile(fileD.getPath());
            }
            return null;
        } catch (RuntimeException unused) {
            return null;
        } finally {
            fileD.delete();
        }
    }

    public Typeface e(Context context, Resources resources, int i, String str, int i2) {
        File fileD = c1a.d(context);
        if (fileD == null) {
            return null;
        }
        try {
            if (c1a.b(fileD, resources, i)) {
                return Typeface.createFromFile(fileD.getPath());
            }
            return null;
        } catch (RuntimeException unused) {
            return null;
        } finally {
            fileD.delete();
        }
    }

    public mh3.c g(mh3.c[] cVarArr, int i) {
        return (mh3.c) f(cVarArr, i, new y0a());
    }
}
