package c.f.e;

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 android.util.Log;
import c.f.d.c.c;
import c.f.i.b;
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.Map;

/* JADX INFO: compiled from: TypefaceCompatApi26Impl.java */
/* JADX INFO: loaded from: classes.dex */
public class g extends e {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    protected final Class<?> f1389g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    protected final Constructor<?> f1390h;

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    protected final Method f1393k;
    protected final Method l;
    protected final Method m;

    public g() throws NoSuchMethodException {
        Method methodW;
        Constructor<?> constructorY;
        Method methodU;
        Method methodV;
        Method methodZ;
        Method methodT;
        Class<?> cls = null;
        try {
            Class<?> clsX = x();
            constructorY = y(clsX);
            methodU = u(clsX);
            methodV = v(clsX);
            methodZ = z(clsX);
            methodT = t(clsX);
            methodW = w(clsX);
            cls = clsX;
        } catch (ClassNotFoundException | NoSuchMethodException e2) {
            Log.e("TypefaceCompatApi26Impl", "Unable to collect necessary methods for class " + e2.getClass().getName(), e2);
            methodW = null;
            constructorY = null;
            methodU = null;
            methodV = null;
            methodZ = null;
            methodT = null;
        }
        this.f1389g = cls;
        this.f1390h = constructorY;
        this.f1391i = methodU;
        this.f1392j = methodV;
        this.f1393k = methodZ;
        this.l = methodT;
        this.m = methodW;
    }

    private Object n() {
        try {
            return this.f1390h.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            return null;
        }
    }

    private void o(Object obj) {
        try {
            this.l.invoke(obj, new Object[0]);
        } catch (IllegalAccessException | InvocationTargetException unused) {
        }
    }

    private boolean p(Context context, Object obj, String str, int i2, int i3, int i4, FontVariationAxis[] fontVariationAxisArr) {
        try {
            return ((Boolean) this.f1391i.invoke(obj, context.getAssets(), str, 0, Boolean.FALSE, Integer.valueOf(i2), Integer.valueOf(i3), Integer.valueOf(i4), fontVariationAxisArr)).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    private boolean q(Object obj, ByteBuffer byteBuffer, int i2, int i3, int i4) {
        try {
            return ((Boolean) this.f1392j.invoke(obj, byteBuffer, Integer.valueOf(i2), null, Integer.valueOf(i3), Integer.valueOf(i4))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    private boolean r(Object obj) {
        try {
            return ((Boolean) this.f1393k.invoke(obj, new Object[0])).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    private boolean s() {
        if (this.f1391i == null) {
            Log.w("TypefaceCompatApi26Impl", "Unable to collect necessary private methods. Fallback to legacy implementation.");
        }
        return this.f1391i != null;
    }

    @Override // c.f.e.e, c.f.e.j
    public Typeface b(Context context, c.b bVar, Resources resources, int i2) {
        if (!s()) {
            return super.b(context, bVar, resources, i2);
        }
        Object objN = n();
        if (objN == null) {
            return null;
        }
        for (c.C0025c c0025c : bVar.a()) {
            if (!p(context, objN, c0025c.a(), c0025c.c(), c0025c.e(), c0025c.f() ? 1 : 0, FontVariationAxis.fromFontVariationSettings(c0025c.d()))) {
                o(objN);
                return null;
            }
        }
        if (r(objN)) {
            return k(objN);
        }
        return null;
    }

    @Override // c.f.e.e, c.f.e.j
    public Typeface c(Context context, CancellationSignal cancellationSignal, b.f[] fVarArr, int i2) {
        Typeface typefaceK;
        if (fVarArr.length < 1) {
            return null;
        }
        if (!s()) {
            b.f fVarH = h(fVarArr, i2);
            try {
                ParcelFileDescriptor parcelFileDescriptorOpenFileDescriptor = context.getContentResolver().openFileDescriptor(fVarH.c(), "r", cancellationSignal);
                if (parcelFileDescriptorOpenFileDescriptor == null) {
                    if (parcelFileDescriptorOpenFileDescriptor != null) {
                        parcelFileDescriptorOpenFileDescriptor.close();
                    }
                    return null;
                }
                try {
                    Typeface typefaceBuild = new Typeface.Builder(parcelFileDescriptorOpenFileDescriptor.getFileDescriptor()).setWeight(fVarH.d()).setItalic(fVarH.e()).build();
                    if (parcelFileDescriptorOpenFileDescriptor != null) {
                        parcelFileDescriptorOpenFileDescriptor.close();
                    }
                    return typefaceBuild;
                } finally {
                }
            } catch (IOException unused) {
                return null;
            }
        }
        Map<Uri, ByteBuffer> mapI = c.f.i.b.i(context, fVarArr, cancellationSignal);
        Object objN = n();
        if (objN == null) {
            return null;
        }
        boolean z = false;
        for (b.f fVar : fVarArr) {
            ByteBuffer byteBuffer = mapI.get(fVar.c());
            if (byteBuffer != null) {
                if (!q(objN, byteBuffer, fVar.b(), fVar.d(), fVar.e() ? 1 : 0)) {
                    o(objN);
                    return null;
                }
                z = true;
            }
        }
        if (!z) {
            o(objN);
            return null;
        }
        if (r(objN) && (typefaceK = k(objN)) != null) {
            return Typeface.create(typefaceK, i2);
        }
        return null;
    }

    @Override // c.f.e.j
    public Typeface e(Context context, Resources resources, int i2, String str, int i3) {
        if (!s()) {
            return super.e(context, resources, i2, str, i3);
        }
        Object objN = n();
        if (objN == null) {
            return null;
        }
        if (!p(context, objN, str, 0, -1, -1, null)) {
            o(objN);
            return null;
        }
        if (r(objN)) {
            return k(objN);
        }
        return null;
    }

    protected Typeface k(Object obj) {
        try {
            Object objNewInstance = Array.newInstance(this.f1389g, 1);
            Array.set(objNewInstance, 0, obj);
            return (Typeface) this.m.invoke(null, objNewInstance, -1, -1);
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    protected Method t(Class<?> cls) throws NoSuchMethodException {
        return cls.getMethod("abortCreation", new Class[0]);
    }

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

    protected Method v(Class<?> cls) throws NoSuchMethodException {
        Class<?> cls2 = Integer.TYPE;
        return cls.getMethod("addFontFromBuffer", ByteBuffer.class, cls2, FontVariationAxis[].class, cls2, cls2);
    }

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

    protected Class<?> x() throws ClassNotFoundException {
        return Class.forName("android.graphics.FontFamily");
    }

    protected Constructor<?> y(Class<?> cls) throws NoSuchMethodException {
        return cls.getConstructor(new Class[0]);
    }

    protected Method z(Class<?> cls) throws NoSuchMethodException {
        return cls.getMethod("freeze", new Class[0]);
    }
}
