blob: 4b5a7b40fe44af02f82934e4910d088933e2fd24 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.preference;
18
19
Tor Norbyec615c6f2015-03-02 10:11:44 -080020import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070021import android.annotation.DrawableRes;
22import android.annotation.StringRes;
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010023import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.app.AlertDialog;
25import android.app.Dialog;
26import android.content.Context;
27import android.content.DialogInterface;
28import android.content.SharedPreferences;
29import android.content.res.TypedArray;
30import android.graphics.drawable.Drawable;
31import android.os.Bundle;
32import android.os.Parcel;
33import android.os.Parcelable;
34import android.text.TextUtils;
35import android.util.AttributeSet;
36import android.view.LayoutInflater;
37import android.view.View;
Amith Yamasani1d458572009-09-15 10:25:51 -070038import android.view.Window;
39import android.view.WindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import 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 Vishniakou5692a8b2018-03-09 15:38:12 -080046 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 * @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 */
54public abstract class DialogPreference extends Preference implements
55 DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
56 PreferenceManager.OnActivityDestroyListener {
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010057 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 private AlertDialog.Builder mBuilder;
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -080059
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010060 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 private CharSequence mDialogTitle;
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010062 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 private CharSequence mDialogMessage;
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010064 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 private Drawable mDialogIcon;
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010066 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 private CharSequence mPositiveButtonText;
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010068 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 private CharSequence mNegativeButtonText;
70 private int mDialogLayoutResId;
71
72 /** The dialog, if it is showing. */
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010073 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private Dialog mDialog;
75
76 /** Which button was clicked. */
Mathew Inwoodeac8d0a2018-08-17 13:51:26 +010077 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 private int mWhichButtonClicked;
Alan Viverette617feb92013-09-09 18:09:13 -070079
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -080080 /** 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 Viverette617feb92013-09-09 18:09:13 -070088 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 Project9066cfe2009-03-03 19:31:44 -080094 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 Viverette599d2a42013-09-16 13:48:29 -0700107 }
108
109 public DialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
110 this(context, attrs, defStyleAttr, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 }
112
113 public DialogPreference(Context context, AttributeSet attrs) {
114 this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle);
115 }
Alan Viverette599d2a42013-09-16 13:48:29 -0700116
117 public DialogPreference(Context context) {
118 this(context, null);
119 }
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 /**
122 * Sets the title of the dialog. This will be shown on subsequent dialogs.
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800123 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 * @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 Vishniakou5692a8b2018-03-09 15:38:12 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 /**
139 * Returns the title to be shown on subsequent dialogs.
140 * @return The title.
141 */
142 public CharSequence getDialogTitle() {
143 return mDialogTitle;
144 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 /**
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 Vishniakou5692a8b2018-03-09 15:38:12 -0800153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 * @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 Vishniakou5692a8b2018-03-09 15:38:12 -0800167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /**
169 * Returns the message to be shown on subsequent dialogs.
170 * @return The message.
171 */
172 public CharSequence getDialogMessage() {
173 return mDialogMessage;
174 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 /**
177 * Sets the icon of the dialog. This will be shown on subsequent dialogs.
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800178 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 * @param dialogIcon The icon, as a {@link Drawable}.
180 */
181 public void setDialogIcon(Drawable dialogIcon) {
182 mDialogIcon = dialogIcon;
183 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 /**
186 * Sets the icon (resource ID) of the dialog. This will be shown on
187 * subsequent dialogs.
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800188 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * @param dialogIconRes The icon, as a resource ID.
190 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700191 public void setDialogIcon(@DrawableRes int dialogIconRes) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800192 mDialogIcon = getContext().getDrawable(dialogIconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 /**
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 Vishniakou5692a8b2018-03-09 15:38:12 -0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 /**
204 * Sets the text of the positive button of the dialog. This will be shown on
205 * subsequent dialogs.
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800206 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * @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 Norbye7b9c9122013-05-30 16:48:33 -0700217 public void setPositiveButtonText(@StringRes int positiveButtonTextResId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 setPositiveButtonText(getContext().getString(positiveButtonTextResId));
219 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 /**
222 * Returns the text of the positive button to be shown on subsequent
223 * dialogs.
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800224 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * @return The text of the positive button.
226 */
227 public CharSequence getPositiveButtonText() {
228 return mPositiveButtonText;
229 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 /**
232 * Sets the text of the negative button of the dialog. This will be shown on
233 * subsequent dialogs.
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800234 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * @param negativeButtonText The text of the negative button.
236 */
237 public void setNegativeButtonText(CharSequence negativeButtonText) {
238 mNegativeButtonText = negativeButtonText;
239 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
242 * @see #setNegativeButtonText(CharSequence)
243 * @param negativeButtonTextResId The negative button text as a resource.
244 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700245 public void setNegativeButtonText(@StringRes int negativeButtonTextResId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 setNegativeButtonText(getContext().getString(negativeButtonTextResId));
247 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 /**
250 * Returns the text of the negative button to be shown on subsequent
251 * dialogs.
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800252 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * @return The text of the negative button.
254 */
255 public CharSequence getNegativeButtonText() {
256 return mNegativeButtonText;
257 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 /**
260 * Sets the layout resource that is inflated as the {@link View} to be shown
261 * as the content View of subsequent dialogs.
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800262 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 * @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 Vishniakou5692a8b2018-03-09 15:38:12 -0800269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 /**
271 * Returns the layout resource that is used as the content View for
272 * subsequent dialogs.
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 * @return The layout resource.
275 */
276 public int getDialogLayoutResource() {
277 return mDialogLayoutResId;
278 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
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 Vishniakou5692a8b2018-03-09 15:38:12 -0800289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 @Override
291 protected void onClick() {
Amith Yamasaniade026f2012-05-16 17:02:32 -0700292 if (mDialog != null && mDialog.isShowing()) return;
293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 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 Vishniakou5692a8b2018-03-09 15:38:12 -0800301 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 * @param state Optional instance state to restore on the dialog
303 */
304 protected void showDialog(Bundle state) {
305 Context context = getContext();
306
Christian Mehlmauer746a95a2010-05-17 21:16:20 +0200307 mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE;
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 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 Vishniakou5692a8b2018-03-09 15:38:12 -0800322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 onPrepareDialogBuilder(mBuilder);
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 getPreferenceManager().registerOnActivityDestroyListener(this);
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 // Create the dialog
328 final Dialog dialog = mDialog = mBuilder.create();
329 if (state != null) {
330 dialog.onRestoreInstanceState(state);
331 }
Amith Yamasani1d458572009-09-15 10:25:51 -0700332 if (needInputMethod()) {
333 requestInputMethod(dialog);
334 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800335 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
336 @Override
337 public void onShow(DialogInterface dialog) {
338 removeDismissCallbacks();
339 }
340 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 dialog.setOnDismissListener(this);
342 dialog.show();
343 }
Amith Yamasani1d458572009-09-15 10:25:51 -0700344
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800345 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 Yamasani1d458572009-09-15 10:25:51 -0700358 /**
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 Powellc82c7a52011-08-28 14:36:05 -0700373 window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Amith Yamasani1d458572009-09-15 10:25:51 -0700374 }
375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 /**
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 Vishniakou5692a8b2018-03-09 15:38:12 -0800380 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 * @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 Vishniakou5692a8b2018-03-09 15:38:12 -0800388
Adam Powell7e06ea82010-12-05 18:22:52 -0800389 LayoutInflater inflater = LayoutInflater.from(mBuilder.getContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 return inflater.inflate(mDialogLayoutResId, null);
391 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
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 Vishniakou5692a8b2018-03-09 15:38:12 -0800397 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 * @param view The content View of the dialog, if it is custom.
399 */
Tor Norbyec615c6f2015-03-02 10:11:44 -0800400 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 protected void onBindDialogView(View view) {
402 View dialogMessageView = view.findViewById(com.android.internal.R.id.message);
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 if (dialogMessageView != null) {
405 final CharSequence message = getDialogMessage();
406 int newVisibility = View.GONE;
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 if (!TextUtils.isEmpty(message)) {
409 if (dialogMessageView instanceof TextView) {
410 ((TextView) dialogMessageView).setText(message);
411 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 newVisibility = View.VISIBLE;
414 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 if (dialogMessageView.getVisibility() != newVisibility) {
417 dialogMessageView.setVisibility(newVisibility);
418 }
419 }
420 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public void onClick(DialogInterface dialog, int which) {
423 mWhichButtonClicked = which;
424 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800425
426 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 public void onDismiss(DialogInterface dialog) {
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800428 removeDismissCallbacks();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 getPreferenceManager().unregisterOnActivityDestroyListener(this);
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 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 Vishniakou5692a8b2018-03-09 15:38:12 -0800438 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 * @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 Vishniakou5692a8b2018-03-09 15:38:12 -0800447 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 * @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 Vishniakou5692a8b2018-03-09 15:38:12 -0800458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 if (mDialog == null || !mDialog.isShowing()) {
460 return;
461 }
Siarhei Vishniakou5692a8b2018-03-09 15:38:12 -0800462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 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 Vishniakou5692a8b2018-03-09 15:38:12 -0800497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 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 Vishniakou5692a8b2018-03-09 15:38:12 -0800526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527}