| 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.content.Context; |
| 22 |
import android.content.DialogInterface; |
| 23 |
import android.content.res.TypedArray; |
| 24 |
import android.os.Parcel; |
| 25 |
import android.os.Parcelable; |
| 26 |
import android.util.AttributeSet; |
| 27 |
|
| 28 |
import java.util.HashSet; |
| 29 |
import java.util.Set; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@Deprecated |
| 49 |
public class MultiSelectListPreference extends DialogPreference { |
| 50 |
private CharSequence[] mEntries; |
| 51 |
private CharSequence[] mEntryValues; |
| 52 |
private Set<String> mValues = new HashSet<String>(); |
| 53 |
private Set<String> mNewValues = new HashSet<String>(); |
| 54 |
private boolean mPreferenceChanged; |
| 55 |
|
| 56 |
public MultiSelectListPreference( |
| 57 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 58 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 59 |
|
| 60 |
final TypedArray a = context.obtainStyledAttributes(attrs, |
| 61 |
com.android.internal.R.styleable.MultiSelectListPreference, defStyleAttr, |
| 62 |
defStyleRes); |
| 63 |
mEntries = a.getTextArray(com.android.internal.R.styleable.MultiSelectListPreference_entries); |
| 64 |
mEntryValues = a.getTextArray(com.android.internal.R.styleable.MultiSelectListPreference_entryValues); |
| 65 |
a.recycle(); |
| 66 |
} |
| 67 |
|
| 68 |
public MultiSelectListPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 69 |
this(context, attrs, defStyleAttr, 0); |
| 70 |
} |
| 71 |
|
| 72 |
public MultiSelectListPreference(Context context, AttributeSet attrs) { |
| 73 |
this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle); |
| 74 |
} |
| 75 |
|
| 76 |
public MultiSelectListPreference(Context context) { |
| 77 |
this(context, null); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public void setEntries(CharSequence[] entries) { |
| 91 |
mEntries = entries; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public void setEntries(@ArrayRes int entriesResId) { |
| 99 |
setEntries(getContext().getResources().getTextArray(entriesResId)); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public CharSequence[] getEntries() { |
| 108 |
return mEntries; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void setEntryValues(CharSequence[] entryValues) { |
| 119 |
mEntryValues = entryValues; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public void setEntryValues(@ArrayRes int entryValuesResId) { |
| 127 |
setEntryValues(getContext().getResources().getTextArray(entryValuesResId)); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public CharSequence[] getEntryValues() { |
| 136 |
return mEntryValues; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public void setValues(Set<String> values) { |
| 146 |
mValues.clear(); |
| 147 |
mValues.addAll(values); |
| 148 |
|
| 149 |
persistStringSet(values); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public Set<String> getValues() { |
| 156 |
return mValues; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public int findIndexOfValue(String value) { |
| 166 |
if (value != null && mEntryValues != null) { |
| 167 |
for (int i = mEntryValues.length - 1; i >= 0; i--) { |
| 168 |
if (mEntryValues[i].equals(value)) { |
| 169 |
return i; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
return -1; |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
protected void onPrepareDialogBuilder(Builder builder) { |
| 178 |
super.onPrepareDialogBuilder(builder); |
| 179 |
|
| 180 |
if (mEntries == null || mEntryValues == null) { |
| 181 |
throw new IllegalStateException( |
| 182 |
"MultiSelectListPreference requires an entries array and " + |
| 183 |
"an entryValues array."); |
| 184 |
} |
| 185 |
|
| 186 |
boolean[] checkedItems = getSelectedItems(); |
| 187 |
builder.setMultiChoiceItems(mEntries, checkedItems, |
| 188 |
new DialogInterface.OnMultiChoiceClickListener() { |
| 189 |
public void onClick(DialogInterface dialog, int which, boolean isChecked) { |
| 190 |
if (isChecked) { |
| 191 |
mPreferenceChanged |= mNewValues.add(mEntryValues[which].toString()); |
| 192 |
} else { |
| 193 |
mPreferenceChanged |= mNewValues.remove(mEntryValues[which].toString()); |
| 194 |
} |
| 195 |
} |
| 196 |
}); |
| 197 |
mNewValues.clear(); |
| 198 |
mNewValues.addAll(mValues); |
| 199 |
} |
| 200 |
|
| 201 |
private boolean[] getSelectedItems() { |
| 202 |
final CharSequence[] entries = mEntryValues; |
| 203 |
final int entryCount = entries.length; |
| 204 |
final Set<String> values = mValues; |
| 205 |
boolean[] result = new boolean[entryCount]; |
| 206 |
|
| 207 |
for (int i = 0; i < entryCount; i++) { |
| 208 |
result[i] = values.contains(entries[i].toString()); |
| 209 |
} |
| 210 |
|
| 211 |
return result; |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
protected void onDialogClosed(boolean positiveResult) { |
| 216 |
super.onDialogClosed(positiveResult); |
| 217 |
|
| 218 |
if (positiveResult && mPreferenceChanged) { |
| 219 |
final Set<String> values = mNewValues; |
| 220 |
if (callChangeListener(values)) { |
| 221 |
setValues(values); |
| 222 |
} |
| 223 |
} |
| 224 |
mPreferenceChanged = false; |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
protected Object onGetDefaultValue(TypedArray a, int index) { |
| 229 |
final CharSequence[] defaultValues = a.getTextArray(index); |
| 230 |
final int valueCount = defaultValues.length; |
| 231 |
final Set<String> result = new HashSet<String>(); |
| 232 |
|
| 233 |
for (int i = 0; i < valueCount; i++) { |
| 234 |
result.add(defaultValues[i].toString()); |
| 235 |
} |
| 236 |
|
| 237 |
return result; |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { |
| 242 |
setValues(restoreValue ? getPersistedStringSet(mValues) : (Set<String>) defaultValue); |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
protected Parcelable onSaveInstanceState() { |
| 247 |
final Parcelable superState = super.onSaveInstanceState(); |
| 248 |
if (isPersistent()) { |
| 249 |
|
| 250 |
return superState; |
| 251 |
} |
| 252 |
|
| 253 |
final SavedState myState = new SavedState(superState); |
| 254 |
myState.values = getValues(); |
| 255 |
return myState; |
| 256 |
} |
| 257 |
|
| 258 |
private static class SavedState extends BaseSavedState { |
| 259 |
Set<String> values; |
| 260 |
|
| 261 |
public SavedState(Parcel source) { |
| 262 |
super(source); |
| 263 |
values = new HashSet<String>(); |
| 264 |
String[] strings = source.readStringArray(); |
| 265 |
|
| 266 |
final int stringCount = strings.length; |
| 267 |
for (int i = 0; i < stringCount; i++) { |
| 268 |
values.add(strings[i]); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
public SavedState(Parcelable superState) { |
| 273 |
super(superState); |
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
public void writeToParcel(Parcel dest, int flags) { |
| 278 |
super.writeToParcel(dest, flags); |
| 279 |
dest.writeStringArray(values.toArray(new String[0])); |
| 280 |
} |
| 281 |
|
| 282 |
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR = |
| 283 |
new Parcelable.Creator<SavedState>() { |
| 284 |
public SavedState createFromParcel(Parcel in) { |
| 285 |
return new SavedState(in); |
| 286 |
} |
| 287 |
|
| 288 |
public SavedState[] newArray(int size) { |
| 289 |
return new SavedState[size]; |
| 290 |
} |
| 291 |
}; |
| 292 |
} |
| 293 |
} |