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