1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package android.support.v14.preference;
18 
19 import android.app.AlertDialog;
20 import android.content.DialogInterface;
21 import android.os.Bundle;
22 
23 import java.util.HashSet;
24 import java.util.Set;
25 
26 public class MultiSelectListPreferenceDialogFragment extends PreferenceDialogFragment {
27 
28     private Set<String> mNewValues = new HashSet<>();
29     private boolean mPreferenceChanged;
30 
newInstance(String key)31     public static MultiSelectListPreferenceDialogFragment newInstance(String key) {
32         final MultiSelectListPreferenceDialogFragment fragment =
33                 new MultiSelectListPreferenceDialogFragment();
34         final Bundle b = new Bundle(1);
35         b.putString(ARG_KEY, key);
36         fragment.setArguments(b);
37         return fragment;
38     }
39 
getListPreference()40     private MultiSelectListPreference getListPreference() {
41         return (MultiSelectListPreference) getPreference();
42     }
43 
44     @Override
onPrepareDialogBuilder(AlertDialog.Builder builder)45     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
46         super.onPrepareDialogBuilder(builder);
47 
48         final MultiSelectListPreference preference = getListPreference();
49 
50         if (preference.getEntries() == null || preference.getEntryValues() == null) {
51             throw new IllegalStateException(
52                     "MultiSelectListPreference requires an entries array and " +
53                             "an entryValues array.");
54         }
55 
56         boolean[] checkedItems = preference.getSelectedItems();
57         builder.setMultiChoiceItems(preference.getEntries(), checkedItems,
58                 new DialogInterface.OnMultiChoiceClickListener() {
59                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
60                         if (isChecked) {
61                             mPreferenceChanged |= mNewValues.add(
62                                     preference.getEntryValues()[which].toString());
63                         } else {
64                             mPreferenceChanged |= mNewValues.remove(
65                                     preference.getEntryValues()[which].toString());
66                         }
67                     }
68                 });
69         mNewValues.clear();
70         mNewValues.addAll(preference.getValues());
71     }
72 
73     @Override
onDialogClosed(boolean positiveResult)74     public void onDialogClosed(boolean positiveResult) {
75         final MultiSelectListPreference preference = getListPreference();
76         if (positiveResult && mPreferenceChanged) {
77             final Set<String> values = mNewValues;
78             if (preference.callChangeListener(values)) {
79                 preference.setValues(values);
80             }
81         }
82         mPreferenceChanged = false;
83     }
84 
85 }
86