package ῲ.ᨨ.ʬ;

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;
import ῲ.ᨨ.ῲ.ῲ.C1796;
import ῲ.ᨨ.ῲ.ῲ.C1804;
import ῲ.ᨨ.㴹.C1847;
import K.K.K.ʬ.C2369;

/* renamed from: ῲ.ᨨ.ʬ.ඓ  reason: contains not printable characters */
/* compiled from: TypefaceCompatApi21Impl */
public class C1709 extends C1719 {

    /* renamed from: ʬ  reason: contains not printable characters */
    public static Constructor<?> f6001 = null;

    /* renamed from: ඓ  reason: contains not printable characters */
    public static Method f6002 = null;

    /* renamed from: ῲ  reason: contains not printable characters */
    public static Class<?> f6003 = null;

    /* renamed from: 㴹  reason: contains not printable characters */
    public static boolean f6004 = false;

    /* renamed from: 㸾  reason: contains not printable characters */
    public static Method f6005;

    /* renamed from: K  reason: contains not printable characters */
    public static void m4381() {
        Method method;
        Class<?> cls;
        Method method2;
        if (!f6004) {
            f6004 = true;
            Constructor<?> constructor = null;
            try {
                cls = Class.forName("android.graphics.FontFamily");
                Constructor<?> constructor2 = cls.getConstructor(new Class[0]);
                method = cls.getMethod("addFontWeightStyle", String.class, Integer.TYPE, Boolean.TYPE);
                method2 = Typeface.class.getMethod("createFromFamiliesWithDefault", Array.newInstance(cls, 1).getClass());
                constructor = constructor2;
            } catch (ClassNotFoundException | NoSuchMethodException e) {
                Log.e("TypefaceCompatApi21Impl", e.getClass().getName(), e);
                method2 = null;
                cls = null;
                method = null;
            }
            f6001 = constructor;
            f6003 = cls;
            f6005 = method;
            f6002 = method2;
        }
    }

    @Override // ῲ.ᨨ.ʬ.C1719
    /* renamed from: K  reason: contains not printable characters */
    public Typeface m4384(Context context, C1796 r10, Resources resources, int i) {
        m4381();
        try {
            Object newInstance = f6001.newInstance(new Object[0]);
            C1804[] r102 = r10.f6145;
            for (C1804 r4 : r102) {
                File r5 = C2369.m6282(context);
                if (r5 == null) {
                    return null;
                }
                try {
                    if (!C2369.m6343(r5, resources, r4.f6163)) {
                        r5.delete();
                        return null;
                    } else if (!m4382(newInstance, r5.getPath(), r4.f6161, r4.f6159)) {
                        return null;
                    } else {
                        r5.delete();
                    }
                } catch (RuntimeException unused) {
                    return null;
                } finally {
                    r5.delete();
                }
            }
            m4381();
            try {
                Object newInstance2 = Array.newInstance(f6003, 1);
                Array.set(newInstance2, 0, newInstance);
                return (Typeface) f6002.invoke(null, newInstance2);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e2) {
            throw new RuntimeException(e2);
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public static boolean m4382(Object obj, String str, int i, boolean z) {
        m4381();
        try {
            return ((Boolean) f6005.invoke(obj, str, Integer.valueOf(i), Boolean.valueOf(z))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:18:0x004e A[SYNTHETIC, Splitter:B:18:0x004e] */
    @Override // ῲ.ᨨ.ʬ.C1719
    /* renamed from: K  reason: contains not printable characters */
    public Typeface m4383(Context context, CancellationSignal cancellationSignal, C1847[] r6, int i) {
        File file;
        FileInputStream fileInputStream;
        if (r6.length < 1) {
            return null;
        }
        try {
            ParcelFileDescriptor openFileDescriptor = context.getContentResolver().openFileDescriptor(m4418(r6, i).f6280, "r", cancellationSignal);
            if (openFileDescriptor == null) {
                if (openFileDescriptor != null) {
                    openFileDescriptor.close();
                }
                return null;
            }
            try {
                String readlink = Os.readlink("/proc/self/fd/" + openFileDescriptor.getFd());
                if (OsConstants.S_ISREG(Os.stat(readlink).st_mode)) {
                    file = new File(readlink);
                    if (file != null) {
                        try {
                            if (file.canRead()) {
                                Typeface createFromFile = Typeface.createFromFile(file);
                                openFileDescriptor.close();
                                return createFromFile;
                            }
                        } catch (Throwable th) {
                            th.addSuppressed(th);
                        }
                    }
                    fileInputStream = new FileInputStream(openFileDescriptor.getFileDescriptor());
                    Typeface r4 = super.m4416(context, fileInputStream);
                    fileInputStream.close();
                    openFileDescriptor.close();
                    return r4;
                }
            } catch (ErrnoException unused) {
            }
            file = null;
            if (file != null) {
            }
            fileInputStream = new FileInputStream(openFileDescriptor.getFileDescriptor());
            try {
                Typeface r42 = super.m4416(context, fileInputStream);
                fileInputStream.close();
                openFileDescriptor.close();
                return r42;
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
        } catch (IOException unused2) {
            return null;
        }
        throw th;
        throw th;
    }
}
