package androidx.appcompat.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.util.Xml;
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/* JADX INFO: compiled from: ResourceManagerInternal.java */
/* JADX INFO: loaded from: classes.dex */
public final class q0 {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private static q0 f1057i;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private WeakHashMap<Context, j.h<ColorStateList>> f1059a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private j.g<String, e> f1060b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private j.h<String> f1061c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final WeakHashMap<Context, j.d<WeakReference<Drawable.ConstantState>>> f1062d = new WeakHashMap<>(0);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private TypedValue f1063e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private boolean f1064f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private f f1065g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private static final PorterDuff.Mode f1056h = PorterDuff.Mode.SRC_IN;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private static final c f1058j = new c(6);

    /* JADX INFO: compiled from: ResourceManagerInternal.java */
    static class a implements e {
        a() {
        }

        @Override // androidx.appcompat.widget.q0.e
        public Drawable a(Context context, XmlPullParser xmlPullParser, AttributeSet attributeSet, Resources.Theme theme) {
            try {
                return e.a.l(context, context.getResources(), xmlPullParser, attributeSet, theme);
            } catch (Exception e3) {
                Log.e("AsldcInflateDelegate", "Exception while inflating <animated-selector>", e3);
                return null;
            }
        }
    }

    /* JADX INFO: compiled from: ResourceManagerInternal.java */
    private static class b implements e {
        b() {
        }

        @Override // androidx.appcompat.widget.q0.e
        public Drawable a(Context context, XmlPullParser xmlPullParser, AttributeSet attributeSet, Resources.Theme theme) {
            try {
                return androidx.vectordrawable.graphics.drawable.b.a(context, context.getResources(), xmlPullParser, attributeSet, theme);
            } catch (Exception e3) {
                Log.e("AvdcInflateDelegate", "Exception while inflating <animated-vector>", e3);
                return null;
            }
        }
    }

    /* JADX INFO: compiled from: ResourceManagerInternal.java */
    private static class c extends j.e<Integer, PorterDuffColorFilter> {
        public c(int i3) {
            super(i3);
        }

        private static int h(int i3, PorterDuff.Mode mode) {
            return ((i3 + 31) * 31) + mode.hashCode();
        }

        PorterDuffColorFilter i(int i3, PorterDuff.Mode mode) {
            return c(Integer.valueOf(h(i3, mode)));
        }

        PorterDuffColorFilter j(int i3, PorterDuff.Mode mode, PorterDuffColorFilter porterDuffColorFilter) {
            return d(Integer.valueOf(h(i3, mode)), porterDuffColorFilter);
        }
    }

    /* JADX INFO: compiled from: ResourceManagerInternal.java */
    static class d implements e {
        d() {
        }

        @Override // androidx.appcompat.widget.q0.e
        public Drawable a(Context context, XmlPullParser xmlPullParser, AttributeSet attributeSet, Resources.Theme theme) {
            String classAttribute = attributeSet.getClassAttribute();
            if (classAttribute != null) {
                try {
                    Drawable drawable = (Drawable) d.class.getClassLoader().loadClass(classAttribute).asSubclass(Drawable.class).getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
                    if (Build.VERSION.SDK_INT >= 21) {
                        f.c.c(drawable, context.getResources(), xmlPullParser, attributeSet, theme);
                    } else {
                        drawable.inflate(context.getResources(), xmlPullParser, attributeSet);
                    }
                    return drawable;
                } catch (Exception e3) {
                    Log.e("DrawableDelegate", "Exception while inflating <drawable>", e3);
                }
            }
            return null;
        }
    }

    /* JADX INFO: compiled from: ResourceManagerInternal.java */
    private interface e {
        Drawable a(Context context, XmlPullParser xmlPullParser, AttributeSet attributeSet, Resources.Theme theme);
    }

    /* JADX INFO: compiled from: ResourceManagerInternal.java */
    public interface f {
        Drawable a(q0 q0Var, Context context, int i3);

        boolean b(Context context, int i3, Drawable drawable);

        ColorStateList c(Context context, int i3);

        boolean d(Context context, int i3, Drawable drawable);

        PorterDuff.Mode e(int i3);
    }

    /* JADX INFO: compiled from: ResourceManagerInternal.java */
    private static class g implements e {
        g() {
        }

        @Override // androidx.appcompat.widget.q0.e
        public Drawable a(Context context, XmlPullParser xmlPullParser, AttributeSet attributeSet, Resources.Theme theme) {
            try {
                return androidx.vectordrawable.graphics.drawable.h.c(context.getResources(), xmlPullParser, attributeSet, theme);
            } catch (Exception e3) {
                Log.e("VdcInflateDelegate", "Exception while inflating <vector>", e3);
                return null;
            }
        }
    }

    private void a(String str, e eVar) {
        if (this.f1060b == null) {
            this.f1060b = new j.g<>();
        }
        this.f1060b.put(str, eVar);
    }

    private synchronized boolean b(Context context, long j3, Drawable drawable) {
        Drawable.ConstantState constantState = drawable.getConstantState();
        if (constantState == null) {
            return false;
        }
        j.d<WeakReference<Drawable.ConstantState>> dVar = this.f1062d.get(context);
        if (dVar == null) {
            dVar = new j.d<>();
            this.f1062d.put(context, dVar);
        }
        dVar.i(j3, new WeakReference<>(constantState));
        return true;
    }

    private void c(Context context, int i3, ColorStateList colorStateList) {
        if (this.f1059a == null) {
            this.f1059a = new WeakHashMap<>();
        }
        j.h<ColorStateList> hVar = this.f1059a.get(context);
        if (hVar == null) {
            hVar = new j.h<>();
            this.f1059a.put(context, hVar);
        }
        hVar.a(i3, colorStateList);
    }

    private void d(Context context) {
        if (this.f1064f) {
            return;
        }
        this.f1064f = true;
        Drawable drawableJ = j(context, f.d.f4605a);
        if (drawableJ == null || !q(drawableJ)) {
            this.f1064f = false;
            throw new IllegalStateException("This app has been built with an incorrect configuration. Please configure your build for VectorDrawableCompat.");
        }
    }

    private static long e(TypedValue typedValue) {
        return (((long) typedValue.assetCookie) << 32) | ((long) typedValue.data);
    }

    private Drawable f(Context context, int i3) {
        if (this.f1063e == null) {
            this.f1063e = new TypedValue();
        }
        TypedValue typedValue = this.f1063e;
        context.getResources().getValue(i3, typedValue, true);
        long jE = e(typedValue);
        Drawable drawableI = i(context, jE);
        if (drawableI != null) {
            return drawableI;
        }
        f fVar = this.f1065g;
        Drawable drawableA = fVar == null ? null : fVar.a(this, context, i3);
        if (drawableA != null) {
            drawableA.setChangingConfigurations(typedValue.changingConfigurations);
            b(context, jE, drawableA);
        }
        return drawableA;
    }

    private static PorterDuffColorFilter g(ColorStateList colorStateList, PorterDuff.Mode mode, int[] iArr) {
        if (colorStateList == null || mode == null) {
            return null;
        }
        return l(colorStateList.getColorForState(iArr, 0), mode);
    }

    public static synchronized q0 h() {
        if (f1057i == null) {
            q0 q0Var = new q0();
            f1057i = q0Var;
            p(q0Var);
        }
        return f1057i;
    }

    private synchronized Drawable i(Context context, long j3) {
        j.d<WeakReference<Drawable.ConstantState>> dVar = this.f1062d.get(context);
        if (dVar == null) {
            return null;
        }
        WeakReference<Drawable.ConstantState> weakReferenceE = dVar.e(j3);
        if (weakReferenceE != null) {
            Drawable.ConstantState constantState = weakReferenceE.get();
            if (constantState != null) {
                return constantState.newDrawable(context.getResources());
            }
            dVar.j(j3);
        }
        return null;
    }

    public static synchronized PorterDuffColorFilter l(int i3, PorterDuff.Mode mode) {
        PorterDuffColorFilter porterDuffColorFilterI;
        c cVar = f1058j;
        porterDuffColorFilterI = cVar.i(i3, mode);
        if (porterDuffColorFilterI == null) {
            porterDuffColorFilterI = new PorterDuffColorFilter(i3, mode);
            cVar.j(i3, mode, porterDuffColorFilterI);
        }
        return porterDuffColorFilterI;
    }

    private ColorStateList n(Context context, int i3) {
        j.h<ColorStateList> hVar;
        WeakHashMap<Context, j.h<ColorStateList>> weakHashMap = this.f1059a;
        if (weakHashMap == null || (hVar = weakHashMap.get(context)) == null) {
            return null;
        }
        return hVar.e(i3);
    }

    private static void p(q0 q0Var) {
        if (Build.VERSION.SDK_INT < 24) {
            q0Var.a("vector", new g());
            q0Var.a("animated-vector", new b());
            q0Var.a("animated-selector", new a());
            q0Var.a("drawable", new d());
        }
    }

    private static boolean q(Drawable drawable) {
        return (drawable instanceof androidx.vectordrawable.graphics.drawable.h) || "android.graphics.drawable.VectorDrawable".equals(drawable.getClass().getName());
    }

    private Drawable r(Context context, int i3) {
        int next;
        j.g<String, e> gVar = this.f1060b;
        if (gVar == null || gVar.isEmpty()) {
            return null;
        }
        j.h<String> hVar = this.f1061c;
        if (hVar != null) {
            String strE = hVar.e(i3);
            if ("appcompat_skip_skip".equals(strE) || (strE != null && this.f1060b.get(strE) == null)) {
                return null;
            }
        } else {
            this.f1061c = new j.h<>();
        }
        if (this.f1063e == null) {
            this.f1063e = new TypedValue();
        }
        TypedValue typedValue = this.f1063e;
        Resources resources = context.getResources();
        resources.getValue(i3, typedValue, true);
        long jE = e(typedValue);
        Drawable drawableI = i(context, jE);
        if (drawableI != null) {
            return drawableI;
        }
        CharSequence charSequence = typedValue.string;
        if (charSequence != null && charSequence.toString().endsWith(".xml")) {
            try {
                XmlResourceParser xml = resources.getXml(i3);
                AttributeSet attributeSetAsAttributeSet = Xml.asAttributeSet(xml);
                do {
                    next = xml.next();
                    if (next == 2) {
                        break;
                    }
                } while (next != 1);
                if (next != 2) {
                    throw new XmlPullParserException("No start tag found");
                }
                String name = xml.getName();
                this.f1061c.a(i3, name);
                e eVar = this.f1060b.get(name);
                if (eVar != null) {
                    drawableI = eVar.a(context, xml, attributeSetAsAttributeSet, context.getTheme());
                }
                if (drawableI != null) {
                    drawableI.setChangingConfigurations(typedValue.changingConfigurations);
                    b(context, jE, drawableI);
                }
            } catch (Exception e3) {
                Log.e("ResourceManagerInternal", "Exception while inflating drawable", e3);
            }
        }
        if (drawableI == null) {
            this.f1061c.a(i3, "appcompat_skip_skip");
        }
        return drawableI;
    }

    private Drawable v(Context context, int i3, boolean z2, Drawable drawable) {
        ColorStateList colorStateListM = m(context, i3);
        if (colorStateListM == null) {
            f fVar = this.f1065g;
            if ((fVar == null || !fVar.d(context, i3, drawable)) && !x(context, i3, drawable) && z2) {
                return null;
            }
            return drawable;
        }
        if (i0.a(drawable)) {
            drawable = drawable.mutate();
        }
        Drawable drawableR = androidx.core.graphics.drawable.a.r(drawable);
        androidx.core.graphics.drawable.a.o(drawableR, colorStateListM);
        PorterDuff.Mode modeO = o(i3);
        if (modeO == null) {
            return drawableR;
        }
        androidx.core.graphics.drawable.a.p(drawableR, modeO);
        return drawableR;
    }

    static void w(Drawable drawable, y0 y0Var, int[] iArr) {
        int[] state = drawable.getState();
        if (i0.a(drawable)) {
            if (!(drawable.mutate() == drawable)) {
                Log.d("ResourceManagerInternal", "Mutated drawable is not the same instance as the input.");
                return;
            }
        }
        if ((drawable instanceof LayerDrawable) && drawable.isStateful()) {
            drawable.setState(new int[0]);
            drawable.setState(state);
        }
        boolean z2 = y0Var.f1142d;
        if (z2 || y0Var.f1141c) {
            drawable.setColorFilter(g(z2 ? y0Var.f1139a : null, y0Var.f1141c ? y0Var.f1140b : f1056h, iArr));
        } else {
            drawable.clearColorFilter();
        }
        if (Build.VERSION.SDK_INT <= 23) {
            drawable.invalidateSelf();
        }
    }

    public synchronized Drawable j(Context context, int i3) {
        return k(context, i3, false);
    }

    synchronized Drawable k(Context context, int i3, boolean z2) {
        Drawable drawableR;
        d(context);
        drawableR = r(context, i3);
        if (drawableR == null) {
            drawableR = f(context, i3);
        }
        if (drawableR == null) {
            drawableR = androidx.core.content.a.d(context, i3);
        }
        if (drawableR != null) {
            drawableR = v(context, i3, z2, drawableR);
        }
        if (drawableR != null) {
            i0.b(drawableR);
        }
        return drawableR;
    }

    synchronized ColorStateList m(Context context, int i3) {
        ColorStateList colorStateListN;
        colorStateListN = n(context, i3);
        if (colorStateListN == null) {
            f fVar = this.f1065g;
            colorStateListN = fVar == null ? null : fVar.c(context, i3);
            if (colorStateListN != null) {
                c(context, i3, colorStateListN);
            }
        }
        return colorStateListN;
    }

    PorterDuff.Mode o(int i3) {
        f fVar = this.f1065g;
        if (fVar == null) {
            return null;
        }
        return fVar.e(i3);
    }

    public synchronized void s(Context context) {
        j.d<WeakReference<Drawable.ConstantState>> dVar = this.f1062d.get(context);
        if (dVar != null) {
            dVar.b();
        }
    }

    synchronized Drawable t(Context context, i1 i1Var, int i3) {
        Drawable drawableR = r(context, i3);
        if (drawableR == null) {
            drawableR = i1Var.a(i3);
        }
        if (drawableR == null) {
            return null;
        }
        return v(context, i3, false, drawableR);
    }

    public synchronized void u(f fVar) {
        this.f1065g = fVar;
    }

    boolean x(Context context, int i3, Drawable drawable) {
        f fVar = this.f1065g;
        return fVar != null && fVar.b(context, i3, drawable);
    }
}
