package p073z;

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 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;
import p014e0.C0655g;
import p071y.C1812f;

/* renamed from: z.x */
/* loaded from: classes.dex */
public class C1913x extends C1907r {

    /* renamed from: g */
    public final Class<?> f4704g;

    /* renamed from: h */
    public final Constructor<?> f4705h;

    /* renamed from: i */
    public final Method f4706i;

    /* renamed from: j */
    public final Method f4707j;

    /* renamed from: k */
    public final Method f4708k;

    /* renamed from: l */
    public final Method f4709l;

    /* renamed from: m */
    public final Method f4710m;

    public C1913x() {
        Class<?> cls;
        Constructor<?> constructor;
        Method method;
        Method method2;
        Method method3;
        Method method4;
        Method method5;
        try {
            cls = m43z();
            constructor = m57A(cls);
            method = m45w(cls);
            method2 = m44x(cls);
            method3 = m56B(cls);
            method4 = m46v(cls);
            method5 = mo41y(cls);
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            Log.e("TypefaceCompatApi26Impl", "Unable to collect necessary methods for class " + e.getClass().getName(), e);
            cls = null;
            constructor = null;
            method = null;
            method2 = null;
            method3 = null;
            method4 = null;
            method5 = null;
        }
        this.f4704g = cls;
        this.f4705h = constructor;
        this.f4706i = method;
        this.f4707j = method2;
        this.f4708k = method3;
        this.f4709l = method4;
        this.f4710m = method5;
    }

    /* renamed from: p */
    private Object m52p() {
        try {
            return this.f4705h.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            return null;
        }
    }

    /* renamed from: A */
    public Constructor<?> m57A(Class<?> cls) {
        return cls.getConstructor(new Class[0]);
    }

    /* renamed from: B */
    public Method m56B(Class<?> cls) {
        return cls.getMethod("freeze", new Class[0]);
    }

    @Override // p073z.C1907r, p073z.C1899o0
    /* renamed from: b */
    public Typeface mo55b(Context context, C1812f.C1815c c1815c, Resources resources, int i) {
        C1812f.C1816d[] m309a;
        FontVariationAxis[] fromFontVariationSettings;
        if (m47u()) {
            Object m52p = m52p();
            if (m52p == null) {
                return null;
            }
            for (C1812f.C1816d c1816d : c1815c.m309a()) {
                String m308a = c1816d.m308a();
                int m306c = c1816d.m306c();
                int m304e = c1816d.m304e();
                boolean m303f = c1816d.m303f();
                fromFontVariationSettings = FontVariationAxis.fromFontVariationSettings(c1816d.m305d());
                if (!m50r(context, m52p, m308a, m306c, m304e, m303f ? 1 : 0, fromFontVariationSettings)) {
                    m51q(m52p);
                    return null;
                }
            }
            if (m48t(m52p)) {
                return mo42m(m52p);
            }
            return null;
        }
        return super.mo55b(context, c1815c, resources, i);
    }

    @Override // p073z.C1907r, p073z.C1899o0
    /* renamed from: c */
    public Typeface mo54c(Context context, CancellationSignal cancellationSignal, C0655g.C0657b[] c0657bArr, int i) {
        Typeface mo42m;
        ParcelFileDescriptor openFileDescriptor;
        Typeface.Builder weight;
        Typeface.Builder italic;
        Typeface build;
        if (c0657bArr.length < 1) {
            return null;
        }
        if (!m47u()) {
            C0655g.C0657b mo90i = mo90i(c0657bArr, i);
            try {
                openFileDescriptor = context.getContentResolver().openFileDescriptor(mo90i.m3189d(), "r", cancellationSignal);
                if (openFileDescriptor == null) {
                    if (openFileDescriptor != null) {
                        openFileDescriptor.close();
                    }
                    return null;
                }
                weight = new Typeface.Builder(openFileDescriptor.getFileDescriptor()).setWeight(mo90i.m3188e());
                italic = weight.setItalic(mo90i.m3187f());
                build = italic.build();
                openFileDescriptor.close();
                return build;
            } catch (IOException unused) {
                return null;
            }
        }
        Map<Uri, ByteBuffer> m73h = C1904p0.m73h(context, c0657bArr, cancellationSignal);
        Object m52p = m52p();
        if (m52p == null) {
            return null;
        }
        boolean z = false;
        for (C0655g.C0657b c0657b : c0657bArr) {
            ByteBuffer byteBuffer = m73h.get(c0657b.m3189d());
            if (byteBuffer != null) {
                if (!m49s(m52p, byteBuffer, c0657b.m3190c(), c0657b.m3188e(), c0657b.m3187f() ? 1 : 0)) {
                    m51q(m52p);
                    return null;
                }
                z = true;
            }
        }
        if (!z) {
            m51q(m52p);
            return null;
        } else if (m48t(m52p) && (mo42m = mo42m(m52p)) != null) {
            return Typeface.create(mo42m, i);
        } else {
            return null;
        }
    }

    @Override // p073z.C1899o0
    /* renamed from: e */
    public Typeface mo53e(Context context, Resources resources, int i, String str, int i2) {
        if (m47u()) {
            Object m52p = m52p();
            if (m52p == null) {
                return null;
            }
            if (!m50r(context, m52p, str, 0, -1, -1, null)) {
                m51q(m52p);
                return null;
            } else if (m48t(m52p)) {
                return mo42m(m52p);
            } else {
                return null;
            }
        }
        return super.mo53e(context, resources, i, str, i2);
    }

    /* renamed from: m */
    public Typeface mo42m(Object obj) {
        try {
            Object newInstance = Array.newInstance(this.f4704g, 1);
            Array.set(newInstance, 0, obj);
            return (Typeface) this.f4710m.invoke(null, newInstance, -1, -1);
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    /* renamed from: q */
    public final void m51q(Object obj) {
        try {
            this.f4709l.invoke(obj, new Object[0]);
        } catch (IllegalAccessException | InvocationTargetException unused) {
        }
    }

    /* renamed from: r */
    public final boolean m50r(Context context, Object obj, String str, int i, int i2, int i3, FontVariationAxis[] fontVariationAxisArr) {
        try {
            return ((Boolean) this.f4706i.invoke(obj, context.getAssets(), str, 0, Boolean.FALSE, Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i3), fontVariationAxisArr)).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    /* renamed from: s */
    public final boolean m49s(Object obj, ByteBuffer byteBuffer, int i, int i2, int i3) {
        try {
            return ((Boolean) this.f4707j.invoke(obj, byteBuffer, Integer.valueOf(i), null, Integer.valueOf(i2), Integer.valueOf(i3))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    /* renamed from: t */
    public final boolean m48t(Object obj) {
        try {
            return ((Boolean) this.f4708k.invoke(obj, new Object[0])).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    /* renamed from: u */
    public final boolean m47u() {
        if (this.f4706i == null) {
            Log.w("TypefaceCompatApi26Impl", "Unable to collect necessary private methods. Fallback to legacy implementation.");
        }
        return this.f4706i != null;
    }

    /* renamed from: v */
    public Method m46v(Class<?> cls) {
        return cls.getMethod("abortCreation", new Class[0]);
    }

    /* renamed from: w */
    public Method m45w(Class<?> cls) {
        Class<?> cls2 = Integer.TYPE;
        return cls.getMethod("addFontFromAssetManager", AssetManager.class, String.class, cls2, Boolean.TYPE, cls2, cls2, cls2, FontVariationAxis[].class);
    }

    /* renamed from: x */
    public Method m44x(Class<?> cls) {
        Class<?> cls2 = Integer.TYPE;
        return cls.getMethod("addFontFromBuffer", ByteBuffer.class, cls2, FontVariationAxis[].class, cls2, cls2);
    }

    /* renamed from: y */
    public Method mo41y(Class<?> cls) {
        Class cls2 = Integer.TYPE;
        Method declaredMethod = Typeface.class.getDeclaredMethod("createFromFamiliesWithDefault", Array.newInstance(cls, 1).getClass(), cls2, cls2);
        declaredMethod.setAccessible(true);
        return declaredMethod;
    }

    /* renamed from: z */
    public Class<?> m43z() {
        return Class.forName("android.graphics.FontFamily");
    }
}