1212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell/*
2212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * Copyright (C) 2010 The Android Open Source Project
3212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell *
4212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * you may not use this file except in compliance with the License.
6212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * You may obtain a copy of the License at
7212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell *
8212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell *
10212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * Unless required by applicable law or agreed to in writing, software
11212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * See the License for the specific language governing permissions and
14212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * limitations under the License.
15212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell */
16212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
17212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellpackage android.preference;
18212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
197b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbyeimport android.annotation.ArrayRes;
20212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellimport android.app.AlertDialog.Builder;
21212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellimport android.content.Context;
22212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellimport android.content.DialogInterface;
23212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellimport android.content.res.TypedArray;
24212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellimport android.os.Parcel;
25212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellimport android.os.Parcelable;
26212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellimport android.util.AttributeSet;
27212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
28212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellimport java.util.HashSet;
29212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellimport java.util.Set;
30212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
31212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell/**
32212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * A {@link Preference} that displays a list of entries as
33212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * a dialog.
34212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * <p>
35212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * This preference will store a set of strings into the SharedPreferences.
36212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * This set will contain one or more values from the
37212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * {@link #setEntryValues(CharSequence[])} array.
38212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell *
39212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * @attr ref android.R.styleable#MultiSelectListPreference_entries
40212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell * @attr ref android.R.styleable#MultiSelectListPreference_entryValues
41212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell */
42212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powellpublic class MultiSelectListPreference extends DialogPreference {
43212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    private CharSequence[] mEntries;
44212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    private CharSequence[] mEntryValues;
45212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    private Set<String> mValues = new HashSet<String>();
46212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    private Set<String> mNewValues = new HashSet<String>();
47212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    private boolean mPreferenceChanged;
48617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette
49617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    public MultiSelectListPreference(
50617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette            Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
51617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette        super(context, attrs, defStyleAttr, defStyleRes);
52617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette
53617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette        final TypedArray a = context.obtainStyledAttributes(attrs,
54617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette                com.android.internal.R.styleable.MultiSelectListPreference, defStyleAttr,
55617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette                defStyleRes);
56212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        mEntries = a.getTextArray(com.android.internal.R.styleable.MultiSelectListPreference_entries);
57212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        mEntryValues = a.getTextArray(com.android.internal.R.styleable.MultiSelectListPreference_entryValues);
58212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        a.recycle();
59212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
60617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette
61617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    public MultiSelectListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
62617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette        this(context, attrs, defStyleAttr, 0);
63617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    }
64617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette
65617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    public MultiSelectListPreference(Context context, AttributeSet attrs) {
66599d2a49e84bf0a4ee752e263a2c29d2ae942c3eAlan Viverette        this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle);
67617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette    }
68212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
69212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    public MultiSelectListPreference(Context context) {
70212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        this(context, null);
71212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
72212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
73212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    /**
74212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * Sets the human-readable entries to be shown in the list. This will be
75212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * shown in subsequent dialogs.
76212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * <p>
77212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * Each entry must have a corresponding index in
78212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * {@link #setEntryValues(CharSequence[])}.
79212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     *
80212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @param entries The entries.
81212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @see #setEntryValues(CharSequence[])
82212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     */
83212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    public void setEntries(CharSequence[] entries) {
84212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        mEntries = entries;
85212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
86212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
87212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    /**
88212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @see #setEntries(CharSequence[])
89212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @param entriesResId The entries array as a resource.
90212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     */
917b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbye    public void setEntries(@ArrayRes int entriesResId) {
92212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        setEntries(getContext().getResources().getTextArray(entriesResId));
93212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
94212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
95212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    /**
96212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * The list of entries to be shown in the list in subsequent dialogs.
97212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     *
98212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @return The list as an array.
99212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     */
100212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    public CharSequence[] getEntries() {
101212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        return mEntries;
102212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
103212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
104212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    /**
105212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * The array to find the value to save for a preference when an entry from
106212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * entries is selected. If a user clicks on the second item in entries, the
107212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * second item in this array will be saved to the preference.
108212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     *
109212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @param entryValues The array to be used as values to save for the preference.
110212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     */
111212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    public void setEntryValues(CharSequence[] entryValues) {
112212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        mEntryValues = entryValues;
113212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
114212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
115212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    /**
116212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @see #setEntryValues(CharSequence[])
117212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @param entryValuesResId The entry values array as a resource.
118212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     */
1197b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbye    public void setEntryValues(@ArrayRes int entryValuesResId) {
120212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        setEntryValues(getContext().getResources().getTextArray(entryValuesResId));
121212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
122212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
123212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    /**
124212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * Returns the array of values to be saved for the preference.
125212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     *
126212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @return The array of values.
127212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     */
128212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    public CharSequence[] getEntryValues() {
129212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        return mEntryValues;
130212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
131212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
132212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    /**
133212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * Sets the value of the key. This should contain entries in
134212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * {@link #getEntryValues()}.
135212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     *
136212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @param values The values to set for the key.
137212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     */
138212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    public void setValues(Set<String> values) {
139cd9ea08d9cb68004b2d5f69302cddf53dc034e7bAmith Yamasani        mValues.clear();
140cd9ea08d9cb68004b2d5f69302cddf53dc034e7bAmith Yamasani        mValues.addAll(values);
141cd9ea08d9cb68004b2d5f69302cddf53dc034e7bAmith Yamasani
142212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        persistStringSet(values);
143212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
144212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
145212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    /**
146212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * Retrieves the current value of the key.
147212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     */
148212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    public Set<String> getValues() {
149212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        return mValues;
150212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
151212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
152212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    /**
153212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * Returns the index of the given value (in the entry values array).
154212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     *
155212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @param value The value whose index should be returned.
156212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     * @return The index of the value, or -1 if not found.
157212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell     */
158212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    public int findIndexOfValue(String value) {
159212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        if (value != null && mEntryValues != null) {
160212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            for (int i = mEntryValues.length - 1; i >= 0; i--) {
161212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                if (mEntryValues[i].equals(value)) {
162212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                    return i;
163212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                }
164212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            }
165212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        }
166212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        return -1;
167212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
168212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
169212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    @Override
170212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    protected void onPrepareDialogBuilder(Builder builder) {
171212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        super.onPrepareDialogBuilder(builder);
172212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
173212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        if (mEntries == null || mEntryValues == null) {
174212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            throw new IllegalStateException(
175212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                    "MultiSelectListPreference requires an entries array and " +
176212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                    "an entryValues array.");
177212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        }
178212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
179212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        boolean[] checkedItems = getSelectedItems();
180212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        builder.setMultiChoiceItems(mEntries, checkedItems,
181212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                new DialogInterface.OnMultiChoiceClickListener() {
182212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
183212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                        if (isChecked) {
184ece60fb243f6d1dcee24f5c8f925f449e0514f7eJustin Koh                            mPreferenceChanged |= mNewValues.add(mEntryValues[which].toString());
185212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                        } else {
186ece60fb243f6d1dcee24f5c8f925f449e0514f7eJustin Koh                            mPreferenceChanged |= mNewValues.remove(mEntryValues[which].toString());
187212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                        }
188212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                    }
189212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                });
190212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        mNewValues.clear();
191212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        mNewValues.addAll(mValues);
192212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
193212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
194212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    private boolean[] getSelectedItems() {
195ece60fb243f6d1dcee24f5c8f925f449e0514f7eJustin Koh        final CharSequence[] entries = mEntryValues;
196212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        final int entryCount = entries.length;
197212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        final Set<String> values = mValues;
198212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        boolean[] result = new boolean[entryCount];
199212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
200212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        for (int i = 0; i < entryCount; i++) {
201212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            result[i] = values.contains(entries[i].toString());
202212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        }
203212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
204212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        return result;
205212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
206212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
207212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    @Override
208212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    protected void onDialogClosed(boolean positiveResult) {
209212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        super.onDialogClosed(positiveResult);
210212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
211212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        if (positiveResult && mPreferenceChanged) {
212212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            final Set<String> values = mNewValues;
213212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            if (callChangeListener(values)) {
214212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                setValues(values);
215212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            }
216212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        }
217212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        mPreferenceChanged = false;
218212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
219212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
220212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    @Override
221212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    protected Object onGetDefaultValue(TypedArray a, int index) {
222212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        final CharSequence[] defaultValues = a.getTextArray(index);
223212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        final int valueCount = defaultValues.length;
224212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        final Set<String> result = new HashSet<String>();
225212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
226212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        for (int i = 0; i < valueCount; i++) {
227212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            result.add(defaultValues[i].toString());
228212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        }
229212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
230212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        return result;
231212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
232212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
233212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    @Override
234212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
235212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        setValues(restoreValue ? getPersistedStringSet(mValues) : (Set<String>) defaultValue);
236212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
237212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
238212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    @Override
239212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    protected Parcelable onSaveInstanceState() {
240212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        final Parcelable superState = super.onSaveInstanceState();
241212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        if (isPersistent()) {
242212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            // No need to save instance state
243212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            return superState;
244212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        }
245212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
246212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        final SavedState myState = new SavedState(superState);
247212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        myState.values = getValues();
248212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        return myState;
249212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
250212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
251212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    private static class SavedState extends BaseSavedState {
252212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        Set<String> values;
253212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
254212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        public SavedState(Parcel source) {
255212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            super(source);
256212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            values = new HashSet<String>();
257212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            String[] strings = source.readStringArray();
258212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
259212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            final int stringCount = strings.length;
260212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            for (int i = 0; i < stringCount; i++) {
261212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                values.add(strings[i]);
262212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            }
263212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        }
264212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
265212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        public SavedState(Parcelable superState) {
266212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            super(superState);
267212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        }
268212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
269212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        @Override
270212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        public void writeToParcel(Parcel dest, int flags) {
271212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            super.writeToParcel(dest, flags);
272212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            dest.writeStringArray(values.toArray(new String[0]));
273212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        }
274212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
275212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        public static final Parcelable.Creator<SavedState> CREATOR =
276212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                new Parcelable.Creator<SavedState>() {
277212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            public SavedState createFromParcel(Parcel in) {
278212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                return new SavedState(in);
279212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            }
280212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell
281212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            public SavedState[] newArray(int size) {
282212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell                return new SavedState[size];
283212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell            }
284212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell        };
285212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell    }
286212db7d3f8ce5297f4a556234a9c0675c697f1cfAdam Powell}
287