package com.google.android.material.button;

import android.R;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.Layout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Button;
import android.widget.Checkable;
import android.widget.CompoundButton;
import androidx.appcompat.widget.f;
import androidx.core.view.y;
import c1.h;
import c1.k;
import c1.n;
import com.google.android.material.internal.m;
import com.google.android.material.internal.o;
import java.util.Iterator;
import java.util.LinkedHashSet;
import l0.j;

/* JADX INFO: loaded from: classes.dex */
public class MaterialButton extends f implements Checkable, n {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    private static final int[] f3691u = {R.attr.state_checkable};

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private static final int[] f3692v = {R.attr.state_checked};

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    private static final int f3693w = j.f5451f;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final com.google.android.material.button.a f3694h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final LinkedHashSet<a> f3695i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private b f3696j;

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private ColorStateList f3698l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private Drawable f3699m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    private int f3700n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    private int f3701o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    private int f3702p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    private int f3703q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    private boolean f3704r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    private boolean f3705s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    private int f3706t;

    public interface a {
        void a(MaterialButton materialButton, boolean z2);
    }

    interface b {
        void a(MaterialButton materialButton, boolean z2);
    }

    static class c extends w.a {
        public static final Parcelable.Creator<c> CREATOR = new a();

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

        class a implements Parcelable.ClassLoaderCreator<c> {
            a() {
            }

            @Override // android.os.Parcelable.Creator
            /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
            public c createFromParcel(Parcel parcel) {
                return new c(parcel, null);
            }

            @Override // android.os.Parcelable.ClassLoaderCreator
            /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
            public c createFromParcel(Parcel parcel, ClassLoader classLoader) {
                return new c(parcel, classLoader);
            }

            @Override // android.os.Parcelable.Creator
            /* JADX INFO: renamed from: c, reason: merged with bridge method [inline-methods] */
            public c[] newArray(int i3) {
                return new c[i3];
            }
        }

        public c(Parcelable parcelable) {
            super(parcelable);
        }

        private void k(Parcel parcel) {
            this.f3707g = parcel.readInt() == 1;
        }

        @Override // w.a, android.os.Parcelable
        public void writeToParcel(Parcel parcel, int i3) {
            super.writeToParcel(parcel, i3);
            parcel.writeInt(this.f3707g ? 1 : 0);
        }

        public c(Parcel parcel, ClassLoader classLoader) {
            super(parcel, classLoader);
            if (classLoader == null) {
                c.class.getClassLoader();
            }
            k(parcel);
        }
    }

    public MaterialButton(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, l0.b.f5334q);
    }

    private boolean b() {
        int i3 = this.f3706t;
        return i3 == 3 || i3 == 4;
    }

    private boolean c() {
        int i3 = this.f3706t;
        return i3 == 1 || i3 == 2;
    }

    private boolean d() {
        int i3 = this.f3706t;
        return i3 == 16 || i3 == 32;
    }

    private boolean e() {
        return y.E(this) == 1;
    }

    private boolean f() {
        com.google.android.material.button.a aVar = this.f3694h;
        return (aVar == null || aVar.o()) ? false : true;
    }

    private void g() {
        if (c()) {
            androidx.core.widget.j.i(this, this.f3699m, null, null, null);
        } else if (b()) {
            androidx.core.widget.j.i(this, null, null, this.f3699m, null);
        } else if (d()) {
            androidx.core.widget.j.i(this, null, this.f3699m, null, null);
        }
    }

    private String getA11yClassName() {
        return (a() ? CompoundButton.class : Button.class).getName();
    }

    private Layout.Alignment getActualTextAlignment() {
        if (Build.VERSION.SDK_INT < 17) {
            return getGravityTextAlignment();
        }
        int textAlignment = getTextAlignment();
        return textAlignment != 1 ? (textAlignment == 6 || textAlignment == 3) ? Layout.Alignment.ALIGN_OPPOSITE : textAlignment != 4 ? Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER : getGravityTextAlignment();
    }

    private Layout.Alignment getGravityTextAlignment() {
        int gravity = getGravity() & 8388615;
        return gravity != 1 ? (gravity == 5 || gravity == 8388613) ? Layout.Alignment.ALIGN_OPPOSITE : Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
    }

    private int getTextHeight() {
        TextPaint paint = getPaint();
        String string = getText().toString();
        if (getTransformationMethod() != null) {
            string = getTransformationMethod().getTransformation(string, this).toString();
        }
        Rect rect = new Rect();
        paint.getTextBounds(string, 0, string.length(), rect);
        return Math.min(rect.height(), getLayout().getHeight());
    }

    private int getTextWidth() {
        TextPaint paint = getPaint();
        String string = getText().toString();
        if (getTransformationMethod() != null) {
            string = getTransformationMethod().getTransformation(string, this).toString();
        }
        return Math.min((int) paint.measureText(string), getLayout().getEllipsizedWidth());
    }

    private void h(boolean z2) {
        Drawable drawable = this.f3699m;
        boolean z3 = true;
        if (drawable != null) {
            Drawable drawableMutate = androidx.core.graphics.drawable.a.r(drawable).mutate();
            this.f3699m = drawableMutate;
            androidx.core.graphics.drawable.a.o(drawableMutate, this.f3698l);
            PorterDuff.Mode mode = this.f3697k;
            if (mode != null) {
                androidx.core.graphics.drawable.a.p(this.f3699m, mode);
            }
            int intrinsicWidth = this.f3700n;
            if (intrinsicWidth == 0) {
                intrinsicWidth = this.f3699m.getIntrinsicWidth();
            }
            int intrinsicHeight = this.f3700n;
            if (intrinsicHeight == 0) {
                intrinsicHeight = this.f3699m.getIntrinsicHeight();
            }
            Drawable drawable2 = this.f3699m;
            int i3 = this.f3701o;
            int i4 = this.f3702p;
            drawable2.setBounds(i3, i4, intrinsicWidth + i3, intrinsicHeight + i4);
            this.f3699m.setVisible(true, z2);
        }
        if (z2) {
            g();
            return;
        }
        Drawable[] drawableArrA = androidx.core.widget.j.a(this);
        Drawable drawable3 = drawableArrA[0];
        Drawable drawable4 = drawableArrA[1];
        Drawable drawable5 = drawableArrA[2];
        if ((!c() || drawable3 == this.f3699m) && ((!b() || drawable5 == this.f3699m) && (!d() || drawable4 == this.f3699m))) {
            z3 = false;
        }
        if (z3) {
            g();
        }
    }

    private void i(int i3, int i4) {
        if (this.f3699m == null || getLayout() == null) {
            return;
        }
        if (!c() && !b()) {
            if (d()) {
                this.f3701o = 0;
                if (this.f3706t == 16) {
                    this.f3702p = 0;
                    h(false);
                    return;
                }
                int intrinsicHeight = this.f3700n;
                if (intrinsicHeight == 0) {
                    intrinsicHeight = this.f3699m.getIntrinsicHeight();
                }
                int textHeight = (((((i4 - getTextHeight()) - getPaddingTop()) - intrinsicHeight) - this.f3703q) - getPaddingBottom()) / 2;
                if (this.f3702p != textHeight) {
                    this.f3702p = textHeight;
                    h(false);
                    return;
                }
                return;
            }
            return;
        }
        this.f3702p = 0;
        Layout.Alignment actualTextAlignment = getActualTextAlignment();
        int i5 = this.f3706t;
        if (i5 == 1 || i5 == 3 || ((i5 == 2 && actualTextAlignment == Layout.Alignment.ALIGN_NORMAL) || (i5 == 4 && actualTextAlignment == Layout.Alignment.ALIGN_OPPOSITE))) {
            this.f3701o = 0;
            h(false);
            return;
        }
        int intrinsicWidth = this.f3700n;
        if (intrinsicWidth == 0) {
            intrinsicWidth = this.f3699m.getIntrinsicWidth();
        }
        int textWidth = ((((i3 - getTextWidth()) - y.I(this)) - intrinsicWidth) - this.f3703q) - y.J(this);
        if (actualTextAlignment == Layout.Alignment.ALIGN_CENTER) {
            textWidth /= 2;
        }
        if (e() != (this.f3706t == 4)) {
            textWidth = -textWidth;
        }
        if (this.f3701o != textWidth) {
            this.f3701o = textWidth;
            h(false);
        }
    }

    public boolean a() {
        com.google.android.material.button.a aVar = this.f3694h;
        return aVar != null && aVar.p();
    }

    @Override // android.view.View
    public ColorStateList getBackgroundTintList() {
        return getSupportBackgroundTintList();
    }

    @Override // android.view.View
    public PorterDuff.Mode getBackgroundTintMode() {
        return getSupportBackgroundTintMode();
    }

    public int getCornerRadius() {
        if (f()) {
            return this.f3694h.b();
        }
        return 0;
    }

    public Drawable getIcon() {
        return this.f3699m;
    }

    public int getIconGravity() {
        return this.f3706t;
    }

    public int getIconPadding() {
        return this.f3703q;
    }

    public int getIconSize() {
        return this.f3700n;
    }

    public ColorStateList getIconTint() {
        return this.f3698l;
    }

    public PorterDuff.Mode getIconTintMode() {
        return this.f3697k;
    }

    public int getInsetBottom() {
        return this.f3694h.c();
    }

    public int getInsetTop() {
        return this.f3694h.d();
    }

    public ColorStateList getRippleColor() {
        if (f()) {
            return this.f3694h.h();
        }
        return null;
    }

    public k getShapeAppearanceModel() {
        if (f()) {
            return this.f3694h.i();
        }
        throw new IllegalStateException("Attempted to get ShapeAppearanceModel from a MaterialButton which has an overwritten background.");
    }

    public ColorStateList getStrokeColor() {
        if (f()) {
            return this.f3694h.j();
        }
        return null;
    }

    public int getStrokeWidth() {
        if (f()) {
            return this.f3694h.k();
        }
        return 0;
    }

    @Override // androidx.appcompat.widget.f, androidx.core.view.w
    public ColorStateList getSupportBackgroundTintList() {
        return f() ? this.f3694h.l() : super.getSupportBackgroundTintList();
    }

    @Override // androidx.appcompat.widget.f, androidx.core.view.w
    public PorterDuff.Mode getSupportBackgroundTintMode() {
        return f() ? this.f3694h.m() : super.getSupportBackgroundTintMode();
    }

    @Override // android.widget.Checkable
    public boolean isChecked() {
        return this.f3704r;
    }

    @Override // android.widget.TextView, android.view.View
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (f()) {
            h.f(this, this.f3694h.f());
        }
    }

    @Override // android.widget.TextView, android.view.View
    protected int[] onCreateDrawableState(int i3) {
        int[] iArrOnCreateDrawableState = super.onCreateDrawableState(i3 + 2);
        if (a()) {
            Button.mergeDrawableStates(iArrOnCreateDrawableState, f3691u);
        }
        if (isChecked()) {
            Button.mergeDrawableStates(iArrOnCreateDrawableState, f3692v);
        }
        return iArrOnCreateDrawableState;
    }

    @Override // androidx.appcompat.widget.f, android.view.View
    public void onInitializeAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        super.onInitializeAccessibilityEvent(accessibilityEvent);
        accessibilityEvent.setClassName(getA11yClassName());
        accessibilityEvent.setChecked(isChecked());
    }

    @Override // androidx.appcompat.widget.f, android.view.View
    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo accessibilityNodeInfo) {
        super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
        accessibilityNodeInfo.setClassName(getA11yClassName());
        accessibilityNodeInfo.setCheckable(a());
        accessibilityNodeInfo.setChecked(isChecked());
        accessibilityNodeInfo.setClickable(isClickable());
    }

    @Override // androidx.appcompat.widget.f, android.widget.TextView, android.view.View
    protected void onLayout(boolean z2, int i3, int i4, int i5, int i6) {
        com.google.android.material.button.a aVar;
        super.onLayout(z2, i3, i4, i5, i6);
        if (Build.VERSION.SDK_INT == 21 && (aVar = this.f3694h) != null) {
            aVar.H(i6 - i4, i5 - i3);
        }
        i(getMeasuredWidth(), getMeasuredHeight());
    }

    @Override // android.widget.TextView, android.view.View
    public void onRestoreInstanceState(Parcelable parcelable) {
        if (!(parcelable instanceof c)) {
            super.onRestoreInstanceState(parcelable);
            return;
        }
        c cVar = (c) parcelable;
        super.onRestoreInstanceState(cVar.j());
        setChecked(cVar.f3707g);
    }

    @Override // android.widget.TextView, android.view.View
    public Parcelable onSaveInstanceState() {
        c cVar = new c(super.onSaveInstanceState());
        cVar.f3707g = this.f3704r;
        return cVar;
    }

    @Override // androidx.appcompat.widget.f, android.widget.TextView
    protected void onTextChanged(CharSequence charSequence, int i3, int i4, int i5) {
        super.onTextChanged(charSequence, i3, i4, i5);
        i(getMeasuredWidth(), getMeasuredHeight());
    }

    @Override // android.view.View
    public boolean performClick() {
        toggle();
        return super.performClick();
    }

    @Override // android.view.View
    public void refreshDrawableState() {
        super.refreshDrawableState();
        if (this.f3699m != null) {
            if (this.f3699m.setState(getDrawableState())) {
                invalidate();
            }
        }
    }

    @Override // android.view.View
    public void setBackground(Drawable drawable) {
        setBackgroundDrawable(drawable);
    }

    @Override // android.view.View
    public void setBackgroundColor(int i3) {
        if (f()) {
            this.f3694h.r(i3);
        } else {
            super.setBackgroundColor(i3);
        }
    }

    @Override // androidx.appcompat.widget.f, android.view.View
    public void setBackgroundDrawable(Drawable drawable) {
        if (!f()) {
            super.setBackgroundDrawable(drawable);
        } else {
            if (drawable == getBackground()) {
                getBackground().setState(drawable.getState());
                return;
            }
            Log.w("MaterialButton", "MaterialButton manages its own background to control elevation, shape, color and states. Consider using backgroundTint, shapeAppearance and other attributes where available. A custom background will ignore these attributes and you should consider handling interaction states such as pressed, focused and disabled");
            this.f3694h.s();
            super.setBackgroundDrawable(drawable);
        }
    }

    @Override // androidx.appcompat.widget.f, android.view.View
    public void setBackgroundResource(int i3) {
        setBackgroundDrawable(i3 != 0 ? d.a.b(getContext(), i3) : null);
    }

    @Override // android.view.View
    public void setBackgroundTintList(ColorStateList colorStateList) {
        setSupportBackgroundTintList(colorStateList);
    }

    @Override // android.view.View
    public void setBackgroundTintMode(PorterDuff.Mode mode) {
        setSupportBackgroundTintMode(mode);
    }

    public void setCheckable(boolean z2) {
        if (f()) {
            this.f3694h.t(z2);
        }
    }

    @Override // android.widget.Checkable
    public void setChecked(boolean z2) {
        if (a() && isEnabled() && this.f3704r != z2) {
            this.f3704r = z2;
            refreshDrawableState();
            if (getParent() instanceof MaterialButtonToggleGroup) {
                ((MaterialButtonToggleGroup) getParent()).m(this, this.f3704r);
            }
            if (this.f3705s) {
                return;
            }
            this.f3705s = true;
            Iterator<a> it = this.f3695i.iterator();
            while (it.hasNext()) {
                it.next().a(this, this.f3704r);
            }
            this.f3705s = false;
        }
    }

    public void setCornerRadius(int i3) {
        if (f()) {
            this.f3694h.u(i3);
        }
    }

    public void setCornerRadiusResource(int i3) {
        if (f()) {
            setCornerRadius(getResources().getDimensionPixelSize(i3));
        }
    }

    @Override // android.view.View
    public void setElevation(float f3) {
        super.setElevation(f3);
        if (f()) {
            this.f3694h.f().W(f3);
        }
    }

    public void setIcon(Drawable drawable) {
        if (this.f3699m != drawable) {
            this.f3699m = drawable;
            h(true);
            i(getMeasuredWidth(), getMeasuredHeight());
        }
    }

    public void setIconGravity(int i3) {
        if (this.f3706t != i3) {
            this.f3706t = i3;
            i(getMeasuredWidth(), getMeasuredHeight());
        }
    }

    public void setIconPadding(int i3) {
        if (this.f3703q != i3) {
            this.f3703q = i3;
            setCompoundDrawablePadding(i3);
        }
    }

    public void setIconResource(int i3) {
        setIcon(i3 != 0 ? d.a.b(getContext(), i3) : null);
    }

    public void setIconSize(int i3) {
        if (i3 < 0) {
            throw new IllegalArgumentException("iconSize cannot be less than 0");
        }
        if (this.f3700n != i3) {
            this.f3700n = i3;
            h(true);
        }
    }

    public void setIconTint(ColorStateList colorStateList) {
        if (this.f3698l != colorStateList) {
            this.f3698l = colorStateList;
            h(false);
        }
    }

    public void setIconTintMode(PorterDuff.Mode mode) {
        if (this.f3697k != mode) {
            this.f3697k = mode;
            h(false);
        }
    }

    public void setIconTintResource(int i3) {
        setIconTint(d.a.a(getContext(), i3));
    }

    public void setInsetBottom(int i3) {
        this.f3694h.v(i3);
    }

    public void setInsetTop(int i3) {
        this.f3694h.w(i3);
    }

    void setInternalBackground(Drawable drawable) {
        super.setBackgroundDrawable(drawable);
    }

    void setOnPressedChangeListenerInternal(b bVar) {
        this.f3696j = bVar;
    }

    @Override // android.view.View
    public void setPressed(boolean z2) {
        b bVar = this.f3696j;
        if (bVar != null) {
            bVar.a(this, z2);
        }
        super.setPressed(z2);
    }

    public void setRippleColor(ColorStateList colorStateList) {
        if (f()) {
            this.f3694h.x(colorStateList);
        }
    }

    public void setRippleColorResource(int i3) {
        if (f()) {
            setRippleColor(d.a.a(getContext(), i3));
        }
    }

    @Override // c1.n
    public void setShapeAppearanceModel(k kVar) {
        if (!f()) {
            throw new IllegalStateException("Attempted to set ShapeAppearanceModel on a MaterialButton which has an overwritten background.");
        }
        this.f3694h.y(kVar);
    }

    void setShouldDrawSurfaceColorStroke(boolean z2) {
        if (f()) {
            this.f3694h.z(z2);
        }
    }

    public void setStrokeColor(ColorStateList colorStateList) {
        if (f()) {
            this.f3694h.A(colorStateList);
        }
    }

    public void setStrokeColorResource(int i3) {
        if (f()) {
            setStrokeColor(d.a.a(getContext(), i3));
        }
    }

    public void setStrokeWidth(int i3) {
        if (f()) {
            this.f3694h.B(i3);
        }
    }

    public void setStrokeWidthResource(int i3) {
        if (f()) {
            setStrokeWidth(getResources().getDimensionPixelSize(i3));
        }
    }

    @Override // androidx.appcompat.widget.f, androidx.core.view.w
    public void setSupportBackgroundTintList(ColorStateList colorStateList) {
        if (f()) {
            this.f3694h.C(colorStateList);
        } else {
            super.setSupportBackgroundTintList(colorStateList);
        }
    }

    @Override // androidx.appcompat.widget.f, androidx.core.view.w
    public void setSupportBackgroundTintMode(PorterDuff.Mode mode) {
        if (f()) {
            this.f3694h.D(mode);
        } else {
            super.setSupportBackgroundTintMode(mode);
        }
    }

    @Override // android.view.View
    public void setTextAlignment(int i3) {
        super.setTextAlignment(i3);
        i(getMeasuredWidth(), getMeasuredHeight());
    }

    @Override // android.widget.Checkable
    public void toggle() {
        setChecked(!this.f3704r);
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public MaterialButton(Context context, AttributeSet attributeSet, int i3) {
        int i4 = f3693w;
        super(e1.a.c(context, attributeSet, i3, i4), attributeSet, i3);
        this.f3695i = new LinkedHashSet<>();
        this.f3704r = false;
        this.f3705s = false;
        Context context2 = getContext();
        TypedArray typedArrayH = m.h(context2, attributeSet, l0.k.V0, i3, i4, new int[0]);
        this.f3703q = typedArrayH.getDimensionPixelSize(l0.k.f5494i1, 0);
        this.f3697k = o.f(typedArrayH.getInt(l0.k.f5506l1, -1), PorterDuff.Mode.SRC_IN);
        this.f3698l = z0.c.a(getContext(), typedArrayH, l0.k.f5502k1);
        this.f3699m = z0.c.d(getContext(), typedArrayH, l0.k.f5486g1);
        this.f3706t = typedArrayH.getInteger(l0.k.f5490h1, 1);
        this.f3700n = typedArrayH.getDimensionPixelSize(l0.k.f5498j1, 0);
        com.google.android.material.button.a aVar = new com.google.android.material.button.a(this, k.e(context2, attributeSet, i3, i4).m());
        this.f3694h = aVar;
        aVar.q(typedArrayH);
        typedArrayH.recycle();
        setCompoundDrawablePadding(this.f3703q);
        h(this.f3699m != null);
    }
}
