summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/preferences/MultiChoicePreference.java
blob: 5749bf29dfe8bc87baf1994dcb5f03665d601d44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* -*- 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.
     * <p>
     * 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<String> getValues() {
        final Set<String> values = new HashSet<String>();

        if (mValues == null) {
            return values;
        }

        for (int i = 0; i < mValues.length; i++) {
            if (mValues[i]) {
                values.add(mEntryValues[i].toString());
            }
        }

        return values;
    }

    @Override
    public void onClick(DialogInterface dialog, int which, boolean val) {
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        if (mEntries == null || mInitialValues == null || mEntryValues == null) {
            throw new IllegalStateException(
                    "MultiChoicePreference requires entries, entryValues, and initialValues arrays.");
        }

        if (mEntries.length != mEntryValues.length || mEntries.length != mInitialValues.length) {
            throw new IllegalStateException(
                    "MultiChoicePreference entries, entryValues, and initialValues arrays must be the same length");
        }

        builder.setMultiChoiceItems(mEntries, mValues, this);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (mPrevValues == null || mInitialValues == null) {
            // Initialization is done asynchronously, so these values may not
            // have been set before the dialog was closed.
            return;
        }

        if (!positiveResult) {
            // user cancelled; reset checkbox values to their previous state
            mValues = mPrevValues.clone();
            return;
        }

        mPrevValues = mValues.clone();

        if (!callChangeListener(getValues())) {
            return;
        }

        persist();
    }

    /* Persists the current data stored by this pref to SharedPreferences. */
    public boolean persist() {
        if (isPersistent()) {
            final SharedPreferences.Editor edit = GeckoSharedPrefs.forProfile(getContext()).edit();
            final boolean res = persist(edit);
            edit.apply();
            return res;
        }

        return false;
    }

    /* Internal persist method. Take an edit so that multiple prefs can be persisted in a single commit. */
    protected boolean persist(SharedPreferences.Editor edit) {
        if (isPersistent()) {
            Set<String> vals = getValues();
            PrefUtils.putStringSet(edit, getKey(), vals).apply();;
            return true;
        }

        return false;
    }

    /* Returns a list of EntryValues that are currently enabled. */
    public Set<String> getPersistedStrings(Set<String> defaultVal) {
        if (!isPersistent()) {
            return defaultVal;
        }

        final SharedPreferences prefs = GeckoSharedPrefs.forProfile(getContext());
        return PrefUtils.getStringSet(prefs, getKey(), defaultVal);
    }

    /**
     * Loads persistent prefs from shared preferences. If the preferences
     * aren't persistent or haven't yet been stored, they will be set to their
     * initial values.
     */
    protected void loadPersistedValues() {
        final int entryCount = mInitialValues.length;
        mValues = new boolean[entryCount];

        if (entryCount != mEntries.length || entryCount != mEntryValues.length) {
            throw new IllegalStateException(
                    "MultiChoicePreference entryValues and initialValues arrays must be the same length");
        }

        ThreadUtils.postToBackgroundThread(new Runnable() {
            @Override
            public void run() {
                final Set<String> stringVals = getPersistedStrings(null);

                for (int i = 0; i < entryCount; i++) {
                    if (stringVals != null) {
                        mValues[i] = stringVals.contains(mEntryValues[i]);
                    } else {
                        final boolean defaultVal = mInitialValues[i].equals("true");
                        mValues[i] = defaultVal;
                    }
                }

                mPrevValues = mValues.clone();
            }
        });
    }
}