package p010d;

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;

/* renamed from: d.f0 */
/* loaded from: classes.dex */
public class C0542f0 {

    /* renamed from: a */
    public static Field f1957a;

    /* renamed from: b */
    public static boolean f1958b;

    /* renamed from: c */
    public static Class<?> f1959c;

    /* renamed from: d */
    public static boolean f1960d;

    /* renamed from: e */
    public static Field f1961e;

    /* renamed from: f */
    public static boolean f1962f;

    /* renamed from: g */
    public static Field f1963g;

    /* renamed from: h */
    public static boolean f1964h;

    /* renamed from: d.f0$a */
    /* loaded from: classes.dex */
    public static class C0543a {
        /* renamed from: a */
        public static void m3506a(LongSparseArray longSparseArray) {
            longSparseArray.clear();
        }
    }

    /* renamed from: a */
    public static void m3511a(Resources resources) {
        int i = Build.VERSION.SDK_INT;
        if (i >= 28) {
            return;
        }
        if (i >= 24) {
            m3508d(resources);
        } else if (i >= 23) {
            m3509c(resources);
        } else if (i >= 21) {
            m3510b(resources);
        }
    }

    /* renamed from: b */
    public static void m3510b(Resources resources) {
        Map map;
        if (!f1958b) {
            try {
                Field declaredField = Resources.class.getDeclaredField("mDrawableCache");
                f1957a = declaredField;
                declaredField.setAccessible(true);
            } catch (NoSuchFieldException e) {
                Log.e("ResourcesFlusher", "Could not retrieve Resources#mDrawableCache field", e);
            }
            f1958b = true;
        }
        Field field = f1957a;
        if (field != null) {
            try {
                map = (Map) field.get(resources);
            } catch (IllegalAccessException e2) {
                Log.e("ResourcesFlusher", "Could not retrieve value from Resources#mDrawableCache", e2);
                map = null;
            }
            if (map != null) {
                map.clear();
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:18:0x002f A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:19:0x0030  */
    /* renamed from: c */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static void m3509c(Resources resources) {
        Object obj;
        if (!f1958b) {
            try {
                Field declaredField = Resources.class.getDeclaredField("mDrawableCache");
                f1957a = declaredField;
                declaredField.setAccessible(true);
            } catch (NoSuchFieldException e) {
                Log.e("ResourcesFlusher", "Could not retrieve Resources#mDrawableCache field", e);
            }
            f1958b = true;
        }
        Field field = f1957a;
        if (field != null) {
            try {
                obj = field.get(resources);
            } catch (IllegalAccessException e2) {
                Log.e("ResourcesFlusher", "Could not retrieve value from Resources#mDrawableCache", e2);
            }
            if (obj != null) {
                return;
            }
            m3507e(obj);
            return;
        }
        obj = null;
        if (obj != null) {
        }
    }

    /* renamed from: d */
    public static void m3508d(Resources resources) {
        Object obj;
        if (!f1964h) {
            try {
                Field declaredField = Resources.class.getDeclaredField("mResourcesImpl");
                f1963g = declaredField;
                declaredField.setAccessible(true);
            } catch (NoSuchFieldException e) {
                Log.e("ResourcesFlusher", "Could not retrieve Resources#mResourcesImpl field", e);
            }
            f1964h = true;
        }
        Field field = f1963g;
        if (field == null) {
            return;
        }
        Object obj2 = null;
        try {
            obj = field.get(resources);
        } catch (IllegalAccessException e2) {
            Log.e("ResourcesFlusher", "Could not retrieve value from Resources#mResourcesImpl", e2);
            obj = null;
        }
        if (obj == null) {
            return;
        }
        if (!f1958b) {
            try {
                Field declaredField2 = obj.getClass().getDeclaredField("mDrawableCache");
                f1957a = declaredField2;
                declaredField2.setAccessible(true);
            } catch (NoSuchFieldException e3) {
                Log.e("ResourcesFlusher", "Could not retrieve ResourcesImpl#mDrawableCache field", e3);
            }
            f1958b = true;
        }
        Field field2 = f1957a;
        if (field2 != null) {
            try {
                obj2 = field2.get(obj);
            } catch (IllegalAccessException e4) {
                Log.e("ResourcesFlusher", "Could not retrieve value from ResourcesImpl#mDrawableCache", e4);
            }
        }
        if (obj2 != null) {
            m3507e(obj2);
        }
    }

    /* renamed from: e */
    public static void m3507e(Object obj) {
        LongSparseArray longSparseArray;
        if (!f1960d) {
            try {
                f1959c = Class.forName("android.content.res.ThemedResourceCache");
            } catch (ClassNotFoundException e) {
                Log.e("ResourcesFlusher", "Could not find ThemedResourceCache class", e);
            }
            f1960d = true;
        }
        Class<?> cls = f1959c;
        if (cls == null) {
            return;
        }
        if (!f1962f) {
            try {
                Field declaredField = cls.getDeclaredField("mUnthemedEntries");
                f1961e = declaredField;
                declaredField.setAccessible(true);
            } catch (NoSuchFieldException e2) {
                Log.e("ResourcesFlusher", "Could not retrieve ThemedResourceCache#mUnthemedEntries field", e2);
            }
            f1962f = true;
        }
        Field field = f1961e;
        if (field == null) {
            return;
        }
        try {
            longSparseArray = (LongSparseArray) field.get(obj);
        } catch (IllegalAccessException e3) {
            Log.e("ResourcesFlusher", "Could not retrieve value from ThemedResourceCache#mUnthemedEntries", e3);
            longSparseArray = null;
        }
        if (longSparseArray != null) {
            C0543a.m3506a(longSparseArray);
        }
    }
}