| 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.ArrayRes; |
| 20 |
import android.app.AlertDialog.Builder; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.DialogInterface; |
| 24 |
import android.content.res.TypedArray; |
| 25 |
import android.os.Parcel; |
| 26 |
import android.os.Parcelable; |
| 27 |
import android.text.TextUtils; |
| 28 |
import android.util.AttributeSet; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
@Deprecated |
| 47 |
public class ListPreference extends DialogPreference { |
| 48 |
private CharSequence[] mEntries; |
| 49 |
private CharSequence[] mEntryValues; |
| 50 |
private String mValue; |
| 51 |
private String mSummary; |
| 52 |
@UnsupportedAppUsage |
| 53 |
private int mClickedDialogEntryIndex; |
| 54 |
private boolean mValueSet; |
| 55 |
|
| 56 |
public ListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 57 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 58 |
|
| 59 |
TypedArray a = context.obtainStyledAttributes( |
| 60 |
attrs, com.android.internal.R.styleable.ListPreference, defStyleAttr, defStyleRes); |
| 61 |
mEntries = a.getTextArray(com.android.internal.R.styleable.ListPreference_entries); |
| 62 |
mEntryValues = a.getTextArray(com.android.internal.R.styleable.ListPreference_entryValues); |
| 63 |
a.recycle(); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
a = context.obtainStyledAttributes(attrs, |
| 69 |
com.android.internal.R.styleable.Preference, defStyleAttr, defStyleRes); |
| 70 |
mSummary = a.getString(com.android.internal.R.styleable.Preference_summary); |
| 71 |
a.recycle(); |
| 72 |
} |
| 73 |
|
| 74 |
public ListPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 75 |
this(context, attrs, defStyleAttr, 0); |
| 76 |
} |
| 77 |
|
| 78 |
public ListPreference(Context context, AttributeSet attrs) { |
| 79 |
this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle); |
| 80 |
} |
| 81 |
|
| 82 |
public ListPreference(Context context) { |
| 83 |
this(context, null); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public void setEntries(CharSequence[] entries) { |
| 97 |
mEntries = entries; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public void setEntries(@ArrayRes int entriesResId) { |
| 105 |
setEntries(getContext().getResources().getTextArray(entriesResId)); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public CharSequence[] getEntries() { |
| 114 |
return mEntries; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public void setEntryValues(CharSequence[] entryValues) { |
| 125 |
mEntryValues = entryValues; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public void setEntryValues(@ArrayRes int entryValuesResId) { |
| 133 |
setEntryValues(getContext().getResources().getTextArray(entryValuesResId)); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public CharSequence[] getEntryValues() { |
| 142 |
return mEntryValues; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public void setValue(String value) { |
| 152 |
|
| 153 |
final boolean changed = !TextUtils.equals(mValue, value); |
| 154 |
if (changed || !mValueSet) { |
| 155 |
mValue = value; |
| 156 |
mValueSet = true; |
| 157 |
persistString(value); |
| 158 |
if (changed) { |
| 159 |
notifyChanged(); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
@Override |
| 173 |
public CharSequence getSummary() { |
| 174 |
final CharSequence entry = getEntry(); |
| 175 |
if (mSummary == null) { |
| 176 |
return super.getSummary(); |
| 177 |
} else { |
| 178 |
return String.format(mSummary, entry == null ? "" : entry); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
@Override |
| 192 |
public void setSummary(CharSequence summary) { |
| 193 |
super.setSummary(summary); |
| 194 |
if (summary == null && mSummary != null) { |
| 195 |
mSummary = null; |
| 196 |
} else if (summary != null && !summary.equals(mSummary)) { |
| 197 |
mSummary = summary.toString(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public void setValueIndex(int index) { |
| 207 |
if (mEntryValues != null) { |
| 208 |
setValue(mEntryValues[index].toString()); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public String getValue() { |
| 219 |
return mValue; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public CharSequence getEntry() { |
| 228 |
int index = getValueIndex(); |
| 229 |
return index >= 0 && mEntries != null ? mEntries[index] : null; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public int findIndexOfValue(String value) { |
| 239 |
if (value != null && mEntryValues != null) { |
| 240 |
for (int i = mEntryValues.length - 1; i >= 0; i--) { |
| 241 |
if (mEntryValues[i].equals(value)) { |
| 242 |
return i; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
return -1; |
| 247 |
} |
| 248 |
|
| 249 |
private int getValueIndex() { |
| 250 |
return findIndexOfValue(mValue); |
| 251 |
} |
| 252 |
|
| 253 |
@Override |
| 254 |
protected void onPrepareDialogBuilder(Builder builder) { |
| 255 |
super.onPrepareDialogBuilder(builder); |
| 256 |
|
| 257 |
if (mEntries == null || mEntryValues == null) { |
| 258 |
throw new IllegalStateException( |
| 259 |
"ListPreference requires an entries array and an entryValues array."); |
| 260 |
} |
| 261 |
|
| 262 |
mClickedDialogEntryIndex = getValueIndex(); |
| 263 |
builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, |
| 264 |
new DialogInterface.OnClickListener() { |
| 265 |
public void onClick(DialogInterface dialog, int which) { |
| 266 |
mClickedDialogEntryIndex = which; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
ListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE); |
| 273 |
postDismiss(); |
| 274 |
} |
| 275 |
}); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
builder.setPositiveButton(null, null); |
| 283 |
} |
| 284 |
|
| 285 |
@Override |
| 286 |
protected void onDialogClosed(boolean positiveResult) { |
| 287 |
super.onDialogClosed(positiveResult); |
| 288 |
|
| 289 |
if (positiveResult && mClickedDialogEntryIndex >= 0 && mEntryValues != null) { |
| 290 |
String value = mEntryValues[mClickedDialogEntryIndex].toString(); |
| 291 |
if (callChangeListener(value)) { |
| 292 |
setValue(value); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
protected Object onGetDefaultValue(TypedArray a, int index) { |
| 299 |
return a.getString(index); |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { |
| 304 |
setValue(restoreValue ? getPersistedString(mValue) : (String) defaultValue); |
| 305 |
} |
| 306 |
|
| 307 |
@Override |
| 308 |
protected Parcelable onSaveInstanceState() { |
| 309 |
final Parcelable superState = super.onSaveInstanceState(); |
| 310 |
if (isPersistent()) { |
| 311 |
|
| 312 |
return superState; |
| 313 |
} |
| 314 |
|
| 315 |
final SavedState myState = new SavedState(superState); |
| 316 |
myState.value = getValue(); |
| 317 |
return myState; |
| 318 |
} |
| 319 |
|
| 320 |
@Override |
| 321 |
protected void onRestoreInstanceState(Parcelable state) { |
| 322 |
if (state == null || !state.getClass().equals(SavedState.class)) { |
| 323 |
|
| 324 |
super.onRestoreInstanceState(state); |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
SavedState myState = (SavedState) state; |
| 329 |
super.onRestoreInstanceState(myState.getSuperState()); |
| 330 |
setValue(myState.value); |
| 331 |
} |
| 332 |
|
| 333 |
private static class SavedState extends BaseSavedState { |
| 334 |
String value; |
| 335 |
|
| 336 |
public SavedState(Parcel source) { |
| 337 |
super(source); |
| 338 |
value = source.readString(); |
| 339 |
} |
| 340 |
|
| 341 |
@Override |
| 342 |
public void writeToParcel(Parcel dest, int flags) { |
| 343 |
super.writeToParcel(dest, flags); |
| 344 |
dest.writeString(value); |
| 345 |
} |
| 346 |
|
| 347 |
public SavedState(Parcelable superState) { |
| 348 |
super(superState); |
| 349 |
} |
| 350 |
|
| 351 |
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
| 352 |
new Parcelable.Creator<SavedState>() { |
| 353 |
public SavedState createFromParcel(Parcel in) { |
| 354 |
return new SavedState(in); |
| 355 |
} |
| 356 |
|
| 357 |
public SavedState[] newArray(int size) { |
| 358 |
return new SavedState[size]; |
| 359 |
} |
| 360 |
}; |
| 361 |
} |
| 362 |
|
| 363 |
} |