The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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.preference; |
| 18 | |
| 19 | |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 20 | import android.annotation.CallSuper; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 21 | import android.annotation.DrawableRes; |
| 22 | import android.annotation.StringRes; |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 23 | import android.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.app.AlertDialog; |
| 25 | import android.app.Dialog; |
| 26 | import android.content.Context; |
| 27 | import android.content.DialogInterface; |
| 28 | import android.content.SharedPreferences; |
| 29 | import android.content.res.TypedArray; |
| 30 | import android.graphics.drawable.Drawable; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.Parcel; |
| 33 | import android.os.Parcelable; |
| 34 | import android.text.TextUtils; |
| 35 | import android.util.AttributeSet; |
| 36 | import android.view.LayoutInflater; |
| 37 | import android.view.View; |
Amith Yamasani | 1d45857 | 2009-09-15 10:25:51 -0700 | [diff] [blame] | 38 | import android.view.Window; |
| 39 | import android.view.WindowManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.widget.TextView; |
| 41 | |
| 42 | /** |
| 43 | * A base class for {@link Preference} objects that are |
| 44 | * dialog-based. These preferences will, when clicked, open a dialog showing the |
| 45 | * actual preference controls. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 46 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | * @attr ref android.R.styleable#DialogPreference_dialogTitle |
| 48 | * @attr ref android.R.styleable#DialogPreference_dialogMessage |
| 49 | * @attr ref android.R.styleable#DialogPreference_dialogIcon |
| 50 | * @attr ref android.R.styleable#DialogPreference_dialogLayout |
| 51 | * @attr ref android.R.styleable#DialogPreference_positiveButtonText |
| 52 | * @attr ref android.R.styleable#DialogPreference_negativeButtonText |
| 53 | */ |
| 54 | public abstract class DialogPreference extends Preference implements |
| 55 | DialogInterface.OnClickListener, DialogInterface.OnDismissListener, |
| 56 | PreferenceManager.OnActivityDestroyListener { |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 57 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | private AlertDialog.Builder mBuilder; |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 59 | |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 60 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | private CharSequence mDialogTitle; |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 62 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | private CharSequence mDialogMessage; |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 64 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | private Drawable mDialogIcon; |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 66 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | private CharSequence mPositiveButtonText; |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 68 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | private CharSequence mNegativeButtonText; |
| 70 | private int mDialogLayoutResId; |
| 71 | |
| 72 | /** The dialog, if it is showing. */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 73 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | private Dialog mDialog; |
| 75 | |
| 76 | /** Which button was clicked. */ |
Mathew Inwood | eac8d0a | 2018-08-17 13:51:26 +0100 | [diff] [blame] | 77 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | private int mWhichButtonClicked; |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 79 | |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 80 | /** Dismiss the dialog on the UI thread, but not inline with handlers */ |
| 81 | private final Runnable mDismissRunnable = new Runnable() { |
| 82 | @Override |
| 83 | public void run() { |
| 84 | mDialog.dismiss(); |
| 85 | } |
| 86 | }; |
| 87 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 88 | public DialogPreference( |
| 89 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 90 | super(context, attrs, defStyleAttr, defStyleRes); |
| 91 | |
| 92 | final TypedArray a = context.obtainStyledAttributes(attrs, |
| 93 | com.android.internal.R.styleable.DialogPreference, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | mDialogTitle = a.getString(com.android.internal.R.styleable.DialogPreference_dialogTitle); |
| 95 | if (mDialogTitle == null) { |
| 96 | // Fallback on the regular title of the preference |
| 97 | // (the one that is seen in the list) |
| 98 | mDialogTitle = getTitle(); |
| 99 | } |
| 100 | mDialogMessage = a.getString(com.android.internal.R.styleable.DialogPreference_dialogMessage); |
| 101 | mDialogIcon = a.getDrawable(com.android.internal.R.styleable.DialogPreference_dialogIcon); |
| 102 | mPositiveButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_positiveButtonText); |
| 103 | mNegativeButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_negativeButtonText); |
| 104 | mDialogLayoutResId = a.getResourceId(com.android.internal.R.styleable.DialogPreference_dialogLayout, |
| 105 | mDialogLayoutResId); |
| 106 | a.recycle(); |
Alan Viverette | 599d2a4 | 2013-09-16 13:48:29 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public DialogPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 110 | this(context, attrs, defStyleAttr, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | public DialogPreference(Context context, AttributeSet attrs) { |
| 114 | this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle); |
| 115 | } |
Alan Viverette | 599d2a4 | 2013-09-16 13:48:29 -0700 | [diff] [blame] | 116 | |
| 117 | public DialogPreference(Context context) { |
| 118 | this(context, null); |
| 119 | } |
| 120 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | /** |
| 122 | * Sets the title of the dialog. This will be shown on subsequent dialogs. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 123 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | * @param dialogTitle The title. |
| 125 | */ |
| 126 | public void setDialogTitle(CharSequence dialogTitle) { |
| 127 | mDialogTitle = dialogTitle; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @see #setDialogTitle(CharSequence) |
| 132 | * @param dialogTitleResId The dialog title as a resource. |
| 133 | */ |
| 134 | public void setDialogTitle(int dialogTitleResId) { |
| 135 | setDialogTitle(getContext().getString(dialogTitleResId)); |
| 136 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | /** |
| 139 | * Returns the title to be shown on subsequent dialogs. |
| 140 | * @return The title. |
| 141 | */ |
| 142 | public CharSequence getDialogTitle() { |
| 143 | return mDialogTitle; |
| 144 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | /** |
| 147 | * Sets the message of the dialog. This will be shown on subsequent dialogs. |
| 148 | * <p> |
| 149 | * This message forms the content View of the dialog and conflicts with |
| 150 | * list-based dialogs, for example. If setting a custom View on a dialog via |
| 151 | * {@link #setDialogLayoutResource(int)}, include a text View with ID |
| 152 | * {@link android.R.id#message} and it will be populated with this message. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 153 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | * @param dialogMessage The message. |
| 155 | */ |
| 156 | public void setDialogMessage(CharSequence dialogMessage) { |
| 157 | mDialogMessage = dialogMessage; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @see #setDialogMessage(CharSequence) |
| 162 | * @param dialogMessageResId The dialog message as a resource. |
| 163 | */ |
| 164 | public void setDialogMessage(int dialogMessageResId) { |
| 165 | setDialogMessage(getContext().getString(dialogMessageResId)); |
| 166 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Returns the message to be shown on subsequent dialogs. |
| 170 | * @return The message. |
| 171 | */ |
| 172 | public CharSequence getDialogMessage() { |
| 173 | return mDialogMessage; |
| 174 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | /** |
| 177 | * Sets the icon of the dialog. This will be shown on subsequent dialogs. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 178 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | * @param dialogIcon The icon, as a {@link Drawable}. |
| 180 | */ |
| 181 | public void setDialogIcon(Drawable dialogIcon) { |
| 182 | mDialogIcon = dialogIcon; |
| 183 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | /** |
| 186 | * Sets the icon (resource ID) of the dialog. This will be shown on |
| 187 | * subsequent dialogs. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 188 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | * @param dialogIconRes The icon, as a resource ID. |
| 190 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 191 | public void setDialogIcon(@DrawableRes int dialogIconRes) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 192 | mDialogIcon = getContext().getDrawable(dialogIconRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | /** |
| 196 | * Returns the icon to be shown on subsequent dialogs. |
| 197 | * @return The icon, as a {@link Drawable}. |
| 198 | */ |
| 199 | public Drawable getDialogIcon() { |
| 200 | return mDialogIcon; |
| 201 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | /** |
| 204 | * Sets the text of the positive button of the dialog. This will be shown on |
| 205 | * subsequent dialogs. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 206 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | * @param positiveButtonText The text of the positive button. |
| 208 | */ |
| 209 | public void setPositiveButtonText(CharSequence positiveButtonText) { |
| 210 | mPositiveButtonText = positiveButtonText; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @see #setPositiveButtonText(CharSequence) |
| 215 | * @param positiveButtonTextResId The positive button text as a resource. |
| 216 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 217 | public void setPositiveButtonText(@StringRes int positiveButtonTextResId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | setPositiveButtonText(getContext().getString(positiveButtonTextResId)); |
| 219 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | /** |
| 222 | * Returns the text of the positive button to be shown on subsequent |
| 223 | * dialogs. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 224 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | * @return The text of the positive button. |
| 226 | */ |
| 227 | public CharSequence getPositiveButtonText() { |
| 228 | return mPositiveButtonText; |
| 229 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | /** |
| 232 | * Sets the text of the negative button of the dialog. This will be shown on |
| 233 | * subsequent dialogs. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 234 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | * @param negativeButtonText The text of the negative button. |
| 236 | */ |
| 237 | public void setNegativeButtonText(CharSequence negativeButtonText) { |
| 238 | mNegativeButtonText = negativeButtonText; |
| 239 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 240 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | /** |
| 242 | * @see #setNegativeButtonText(CharSequence) |
| 243 | * @param negativeButtonTextResId The negative button text as a resource. |
| 244 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 245 | public void setNegativeButtonText(@StringRes int negativeButtonTextResId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | setNegativeButtonText(getContext().getString(negativeButtonTextResId)); |
| 247 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 248 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | /** |
| 250 | * Returns the text of the negative button to be shown on subsequent |
| 251 | * dialogs. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 252 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | * @return The text of the negative button. |
| 254 | */ |
| 255 | public CharSequence getNegativeButtonText() { |
| 256 | return mNegativeButtonText; |
| 257 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 258 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | /** |
| 260 | * Sets the layout resource that is inflated as the {@link View} to be shown |
| 261 | * as the content View of subsequent dialogs. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 262 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | * @param dialogLayoutResId The layout resource ID to be inflated. |
| 264 | * @see #setDialogMessage(CharSequence) |
| 265 | */ |
| 266 | public void setDialogLayoutResource(int dialogLayoutResId) { |
| 267 | mDialogLayoutResId = dialogLayoutResId; |
| 268 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 269 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | /** |
| 271 | * Returns the layout resource that is used as the content View for |
| 272 | * subsequent dialogs. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 273 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | * @return The layout resource. |
| 275 | */ |
| 276 | public int getDialogLayoutResource() { |
| 277 | return mDialogLayoutResId; |
| 278 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | /** |
| 281 | * Prepares the dialog builder to be shown when the preference is clicked. |
| 282 | * Use this to set custom properties on the dialog. |
| 283 | * <p> |
| 284 | * Do not {@link AlertDialog.Builder#create()} or |
| 285 | * {@link AlertDialog.Builder#show()}. |
| 286 | */ |
| 287 | protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { |
| 288 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | @Override |
| 291 | protected void onClick() { |
Amith Yamasani | ade026f | 2012-05-16 17:02:32 -0700 | [diff] [blame] | 292 | if (mDialog != null && mDialog.isShowing()) return; |
| 293 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | showDialog(null); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Shows the dialog associated with this Preference. This is normally initiated |
| 299 | * automatically on clicking on the preference. Call this method if you need to |
| 300 | * show the dialog on some other event. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 301 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | * @param state Optional instance state to restore on the dialog |
| 303 | */ |
| 304 | protected void showDialog(Bundle state) { |
| 305 | Context context = getContext(); |
| 306 | |
Christian Mehlmauer | 746a95a | 2010-05-17 21:16:20 +0200 | [diff] [blame] | 307 | mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE; |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | mBuilder = new AlertDialog.Builder(context) |
| 310 | .setTitle(mDialogTitle) |
| 311 | .setIcon(mDialogIcon) |
| 312 | .setPositiveButton(mPositiveButtonText, this) |
| 313 | .setNegativeButton(mNegativeButtonText, this); |
| 314 | |
| 315 | View contentView = onCreateDialogView(); |
| 316 | if (contentView != null) { |
| 317 | onBindDialogView(contentView); |
| 318 | mBuilder.setView(contentView); |
| 319 | } else { |
| 320 | mBuilder.setMessage(mDialogMessage); |
| 321 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 322 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | onPrepareDialogBuilder(mBuilder); |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 324 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | getPreferenceManager().registerOnActivityDestroyListener(this); |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 326 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | // Create the dialog |
| 328 | final Dialog dialog = mDialog = mBuilder.create(); |
| 329 | if (state != null) { |
| 330 | dialog.onRestoreInstanceState(state); |
| 331 | } |
Amith Yamasani | 1d45857 | 2009-09-15 10:25:51 -0700 | [diff] [blame] | 332 | if (needInputMethod()) { |
| 333 | requestInputMethod(dialog); |
| 334 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 335 | dialog.setOnShowListener(new DialogInterface.OnShowListener() { |
| 336 | @Override |
| 337 | public void onShow(DialogInterface dialog) { |
| 338 | removeDismissCallbacks(); |
| 339 | } |
| 340 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | dialog.setOnDismissListener(this); |
| 342 | dialog.show(); |
| 343 | } |
Amith Yamasani | 1d45857 | 2009-09-15 10:25:51 -0700 | [diff] [blame] | 344 | |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 345 | void postDismiss() { |
| 346 | removeDismissCallbacks(); |
| 347 | View decorView = mDialog.getWindow().getDecorView(); |
| 348 | decorView.post(mDismissRunnable); |
| 349 | } |
| 350 | |
| 351 | private void removeDismissCallbacks() { |
| 352 | if (mDialog != null && mDialog.getWindow() != null |
| 353 | && mDialog.getWindow().getDecorView() != null) { |
| 354 | mDialog.getWindow().getDecorView().removeCallbacks(mDismissRunnable); |
| 355 | } |
| 356 | } |
| 357 | |
Amith Yamasani | 1d45857 | 2009-09-15 10:25:51 -0700 | [diff] [blame] | 358 | /** |
| 359 | * Returns whether the preference needs to display a soft input method when the dialog |
| 360 | * is displayed. Default is false. Subclasses should override this method if they need |
| 361 | * the soft input method brought up automatically. |
| 362 | * @hide |
| 363 | */ |
| 364 | protected boolean needInputMethod() { |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Sets the required flags on the dialog window to enable input method window to show up. |
| 370 | */ |
| 371 | private void requestInputMethod(Dialog dialog) { |
| 372 | Window window = dialog.getWindow(); |
Adam Powell | c82c7a5 | 2011-08-28 14:36:05 -0700 | [diff] [blame] | 373 | window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); |
Amith Yamasani | 1d45857 | 2009-09-15 10:25:51 -0700 | [diff] [blame] | 374 | } |
| 375 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | /** |
| 377 | * Creates the content view for the dialog (if a custom content view is |
| 378 | * required). By default, it inflates the dialog layout resource if it is |
| 379 | * set. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 380 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | * @return The content View for the dialog. |
| 382 | * @see #setLayoutResource(int) |
| 383 | */ |
| 384 | protected View onCreateDialogView() { |
| 385 | if (mDialogLayoutResId == 0) { |
| 386 | return null; |
| 387 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 388 | |
Adam Powell | 7e06ea8 | 2010-12-05 18:22:52 -0800 | [diff] [blame] | 389 | LayoutInflater inflater = LayoutInflater.from(mBuilder.getContext()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | return inflater.inflate(mDialogLayoutResId, null); |
| 391 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 392 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | /** |
| 394 | * Binds views in the content View of the dialog to data. |
| 395 | * <p> |
| 396 | * Make sure to call through to the superclass implementation. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 397 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | * @param view The content View of the dialog, if it is custom. |
| 399 | */ |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 400 | @CallSuper |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | protected void onBindDialogView(View view) { |
| 402 | View dialogMessageView = view.findViewById(com.android.internal.R.id.message); |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | if (dialogMessageView != null) { |
| 405 | final CharSequence message = getDialogMessage(); |
| 406 | int newVisibility = View.GONE; |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 407 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | if (!TextUtils.isEmpty(message)) { |
| 409 | if (dialogMessageView instanceof TextView) { |
| 410 | ((TextView) dialogMessageView).setText(message); |
| 411 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 412 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | newVisibility = View.VISIBLE; |
| 414 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | if (dialogMessageView.getVisibility() != newVisibility) { |
| 417 | dialogMessageView.setVisibility(newVisibility); |
| 418 | } |
| 419 | } |
| 420 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 421 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | public void onClick(DialogInterface dialog, int which) { |
| 423 | mWhichButtonClicked = which; |
| 424 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 425 | |
| 426 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | public void onDismiss(DialogInterface dialog) { |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 428 | removeDismissCallbacks(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | getPreferenceManager().unregisterOnActivityDestroyListener(this); |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 430 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | mDialog = null; |
| 432 | onDialogClosed(mWhichButtonClicked == DialogInterface.BUTTON_POSITIVE); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Called when the dialog is dismissed and should be used to save data to |
| 437 | * the {@link SharedPreferences}. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 438 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | * @param positiveResult Whether the positive button was clicked (true), or |
| 440 | * the negative button was clicked or the dialog was canceled (false). |
| 441 | */ |
| 442 | protected void onDialogClosed(boolean positiveResult) { |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Gets the dialog that is shown by this preference. |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 447 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | * @return The dialog, or null if a dialog is not being shown. |
| 449 | */ |
| 450 | public Dialog getDialog() { |
| 451 | return mDialog; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * {@inheritDoc} |
| 456 | */ |
| 457 | public void onActivityDestroy() { |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | if (mDialog == null || !mDialog.isShowing()) { |
| 460 | return; |
| 461 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 462 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | mDialog.dismiss(); |
| 464 | } |
| 465 | |
| 466 | @Override |
| 467 | protected Parcelable onSaveInstanceState() { |
| 468 | final Parcelable superState = super.onSaveInstanceState(); |
| 469 | if (mDialog == null || !mDialog.isShowing()) { |
| 470 | return superState; |
| 471 | } |
| 472 | |
| 473 | final SavedState myState = new SavedState(superState); |
| 474 | myState.isDialogShowing = true; |
| 475 | myState.dialogBundle = mDialog.onSaveInstanceState(); |
| 476 | return myState; |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | protected void onRestoreInstanceState(Parcelable state) { |
| 481 | if (state == null || !state.getClass().equals(SavedState.class)) { |
| 482 | // Didn't save state for us in onSaveInstanceState |
| 483 | super.onRestoreInstanceState(state); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | SavedState myState = (SavedState) state; |
| 488 | super.onRestoreInstanceState(myState.getSuperState()); |
| 489 | if (myState.isDialogShowing) { |
| 490 | showDialog(myState.dialogBundle); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | private static class SavedState extends BaseSavedState { |
| 495 | boolean isDialogShowing; |
| 496 | Bundle dialogBundle; |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 497 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | public SavedState(Parcel source) { |
| 499 | super(source); |
| 500 | isDialogShowing = source.readInt() == 1; |
| 501 | dialogBundle = source.readBundle(); |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public void writeToParcel(Parcel dest, int flags) { |
| 506 | super.writeToParcel(dest, flags); |
| 507 | dest.writeInt(isDialogShowing ? 1 : 0); |
| 508 | dest.writeBundle(dialogBundle); |
| 509 | } |
| 510 | |
| 511 | public SavedState(Parcelable superState) { |
| 512 | super(superState); |
| 513 | } |
| 514 | |
| 515 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 516 | new Parcelable.Creator<SavedState>() { |
| 517 | public SavedState createFromParcel(Parcel in) { |
| 518 | return new SavedState(in); |
| 519 | } |
| 520 | |
| 521 | public SavedState[] newArray(int size) { |
| 522 | return new SavedState[size]; |
| 523 | } |
| 524 | }; |
| 525 | } |
Siarhei Vishniakou | 5692a8b | 2018-03-09 15:38:12 -0800 | [diff] [blame] | 526 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | } |