• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.support.v7.preference.ListPreference;
23 
24 public class ListPreferenceDialogFragment extends PreferenceDialogFragment {
25 
26     private int mClickedDialogEntryIndex;
27 
newInstance(String key)28     public static ListPreferenceDialogFragment newInstance(String key) {
29         final ListPreferenceDialogFragment fragment = new ListPreferenceDialogFragment();
30         final Bundle b = new Bundle(1);
31         b.putString(ARG_KEY, key);
32         fragment.setArguments(b);
33         return fragment;
34     }
35 
getListPreference()36     private ListPreference getListPreference() {
37         return (ListPreference) getPreference();
38     }
39 
40     @Override
onPrepareDialogBuilder(AlertDialog.Builder builder)41     protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
42         super.onPrepareDialogBuilder(builder);
43 
44         final ListPreference preference = getListPreference();
45 
46         if (preference.getEntries() == null || preference.getEntryValues() == null) {
47             throw new IllegalStateException(
48                     "ListPreference requires an entries array and an entryValues array.");
49         }
50 
51         mClickedDialogEntryIndex = preference.findIndexOfValue(preference.getValue());
52         builder.setSingleChoiceItems(preference.getEntries(), mClickedDialogEntryIndex,
53                 new DialogInterface.OnClickListener() {
54                     public void onClick(DialogInterface dialog, int which) {
55                         mClickedDialogEntryIndex = which;
56 
57                         /*
58                          * Clicking on an item simulates the positive button
59                          * click, and dismisses the dialog.
60                          */
61                         ListPreferenceDialogFragment.this.onClick(dialog,
62                                 DialogInterface.BUTTON_POSITIVE);
63                         dialog.dismiss();
64                     }
65                 });
66 
67         /*
68          * The typical interaction for list-based dialogs is to have
69          * click-on-an-item dismiss the dialog instead of the user having to
70          * press 'Ok'.
71          */
72         builder.setPositiveButton(null, null);
73     }
74 
75     @Override
onDialogClosed(boolean positiveResult)76     public void onDialogClosed(boolean positiveResult) {
77         final ListPreference preference = getListPreference();
78         if (positiveResult && mClickedDialogEntryIndex >= 0 &&
79                 preference.getEntryValues() != null) {
80             String value = preference.getEntryValues()[mClickedDialogEntryIndex].toString();
81             if (preference.callChangeListener(value)) {
82                 preference.setValue(value);
83             }
84         }
85     }
86 
87 }
88