1 /*
2  * Copyright (C) 2013 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 com.android.settings;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.os.Bundle;
24 import android.support.v14.preference.ListPreferenceDialogFragment;
25 import android.support.v7.preference.ListPreference;
26 import android.util.AttributeSet;
27 
28 public class CustomListPreference extends ListPreference {
29 
CustomListPreference(Context context, AttributeSet attrs)30     public CustomListPreference(Context context, AttributeSet attrs) {
31         super(context, attrs);
32     }
33 
CustomListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)34     public CustomListPreference(Context context, AttributeSet attrs, int defStyleAttr,
35                                 int defStyleRes) {
36         super(context, attrs, defStyleAttr, defStyleRes);
37     }
38 
onPrepareDialogBuilder(AlertDialog.Builder builder, DialogInterface.OnClickListener listener)39     protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
40             DialogInterface.OnClickListener listener) {
41     }
42 
onDialogClosed(boolean positiveResult)43     protected void onDialogClosed(boolean positiveResult) {
44     }
45 
onDialogCreated(Dialog dialog)46     protected void onDialogCreated(Dialog dialog) {
47     }
48 
isAutoClosePreference()49     protected boolean isAutoClosePreference() {
50         return true;
51     }
52 
onDialogStateRestored(Dialog dialog, Bundle savedInstanceState)53     protected void onDialogStateRestored(Dialog dialog, Bundle savedInstanceState) {
54     }
55 
56     public static class CustomListPreferenceDialogFragment extends ListPreferenceDialogFragment {
57 
58         private static final java.lang.String KEY_CLICKED_ENTRY_INDEX
59                 = "settings.CustomListPrefDialog.KEY_CLICKED_ENTRY_INDEX";
60 
61         private int mClickedDialogEntryIndex;
62 
newInstance(String key)63         public static ListPreferenceDialogFragment newInstance(String key) {
64             final ListPreferenceDialogFragment fragment = new CustomListPreferenceDialogFragment();
65             final Bundle b = new Bundle(1);
66             b.putString(ARG_KEY, key);
67             fragment.setArguments(b);
68             return fragment;
69         }
70 
getCustomizablePreference()71         private CustomListPreference getCustomizablePreference() {
72             return (CustomListPreference) getPreference();
73         }
74 
75         @Override
onPrepareDialogBuilder(AlertDialog.Builder builder)76         protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
77             super.onPrepareDialogBuilder(builder);
78             mClickedDialogEntryIndex = getCustomizablePreference()
79                     .findIndexOfValue(getCustomizablePreference().getValue());
80             getCustomizablePreference().onPrepareDialogBuilder(builder, getOnItemClickListener());
81             if (!getCustomizablePreference().isAutoClosePreference()) {
82                 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
83                     @Override
84                     public void onClick(DialogInterface dialog, int which) {
85                         CustomListPreferenceDialogFragment.this.onClick(dialog,
86                                 DialogInterface.BUTTON_POSITIVE);
87                         dialog.dismiss();
88                     }
89                 });
90             }
91         }
92 
93         @Override
onCreateDialog(Bundle savedInstanceState)94         public Dialog onCreateDialog(Bundle savedInstanceState) {
95             Dialog dialog = super.onCreateDialog(savedInstanceState);
96             if (savedInstanceState != null) {
97                 mClickedDialogEntryIndex = savedInstanceState.getInt(KEY_CLICKED_ENTRY_INDEX,
98                         mClickedDialogEntryIndex);
99             }
100             getCustomizablePreference().onDialogCreated(dialog);
101             return dialog;
102         }
103 
104         @Override
onSaveInstanceState(Bundle outState)105         public void onSaveInstanceState(Bundle outState) {
106             super.onSaveInstanceState(outState);
107             outState.putInt(KEY_CLICKED_ENTRY_INDEX, mClickedDialogEntryIndex);
108         }
109 
110         @Override
onActivityCreated(Bundle savedInstanceState)111         public void onActivityCreated(Bundle savedInstanceState) {
112             super.onActivityCreated(savedInstanceState);
113             getCustomizablePreference().onDialogStateRestored(getDialog(), savedInstanceState);
114         }
115 
getOnItemClickListener()116         protected DialogInterface.OnClickListener getOnItemClickListener() {
117             return new DialogInterface.OnClickListener() {
118                 public void onClick(DialogInterface dialog, int which) {
119                     setClickedDialogEntryIndex(which);
120 
121 
122                     if (getCustomizablePreference().isAutoClosePreference()) {
123                         /*
124                          * Clicking on an item simulates the positive button
125                          * click, and dismisses the dialog.
126                          */
127                         CustomListPreferenceDialogFragment.this.onClick(dialog,
128                                 DialogInterface.BUTTON_POSITIVE);
129                         dialog.dismiss();
130                     }
131                 }
132             };
133         }
134 
setClickedDialogEntryIndex(int which)135         protected void setClickedDialogEntryIndex(int which) {
136             mClickedDialogEntryIndex = which;
137         }
138 
139         @Override
onDialogClosed(boolean positiveResult)140         public void onDialogClosed(boolean positiveResult) {
141             getCustomizablePreference().onDialogClosed(positiveResult);
142             final ListPreference preference = getCustomizablePreference();
143             if (positiveResult && mClickedDialogEntryIndex >= 0 &&
144                     preference.getEntryValues() != null) {
145                 String value = preference.getEntryValues()[mClickedDialogEntryIndex].toString();
146                 if (preference.callChangeListener(value)) {
147                     preference.setValue(value);
148                 }
149             }
150         }
151     }
152 }
153