package com.google.android.material.materialswitch;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.view.View;
import com.google.android.material.R;
import com.google.android.material.drawable.DrawableUtils;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.theme.overlay.MaterialThemeOverlay;
import defpackage.cm7;
import defpackage.fp9;
import defpackage.g61;
import defpackage.gp;
import defpackage.jg9;
import defpackage.of2;
import defpackage.rq6;
import defpackage.vo6;

/* JADX INFO: loaded from: classes2.dex */
public class MaterialSwitch extends jg9 {
    private static final int DEF_STYLE_RES = R.style.Widget_Material3_CompoundButton_MaterialSwitch;
    private static final int[] STATE_SET_WITH_ICON = {R.attr.state_with_icon};
    private int[] currentStateChecked;
    private int[] currentStateUnchecked;

    @rq6
    private Drawable thumbDrawable;

    @rq6
    private Drawable thumbIconDrawable;

    @cm7
    private int thumbIconSize;

    @rq6
    private ColorStateList thumbIconTintList;

    @vo6
    private PorterDuff.Mode thumbIconTintMode;

    @rq6
    private ColorStateList thumbTintList;

    @rq6
    private Drawable trackDecorationDrawable;

    @rq6
    private ColorStateList trackDecorationTintList;

    @vo6
    private PorterDuff.Mode trackDecorationTintMode;

    @rq6
    private Drawable trackDrawable;

    @rq6
    private ColorStateList trackTintList;

    public MaterialSwitch(@vo6 Context context) {
        this(context, null);
    }

    private void refreshThumbDrawable() {
        this.thumbDrawable = DrawableUtils.createTintableDrawableIfNeeded(this.thumbDrawable, this.thumbTintList, getThumbTintMode());
        this.thumbIconDrawable = DrawableUtils.createTintableDrawableIfNeeded(this.thumbIconDrawable, this.thumbIconTintList, this.thumbIconTintMode);
        updateDrawableTints();
        Drawable drawable = this.thumbDrawable;
        Drawable drawable2 = this.thumbIconDrawable;
        int i = this.thumbIconSize;
        super.setThumbDrawable(DrawableUtils.compositeTwoLayeredDrawable(drawable, drawable2, i, i));
        refreshDrawableState();
    }

    private void refreshTrackDrawable() {
        this.trackDrawable = DrawableUtils.createTintableDrawableIfNeeded(this.trackDrawable, this.trackTintList, getTrackTintMode());
        this.trackDecorationDrawable = DrawableUtils.createTintableDrawableIfNeeded(this.trackDecorationDrawable, this.trackDecorationTintList, this.trackDecorationTintMode);
        updateDrawableTints();
        Drawable layerDrawable = this.trackDrawable;
        if (layerDrawable != null && this.trackDecorationDrawable != null) {
            layerDrawable = new LayerDrawable(new Drawable[]{this.trackDrawable, this.trackDecorationDrawable});
        } else if (layerDrawable == null) {
            layerDrawable = this.trackDecorationDrawable;
        }
        if (layerDrawable != null) {
            setSwitchMinWidth(layerDrawable.getIntrinsicWidth());
        }
        super.setTrackDrawable(layerDrawable);
    }

    private static void setInterpolatedDrawableTintIfPossible(@rq6 Drawable drawable, @rq6 ColorStateList colorStateList, @vo6 int[] iArr, @vo6 int[] iArr2, float f) {
        if (drawable == null || colorStateList == null) {
            return;
        }
        drawable.setTint(g61.d(colorStateList.getColorForState(iArr, 0), colorStateList.getColorForState(iArr2, 0), f));
    }

    private void updateDrawableTints() {
        if (this.thumbTintList == null && this.thumbIconTintList == null && this.trackTintList == null && this.trackDecorationTintList == null) {
            return;
        }
        float thumbPosition = getThumbPosition();
        ColorStateList colorStateList = this.thumbTintList;
        if (colorStateList != null) {
            setInterpolatedDrawableTintIfPossible(this.thumbDrawable, colorStateList, this.currentStateUnchecked, this.currentStateChecked, thumbPosition);
        }
        ColorStateList colorStateList2 = this.thumbIconTintList;
        if (colorStateList2 != null) {
            setInterpolatedDrawableTintIfPossible(this.thumbIconDrawable, colorStateList2, this.currentStateUnchecked, this.currentStateChecked, thumbPosition);
        }
        ColorStateList colorStateList3 = this.trackTintList;
        if (colorStateList3 != null) {
            setInterpolatedDrawableTintIfPossible(this.trackDrawable, colorStateList3, this.currentStateUnchecked, this.currentStateChecked, thumbPosition);
        }
        ColorStateList colorStateList4 = this.trackDecorationTintList;
        if (colorStateList4 != null) {
            setInterpolatedDrawableTintIfPossible(this.trackDecorationDrawable, colorStateList4, this.currentStateUnchecked, this.currentStateChecked, thumbPosition);
        }
    }

    @Override // defpackage.jg9
    @rq6
    public Drawable getThumbDrawable() {
        return this.thumbDrawable;
    }

    @rq6
    public Drawable getThumbIconDrawable() {
        return this.thumbIconDrawable;
    }

    @cm7
    public int getThumbIconSize() {
        return this.thumbIconSize;
    }

    @rq6
    public ColorStateList getThumbIconTintList() {
        return this.thumbIconTintList;
    }

    @vo6
    public PorterDuff.Mode getThumbIconTintMode() {
        return this.thumbIconTintMode;
    }

    @Override // defpackage.jg9
    @rq6
    public ColorStateList getThumbTintList() {
        return this.thumbTintList;
    }

    @rq6
    public Drawable getTrackDecorationDrawable() {
        return this.trackDecorationDrawable;
    }

    @rq6
    public ColorStateList getTrackDecorationTintList() {
        return this.trackDecorationTintList;
    }

    @vo6
    public PorterDuff.Mode getTrackDecorationTintMode() {
        return this.trackDecorationTintMode;
    }

    @Override // defpackage.jg9
    @rq6
    public Drawable getTrackDrawable() {
        return this.trackDrawable;
    }

    @Override // defpackage.jg9
    @rq6
    public ColorStateList getTrackTintList() {
        return this.trackTintList;
    }

    @Override // android.view.View
    public void invalidate() {
        updateDrawableTints();
        super.invalidate();
    }

    @Override // defpackage.jg9, android.widget.CompoundButton, android.widget.TextView, android.view.View
    public int[] onCreateDrawableState(int i) {
        int[] iArrOnCreateDrawableState = super.onCreateDrawableState(i + 1);
        if (this.thumbIconDrawable != null) {
            View.mergeDrawableStates(iArrOnCreateDrawableState, STATE_SET_WITH_ICON);
        }
        this.currentStateUnchecked = DrawableUtils.getUncheckedState(iArrOnCreateDrawableState);
        this.currentStateChecked = DrawableUtils.getCheckedState(iArrOnCreateDrawableState);
        return iArrOnCreateDrawableState;
    }

    @Override // defpackage.jg9
    public void setThumbDrawable(@rq6 Drawable drawable) {
        this.thumbDrawable = drawable;
        refreshThumbDrawable();
    }

    public void setThumbIconDrawable(@rq6 Drawable drawable) {
        this.thumbIconDrawable = drawable;
        refreshThumbDrawable();
    }

    public void setThumbIconResource(@of2 int i) {
        setThumbIconDrawable(gp.a(getContext(), i));
    }

    public void setThumbIconSize(@cm7 int i) {
        if (this.thumbIconSize != i) {
            this.thumbIconSize = i;
            refreshThumbDrawable();
        }
    }

    public void setThumbIconTintList(@rq6 ColorStateList colorStateList) {
        this.thumbIconTintList = colorStateList;
        refreshThumbDrawable();
    }

    public void setThumbIconTintMode(@vo6 PorterDuff.Mode mode) {
        this.thumbIconTintMode = mode;
        refreshThumbDrawable();
    }

    @Override // defpackage.jg9
    public void setThumbTintList(@rq6 ColorStateList colorStateList) {
        this.thumbTintList = colorStateList;
        refreshThumbDrawable();
    }

    @Override // defpackage.jg9
    public void setThumbTintMode(@rq6 PorterDuff.Mode mode) {
        super.setThumbTintMode(mode);
        refreshThumbDrawable();
    }

    public void setTrackDecorationDrawable(@rq6 Drawable drawable) {
        this.trackDecorationDrawable = drawable;
        refreshTrackDrawable();
    }

    public void setTrackDecorationResource(@of2 int i) {
        setTrackDecorationDrawable(gp.a(getContext(), i));
    }

    public void setTrackDecorationTintList(@rq6 ColorStateList colorStateList) {
        this.trackDecorationTintList = colorStateList;
        refreshTrackDrawable();
    }

    public void setTrackDecorationTintMode(@vo6 PorterDuff.Mode mode) {
        this.trackDecorationTintMode = mode;
        refreshTrackDrawable();
    }

    @Override // defpackage.jg9
    public void setTrackDrawable(@rq6 Drawable drawable) {
        this.trackDrawable = drawable;
        refreshTrackDrawable();
    }

    @Override // defpackage.jg9
    public void setTrackTintList(@rq6 ColorStateList colorStateList) {
        this.trackTintList = colorStateList;
        refreshTrackDrawable();
    }

    @Override // defpackage.jg9
    public void setTrackTintMode(@rq6 PorterDuff.Mode mode) {
        super.setTrackTintMode(mode);
        refreshTrackDrawable();
    }

    public MaterialSwitch(@vo6 Context context, @rq6 AttributeSet attributeSet) {
        this(context, attributeSet, R.attr.materialSwitchStyle);
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public MaterialSwitch(@vo6 Context context, @rq6 AttributeSet attributeSet, int i) {
        int i2 = DEF_STYLE_RES;
        super(MaterialThemeOverlay.wrap(context, attributeSet, i, i2), attributeSet, i);
        this.thumbIconSize = -1;
        Context context2 = getContext();
        this.thumbDrawable = super.getThumbDrawable();
        this.thumbTintList = super.getThumbTintList();
        super.setThumbTintList(null);
        this.trackDrawable = super.getTrackDrawable();
        this.trackTintList = super.getTrackTintList();
        super.setTrackTintList(null);
        fp9 fp9VarObtainTintedStyledAttributes = ThemeEnforcement.obtainTintedStyledAttributes(context2, attributeSet, R.styleable.MaterialSwitch, i, i2, new int[0]);
        this.thumbIconDrawable = fp9VarObtainTintedStyledAttributes.b(R.styleable.MaterialSwitch_thumbIcon);
        int i3 = R.styleable.MaterialSwitch_thumbIconSize;
        TypedArray typedArray = fp9VarObtainTintedStyledAttributes.b;
        this.thumbIconSize = typedArray.getDimensionPixelSize(i3, -1);
        this.thumbIconTintList = fp9VarObtainTintedStyledAttributes.a(R.styleable.MaterialSwitch_thumbIconTint);
        int i4 = typedArray.getInt(R.styleable.MaterialSwitch_thumbIconTintMode, -1);
        PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
        this.thumbIconTintMode = ViewUtils.parseTintMode(i4, mode);
        this.trackDecorationDrawable = fp9VarObtainTintedStyledAttributes.b(R.styleable.MaterialSwitch_trackDecoration);
        this.trackDecorationTintList = fp9VarObtainTintedStyledAttributes.a(R.styleable.MaterialSwitch_trackDecorationTint);
        this.trackDecorationTintMode = ViewUtils.parseTintMode(typedArray.getInt(R.styleable.MaterialSwitch_trackDecorationTintMode, -1), mode);
        fp9VarObtainTintedStyledAttributes.g();
        setEnforceSwitchWidth(false);
        refreshThumbDrawable();
        refreshTrackDrawable();
    }
}
