package j0;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.fonts.FontVariationAxis;
import android.net.Uri;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
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.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class l extends j {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Class f28520g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final Constructor f28521h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final Method f28522i;

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

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final Method f28525l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final Method f28526m;

    public l() {
        Class<?> cls;
        Method method;
        Constructor<?> constructor;
        Method methodI;
        Method methodJ;
        Method method2;
        Method methodK;
        try {
            cls = Class.forName("android.graphics.FontFamily");
            constructor = cls.getConstructor(new Class[0]);
            methodI = i(cls);
            methodJ = j(cls);
            method2 = cls.getMethod("freeze", new Class[0]);
            method = cls.getMethod("abortCreation", new Class[0]);
            methodK = k(cls);
        } catch (ClassNotFoundException | NoSuchMethodException unused) {
            cls = null;
            method = null;
            constructor = null;
            methodI = null;
            methodJ = null;
            method2 = null;
            methodK = null;
        }
        this.f28520g = cls;
        this.f28521h = constructor;
        this.f28522i = methodI;
        this.f28523j = methodJ;
        this.f28524k = method2;
        this.f28525l = method;
        this.f28526m = methodK;
    }

    private boolean addFontFromAssetManager(Context context, Object obj, String str, int i10, int i11, int i12, @Nullable FontVariationAxis[] fontVariationAxisArr) {
        try {
            return ((Boolean) this.f28522i.invoke(obj, context.getAssets(), str, 0, Boolean.FALSE, Integer.valueOf(i10), Integer.valueOf(i11), Integer.valueOf(i12), fontVariationAxisArr)).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    public static Method i(Class cls) {
        Class<?> cls2 = Integer.TYPE;
        return cls.getMethod("addFontFromAssetManager", AssetManager.class, String.class, cls2, Boolean.TYPE, cls2, cls2, cls2, FontVariationAxis[].class);
    }

    public static Method j(Class cls) {
        Class<?> cls2 = Integer.TYPE;
        return cls.getMethod("addFontFromBuffer", ByteBuffer.class, cls2, FontVariationAxis[].class, cls2, cls2);
    }

    @Nullable
    private Object newFamily() {
        try {
            return this.f28521h.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            return null;
        }
    }

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

    @Nullable
    public Typeface createFromFamiliesWithDefault(Object obj) {
        try {
            Object objNewInstance = Array.newInstance((Class<?>) this.f28520g, 1);
            Array.set(objNewInstance, 0, obj);
            return (Typeface) this.f28526m.invoke(null, objNewInstance, -1, -1);
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    @Override // j0.j, j0.p
    @Nullable
    public Typeface createFromFontFamilyFilesResourceEntry(Context context, i0.f fVar, Resources resources, int i10) {
        if (this.f28522i == null) {
            return super.createFromFontFamilyFilesResourceEntry(context, fVar, resources, i10);
        }
        Object objNewFamily = newFamily();
        if (objNewFamily == null) {
            return null;
        }
        for (i0.g gVar : fVar.f26490a) {
            if (!addFontFromAssetManager(context, objNewFamily, gVar.f26491a, gVar.f26495e, gVar.f26492b, gVar.f26493c ? 1 : 0, FontVariationAxis.fromFontVariationSettings(gVar.getVariationSettings()))) {
                g(objNewFamily);
                return null;
            }
        }
        if (h(objNewFamily)) {
            return createFromFamiliesWithDefault(objNewFamily);
        }
        return null;
    }

    @Override // j0.j, j0.p
    @Nullable
    public Typeface createFromFontInfo(Context context, @Nullable CancellationSignal cancellationSignal, @NonNull n0.k[] kVarArr, int i10) {
        Typeface typefaceCreateFromFamiliesWithDefault;
        if (kVarArr.length < 1) {
            return null;
        }
        if (this.f28522i == null) {
            n0.k kVarD = d(i10, kVarArr);
            try {
                ParcelFileDescriptor parcelFileDescriptorOpenFileDescriptor = context.getContentResolver().openFileDescriptor(kVarD.f30748a, "r", cancellationSignal);
                if (parcelFileDescriptorOpenFileDescriptor == null) {
                    if (parcelFileDescriptorOpenFileDescriptor != null) {
                        parcelFileDescriptorOpenFileDescriptor.close();
                    }
                    return null;
                }
                try {
                    Typeface typefaceBuild = new Typeface.Builder(parcelFileDescriptorOpenFileDescriptor.getFileDescriptor()).setWeight(kVarD.f30750c).setItalic(kVarD.f30751d).build();
                    parcelFileDescriptorOpenFileDescriptor.close();
                    return typefaceBuild;
                } finally {
                }
            } catch (IOException unused) {
                return null;
            }
        }
        Map<Uri, ByteBuffer> fontInfoIntoByteBuffer = q.readFontInfoIntoByteBuffer(context, kVarArr, cancellationSignal);
        Object objNewFamily = newFamily();
        if (objNewFamily == null) {
            return null;
        }
        int length = kVarArr.length;
        int i11 = 0;
        boolean z10 = false;
        while (i11 < length) {
            n0.k kVar = kVarArr[i11];
            ByteBuffer byteBuffer = fontInfoIntoByteBuffer.get(kVar.f30748a);
            if (byteBuffer != null) {
                if (!((Boolean) this.f28523j.invoke(objNewFamily, byteBuffer, Integer.valueOf(kVar.f30749b), null, Integer.valueOf(kVar.f30750c), Integer.valueOf(kVar.f30751d ? 1 : 0))).booleanValue()) {
                    g(objNewFamily);
                    return null;
                }
                z10 = true;
            }
            i11++;
            z10 = z10;
        }
        if (!z10) {
            g(objNewFamily);
            return null;
        }
        if (h(objNewFamily) && (typefaceCreateFromFamiliesWithDefault = createFromFamiliesWithDefault(objNewFamily)) != null) {
            return Typeface.create(typefaceCreateFromFamiliesWithDefault, i10);
        }
        return null;
    }

    @Override // j0.p
    @Nullable
    @RequiresApi(29)
    public /* bridge */ /* synthetic */ Typeface createFromFontInfoWithFallback(@NonNull Context context, @Nullable CancellationSignal cancellationSignal, @NonNull List list, int i10) {
        return super.createFromFontInfoWithFallback(context, cancellationSignal, list, i10);
    }

    @Override // j0.p
    @Nullable
    public Typeface createFromResourcesFontFile(Context context, Resources resources, int i10, String str, int i11) {
        if (this.f28522i == null) {
            return super.createFromResourcesFontFile(context, resources, i10, str, i11);
        }
        Object objNewFamily = newFamily();
        if (objNewFamily == null) {
            return null;
        }
        if (!addFontFromAssetManager(context, objNewFamily, str, 0, -1, -1, null)) {
            g(objNewFamily);
            return null;
        }
        if (h(objNewFamily)) {
            return createFromFamiliesWithDefault(objNewFamily);
        }
        return null;
    }

    public final void g(Object obj) {
        try {
            this.f28525l.invoke(obj, new Object[0]);
        } catch (IllegalAccessException | InvocationTargetException unused) {
        }
    }

    public final boolean h(Object obj) {
        try {
            return ((Boolean) this.f28524k.invoke(obj, new Object[0])).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    public Method k(Class cls) throws NoSuchMethodException {
        Class cls2 = Integer.TYPE;
        Method declaredMethod = Typeface.class.getDeclaredMethod("createFromFamiliesWithDefault", Array.newInstance((Class<?>) cls, 1).getClass(), cls2, cls2);
        declaredMethod.setAccessible(true);
        return declaredMethod;
    }
}
