package F;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.ParcelFileDescriptor;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/* JADX INFO: loaded from: classes.dex */
public class f extends U2.a {

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static Class f2238h = null;
    public static Constructor i = null;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static Method f2239j = null;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static Method f2240k = null;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public static boolean f2241l = false;

    public static boolean O(Object obj, String str, int i5, boolean z8) throws NoSuchMethodException {
        P();
        try {
            try {
                return ((Boolean) f2239j.invoke(obj, str, Integer.valueOf(i5), Boolean.valueOf(z8))).booleanValue();
            } catch (InvocationTargetException e8) {
                e = e8;
                throw new RuntimeException(e);
            }
        } catch (IllegalAccessException | InvocationTargetException e9) {
            e = e9;
        }
    }

    public static void P() throws NoSuchMethodException {
        Method method;
        Class<?> cls;
        Method method2;
        if (f2241l) {
            return;
        }
        f2241l = true;
        Constructor<?> constructor = null;
        try {
            cls = Class.forName("android.graphics.FontFamily");
            Constructor<?> constructor2 = cls.getConstructor(null);
            method2 = cls.getMethod("addFontWeightStyle", String.class, Integer.TYPE, Boolean.TYPE);
            method = Typeface.class.getMethod("createFromFamiliesWithDefault", Array.newInstance(cls, 1).getClass());
            constructor = constructor2;
        } catch (ClassNotFoundException | NoSuchMethodException e8) {
            Log.e("TypefaceCompatApi21Impl", e8.getClass().getName(), e8);
            method = null;
            cls = null;
            method2 = null;
        }
        i = constructor;
        f2238h = cls;
        f2239j = method2;
        f2240k = method;
    }

    @Override // U2.a
    public Typeface h(Context context, E.e eVar, Resources resources, int i5) throws NoSuchMethodException {
        P();
        try {
            Object objNewInstance = i.newInstance(null);
            for (E.f fVar : eVar.f1937a) {
                File fileW = W2.g.w(context);
                if (fileW == null) {
                    return null;
                }
                try {
                    if (!W2.g.i(fileW, resources, fVar.f1943f)) {
                        return null;
                    }
                    if (!O(objNewInstance, fileW.getPath(), fVar.f1939b, fVar.f1940c)) {
                        return null;
                    }
                    fileW.delete();
                } catch (RuntimeException unused) {
                    return null;
                } finally {
                    fileW.delete();
                }
            }
            P();
            try {
                Object objNewInstance2 = Array.newInstance((Class<?>) f2238h, 1);
                Array.set(objNewInstance2, 0, objNewInstance);
                return (Typeface) f2240k.invoke(null, objNewInstance2);
            } catch (IllegalAccessException | InvocationTargetException e8) {
                throw new RuntimeException(e8);
            }
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e9) {
            throw new RuntimeException(e9);
        }
    }

    @Override // U2.a
    public Typeface i(Context context, J.j[] jVarArr, int i5) {
        String str;
        if (jVarArr.length >= 1) {
            try {
                ParcelFileDescriptor parcelFileDescriptorOpenFileDescriptor = context.getContentResolver().openFileDescriptor(A(jVarArr, i5).f3435a, "r", null);
                if (parcelFileDescriptorOpenFileDescriptor != null) {
                    try {
                        try {
                            str = Os.readlink("/proc/self/fd/" + parcelFileDescriptorOpenFileDescriptor.getFd());
                        } finally {
                        }
                    } catch (ErrnoException unused) {
                    }
                    File file = OsConstants.S_ISREG(Os.stat(str).st_mode) ? new File(str) : null;
                    if (file != null && file.canRead()) {
                        Typeface typefaceCreateFromFile = Typeface.createFromFile(file);
                        parcelFileDescriptorOpenFileDescriptor.close();
                        return typefaceCreateFromFile;
                    }
                    FileInputStream fileInputStream = new FileInputStream(parcelFileDescriptorOpenFileDescriptor.getFileDescriptor());
                    try {
                        Typeface typefaceK = k(context, fileInputStream);
                        fileInputStream.close();
                        parcelFileDescriptorOpenFileDescriptor.close();
                        return typefaceK;
                    } finally {
                    }
                }
                if (parcelFileDescriptorOpenFileDescriptor != null) {
                    parcelFileDescriptorOpenFileDescriptor.close();
                    return null;
                }
            } catch (IOException unused2) {
            }
        }
        return null;
    }
}
