package F;

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

/* JADX INFO: loaded from: classes.dex */
public final class g extends U2.a {

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final Class f2242h;
    public static final Constructor i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final Method f2243j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static final Method f2244k;

    static {
        Method method;
        Class<?> cls;
        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 e8) {
            Log.e("TypefaceCompatApi24Impl", e8.getClass().getName(), e8);
            method = null;
            cls = null;
            method2 = null;
        }
        i = constructor;
        f2242h = cls;
        f2243j = method2;
        f2244k = method;
    }

    public static boolean O(Object obj, ByteBuffer byteBuffer, int i5, int i8, boolean z8) {
        try {
            return ((Boolean) f2243j.invoke(obj, byteBuffer, Integer.valueOf(i5), null, Integer.valueOf(i8), Boolean.valueOf(z8))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    public static Typeface P(Object obj) {
        try {
            Object objNewInstance = Array.newInstance((Class<?>) f2242h, 1);
            Array.set(objNewInstance, 0, obj);
            return (Typeface) f2244k.invoke(null, objNewInstance);
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:33:0x005b  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x0067 A[SYNTHETIC] */
    @Override // U2.a
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Typeface h(Context context, E.e eVar, Resources resources, int i5) throws IllegalAccessException, InstantiationException, InvocationTargetException {
        Object objNewInstance;
        MappedByteBuffer map;
        FileInputStream fileInputStream;
        try {
            objNewInstance = i.newInstance(null);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            objNewInstance = null;
        }
        if (objNewInstance != null) {
            for (E.f fVar : eVar.f1937a) {
                int i8 = fVar.f1943f;
                File fileW = W2.g.w(context);
                if (fileW != null) {
                    try {
                        if (W2.g.i(fileW, resources, i8)) {
                            try {
                                fileInputStream = new FileInputStream(fileW);
                            } 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) {
                                    if (O(objNewInstance, map, fVar.f1942e, fVar.f1939b, fVar.f1940c)) {
                                    }
                                }
                            } finally {
                            }
                        }
                    } finally {
                        fileW.delete();
                    }
                }
                map = null;
                if (map == null) {
                }
            }
            return P(objNewInstance);
        }
        return null;
    }

    @Override // U2.a
    public final Typeface i(Context context, J.j[] jVarArr, int i5) {
        Object objNewInstance;
        try {
            objNewInstance = i.newInstance(null);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            objNewInstance = null;
        }
        if (objNewInstance != null) {
            int i8 = 0;
            q.i iVar = new q.i(0);
            int length = jVarArr.length;
            while (true) {
                if (i8 >= length) {
                    Typeface typefaceP = P(objNewInstance);
                    if (typefaceP != null) {
                        return Typeface.create(typefaceP, i5);
                    }
                } else {
                    J.j jVar = jVarArr[i8];
                    Uri uri = jVar.f3435a;
                    ByteBuffer byteBufferB = (ByteBuffer) iVar.get(uri);
                    if (byteBufferB == null) {
                        byteBufferB = W2.g.B(context, uri);
                        iVar.put(uri, byteBufferB);
                    }
                    if (byteBufferB == null) {
                        break;
                    }
                    if (!O(objNewInstance, byteBufferB, jVar.f3436b, jVar.f3437c, jVar.f3438d)) {
                        break;
                    }
                    i8++;
                }
            }
        }
        return null;
    }
}
