package i0;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.SparseArray;
import android.util.TypedValue;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.FontRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import java.io.IOException;
import java.util.WeakHashMap;
import org.xmlpull.v1.XmlPullParserException;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final ThreadLocal f26513a = new ThreadLocal();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final WeakHashMap f26514b = new WeakHashMap(0);

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final Object f26515c = new Object();

    private static void addColorStateListToCache(@NonNull n nVar, @ColorRes int i10, @NonNull ColorStateList colorStateList, @Nullable Resources.Theme theme) {
        synchronized (f26515c) {
            try {
                WeakHashMap weakHashMap = f26514b;
                SparseArray sparseArray = (SparseArray) weakHashMap.get(nVar);
                if (sparseArray == null) {
                    sparseArray = new SparseArray();
                    weakHashMap.put(nVar, sparseArray);
                }
                sparseArray.append(i10, new m(colorStateList, nVar.f26505a.getConfiguration(), theme));
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:21:0x003c, code lost:
    
        if (r2.f26504c == r5.hashCode()) goto L22;
     */
    @Nullable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private static ColorStateList getCachedColorStateList(@NonNull n nVar, @ColorRes int i10) {
        m mVar;
        synchronized (f26515c) {
            try {
                SparseArray sparseArray = (SparseArray) f26514b.get(nVar);
                if (sparseArray != null && sparseArray.size() > 0 && (mVar = (m) sparseArray.get(i10)) != null) {
                    if (mVar.f26503b.equals(nVar.f26505a.getConfiguration())) {
                        Resources.Theme theme = nVar.f26506b;
                        if (theme != null || mVar.f26504c != 0) {
                            if (theme != null) {
                            }
                        }
                        return mVar.f26502a;
                    }
                    sparseArray.remove(i10);
                }
                return null;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Nullable
    public static Typeface getCachedFont(@NonNull Context context, @FontRes int i10) throws Resources.NotFoundException {
        if (context.isRestricted()) {
            return null;
        }
        return loadFont(context, i10, new TypedValue(), 0, null, null, false, true);
    }

    @ColorInt
    public static int getColor(@NonNull Resources resources, @ColorRes int i10, @Nullable Resources.Theme theme) throws Resources.NotFoundException {
        return resources.getColor(i10, theme);
    }

    @Nullable
    public static ColorStateList getColorStateList(@NonNull Resources resources, @ColorRes int i10, @Nullable Resources.Theme theme) throws Resources.NotFoundException {
        n nVar = new n(resources, theme);
        ColorStateList cachedColorStateList = getCachedColorStateList(nVar, i10);
        if (cachedColorStateList != null) {
            return cachedColorStateList;
        }
        ColorStateList colorStateListInflateColorStateList = inflateColorStateList(resources, i10, theme);
        if (colorStateListInflateColorStateList == null) {
            return l.getColorStateList(resources, i10, theme);
        }
        addColorStateListToCache(nVar, i10, colorStateListInflateColorStateList, theme);
        return colorStateListInflateColorStateList;
    }

    @Nullable
    public static Drawable getDrawable(@NonNull Resources resources, @DrawableRes int i10, @Nullable Resources.Theme theme) throws Resources.NotFoundException {
        return resources.getDrawable(i10, theme);
    }

    @Nullable
    public static Drawable getDrawableForDensity(@NonNull Resources resources, @DrawableRes int i10, int i11, @Nullable Resources.Theme theme) throws Resources.NotFoundException {
        return resources.getDrawableForDensity(i10, i11, theme);
    }

    @Nullable
    public static Typeface getFont(@NonNull Context context, @FontRes int i10) throws Resources.NotFoundException {
        if (context.isRestricted()) {
            return null;
        }
        return loadFont(context, i10, new TypedValue(), 0, null, null, false, false);
    }

    @Nullable
    private static ColorStateList inflateColorStateList(Resources resources, int i10, @Nullable Resources.Theme theme) {
        ThreadLocal threadLocal = f26513a;
        TypedValue typedValue = (TypedValue) threadLocal.get();
        if (typedValue == null) {
            typedValue = new TypedValue();
            threadLocal.set(typedValue);
        }
        resources.getValue(i10, typedValue, true);
        int i11 = typedValue.type;
        if (i11 >= 28 && i11 <= 31) {
            return null;
        }
        try {
            return c.createFromXml(resources, resources.getXml(i10), theme);
        } catch (Exception unused) {
            return null;
        }
    }

    private static Typeface loadFont(@NonNull Context context, int i10, @NonNull TypedValue typedValue, int i11, @Nullable p pVar, @Nullable Handler handler, boolean z10, boolean z11) {
        Resources resources = context.getResources();
        resources.getValue(i10, typedValue, true);
        Typeface typefaceLoadFont = loadFont(context, resources, typedValue, i10, i11, pVar, handler, z10, z11);
        if (typefaceLoadFont != null || pVar != null || z11) {
            return typefaceLoadFont;
        }
        throw new Resources.NotFoundException("Font resource ID #0x" + Integer.toHexString(i10) + " could not be retrieved.");
    }

    public static void getFont(@NonNull Context context, @FontRes int i10, @NonNull p pVar, @Nullable Handler handler) throws Resources.NotFoundException {
        p0.g.checkNotNull(pVar);
        if (context.isRestricted()) {
            pVar.callbackFailAsync(-4, handler);
        } else {
            loadFont(context, i10, new TypedValue(), 0, pVar, handler, false, false);
        }
    }

    private static Typeface loadFont(@NonNull Context context, Resources resources, @NonNull TypedValue typedValue, int i10, int i11, @Nullable p pVar, @Nullable Handler handler, boolean z10, boolean z11) {
        CharSequence charSequence = typedValue.string;
        if (charSequence != null) {
            String string = charSequence.toString();
            if (!string.startsWith("res/")) {
                if (pVar != null) {
                    pVar.callbackFailAsync(-3, handler);
                }
                return null;
            }
            Typeface typefaceFindFromCache = j0.i.findFromCache(resources, i10, string, typedValue.assetCookie, i11);
            if (typefaceFindFromCache != null) {
                if (pVar != null) {
                    pVar.callbackSuccessAsync(typefaceFindFromCache, handler);
                }
                return typefaceFindFromCache;
            }
            if (z11) {
                return null;
            }
            try {
                if (string.toLowerCase().endsWith(".xml")) {
                    e eVar = i.parse(resources.getXml(i10), resources);
                    if (eVar != null) {
                        return j0.i.createFromResourcesFamilyXml(context, eVar, resources, i10, string, typedValue.assetCookie, i11, pVar, handler, z10);
                    }
                    if (pVar != null) {
                        pVar.callbackFailAsync(-3, handler);
                    }
                    return null;
                }
                Typeface typefaceCreateFromResourcesFontFile = j0.i.createFromResourcesFontFile(context, resources, i10, string, typedValue.assetCookie, i11);
                if (pVar != null) {
                    if (typefaceCreateFromResourcesFontFile != null) {
                        pVar.callbackSuccessAsync(typefaceCreateFromResourcesFontFile, handler);
                    } else {
                        pVar.callbackFailAsync(-3, handler);
                    }
                }
                return typefaceCreateFromResourcesFontFile;
            } catch (IOException | XmlPullParserException unused) {
                if (pVar != null) {
                    pVar.callbackFailAsync(-3, handler);
                }
                return null;
            }
        }
        throw new Resources.NotFoundException("Resource \"" + resources.getResourceName(i10) + "\" (" + Integer.toHexString(i10) + ") is not a Font: " + typedValue);
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f557c})
    public static Typeface getFont(@NonNull Context context, @FontRes int i10, @NonNull TypedValue typedValue, int i11, @Nullable p pVar) throws Resources.NotFoundException {
        if (context.isRestricted()) {
            return null;
        }
        return loadFont(context, i10, typedValue, i11, pVar, null, true, false);
    }
}
