| 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.SharedPreferences; |
| 23 |
import android.content.res.TypedArray; |
| 24 |
import android.os.Parcel; |
| 25 |
import android.os.Parcelable; |
| 26 |
import android.text.TextUtils; |
| 27 |
import android.util.AttributeSet; |
| 28 |
import android.view.View; |
| 29 |
import android.widget.TextView; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
@Deprecated |
| 43 |
public abstract class TwoStatePreference extends Preference { |
| 44 |
|
| 45 |
private CharSequence mSummaryOn; |
| 46 |
private CharSequence mSummaryOff; |
| 47 |
boolean mChecked; |
| 48 |
private boolean mCheckedSet; |
| 49 |
private boolean mDisableDependentsState; |
| 50 |
|
| 51 |
public TwoStatePreference( |
| 52 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 53 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 54 |
} |
| 55 |
|
| 56 |
public TwoStatePreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 57 |
this(context, attrs, defStyleAttr, 0); |
| 58 |
} |
| 59 |
|
| 60 |
public TwoStatePreference(Context context, AttributeSet attrs) { |
| 61 |
this(context, attrs, 0); |
| 62 |
} |
| 63 |
|
| 64 |
public TwoStatePreference(Context context) { |
| 65 |
this(context, null); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
protected void onClick() { |
| 70 |
super.onClick(); |
| 71 |
|
| 72 |
final boolean newValue = !isChecked(); |
| 73 |
if (callChangeListener(newValue)) { |
| 74 |
setChecked(newValue); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public void setChecked(boolean checked) { |
| 84 |
|
| 85 |
final boolean changed = mChecked != checked; |
| 86 |
if (changed || !mCheckedSet) { |
| 87 |
mChecked = checked; |
| 88 |
mCheckedSet = true; |
| 89 |
persistBoolean(checked); |
| 90 |
if (changed) { |
| 91 |
notifyDependencyChange(shouldDisableDependents()); |
| 92 |
notifyChanged(); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public boolean isChecked() { |
| 103 |
return mChecked; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public boolean shouldDisableDependents() { |
| 108 |
boolean shouldDisable = mDisableDependentsState ? mChecked : !mChecked; |
| 109 |
return shouldDisable || super.shouldDisableDependents(); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public void setSummaryOn(CharSequence summary) { |
| 118 |
mSummaryOn = summary; |
| 119 |
if (isChecked()) { |
| 120 |
notifyChanged(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public void setSummaryOn(@StringRes int summaryResId) { |
| 129 |
setSummaryOn(getContext().getString(summaryResId)); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public CharSequence getSummaryOn() { |
| 137 |
return mSummaryOn; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public void setSummaryOff(CharSequence summary) { |
| 146 |
mSummaryOff = summary; |
| 147 |
if (!isChecked()) { |
| 148 |
notifyChanged(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void setSummaryOff(@StringRes int summaryResId) { |
| 157 |
setSummaryOff(getContext().getString(summaryResId)); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public CharSequence getSummaryOff() { |
| 165 |
return mSummaryOff; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public boolean getDisableDependentsState() { |
| 176 |
return mDisableDependentsState; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public void setDisableDependentsState(boolean disableDependentsState) { |
| 186 |
mDisableDependentsState = disableDependentsState; |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
protected Object onGetDefaultValue(TypedArray a, int index) { |
| 191 |
return a.getBoolean(index, false); |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { |
| 196 |
setChecked(restoreValue ? getPersistedBoolean(mChecked) |
| 197 |
: (Boolean) defaultValue); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
@UnsupportedAppUsage |
| 205 |
void syncSummaryView(View view) { |
| 206 |
|
| 207 |
TextView summaryView = (TextView) view.findViewById(com.android.internal.R.id.summary); |
| 208 |
if (summaryView != null) { |
| 209 |
boolean useDefaultSummary = true; |
| 210 |
if (mChecked && !TextUtils.isEmpty(mSummaryOn)) { |
| 211 |
summaryView.setText(mSummaryOn); |
| 212 |
useDefaultSummary = false; |
| 213 |
} else if (!mChecked && !TextUtils.isEmpty(mSummaryOff)) { |
| 214 |
summaryView.setText(mSummaryOff); |
| 215 |
useDefaultSummary = false; |
| 216 |
} |
| 217 |
|
| 218 |
if (useDefaultSummary) { |
| 219 |
final CharSequence summary = getSummary(); |
| 220 |
if (!TextUtils.isEmpty(summary)) { |
| 221 |
summaryView.setText(summary); |
| 222 |
useDefaultSummary = false; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
int newVisibility = View.GONE; |
| 227 |
if (!useDefaultSummary) { |
| 228 |
|
| 229 |
newVisibility = View.VISIBLE; |
| 230 |
} |
| 231 |
if (newVisibility != summaryView.getVisibility()) { |
| 232 |
summaryView.setVisibility(newVisibility); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
protected Parcelable onSaveInstanceState() { |
| 239 |
final Parcelable superState = super.onSaveInstanceState(); |
| 240 |
if (isPersistent()) { |
| 241 |
|
| 242 |
return superState; |
| 243 |
} |
| 244 |
|
| 245 |
final SavedState myState = new SavedState(superState); |
| 246 |
myState.checked = isChecked(); |
| 247 |
return myState; |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
protected void onRestoreInstanceState(Parcelable state) { |
| 252 |
if (state == null || !state.getClass().equals(SavedState.class)) { |
| 253 |
|
| 254 |
super.onRestoreInstanceState(state); |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
SavedState myState = (SavedState) state; |
| 259 |
super.onRestoreInstanceState(myState.getSuperState()); |
| 260 |
setChecked(myState.checked); |
| 261 |
} |
| 262 |
|
| 263 |
static class SavedState extends BaseSavedState { |
| 264 |
boolean checked; |
| 265 |
|
| 266 |
public SavedState(Parcel source) { |
| 267 |
super(source); |
| 268 |
checked = source.readInt() == 1; |
| 269 |
} |
| 270 |
|
| 271 |
@Override |
| 272 |
public void writeToParcel(Parcel dest, int flags) { |
| 273 |
super.writeToParcel(dest, flags); |
| 274 |
dest.writeInt(checked ? 1 : 0); |
| 275 |
} |
| 276 |
|
| 277 |
public SavedState(Parcelable superState) { |
| 278 |
super(superState); |
| 279 |
} |
| 280 |
|
| 281 |
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
| 282 |
new Parcelable.Creator<SavedState>() { |
| 283 |
public SavedState createFromParcel(Parcel in) { |
| 284 |
return new SavedState(in); |
| 285 |
} |
| 286 |
|
| 287 |
public SavedState[] newArray(int size) { |
| 288 |
return new SavedState[size]; |
| 289 |
} |
| 290 |
}; |
| 291 |
} |
| 292 |
} |