package z1;

import C1.l;
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;
import n2.AbstractC2612c;
import n2.AbstractC2613d;

/* JADX INFO: loaded from: classes.dex */
public class g extends AbstractC2612c {

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

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

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

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

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

    public static boolean x(Object obj, String str, int i6, boolean z6) {
        y();
        try {
            return ((Boolean) f26281e.invoke(obj, str, Integer.valueOf(i6), Boolean.valueOf(z6))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException e6) {
            throw new RuntimeException(e6);
        }
    }

    public static void y() {
        Class<?> cls;
        Method method;
        Constructor<?> constructor;
        Method method2;
        if (f26283g) {
            return;
        }
        f26283g = 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 e6) {
            Log.e("TypefaceCompatApi21Impl", e6.getClass().getName(), e6);
            cls = null;
            method = null;
            constructor = null;
            method2 = null;
        }
        f26280d = constructor;
        f26279c = cls;
        f26281e = method2;
        f26282f = method;
    }

    @Override // n2.AbstractC2612c
    public Typeface f(Context context, y1.e eVar, Resources resources) {
        y();
        try {
            Object objNewInstance = f26280d.newInstance(new Object[0]);
            for (y1.f fVar : eVar.f26149a) {
                File fileH = AbstractC2613d.h(context);
                if (fileH == null) {
                    return null;
                }
                try {
                    if (!AbstractC2613d.f(fileH, resources, fVar.f26155f)) {
                        return null;
                    }
                    if (!x(objNewInstance, fileH.getPath(), fVar.f26151b, fVar.f26152c)) {
                        return null;
                    }
                    fileH.delete();
                } catch (RuntimeException unused) {
                    return null;
                } finally {
                    fileH.delete();
                }
            }
            y();
            try {
                Object objNewInstance2 = Array.newInstance((Class<?>) f26279c, 1);
                Array.set(objNewInstance2, 0, objNewInstance);
                return (Typeface) f26282f.invoke(null, objNewInstance2);
            } catch (IllegalAccessException | InvocationTargetException e6) {
                throw new RuntimeException(e6);
            }
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e7) {
            throw new RuntimeException(e7);
        }
    }

    @Override // n2.AbstractC2612c
    public Typeface g(Context context, l[] lVarArr, int i6) {
        String str;
        if (lVarArr.length >= 1) {
            try {
                ParcelFileDescriptor parcelFileDescriptorOpenFileDescriptor = context.getContentResolver().openFileDescriptor(m(lVarArr, i6).f692a, "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 typefaceI = i(context, fileInputStream);
                        fileInputStream.close();
                        parcelFileDescriptorOpenFileDescriptor.close();
                        return typefaceI;
                    } finally {
                    }
                }
                if (parcelFileDescriptorOpenFileDescriptor != null) {
                    parcelFileDescriptorOpenFileDescriptor.close();
                    return null;
                }
            } catch (IOException unused2) {
            }
        }
        return null;
    }
}
