package androidx.appcompat.app;

import android.content.res.Resources;
import android.os.Build;
import android.util.Log;
import android.util.LongSparseArray;
import java.lang.reflect.Field;
import java.util.Map;

/* JADX INFO: compiled from: ResourcesFlusher.java */
/* JADX INFO: loaded from: classes.dex */
class k {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static Field f265a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static boolean f266b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static Class<?> f267c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static boolean f268d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private static Field f269e;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private static Field f271g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private static boolean f272h;

    /* JADX INFO: compiled from: ResourcesFlusher.java */
    static class a {
        static void a(LongSparseArray longSparseArray) {
            longSparseArray.clear();
        }
    }

    static void a(Resources resources) {
        int i3 = Build.VERSION.SDK_INT;
        if (i3 >= 28) {
            return;
        }
        if (i3 >= 24) {
            d(resources);
        } else if (i3 >= 23) {
            c(resources);
        } else if (i3 >= 21) {
            b(resources);
        }
    }

    private static void b(Resources resources) {
        if (!f266b) {
            try {
                Field declaredField = Resources.class.getDeclaredField("mDrawableCache");
                f265a = declaredField;
                declaredField.setAccessible(true);
            } catch (NoSuchFieldException e3) {
                Log.e("ResourcesFlusher", "Could not retrieve Resources#mDrawableCache field", e3);
            }
            f266b = true;
        }
        Field field = f265a;
        if (field != null) {
            Map map = null;
            try {
                map = (Map) field.get(resources);
            } catch (IllegalAccessException e4) {
                Log.e("ResourcesFlusher", "Could not retrieve value from Resources#mDrawableCache", e4);
            }
            if (map != null) {
                map.clear();
            }
        }
    }

    private static void c(Resources resources) {
        if (!f266b) {
            try {
                Field declaredField = Resources.class.getDeclaredField("mDrawableCache");
                f265a = declaredField;
                declaredField.setAccessible(true);
            } catch (NoSuchFieldException e3) {
                Log.e("ResourcesFlusher", "Could not retrieve Resources#mDrawableCache field", e3);
            }
            f266b = true;
        }
        Object obj = null;
        Field field = f265a;
        if (field != null) {
            try {
                obj = field.get(resources);
            } catch (IllegalAccessException e4) {
                Log.e("ResourcesFlusher", "Could not retrieve value from Resources#mDrawableCache", e4);
            }
        }
        if (obj == null) {
            return;
        }
        e(obj);
    }

    private static void d(Resources resources) {
        Object obj;
        if (!f272h) {
            try {
                Field declaredField = Resources.class.getDeclaredField("mResourcesImpl");
                f271g = declaredField;
                declaredField.setAccessible(true);
            } catch (NoSuchFieldException e3) {
                Log.e("ResourcesFlusher", "Could not retrieve Resources#mResourcesImpl field", e3);
            }
            f272h = true;
        }
        Field field = f271g;
        if (field == null) {
            return;
        }
        Object obj2 = null;
        try {
            obj = field.get(resources);
        } catch (IllegalAccessException e4) {
            Log.e("ResourcesFlusher", "Could not retrieve value from Resources#mResourcesImpl", e4);
            obj = null;
        }
        if (obj == null) {
            return;
        }
        if (!f266b) {
            try {
                Field declaredField2 = obj.getClass().getDeclaredField("mDrawableCache");
                f265a = declaredField2;
                declaredField2.setAccessible(true);
            } catch (NoSuchFieldException e5) {
                Log.e("ResourcesFlusher", "Could not retrieve ResourcesImpl#mDrawableCache field", e5);
            }
            f266b = true;
        }
        Field field2 = f265a;
        if (field2 != null) {
            try {
                obj2 = field2.get(obj);
            } catch (IllegalAccessException e6) {
                Log.e("ResourcesFlusher", "Could not retrieve value from ResourcesImpl#mDrawableCache", e6);
            }
        }
        if (obj2 != null) {
            e(obj2);
        }
    }

    private static void e(Object obj) {
        if (!f268d) {
            try {
                f267c = Class.forName("android.content.res.ThemedResourceCache");
            } catch (ClassNotFoundException e3) {
                Log.e("ResourcesFlusher", "Could not find ThemedResourceCache class", e3);
            }
            f268d = true;
        }
        Class<?> cls = f267c;
        if (cls == null) {
            return;
        }
        if (!f270f) {
            try {
                Field declaredField = cls.getDeclaredField("mUnthemedEntries");
                f269e = declaredField;
                declaredField.setAccessible(true);
            } catch (NoSuchFieldException e4) {
                Log.e("ResourcesFlusher", "Could not retrieve ThemedResourceCache#mUnthemedEntries field", e4);
            }
            f270f = true;
        }
        Field field = f269e;
        if (field == null) {
            return;
        }
        LongSparseArray longSparseArray = null;
        try {
            longSparseArray = (LongSparseArray) field.get(obj);
        } catch (IllegalAccessException e5) {
            Log.e("ResourcesFlusher", "Could not retrieve value from ThemedResourceCache#mUnthemedEntries", e5);
        }
        if (longSparseArray != null) {
            a.a(longSparseArray);
        }
    }
}
