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