package j0;

import android.content.Context;
import android.graphics.Typeface;
import android.util.SparseArray;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.reflect.Field;

/* JADX INFO: loaded from: classes.dex */
public abstract class r {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final Field f28528a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final androidx.collection.l f28529b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final Object f28530c;

    static {
        Field declaredField;
        try {
            declaredField = Typeface.class.getDeclaredField("native_instance");
            declaredField.setAccessible(true);
        } catch (Exception unused) {
            declaredField = null;
        }
        f28528a = declaredField;
        f28529b = new androidx.collection.l(3);
        f28530c = new Object();
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$PrimitiveArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    @Nullable
    public static Typeface createWeightStyle(@NonNull p pVar, @NonNull Context context, @NonNull Typeface typeface, int i10, boolean z10) {
        Field field = f28528a;
        if (field == null) {
            return null;
        }
        int i11 = (i10 << 1) | (z10 ? 1 : 0);
        synchronized (f28530c) {
            try {
                try {
                    long jLongValue = ((Number) field.get(typeface)).longValue();
                    androidx.collection.l lVar = f28529b;
                    SparseArray sparseArray = (SparseArray) lVar.c(jLongValue);
                    if (sparseArray == null) {
                        sparseArray = new SparseArray(4);
                        lVar.e(jLongValue, sparseArray);
                    } else {
                        Typeface typeface2 = (Typeface) sparseArray.get(i11);
                        if (typeface2 != null) {
                            return typeface2;
                        }
                    }
                    Typeface bestFontFromFamily = getBestFontFromFamily(pVar, context, typeface, i10, z10);
                    if (bestFontFromFamily == null) {
                        int i12 = 1;
                        Object[] objArr = i10 >= 600;
                        if (objArr != true && !z10) {
                            i12 = 0;
                        } else if (objArr != true) {
                            i12 = 2;
                        } else if (z10) {
                            i12 = 3;
                        }
                        bestFontFromFamily = Typeface.create(typeface, i12);
                    }
                    sparseArray.put(i11, bestFontFromFamily);
                    return bestFontFromFamily;
                } catch (IllegalAccessException e10) {
                    throw new RuntimeException(e10);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Nullable
    private static Typeface getBestFontFromFamily(@NonNull p pVar, @NonNull Context context, @NonNull Typeface typeface, int i10, boolean z10) {
        i0.f fontFamily = pVar.getFontFamily(typeface);
        if (fontFamily == null) {
            return null;
        }
        return pVar.createFromFontFamilyFilesResourceEntry(context, fontFamily, context.getResources(), i10, z10);
    }
}
