package androidx.preference; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.View; import android.view.accessibility.AccessibilityManager; import android.widget.Checkable; import android.widget.CompoundButton; import android.widget.Switch; import androidx.annotation.RestrictTo; import androidx.core.content.res.TypedArrayUtils; public class SwitchPreference extends TwoStatePreference { private final Listener mListener; private CharSequence mSwitchOff; private CharSequence mSwitchOn; public class Listener implements CompoundButton.OnCheckedChangeListener { public Listener() { } @Override // android.widget.CompoundButton.OnCheckedChangeListener public void onCheckedChanged(CompoundButton compoundButton, boolean z2) { if (!SwitchPreference.this.callChangeListener(Boolean.valueOf(z2))) { compoundButton.setChecked(!z2); } else { SwitchPreference.this.setChecked(z2); } } } public SwitchPreference(Context context) { this(context, null); } public SwitchPreference(Context context, AttributeSet attributeSet) { this(context, attributeSet, TypedArrayUtils.getAttr(context, R.attr.switchPreferenceStyle, 16843629)); } public SwitchPreference(Context context, AttributeSet attributeSet, int i) { this(context, attributeSet, i, 0); } public SwitchPreference(Context context, AttributeSet attributeSet, int i, int i2) { super(context, attributeSet, i, i2); this.mListener = new Listener(); TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.SwitchPreference, i, i2); setSummaryOn(TypedArrayUtils.getString(obtainStyledAttributes, R.styleable.SwitchPreference_summaryOn, R.styleable.SwitchPreference_android_summaryOn)); setSummaryOff(TypedArrayUtils.getString(obtainStyledAttributes, R.styleable.SwitchPreference_summaryOff, R.styleable.SwitchPreference_android_summaryOff)); setSwitchTextOn(TypedArrayUtils.getString(obtainStyledAttributes, R.styleable.SwitchPreference_switchTextOn, R.styleable.SwitchPreference_android_switchTextOn)); setSwitchTextOff(TypedArrayUtils.getString(obtainStyledAttributes, R.styleable.SwitchPreference_switchTextOff, R.styleable.SwitchPreference_android_switchTextOff)); setDisableDependentsState(TypedArrayUtils.getBoolean(obtainStyledAttributes, R.styleable.SwitchPreference_disableDependentsState, R.styleable.SwitchPreference_android_disableDependentsState, false)); obtainStyledAttributes.recycle(); } private void syncSwitchView(View view) { boolean z2 = view instanceof Switch; if (z2) { ((Switch) view).setOnCheckedChangeListener(null); } if (view instanceof Checkable) { ((Checkable) view).setChecked(this.mChecked); } if (z2) { Switch r4 = (Switch) view; r4.setTextOn(this.mSwitchOn); r4.setTextOff(this.mSwitchOff); r4.setOnCheckedChangeListener(this.mListener); } } private void syncViewIfAccessibilityEnabled(View view) { if (((AccessibilityManager) getContext().getSystemService("accessibility")).isEnabled()) { syncSwitchView(view.findViewById(16908352)); syncSummaryView(view.findViewById(16908304)); } } public CharSequence getSwitchTextOff() { return this.mSwitchOff; } public CharSequence getSwitchTextOn() { return this.mSwitchOn; } @Override // androidx.preference.Preference public void onBindViewHolder(PreferenceViewHolder preferenceViewHolder) { super.onBindViewHolder(preferenceViewHolder); syncSwitchView(preferenceViewHolder.findViewById(16908352)); syncSummaryView(preferenceViewHolder); } @Override // androidx.preference.Preference @RestrictTo({RestrictTo.Scope.LIBRARY}) public void performClick(View view) { super.performClick(view); syncViewIfAccessibilityEnabled(view); } public void setSwitchTextOff(int i) { setSwitchTextOff(getContext().getString(i)); } public void setSwitchTextOff(CharSequence charSequence) { this.mSwitchOff = charSequence; notifyChanged(); } public void setSwitchTextOn(int i) { setSwitchTextOn(getContext().getString(i)); } public void setSwitchTextOn(CharSequence charSequence) { this.mSwitchOn = charSequence; notifyChanged(); } }