package G;

import S4.m;
import android.content.Context;
import android.graphics.Typeface;
import android.net.Uri;
import android.util.Log;
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.util.List;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final Class f774a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Constructor f775b;

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

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

    static {
        Class<?> cls;
        Constructor<?> constructor;
        Method method;
        Method method2;
        try {
            cls = Class.forName("android.graphics.FontFamily");
            constructor = cls.getConstructor(new Class[0]);
            Class cls2 = Integer.TYPE;
            method = cls.getMethod("addFontWeightStyle", ByteBuffer.class, cls2, List.class, cls2, Boolean.TYPE);
            method2 = Typeface.class.getMethod("createFromFamiliesWithDefault", Array.newInstance(cls, 1).getClass());
        } catch (ClassNotFoundException | NoSuchMethodException e2) {
            Log.e("TypefaceCompatApi24Impl", e2.getClass().getName(), e2);
            cls = null;
            constructor = null;
            method = null;
            method2 = null;
        }
        f775b = constructor;
        f774a = cls;
        f776c = method;
        f777d = method2;
    }

    public static boolean Q(Object obj, ByteBuffer byteBuffer, int i, int i5, boolean z5) {
        try {
            return ((Boolean) f776c.invoke(obj, byteBuffer, Integer.valueOf(i), null, Integer.valueOf(i5), Boolean.valueOf(z5))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

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

    /* JADX WARN: Removed duplicated region for block: B:33:0x005c  */
    /* JADX WARN: Removed duplicated region for block: B:54:0x0068 A[SYNTHETIC] */
    @Override // S4.d
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public final android.graphics.Typeface o(android.content.Context r17, F.e r18, android.content.res.Resources r19, int r20) {
        /*
            r16 = this;
            r1 = 0
            r0 = 0
            java.lang.reflect.Constructor r2 = G.g.f775b     // Catch: java.lang.Throwable -> Lb
            java.lang.Object[] r3 = new java.lang.Object[r0]     // Catch: java.lang.Throwable -> Lb
            java.lang.Object r2 = r2.newInstance(r3)     // Catch: java.lang.Throwable -> Lb
            goto Lc
        Lb:
            r2 = r1
        Lc:
            if (r2 != 0) goto Lf
            goto L68
        Lf:
            r3 = r18
            F.f[] r3 = r3.f581a
            int r4 = r3.length
            r5 = r0
        L15:
            if (r5 >= r4) goto L71
            r6 = r3[r5]
            int r0 = r6.f587f
            java.io.File r7 = S4.m.q(r17)
            if (r7 != 0) goto L25
            r8 = r19
        L23:
            r0 = r1
            goto L59
        L25:
            r8 = r19
            boolean r0 = S4.m.d(r7, r8, r0)     // Catch: java.lang.Throwable -> L6c
            if (r0 != 0) goto L31
            r7.delete()
            goto L23
        L31:
            java.io.FileInputStream r9 = new java.io.FileInputStream     // Catch: java.io.IOException -> L55 java.lang.Throwable -> L6c
            r9.<init>(r7)     // Catch: java.io.IOException -> L55 java.lang.Throwable -> L6c
            java.nio.channels.FileChannel r10 = r9.getChannel()     // Catch: java.lang.Throwable -> L4a
            long r14 = r10.size()     // Catch: java.lang.Throwable -> L4a
            java.nio.channels.FileChannel$MapMode r11 = java.nio.channels.FileChannel.MapMode.READ_ONLY     // Catch: java.lang.Throwable -> L4a
            r12 = 0
            java.nio.MappedByteBuffer r0 = r10.map(r11, r12, r14)     // Catch: java.lang.Throwable -> L4a
            r9.close()     // Catch: java.io.IOException -> L55 java.lang.Throwable -> L6c
            goto L56
        L4a:
            r0 = move-exception
            r10 = r0
            r9.close()     // Catch: java.lang.Throwable -> L50
            goto L54
        L50:
            r0 = move-exception
            r10.addSuppressed(r0)     // Catch: java.io.IOException -> L55 java.lang.Throwable -> L6c
        L54:
            throw r10     // Catch: java.io.IOException -> L55 java.lang.Throwable -> L6c
        L55:
            r0 = r1
        L56:
            r7.delete()
        L59:
            if (r0 != 0) goto L5c
            goto L68
        L5c:
            int r7 = r6.f583b
            boolean r9 = r6.f584c
            int r6 = r6.f586e
            boolean r0 = Q(r2, r0, r6, r7, r9)
            if (r0 != 0) goto L69
        L68:
            return r1
        L69:
            int r5 = r5 + 1
            goto L15
        L6c:
            r0 = move-exception
            r7.delete()
            throw r0
        L71:
            android.graphics.Typeface r0 = R(r2)
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: G.g.o(android.content.Context, F.e, android.content.res.Resources, int):android.graphics.Typeface");
    }

    @Override // S4.d
    public final Typeface p(Context context, M.k[] kVarArr, int i) {
        Object objNewInstance;
        int i5 = 0;
        try {
            objNewInstance = f775b.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            objNewInstance = null;
        }
        if (objNewInstance != null) {
            r.j jVar = new r.j(0);
            int length = kVarArr.length;
            while (true) {
                if (i5 >= length) {
                    Typeface typefaceR = R(objNewInstance);
                    if (typefaceR != null) {
                        return Typeface.create(typefaceR, i);
                    }
                } else {
                    M.k kVar = kVarArr[i5];
                    Uri uri = kVar.f2137a;
                    ByteBuffer byteBufferV = (ByteBuffer) jVar.get(uri);
                    if (byteBufferV == null) {
                        byteBufferV = m.v(context, uri);
                        jVar.put(uri, byteBufferV);
                    }
                    if (byteBufferV == null) {
                        break;
                    }
                    if (!Q(objNewInstance, byteBufferV, kVar.f2138b, kVar.f2139c, kVar.f2140d)) {
                        break;
                    }
                    i5++;
                }
            }
        }
        return null;
    }
}
