package a.c.d;

import a.c.c.b.c;
import a.c.f.b;
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;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    protected final Class f188a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    protected final Constructor f189b;

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

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

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

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

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

    public f() throws NoSuchMethodException {
        Method methodS;
        Constructor constructorU;
        Method methodQ;
        Method methodR;
        Method methodV;
        Method methodP;
        Class cls = null;
        try {
            Class clsT = t();
            constructorU = u(clsT);
            methodQ = q(clsT);
            methodR = r(clsT);
            methodV = v(clsT);
            methodP = p(clsT);
            methodS = s(clsT);
            cls = clsT;
        } catch (ClassNotFoundException | NoSuchMethodException e2) {
            Log.e("TypefaceCompatApi26Impl", "Unable to collect necessary methods for class " + e2.getClass().getName(), e2);
            methodS = null;
            constructorU = null;
            methodQ = null;
            methodR = null;
            methodV = null;
            methodP = null;
        }
        this.f188a = cls;
        this.f189b = constructorU;
        this.f190c = methodQ;
        this.f191d = methodR;
        this.f192e = methodV;
        this.f193f = methodP;
        this.f194g = methodS;
    }

    private void i(Object obj) {
        try {
            this.f193f.invoke(obj, new Object[0]);
        } catch (IllegalAccessException | InvocationTargetException e2) {
            throw new RuntimeException(e2);
        }
    }

    private boolean j(Context context, Object obj, String str, int i, int i2, int i3, FontVariationAxis[] fontVariationAxisArr) {
        try {
            return ((Boolean) this.f190c.invoke(obj, context.getAssets(), str, 0, Boolean.FALSE, Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i3), fontVariationAxisArr)).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException e2) {
            throw new RuntimeException(e2);
        }
    }

    private boolean k(Object obj, ByteBuffer byteBuffer, int i, int i2, int i3) {
        try {
            return ((Boolean) this.f191d.invoke(obj, byteBuffer, Integer.valueOf(i), null, Integer.valueOf(i2), Integer.valueOf(i3))).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException e2) {
            throw new RuntimeException(e2);
        }
    }

    private boolean m(Object obj) {
        try {
            return ((Boolean) this.f192e.invoke(obj, new Object[0])).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException e2) {
            throw new RuntimeException(e2);
        }
    }

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

    private Object o() {
        try {
            return this.f189b.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e2) {
            throw new RuntimeException(e2);
        }
    }

    @Override // a.c.d.h
    public Typeface a(Context context, c.b bVar, Resources resources, int i) {
        if (!n()) {
            return super.a(context, bVar, resources, i);
        }
        Object objO = o();
        for (c.C0005c c0005c : bVar.a()) {
            if (!j(context, objO, c0005c.a(), c0005c.c(), c0005c.e(), c0005c.f() ? 1 : 0, FontVariationAxis.fromFontVariationSettings(c0005c.d()))) {
                i(objO);
                return null;
            }
        }
        if (m(objO)) {
            return l(objO);
        }
        return null;
    }

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

    @Override // a.c.d.h
    public Typeface d(Context context, Resources resources, int i, String str, int i2) {
        if (!n()) {
            return super.d(context, resources, i, str, i2);
        }
        Object objO = o();
        if (!j(context, objO, str, 0, -1, -1, null)) {
            i(objO);
            return null;
        }
        if (m(objO)) {
            return l(objO);
        }
        return null;
    }

    protected Typeface l(Object obj) {
        try {
            Object objNewInstance = Array.newInstance((Class<?>) this.f188a, 1);
            Array.set(objNewInstance, 0, obj);
            return (Typeface) this.f194g.invoke(null, objNewInstance, -1, -1);
        } catch (IllegalAccessException | InvocationTargetException e2) {
            throw new RuntimeException(e2);
        }
    }

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

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

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

    protected Method s(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;
    }

    protected Class t() throws ClassNotFoundException {
        return Class.forName("android.graphics.FontFamily");
    }

    protected Constructor u(Class cls) throws NoSuchMethodException {
        return cls.getConstructor(new Class[0]);
    }

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