package o;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.net.Uri;
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 java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.ph0, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C2167ph0 extends AbstractC2461sm0 {
    public static final Class k;
    public static final Constructor l;
    public static final Method m;
    public static final Method n;

    static {
        Class<?> cls;
        Method method;
        Method method2;
        Constructor<?> constructor = null;
        try {
            cls = Class.forName("android.graphics.FontFamily");
            Constructor<?> constructor2 = cls.getConstructor(null);
            Class cls2 = Integer.TYPE;
            method2 = cls.getMethod("addFontWeightStyle", ByteBuffer.class, cls2, List.class, cls2, Boolean.TYPE);
            method = Typeface.class.getMethod("createFromFamiliesWithDefault", Array.newInstance(cls, 1).getClass());
            constructor = constructor2;
        } catch (ClassNotFoundException | NoSuchMethodException unused) {
            cls = null;
            method = null;
            method2 = null;
        }
        l = constructor;
        k = cls;
        m = method2;
        n = method;
    }

    public static boolean H(Object obj, ByteBuffer byteBuffer, int i, int i2, boolean z) {
        try {
            return ((Boolean) m.invoke(obj, byteBuffer, Integer.valueOf(i), null, Integer.valueOf(i2), Boolean.valueOf(z))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    public static Typeface I(Object obj) throws ArrayIndexOutOfBoundsException, IllegalArgumentException, NegativeArraySizeException {
        try {
            Object objNewInstance = Array.newInstance((Class<?>) k, 1);
            Array.set(objNewInstance, 0, obj);
            return (Typeface) n.invoke(null, objNewInstance);
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    @Override // o.AbstractC2461sm0
    public final Typeface e(Context context, GA ga, Resources resources, int i) throws IllegalAccessException, InstantiationException, IllegalArgumentException, InvocationTargetException {
        Object objNewInstance;
        MappedByteBuffer map;
        FileInputStream fileInputStream;
        try {
            objNewInstance = l.newInstance(null);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            objNewInstance = null;
        }
        if (objNewInstance != null) {
            for (HA ha : ga.a) {
                int i2 = ha.f;
                File fileX = AbstractC2841wm0.x(context);
                if (fileX != null) {
                    try {
                        if (AbstractC2841wm0.m(fileX, resources, i2)) {
                            try {
                                fileInputStream = new FileInputStream(fileX);
                            } catch (IOException unused2) {
                                map = null;
                            }
                            try {
                                FileChannel channel = fileInputStream.getChannel();
                                map = channel.map(FileChannel.MapMode.READ_ONLY, 0L, channel.size());
                                fileInputStream.close();
                                if (map != null && H(objNewInstance, map, ha.e, ha.b, ha.c)) {
                                }
                            } finally {
                            }
                        }
                    } finally {
                        fileX.delete();
                    }
                }
                map = null;
                if (map != null) {
                }
            }
            return I(objNewInstance);
        }
        return null;
    }

    @Override // o.AbstractC2461sm0
    public final Typeface f(Context context, QA[] qaArr, int i) throws IllegalAccessException, InstantiationException, IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException, InvocationTargetException, NegativeArraySizeException {
        Object objNewInstance;
        try {
            objNewInstance = l.newInstance(null);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            objNewInstance = null;
        }
        if (objNewInstance != null) {
            int i2 = 0;
            I80 i80 = new I80(0);
            int length = qaArr.length;
            while (true) {
                if (i2 < length) {
                    QA qa = qaArr[i2];
                    Uri uri = qa.a;
                    ByteBuffer byteBufferZ = (ByteBuffer) i80.get(uri);
                    if (byteBufferZ == null) {
                        byteBufferZ = AbstractC2841wm0.z(context, uri);
                        i80.put(uri, byteBufferZ);
                    }
                    if (byteBufferZ == null || !H(objNewInstance, byteBufferZ, qa.b, qa.c, qa.d)) {
                        break;
                    }
                    i2++;
                } else {
                    Typeface typefaceI = I(objNewInstance);
                    if (typefaceI != null) {
                        return Typeface.create(typefaceI, i);
                    }
                }
            }
        }
        return null;
    }
}
