package j0;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.CancellationSignal;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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 k extends p {

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

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

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

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

    static {
        Class<?> cls;
        Method method;
        Constructor<?> constructor;
        Method method2;
        try {
            cls = Class.forName("android.graphics.FontFamily");
            constructor = cls.getConstructor(new Class[0]);
            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());
        } catch (ClassNotFoundException | NoSuchMethodException unused) {
            cls = null;
            method = null;
            constructor = null;
            method2 = null;
        }
        f28517c = constructor;
        f28516b = cls;
        f28518d = method2;
        f28519e = method;
    }

    public static boolean e(Object obj, ByteBuffer byteBuffer, int i10, int i11, boolean z10) {
        try {
            return ((Boolean) f28518d.invoke(obj, byteBuffer, Integer.valueOf(i10), null, Integer.valueOf(i11), Boolean.valueOf(z10))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

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

    @Override // j0.p
    public final Typeface b(Context context, Typeface typeface, int i10, boolean z10) {
        Typeface typefaceCreateWeightStyle;
        try {
            typefaceCreateWeightStyle = s.createWeightStyle(typeface, i10, z10);
        } catch (RuntimeException unused) {
            typefaceCreateWeightStyle = null;
        }
        return typefaceCreateWeightStyle == null ? super.b(context, typeface, i10, z10) : typefaceCreateWeightStyle;
    }

    @Override // j0.p
    @Nullable
    public Typeface createFromFontFamilyFilesResourceEntry(Context context, i0.f fVar, Resources resources, int i10) {
        Object objNewInstance;
        try {
            objNewInstance = f28517c.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            objNewInstance = null;
        }
        if (objNewInstance == null) {
            return null;
        }
        for (i0.g gVar : fVar.f26490a) {
            ByteBuffer byteBufferCopyToDirectBuffer = q.copyToDirectBuffer(context, resources, gVar.f26496f);
            if (byteBufferCopyToDirectBuffer == null) {
                return null;
            }
            if (!e(objNewInstance, byteBufferCopyToDirectBuffer, gVar.f26495e, gVar.f26492b, gVar.f26493c)) {
                return null;
            }
        }
        return f(objNewInstance);
    }

    @Override // j0.p
    @Nullable
    public Typeface createFromFontInfo(Context context, @Nullable CancellationSignal cancellationSignal, @NonNull n0.k[] kVarArr, int i10) {
        Object objNewInstance;
        try {
            objNewInstance = f28517c.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            objNewInstance = null;
        }
        if (objNewInstance == null) {
            return null;
        }
        androidx.collection.s sVar = new androidx.collection.s(0);
        for (n0.k kVar : kVarArr) {
            Uri uri = kVar.f30748a;
            ByteBuffer byteBufferMmap = (ByteBuffer) sVar.get(uri);
            if (byteBufferMmap == null) {
                byteBufferMmap = q.mmap(context, cancellationSignal, uri);
                sVar.put(uri, byteBufferMmap);
            }
            if (byteBufferMmap == null) {
                return null;
            }
            if (!e(objNewInstance, byteBufferMmap, kVar.f30749b, kVar.f30750c, kVar.f30751d)) {
                return null;
            }
        }
        Typeface typefaceF = f(objNewInstance);
        if (typefaceF == null) {
            return null;
        }
        return Typeface.create(typefaceF, i10);
    }
}
