package androidx.appcompat.widget;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Build;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

/* JADX INFO: compiled from: TintContextWrapper.java */
/* JADX INFO: loaded from: classes.dex */
public class x0 extends ContextWrapper {

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static ArrayList<WeakReference<x0>> f1135d;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final Resources f1136a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Resources.Theme f1137b;

    private x0(Context context) {
        super(context);
        if (!i1.c()) {
            this.f1136a = new z0(this, context.getResources());
            this.f1137b = null;
            return;
        }
        i1 i1Var = new i1(this, context.getResources());
        this.f1136a = i1Var;
        Resources.Theme themeNewTheme = i1Var.newTheme();
        this.f1137b = themeNewTheme;
        themeNewTheme.setTo(context.getTheme());
    }

    private static boolean a(Context context) {
        if ((context instanceof x0) || (context.getResources() instanceof z0) || (context.getResources() instanceof i1)) {
            return false;
        }
        return Build.VERSION.SDK_INT < 21 || i1.c();
    }

    public static Context b(Context context) {
        if (!a(context)) {
            return context;
        }
        synchronized (f1134c) {
            ArrayList<WeakReference<x0>> arrayList = f1135d;
            if (arrayList == null) {
                f1135d = new ArrayList<>();
            } else {
                for (int size = arrayList.size() - 1; size >= 0; size--) {
                    WeakReference<x0> weakReference = f1135d.get(size);
                    if (weakReference == null || weakReference.get() == null) {
                        f1135d.remove(size);
                    }
                }
                for (int size2 = f1135d.size() - 1; size2 >= 0; size2--) {
                    WeakReference<x0> weakReference2 = f1135d.get(size2);
                    x0 x0Var = weakReference2 != null ? weakReference2.get() : null;
                    if (x0Var != null && x0Var.getBaseContext() == context) {
                        return x0Var;
                    }
                }
            }
            x0 x0Var2 = new x0(context);
            f1135d.add(new WeakReference<>(x0Var2));
            return x0Var2;
        }
    }

    @Override // android.content.ContextWrapper, android.content.Context
    public AssetManager getAssets() {
        return this.f1136a.getAssets();
    }

    @Override // android.content.ContextWrapper, android.content.Context
    public Resources getResources() {
        return this.f1136a;
    }

    @Override // android.content.ContextWrapper, android.content.Context
    public Resources.Theme getTheme() {
        Resources.Theme theme = this.f1137b;
        return theme == null ? super.getTheme() : theme;
    }

    @Override // android.content.ContextWrapper, android.content.Context
    public void setTheme(int i3) {
        Resources.Theme theme = this.f1137b;
        if (theme == null) {
            super.setTheme(i3);
        } else {
            theme.applyStyle(i3, true);
        }
    }
}
