package X6FRs_4.IMuAHx_6.IFxWkV_5;

import X6FRs_4.IMuAHx_6.X6FRs_4.PGPF6hQ_2.PGPF6hQ_2;
import X6FRs_4.IMuAHx_6.XnFJ5_9.QxJ9d_1;
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 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;
/* compiled from: TypefaceCompatApi21Impl.java */
/* loaded from: classes.dex */
public class Mqv2r_3 extends FSaI4_7 {
    public static Class KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public static Constructor f397KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public static Method f398KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public static boolean f399KQgRr5e_0;
    public static Method QxJ9d_1;

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public static void m284KQgRr5e_0() {
        Method method;
        Class<?> cls;
        Method method2;
        if (f399KQgRr5e_0) {
            return;
        }
        f399KQgRr5e_0 = true;
        Constructor<?> constructor = null;
        try {
            cls = Class.forName("android.graphics.FontFamily");
            Constructor<?> constructor2 = 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());
            constructor = constructor2;
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            Log.e("TypefaceCompatApi21Impl", e.getClass().getName(), e);
            method = null;
            cls = null;
            method2 = null;
        }
        f397KQgRr5e_0 = constructor;
        KQgRr5e_0 = cls;
        f398KQgRr5e_0 = method2;
        QxJ9d_1 = method;
    }

    public final File KQgRr5e_0(ParcelFileDescriptor parcelFileDescriptor) {
        try {
            String readlink = Os.readlink("/proc/self/fd/" + parcelFileDescriptor.getFd());
            if (OsConstants.S_ISREG(Os.stat(readlink).st_mode)) {
                return new File(readlink);
            }
        } catch (ErrnoException unused) {
        }
        return null;
    }

    public static Object KQgRr5e_0() {
        m284KQgRr5e_0();
        try {
            return f397KQgRr5e_0.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static Typeface KQgRr5e_0(Object obj) {
        m284KQgRr5e_0();
        try {
            Object newInstance = Array.newInstance(KQgRr5e_0, 1);
            Array.set(newInstance, 0, obj);
            return (Typeface) QxJ9d_1.invoke(null, newInstance);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean KQgRr5e_0(Object obj, String str, int i, boolean z) {
        m284KQgRr5e_0();
        try {
            return ((Boolean) f398KQgRr5e_0.invoke(obj, str, Integer.valueOf(i), Boolean.valueOf(z))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    @Override // X6FRs_4.IMuAHx_6.IFxWkV_5.FSaI4_7
    public Typeface KQgRr5e_0(Context context, CancellationSignal cancellationSignal, QxJ9d_1.IFxWkV_5[] iFxWkV_5Arr, int i) {
        if (iFxWkV_5Arr.length < 1) {
            return null;
        }
        QxJ9d_1.IFxWkV_5 KQgRr5e_02 = KQgRr5e_0(iFxWkV_5Arr, i);
        try {
            ParcelFileDescriptor openFileDescriptor = context.getContentResolver().openFileDescriptor(KQgRr5e_02.m346KQgRr5e_0(), "r", cancellationSignal);
            if (openFileDescriptor == null) {
                if (openFileDescriptor != null) {
                    openFileDescriptor.close();
                }
                return null;
            }
            File KQgRr5e_03 = KQgRr5e_0(openFileDescriptor);
            if (KQgRr5e_03 != null && KQgRr5e_03.canRead()) {
                Typeface createFromFile = Typeface.createFromFile(KQgRr5e_03);
                if (openFileDescriptor != null) {
                    openFileDescriptor.close();
                }
                return createFromFile;
            }
            FileInputStream fileInputStream = new FileInputStream(openFileDescriptor.getFileDescriptor());
            Typeface KQgRr5e_04 = super.KQgRr5e_0(context, fileInputStream);
            fileInputStream.close();
            if (openFileDescriptor != null) {
                openFileDescriptor.close();
            }
            return KQgRr5e_04;
        } catch (IOException unused) {
            return null;
        }
    }

    @Override // X6FRs_4.IMuAHx_6.IFxWkV_5.FSaI4_7
    public Typeface KQgRr5e_0(Context context, PGPF6hQ_2.QxJ9d_1 qxJ9d_1, Resources resources, int i) {
        PGPF6hQ_2.C0046PGPF6hQ_2[] KQgRr5e_02;
        Object KQgRr5e_03 = KQgRr5e_0();
        for (PGPF6hQ_2.C0046PGPF6hQ_2 c0046PGPF6hQ_2 : qxJ9d_1.KQgRr5e_0()) {
            File KQgRr5e_04 = Xgtqvk_8.KQgRr5e_0(context);
            if (KQgRr5e_04 == null) {
                return null;
            }
            try {
                if (!Xgtqvk_8.KQgRr5e_0(KQgRr5e_04, resources, c0046PGPF6hQ_2.KQgRr5e_0())) {
                    return null;
                }
                if (!KQgRr5e_0(KQgRr5e_03, KQgRr5e_04.getPath(), c0046PGPF6hQ_2.PGPF6hQ_2(), c0046PGPF6hQ_2.m339KQgRr5e_0())) {
                    return null;
                }
                KQgRr5e_04.delete();
            } catch (RuntimeException unused) {
                return null;
            } finally {
                KQgRr5e_04.delete();
            }
        }
        return KQgRr5e_0(KQgRr5e_03);
    }
}
