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 f0 extends ContextWrapper {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static ArrayList<WeakReference<f0>> f593b;

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

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

    private f0(Context context) {
        super(context);
        if (!n0.b()) {
            this.f594c = new h0(this, context.getResources());
            this.f595d = null;
            return;
        }
        n0 n0Var = new n0(this, context.getResources());
        this.f594c = n0Var;
        Resources.Theme themeNewTheme = n0Var.newTheme();
        this.f595d = themeNewTheme;
        themeNewTheme.setTo(context.getTheme());
    }

    private static boolean a(Context context) {
        if ((context instanceof f0) || (context.getResources() instanceof h0) || (context.getResources() instanceof n0)) {
            return false;
        }
        return Build.VERSION.SDK_INT < 21 || n0.b();
    }

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

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

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

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

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