package c.f.d;

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.g.m;
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.Collections;
import java.util.HashMap;
import java.util.Map;

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

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

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

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

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

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

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

    public f() throws NoSuchMethodException {
        Method methodO;
        Method method;
        Constructor<?> constructor;
        Method methodM;
        Method methodN;
        Method method2;
        Class<?> cls = null;
        try {
            Class<?> cls2 = Class.forName("android.graphics.FontFamily");
            constructor = cls2.getConstructor(new Class[0]);
            methodM = m(cls2);
            methodN = n(cls2);
            method2 = cls2.getMethod("freeze", new Class[0]);
            method = cls2.getMethod("abortCreation", new Class[0]);
            methodO = o(cls2);
            cls = cls2;
        } catch (ClassNotFoundException | NoSuchMethodException e2) {
            StringBuilder sbB = d.b.a.a.a.B("Unable to collect necessary methods for class ");
            sbB.append(e2.getClass().getName());
            Log.e("TypefaceCompatApi26Impl", sbB.toString(), e2);
            methodO = null;
            method = null;
            constructor = null;
            methodM = null;
            methodN = null;
            method2 = null;
        }
        this.f1342g = cls;
        this.f1343h = constructor;
        this.f1344i = methodM;
        this.f1345j = methodN;
        this.f1346k = method2;
        this.f1347l = method;
        this.m = methodO;
    }

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

    @Override // c.f.d.e, c.f.d.j
    public Typeface a(Context context, c.f.c.b.h hVar, Resources resources, int i2) {
        if (!k()) {
            return super.a(context, hVar, resources, i2);
        }
        Object objL = l();
        if (objL == null) {
            return null;
        }
        for (c.f.c.b.i iVar : hVar.a) {
            if (!h(context, objL, iVar.a, iVar.f1310e, iVar.f1307b, iVar.f1308c ? 1 : 0, FontVariationAxis.fromFontVariationSettings(iVar.f1309d))) {
                g(objL);
                return null;
            }
        }
        if (j(objL)) {
            return i(objL);
        }
        return null;
    }

    @Override // c.f.d.j
    public Typeface b(Context context, CancellationSignal cancellationSignal, m[] mVarArr, int i2) {
        Typeface typefaceI;
        boolean zBooleanValue;
        if (mVarArr.length < 1) {
            return null;
        }
        if (!k()) {
            m mVar = (m) j.d(mVarArr, i2, new i(this));
            try {
                ParcelFileDescriptor parcelFileDescriptorOpenFileDescriptor = context.getContentResolver().openFileDescriptor(mVar.a, "r", cancellationSignal);
                if (parcelFileDescriptorOpenFileDescriptor == null) {
                    if (parcelFileDescriptorOpenFileDescriptor != null) {
                        parcelFileDescriptorOpenFileDescriptor.close();
                    }
                    return null;
                }
                try {
                    Typeface typefaceBuild = new Typeface.Builder(parcelFileDescriptorOpenFileDescriptor.getFileDescriptor()).setWeight(mVar.f1375c).setItalic(mVar.f1376d).build();
                    parcelFileDescriptorOpenFileDescriptor.close();
                    return typefaceBuild;
                } finally {
                }
            } catch (IOException unused) {
                return null;
            }
        }
        HashMap map = new HashMap();
        for (m mVar2 : mVarArr) {
            if (mVar2.f1377e == 0) {
                Uri uri = mVar2.a;
                if (!map.containsKey(uri)) {
                    map.put(uri, c.f.b.h.w(context, cancellationSignal, uri));
                }
            }
        }
        Map mapUnmodifiableMap = Collections.unmodifiableMap(map);
        Object objL = l();
        if (objL == null) {
            return null;
        }
        int length = mVarArr.length;
        int i3 = 0;
        boolean z = false;
        while (i3 < length) {
            m mVar3 = mVarArr[i3];
            ByteBuffer byteBuffer = (ByteBuffer) mapUnmodifiableMap.get(mVar3.a);
            if (byteBuffer != null) {
                try {
                    zBooleanValue = ((Boolean) this.f1345j.invoke(objL, byteBuffer, Integer.valueOf(mVar3.f1374b), null, Integer.valueOf(mVar3.f1375c), Integer.valueOf(mVar3.f1376d ? 1 : 0))).booleanValue();
                } catch (IllegalAccessException | InvocationTargetException unused2) {
                    zBooleanValue = false;
                }
                if (!zBooleanValue) {
                    g(objL);
                    return null;
                }
                z = true;
            }
            i3++;
            z = z;
        }
        if (!z) {
            g(objL);
            return null;
        }
        if (j(objL) && (typefaceI = i(objL)) != null) {
            return Typeface.create(typefaceI, i2);
        }
        return null;
    }

    @Override // c.f.d.j
    public Typeface c(Context context, Resources resources, int i2, String str, int i3) {
        if (!k()) {
            return super.c(context, resources, i2, str, i3);
        }
        Object objL = l();
        if (objL == null) {
            return null;
        }
        if (!h(context, objL, str, 0, -1, -1, null)) {
            g(objL);
            return null;
        }
        if (j(objL)) {
            return i(objL);
        }
        return null;
    }

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

    public final boolean h(Context context, Object obj, String str, int i2, int i3, int i4, FontVariationAxis[] fontVariationAxisArr) {
        try {
            return ((Boolean) this.f1344i.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;
        }
    }

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

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

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

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

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

    public Method o(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;
    }
}
