content: package androidx.appcompat.widget;
  
  import android.content.Context;
  import android.content.res.ColorStateList;
  import android.content.res.Resources;
  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 androidx.appcompat.graphics.drawable.AnimatedStateListDrawableCompat;
  import androidx.appcompat.resources.Compatibility;
  import androidx.appcompat.resources.R;
  import androidx.collection.LongSparseArray;
  import androidx.collection.LruCache;
  import androidx.collection.SimpleArrayMap;
  import androidx.collection.SparseArrayCompat;
  import androidx.core.content.ContextCompat;
  import androidx.core.graphics.drawable.DrawableCompat;
  import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat;
  import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
  import java.lang.ref.WeakReference;
  import java.util.WeakHashMap;
  import org.xmlpull.v1.XmlPullParser;
  
  public final class ResourceManagerInternal {
      private static final ColorFilterLruCache COLOR_FILTER_CACHE = new ColorFilterLruCache(6);
      private static final boolean DEBUG = false;
      private static final PorterDuff.Mode DEFAULT_MODE = PorterDuff.Mode.SRC_IN;
      private static ResourceManagerInternal INSTANCE = null;
      private static final String PLATFORM_VD_CLAZZ = "android.graphics.drawable.VectorDrawable";
      private static final String SKIP_DRAWABLE_TAG = "appcompat_skip_skip";
      private static final String TAG = "ResourceManagerInternal";
      private SimpleArrayMap<String, InflateDelegate> mDelegates;
      private final WeakHashMap<Context, LongSparseArray<WeakReference<Drawable.ConstantState>>> mDrawableCaches = new WeakHashMap<>(0);
      private boolean mHasCheckedVectorDrawableSetup;
      private ResourceManagerHooks mHooks;
      private SparseArrayCompat<String> mKnownDrawableIdTags;
      private WeakHashMap<Context, SparseArrayCompat<ColorStateList>> mTintLists;
      private TypedValue mTypedValue;
  
      static class AsldcInflateDelegate implements InflateDelegate {
          AsldcInflateDelegate() {
          }
  
          public Drawable createFromXmlInner(Context context, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) {
              try {
                  return AnimatedStateListDrawableCompat.createFromXmlInner(context, context.getResources(), parser, attrs, theme);
              } catch (Exception e) {
                  Log.e("AsldcInflateDelegate", "Exception while inflating <animated-selector>", e);
                  return null;
              }
          }
      }
  
      private static class AvdcInflateDelegate implements InflateDelegate {
          AvdcInflateDelegate() {
          }
  
          public Drawable createFromXmlInner(Context context, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) {
              try {
                  return AnimatedVectorDrawableCompat.createFromXmlInner(context, context.getResources(), parser, attrs, theme);
              } catch (Exception e) {
                  Log.e("AvdcInflateDelegate", "Exception while inflating <animated-vector>", e);
                  return null;
              }
          }
      }
  
      private static class ColorFilterLruCache extends LruCache<Integer, PorterDuffColorFilter> {
          public ColorFilterLruCache(int maxSize) {
              super(maxSize);
          }
  
          private static int generateCacheKey(int color, PorterDuff.Mode mode) {
              return (((1 * 31) + color) * 31) + mode.hashCode();
          }
  
          /* access modifiers changed from: package-private */
          public PorterDuffColorFilter get(int color, PorterDuff.Mode mode) {
              return (PorterDuffColorFilter) get(Integer.valueOf(generateCacheKey(color, mode)));
          }
  
          /* access modifiers changed from: package-private */
          public PorterDuffColorFilter put(int color, PorterDuff.Mode mode, PorterDuffColorFilter filter) {
              return (PorterDuffColorFilter) put(Integer.valueOf(generateCacheKey(color, mode)), filter);
          }
      }
  
      static class DrawableDelegate implements InflateDelegate {
          DrawableDelegate() {
          }
  
          public Drawable createFromXmlInner(Context context, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) {
              String classAttribute = attrs.getClassAttribute();
              if (classAttribute == null) {
                  return null;
              }
              try {
                  Drawable drawable = (Drawable) DrawableDelegate.class.getClassLoader().loadClass(classAttribute).asSubclass(Drawable.class).getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
                  if (Build.VERSION.SDK_INT >= 21) {
                      Compatibility.Api21Impl.inflate(drawable, context.getResources(), parser, attrs, theme);
                  } else {
                      drawable.inflate(context.getResources(), parser, attrs);
                  }
                  return drawable;
              } catch (Exception e) {
                  Log.e("DrawableDelegate", "Exception while inflating <drawable>", e);
                  return null;
              }
          }
      }
  
      private interface InflateDelegate {
          Drawable createFromXmlInner(Context context, XmlPullParser xmlPullParser, AttributeSet attributeSet, Resources.Theme theme);
      }
  
      public interface ResourceManagerHooks {
          Drawable createDrawableFor(ResourceManagerInternal resourceManagerInternal, Context context, int i);
  
          ColorStateList getTintListForDrawableRes(Context context, int i);
  
          PorterDuff.Mode getTintModeForDrawableRes(int i);
  
          boolean tintDrawable(Context context, int i, Drawable drawable);
  
          boolean tintDrawableUsingColorFilter(Context context, int i, Drawable drawable);
      }
  
      private static class VdcInflateDelegate implements InflateDelegate {
          VdcInflateDelegate() {
          }
  
          public Drawable createFromXmlInner(Context context, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) {
              try {
                  return VectorDrawableCompat.createFromXmlInner(context.getResources(), parser, attrs, theme);
              } catch (Exception e) {
                  Log.e("VdcInflateDelegate", "Exception while inflating <vector>", e);
                  return null;
              }
          }
      }
  
      private void addDelegate(String tagName, InflateDelegate delegate) {
          if (this.mDelegates == null) {
              this.mDelegates = new SimpleArrayMap<>();
          }
          this.mDelegates.put(tagName, delegate);
      }
  
      private synchronized boolean addDrawableToCache(Context context, long key, Drawable drawable) {
          Drawable.ConstantState constantState = drawable.getConstantState();
          if (constantState == null) {
              return false;
          }
          LongSparseArray longSparseArray = this.mDrawableCaches.get(context);
          if (longSparseArray == null) {
              longSparseArray = new LongSparseArray();
              this.mDrawableCaches.put(context, longSparseArray);
          }
          longSparseArray.put(key, new WeakReference(constantState));
          return true;
      }
  
      private void addTintListToCache(Context context, int resId, ColorStateList tintList) {
          if (this.mTintLists == null) {
              this.mTintLists = new WeakHashMap<>();
          }
          SparseArrayCompat sparseArrayCompat = this.mTintLists.get(context);
          if (sparseArrayCompat == null) {
              sparseArrayCompat = new SparseArrayCompat();
              this.mTintLists.put(context, sparseArrayCompat);
          }
          sparseArrayCompat.append(resId, tintList);
      }
  
      private void checkVectorDrawableSetup(Context context) {
          if (!this.mHasCheckedVectorDrawableSetup) {
              this.mHasCheckedVectorDrawableSetup = true;
              Drawable drawable = getDrawable(context, R.drawable.abc_vector_test);
              if (drawable == null || !isVectorDrawable(drawable)) {
                  this.mHasCheckedVectorDrawableSetup = false;
                  throw new IllegalStateException("This app has been built with an incorrect configuration. Please configure your build for VectorDrawableCompat.");
              }
          }
      }
  
      private static long createCacheKey(TypedValue tv) {
          return (((long) tv.assetCookie) << 32) | ((long) tv.data);
      }
  
      private Drawable createDrawableIfNeeded(Context context, int resId) {
          if (this.mTypedValue == null) {
              this.mTypedValue = new TypedValue();
          }
          TypedValue typedValue = this.mTypedValue;
          context.getResources().getValue(resId, typedValue, true);
          long createCacheKey = createCacheKey(typedValue);
          Drawable cachedDrawable = getCachedDrawable(context, createCacheKey);
          if (cachedDrawable != null) {
              return cachedDrawable;
          }
          ResourceManagerHooks resourceManagerHooks = this.mHooks;
          Drawable createDrawableFor = resourceManagerHooks == null ? null : resourceManagerHooks.createDrawableFor(this, context, resId);
          if (createDrawableFor != null) {
              createDrawableFor.setChangingConfigurations(typedValue.changingConfigurations);
              addDrawableToCache(context, createCacheKey, createDrawableFor);
          }
          return createDrawableFor;
      }
  
      private static PorterDuffColorFilter createTintFilter(ColorStateList tint, PorterDuff.Mode tintMode, int[] state) {
          if (tint == null || tintMode == null) {
              return null;
          }
          return getPorterDuffColorFilter(tint.getColorForState(state, 0), tintMode);
      }
  
      public static synchronized ResourceManagerInternal get() {
          ResourceManagerInternal resourceManagerInternal;
          synchronized (ResourceManagerInternal.class) {
              if (INSTANCE == null) {
                  ResourceManagerInternal resourceManagerInternal2 = new ResourceManagerInternal();
                  INSTANCE = resourceManagerInternal2;
                  installDefaultInflateDelegates(resourceManagerInternal2);
              }
              resourceManagerInternal = INSTANCE;
          }
          return resourceManagerInternal;
      }
  
      /* JADX WARNING: Code restructure failed: missing block: B:18:0x002c, code lost:
          return null;
       */
      /* Code decompiled incorrectly, please refer to instructions dump. */
      private synchronized android.graphics.drawable.Drawable getCachedDrawable(android.content.Context r5, long r6) {
          /*
              r4 = this;
              monitor-enter(r4)
              java.util.WeakHashMap<android.content.Context, androidx.collection.LongSparseArray<java.lang.ref.WeakReference<android.graphics.drawable.Drawable$ConstantState>>> r0 = r4.mDrawableCaches     // Catch:{ all -> 0x002d }
              java.lang.Object r0 = r0.get(r5)     // Catch:{ all -> 0x002d }
              androidx.collection.LongSparseArray r0 = (androidx.collection.LongSparseArray) r0     // Catch:{ all -> 0x002d }
              r1 = 0
              if (r0 != 0) goto L_0x000e
              monitor-exit(r4)
              return r1
          L_0x000e:
              java.lang.Object r2 = r0.get(r6)     // Catch:{ all -> 0x002d }
              java.lang.ref.WeakReference r2 = (java.lang.ref.WeakReference) r2     // Catch:{ all -> 0x002d }
              if (r2 == 0) goto L_0x002b
              java.lang.Object r3 = r2.get()     // Catch:{ all -> 0x002d }
              android.graphics.drawable.Drawable$ConstantState r3 = (android.graphics.drawable.Drawable.ConstantState) r3     // Catch:{ all -> 0x002d }
              if (r3 == 0) goto L_0x0028
              android.content.res.Resources r1 = r5.getResources()     // Catch:{ all -> 0x002d }
              android.graphics.drawable.Drawable r1 = r3.newDrawable(r1)     // Catch:{ all -> 0x002d }
              monitor-exit(r4)
              return r1
          L_0x0028:
              r0.remove(r6)     // Catch:{ all -> 0x002d }
          L_0x002b:
              monitor-exit(r4)
              return r1
          L_0x002d:
              r5 = move-exception
              monitor-exit(r4)
              throw r5
          */
          throw new UnsupportedOperationException("Method not decompiled: androidx.appcompat.widget.ResourceManagerInternal.getCachedDrawable(android.content.Context, long):android.graphics.drawable.Drawable");
      }
  
      public static synchronized PorterDuffColorFilter getPorterDuffColorFilter(int color, PorterDuff.Mode mode) {
          PorterDuffColorFilter porterDuffColorFilter;
          synchronized (ResourceManagerInternal.class) {
              ColorFilterLruCache colorFilterLruCache = COLOR_FILTER_CACHE;
              porterDuffColorFilter = colorFilterLruCache.get(color, mode);
              if (porterDuffColorFilter == null) {
                  porterDuffColorFilter = new PorterDuffColorFilter(color, mode);
                  colorFilterLruCache.put(color, mode, porterDuffColorFilter);
              }
          }
          return porterDuffColorFilter;
      }
  
      private ColorStateList getTintListFromCache(Context context, int resId) {
          SparseArrayCompat sparseArrayCompat;
          WeakHashMap<Context, SparseArrayCompat<ColorStateList>> weakHashMap = this.mTintLists;
          if (weakHashMap == null || (sparseArrayCompat = weakHashMap.get(context)) == null) {
              return null;
          }
          return (ColorStateList) sparseArrayCompat.get(resId);
      }
  
      private static void installDefaultInflateDelegates(ResourceManagerInternal manager) {
          if (Build.VERSION.SDK_INT < 24) {
              manager.addDelegate("vector", new VdcInflateDelegate());
              manager.addDelegate("animated-vector", new AvdcInflateDelegate());
              manager.addDelegate("animated-selector", new AsldcInflateDelegate());
              manager.addDelegate("drawable", new DrawableDelegate());
          }
      }
  
      private static boolean isVectorDrawable(Drawable d) {
          return (d instanceof VectorDrawableCompat) || PLATFORM_VD_CLAZZ.equals(d.getClass().getName());
      }
  
      /* JADX WARNING: Removed duplicated region for block: B:30:0x0078 A[Catch:{ Exception -> 0x00a8 }] */
      /* JADX WARNING: Removed duplicated region for block: B:36:0x00a0 A[Catch:{ Exception -> 0x00a8 }] */
      /* Code decompiled incorrectly, please refer to instructions dump. */
      private android.graphics.drawable.Drawable loadDrawableFromDelegates(android.content.Context r13, int r14) {
          /*
              r12 = this;
              androidx.collection.SimpleArrayMap<java.lang.String, androidx.appcompat.widget.ResourceManagerInternal$InflateDelegate> r0 = r12.mDelegates
              r1 = 0
              if (r0 == 0) goto L_0x00b8
              boolean r0 = r0.isEmpty()
              if (r0 != 0) goto L_0x00b8
              androidx.collection.SparseArrayCompat<java.lang.String> r0 = r12.mKnownDrawableIdTags
              java.lang.String r2 = "appcompat_skip_skip"
              if (r0 == 0) goto L_0x002a
              java.lang.Object r0 = r0.get(r14)
              java.lang.String r0 = (java.lang.String) r0
              boolean r3 = r2.equals(r0)
              if (r3 != 0) goto L_0x0029
              if (r0 == 0) goto L_0x0028
              androidx.collection.SimpleArrayMap<java.lang.String, androidx.appcompat.widget.ResourceManagerInternal$InflateDelegate> r3 = r12.mDelegates
              java.lang.Object r3 = r3.get(r0)
              if (r3 != 0) goto L_0x0028
              goto L_0x0029
          L_0x0028:
              goto L_0x0031
          L_0x0029:
              return r1
          L_0x002a:
              androidx.collection.SparseArrayCompat r0 = new androidx.collection.SparseArrayCompat
              r0.<init>()
              r12.mKnownDrawableIdTags = r0
          L_0x0031:
              android.util.TypedValue r0 = r12.mTypedValue
              if (r0 != 0) goto L_0x003c
              android.util.TypedValue r0 = new android.util.TypedValue
              r0.<init>()
              r12.mTypedValue = r0
          L_0x003c:
              android.util.TypedValue r0 = r12.mTypedValue
              android.content.res.Resources r1 = r13.getResources()
              r3 = 1
              r1.getValue(r14, r0, r3)
              long r4 = createCacheKey(r0)
              android.graphics.drawable.Drawable r6 = r12.getCachedDrawable(r13, r4)
              if (r6 == 0) goto L_0x0051
              return r6
          L_0x0051:
              java.lang.CharSequence r7 = r0.string
              if (r7 == 0) goto L_0x00b0
              java.lang.CharSequence r7 = r0.string
              java.lang.String r7 = r7.toString()
              java.lang.String r8 = ".xml"
              boolean r7 = r7.endsWith(r8)
              if (r7 == 0) goto L_0x00b0
              android.content.res.XmlResourceParser r7 = r1.getXml(r14)     // Catch:{ Exception -> 0x00a8 }
              android.util.AttributeSet r8 = android.util.Xml.asAttributeSet(r7)     // Catch:{ Exception -> 0x00a8 }
          L_0x006b:
              int r9 = r7.next()     // Catch:{ Exception -> 0x00a8 }
              r10 = r9
              r11 = 2
              if (r9 == r11) goto L_0x0076
              if (r10 == r3) goto L_0x0076
              goto L_0x006b
          L_0x0076:
              if (r10 != r11) goto L_0x00a0
              java.lang.String r3 = r7.getName()     // Catch:{ Exception -> 0x00a8 }
              androidx.collection.SparseArrayCompat<java.lang.String> r9 = r12.mKnownDrawableIdTags     // Catch:{ Exception -> 0x00a8 }
              r9.append(r14, r3)     // Catch:{ Exception -> 0x00a8 }
              androidx.collection.SimpleArrayMap<java.lang.String, androidx.appcompat.widget.ResourceManagerInternal$InflateDelegate> r9 = r12.mDelegates     // Catch:{ Exception -> 0x00a8 }
              java.lang.Object r9 = r9.get(r3)     // Catch:{ Exception -> 0x00a8 }
              androidx.appcompat.widget.ResourceManagerInternal$InflateDelegate r9 = (androidx.appcompat.widget.ResourceManagerInternal.InflateDelegate) r9     // Catch:{ Exception -> 0x00a8 }
              if (r9 == 0) goto L_0x0095
              android.content.res.Resources$Theme r11 = r13.getTheme()     // Catch:{ Exception -> 0x00a8 }
              android.graphics.drawable.Drawable r11 = r9.createFromXmlInner(r13, r7, r8, r11)     // Catch:{ Exception -> 0x00a8 }
              r6 = r11
          L_0x0095:
              if (r6 == 0) goto L_0x009f
              int r11 = r0.changingConfigurations     // Catch:{ Exception -> 0x00a8 }
              r6.setChangingConfigurations(r11)     // Catch:{ Exception -> 0x00a8 }
              r12.addDrawableToCache(r13, r4, r6)     // Catch:{ Exception -> 0x00a8 }
          L_0x009f:
              goto L_0x00b0
          L_0x00a0:
              org.xmlpull.v1.XmlPullParserException r3 = new org.xmlpull.v1.XmlPullParserException     // Catch:{ Exception -> 0x00a8 }
              java.lang.String r9 = "No start tag found"
              r3.<init>(r9)     // Catch:{ Exception -> 0x00a8 }
              throw r3     // Catch:{ Exception -> 0x00a8 }
          L_0x00a8:
              r3 = move-exception
              java.lang.String r7 = "ResourceManagerInternal"
              java.lang.String r8 = "Exception while inflating drawable"
              android.util.Log.e(r7, r8, r3)
          L_0x00b0:
              if (r6 != 0) goto L_0x00b7
              androidx.collection.SparseArrayCompat<java.lang.String> r3 = r12.mKnownDrawableIdTags
              r3.append(r14, r2)
          L_0x00b7:
              return r6
          L_0x00b8:
              return r1
          */
          throw new UnsupportedOperationException("Method not decompiled: androidx.appcompat.widget.ResourceManagerInternal.loadDrawableFromDelegates(android.content.Context, int):android.graphics.drawable.Drawable");
      }
  
      private Drawable tintDrawable(Context context, int resId, boolean failIfNotKnown, Drawable drawable) {
          ColorStateList tintList = getTintList(context, resId);
          if (tintList != null) {
              if (DrawableUtils.canSafelyMutateDrawable(drawable)) {
                  drawable = drawable.mutate();
              }
              Drawable drawable2 = DrawableCompat.wrap(drawable);
              DrawableCompat.setTintList(drawable2, tintList);
              PorterDuff.Mode tintMode = getTintMode(resId);
              if (tintMode == null) {
                  return drawable2;
              }
              DrawableCompat.setTintMode(drawable2, tintMode);
              return drawable2;
          }
          ResourceManagerHooks resourceManagerHooks = this.mHooks;
          if ((resourceManagerHooks == null || !resourceManagerHooks.tintDrawable(context, resId, drawable)) && !tintDrawableUsingColorFilter(context, resId, drawable) && failIfNotKnown) {
              return null;
          }
          return drawable;
      }
  
      static void tintDrawable(Drawable drawable, TintInfo tint, int[] state) {
          int[] state2 = drawable.getState();
          if (DrawableUtils.canSafelyMutateDrawable(drawable)) {
              if (!(drawable.mutate() == drawable)) {
                  Log.d(TAG, "Mutated drawable is not the same instance as the input.");
                  return;
              }
          }
          if ((drawable instanceof LayerDrawable) && drawable.isStateful()) {
              drawable.setState(new int[0]);
              drawable.setState(state2);
          }
          if (tint.mHasTintList || tint.mHasTintMode) {
              drawable.setColorFilter(createTintFilter(tint.mHasTintList ? tint.mTintList : null, tint.mHasTintMode ? tint.mTintMode : DEFAULT_MODE, state));
          } else {
              drawable.clearColorFilter();
          }
          if (Build.VERSION.SDK_INT <= 23) {
              drawable.invalidateSelf();
          }
      }
  
      public synchronized Drawable getDrawable(Context context, int resId) {
          return getDrawable(context, resId, false);
      }
  
      /* access modifiers changed from: package-private */
      public synchronized Drawable getDrawable(Context context, int resId, boolean failIfNotKnown) {
          Drawable loadDrawableFromDelegates;
          checkVectorDrawableSetup(context);
          loadDrawableFromDelegates = loadDrawableFromDelegates(context, resId);
          if (loadDrawableFromDelegates == null) {
              loadDrawableFromDelegates = createDrawableIfNeeded(context, resId);
          }
          if (loadDrawableFromDelegates == null) {
              loadDrawableFromDelegates = ContextCompat.getDrawable(context, resId);
          }
          if (loadDrawableFromDelegates != null) {
              loadDrawableFromDelegates = tintDrawable(context, resId, failIfNotKnown, loadDrawableFromDelegates);
          }
          if (loadDrawableFromDelegates != null) {
              DrawableUtils.fixDrawable(loadDrawableFromDelegates);
          }
          return loadDrawableFromDelegates;
      }
  
      /* access modifiers changed from: package-private */
      public synchronized ColorStateList getTintList(Context context, int resId) {
          ColorStateList tintListFromCache;
          tintListFromCache = getTintListFromCache(context, resId);
          if (tintListFromCache == null) {
              ResourceManagerHooks resourceManagerHooks = this.mHooks;
              tintListFromCache = resourceManagerHooks == null ? null : resourceManagerHooks.getTintListForDrawableRes(context, resId);
              if (tintListFromCache != null) {
                  addTintListToCache(context, resId, tintListFromCache);
              }
          }
          return tintListFromCache;
      }
  
      /* access modifiers changed from: package-private */
      public PorterDuff.Mode getTintMode(int resId) {
          ResourceManagerHooks resourceManagerHooks = this.mHooks;
          if (resourceManagerHooks == null) {
              return null;
          }
          return resourceManagerHooks.getTintModeForDrawableRes(resId);
      }
  
      public synchronized void onConfigurationChanged(Context context) {
          LongSparseArray longSparseArray = this.mDrawableCaches.get(context);
          if (longSparseArray != null) {
              longSparseArray.clear();
          }
      }
  
      /* access modifiers changed from: package-private */
      public synchronized Drawable onDrawableLoadedFromResources(Context context, VectorEnabledTintResources resources, int resId) {
          Drawable loadDrawableFromDelegates = loadDrawableFromDelegates(context, resId);
          if (loadDrawableFromDelegates == null) {
              loadDrawableFromDelegates = resources.getDrawableCanonical(resId);
          }
          if (loadDrawableFromDelegates == null) {
              return null;
          }
          return tintDrawable(context, resId, false, loadDrawableFromDelegates);
      }
  
      public synchronized void setHooks(ResourceManagerHooks hooks) {
          this.mHooks = hooks;
      }
  
      /* access modifiers changed from: package-private */
      public boolean tintDrawableUsingColorFilter(Context context, int resId, Drawable drawable) {
          ResourceManagerHooks resourceManagerHooks = this.mHooks;
          return resourceManagerHooks != null && resourceManagerHooks.tintDrawableUsingColorFilter(context, resId, drawable);
      }
  }
  