Debian Codegraph @

core/java/android/preference/MultiSelectListPreference.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * A {@link Preference} that displays a list of entries as
33 * a dialog.
34 * <p>
35 * This preference will store a set of strings into the SharedPreferences.
36 * This set will contain one or more values from the
37 * {@link #setEntryValues(CharSequence[])} array.
38 *
39 * @attr ref android.R.styleable#MultiSelectListPreference_entries
40 * @attr ref android.R.styleable#MultiSelectListPreference_entryValues
41 *
42 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
43 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
44 * Preference Library</a> for consistent behavior across all devices. For more information on
45 * using the AndroidX Preference Library see
46 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
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 * Sets the human-readable entries to be shown in the list. This will be
82 * shown in subsequent dialogs.
83 * <p>
84 * Each entry must have a corresponding index in
85 * {@link #setEntryValues(CharSequence[])}.
86 *
87 * @param entries The entries.
88 * @see #setEntryValues(CharSequence[])
89 */
90 public void setEntries(CharSequence[] entries) {
91 mEntries = entries;
92 }
93
94 /**
95 * @see #setEntries(CharSequence[])
96 * @param entriesResId The entries array as a resource.
97 */
98 public void setEntries(@ArrayRes int entriesResId) {
99 setEntries(getContext().getResources().getTextArray(entriesResId));
100 }
101
102 /**
103 * The list of entries to be shown in the list in subsequent dialogs.
104 *
105 * @return The list as an array.
106 */
107 public CharSequence[] getEntries() {
108 return mEntries;
109 }
110
111 /**
112 * The array to find the value to save for a preference when an entry from
113 * entries is selected. If a user clicks on the second item in entries, the
114 * second item in this array will be saved to the preference.
115 *
116 * @param entryValues The array to be used as values to save for the preference.
117 */
118 public void setEntryValues(CharSequence[] entryValues) {
119 mEntryValues = entryValues;
120 }
121
122 /**
123 * @see #setEntryValues(CharSequence[])
124 * @param entryValuesResId The entry values array as a resource.
125 */
126 public void setEntryValues(@ArrayRes int entryValuesResId) {
127 setEntryValues(getContext().getResources().getTextArray(entryValuesResId));
128 }
129
130 /**
131 * Returns the array of values to be saved for the preference.
132 *
133 * @return The array of values.
134 */
135 public CharSequence[] getEntryValues() {
136 return mEntryValues;
137 }
138
139 /**
140 * Sets the value of the key. This should contain entries in
141 * {@link #getEntryValues()}.
142 *
143 * @param values The values to set for the key.
144 */
145 public void setValues(Set<String> values) {
146 mValues.clear();
147 mValues.addAll(values);
148
149 persistStringSet(values);
150 }
151
152 /**
153 * Retrieves the current value of the key.
154 */
155 public Set<String> getValues() {
156 return mValues;
157 }
158
159 /**
160 * Returns the index of the given value (in the entry values array).
161 *
162 * @param value The value whose index should be returned.
163 * @return The index of the value, or -1 if not found.
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 // No need to save instance state
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 }