package G;

import S4.m;
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 S4.d {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static Class f769a = null;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static Constructor f770b = null;

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

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

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

    public static boolean Q(Object obj, String str, int i, boolean z5) {
        R();
        try {
            return ((Boolean) f771c.invoke(obj, str, Integer.valueOf(i), Boolean.valueOf(z5))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException e2) {
            throw new RuntimeException(e2);
        }
    }

    public static void R() {
        Class<?> cls;
        Method method;
        Constructor<?> constructor;
        Method method2;
        if (f773e) {
            return;
        }
        f773e = true;
        try {
            cls = Class.forName("android.graphics.FontFamily");
            constructor = cls.getConstructor(new Class[0]);
            method2 = cls.getMethod("addFontWeightStyle", String.class, Integer.TYPE, Boolean.TYPE);
            method = Typeface.class.getMethod("createFromFamiliesWithDefault", Array.newInstance(cls, 1).getClass());
        } catch (ClassNotFoundException | NoSuchMethodException e2) {
            Log.e("TypefaceCompatApi21Impl", e2.getClass().getName(), e2);
            cls = null;
            method = null;
            constructor = null;
            method2 = null;
        }
        f770b = constructor;
        f769a = cls;
        f771c = method2;
        f772d = method;
    }

    @Override // S4.d
    public Typeface o(Context context, F.e eVar, Resources resources, int i) {
        R();
        try {
            Object objNewInstance = f770b.newInstance(new Object[0]);
            for (F.f fVar : eVar.f581a) {
                File fileQ = m.q(context);
                if (fileQ == null) {
                    return null;
                }
                try {
                    if (!m.d(fileQ, resources, fVar.f587f)) {
                        return null;
                    }
                    if (!Q(objNewInstance, fileQ.getPath(), fVar.f583b, fVar.f584c)) {
                        return null;
                    }
                    fileQ.delete();
                } catch (RuntimeException unused) {
                    return null;
                } finally {
                    fileQ.delete();
                }
            }
            R();
            try {
                Object objNewInstance2 = Array.newInstance((Class<?>) f769a, 1);
                Array.set(objNewInstance2, 0, objNewInstance);
                return (Typeface) f772d.invoke(null, objNewInstance2);
            } catch (IllegalAccessException | InvocationTargetException e2) {
                throw new RuntimeException(e2);
            }
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e6) {
            throw new RuntimeException(e6);
        }
    }

    @Override // S4.d
    public Typeface p(Context context, M.k[] kVarArr, int i) {
        String str;
        if (kVarArr.length >= 1) {
            try {
                ParcelFileDescriptor parcelFileDescriptorOpenFileDescriptor = context.getContentResolver().openFileDescriptor(u(kVarArr, i).f2137a, "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 typefaceR = r(context, fileInputStream);
                        fileInputStream.close();
                        parcelFileDescriptorOpenFileDescriptor.close();
                        return typefaceR;
                    } finally {
                    }
                }
                if (parcelFileDescriptorOpenFileDescriptor != null) {
                    parcelFileDescriptorOpenFileDescriptor.close();
                    return null;
                }
            } catch (IOException unused2) {
            }
        }
        return null;
    }
}
