| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.preference; |
| 18 |
|
| 19 |
import android.annotation.StringRes; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.Context; |
| 22 |
import android.content.res.TypedArray; |
| 23 |
import android.os.Build; |
| 24 |
import android.util.AttributeSet; |
| 25 |
import android.view.View; |
| 26 |
import android.widget.Checkable; |
| 27 |
import android.widget.CompoundButton; |
| 28 |
import android.widget.Switch; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
@Deprecated |
| 48 |
public class SwitchPreference extends TwoStatePreference { |
| 49 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 50 |
private final Listener mListener = new Listener(); |
| 51 |
|
| 52 |
|
| 53 |
private CharSequence mSwitchOn; |
| 54 |
private CharSequence mSwitchOff; |
| 55 |
|
| 56 |
private class Listener implements CompoundButton.OnCheckedChangeListener { |
| 57 |
@Override |
| 58 |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 59 |
if (!callChangeListener(isChecked)) { |
| 60 |
|
| 61 |
|
| 62 |
buttonView.setChecked(!isChecked); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
SwitchPreference.this.setChecked(isChecked); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 84 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 85 |
|
| 86 |
TypedArray a = context.obtainStyledAttributes(attrs, |
| 87 |
com.android.internal.R.styleable.SwitchPreference, defStyleAttr, defStyleRes); |
| 88 |
setSummaryOn(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOn)); |
| 89 |
setSummaryOff(a.getString(com.android.internal.R.styleable.SwitchPreference_summaryOff)); |
| 90 |
setSwitchTextOn(a.getString( |
| 91 |
com.android.internal.R.styleable.SwitchPreference_switchTextOn)); |
| 92 |
setSwitchTextOff(a.getString( |
| 93 |
com.android.internal.R.styleable.SwitchPreference_switchTextOff)); |
| 94 |
setDisableDependentsState(a.getBoolean( |
| 95 |
com.android.internal.R.styleable.SwitchPreference_disableDependentsState, false)); |
| 96 |
a.recycle(); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 109 |
this(context, attrs, defStyleAttr, 0); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public SwitchPreference(Context context, AttributeSet attrs) { |
| 119 |
this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public SwitchPreference(Context context) { |
| 128 |
this(context, null); |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
protected void onBindView(View view) { |
| 133 |
super.onBindView(view); |
| 134 |
|
| 135 |
View checkableView = view.findViewById(com.android.internal.R.id.switch_widget); |
| 136 |
if (checkableView != null && checkableView instanceof Checkable) { |
| 137 |
if (checkableView instanceof Switch) { |
| 138 |
final Switch switchView = (Switch) checkableView; |
| 139 |
switchView.setOnCheckedChangeListener(null); |
| 140 |
} |
| 141 |
|
| 142 |
((Checkable) checkableView).setChecked(mChecked); |
| 143 |
|
| 144 |
if (checkableView instanceof Switch) { |
| 145 |
final Switch switchView = (Switch) checkableView; |
| 146 |
switchView.setTextOn(mSwitchOn); |
| 147 |
switchView.setTextOff(mSwitchOff); |
| 148 |
switchView.setOnCheckedChangeListener(mListener); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
syncSummaryView(view); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public void setSwitchTextOn(CharSequence onText) { |
| 162 |
mSwitchOn = onText; |
| 163 |
notifyChanged(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public void setSwitchTextOff(CharSequence offText) { |
| 173 |
mSwitchOff = offText; |
| 174 |
notifyChanged(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public void setSwitchTextOn(@StringRes int resId) { |
| 184 |
setSwitchTextOn(getContext().getString(resId)); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public void setSwitchTextOff(@StringRes int resId) { |
| 194 |
setSwitchTextOff(getContext().getString(resId)); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public CharSequence getSwitchTextOn() { |
| 201 |
return mSwitchOn; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public CharSequence getSwitchTextOff() { |
| 208 |
return mSwitchOff; |
| 209 |
} |
| 210 |
} |