/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mozilla.gecko.preferences; import org.mozilla.gecko.R; import org.mozilla.gecko.GeckoSharedPrefs; import org.mozilla.gecko.util.PrefUtils; import org.mozilla.gecko.util.ThreadUtils; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.res.TypedArray; import android.content.SharedPreferences; import android.preference.DialogPreference; import android.util.AttributeSet; import java.util.HashSet; import java.util.Set; class MultiChoicePreference extends DialogPreference implements DialogInterface.OnMultiChoiceClickListener { private static final String LOGTAG = "GeckoMultiChoicePreference"; private boolean mValues[]; private boolean mPrevValues[]; private CharSequence mEntryValues[]; private CharSequence mEntries[]; private CharSequence mInitialValues[]; public MultiChoicePreference(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultiChoicePreference); mEntries = a.getTextArray(R.styleable.MultiChoicePreference_entries); mEntryValues = a.getTextArray(R.styleable.MultiChoicePreference_entryValues); mInitialValues = a.getTextArray(R.styleable.MultiChoicePreference_initialValues); a.recycle(); loadPersistedValues(); } public MultiChoicePreference(Context context) { this(context, null); } /** * Sets the human-readable entries to be shown in the list. This will be * shown in subsequent dialogs. *
* Each entry must have a corresponding index in
* {@link #setEntryValues(CharSequence[])} and
* {@link #setInitialValues(CharSequence[])}.
*
* @param entries The entries.
*/
public void setEntries(CharSequence[] entries) {
mEntries = entries.clone();
}
/**
* @param entriesResId The entries array as a resource.
*/
public void setEntries(int entriesResId) {
setEntries(getContext().getResources().getTextArray(entriesResId));
}
/**
* Sets the preference values for preferences shown in the list.
*
* @param entryValues The entry values.
*/
public void setEntryValues(CharSequence[] entryValues) {
mEntryValues = entryValues.clone();
loadPersistedValues();
}
/**
* Entry values define a separate pref for each row in the dialog.
*
* @param entryValuesResId The entryValues array as a resource.
*/
public void setEntryValues(int entryValuesResId) {
setEntryValues(getContext().getResources().getTextArray(entryValuesResId));
}
/**
* The array of initial entry values in this list. Each entryValue
* corresponds to an entryKey. These values are used if a) the preference
* isn't persisted, or b) the preference is persisted but hasn't yet been
* set.
*
* @param initialValues The entry values
*/
public void setInitialValues(CharSequence[] initialValues) {
mInitialValues = initialValues.clone();
loadPersistedValues();
}
/**
* @param initialValuesResId The initialValues array as a resource.
*/
public void setInitialValues(int initialValuesResId) {
setInitialValues(getContext().getResources().getTextArray(initialValuesResId));
}
/**
* The list of translated strings corresponding to each preference.
*
* @return The array of entries.
*/
public CharSequence[] getEntries() {
return mEntries.clone();
}
/**
* The list of values corresponding to each preference.
*
* @return The array of values.
*/
public CharSequence[] getEntryValues() {
return mEntryValues.clone();
}
/**
* The list of initial values for each preference. Each string in this list
* should be either "true" or "false".
*
* @return The array of initial values.
*/
public CharSequence[] getInitialValues() {
return mInitialValues.clone();
}
public void setValue(final int i, final boolean value) {
mValues[i] = value;
mPrevValues = mValues.clone();
}
/**
* The list of values for each preference. These values are updated after
* the dialog has been displayed.
*
* @return The array of values.
*/
public Set