package com.google.android.material.checkbox;

import android.R;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.AnimatedStateListDrawable;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.autofill.AutofillManager;
import android.widget.CompoundButton;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.AppCompatCheckBox;
import com.google.android.gms.measurement.internal.u1;
import com.google.android.material.internal.q0;
import com.google.android.material.internal.w0;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import l.e;
import l.p3;
import t3.f;
import u0.d;
import x8.c;
import x8.g;
import x8.k;
import x8.l;
import x8.m;

/* JADX INFO: loaded from: classes2.dex */
public class MaterialCheckBox extends AppCompatCheckBox {
    public static final int S = l.Widget_MaterialComponents_CompoundButton_CheckBox;
    public static final int[] T = {c.state_indeterminate};
    public static final int[] U;
    public static final int[][] V;
    public static final int W;
    public boolean Q;
    public final a R;

    @Nullable
    private Drawable buttonDrawable;

    @Nullable
    private Drawable buttonIconDrawable;

    @Nullable
    ColorStateList buttonIconTintList;

    @Nullable
    ColorStateList buttonTintList;

    @Nullable
    private CharSequence customStateDescription;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final LinkedHashSet f22414e;

    @Nullable
    private CharSequence errorAccessibilityLabel;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final LinkedHashSet f22415f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public boolean f22416g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f22417h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public boolean f22418i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public boolean f22419j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public PorterDuff.Mode f22420k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public int f22421l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int[] f22422m;

    @Nullable
    private ColorStateList materialThemeColorsTintList;

    @Nullable
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;

    @Nullable
    private final f transitionToUnchecked;

    @Retention(RetentionPolicy.SOURCE)
    @RestrictTo({RestrictTo.Scope.f556b})
    public @interface CheckedState {
    }

    static {
        int i10 = c.state_error;
        U = new int[]{i10};
        V = new int[][]{new int[]{R.attr.state_enabled, i10}, new int[]{R.attr.state_enabled, R.attr.state_checked}, new int[]{R.attr.state_enabled, -16842912}, new int[]{-16842910, R.attr.state_checked}, new int[]{-16842910, -16842912}};
        W = Resources.getSystem().getIdentifier("btn_check_material_anim", "drawable", "android");
    }

    public MaterialCheckBox(Context context) {
        this(context, null);
    }

    @NonNull
    private String getButtonStateDescription() {
        int i10 = this.f22421l;
        return i10 == 1 ? getResources().getString(k.mtrl_checkbox_state_description_checked) : i10 == 0 ? getResources().getString(k.mtrl_checkbox_state_description_unchecked) : getResources().getString(k.mtrl_checkbox_state_description_indeterminate);
    }

    private ColorStateList getMaterialThemeColorsTintList() {
        if (this.materialThemeColorsTintList == null) {
            int iB = h9.a.b(d.a.colorControlActivated, this);
            int iB2 = h9.a.b(d.a.colorError, this);
            int iB3 = h9.a.b(c.colorSurface, this);
            int iB4 = h9.a.b(c.colorOnSurface, this);
            this.materialThemeColorsTintList = new ColorStateList(V, new int[]{h9.a.e(1.0f, iB3, iB2), h9.a.e(1.0f, iB3, iB), h9.a.e(0.54f, iB3, iB4), h9.a.e(0.38f, iB3, iB4), h9.a.e(0.38f, iB3, iB4)});
        }
        return this.materialThemeColorsTintList;
    }

    @Nullable
    private ColorStateList getSuperButtonTintList() {
        ColorStateList colorStateList = this.buttonTintList;
        return colorStateList != null ? colorStateList : super.getButtonTintList() != null ? super.getButtonTintList() : getSupportButtonTintList();
    }

    public final void a() {
        ColorStateList colorStateList;
        ColorStateList colorStateList2;
        f fVar;
        e eVar;
        this.buttonDrawable = j9.c.createTintableMutatedDrawableIfNeeded(this.buttonDrawable, this.buttonTintList, d.getButtonTintMode(this));
        this.buttonIconDrawable = j9.c.createTintableMutatedDrawableIfNeeded(this.buttonIconDrawable, this.buttonIconTintList, this.f22420k);
        if (this.f22419j) {
            f fVar2 = this.transitionToUnchecked;
            if (fVar2 != null) {
                Drawable drawable = fVar2.f32876a;
                a aVar = this.R;
                if (drawable != null) {
                    AnimatedVectorDrawable animatedVectorDrawable = (AnimatedVectorDrawable) drawable;
                    if (aVar.f32863a == null) {
                        aVar.f32863a = new t3.b(aVar);
                    }
                    animatedVectorDrawable.unregisterAnimationCallback(aVar.f32863a);
                }
                ArrayList arrayList = fVar2.f32874e;
                if (arrayList != null && aVar != null) {
                    arrayList.remove(aVar);
                    if (fVar2.f32874e.size() == 0 && (eVar = fVar2.f32873d) != null) {
                        fVar2.f32871b.f32866c.removeListener(eVar);
                        fVar2.f32873d = null;
                    }
                }
                f fVar3 = this.transitionToUnchecked;
                Drawable drawable2 = fVar3.f32876a;
                if (drawable2 != null) {
                    AnimatedVectorDrawable animatedVectorDrawable2 = (AnimatedVectorDrawable) drawable2;
                    if (aVar.f32863a == null) {
                        aVar.f32863a = new t3.b(aVar);
                    }
                    animatedVectorDrawable2.registerAnimationCallback(aVar.f32863a);
                } else if (aVar != null) {
                    if (fVar3.f32874e == null) {
                        fVar3.f32874e = new ArrayList();
                    }
                    if (!fVar3.f32874e.contains(aVar)) {
                        fVar3.f32874e.add(aVar);
                        if (fVar3.f32873d == null) {
                            fVar3.f32873d = new e(fVar3, 2);
                        }
                        fVar3.f32871b.f32866c.addListener(fVar3.f32873d);
                    }
                }
            }
            Drawable drawable3 = this.buttonDrawable;
            if ((drawable3 instanceof AnimatedStateListDrawable) && (fVar = this.transitionToUnchecked) != null) {
                int i10 = g.checked;
                int i11 = g.unchecked;
                ((AnimatedStateListDrawable) drawable3).addTransition(i10, i11, fVar, false);
                ((AnimatedStateListDrawable) this.buttonDrawable).addTransition(g.indeterminate, i11, this.transitionToUnchecked, false);
            }
        }
        Drawable drawable4 = this.buttonDrawable;
        if (drawable4 != null && (colorStateList2 = this.buttonTintList) != null) {
            drawable4.setTintList(colorStateList2);
        }
        Drawable drawable5 = this.buttonIconDrawable;
        if (drawable5 != null && (colorStateList = this.buttonIconTintList) != null) {
            drawable5.setTintList(colorStateList);
        }
        super.setButtonDrawable(j9.c.compositeTwoLayeredDrawable(this.buttonDrawable, this.buttonIconDrawable));
        refreshDrawableState();
    }

    @Override // android.widget.CompoundButton
    @Nullable
    public Drawable getButtonDrawable() {
        return this.buttonDrawable;
    }

    @Nullable
    public Drawable getButtonIconDrawable() {
        return this.buttonIconDrawable;
    }

    @Nullable
    public ColorStateList getButtonIconTintList() {
        return this.buttonIconTintList;
    }

    @NonNull
    public PorterDuff.Mode getButtonIconTintMode() {
        return this.f22420k;
    }

    @Override // android.widget.CompoundButton
    @Nullable
    public ColorStateList getButtonTintList() {
        return this.buttonTintList;
    }

    public int getCheckedState() {
        return this.f22421l;
    }

    @Nullable
    public CharSequence getErrorAccessibilityLabel() {
        return this.errorAccessibilityLabel;
    }

    @Override // android.widget.CompoundButton, android.widget.Checkable
    public final boolean isChecked() {
        return this.f22421l == 1;
    }

    @Override // android.widget.TextView, android.view.View
    public final void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (this.f22416g && this.buttonTintList == null && this.buttonIconTintList == null) {
            setUseMaterialThemeColors(true);
        }
    }

    @Override // android.widget.CompoundButton, android.widget.TextView, android.view.View
    public final int[] onCreateDrawableState(int i10) {
        int[] iArrOnCreateDrawableState = super.onCreateDrawableState(i10 + 2);
        if (getCheckedState() == 2) {
            View.mergeDrawableStates(iArrOnCreateDrawableState, T);
        }
        if (this.f22418i) {
            View.mergeDrawableStates(iArrOnCreateDrawableState, U);
        }
        this.f22422m = j9.c.a(iArrOnCreateDrawableState);
        return iArrOnCreateDrawableState;
    }

    @Override // android.widget.CompoundButton, android.widget.TextView, android.view.View
    public final void onDraw(Canvas canvas) {
        Drawable buttonDrawable;
        if (!this.f22417h || !TextUtils.isEmpty(getText()) || (buttonDrawable = d.getButtonDrawable(this)) == null) {
            super.onDraw(canvas);
            return;
        }
        int width = ((getWidth() - buttonDrawable.getIntrinsicWidth()) / 2) * (getLayoutDirection() == 1 ? -1 : 1);
        int iSave = canvas.save();
        canvas.translate(width, 0.0f);
        super.onDraw(canvas);
        canvas.restoreToCount(iSave);
        if (getBackground() != null) {
            Rect bounds = buttonDrawable.getBounds();
            getBackground().setHotspotBounds(bounds.left + width, bounds.top, bounds.right + width, bounds.bottom);
        }
    }

    @Override // android.view.View
    public void onInitializeAccessibilityNodeInfo(@Nullable AccessibilityNodeInfo accessibilityNodeInfo) {
        super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
        if (accessibilityNodeInfo != null && this.f22418i) {
            accessibilityNodeInfo.setText(((Object) accessibilityNodeInfo.getText()) + ", " + ((Object) this.errorAccessibilityLabel));
        }
    }

    @Override // android.widget.CompoundButton, android.widget.TextView, android.view.View
    public void onRestoreInstanceState(@Nullable Parcelable parcelable) {
        if (!(parcelable instanceof b)) {
            super.onRestoreInstanceState(parcelable);
            return;
        }
        b bVar = (b) parcelable;
        super.onRestoreInstanceState(bVar.getSuperState());
        setCheckedState(bVar.f22424a);
    }

    @Override // android.widget.CompoundButton, android.widget.TextView, android.view.View
    @Nullable
    public Parcelable onSaveInstanceState() {
        b bVar = new b(super.onSaveInstanceState());
        bVar.f22424a = getCheckedState();
        return bVar;
    }

    @Override // androidx.appcompat.widget.AppCompatCheckBox, android.widget.CompoundButton
    public void setButtonDrawable(@DrawableRes int i10) {
        setButtonDrawable(f.a.getDrawable(getContext(), i10));
    }

    public void setButtonIconDrawable(@Nullable Drawable drawable) {
        this.buttonIconDrawable = drawable;
        a();
    }

    public void setButtonIconDrawableResource(@DrawableRes int i10) {
        setButtonIconDrawable(f.a.getDrawable(getContext(), i10));
    }

    public void setButtonIconTintList(@Nullable ColorStateList colorStateList) {
        if (this.buttonIconTintList == colorStateList) {
            return;
        }
        this.buttonIconTintList = colorStateList;
        a();
    }

    public void setButtonIconTintMode(@NonNull PorterDuff.Mode mode) {
        if (this.f22420k == mode) {
            return;
        }
        this.f22420k = mode;
        a();
    }

    @Override // android.widget.CompoundButton
    public void setButtonTintList(@Nullable ColorStateList colorStateList) {
        if (this.buttonTintList == colorStateList) {
            return;
        }
        this.buttonTintList = colorStateList;
        a();
    }

    @Override // android.widget.CompoundButton
    public void setButtonTintMode(@Nullable PorterDuff.Mode mode) {
        setSupportButtonTintMode(mode);
        a();
    }

    public void setCenterIfNoTextEnabled(boolean z10) {
        this.f22417h = z10;
    }

    @Override // android.widget.CompoundButton, android.widget.Checkable
    public void setChecked(boolean z10) {
        setCheckedState(z10 ? 1 : 0);
    }

    public void setCheckedState(int i10) {
        AutofillManager autofillManager;
        CompoundButton.OnCheckedChangeListener onCheckedChangeListener;
        if (this.f22421l != i10) {
            this.f22421l = i10;
            super.setChecked(i10 == 1);
            refreshDrawableState();
            int i11 = Build.VERSION.SDK_INT;
            if (i11 >= 30 && this.customStateDescription == null) {
                super.setStateDescription(getButtonStateDescription());
            }
            if (this.Q) {
                return;
            }
            this.Q = true;
            LinkedHashSet linkedHashSet = this.f22415f;
            if (linkedHashSet != null) {
                Iterator it = linkedHashSet.iterator();
                if (it.hasNext()) {
                    u1.x(it.next());
                    throw null;
                }
            }
            if (this.f22421l != 2 && (onCheckedChangeListener = this.onCheckedChangeListener) != null) {
                onCheckedChangeListener.onCheckedChanged(this, isChecked());
            }
            if (i11 >= 26 && (autofillManager = (AutofillManager) getContext().getSystemService(AutofillManager.class)) != null) {
                autofillManager.notifyValueChanged(this);
            }
            this.Q = false;
        }
    }

    public void setErrorAccessibilityLabel(@Nullable CharSequence charSequence) {
        this.errorAccessibilityLabel = charSequence;
    }

    public void setErrorAccessibilityLabelResource(@StringRes int i10) {
        setErrorAccessibilityLabel(i10 != 0 ? getResources().getText(i10) : null);
    }

    public void setErrorShown(boolean z10) {
        if (this.f22418i == z10) {
            return;
        }
        this.f22418i = z10;
        refreshDrawableState();
        Iterator it = this.f22414e.iterator();
        if (it.hasNext()) {
            u1.x(it.next());
            throw null;
        }
    }

    @Override // android.widget.CompoundButton
    public void setOnCheckedChangeListener(@Nullable CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    @Override // android.widget.CompoundButton, android.view.View
    @RequiresApi(30)
    public void setStateDescription(@Nullable CharSequence charSequence) {
        this.customStateDescription = charSequence;
        if (charSequence != null) {
            super.setStateDescription(charSequence);
        } else {
            if (Build.VERSION.SDK_INT < 30 || charSequence != null) {
                return;
            }
            super.setStateDescription(getButtonStateDescription());
        }
    }

    public void setUseMaterialThemeColors(boolean z10) {
        this.f22416g = z10;
        if (z10) {
            d.setButtonTintList(this, getMaterialThemeColorsTintList());
        } else {
            d.setButtonTintList(this, null);
        }
    }

    @Override // android.widget.CompoundButton, android.widget.Checkable
    public final void toggle() {
        setChecked(!isChecked());
    }

    public MaterialCheckBox(Context context, @Nullable AttributeSet attributeSet) {
        this(context, attributeSet, d.a.checkboxStyle);
    }

    @Override // androidx.appcompat.widget.AppCompatCheckBox, android.widget.CompoundButton
    public void setButtonDrawable(@Nullable Drawable drawable) {
        this.buttonDrawable = drawable;
        this.f22419j = false;
        a();
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public MaterialCheckBox(Context context, @Nullable AttributeSet attributeSet, int i10) {
        int i11 = S;
        super(x9.a.wrap(context, attributeSet, i10, i11), attributeSet, i10);
        this.f22414e = new LinkedHashSet();
        this.f22415f = new LinkedHashSet();
        this.transitionToUnchecked = f.create(getContext(), x8.f.mtrl_checkbox_button_checked_unchecked);
        this.R = new a(this);
        Context context2 = getContext();
        this.buttonDrawable = d.getButtonDrawable(this);
        this.buttonTintList = getSuperButtonTintList();
        setSupportButtonTintList(null);
        p3 p3VarD = q0.d(context2, attributeSet, m.MaterialCheckBox, i10, i11, new int[0]);
        this.buttonIconDrawable = p3VarD.b(m.MaterialCheckBox_buttonIcon);
        Drawable drawable = this.buttonDrawable;
        TypedArray typedArray = p3VarD.f29809b;
        if (drawable != null && q9.c.a(context2, c.isMaterial3Theme, false)) {
            int resourceId = typedArray.getResourceId(m.MaterialCheckBox_android_button, 0);
            int resourceId2 = typedArray.getResourceId(m.MaterialCheckBox_buttonCompat, 0);
            if (resourceId == W && resourceId2 == 0) {
                super.setButtonDrawable((Drawable) null);
                this.buttonDrawable = f.a.getDrawable(context2, x8.f.mtrl_checkbox_button);
                this.f22419j = true;
                if (this.buttonIconDrawable == null) {
                    this.buttonIconDrawable = f.a.getDrawable(context2, x8.f.mtrl_checkbox_button_icon);
                }
            }
        }
        this.buttonIconTintList = q9.d.getColorStateList(context2, p3VarD, m.MaterialCheckBox_buttonIconTint);
        this.f22420k = w0.f(typedArray.getInt(m.MaterialCheckBox_buttonIconTintMode, -1), PorterDuff.Mode.SRC_IN);
        this.f22416g = typedArray.getBoolean(m.MaterialCheckBox_useMaterialThemeColors, false);
        this.f22417h = typedArray.getBoolean(m.MaterialCheckBox_centerIfNoTextEnabled, true);
        this.f22418i = typedArray.getBoolean(m.MaterialCheckBox_errorShown, false);
        this.errorAccessibilityLabel = typedArray.getText(m.MaterialCheckBox_errorAccessibilityLabel);
        int i12 = m.MaterialCheckBox_checkedState;
        if (typedArray.hasValue(i12)) {
            setCheckedState(typedArray.getInt(i12, 0));
        }
        p3VarD.e();
        a();
    }
}
