19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* 29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project 39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License. 69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at 79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and 149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License. 159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.preference; 189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 20c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbyeimport android.annotation.CallSuper; 217b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbyeimport android.annotation.DrawableRes; 227b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbyeimport android.annotation.StringRes; 239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.AlertDialog; 249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.Dialog; 259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context; 269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.DialogInterface; 279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.SharedPreferences; 289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.res.TypedArray; 299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.graphics.drawable.Drawable; 309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Bundle; 319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Parcel; 329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Parcelable; 339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.text.TextUtils; 349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.AttributeSet; 359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.view.LayoutInflater; 369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.view.View; 371d458570757e607f0dc11fb0e963017916ac0701Amith Yamasaniimport android.view.Window; 381d458570757e607f0dc11fb0e963017916ac0701Amith Yamasaniimport android.view.WindowManager; 399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.widget.TextView; 409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/** 429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * A base class for {@link Preference} objects that are 439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * dialog-based. These preferences will, when clicked, open a dialog showing the 449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * actual preference controls. 459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @attr ref android.R.styleable#DialogPreference_dialogTitle 479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @attr ref android.R.styleable#DialogPreference_dialogMessage 489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @attr ref android.R.styleable#DialogPreference_dialogIcon 499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @attr ref android.R.styleable#DialogPreference_dialogLayout 509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @attr ref android.R.styleable#DialogPreference_positiveButtonText 519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @attr ref android.R.styleable#DialogPreference_negativeButtonText 529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic abstract class DialogPreference extends Preference implements 549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project DialogInterface.OnClickListener, DialogInterface.OnDismissListener, 559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project PreferenceManager.OnActivityDestroyListener { 569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private AlertDialog.Builder mBuilder; 579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private CharSequence mDialogTitle; 599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private CharSequence mDialogMessage; 609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private Drawable mDialogIcon; 619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private CharSequence mPositiveButtonText; 629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private CharSequence mNegativeButtonText; 639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private int mDialogLayoutResId; 649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** The dialog, if it is showing. */ 669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private Dialog mDialog; 679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** Which button was clicked. */ 699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private int mWhichButtonClicked; 70617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette 71617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette public DialogPreference( 72617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 73617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette super(context, attrs, defStyleAttr, defStyleRes); 74617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette 75617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette final TypedArray a = context.obtainStyledAttributes(attrs, 76617feb99a06e7ffb3894e86a286bf30e085f321aAlan Viverette com.android.internal.R.styleable.DialogPreference, defStyleAttr, defStyleRes); 779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialogTitle = a.getString(com.android.internal.R.styleable.DialogPreference_dialogTitle); 789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (mDialogTitle == null) { 799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Fallback on the regular title of the preference 809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // (the one that is seen in the list) 819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialogTitle = getTitle(); 829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialogMessage = a.getString(com.android.internal.R.styleable.DialogPreference_dialogMessage); 849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialogIcon = a.getDrawable(com.android.internal.R.styleable.DialogPreference_dialogIcon); 859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mPositiveButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_positiveButtonText); 869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mNegativeButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_negativeButtonText); 879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialogLayoutResId = a.getResourceId(com.android.internal.R.styleable.DialogPreference_dialogLayout, 889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialogLayoutResId); 899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project a.recycle(); 90599d2a49e84bf0a4ee752e263a2c29d2ae942c3eAlan Viverette } 91599d2a49e84bf0a4ee752e263a2c29d2ae942c3eAlan Viverette 92599d2a49e84bf0a4ee752e263a2c29d2ae942c3eAlan Viverette public DialogPreference(Context context, AttributeSet attrs, int defStyleAttr) { 93599d2a49e84bf0a4ee752e263a2c29d2ae942c3eAlan Viverette this(context, attrs, defStyleAttr, 0); 949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public DialogPreference(Context context, AttributeSet attrs) { 979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project this(context, attrs, com.android.internal.R.attr.dialogPreferenceStyle); 989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 99599d2a49e84bf0a4ee752e263a2c29d2ae942c3eAlan Viverette 100599d2a49e84bf0a4ee752e263a2c29d2ae942c3eAlan Viverette public DialogPreference(Context context) { 101599d2a49e84bf0a4ee752e263a2c29d2ae942c3eAlan Viverette this(context, null); 102599d2a49e84bf0a4ee752e263a2c29d2ae942c3eAlan Viverette } 103599d2a49e84bf0a4ee752e263a2c29d2ae942c3eAlan Viverette 1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Sets the title of the dialog. This will be shown on subsequent dialogs. 1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param dialogTitle The title. 1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setDialogTitle(CharSequence dialogTitle) { 1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialogTitle = dialogTitle; 1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @see #setDialogTitle(CharSequence) 1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param dialogTitleResId The dialog title as a resource. 1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setDialogTitle(int dialogTitleResId) { 1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project setDialogTitle(getContext().getString(dialogTitleResId)); 1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns the title to be shown on subsequent dialogs. 1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return The title. 1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public CharSequence getDialogTitle() { 1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mDialogTitle; 1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Sets the message of the dialog. This will be shown on subsequent dialogs. 1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p> 1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * This message forms the content View of the dialog and conflicts with 1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * list-based dialogs, for example. If setting a custom View on a dialog via 1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link #setDialogLayoutResource(int)}, include a text View with ID 1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link android.R.id#message} and it will be populated with this message. 1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param dialogMessage The message. 1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setDialogMessage(CharSequence dialogMessage) { 1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialogMessage = dialogMessage; 1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @see #setDialogMessage(CharSequence) 1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param dialogMessageResId The dialog message as a resource. 1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setDialogMessage(int dialogMessageResId) { 1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project setDialogMessage(getContext().getString(dialogMessageResId)); 1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns the message to be shown on subsequent dialogs. 1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return The message. 1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public CharSequence getDialogMessage() { 1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mDialogMessage; 1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Sets the icon of the dialog. This will be shown on subsequent dialogs. 1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param dialogIcon The icon, as a {@link Drawable}. 1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setDialogIcon(Drawable dialogIcon) { 1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialogIcon = dialogIcon; 1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Sets the icon (resource ID) of the dialog. This will be shown on 1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * subsequent dialogs. 1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param dialogIconRes The icon, as a resource ID. 1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1747b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbye public void setDialogIcon(@DrawableRes int dialogIconRes) { 1758eea3ea5591e59f55cbb4f6b2b7e9363a285ced3Alan Viverette mDialogIcon = getContext().getDrawable(dialogIconRes); 1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns the icon to be shown on subsequent dialogs. 1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return The icon, as a {@link Drawable}. 1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public Drawable getDialogIcon() { 1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mDialogIcon; 1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Sets the text of the positive button of the dialog. This will be shown on 1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * subsequent dialogs. 1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param positiveButtonText The text of the positive button. 1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setPositiveButtonText(CharSequence positiveButtonText) { 1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mPositiveButtonText = positiveButtonText; 1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @see #setPositiveButtonText(CharSequence) 1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param positiveButtonTextResId The positive button text as a resource. 1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2007b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbye public void setPositiveButtonText(@StringRes int positiveButtonTextResId) { 2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project setPositiveButtonText(getContext().getString(positiveButtonTextResId)); 2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns the text of the positive button to be shown on subsequent 2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * dialogs. 2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return The text of the positive button. 2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public CharSequence getPositiveButtonText() { 2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mPositiveButtonText; 2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Sets the text of the negative button of the dialog. This will be shown on 2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * subsequent dialogs. 2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param negativeButtonText The text of the negative button. 2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setNegativeButtonText(CharSequence negativeButtonText) { 2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mNegativeButtonText = negativeButtonText; 2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @see #setNegativeButtonText(CharSequence) 2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param negativeButtonTextResId The negative button text as a resource. 2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2287b9c912f536925ac6ec43935d6e97506851b33d6Tor Norbye public void setNegativeButtonText(@StringRes int negativeButtonTextResId) { 2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project setNegativeButtonText(getContext().getString(negativeButtonTextResId)); 2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns the text of the negative button to be shown on subsequent 2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * dialogs. 2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return The text of the negative button. 2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public CharSequence getNegativeButtonText() { 2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mNegativeButtonText; 2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Sets the layout resource that is inflated as the {@link View} to be shown 2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * as the content View of subsequent dialogs. 2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param dialogLayoutResId The layout resource ID to be inflated. 2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @see #setDialogMessage(CharSequence) 2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setDialogLayoutResource(int dialogLayoutResId) { 2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialogLayoutResId = dialogLayoutResId; 2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns the layout resource that is used as the content View for 2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * subsequent dialogs. 2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return The layout resource. 2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public int getDialogLayoutResource() { 2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mDialogLayoutResId; 2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Prepares the dialog builder to be shown when the preference is clicked. 2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Use this to set custom properties on the dialog. 2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p> 2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Do not {@link AlertDialog.Builder#create()} or 2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@link AlertDialog.Builder#show()}. 2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @Override 2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project protected void onClick() { 275ade026f749af1871563417e323684c02a55edc4eAmith Yamasani if (mDialog != null && mDialog.isShowing()) return; 276ade026f749af1871563417e323684c02a55edc4eAmith Yamasani 2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project showDialog(null); 2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Shows the dialog associated with this Preference. This is normally initiated 2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * automatically on clicking on the preference. Call this method if you need to 2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * show the dialog on some other event. 2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param state Optional instance state to restore on the dialog 2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project protected void showDialog(Bundle state) { 2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Context context = getContext(); 2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 290746a95ab7145a1c7583d1f2d6004a508cd7a31dcChristian Mehlmauer mWhichButtonClicked = DialogInterface.BUTTON_NEGATIVE; 2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mBuilder = new AlertDialog.Builder(context) 2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project .setTitle(mDialogTitle) 2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project .setIcon(mDialogIcon) 2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project .setPositiveButton(mPositiveButtonText, this) 2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project .setNegativeButton(mNegativeButtonText, this); 2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project View contentView = onCreateDialogView(); 2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (contentView != null) { 3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project onBindDialogView(contentView); 3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mBuilder.setView(contentView); 3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } else { 3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mBuilder.setMessage(mDialogMessage); 3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project onPrepareDialogBuilder(mBuilder); 3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project getPreferenceManager().registerOnActivityDestroyListener(this); 3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Create the dialog 3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project final Dialog dialog = mDialog = mBuilder.create(); 3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (state != null) { 3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project dialog.onRestoreInstanceState(state); 3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3151d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani if (needInputMethod()) { 3161d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani requestInputMethod(dialog); 3171d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani } 3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project dialog.setOnDismissListener(this); 3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project dialog.show(); 3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3211d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani 3221d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani /** 3231d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani * Returns whether the preference needs to display a soft input method when the dialog 3241d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani * is displayed. Default is false. Subclasses should override this method if they need 3251d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani * the soft input method brought up automatically. 3261d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani * @hide 3271d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani */ 3281d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani protected boolean needInputMethod() { 3291d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani return false; 3301d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani } 3311d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani 3321d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani /** 3331d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani * Sets the required flags on the dialog window to enable input method window to show up. 3341d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani */ 3351d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani private void requestInputMethod(Dialog dialog) { 3361d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani Window window = dialog.getWindow(); 337c82c7a5403584ccab08b623566de837b2a3dd952Adam Powell window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 3381d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani } 3391d458570757e607f0dc11fb0e963017916ac0701Amith Yamasani 3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Creates the content view for the dialog (if a custom content view is 3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * required). By default, it inflates the dialog layout resource if it is 3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * set. 3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return The content View for the dialog. 3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @see #setLayoutResource(int) 3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project protected View onCreateDialogView() { 3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (mDialogLayoutResId == 0) { 3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return null; 3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3537e06ea80f781b26e17d630ccbb08237be2541f36Adam Powell LayoutInflater inflater = LayoutInflater.from(mBuilder.getContext()); 3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return inflater.inflate(mDialogLayoutResId, null); 3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Binds views in the content View of the dialog to data. 3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p> 3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Make sure to call through to the superclass implementation. 3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param view The content View of the dialog, if it is custom. 3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 364c615c6fc9caca76cd96998f86e1f1e6393aeadbbTor Norbye @CallSuper 3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project protected void onBindDialogView(View view) { 3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project View dialogMessageView = view.findViewById(com.android.internal.R.id.message); 3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (dialogMessageView != null) { 3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project final CharSequence message = getDialogMessage(); 3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project int newVisibility = View.GONE; 3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (!TextUtils.isEmpty(message)) { 3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (dialogMessageView instanceof TextView) { 3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project ((TextView) dialogMessageView).setText(message); 3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project newVisibility = View.VISIBLE; 3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (dialogMessageView.getVisibility() != newVisibility) { 3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project dialogMessageView.setVisibility(newVisibility); 3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void onClick(DialogInterface dialog, int which) { 3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mWhichButtonClicked = which; 3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void onDismiss(DialogInterface dialog) { 3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project getPreferenceManager().unregisterOnActivityDestroyListener(this); 3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialog = null; 3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project onDialogClosed(mWhichButtonClicked == DialogInterface.BUTTON_POSITIVE); 3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Called when the dialog is dismissed and should be used to save data to 4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the {@link SharedPreferences}. 4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @param positiveResult Whether the positive button was clicked (true), or 4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the negative button was clicked or the dialog was canceled (false). 4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project protected void onDialogClosed(boolean positiveResult) { 4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Gets the dialog that is shown by this preference. 4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @return The dialog, or null if a dialog is not being shown. 4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public Dialog getDialog() { 4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mDialog; 4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * {@inheritDoc} 4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void onActivityDestroy() { 4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (mDialog == null || !mDialog.isShowing()) { 4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return; 4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mDialog.dismiss(); 4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @Override 4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project protected Parcelable onSaveInstanceState() { 4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project final Parcelable superState = super.onSaveInstanceState(); 4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (mDialog == null || !mDialog.isShowing()) { 4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return superState; 4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project final SavedState myState = new SavedState(superState); 4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project myState.isDialogShowing = true; 4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project myState.dialogBundle = mDialog.onSaveInstanceState(); 4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return myState; 4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @Override 4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project protected void onRestoreInstanceState(Parcelable state) { 4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (state == null || !state.getClass().equals(SavedState.class)) { 4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project // Didn't save state for us in onSaveInstanceState 4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project super.onRestoreInstanceState(state); 4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return; 4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project SavedState myState = (SavedState) state; 4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project super.onRestoreInstanceState(myState.getSuperState()); 4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project if (myState.isDialogShowing) { 4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project showDialog(myState.dialogBundle); 4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project private static class SavedState extends BaseSavedState { 4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project boolean isDialogShowing; 4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project Bundle dialogBundle; 4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public SavedState(Parcel source) { 4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project super(source); 4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project isDialogShowing = source.readInt() == 1; 4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project dialogBundle = source.readBundle(); 4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project @Override 4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void writeToParcel(Parcel dest, int flags) { 4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project super.writeToParcel(dest, flags); 4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project dest.writeInt(isDialogShowing ? 1 : 0); 4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project dest.writeBundle(dialogBundle); 4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public SavedState(Parcelable superState) { 4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project super(superState); 4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public static final Parcelable.Creator<SavedState> CREATOR = 4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project new Parcelable.Creator<SavedState>() { 4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public SavedState createFromParcel(Parcel in) { 4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return new SavedState(in); 4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public SavedState[] newArray(int size) { 4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return new SavedState[size]; 4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project }; 4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 491