The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 19 | import android.annotation.CallSuper; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 20 | import android.annotation.DrawableRes; |
| 21 | import android.annotation.IdRes; |
| 22 | import android.annotation.LayoutRes; |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 23 | import android.annotation.NonNull; |
Scott Kennedy | c051955 | 2015-02-11 15:33:10 -0800 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Clara Bayarri | 75e0979 | 2015-07-29 16:20:40 +0100 | [diff] [blame] | 25 | import android.annotation.StringRes; |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 26 | import android.annotation.StyleRes; |
Karl Rosaen | 53d24af | 2009-07-14 14:58:10 -0700 | [diff] [blame] | 27 | import android.content.ComponentName; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 28 | import android.content.Context; |
Karl Rosaen | 7bafed8 | 2009-09-04 11:15:21 -0700 | [diff] [blame] | 29 | import android.content.ContextWrapper; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 30 | import android.content.DialogInterface; |
Jorim Jaggi | b10e33f | 2015-02-04 21:57:40 +0100 | [diff] [blame] | 31 | import android.content.pm.ApplicationInfo; |
Chris Craik | 6faa9e5 | 2018-01-11 10:46:10 -0800 | [diff] [blame] | 32 | import android.content.res.Configuration; |
Adam Lesinski | 9553fb3 | 2017-05-23 18:53:44 -0700 | [diff] [blame] | 33 | import android.content.res.ResourceId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.graphics.drawable.Drawable; |
| 35 | import android.net.Uri; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 36 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.Handler; |
Svetoslav Ganov | 44bfdd8 | 2012-01-26 10:00:18 -0800 | [diff] [blame] | 38 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.os.Message; |
Adam Powell | 89fc3ac | 2011-11-01 18:00:44 -0700 | [diff] [blame] | 40 | import android.util.Log; |
Adam Powell | 2fbf4de6 | 2010-09-30 15:46:46 -0700 | [diff] [blame] | 41 | import android.util.TypedValue; |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 42 | import android.view.ActionMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.view.ContextMenu; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 44 | import android.view.ContextMenu.ContextMenuInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.view.ContextThemeWrapper; |
| 46 | import android.view.Gravity; |
| 47 | import android.view.KeyEvent; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 48 | import android.view.LayoutInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.view.Menu; |
| 50 | import android.view.MenuItem; |
| 51 | import android.view.MotionEvent; |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 52 | import android.view.SearchEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.view.View; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 54 | import android.view.View.OnCreateContextMenuListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.view.ViewGroup; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 56 | import android.view.ViewGroup.LayoutParams; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.view.Window; |
| 58 | import android.view.WindowManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 59 | import android.view.accessibility.AccessibilityEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Chris Craik | 6faa9e5 | 2018-01-11 10:46:10 -0800 | [diff] [blame] | 61 | import com.android.internal.R; |
| 62 | import com.android.internal.app.WindowDecorActionBar; |
| 63 | import com.android.internal.policy.PhoneWindow; |
| 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import java.lang.ref.WeakReference; |
| 66 | |
| 67 | /** |
| 68 | * Base class for Dialogs. |
| 69 | * |
| 70 | * <p>Note: Activities provide a facility to manage the creation, saving and |
| 71 | * restoring of dialogs. See {@link Activity#onCreateDialog(int)}, |
| 72 | * {@link Activity#onPrepareDialog(int, Dialog)}, |
| 73 | * {@link Activity#showDialog(int)}, and {@link Activity#dismissDialog(int)}. If |
| 74 | * these methods are used, {@link #getOwnerActivity()} will return the Activity |
| 75 | * that managed this dialog. |
| 76 | * |
| 77 | * <p>Often you will want to have a Dialog display on top of the current |
| 78 | * input method, because there is no reason for it to accept text. You can |
| 79 | * do this by setting the {@link WindowManager.LayoutParams#FLAG_ALT_FOCUSABLE_IM |
| 80 | * WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM} window flag (assuming |
| 81 | * your Dialog takes input focus, as it the default) with the following code: |
| 82 | * |
| 83 | * <pre> |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 84 | * getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, |
| 85 | * WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);</pre> |
| 86 | * |
| 87 | * <div class="special reference"> |
| 88 | * <h3>Developer Guides</h3> |
| 89 | * <p>For more information about creating dialogs, read the |
| 90 | * <a href="{@docRoot}guide/topics/ui/dialogs.html">Dialogs</a> developer guide.</p> |
| 91 | * </div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | */ |
| 93 | public class Dialog implements DialogInterface, Window.Callback, |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 94 | KeyEvent.Callback, OnCreateContextMenuListener, Window.OnWindowDismissedCallback { |
Adam Powell | 89fc3ac | 2011-11-01 18:00:44 -0700 | [diff] [blame] | 95 | private static final String TAG = "Dialog"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | private Activity mOwnerActivity; |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 97 | |
| 98 | private final WindowManager mWindowManager; |
| 99 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | final Context mContext; |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 101 | final Window mWindow; |
| 102 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | View mDecor; |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 104 | |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 105 | private ActionBar mActionBar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | /** |
| 107 | * This field should be made private, so it is hidden from the SDK. |
| 108 | * {@hide} |
| 109 | */ |
| 110 | protected boolean mCancelable = true; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 111 | |
Dianne Hackborn | f812fee | 2011-01-25 14:54:29 -0800 | [diff] [blame] | 112 | private String mCancelAndDismissTaken; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | private Message mCancelMessage; |
| 114 | private Message mDismissMessage; |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 115 | private Message mShowMessage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | private OnKeyListener mOnKeyListener; |
| 118 | |
| 119 | private boolean mCreated = false; |
| 120 | private boolean mShowing = false; |
Dianne Hackborn | 8b4cac1 | 2011-01-03 13:58:14 -0800 | [diff] [blame] | 121 | private boolean mCanceled = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | private final Handler mHandler = new Handler(); |
| 124 | |
Romain Guy | 7883c97 | 2010-03-01 16:39:17 -0800 | [diff] [blame] | 125 | private static final int DISMISS = 0x43; |
| 126 | private static final int CANCEL = 0x44; |
| 127 | private static final int SHOW = 0x45; |
| 128 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 129 | private final Handler mListenersHandler; |
Romain Guy | 7883c97 | 2010-03-01 16:39:17 -0800 | [diff] [blame] | 130 | |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 131 | private SearchEvent mSearchEvent; |
| 132 | |
Adam Powell | cfe9aee | 2011-11-01 14:56:27 -0700 | [diff] [blame] | 133 | private ActionMode mActionMode; |
| 134 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 135 | private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY; |
| 136 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 137 | private final Runnable mDismissAction = this::dismissDialog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | |
| 139 | /** |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 140 | * Creates a dialog window that uses the default dialog theme. |
| 141 | * <p> |
| 142 | * The supplied {@code context} is used to obtain the window manager and |
| 143 | * base theme used to present the dialog. |
| 144 | * |
| 145 | * @param context the context in which the dialog should run |
| 146 | * @see android.R.styleable#Theme_dialogTheme |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | */ |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 148 | public Dialog(@NonNull Context context) { |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 149 | this(context, 0, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 153 | * Creates a dialog window that uses a custom dialog style. |
| 154 | * <p> |
| 155 | * The supplied {@code context} is used to obtain the window manager and |
| 156 | * base theme used to present the dialog. |
| 157 | * <p> |
| 158 | * The supplied {@code theme} is applied on top of the context's theme. See |
| 159 | * <a href="{@docRoot}guide/topics/resources/available-resources.html#stylesandthemes"> |
| 160 | * Style and Theme Resources</a> for more information about defining and |
| 161 | * using styles. |
Jorim Jaggi | b10e33f | 2015-02-04 21:57:40 +0100 | [diff] [blame] | 162 | * |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 163 | * @param context the context in which the dialog should run |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 164 | * @param themeResId a style resource describing the theme to use for the |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 165 | * window, or {@code 0} to use the default dialog theme |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | */ |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 167 | public Dialog(@NonNull Context context, @StyleRes int themeResId) { |
| 168 | this(context, themeResId, true); |
Dianne Hackborn | e79b554 | 2011-01-27 15:18:46 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 171 | Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) { |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 172 | if (createContextThemeWrapper) { |
Adam Lesinski | 9553fb3 | 2017-05-23 18:53:44 -0700 | [diff] [blame] | 173 | if (themeResId == ResourceId.ID_NULL) { |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 174 | final TypedValue outValue = new TypedValue(); |
| 175 | context.getTheme().resolveAttribute(R.attr.dialogTheme, outValue, true); |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 176 | themeResId = outValue.resourceId; |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 177 | } |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 178 | mContext = new ContextThemeWrapper(context, themeResId); |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 179 | } else { |
| 180 | mContext = context; |
Adam Powell | 2fbf4de6 | 2010-09-30 15:46:46 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 183 | mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 184 | |
| 185 | final Window w = new PhoneWindow(mContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | mWindow = w; |
| 187 | w.setCallback(this); |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 188 | w.setOnWindowDismissedCallback(this); |
Michael Kwan | 67639a5 | 2016-12-16 12:38:10 -0800 | [diff] [blame] | 189 | w.setOnWindowSwipeDismissedCallback(() -> { |
| 190 | if (mCancelable) { |
| 191 | cancel(); |
| 192 | } |
| 193 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | w.setWindowManager(mWindowManager, null, null); |
| 195 | w.setGravity(Gravity.CENTER); |
Alan Viverette | 5696f04 | 2015-04-08 11:03:32 -0700 | [diff] [blame] | 196 | |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 197 | mListenersHandler = new ListenersHandler(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | /** |
| 201 | * @deprecated |
| 202 | * @hide |
| 203 | */ |
| 204 | @Deprecated |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 205 | protected Dialog(@NonNull Context context, boolean cancelable, |
| 206 | @Nullable Message cancelCallback) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | this(context); |
| 208 | mCancelable = cancelable; |
Michael Kwan | f7964be | 2016-11-30 16:44:33 -0800 | [diff] [blame] | 209 | updateWindowForCancelable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | mCancelMessage = cancelCallback; |
| 211 | } |
| 212 | |
Alan Viverette | 682a433 | 2015-04-10 11:05:50 -0700 | [diff] [blame] | 213 | protected Dialog(@NonNull Context context, boolean cancelable, |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 214 | @Nullable OnCancelListener cancelListener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | this(context); |
| 216 | mCancelable = cancelable; |
Michael Kwan | f7964be | 2016-11-30 16:44:33 -0800 | [diff] [blame] | 217 | updateWindowForCancelable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | setOnCancelListener(cancelListener); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Retrieve the Context this Dialog is running in. |
| 223 | * |
Adam Powell | c49c173 | 2010-09-28 16:03:15 -0700 | [diff] [blame] | 224 | * @return Context The Context used by the Dialog. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 226 | public final @NonNull Context getContext() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | return mContext; |
| 228 | } |
| 229 | |
| 230 | /** |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 231 | * Retrieve the {@link ActionBar} attached to this dialog, if present. |
| 232 | * |
| 233 | * @return The ActionBar attached to the dialog or null if no ActionBar is present. |
| 234 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 235 | public @Nullable ActionBar getActionBar() { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 236 | return mActionBar; |
| 237 | } |
| 238 | |
| 239 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | * Sets the Activity that owns this dialog. An example use: This Dialog will |
| 241 | * use the suggested volume control stream of the Activity. |
| 242 | * |
| 243 | * @param activity The Activity that owns this dialog. |
| 244 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 245 | public final void setOwnerActivity(@NonNull Activity activity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | mOwnerActivity = activity; |
| 247 | |
| 248 | getWindow().setVolumeControlStream(mOwnerActivity.getVolumeControlStream()); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns the Activity that owns this Dialog. For example, if |
| 253 | * {@link Activity#showDialog(int)} is used to show this Dialog, that |
| 254 | * Activity will be the owner (by default). Depending on how this dialog was |
| 255 | * created, this may return null. |
| 256 | * |
| 257 | * @return The Activity that owns this Dialog. |
| 258 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 259 | public final @Nullable Activity getOwnerActivity() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | return mOwnerActivity; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @return Whether the dialog is currently showing. |
| 265 | */ |
| 266 | public boolean isShowing() { |
| 267 | return mShowing; |
| 268 | } |
| 269 | |
| 270 | /** |
Alan Viverette | f34ef74 | 2013-12-11 15:59:53 -0800 | [diff] [blame] | 271 | * Forces immediate creation of the dialog. |
| 272 | * <p> |
| 273 | * Note that you should not override this method to perform dialog creation. |
| 274 | * Rather, override {@link #onCreate(Bundle)}. |
| 275 | */ |
| 276 | public void create() { |
| 277 | if (!mCreated) { |
| 278 | dispatchOnCreate(null); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | * Start the dialog and display it on screen. The window is placed in the |
| 284 | * application layer and opaque. Note that you should not override this |
| 285 | * method to do initialization when the dialog is shown, instead implement |
| 286 | * that in {@link #onStart}. |
| 287 | */ |
| 288 | public void show() { |
| 289 | if (mShowing) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 290 | if (mDecor != null) { |
Adam Powell | e67a9dc | 2010-08-17 17:28:56 -0700 | [diff] [blame] | 291 | if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) { |
| 292 | mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR); |
| 293 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 294 | mDecor.setVisibility(View.VISIBLE); |
| 295 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | |
Dianne Hackborn | 8b4cac1 | 2011-01-03 13:58:14 -0800 | [diff] [blame] | 299 | mCanceled = false; |
Robert Carr | d7dbec7 | 2016-10-05 13:13:21 -0700 | [diff] [blame] | 300 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | if (!mCreated) { |
| 302 | dispatchOnCreate(null); |
Robert Carr | d7dbec7 | 2016-10-05 13:13:21 -0700 | [diff] [blame] | 303 | } else { |
| 304 | // Fill the DecorView in on any configuration changes that |
| 305 | // may have occured while it was removed from the WindowManager. |
| 306 | final Configuration config = mContext.getResources().getConfiguration(); |
| 307 | mWindow.getDecorView().dispatchConfigurationChanged(config); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | onStart(); |
| 311 | mDecor = mWindow.getDecorView(); |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 312 | |
| 313 | if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) { |
Adam Powell | 04fe6eb | 2013-05-31 14:39:48 -0700 | [diff] [blame] | 314 | final ApplicationInfo info = mContext.getApplicationInfo(); |
| 315 | mWindow.setDefaultIcon(info.icon); |
| 316 | mWindow.setDefaultLogo(info.logo); |
Adam Powell | e43340c | 2014-03-17 19:10:43 -0700 | [diff] [blame] | 317 | mActionBar = new WindowDecorActionBar(this); |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 318 | } |
| 319 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | WindowManager.LayoutParams l = mWindow.getAttributes(); |
| 321 | if ((l.softInputMode |
| 322 | & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) { |
| 323 | WindowManager.LayoutParams nl = new WindowManager.LayoutParams(); |
| 324 | nl.copyFrom(l); |
| 325 | nl.softInputMode |= |
| 326 | WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION; |
| 327 | l = nl; |
| 328 | } |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 329 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 330 | mWindowManager.addView(mDecor, l); |
| 331 | mShowing = true; |
| 332 | |
| 333 | sendShowMessage(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Hide the dialog, but do not dismiss it. |
| 338 | */ |
| 339 | public void hide() { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 340 | if (mDecor != null) { |
| 341 | mDecor.setVisibility(View.GONE); |
| 342 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Dismiss this dialog, removing it from the screen. This method can be |
| 347 | * invoked safely from any thread. Note that you should not override this |
| 348 | * method to do cleanup when the dialog is dismissed, instead implement |
| 349 | * that in {@link #onStop}. |
| 350 | */ |
Craig Mautner | 92098c7 | 2013-06-10 11:27:26 -0700 | [diff] [blame] | 351 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | public void dismiss() { |
Svetoslav Ganov | 44bfdd8 | 2012-01-26 10:00:18 -0800 | [diff] [blame] | 353 | if (Looper.myLooper() == mHandler.getLooper()) { |
| 354 | dismissDialog(); |
| 355 | } else { |
| 356 | mHandler.post(mDismissAction); |
| 357 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Adam Powell | 89fc3ac | 2011-11-01 18:00:44 -0700 | [diff] [blame] | 360 | void dismissDialog() { |
Romain Guy | 08a4ac3 | 2010-03-16 11:40:40 -0700 | [diff] [blame] | 361 | if (mDecor == null || !mShowing) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | return; |
| 363 | } |
| 364 | |
Adam Powell | 89fc3ac | 2011-11-01 18:00:44 -0700 | [diff] [blame] | 365 | if (mWindow.isDestroyed()) { |
| 366 | Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!"); |
| 367 | return; |
| 368 | } |
| 369 | |
Romain Guy | d2671e1 | 2010-03-11 18:06:42 -0800 | [diff] [blame] | 370 | try { |
Craig Mautner | 92098c7 | 2013-06-10 11:27:26 -0700 | [diff] [blame] | 371 | mWindowManager.removeViewImmediate(mDecor); |
Romain Guy | d2671e1 | 2010-03-11 18:06:42 -0800 | [diff] [blame] | 372 | } finally { |
Adam Powell | cfe9aee | 2011-11-01 14:56:27 -0700 | [diff] [blame] | 373 | if (mActionMode != null) { |
| 374 | mActionMode.finish(); |
| 375 | } |
Romain Guy | d2671e1 | 2010-03-11 18:06:42 -0800 | [diff] [blame] | 376 | mDecor = null; |
| 377 | mWindow.closeAllPanels(); |
| 378 | onStop(); |
| 379 | mShowing = false; |
Craig Mautner | 92098c7 | 2013-06-10 11:27:26 -0700 | [diff] [blame] | 380 | |
Romain Guy | d2671e1 | 2010-03-11 18:06:42 -0800 | [diff] [blame] | 381 | sendDismissMessage(); |
| 382 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | private void sendDismissMessage() { |
| 386 | if (mDismissMessage != null) { |
| 387 | // Obtain a new message so this dialog can be re-used |
| 388 | Message.obtain(mDismissMessage).sendToTarget(); |
| 389 | } |
| 390 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 391 | |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 392 | private void sendShowMessage() { |
| 393 | if (mShowMessage != null) { |
| 394 | // Obtain a new message so this dialog can be re-used |
| 395 | Message.obtain(mShowMessage).sendToTarget(); |
| 396 | } |
| 397 | } |
| 398 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 399 | // internal method to make sure mCreated is set properly without requiring |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | // users to call through to super in onCreate |
| 401 | void dispatchOnCreate(Bundle savedInstanceState) { |
Alan Viverette | f34ef74 | 2013-12-11 15:59:53 -0800 | [diff] [blame] | 402 | if (!mCreated) { |
Romain Guy | 6de4aed | 2009-07-08 10:54:45 -0700 | [diff] [blame] | 403 | onCreate(savedInstanceState); |
| 404 | mCreated = true; |
| 405 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /** |
Chet Haase | 49afa5b | 2010-08-23 11:39:53 -0700 | [diff] [blame] | 409 | * Similar to {@link Activity#onCreate}, you should initialize your dialog |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | * in this method, including calling {@link #setContentView}. |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 411 | * @param savedInstanceState If this dialog is being reinitialized after a |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | * the hosting activity was previously shut down, holds the result from |
| 413 | * the most recent call to {@link #onSaveInstanceState}, or null if this |
| 414 | * is the first time. |
| 415 | */ |
| 416 | protected void onCreate(Bundle savedInstanceState) { |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Called when the dialog is starting. |
| 421 | */ |
| 422 | protected void onStart() { |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 423 | if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Called to tell you that you're stopping. |
| 428 | */ |
| 429 | protected void onStop() { |
Adam Powell | 50efbed | 2011-02-08 16:20:15 -0800 | [diff] [blame] | 430 | if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | private static final String DIALOG_SHOWING_TAG = "android:dialogShowing"; |
| 434 | private static final String DIALOG_HIERARCHY_TAG = "android:dialogHierarchy"; |
| 435 | |
| 436 | /** |
| 437 | * Saves the state of the dialog into a bundle. |
| 438 | * |
| 439 | * The default implementation saves the state of its view hierarchy, so you'll |
| 440 | * likely want to call through to super if you override this to save additional |
| 441 | * state. |
| 442 | * @return A bundle with the state of the dialog. |
| 443 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 444 | public @NonNull Bundle onSaveInstanceState() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | Bundle bundle = new Bundle(); |
| 446 | bundle.putBoolean(DIALOG_SHOWING_TAG, mShowing); |
| 447 | if (mCreated) { |
| 448 | bundle.putBundle(DIALOG_HIERARCHY_TAG, mWindow.saveHierarchyState()); |
| 449 | } |
| 450 | return bundle; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Restore the state of the dialog from a previously saved bundle. |
| 455 | * |
| 456 | * The default implementation restores the state of the dialog's view |
| 457 | * hierarchy that was saved in the default implementation of {@link #onSaveInstanceState()}, |
| 458 | * so be sure to call through to super when overriding unless you want to |
| 459 | * do all restoring of state yourself. |
| 460 | * @param savedInstanceState The state of the dialog previously saved by |
| 461 | * {@link #onSaveInstanceState()}. |
| 462 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 463 | public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | final Bundle dialogHierarchyState = savedInstanceState.getBundle(DIALOG_HIERARCHY_TAG); |
| 465 | if (dialogHierarchyState == null) { |
| 466 | // dialog has never been shown, or onCreated, nothing to restore. |
| 467 | return; |
| 468 | } |
| 469 | dispatchOnCreate(savedInstanceState); |
| 470 | mWindow.restoreHierarchyState(dialogHierarchyState); |
| 471 | if (savedInstanceState.getBoolean(DIALOG_SHOWING_TAG)) { |
| 472 | show(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Retrieve the current Window for the activity. This can be used to |
| 478 | * directly access parts of the Window API that are not available |
| 479 | * through Activity/Screen. |
| 480 | * |
| 481 | * @return Window The current window, or null if the activity is not |
| 482 | * visual. |
| 483 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 484 | public @Nullable Window getWindow() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | return mWindow; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Call {@link android.view.Window#getCurrentFocus} on the |
| 490 | * Window if this Activity to return the currently focused view. |
| 491 | * |
| 492 | * @return View The current View with focus or null. |
| 493 | * |
| 494 | * @see #getWindow |
| 495 | * @see android.view.Window#getCurrentFocus |
| 496 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 497 | public @Nullable View getCurrentFocus() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | return mWindow != null ? mWindow.getCurrentFocus() : null; |
| 499 | } |
| 500 | |
| 501 | /** |
Alan Viverette | db7423c | 2017-03-31 13:13:58 -0400 | [diff] [blame] | 502 | * Finds the first descendant view with the given ID or {@code null} if the |
| 503 | * ID is invalid (< 0), there is no matching view in the hierarchy, or the |
| 504 | * dialog has not yet been fully created (for example, via {@link #show()} |
| 505 | * or {@link #create()}). |
| 506 | * <p> |
| 507 | * <strong>Note:</strong> In most cases -- depending on compiler support -- |
| 508 | * the resulting view is automatically cast to the target class type. If |
| 509 | * the target class type is unconstrained, an explicit cast may be |
| 510 | * necessary. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | * |
Alan Viverette | db7423c | 2017-03-31 13:13:58 -0400 | [diff] [blame] | 512 | * @param id the ID to search for |
| 513 | * @return a view with given ID if found, or {@code null} otherwise |
| 514 | * @see View#findViewById(int) |
Chris Craik | 6faa9e5 | 2018-01-11 10:46:10 -0800 | [diff] [blame] | 515 | * @see Dialog#requireViewById(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | */ |
Alan Viverette | db7423c | 2017-03-31 13:13:58 -0400 | [diff] [blame] | 517 | @Nullable |
| 518 | public <T extends View> T findViewById(@IdRes int id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | return mWindow.findViewById(id); |
| 520 | } |
| 521 | |
| 522 | /** |
Chris Craik | 6faa9e5 | 2018-01-11 10:46:10 -0800 | [diff] [blame] | 523 | * Finds the first descendant view with the given ID or throws an IllegalArgumentException if |
| 524 | * the ID is invalid (< 0), there is no matching view in the hierarchy, or the dialog has not |
| 525 | * yet been fully created (for example, via {@link #show()} or {@link #create()}). |
| 526 | * <p> |
| 527 | * <strong>Note:</strong> In most cases -- depending on compiler support -- |
| 528 | * the resulting view is automatically cast to the target class type. If |
| 529 | * the target class type is unconstrained, an explicit cast may be |
| 530 | * necessary. |
| 531 | * |
| 532 | * @param id the ID to search for |
| 533 | * @return a view with given ID |
| 534 | * @see View#requireViewById(int) |
| 535 | * @see Dialog#findViewById(int) |
| 536 | */ |
| 537 | @NonNull |
| 538 | public final <T extends View> T requireViewById(@IdRes int id) { |
| 539 | T view = findViewById(id); |
| 540 | if (view == null) { |
| 541 | throw new IllegalArgumentException("ID does not reference a View inside this Dialog"); |
| 542 | } |
| 543 | return view; |
| 544 | } |
| 545 | |
| 546 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | * Set the screen content from a layout resource. The resource will be |
| 548 | * inflated, adding all top-level views to the screen. |
| 549 | * |
| 550 | * @param layoutResID Resource ID to be inflated. |
| 551 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 552 | public void setContentView(@LayoutRes int layoutResID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | mWindow.setContentView(layoutResID); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Set the screen content to an explicit view. This view is placed |
| 558 | * directly into the screen's view hierarchy. It can itself be a complex |
Alan Viverette | ec18670 | 2013-12-05 11:10:31 -0800 | [diff] [blame] | 559 | * view hierarchy. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | * |
| 561 | * @param view The desired content to display. |
| 562 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 563 | public void setContentView(@NonNull View view) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | mWindow.setContentView(view); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Set the screen content to an explicit view. This view is placed |
| 569 | * directly into the screen's view hierarchy. It can itself be a complex |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 570 | * view hierarchy. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | * |
| 572 | * @param view The desired content to display. |
| 573 | * @param params Layout parameters for the view. |
| 574 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 575 | public void setContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | mWindow.setContentView(view, params); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Add an additional content view to the screen. Added after any existing |
| 581 | * ones in the screen -- existing views are NOT removed. |
| 582 | * |
| 583 | * @param view The desired content to display. |
| 584 | * @param params Layout parameters for the view. |
| 585 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 586 | public void addContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | mWindow.addContentView(view, params); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Set the title text for this dialog's window. |
| 592 | * |
| 593 | * @param title The new text to display in the title. |
| 594 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 595 | public void setTitle(@Nullable CharSequence title) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | mWindow.setTitle(title); |
| 597 | mWindow.getAttributes().setTitle(title); |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Set the title text for this dialog's window. The text is retrieved |
| 602 | * from the resources with the supplied identifier. |
| 603 | * |
| 604 | * @param titleId the title's text resource identifier |
| 605 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 606 | public void setTitle(@StringRes int titleId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | setTitle(mContext.getText(titleId)); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * A key was pressed down. |
Emilie Roberts | c1c572e | 2018-01-31 17:33:32 +0000 | [diff] [blame] | 612 | * <p> |
| 613 | * If the focused view didn't want this event, this method is called. |
| 614 | * <p> |
| 615 | * Default implementation consumes {@link KeyEvent#KEYCODE_BACK KEYCODE_BACK} |
| 616 | * and, as of {@link android.os.Build.VERSION_CODES#P P}, {@link KeyEvent#KEYCODE_ESCAPE |
| 617 | * KEYCODE_ESCAPE} to later handle them in {@link #onKeyUp}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | * @see #onKeyUp |
| 620 | * @see android.view.KeyEvent |
| 621 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 622 | @Override |
| 623 | public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) { |
Emilie Roberts | c1c572e | 2018-01-31 17:33:32 +0000 | [diff] [blame] | 624 | if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 625 | event.startTracking(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | return true; |
| 627 | } |
| 628 | |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 633 | * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent) |
| 634 | * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle |
| 635 | * the event). |
| 636 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 637 | @Override |
| 638 | public boolean onKeyLongPress(int keyCode, @NonNull KeyEvent event) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 639 | return false; |
| 640 | } |
| 641 | |
| 642 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | * A key was released. |
Emilie Roberts | c1c572e | 2018-01-31 17:33:32 +0000 | [diff] [blame] | 644 | * <p> |
| 645 | * Default implementation consumes {@link KeyEvent#KEYCODE_BACK KEYCODE_BACK} |
| 646 | * and, as of {@link android.os.Build.VERSION_CODES#P P}, {@link KeyEvent#KEYCODE_ESCAPE |
| 647 | * KEYCODE_ESCAPE} to close the dialog. |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 648 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | * @see #onKeyDown |
Emilie Roberts | c1c572e | 2018-01-31 17:33:32 +0000 | [diff] [blame] | 650 | * @see android.view.KeyEvent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 652 | @Override |
| 653 | public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) { |
Emilie Roberts | c1c572e | 2018-01-31 17:33:32 +0000 | [diff] [blame] | 654 | if ((keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) |
| 655 | && event.isTracking() |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 656 | && !event.isCanceled()) { |
| 657 | onBackPressed(); |
| 658 | return true; |
| 659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | return false; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent) |
| 665 | * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle |
| 666 | * the event). |
| 667 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 668 | @Override |
| 669 | public boolean onKeyMultiple(int keyCode, int repeatCount, @NonNull KeyEvent event) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | return false; |
| 671 | } |
| 672 | |
| 673 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 674 | * Called when the dialog has detected the user's press of the back |
| 675 | * key. The default implementation simply cancels the dialog (only if |
| 676 | * it is cancelable), but you can override this to do whatever you want. |
| 677 | */ |
| 678 | public void onBackPressed() { |
| 679 | if (mCancelable) { |
| 680 | cancel(); |
| 681 | } |
| 682 | } |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 683 | |
| 684 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 685 | * Called when a key shortcut event is not handled by any of the views in the Dialog. |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 686 | * Override this method to implement global key shortcuts for the Dialog. |
| 687 | * Key shortcuts can also be implemented by setting the |
| 688 | * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items. |
| 689 | * |
| 690 | * @param keyCode The value in event.getKeyCode(). |
| 691 | * @param event Description of the key event. |
| 692 | * @return True if the key shortcut was handled. |
| 693 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 694 | public boolean onKeyShortcut(int keyCode, @NonNull KeyEvent event) { |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 695 | return false; |
| 696 | } |
| 697 | |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 698 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | * Called when a touch screen event was not handled by any of the views |
| 700 | * under it. This is most useful to process touch events that happen outside |
| 701 | * of your window bounds, where there is no view to receive it. |
| 702 | * |
| 703 | * @param event The touch screen event being processed. |
| 704 | * @return Return true if you have consumed the event, false if you haven't. |
| 705 | * The default implementation will cancel the dialog when a touch |
| 706 | * happens outside of the window bounds. |
| 707 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 708 | public boolean onTouchEvent(@NonNull MotionEvent event) { |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 709 | if (mCancelable && mShowing && mWindow.shouldCloseOnTouch(mContext, event)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | cancel(); |
| 711 | return true; |
| 712 | } |
| 713 | |
| 714 | return false; |
| 715 | } |
| 716 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | /** |
| 718 | * Called when the trackball was moved and not handled by any of the |
| 719 | * views inside of the activity. So, for example, if the trackball moves |
| 720 | * while focus is on a button, you will receive a call here because |
| 721 | * buttons do not normally do anything with trackball events. The call |
| 722 | * here happens <em>before</em> trackball movements are converted to |
| 723 | * DPAD key events, which then get sent back to the view hierarchy, and |
| 724 | * will be processed at the point for things like focus navigation. |
| 725 | * |
| 726 | * @param event The trackball event being processed. |
| 727 | * |
| 728 | * @return Return true if you have consumed the event, false if you haven't. |
| 729 | * The default implementation always returns false. |
| 730 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 731 | public boolean onTrackballEvent(@NonNull MotionEvent event) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | return false; |
| 733 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 734 | |
| 735 | /** |
| 736 | * Called when a generic motion event was not handled by any of the |
| 737 | * views inside of the dialog. |
| 738 | * <p> |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 739 | * Generic motion events describe joystick movements, mouse hovers, track pad |
| 740 | * touches, scroll wheel movements and other input events. The |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 741 | * {@link MotionEvent#getSource() source} of the motion event specifies |
| 742 | * the class of input that was received. Implementations of this method |
| 743 | * must examine the bits in the source before processing the event. |
| 744 | * The following code example shows how this is done. |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 745 | * </p><p> |
| 746 | * Generic motion events with source class |
| 747 | * {@link android.view.InputDevice#SOURCE_CLASS_POINTER} |
| 748 | * are delivered to the view under the pointer. All other generic motion events are |
| 749 | * delivered to the focused view. |
| 750 | * </p><p> |
| 751 | * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to |
| 752 | * handle this event. |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 753 | * </p> |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 754 | * |
| 755 | * @param event The generic motion event being processed. |
| 756 | * |
| 757 | * @return Return true if you have consumed the event, false if you haven't. |
| 758 | * The default implementation always returns false. |
| 759 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 760 | public boolean onGenericMotionEvent(@NonNull MotionEvent event) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 761 | return false; |
| 762 | } |
| 763 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 764 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | public void onWindowAttributesChanged(WindowManager.LayoutParams params) { |
| 766 | if (mDecor != null) { |
| 767 | mWindowManager.updateViewLayout(mDecor, params); |
| 768 | } |
| 769 | } |
| 770 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 771 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | public void onContentChanged() { |
| 773 | } |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 774 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 775 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | public void onWindowFocusChanged(boolean hasFocus) { |
| 777 | } |
| 778 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 779 | @Override |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 780 | public void onAttachedToWindow() { |
| 781 | } |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 782 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 783 | @Override |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 784 | public void onDetachedFromWindow() { |
| 785 | } |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 786 | |
Adam Powell | 117b695 | 2014-05-05 18:14:56 -0700 | [diff] [blame] | 787 | /** @hide */ |
| 788 | @Override |
Ned Burns | 7d6cb91 | 2016-12-02 17:25:33 -0500 | [diff] [blame] | 789 | public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) { |
Will Haldean Brown | ca6234e | 2014-02-12 10:23:41 -0800 | [diff] [blame] | 790 | dismiss(); |
| 791 | } |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 792 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | /** |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 794 | * Called to process key events. You can override this to intercept all |
| 795 | * key events before they are dispatched to the window. Be sure to call |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | * this implementation for key events that should be handled normally. |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 797 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | * @param event The key event. |
Wale Ogunwale | ba7881c | 2015-08-01 19:28:29 -0700 | [diff] [blame] | 799 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | * @return boolean Return true if this event was consumed. |
| 801 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 802 | @Override |
| 803 | public boolean dispatchKeyEvent(@NonNull KeyEvent event) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | if ((mOnKeyListener != null) && (mOnKeyListener.onKey(this, event.getKeyCode(), event))) { |
| 805 | return true; |
| 806 | } |
| 807 | if (mWindow.superDispatchKeyEvent(event)) { |
| 808 | return true; |
| 809 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 810 | return event.dispatch(this, mDecor != null |
| 811 | ? mDecor.getKeyDispatcherState() : null, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /** |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 815 | * Called to process a key shortcut event. |
| 816 | * You can override this to intercept all key shortcut events before they are |
| 817 | * dispatched to the window. Be sure to call this implementation for key shortcut |
| 818 | * events that should be handled normally. |
| 819 | * |
| 820 | * @param event The key shortcut event. |
| 821 | * @return True if this event was consumed. |
| 822 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 823 | @Override |
| 824 | public boolean dispatchKeyShortcutEvent(@NonNull KeyEvent event) { |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 825 | if (mWindow.superDispatchKeyShortcutEvent(event)) { |
| 826 | return true; |
| 827 | } |
| 828 | return onKeyShortcut(event.getKeyCode(), event); |
| 829 | } |
| 830 | |
| 831 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | * Called to process touch screen events. You can override this to |
| 833 | * intercept all touch screen events before they are dispatched to the |
| 834 | * window. Be sure to call this implementation for touch screen events |
| 835 | * that should be handled normally. |
| 836 | * |
| 837 | * @param ev The touch screen event. |
| 838 | * |
| 839 | * @return boolean Return true if this event was consumed. |
| 840 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 841 | @Override |
| 842 | public boolean dispatchTouchEvent(@NonNull MotionEvent ev) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | if (mWindow.superDispatchTouchEvent(ev)) { |
| 844 | return true; |
| 845 | } |
| 846 | return onTouchEvent(ev); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Called to process trackball events. You can override this to |
| 851 | * intercept all trackball events before they are dispatched to the |
| 852 | * window. Be sure to call this implementation for trackball events |
| 853 | * that should be handled normally. |
| 854 | * |
| 855 | * @param ev The trackball event. |
| 856 | * |
| 857 | * @return boolean Return true if this event was consumed. |
| 858 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 859 | @Override |
| 860 | public boolean dispatchTrackballEvent(@NonNull MotionEvent ev) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | if (mWindow.superDispatchTrackballEvent(ev)) { |
| 862 | return true; |
| 863 | } |
| 864 | return onTrackballEvent(ev); |
| 865 | } |
| 866 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 867 | /** |
| 868 | * Called to process generic motion events. You can override this to |
| 869 | * intercept all generic motion events before they are dispatched to the |
| 870 | * window. Be sure to call this implementation for generic motion events |
| 871 | * that should be handled normally. |
| 872 | * |
| 873 | * @param ev The generic motion event. |
| 874 | * |
| 875 | * @return boolean Return true if this event was consumed. |
| 876 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 877 | @Override |
| 878 | public boolean dispatchGenericMotionEvent(@NonNull MotionEvent ev) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 879 | if (mWindow.superDispatchGenericMotionEvent(ev)) { |
| 880 | return true; |
| 881 | } |
| 882 | return onGenericMotionEvent(ev); |
| 883 | } |
| 884 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 885 | @Override |
| 886 | public boolean dispatchPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 887 | event.setClassName(getClass().getName()); |
| 888 | event.setPackageName(mContext.getPackageName()); |
| 889 | |
| 890 | LayoutParams params = getWindow().getAttributes(); |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 891 | boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) && |
| 892 | (params.height == LayoutParams.MATCH_PARENT); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 893 | event.setFullScreen(isFullScreen); |
| 894 | |
| 895 | return false; |
| 896 | } |
| 897 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | /** |
| 899 | * @see Activity#onCreatePanelView(int) |
| 900 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 901 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | public View onCreatePanelView(int featureId) { |
| 903 | return null; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * @see Activity#onCreatePanelMenu(int, Menu) |
| 908 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 909 | @Override |
| 910 | public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | if (featureId == Window.FEATURE_OPTIONS_PANEL) { |
| 912 | return onCreateOptionsMenu(menu); |
| 913 | } |
| 914 | |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * @see Activity#onPreparePanel(int, View, Menu) |
| 920 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 921 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | public boolean onPreparePanel(int featureId, View view, Menu menu) { |
| 923 | if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 924 | return onPrepareOptionsMenu(menu) && menu.hasVisibleItems(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | } |
| 926 | return true; |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * @see Activity#onMenuOpened(int, Menu) |
| 931 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 932 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | public boolean onMenuOpened(int featureId, Menu menu) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 934 | if (featureId == Window.FEATURE_ACTION_BAR) { |
| 935 | mActionBar.dispatchMenuVisibilityChanged(true); |
| 936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | return true; |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * @see Activity#onMenuItemSelected(int, MenuItem) |
| 942 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 943 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 944 | public boolean onMenuItemSelected(int featureId, MenuItem item) { |
| 945 | return false; |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * @see Activity#onPanelClosed(int, Menu) |
| 950 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 951 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | public void onPanelClosed(int featureId, Menu menu) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 953 | if (featureId == Window.FEATURE_ACTION_BAR) { |
| 954 | mActionBar.dispatchMenuVisibilityChanged(false); |
| 955 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /** |
| 959 | * It is usually safe to proxy this call to the owner activity's |
| 960 | * {@link Activity#onCreateOptionsMenu(Menu)} if the client desires the same |
| 961 | * menu for this Dialog. |
| 962 | * |
| 963 | * @see Activity#onCreateOptionsMenu(Menu) |
| 964 | * @see #getOwnerActivity() |
| 965 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 966 | public boolean onCreateOptionsMenu(@NonNull Menu menu) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | return true; |
| 968 | } |
| 969 | |
| 970 | /** |
| 971 | * It is usually safe to proxy this call to the owner activity's |
| 972 | * {@link Activity#onPrepareOptionsMenu(Menu)} if the client desires the |
| 973 | * same menu for this Dialog. |
| 974 | * |
| 975 | * @see Activity#onPrepareOptionsMenu(Menu) |
| 976 | * @see #getOwnerActivity() |
| 977 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 978 | public boolean onPrepareOptionsMenu(@NonNull Menu menu) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | return true; |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * @see Activity#onOptionsItemSelected(MenuItem) |
| 984 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 985 | public boolean onOptionsItemSelected(@NonNull MenuItem item) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | return false; |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * @see Activity#onOptionsMenuClosed(Menu) |
| 991 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 992 | public void onOptionsMenuClosed(@NonNull Menu menu) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /** |
| 996 | * @see Activity#openOptionsMenu() |
| 997 | */ |
| 998 | public void openOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 999 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) { |
| 1000 | mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null); |
| 1001 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | } |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 1003 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | /** |
| 1005 | * @see Activity#closeOptionsMenu() |
| 1006 | */ |
| 1007 | public void closeOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 1008 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) { |
| 1009 | mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL); |
| 1010 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | /** |
Adam Powell | e67a9dc | 2010-08-17 17:28:56 -0700 | [diff] [blame] | 1014 | * @see Activity#invalidateOptionsMenu() |
| 1015 | */ |
| 1016 | public void invalidateOptionsMenu() { |
Jose Lima | 7a22fc6 | 2015-01-23 17:24:22 -0800 | [diff] [blame] | 1017 | if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) { |
| 1018 | mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); |
| 1019 | } |
Adam Powell | e67a9dc | 2010-08-17 17:28:56 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | * @see Activity#onCreateContextMenu(ContextMenu, View, ContextMenuInfo) |
| 1024 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1025 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * @see Activity#registerForContextMenu(View) |
| 1031 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1032 | public void registerForContextMenu(@NonNull View view) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | view.setOnCreateContextMenuListener(this); |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * @see Activity#unregisterForContextMenu(View) |
| 1038 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1039 | public void unregisterForContextMenu(@NonNull View view) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | view.setOnCreateContextMenuListener(null); |
| 1041 | } |
| 1042 | |
| 1043 | /** |
| 1044 | * @see Activity#openContextMenu(View) |
| 1045 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1046 | public void openContextMenu(@NonNull View view) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | view.showContextMenu(); |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * @see Activity#onContextItemSelected(MenuItem) |
| 1052 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1053 | public boolean onContextItemSelected(@NonNull MenuItem item) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | return false; |
| 1055 | } |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 1056 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | /** |
| 1058 | * @see Activity#onContextMenuClosed(Menu) |
| 1059 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1060 | public void onContextMenuClosed(@NonNull Menu menu) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | } |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 1062 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | /** |
| 1064 | * This hook is called when the user signals the desire to start a search. |
| 1065 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1066 | @Override |
| 1067 | public boolean onSearchRequested(@NonNull SearchEvent searchEvent) { |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 1068 | mSearchEvent = searchEvent; |
| 1069 | return onSearchRequested(); |
| 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * This hook is called when the user signals the desire to start a search. |
| 1074 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1075 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1076 | public boolean onSearchRequested() { |
Karl Rosaen | 53d24af | 2009-07-14 14:58:10 -0700 | [diff] [blame] | 1077 | final SearchManager searchManager = (SearchManager) mContext |
| 1078 | .getSystemService(Context.SEARCH_SERVICE); |
| 1079 | |
Bjorn Bringert | b8144a9 | 2010-02-22 20:48:57 +0000 | [diff] [blame] | 1080 | // associate search with owner activity |
Karl Rosaen | 7bafed8 | 2009-09-04 11:15:21 -0700 | [diff] [blame] | 1081 | final ComponentName appName = getAssociatedActivity(); |
lge-aosp | 06ca997 | 2011-03-15 10:25:57 +0900 | [diff] [blame] | 1082 | if (appName != null && searchManager.getSearchableInfo(appName) != null) { |
Bjorn Bringert | b8144a9 | 2010-02-22 20:48:57 +0000 | [diff] [blame] | 1083 | searchManager.startSearch(null, false, appName, null, false); |
| 1084 | dismiss(); |
| 1085 | return true; |
| 1086 | } else { |
| 1087 | return false; |
| 1088 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | } |
| 1090 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 1091 | /** |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 1092 | * During the onSearchRequested() callbacks, this function will return the |
| 1093 | * {@link SearchEvent} that triggered the callback, if it exists. |
| 1094 | * |
| 1095 | * @return SearchEvent The SearchEvent that triggered the {@link |
| 1096 | * #onSearchRequested} callback. |
| 1097 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1098 | public final @Nullable SearchEvent getSearchEvent() { |
Tim Kilbourn | 6a975b3 | 2015-04-09 17:14:34 -0700 | [diff] [blame] | 1099 | return mSearchEvent; |
| 1100 | } |
| 1101 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 1102 | @Override |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 1103 | public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 1104 | if (mActionBar != null && mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) { |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 1105 | return mActionBar.startActionMode(callback); |
| 1106 | } |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 1107 | return null; |
| 1108 | } |
| 1109 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 1110 | @Override |
| 1111 | public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) { |
| 1112 | try { |
| 1113 | mActionModeTypeStarting = type; |
| 1114 | return onWindowStartingActionMode(callback); |
| 1115 | } finally { |
| 1116 | mActionModeTypeStarting = ActionMode.TYPE_PRIMARY; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * {@inheritDoc} |
Adam Powell | cfe9aee | 2011-11-01 14:56:27 -0700 | [diff] [blame] | 1122 | * |
| 1123 | * Note that if you override this method you should always call through |
| 1124 | * to the superclass implementation by calling super.onActionModeStarted(mode). |
| 1125 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1126 | @Override |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1127 | @CallSuper |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 1128 | public void onActionModeStarted(ActionMode mode) { |
Adam Powell | cfe9aee | 2011-11-01 14:56:27 -0700 | [diff] [blame] | 1129 | mActionMode = mode; |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Adam Powell | cfe9aee | 2011-11-01 14:56:27 -0700 | [diff] [blame] | 1132 | /** |
| 1133 | * {@inheritDoc} |
| 1134 | * |
| 1135 | * Note that if you override this method you should always call through |
| 1136 | * to the superclass implementation by calling super.onActionModeFinished(mode). |
| 1137 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1138 | @Override |
Tor Norbye | c615c6f | 2015-03-02 10:11:44 -0800 | [diff] [blame] | 1139 | @CallSuper |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 1140 | public void onActionModeFinished(ActionMode mode) { |
Adam Powell | cfe9aee | 2011-11-01 14:56:27 -0700 | [diff] [blame] | 1141 | if (mode == mActionMode) { |
| 1142 | mActionMode = null; |
| 1143 | } |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Karl Rosaen | 7bafed8 | 2009-09-04 11:15:21 -0700 | [diff] [blame] | 1146 | /** |
Adam Powell | dec9dfd | 2010-08-09 15:27:54 -0700 | [diff] [blame] | 1147 | * @return The activity associated with this dialog, or null if there is no associated activity. |
Karl Rosaen | 7bafed8 | 2009-09-04 11:15:21 -0700 | [diff] [blame] | 1148 | */ |
| 1149 | private ComponentName getAssociatedActivity() { |
| 1150 | Activity activity = mOwnerActivity; |
| 1151 | Context context = getContext(); |
| 1152 | while (activity == null && context != null) { |
| 1153 | if (context instanceof Activity) { |
| 1154 | activity = (Activity) context; // found it! |
| 1155 | } else { |
| 1156 | context = (context instanceof ContextWrapper) ? |
| 1157 | ((ContextWrapper) context).getBaseContext() : // unwrap one level |
| 1158 | null; // done |
| 1159 | } |
| 1160 | } |
| 1161 | return activity == null ? null : activity.getComponentName(); |
| 1162 | } |
| 1163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | |
| 1165 | /** |
| 1166 | * Request that key events come to this dialog. Use this if your |
| 1167 | * dialog has no views with focus, but the dialog still wants |
| 1168 | * a chance to process key events. |
| 1169 | * |
| 1170 | * @param get true if the dialog should receive key events, false otherwise |
| 1171 | * @see android.view.Window#takeKeyEvents |
| 1172 | */ |
| 1173 | public void takeKeyEvents(boolean get) { |
| 1174 | mWindow.takeKeyEvents(get); |
| 1175 | } |
| 1176 | |
| 1177 | /** |
| 1178 | * Enable extended window features. This is a convenience for calling |
| 1179 | * {@link android.view.Window#requestFeature getWindow().requestFeature()}. |
| 1180 | * |
| 1181 | * @param featureId The desired feature as defined in |
| 1182 | * {@link android.view.Window}. |
| 1183 | * @return Returns true if the requested feature is supported and now |
| 1184 | * enabled. |
| 1185 | * |
| 1186 | * @see android.view.Window#requestFeature |
| 1187 | */ |
| 1188 | public final boolean requestWindowFeature(int featureId) { |
| 1189 | return getWindow().requestFeature(featureId); |
| 1190 | } |
| 1191 | |
| 1192 | /** |
| 1193 | * Convenience for calling |
| 1194 | * {@link android.view.Window#setFeatureDrawableResource}. |
| 1195 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 1196 | public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | getWindow().setFeatureDrawableResource(featureId, resId); |
| 1198 | } |
| 1199 | |
| 1200 | /** |
| 1201 | * Convenience for calling |
| 1202 | * {@link android.view.Window#setFeatureDrawableUri}. |
| 1203 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1204 | public final void setFeatureDrawableUri(int featureId, @Nullable Uri uri) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1205 | getWindow().setFeatureDrawableUri(featureId, uri); |
| 1206 | } |
| 1207 | |
| 1208 | /** |
| 1209 | * Convenience for calling |
| 1210 | * {@link android.view.Window#setFeatureDrawable(int, Drawable)}. |
| 1211 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1212 | public final void setFeatureDrawable(int featureId, @Nullable Drawable drawable) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | getWindow().setFeatureDrawable(featureId, drawable); |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * Convenience for calling |
| 1218 | * {@link android.view.Window#setFeatureDrawableAlpha}. |
| 1219 | */ |
| 1220 | public final void setFeatureDrawableAlpha(int featureId, int alpha) { |
| 1221 | getWindow().setFeatureDrawableAlpha(featureId, alpha); |
| 1222 | } |
| 1223 | |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1224 | public @NonNull LayoutInflater getLayoutInflater() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1225 | return getWindow().getLayoutInflater(); |
| 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | * Sets whether this dialog is cancelable with the |
| 1230 | * {@link KeyEvent#KEYCODE_BACK BACK} key. |
| 1231 | */ |
| 1232 | public void setCancelable(boolean flag) { |
| 1233 | mCancelable = flag; |
Michael Kwan | f7964be | 2016-11-30 16:44:33 -0800 | [diff] [blame] | 1234 | updateWindowForCancelable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /** |
| 1238 | * Sets whether this dialog is canceled when touched outside the window's |
| 1239 | * bounds. If setting to true, the dialog is set to be cancelable if not |
| 1240 | * already set. |
| 1241 | * |
| 1242 | * @param cancel Whether the dialog should be canceled when touched outside |
| 1243 | * the window. |
| 1244 | */ |
| 1245 | public void setCanceledOnTouchOutside(boolean cancel) { |
| 1246 | if (cancel && !mCancelable) { |
| 1247 | mCancelable = true; |
Michael Kwan | f7964be | 2016-11-30 16:44:33 -0800 | [diff] [blame] | 1248 | updateWindowForCancelable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1249 | } |
| 1250 | |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 1251 | mWindow.setCloseOnTouchOutside(cancel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | /** |
| 1255 | * Cancel the dialog. This is essentially the same as calling {@link #dismiss()}, but it will |
| 1256 | * also call your {@link DialogInterface.OnCancelListener} (if registered). |
| 1257 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1258 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | public void cancel() { |
Dianne Hackborn | 8b4cac1 | 2011-01-03 13:58:14 -0800 | [diff] [blame] | 1260 | if (!mCanceled && mCancelMessage != null) { |
| 1261 | mCanceled = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | // Obtain a new message so this dialog can be re-used |
| 1263 | Message.obtain(mCancelMessage).sendToTarget(); |
| 1264 | } |
| 1265 | dismiss(); |
| 1266 | } |
| 1267 | |
| 1268 | /** |
| 1269 | * Set a listener to be invoked when the dialog is canceled. |
Adam Powell | 7f02dc5 | 2012-08-27 13:35:16 -0700 | [diff] [blame] | 1270 | * |
| 1271 | * <p>This will only be invoked when the dialog is canceled. |
| 1272 | * Cancel events alone will not capture all ways that |
| 1273 | * the dialog might be dismissed. If the creator needs |
| 1274 | * to know when a dialog is dismissed in general, use |
| 1275 | * {@link #setOnDismissListener}.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1276 | * |
| 1277 | * @param listener The {@link DialogInterface.OnCancelListener} to use. |
| 1278 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1279 | public void setOnCancelListener(@Nullable OnCancelListener listener) { |
Dianne Hackborn | f812fee | 2011-01-25 14:54:29 -0800 | [diff] [blame] | 1280 | if (mCancelAndDismissTaken != null) { |
| 1281 | throw new IllegalStateException( |
| 1282 | "OnCancelListener is already taken by " |
| 1283 | + mCancelAndDismissTaken + " and can not be replaced."); |
| 1284 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | if (listener != null) { |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 1286 | mCancelMessage = mListenersHandler.obtainMessage(CANCEL, listener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | } else { |
| 1288 | mCancelMessage = null; |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | /** |
| 1293 | * Set a message to be sent when the dialog is canceled. |
| 1294 | * @param msg The msg to send when the dialog is canceled. |
| 1295 | * @see #setOnCancelListener(android.content.DialogInterface.OnCancelListener) |
| 1296 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1297 | public void setCancelMessage(@Nullable Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | mCancelMessage = msg; |
| 1299 | } |
| 1300 | |
| 1301 | /** |
| 1302 | * Set a listener to be invoked when the dialog is dismissed. |
| 1303 | * @param listener The {@link DialogInterface.OnDismissListener} to use. |
| 1304 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1305 | public void setOnDismissListener(@Nullable OnDismissListener listener) { |
Dianne Hackborn | f812fee | 2011-01-25 14:54:29 -0800 | [diff] [blame] | 1306 | if (mCancelAndDismissTaken != null) { |
| 1307 | throw new IllegalStateException( |
| 1308 | "OnDismissListener is already taken by " |
| 1309 | + mCancelAndDismissTaken + " and can not be replaced."); |
| 1310 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | if (listener != null) { |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 1312 | mDismissMessage = mListenersHandler.obtainMessage(DISMISS, listener); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | } else { |
| 1314 | mDismissMessage = null; |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | /** |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 1319 | * Sets a listener to be invoked when the dialog is shown. |
Ficus Kirkpatrick | 130a8b7 | 2010-01-14 15:39:43 -0800 | [diff] [blame] | 1320 | * @param listener The {@link DialogInterface.OnShowListener} to use. |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 1321 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1322 | public void setOnShowListener(@Nullable OnShowListener listener) { |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 1323 | if (listener != null) { |
| 1324 | mShowMessage = mListenersHandler.obtainMessage(SHOW, listener); |
| 1325 | } else { |
| 1326 | mShowMessage = null; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1331 | * Set a message to be sent when the dialog is dismissed. |
| 1332 | * @param msg The msg to send when the dialog is dismissed. |
| 1333 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1334 | public void setDismissMessage(@Nullable Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | mDismissMessage = msg; |
| 1336 | } |
| 1337 | |
Dianne Hackborn | f812fee | 2011-01-25 14:54:29 -0800 | [diff] [blame] | 1338 | /** @hide */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1339 | public boolean takeCancelAndDismissListeners(@Nullable String msg, |
| 1340 | @Nullable OnCancelListener cancel, @Nullable OnDismissListener dismiss) { |
Dianne Hackborn | f812fee | 2011-01-25 14:54:29 -0800 | [diff] [blame] | 1341 | if (mCancelAndDismissTaken != null) { |
| 1342 | mCancelAndDismissTaken = null; |
| 1343 | } else if (mCancelMessage != null || mDismissMessage != null) { |
| 1344 | return false; |
| 1345 | } |
| 1346 | |
| 1347 | setOnCancelListener(cancel); |
| 1348 | setOnDismissListener(dismiss); |
| 1349 | mCancelAndDismissTaken = msg; |
| 1350 | |
| 1351 | return true; |
| 1352 | } |
| 1353 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1354 | /** |
| 1355 | * By default, this will use the owner Activity's suggested stream type. |
| 1356 | * |
| 1357 | * @see Activity#setVolumeControlStream(int) |
| 1358 | * @see #setOwnerActivity(Activity) |
| 1359 | */ |
| 1360 | public final void setVolumeControlStream(int streamType) { |
| 1361 | getWindow().setVolumeControlStream(streamType); |
| 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * @see Activity#getVolumeControlStream() |
| 1366 | */ |
| 1367 | public final int getVolumeControlStream() { |
| 1368 | return getWindow().getVolumeControlStream(); |
| 1369 | } |
| 1370 | |
| 1371 | /** |
| 1372 | * Sets the callback that will be called if a key is dispatched to the dialog. |
| 1373 | */ |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1374 | public void setOnKeyListener(@Nullable OnKeyListener onKeyListener) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | mOnKeyListener = onKeyListener; |
| 1376 | } |
| 1377 | |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 1378 | private static final class ListenersHandler extends Handler { |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1379 | private final WeakReference<DialogInterface> mDialog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1380 | |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 1381 | public ListenersHandler(Dialog dialog) { |
Alan Viverette | 48728c2 | 2016-04-01 15:00:10 -0400 | [diff] [blame] | 1382 | mDialog = new WeakReference<>(dialog); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | @Override |
| 1386 | public void handleMessage(Message msg) { |
| 1387 | switch (msg.what) { |
| 1388 | case DISMISS: |
| 1389 | ((OnDismissListener) msg.obj).onDismiss(mDialog.get()); |
| 1390 | break; |
| 1391 | case CANCEL: |
| 1392 | ((OnCancelListener) msg.obj).onCancel(mDialog.get()); |
| 1393 | break; |
Romain Guy | 045163a | 2009-07-14 13:59:33 -0700 | [diff] [blame] | 1394 | case SHOW: |
| 1395 | ((OnShowListener) msg.obj).onShow(mDialog.get()); |
| 1396 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | } |
Michael Kwan | f7964be | 2016-11-30 16:44:33 -0800 | [diff] [blame] | 1400 | |
| 1401 | private void updateWindowForCancelable() { |
| 1402 | mWindow.setCloseOnSwipeEnabled(mCancelable); |
| 1403 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | } |