Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 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; |
Adrian Roos | d553f4f | 2016-02-22 13:03:42 -0800 | [diff] [blame] | 20 | import android.app.Dialog; |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 21 | import android.app.Fragment; |
| 22 | import android.app.FragmentTransaction; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.DialogInterface; |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 25 | import android.content.DialogInterface.OnClickListener; |
| 26 | import android.content.Intent; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 27 | import android.os.Bundle; |
| 28 | import android.support.v14.preference.ListPreferenceDialogFragment; |
| 29 | import android.support.v7.preference.ListPreference; |
| 30 | import android.util.AttributeSet; |
| 31 | |
Tamas Berghammer | 265d3c2 | 2016-06-22 15:34:45 +0100 | [diff] [blame] | 32 | import com.android.internal.logging.nano.MetricsProto; |
Fan Zhang | ac5e593 | 2016-08-24 18:13:33 -0700 | [diff] [blame] | 33 | import com.android.settings.core.instrumentation.InstrumentedDialogFragment; |
| 34 | |
Julia Reynolds | 2e1e0b7 | 2015-12-14 08:32:46 -0500 | [diff] [blame] | 35 | public class CustomListPreference extends ListPreference { |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 36 | |
| 37 | public CustomListPreference(Context context, AttributeSet attrs) { |
| 38 | super(context, attrs); |
| 39 | } |
| 40 | |
| 41 | public CustomListPreference(Context context, AttributeSet attrs, int defStyleAttr, |
| 42 | int defStyleRes) { |
| 43 | super(context, attrs, defStyleAttr, defStyleRes); |
| 44 | } |
| 45 | |
| 46 | protected void onPrepareDialogBuilder(AlertDialog.Builder builder, |
| 47 | DialogInterface.OnClickListener listener) { |
| 48 | } |
| 49 | |
| 50 | protected void onDialogClosed(boolean positiveResult) { |
| 51 | } |
| 52 | |
Adrian Roos | d553f4f | 2016-02-22 13:03:42 -0800 | [diff] [blame] | 53 | protected void onDialogCreated(Dialog dialog) { |
| 54 | } |
| 55 | |
| 56 | protected boolean isAutoClosePreference() { |
| 57 | return true; |
| 58 | } |
| 59 | |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 60 | /** |
| 61 | * Called when a user is about to choose the given value, to determine if we |
| 62 | * should show a confirmation dialog. |
| 63 | * |
| 64 | * @param value the value the user is about to choose |
| 65 | * @return the message to show in a confirmation dialog, or {@code null} to |
| 66 | * not request confirmation |
| 67 | */ |
| 68 | protected CharSequence getConfirmationMessage(String value) { |
| 69 | return null; |
| 70 | } |
| 71 | |
Adrian Roos | 4bd4207 | 2016-04-29 14:37:02 -0700 | [diff] [blame] | 72 | protected void onDialogStateRestored(Dialog dialog, Bundle savedInstanceState) { |
| 73 | } |
| 74 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 75 | public static class CustomListPreferenceDialogFragment extends ListPreferenceDialogFragment { |
| 76 | |
Adrian Roos | 4bd4207 | 2016-04-29 14:37:02 -0700 | [diff] [blame] | 77 | private static final java.lang.String KEY_CLICKED_ENTRY_INDEX |
| 78 | = "settings.CustomListPrefDialog.KEY_CLICKED_ENTRY_INDEX"; |
| 79 | |
Jason Monk | 9cbf325 | 2015-12-14 16:39:45 -0500 | [diff] [blame] | 80 | private int mClickedDialogEntryIndex; |
| 81 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 82 | public static ListPreferenceDialogFragment newInstance(String key) { |
| 83 | final ListPreferenceDialogFragment fragment = new CustomListPreferenceDialogFragment(); |
| 84 | final Bundle b = new Bundle(1); |
| 85 | b.putString(ARG_KEY, key); |
| 86 | fragment.setArguments(b); |
| 87 | return fragment; |
| 88 | } |
| 89 | |
| 90 | private CustomListPreference getCustomizablePreference() { |
| 91 | return (CustomListPreference) getPreference(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { |
| 96 | super.onPrepareDialogBuilder(builder); |
Jason Monk | 9cbf325 | 2015-12-14 16:39:45 -0500 | [diff] [blame] | 97 | mClickedDialogEntryIndex = getCustomizablePreference() |
| 98 | .findIndexOfValue(getCustomizablePreference().getValue()); |
Sudheer Shanka | 550d068 | 2016-01-13 15:16:55 +0000 | [diff] [blame] | 99 | getCustomizablePreference().onPrepareDialogBuilder(builder, getOnItemClickListener()); |
Adrian Roos | d553f4f | 2016-02-22 13:03:42 -0800 | [diff] [blame] | 100 | if (!getCustomizablePreference().isAutoClosePreference()) { |
| 101 | builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() { |
| 102 | @Override |
| 103 | public void onClick(DialogInterface dialog, int which) { |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 104 | onItemChosen(); |
Adrian Roos | d553f4f | 2016-02-22 13:03:42 -0800 | [diff] [blame] | 105 | } |
| 106 | }); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 112 | Dialog dialog = super.onCreateDialog(savedInstanceState); |
Adrian Roos | 4bd4207 | 2016-04-29 14:37:02 -0700 | [diff] [blame] | 113 | if (savedInstanceState != null) { |
| 114 | mClickedDialogEntryIndex = savedInstanceState.getInt(KEY_CLICKED_ENTRY_INDEX, |
| 115 | mClickedDialogEntryIndex); |
| 116 | } |
Adrian Roos | d553f4f | 2016-02-22 13:03:42 -0800 | [diff] [blame] | 117 | getCustomizablePreference().onDialogCreated(dialog); |
| 118 | return dialog; |
Sudheer Shanka | 550d068 | 2016-01-13 15:16:55 +0000 | [diff] [blame] | 119 | } |
Jason Monk | 9cbf325 | 2015-12-14 16:39:45 -0500 | [diff] [blame] | 120 | |
Adrian Roos | 4bd4207 | 2016-04-29 14:37:02 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public void onSaveInstanceState(Bundle outState) { |
| 123 | super.onSaveInstanceState(outState); |
| 124 | outState.putInt(KEY_CLICKED_ENTRY_INDEX, mClickedDialogEntryIndex); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onActivityCreated(Bundle savedInstanceState) { |
| 129 | super.onActivityCreated(savedInstanceState); |
| 130 | getCustomizablePreference().onDialogStateRestored(getDialog(), savedInstanceState); |
| 131 | } |
| 132 | |
Sudheer Shanka | 550d068 | 2016-01-13 15:16:55 +0000 | [diff] [blame] | 133 | protected DialogInterface.OnClickListener getOnItemClickListener() { |
| 134 | return new DialogInterface.OnClickListener() { |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 135 | @Override |
Sudheer Shanka | 550d068 | 2016-01-13 15:16:55 +0000 | [diff] [blame] | 136 | public void onClick(DialogInterface dialog, int which) { |
| 137 | setClickedDialogEntryIndex(which); |
Adrian Roos | d553f4f | 2016-02-22 13:03:42 -0800 | [diff] [blame] | 138 | if (getCustomizablePreference().isAutoClosePreference()) { |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 139 | onItemChosen(); |
Adrian Roos | d553f4f | 2016-02-22 13:03:42 -0800 | [diff] [blame] | 140 | } |
Sudheer Shanka | 550d068 | 2016-01-13 15:16:55 +0000 | [diff] [blame] | 141 | } |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | protected void setClickedDialogEntryIndex(int which) { |
| 146 | mClickedDialogEntryIndex = which; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 149 | private String getValue() { |
| 150 | final ListPreference preference = getCustomizablePreference(); |
| 151 | if (mClickedDialogEntryIndex >= 0 && preference.getEntryValues() != null) { |
| 152 | return preference.getEntryValues()[mClickedDialogEntryIndex].toString(); |
| 153 | } else { |
| 154 | return null; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Called when user has made a concrete item choice, but we might need |
| 160 | * to make a quick detour to confirm that choice with a second dialog. |
| 161 | */ |
| 162 | protected void onItemChosen() { |
| 163 | final CharSequence message = getCustomizablePreference() |
| 164 | .getConfirmationMessage(getValue()); |
| 165 | if (message != null) { |
| 166 | final Fragment f = new ConfirmDialogFragment(); |
| 167 | final Bundle args = new Bundle(); |
| 168 | args.putCharSequence(Intent.EXTRA_TEXT, message); |
| 169 | f.setArguments(args); |
| 170 | f.setTargetFragment(CustomListPreferenceDialogFragment.this, 0); |
| 171 | final FragmentTransaction ft = getFragmentManager().beginTransaction(); |
| 172 | ft.add(f, getTag() + "-Confirm"); |
| 173 | ft.commitAllowingStateLoss(); |
| 174 | } else { |
| 175 | onItemConfirmed(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Called when user has made a concrete item choice and we've fully |
| 181 | * confirmed they want to move forward (if we took a detour above). |
| 182 | */ |
| 183 | protected void onItemConfirmed() { |
| 184 | onClick(getDialog(), DialogInterface.BUTTON_POSITIVE); |
| 185 | getDialog().dismiss(); |
| 186 | } |
| 187 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 188 | @Override |
| 189 | public void onDialogClosed(boolean positiveResult) { |
| 190 | getCustomizablePreference().onDialogClosed(positiveResult); |
Jason Monk | 9cbf325 | 2015-12-14 16:39:45 -0500 | [diff] [blame] | 191 | final ListPreference preference = getCustomizablePreference(); |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 192 | final String value = getValue(); |
| 193 | if (positiveResult && value != null) { |
Jason Monk | 9cbf325 | 2015-12-14 16:39:45 -0500 | [diff] [blame] | 194 | if (preference.callChangeListener(value)) { |
| 195 | preference.setValue(value); |
| 196 | } |
| 197 | } |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 198 | } |
| 199 | } |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 200 | |
Fan Zhang | ac5e593 | 2016-08-24 18:13:33 -0700 | [diff] [blame] | 201 | public static class ConfirmDialogFragment extends InstrumentedDialogFragment { |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 202 | @Override |
| 203 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 204 | return new AlertDialog.Builder(getActivity()) |
| 205 | .setMessage(getArguments().getCharSequence(Intent.EXTRA_TEXT)) |
| 206 | .setPositiveButton(android.R.string.ok, new OnClickListener() { |
| 207 | @Override |
| 208 | public void onClick(DialogInterface dialog, int which) { |
| 209 | final Fragment f = getTargetFragment(); |
| 210 | if (f != null) { |
| 211 | ((CustomListPreferenceDialogFragment) f).onItemConfirmed(); |
| 212 | } |
| 213 | } |
| 214 | }) |
| 215 | .setNegativeButton(android.R.string.cancel, null) |
| 216 | .create(); |
| 217 | } |
Fan Zhang | ac5e593 | 2016-08-24 18:13:33 -0700 | [diff] [blame] | 218 | |
| 219 | @Override |
| 220 | public int getMetricsCategory() { |
| 221 | return MetricsProto.MetricsEvent.DIALOG_CUSTOM_LIST_CONFIRMATION; |
| 222 | } |
Jeff Sharkey | 4a8136b | 2016-07-27 12:53:34 -0600 | [diff] [blame] | 223 | } |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 224 | } |