blob: 8880296790180654a92fa55bd2a0980f4adb823d [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 Inwoodf2217132018-08-17 13:41:55 +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 Vishniakouc6442332018-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
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000053 *
54 * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
55 * <a href="{@docRoot}reference/androidx/preference/package-summary.html">
56 * Preference Library</a> for consistent behavior across all devices. For more information on
57 * using the AndroidX Preference Library see
58 * <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 */
Louis Pullen-Freilichb9596fa2018-11-19 17:40:56 +000060@Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061public abstract class DialogPreference extends Preference implements
62 DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
63 PreferenceManager.OnActivityDestroyListener {
Mathew Inwoodf2217132018-08-17 13:41:55 +010064 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 private AlertDialog.Builder mBuilder;
Siarhei Vishniakouc6442332018-03-09 15:38:12 -080066
Mathew Inwoodf2217132018-08-17 13:41:55 +010067 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 private CharSequence mDialogTitle;
Mathew Inwoodf2217132018-08-17 13:41:55 +010069 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 private CharSequence mDialogMessage;
Mathew Inwoodf2217132018-08-17 13:41:55 +010071 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 private Drawable mDialogIcon;
Mathew Inwoodf2217132018-08-17 13:41:55 +010073 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private CharSequence mPositiveButtonText;
Mathew Inwoodf2217132018-08-17 13:41:55 +010075 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 private CharSequence mNegativeButtonText;
77 private int mDialogLayoutResId;
78
79 /** The dialog, if it is showing. */
Mathew Inwoodf2217132018-08-17 13:41:55 +010080 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 private Dialog mDialog;
82
83 /** Which button was clicked. */
Mathew Inwoodf2217132018-08-17 13:41:55 +010084 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 private int mWhichButtonClicked;
Alan Viverette617feb92013-09-09 18:09:13 -070086
Siarhei Vishniakouc6442332018-03-09 15:38:12 -080087 /** Dismiss the dialog on the UI thread, but not inline with handlers */
88 private final Runnable mDismissRunnable = new Runnable() {
89 @Override
90 public void run() {
91 mDialog.dismiss();
92 }
93 };
94
Alan Viverette617feb92013-09-09 18:09:13 -070095 public DialogPreference(
96 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
97 super(context, attrs, defStyleAttr, defStyleRes);
98
99 final TypedArray a = context.obtainStyledAttributes(attrs,
100 com.android.internal.R.styleable.DialogPreference, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 mDialogTitle = a.getString(com.android.internal.R.styleable.DialogPreference_dialogTitle);
102 if (mDialogTitle == null) {
103 // Fallback on the regular title of the preference
104 // (the one that is seen in the list)
105 mDialogTitle = getTitle();
106 }
107 mDialogMessage = a.getString(com.android.internal.R.styleable.DialogPreference_dialogMessage);
108 mDialogIcon = a.getDrawable(com.android.internal.R.styleable.DialogPreference_dialogIcon);
109 mPositiveButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_positiveButtonText);
110 mNegativeButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_negativeButtonText);
111 mDialogLayoutResId = a.getResourceId(com.android.internal.R.styleable.DialogPreference_dialogLayout,
112 mDialogLayoutResId);
113 a.recycle();
Alan Viverette599d2a42013-09-16 13:48:29 -0700114 }
115
116 public DialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
117 this(context, attrs, defStyleAttr, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 }
119
120 public DialogPreference(Context context, AttributeSet attrs) {
121 this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle);
122 }
Alan Viverette599d2a42013-09-16 13:48:29 -0700123
124 public DialogPreference(Context context) {
125 this(context, null);
126 }
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 /**
129 * Sets the title of the dialog. This will be shown on subsequent dialogs.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800130 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * @param dialogTitle The title.
132 */
133 public void setDialogTitle(CharSequence dialogTitle) {
134 mDialogTitle = dialogTitle;
135 }
136
137 /**
138 * @see #setDialogTitle(CharSequence)
139 * @param dialogTitleResId The dialog title as a resource.
140 */
141 public void setDialogTitle(int dialogTitleResId) {
142 setDialogTitle(getContext().getString(dialogTitleResId));
143 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 /**
146 * Returns the title to be shown on subsequent dialogs.
147 * @return The title.
148 */
149 public CharSequence getDialogTitle() {
150 return mDialogTitle;
151 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
154 * Sets the message of the dialog. This will be shown on subsequent dialogs.
155 * <p>
156 * This message forms the content View of the dialog and conflicts with
157 * list-based dialogs, for example. If setting a custom View on a dialog via
158 * {@link #setDialogLayoutResource(int)}, include a text View with ID
159 * {@link android.R.id#message} and it will be populated with this message.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * @param dialogMessage The message.
162 */
163 public void setDialogMessage(CharSequence dialogMessage) {
164 mDialogMessage = dialogMessage;
165 }
166
167 /**
168 * @see #setDialogMessage(CharSequence)
169 * @param dialogMessageResId The dialog message as a resource.
170 */
171 public void setDialogMessage(int dialogMessageResId) {
172 setDialogMessage(getContext().getString(dialogMessageResId));
173 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 /**
176 * Returns the message to be shown on subsequent dialogs.
177 * @return The message.
178 */
179 public CharSequence getDialogMessage() {
180 return mDialogMessage;
181 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 /**
184 * Sets the icon of the dialog. This will be shown on subsequent dialogs.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * @param dialogIcon The icon, as a {@link Drawable}.
187 */
188 public void setDialogIcon(Drawable dialogIcon) {
189 mDialogIcon = dialogIcon;
190 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 /**
193 * Sets the icon (resource ID) of the dialog. This will be shown on
194 * subsequent dialogs.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800195 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 * @param dialogIconRes The icon, as a resource ID.
197 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700198 public void setDialogIcon(@DrawableRes int dialogIconRes) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800199 mDialogIcon = getContext().getDrawable(dialogIconRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /**
203 * Returns the icon to be shown on subsequent dialogs.
204 * @return The icon, as a {@link Drawable}.
205 */
206 public Drawable getDialogIcon() {
207 return mDialogIcon;
208 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 /**
211 * Sets the text of the positive button of the dialog. This will be shown on
212 * subsequent dialogs.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800213 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * @param positiveButtonText The text of the positive button.
215 */
216 public void setPositiveButtonText(CharSequence positiveButtonText) {
217 mPositiveButtonText = positiveButtonText;
218 }
219
220 /**
221 * @see #setPositiveButtonText(CharSequence)
222 * @param positiveButtonTextResId The positive button text as a resource.
223 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700224 public void setPositiveButtonText(@StringRes int positiveButtonTextResId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 setPositiveButtonText(getContext().getString(positiveButtonTextResId));
226 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 /**
229 * Returns the text of the positive button to be shown on subsequent
230 * dialogs.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800231 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * @return The text of the positive button.
233 */
234 public CharSequence getPositiveButtonText() {
235 return mPositiveButtonText;
236 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 /**
239 * Sets the text of the negative button of the dialog. This will be shown on
240 * subsequent dialogs.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800241 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 * @param negativeButtonText The text of the negative button.
243 */
244 public void setNegativeButtonText(CharSequence negativeButtonText) {
245 mNegativeButtonText = negativeButtonText;
246 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 /**
249 * @see #setNegativeButtonText(CharSequence)
250 * @param negativeButtonTextResId The negative button text as a resource.
251 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700252 public void setNegativeButtonText(@StringRes int negativeButtonTextResId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 setNegativeButtonText(getContext().getString(negativeButtonTextResId));
254 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 /**
257 * Returns the text of the negative button to be shown on subsequent
258 * dialogs.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800259 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * @return The text of the negative button.
261 */
262 public CharSequence getNegativeButtonText() {
263 return mNegativeButtonText;
264 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 /**
267 * Sets the layout resource that is inflated as the {@link View} to be shown
268 * as the content View of subsequent dialogs.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800269 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * @param dialogLayoutResId The layout resource ID to be inflated.
271 * @see #setDialogMessage(CharSequence)
272 */
273 public void setDialogLayoutResource(int dialogLayoutResId) {
274 mDialogLayoutResId = dialogLayoutResId;
275 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 /**
278 * Returns the layout resource that is used as the content View for
279 * subsequent dialogs.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800280 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * @return The layout resource.
282 */
283 public int getDialogLayoutResource() {
284 return mDialogLayoutResId;
285 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 /**
288 * Prepares the dialog builder to be shown when the preference is clicked.
289 * Use this to set custom properties on the dialog.
290 * <p>
291 * Do not {@link AlertDialog.Builder#create()} or
292 * {@link AlertDialog.Builder#show()}.
293 */
294 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
295 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 @Override
298 protected void onClick() {
Amith Yamasaniade026f2012-05-16 17:02:32 -0700299 if (mDialog != null && mDialog.isShowing()) return;
300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 showDialog(null);
302 }
303
304 /**
305 * Shows the dialog associated with this Preference. This is normally initiated
306 * automatically on clicking on the preference. Call this method if you need to
307 * show the dialog on some other event.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800308 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 * @param state Optional instance state to restore on the dialog
310 */
311 protected void showDialog(Bundle state) {
312 Context context = getContext();
313
Christian Mehlmauer746a95a2010-05-17 21:16:20 +0200314 mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE;
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 mBuilder = new AlertDialog.Builder(context)
317 .setTitle(mDialogTitle)
318 .setIcon(mDialogIcon)
319 .setPositiveButton(mPositiveButtonText, this)
320 .setNegativeButton(mNegativeButtonText, this);
321
322 View contentView = onCreateDialogView();
323 if (contentView != null) {
324 onBindDialogView(contentView);
325 mBuilder.setView(contentView);
326 } else {
327 mBuilder.setMessage(mDialogMessage);
328 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 onPrepareDialogBuilder(mBuilder);
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 getPreferenceManager().registerOnActivityDestroyListener(this);
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 // Create the dialog
335 final Dialog dialog = mDialog = mBuilder.create();
336 if (state != null) {
337 dialog.onRestoreInstanceState(state);
338 }
Amith Yamasani1d458572009-09-15 10:25:51 -0700339 if (needInputMethod()) {
340 requestInputMethod(dialog);
341 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800342 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
343 @Override
344 public void onShow(DialogInterface dialog) {
345 removeDismissCallbacks();
346 }
347 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 dialog.setOnDismissListener(this);
349 dialog.show();
350 }
Amith Yamasani1d458572009-09-15 10:25:51 -0700351
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800352 void postDismiss() {
353 removeDismissCallbacks();
354 View decorView = mDialog.getWindow().getDecorView();
355 decorView.post(mDismissRunnable);
356 }
357
358 private void removeDismissCallbacks() {
359 if (mDialog != null && mDialog.getWindow() != null
360 && mDialog.getWindow().getDecorView() != null) {
361 mDialog.getWindow().getDecorView().removeCallbacks(mDismissRunnable);
362 }
363 }
364
Amith Yamasani1d458572009-09-15 10:25:51 -0700365 /**
366 * Returns whether the preference needs to display a soft input method when the dialog
367 * is displayed. Default is false. Subclasses should override this method if they need
368 * the soft input method brought up automatically.
369 * @hide
370 */
371 protected boolean needInputMethod() {
372 return false;
373 }
374
375 /**
376 * Sets the required flags on the dialog window to enable input method window to show up.
377 */
378 private void requestInputMethod(Dialog dialog) {
379 Window window = dialog.getWindow();
Adam Powellc82c7a52011-08-28 14:36:05 -0700380 window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Amith Yamasani1d458572009-09-15 10:25:51 -0700381 }
382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 /**
384 * Creates the content view for the dialog (if a custom content view is
385 * required). By default, it inflates the dialog layout resource if it is
386 * set.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800387 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * @return The content View for the dialog.
389 * @see #setLayoutResource(int)
390 */
391 protected View onCreateDialogView() {
392 if (mDialogLayoutResId == 0) {
393 return null;
394 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800395
Adam Powell7e06ea82010-12-05 18:22:52 -0800396 LayoutInflater inflater = LayoutInflater.from(mBuilder.getContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 return inflater.inflate(mDialogLayoutResId, null);
398 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 /**
401 * Binds views in the content View of the dialog to data.
402 * <p>
403 * Make sure to call through to the superclass implementation.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800404 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 * @param view The content View of the dialog, if it is custom.
406 */
Tor Norbyec615c6f2015-03-02 10:11:44 -0800407 @CallSuper
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 protected void onBindDialogView(View view) {
409 View dialogMessageView = view.findViewById(com.android.internal.R.id.message);
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 if (dialogMessageView != null) {
412 final CharSequence message = getDialogMessage();
413 int newVisibility = View.GONE;
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 if (!TextUtils.isEmpty(message)) {
416 if (dialogMessageView instanceof TextView) {
417 ((TextView) dialogMessageView).setText(message);
418 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 newVisibility = View.VISIBLE;
421 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 if (dialogMessageView.getVisibility() != newVisibility) {
424 dialogMessageView.setVisibility(newVisibility);
425 }
426 }
427 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 public void onClick(DialogInterface dialog, int which) {
430 mWhichButtonClicked = which;
431 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800432
433 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 public void onDismiss(DialogInterface dialog) {
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800435 removeDismissCallbacks();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 getPreferenceManager().unregisterOnActivityDestroyListener(this);
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 mDialog = null;
439 onDialogClosed(mWhichButtonClicked == DialogInterface.BUTTON_POSITIVE);
440 }
441
442 /**
443 * Called when the dialog is dismissed and should be used to save data to
444 * the {@link SharedPreferences}.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 * @param positiveResult Whether the positive button was clicked (true), or
447 * the negative button was clicked or the dialog was canceled (false).
448 */
449 protected void onDialogClosed(boolean positiveResult) {
450 }
451
452 /**
453 * Gets the dialog that is shown by this preference.
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800454 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 * @return The dialog, or null if a dialog is not being shown.
456 */
457 public Dialog getDialog() {
458 return mDialog;
459 }
460
461 /**
462 * {@inheritDoc}
463 */
464 public void onActivityDestroy() {
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 if (mDialog == null || !mDialog.isShowing()) {
467 return;
468 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 mDialog.dismiss();
471 }
472
473 @Override
474 protected Parcelable onSaveInstanceState() {
475 final Parcelable superState = super.onSaveInstanceState();
476 if (mDialog == null || !mDialog.isShowing()) {
477 return superState;
478 }
479
480 final SavedState myState = new SavedState(superState);
481 myState.isDialogShowing = true;
482 myState.dialogBundle = mDialog.onSaveInstanceState();
483 return myState;
484 }
485
486 @Override
487 protected void onRestoreInstanceState(Parcelable state) {
488 if (state == null || !state.getClass().equals(SavedState.class)) {
489 // Didn't save state for us in onSaveInstanceState
490 super.onRestoreInstanceState(state);
491 return;
492 }
493
494 SavedState myState = (SavedState) state;
495 super.onRestoreInstanceState(myState.getSuperState());
496 if (myState.isDialogShowing) {
497 showDialog(myState.dialogBundle);
498 }
499 }
500
501 private static class SavedState extends BaseSavedState {
502 boolean isDialogShowing;
503 Bundle dialogBundle;
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 public SavedState(Parcel source) {
506 super(source);
507 isDialogShowing = source.readInt() == 1;
508 dialogBundle = source.readBundle();
509 }
510
511 @Override
512 public void writeToParcel(Parcel dest, int flags) {
513 super.writeToParcel(dest, flags);
514 dest.writeInt(isDialogShowing ? 1 : 0);
515 dest.writeBundle(dialogBundle);
516 }
517
518 public SavedState(Parcelable superState) {
519 super(superState);
520 }
521
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700522 public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 new Parcelable.Creator<SavedState>() {
524 public SavedState createFromParcel(Parcel in) {
525 return new SavedState(in);
526 }
527
528 public SavedState[] newArray(int size) {
529 return new SavedState[size];
530 }
531 };
532 }
Siarhei Vishniakouc6442332018-03-09 15:38:12 -0800533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534}