package j0;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
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 j extends p {

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

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

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

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

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

    public static boolean e(Object obj, String str, int i10, boolean z10) {
        f();
        try {
            return ((Boolean) f28513d.invoke(obj, str, Integer.valueOf(i10), Boolean.valueOf(z10))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException e10) {
            throw new RuntimeException(e10);
        }
    }

    public static void f() {
        Class<?> cls;
        Method method;
        Constructor<?> constructor;
        Method method2;
        if (f28515f) {
            return;
        }
        f28515f = 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 unused) {
            cls = null;
            method = null;
            constructor = null;
            method2 = null;
        }
        f28512c = constructor;
        f28511b = cls;
        f28513d = method2;
        f28514e = method;
    }

    @Override // j0.p
    public Typeface b(Context context, Typeface typeface, int i10, boolean z10) {
        Typeface typefaceCreateWeightStyle;
        try {
            typefaceCreateWeightStyle = s.createWeightStyle(typeface, i10, z10);
        } catch (RuntimeException unused) {
            typefaceCreateWeightStyle = null;
        }
        return typefaceCreateWeightStyle == null ? super.b(context, typeface, i10, z10) : typefaceCreateWeightStyle;
    }

    @Override // j0.p
    public Typeface createFromFontFamilyFilesResourceEntry(Context context, i0.f fVar, Resources resources, int i10) {
        f();
        try {
            Object objNewInstance = f28512c.newInstance(new Object[0]);
            for (i0.g gVar : fVar.f26490a) {
                File tempFile = q.getTempFile(context);
                if (tempFile == null) {
                    return null;
                }
                try {
                    if (!q.a(tempFile, resources, gVar.f26496f)) {
                        return null;
                    }
                    if (!e(objNewInstance, tempFile.getPath(), gVar.f26492b, gVar.f26493c)) {
                        return null;
                    }
                    tempFile.delete();
                } catch (RuntimeException unused) {
                    return null;
                } finally {
                    tempFile.delete();
                }
            }
            f();
            try {
                Object objNewInstance2 = Array.newInstance((Class<?>) f28511b, 1);
                Array.set(objNewInstance2, 0, objNewInstance);
                return (Typeface) f28514e.invoke(null, objNewInstance2);
            } catch (IllegalAccessException | InvocationTargetException e10) {
                throw new RuntimeException(e10);
            }
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e11) {
            throw new RuntimeException(e11);
        }
    }

    @Override // j0.p
    public Typeface createFromFontInfo(Context context, CancellationSignal cancellationSignal, n0.k[] kVarArr, int i10) {
        String str;
        if (kVarArr.length < 1) {
            return null;
        }
        try {
            ParcelFileDescriptor parcelFileDescriptorOpenFileDescriptor = context.getContentResolver().openFileDescriptor(d(i10, kVarArr).f30748a, "r", cancellationSignal);
            if (parcelFileDescriptorOpenFileDescriptor == null) {
                if (parcelFileDescriptorOpenFileDescriptor != null) {
                    parcelFileDescriptorOpenFileDescriptor.close();
                }
                return null;
            }
            try {
                try {
                    str = Os.readlink("/proc/self/fd/" + parcelFileDescriptorOpenFileDescriptor.getFd());
                } 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 typefaceA = a(context, fileInputStream);
                    fileInputStream.close();
                    parcelFileDescriptorOpenFileDescriptor.close();
                    return typefaceA;
                } finally {
                }
            } finally {
            }
        } catch (IOException unused2) {
            return null;
        }
    }
}
