/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package android.support.v14.preference; import android.content.Context; import android.content.SharedPreferences; import android.content.res.TypedArray; import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.ArrayRes; import android.support.annotation.NonNull; import android.support.v4.content.SharedPreferencesCompat; import android.support.v4.content.res.TypedArrayUtils; import android.support.v7.preference.DialogPreference; import android.util.AttributeSet; import java.util.Collections; import java.util.HashSet; import java.util.Set; /** * A {@link android.support.v7.preference.Preference} that displays a list of entries as * a dialog. *
* This preference will store a set of strings into the SharedPreferences.
* This set will contain one or more values from the
* {@link #setEntryValues(CharSequence[])} array.
*
* @attr name android:entries
* @attr name android:entryValues
*/
public class MultiSelectListPreference extends DialogPreference {
private CharSequence[] mEntries;
private CharSequence[] mEntryValues;
private Set
* This will check if this Preference is persistent, get an editor from
* the {@link android.preference.PreferenceManager}, put in the strings, and check if we should
* commit (and commit if so).
*
* @param values The values to persist.
* @return True if the Preference is persistent. (This is not whether the
* value was persisted, since we may not necessarily commit if there
* will be a batch commit later.)
* @see #getPersistedString
*
* @hide
*/
protected boolean persistStringSet(Set
* This will check if this Preference is persistent, get the SharedPreferences
* from the {@link android.preference.PreferenceManager}, and get the value.
*
* @param defaultReturnValue The default value to return if either the
* Preference is not persistent or the Preference is not in the
* shared preferences.
* @return The value from the SharedPreferences or the default return
* value.
* @see #persistStringSet(Set)
*
* @hide
*/
protected Set
* Each entry must have a corresponding index in
* {@link #setEntryValues(CharSequence[])}.
*
* @param entries The entries.
* @see #setEntryValues(CharSequence[])
*/
public void setEntries(CharSequence[] entries) {
mEntries = entries;
}
/**
* @see #setEntries(CharSequence[])
* @param entriesResId The entries array as a resource.
*/
public void setEntries(@ArrayRes int entriesResId) {
setEntries(getContext().getResources().getTextArray(entriesResId));
}
/**
* The list of entries to be shown in the list in subsequent dialogs.
*
* @return The list as an array.
*/
public CharSequence[] getEntries() {
return mEntries;
}
/**
* The array to find the value to save for a preference when an entry from
* entries is selected. If a user clicks on the second item in entries, the
* second item in this array will be saved to the preference.
*
* @param entryValues The array to be used as values to save for the preference.
*/
public void setEntryValues(CharSequence[] entryValues) {
mEntryValues = entryValues;
}
/**
* @see #setEntryValues(CharSequence[])
* @param entryValuesResId The entry values array as a resource.
*/
public void setEntryValues(@ArrayRes int entryValuesResId) {
setEntryValues(getContext().getResources().getTextArray(entryValuesResId));
}
/**
* Returns the array of values to be saved for the preference.
*
* @return The array of values.
*/
public CharSequence[] getEntryValues() {
return mEntryValues;
}
/**
* Sets the value of the key. This should contain entries in
* {@link #getEntryValues()}.
*
* @param values The values to set for the key.
*/
public void setValues(Set