package G;

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

/* JADX INFO: loaded from: classes.dex */
public class h extends f {

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

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

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

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

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

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

    public h() {
        Class<?> cls;
        Method method;
        Constructor<?> constructor;
        Method methodX;
        Method method2;
        Method method3;
        Method methodY;
        try {
            cls = Class.forName("android.graphics.FontFamily");
            constructor = cls.getConstructor(new Class[0]);
            methodX = X(cls);
            Class cls2 = Integer.TYPE;
            method2 = cls.getMethod("addFontFromBuffer", ByteBuffer.class, cls2, FontVariationAxis[].class, cls2, cls2);
            method3 = cls.getMethod("freeze", new Class[0]);
            method = cls.getMethod("abortCreation", new Class[0]);
            methodY = Y(cls);
        } catch (ClassNotFoundException | NoSuchMethodException e2) {
            Log.e("TypefaceCompatApi26Impl", "Unable to collect necessary methods for class ".concat(e2.getClass().getName()), e2);
            cls = null;
            method = null;
            constructor = null;
            methodX = null;
            method2 = null;
            method3 = null;
            methodY = null;
        }
        this.f778f = cls;
        this.f779g = constructor;
        this.f780h = methodX;
        this.i = method2;
        this.f781j = method3;
        this.f782k = method;
        this.f783l = methodY;
    }

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

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

    public final boolean T(Context context, Object obj, String str, int i, int i5, int i6, FontVariationAxis[] fontVariationAxisArr) {
        try {
            return ((Boolean) this.f780h.invoke(obj, context.getAssets(), str, 0, Boolean.FALSE, Integer.valueOf(i), Integer.valueOf(i5), Integer.valueOf(i6), fontVariationAxisArr)).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

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

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

    public final Object W() {
        try {
            return this.f779g.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            return null;
        }
    }

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

    @Override // G.f, S4.d
    public final Typeface o(Context context, F.e eVar, Resources resources, int i) {
        Method method = this.f780h;
        if (method == null) {
            Log.w("TypefaceCompatApi26Impl", "Unable to collect necessary private methods. Fallback to legacy implementation.");
        }
        if (method == null) {
            return super.o(context, eVar, resources, i);
        }
        Object objW = W();
        if (objW != null) {
            F.f[] fVarArr = eVar.f581a;
            int length = fVarArr.length;
            int i5 = 0;
            while (i5 < length) {
                F.f fVar = fVarArr[i5];
                String str = fVar.f582a;
                FontVariationAxis[] fontVariationAxisArrFromFontVariationSettings = FontVariationAxis.fromFontVariationSettings(fVar.f585d);
                Context context2 = context;
                if (!T(context2, objW, str, fVar.f586e, fVar.f583b, fVar.f584c ? 1 : 0, fontVariationAxisArrFromFontVariationSettings)) {
                    S(objW);
                    return null;
                }
                i5++;
                context = context2;
            }
            if (V(objW)) {
                return U(objW);
            }
        }
        return null;
    }

    @Override // G.f, S4.d
    public final Typeface p(Context context, M.k[] kVarArr, int i) {
        Typeface typefaceU;
        boolean zBooleanValue;
        if (kVarArr.length >= 1) {
            Method method = this.f780h;
            if (method == null) {
                Log.w("TypefaceCompatApi26Impl", "Unable to collect necessary private methods. Fallback to legacy implementation.");
            }
            if (method != null) {
                HashMap map = new HashMap();
                for (M.k kVar : kVarArr) {
                    if (kVar.f2142f == 0) {
                        Uri uri = kVar.f2137a;
                        if (!map.containsKey(uri)) {
                            map.put(uri, m.v(context, uri));
                        }
                    }
                }
                Map mapUnmodifiableMap = Collections.unmodifiableMap(map);
                Object objW = W();
                if (objW != null) {
                    int length = kVarArr.length;
                    int i5 = 0;
                    boolean z5 = false;
                    while (i5 < length) {
                        M.k kVar2 = kVarArr[i5];
                        ByteBuffer byteBuffer = (ByteBuffer) mapUnmodifiableMap.get(kVar2.f2137a);
                        if (byteBuffer != null) {
                            try {
                                zBooleanValue = ((Boolean) this.i.invoke(objW, byteBuffer, Integer.valueOf(kVar2.f2138b), null, Integer.valueOf(kVar2.f2139c), Integer.valueOf(kVar2.f2140d ? 1 : 0))).booleanValue();
                            } catch (IllegalAccessException | InvocationTargetException unused) {
                                zBooleanValue = false;
                            }
                            if (!zBooleanValue) {
                                S(objW);
                                return null;
                            }
                            z5 = true;
                        }
                        i5++;
                        z5 = z5;
                    }
                    if (!z5) {
                        S(objW);
                        return null;
                    }
                    if (V(objW) && (typefaceU = U(objW)) != null) {
                        return Typeface.create(typefaceU, i);
                    }
                }
            } else {
                M.k kVarU = u(kVarArr, i);
                try {
                    ParcelFileDescriptor parcelFileDescriptorOpenFileDescriptor = context.getContentResolver().openFileDescriptor(kVarU.f2137a, "r", null);
                    if (parcelFileDescriptorOpenFileDescriptor != null) {
                        try {
                            Typeface typefaceBuild = new Typeface.Builder(parcelFileDescriptorOpenFileDescriptor.getFileDescriptor()).setWeight(kVarU.f2139c).setItalic(kVarU.f2140d).build();
                            parcelFileDescriptorOpenFileDescriptor.close();
                            return typefaceBuild;
                        } finally {
                        }
                    }
                    if (parcelFileDescriptorOpenFileDescriptor != null) {
                        parcelFileDescriptorOpenFileDescriptor.close();
                        return null;
                    }
                } catch (IOException unused2) {
                }
            }
        }
        return null;
    }

    @Override // S4.d
    public final Typeface s(Context context, Resources resources, int i, String str, int i5) {
        Method method = this.f780h;
        if (method == null) {
            Log.w("TypefaceCompatApi26Impl", "Unable to collect necessary private methods. Fallback to legacy implementation.");
        }
        if (method == null) {
            return super.s(context, resources, i, str, i5);
        }
        Object objW = W();
        if (objW != null) {
            if (!T(context, objW, str, 0, -1, -1, null)) {
                S(objW);
                return null;
            }
            if (V(objW)) {
                return U(objW);
            }
        }
        return null;
    }
}
