blob: eb260265c15fccc76efde23e4c663102cc9fd0a4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.app;
18
Tor Norbyec615c6f2015-03-02 10:11:44 -080019import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
21import android.annotation.IdRes;
22import android.annotation.LayoutRes;
Alan Viverette682a4332015-04-10 11:05:50 -070023import android.annotation.NonNull;
Scott Kennedyc0519552015-02-11 15:33:10 -080024import android.annotation.Nullable;
Clara Bayarri75e09792015-07-29 16:20:40 +010025import android.annotation.StringRes;
Alan Viverette682a4332015-04-10 11:05:50 -070026import android.annotation.StyleRes;
Karl Rosaen53d24af2009-07-14 14:58:10 -070027import android.content.ComponentName;
Adam Powell6e346362010-07-23 10:18:23 -070028import android.content.Context;
Karl Rosaen7bafed82009-09-04 11:15:21 -070029import android.content.ContextWrapper;
Adam Powell6e346362010-07-23 10:18:23 -070030import android.content.DialogInterface;
Jorim Jaggib10e33f2015-02-04 21:57:40 +010031import android.content.pm.ApplicationInfo;
Chris Craik6faa9e52018-01-11 10:46:10 -080032import android.content.res.Configuration;
Adam Lesinski9553fb32017-05-23 18:53:44 -070033import android.content.res.ResourceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.drawable.Drawable;
35import android.net.Uri;
svetoslavganov75986cf2009-05-14 22:28:01 -070036import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Handler;
Svetoslav Ganov44bfdd82012-01-26 10:00:18 -080038import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.Message;
Adam Powell89fc3ac2011-11-01 18:00:44 -070040import android.util.Log;
Adam Powell2fbf4de62010-09-30 15:46:46 -070041import android.util.TypedValue;
Adam Powelldec9dfd2010-08-09 15:27:54 -070042import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070044import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.view.ContextThemeWrapper;
46import android.view.Gravity;
47import android.view.KeyEvent;
svetoslavganov75986cf2009-05-14 22:28:01 -070048import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.view.Menu;
50import android.view.MenuItem;
51import android.view.MotionEvent;
Tim Kilbourn6a975b32015-04-09 17:14:34 -070052import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -070054import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -070056import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.view.Window;
58import android.view.WindowManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
Chris Craik6faa9e52018-01-11 10:46:10 -080061import com.android.internal.R;
62import com.android.internal.app.WindowDecorActionBar;
63import com.android.internal.policy.PhoneWindow;
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import 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 Fernandez558459f2011-10-13 16:47:36 -070084 * 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 Project9066cfe2009-03-03 19:31:44 -080092 */
93public class Dialog implements DialogInterface, Window.Callback,
Adam Powell117b6952014-05-05 18:14:56 -070094 KeyEvent.Callback, OnCreateContextMenuListener, Window.OnWindowDismissedCallback {
Adam Powell89fc3ac2011-11-01 18:00:44 -070095 private static final String TAG = "Dialog";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 private Activity mOwnerActivity;
Alan Viverette48728c22016-04-01 15:00:10 -040097
98 private final WindowManager mWindowManager;
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 final Context mContext;
Alan Viverette48728c22016-04-01 15:00:10 -0400101 final Window mWindow;
102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 View mDecor;
Alan Viverette48728c22016-04-01 15:00:10 -0400104
Adam Powelle43340c2014-03-17 19:10:43 -0700105 private ActionBar mActionBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 /**
107 * This field should be made private, so it is hidden from the SDK.
108 * {@hide}
109 */
110 protected boolean mCancelable = true;
svetoslavganov75986cf2009-05-14 22:28:01 -0700111
Dianne Hackbornf812fee2011-01-25 14:54:29 -0800112 private String mCancelAndDismissTaken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 private Message mCancelMessage;
114 private Message mDismissMessage;
Romain Guy045163a2009-07-14 13:59:33 -0700115 private Message mShowMessage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 private OnKeyListener mOnKeyListener;
118
119 private boolean mCreated = false;
120 private boolean mShowing = false;
Dianne Hackborn8b4cac12011-01-03 13:58:14 -0800121 private boolean mCanceled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 private final Handler mHandler = new Handler();
124
Romain Guy7883c972010-03-01 16:39:17 -0800125 private static final int DISMISS = 0x43;
126 private static final int CANCEL = 0x44;
127 private static final int SHOW = 0x45;
128
Alan Viverette48728c22016-04-01 15:00:10 -0400129 private final Handler mListenersHandler;
Romain Guy7883c972010-03-01 16:39:17 -0800130
Tim Kilbourn6a975b32015-04-09 17:14:34 -0700131 private SearchEvent mSearchEvent;
132
Adam Powellcfe9aee2011-11-01 14:56:27 -0700133 private ActionMode mActionMode;
134
Clara Bayarri4423d912015-03-02 19:42:48 +0000135 private int mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
136
Alan Viverette48728c22016-04-01 15:00:10 -0400137 private final Runnable mDismissAction = this::dismissDialog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
139 /**
Alan Viverette5696f042015-04-08 11:03:32 -0700140 * 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 Project9066cfe2009-03-03 19:31:44 -0800147 */
Alan Viverette682a4332015-04-10 11:05:50 -0700148 public Dialog(@NonNull Context context) {
Dianne Hackborne79b5542011-01-27 15:18:46 -0800149 this(context, 0, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 }
151
152 /**
Alan Viverette5696f042015-04-08 11:03:32 -0700153 * 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 Jaggib10e33f2015-02-04 21:57:40 +0100162 *
Alan Viverette5696f042015-04-08 11:03:32 -0700163 * @param context the context in which the dialog should run
Alan Viverette682a4332015-04-10 11:05:50 -0700164 * @param themeResId a style resource describing the theme to use for the
Alan Viverette5696f042015-04-08 11:03:32 -0700165 * window, or {@code 0} to use the default dialog theme
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 */
Alan Viverette682a4332015-04-10 11:05:50 -0700167 public Dialog(@NonNull Context context, @StyleRes int themeResId) {
168 this(context, themeResId, true);
Dianne Hackborne79b5542011-01-27 15:18:46 -0800169 }
170
Alan Viverette682a4332015-04-10 11:05:50 -0700171 Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
Jeff Browna492c3a2012-08-23 19:48:44 -0700172 if (createContextThemeWrapper) {
Adam Lesinski9553fb32017-05-23 18:53:44 -0700173 if (themeResId == ResourceId.ID_NULL) {
Alan Viverette5696f042015-04-08 11:03:32 -0700174 final TypedValue outValue = new TypedValue();
175 context.getTheme().resolveAttribute(R.attr.dialogTheme, outValue, true);
Alan Viverette682a4332015-04-10 11:05:50 -0700176 themeResId = outValue.resourceId;
Jeff Browna492c3a2012-08-23 19:48:44 -0700177 }
Alan Viverette682a4332015-04-10 11:05:50 -0700178 mContext = new ContextThemeWrapper(context, themeResId);
Jeff Browna492c3a2012-08-23 19:48:44 -0700179 } else {
180 mContext = context;
Adam Powell2fbf4de62010-09-30 15:46:46 -0700181 }
182
Alan Viverette682a4332015-04-10 11:05:50 -0700183 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Alan Viverette5696f042015-04-08 11:03:32 -0700184
185 final Window w = new PhoneWindow(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 mWindow = w;
187 w.setCallback(this);
Adam Powell117b6952014-05-05 18:14:56 -0700188 w.setOnWindowDismissedCallback(this);
Michael Kwan67639a52016-12-16 12:38:10 -0800189 w.setOnWindowSwipeDismissedCallback(() -> {
190 if (mCancelable) {
191 cancel();
192 }
193 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 w.setWindowManager(mWindowManager, null, null);
195 w.setGravity(Gravity.CENTER);
Alan Viverette5696f042015-04-08 11:03:32 -0700196
Romain Guy045163a2009-07-14 13:59:33 -0700197 mListenersHandler = new ListenersHandler(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 }
Jeff Browna492c3a2012-08-23 19:48:44 -0700199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 /**
201 * @deprecated
202 * @hide
203 */
204 @Deprecated
Alan Viverette48728c22016-04-01 15:00:10 -0400205 protected Dialog(@NonNull Context context, boolean cancelable,
206 @Nullable Message cancelCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 this(context);
208 mCancelable = cancelable;
Michael Kwanf7964be2016-11-30 16:44:33 -0800209 updateWindowForCancelable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 mCancelMessage = cancelCallback;
211 }
212
Alan Viverette682a4332015-04-10 11:05:50 -0700213 protected Dialog(@NonNull Context context, boolean cancelable,
Alan Viverette48728c22016-04-01 15:00:10 -0400214 @Nullable OnCancelListener cancelListener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 this(context);
216 mCancelable = cancelable;
Michael Kwanf7964be2016-11-30 16:44:33 -0800217 updateWindowForCancelable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 setOnCancelListener(cancelListener);
219 }
220
221 /**
222 * Retrieve the Context this Dialog is running in.
223 *
Adam Powellc49c1732010-09-28 16:03:15 -0700224 * @return Context The Context used by the Dialog.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 */
Alan Viverette48728c22016-04-01 15:00:10 -0400226 public final @NonNull Context getContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 return mContext;
228 }
229
230 /**
Adam Powelldec9dfd2010-08-09 15:27:54 -0700231 * 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 Viverette48728c22016-04-01 15:00:10 -0400235 public @Nullable ActionBar getActionBar() {
Adam Powelldec9dfd2010-08-09 15:27:54 -0700236 return mActionBar;
237 }
238
239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 * 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 Viverette48728c22016-04-01 15:00:10 -0400245 public final void setOwnerActivity(@NonNull Activity activity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 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 Viverette48728c22016-04-01 15:00:10 -0400259 public final @Nullable Activity getOwnerActivity() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 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 Viverettef34ef742013-12-11 15:59:53 -0800271 * 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 Project9066cfe2009-03-03 19:31:44 -0800283 * 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) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700290 if (mDecor != null) {
Adam Powelle67a9dc2010-08-17 17:28:56 -0700291 if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
292 mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR);
293 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700294 mDecor.setVisibility(View.VISIBLE);
295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 return;
297 }
298
Dianne Hackborn8b4cac12011-01-03 13:58:14 -0800299 mCanceled = false;
Robert Carrd7dbec72016-10-05 13:13:21 -0700300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 if (!mCreated) {
302 dispatchOnCreate(null);
Robert Carrd7dbec72016-10-05 13:13:21 -0700303 } 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 Project9066cfe2009-03-03 19:31:44 -0800308 }
309
310 onStart();
311 mDecor = mWindow.getDecorView();
Adam Powelldec9dfd2010-08-09 15:27:54 -0700312
313 if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
Adam Powell04fe6eb2013-05-31 14:39:48 -0700314 final ApplicationInfo info = mContext.getApplicationInfo();
315 mWindow.setDefaultIcon(info.icon);
316 mWindow.setDefaultLogo(info.logo);
Adam Powelle43340c2014-03-17 19:10:43 -0700317 mActionBar = new WindowDecorActionBar(this);
Adam Powelldec9dfd2010-08-09 15:27:54 -0700318 }
319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 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 Guy045163a2009-07-14 13:59:33 -0700329
Alan Viverette48728c22016-04-01 15:00:10 -0400330 mWindowManager.addView(mDecor, l);
331 mShowing = true;
332
333 sendShowMessage();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 }
335
336 /**
337 * Hide the dialog, but do not dismiss it.
338 */
339 public void hide() {
svetoslavganov75986cf2009-05-14 22:28:01 -0700340 if (mDecor != null) {
341 mDecor.setVisibility(View.GONE);
342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
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 Mautner92098c72013-06-10 11:27:26 -0700351 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 public void dismiss() {
Svetoslav Ganov44bfdd82012-01-26 10:00:18 -0800353 if (Looper.myLooper() == mHandler.getLooper()) {
354 dismissDialog();
355 } else {
356 mHandler.post(mDismissAction);
357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
359
Adam Powell89fc3ac2011-11-01 18:00:44 -0700360 void dismissDialog() {
Romain Guy08a4ac32010-03-16 11:40:40 -0700361 if (mDecor == null || !mShowing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 return;
363 }
364
Adam Powell89fc3ac2011-11-01 18:00:44 -0700365 if (mWindow.isDestroyed()) {
366 Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!");
367 return;
368 }
369
Romain Guyd2671e12010-03-11 18:06:42 -0800370 try {
Craig Mautner92098c72013-06-10 11:27:26 -0700371 mWindowManager.removeViewImmediate(mDecor);
Romain Guyd2671e12010-03-11 18:06:42 -0800372 } finally {
Adam Powellcfe9aee2011-11-01 14:56:27 -0700373 if (mActionMode != null) {
374 mActionMode.finish();
375 }
Romain Guyd2671e12010-03-11 18:06:42 -0800376 mDecor = null;
377 mWindow.closeAllPanels();
378 onStop();
379 mShowing = false;
Craig Mautner92098c72013-06-10 11:27:26 -0700380
Romain Guyd2671e12010-03-11 18:06:42 -0800381 sendDismissMessage();
382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
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 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700391
Romain Guy045163a2009-07-14 13:59:33 -0700392 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 Viverette48728c22016-04-01 15:00:10 -0400399 // internal method to make sure mCreated is set properly without requiring
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 // users to call through to super in onCreate
401 void dispatchOnCreate(Bundle savedInstanceState) {
Alan Viverettef34ef742013-12-11 15:59:53 -0800402 if (!mCreated) {
Romain Guy6de4aed2009-07-08 10:54:45 -0700403 onCreate(savedInstanceState);
404 mCreated = true;
405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407
408 /**
Chet Haase49afa5b2010-08-23 11:39:53 -0700409 * Similar to {@link Activity#onCreate}, you should initialize your dialog
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 * in this method, including calling {@link #setContentView}.
Alan Viverette48728c22016-04-01 15:00:10 -0400411 * @param savedInstanceState If this dialog is being reinitialized after a
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 * 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 Powell50efbed2011-02-08 16:20:15 -0800423 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425
426 /**
427 * Called to tell you that you're stopping.
428 */
429 protected void onStop() {
Adam Powell50efbed2011-02-08 16:20:15 -0800430 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 }
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 Viverette48728c22016-04-01 15:00:10 -0400444 public @NonNull Bundle onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 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 Viverette48728c22016-04-01 15:00:10 -0400463 public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 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 Viverette48728c22016-04-01 15:00:10 -0400484 public @Nullable Window getWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 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 Viverette48728c22016-04-01 15:00:10 -0400497 public @Nullable View getCurrentFocus() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 return mWindow != null ? mWindow.getCurrentFocus() : null;
499 }
500
501 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -0400502 * 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 Project9066cfe2009-03-03 19:31:44 -0800511 *
Alan Viverettedb7423c2017-03-31 13:13:58 -0400512 * @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 Craik6faa9e52018-01-11 10:46:10 -0800515 * @see Dialog#requireViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 */
Alan Viverettedb7423c2017-03-31 13:13:58 -0400517 @Nullable
518 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 return mWindow.findViewById(id);
520 }
521
522 /**
Chris Craik6faa9e52018-01-11 10:46:10 -0800523 * 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 Project9066cfe2009-03-03 19:31:44 -0800547 * 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 Norbye7b9c9122013-05-30 16:48:33 -0700552 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 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 Viveretteec186702013-12-05 11:10:31 -0800559 * view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 *
561 * @param view The desired content to display.
562 */
Alan Viverette48728c22016-04-01 15:00:10 -0400563 public void setContentView(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 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 Viverette48728c22016-04-01 15:00:10 -0400570 * view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 *
572 * @param view The desired content to display.
573 * @param params Layout parameters for the view.
574 */
Alan Viverette48728c22016-04-01 15:00:10 -0400575 public void setContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 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 Viverette48728c22016-04-01 15:00:10 -0400586 public void addContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 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 Viverette48728c22016-04-01 15:00:10 -0400595 public void setTitle(@Nullable CharSequence title) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 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 Norbye7b9c9122013-05-30 16:48:33 -0700606 public void setTitle(@StringRes int titleId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 setTitle(mContext.getText(titleId));
608 }
609
610 /**
611 * A key was pressed down.
Emilie Robertsc1c572e2018-01-31 17:33:32 +0000612 * <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 Project9066cfe2009-03-03 19:31:44 -0800618 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @see #onKeyUp
620 * @see android.view.KeyEvent
621 */
Alan Viverette48728c22016-04-01 15:00:10 -0400622 @Override
623 public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
Emilie Robertsc1c572e2018-01-31 17:33:32 +0000624 if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700625 event.startTracking();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 return true;
627 }
628
629 return false;
630 }
631
632 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700633 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
634 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
635 * the event).
636 */
Alan Viverette48728c22016-04-01 15:00:10 -0400637 @Override
638 public boolean onKeyLongPress(int keyCode, @NonNull KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700639 return false;
640 }
641
642 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * A key was released.
Emilie Robertsc1c572e2018-01-31 17:33:32 +0000644 * <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 Hackborn83fe3f52009-09-12 23:38:30 -0700648 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 * @see #onKeyDown
Emilie Robertsc1c572e2018-01-31 17:33:32 +0000650 * @see android.view.KeyEvent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 */
Alan Viverette48728c22016-04-01 15:00:10 -0400652 @Override
653 public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
Emilie Robertsc1c572e2018-01-31 17:33:32 +0000654 if ((keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE)
655 && event.isTracking()
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700656 && !event.isCanceled()) {
657 onBackPressed();
658 return true;
659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 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 Viverette48728c22016-04-01 15:00:10 -0400668 @Override
669 public boolean onKeyMultiple(int keyCode, int repeatCount, @NonNull KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 return false;
671 }
672
673 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700674 * 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 Brown64da12a2011-01-04 19:57:47 -0800683
684 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900685 * Called when a key shortcut event is not handled by any of the views in the Dialog.
Jeff Brown64da12a2011-01-04 19:57:47 -0800686 * 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 Viverette48728c22016-04-01 15:00:10 -0400694 public boolean onKeyShortcut(int keyCode, @NonNull KeyEvent event) {
Jeff Brown64da12a2011-01-04 19:57:47 -0800695 return false;
696 }
697
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700698 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 * 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 Viverette48728c22016-04-01 15:00:10 -0400708 public boolean onTouchEvent(@NonNull MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -0800709 if (mCancelable && mShowing && mWindow.shouldCloseOnTouch(mContext, event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 cancel();
711 return true;
712 }
713
714 return false;
715 }
716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 /**
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 Viverette48728c22016-04-01 15:00:10 -0400731 public boolean onTrackballEvent(@NonNull MotionEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 return false;
733 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800734
735 /**
736 * Called when a generic motion event was not handled by any of the
737 * views inside of the dialog.
738 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -0800739 * Generic motion events describe joystick movements, mouse hovers, track pad
740 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -0800741 * {@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 Brown33bbfd22011-02-24 20:55:35 -0800745 * </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 Browncb1404e2011-01-15 18:14:15 -0800753 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -0800754 *
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 Viverette48728c22016-04-01 15:00:10 -0400760 public boolean onGenericMotionEvent(@NonNull MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -0800761 return false;
762 }
763
Alan Viverette48728c22016-04-01 15:00:10 -0400764 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
766 if (mDecor != null) {
767 mWindowManager.updateViewLayout(mDecor, params);
768 }
769 }
770
Alan Viverette48728c22016-04-01 15:00:10 -0400771 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 public void onContentChanged() {
773 }
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700774
Alan Viverette48728c22016-04-01 15:00:10 -0400775 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 public void onWindowFocusChanged(boolean hasFocus) {
777 }
778
Alan Viverette48728c22016-04-01 15:00:10 -0400779 @Override
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700780 public void onAttachedToWindow() {
781 }
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700782
Alan Viverette48728c22016-04-01 15:00:10 -0400783 @Override
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700784 public void onDetachedFromWindow() {
785 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -0800786
Adam Powell117b6952014-05-05 18:14:56 -0700787 /** @hide */
788 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -0500789 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Will Haldean Brownca6234e2014-02-12 10:23:41 -0800790 dismiss();
791 }
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 /**
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700794 * 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 Project9066cfe2009-03-03 19:31:44 -0800796 * this implementation for key events that should be handled normally.
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700797 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 * @param event The key event.
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700799 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 * @return boolean Return true if this event was consumed.
801 */
Alan Viverette48728c22016-04-01 15:00:10 -0400802 @Override
803 public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 if ((mOnKeyListener != null) && (mOnKeyListener.onKey(this, event.getKeyCode(), event))) {
805 return true;
806 }
807 if (mWindow.superDispatchKeyEvent(event)) {
808 return true;
809 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700810 return event.dispatch(this, mDecor != null
811 ? mDecor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 }
813
814 /**
Jeff Brown64da12a2011-01-04 19:57:47 -0800815 * 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 Viverette48728c22016-04-01 15:00:10 -0400823 @Override
824 public boolean dispatchKeyShortcutEvent(@NonNull KeyEvent event) {
Jeff Brown64da12a2011-01-04 19:57:47 -0800825 if (mWindow.superDispatchKeyShortcutEvent(event)) {
826 return true;
827 }
828 return onKeyShortcut(event.getKeyCode(), event);
829 }
830
831 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 * 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 Viverette48728c22016-04-01 15:00:10 -0400841 @Override
842 public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 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 Viverette48728c22016-04-01 15:00:10 -0400859 @Override
860 public boolean dispatchTrackballEvent(@NonNull MotionEvent ev) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 if (mWindow.superDispatchTrackballEvent(ev)) {
862 return true;
863 }
864 return onTrackballEvent(ev);
865 }
866
Jeff Browncb1404e2011-01-15 18:14:15 -0800867 /**
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 Viverette48728c22016-04-01 15:00:10 -0400877 @Override
878 public boolean dispatchGenericMotionEvent(@NonNull MotionEvent ev) {
Jeff Browncb1404e2011-01-15 18:14:15 -0800879 if (mWindow.superDispatchGenericMotionEvent(ev)) {
880 return true;
881 }
882 return onGenericMotionEvent(ev);
883 }
884
Alan Viverette48728c22016-04-01 15:00:10 -0400885 @Override
886 public boolean dispatchPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700887 event.setClassName(getClass().getName());
888 event.setPackageName(mContext.getPackageName());
889
890 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -0800891 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
892 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -0700893 event.setFullScreen(isFullScreen);
894
895 return false;
896 }
897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 /**
899 * @see Activity#onCreatePanelView(int)
900 */
Alan Viverette48728c22016-04-01 15:00:10 -0400901 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 public View onCreatePanelView(int featureId) {
903 return null;
904 }
905
906 /**
907 * @see Activity#onCreatePanelMenu(int, Menu)
908 */
Alan Viverette48728c22016-04-01 15:00:10 -0400909 @Override
910 public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 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 Viverette48728c22016-04-01 15:00:10 -0400921 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 public boolean onPreparePanel(int featureId, View view, Menu menu) {
923 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
Alan Viverette48728c22016-04-01 15:00:10 -0400924 return onPrepareOptionsMenu(menu) && menu.hasVisibleItems();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 }
926 return true;
927 }
928
929 /**
930 * @see Activity#onMenuOpened(int, Menu)
931 */
Alan Viverette48728c22016-04-01 15:00:10 -0400932 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -0800934 if (featureId == Window.FEATURE_ACTION_BAR) {
935 mActionBar.dispatchMenuVisibilityChanged(true);
936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 return true;
938 }
939
940 /**
941 * @see Activity#onMenuItemSelected(int, MenuItem)
942 */
Alan Viverette48728c22016-04-01 15:00:10 -0400943 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 public boolean onMenuItemSelected(int featureId, MenuItem item) {
945 return false;
946 }
947
948 /**
949 * @see Activity#onPanelClosed(int, Menu)
950 */
Alan Viverette48728c22016-04-01 15:00:10 -0400951 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 public void onPanelClosed(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -0800953 if (featureId == Window.FEATURE_ACTION_BAR) {
954 mActionBar.dispatchMenuVisibilityChanged(false);
955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
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 Viverette48728c22016-04-01 15:00:10 -0400966 public boolean onCreateOptionsMenu(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 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 Viverette48728c22016-04-01 15:00:10 -0400978 public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 return true;
980 }
981
982 /**
983 * @see Activity#onOptionsItemSelected(MenuItem)
984 */
Alan Viverette48728c22016-04-01 15:00:10 -0400985 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 return false;
987 }
988
989 /**
990 * @see Activity#onOptionsMenuClosed(Menu)
991 */
Alan Viverette48728c22016-04-01 15:00:10 -0400992 public void onOptionsMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 }
994
995 /**
996 * @see Activity#openOptionsMenu()
997 */
998 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -0800999 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
1000 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
1001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
Jose Lima7a22fc62015-01-23 17:24:22 -08001003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 /**
1005 * @see Activity#closeOptionsMenu()
1006 */
1007 public void closeOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08001008 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
1009 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
1010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 }
1012
1013 /**
Adam Powelle67a9dc2010-08-17 17:28:56 -07001014 * @see Activity#invalidateOptionsMenu()
1015 */
1016 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -08001017 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
1018 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
1019 }
Adam Powelle67a9dc2010-08-17 17:28:56 -07001020 }
1021
1022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 * @see Activity#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)
1024 */
Alan Viverette48728c22016-04-01 15:00:10 -04001025 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1027 }
1028
1029 /**
1030 * @see Activity#registerForContextMenu(View)
1031 */
Alan Viverette48728c22016-04-01 15:00:10 -04001032 public void registerForContextMenu(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 view.setOnCreateContextMenuListener(this);
1034 }
1035
1036 /**
1037 * @see Activity#unregisterForContextMenu(View)
1038 */
Alan Viverette48728c22016-04-01 15:00:10 -04001039 public void unregisterForContextMenu(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 view.setOnCreateContextMenuListener(null);
1041 }
1042
1043 /**
1044 * @see Activity#openContextMenu(View)
1045 */
Alan Viverette48728c22016-04-01 15:00:10 -04001046 public void openContextMenu(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 view.showContextMenu();
1048 }
1049
1050 /**
1051 * @see Activity#onContextItemSelected(MenuItem)
1052 */
Alan Viverette48728c22016-04-01 15:00:10 -04001053 public boolean onContextItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 return false;
1055 }
Clara Bayarri4423d912015-03-02 19:42:48 +00001056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 /**
1058 * @see Activity#onContextMenuClosed(Menu)
1059 */
Alan Viverette48728c22016-04-01 15:00:10 -04001060 public void onContextMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
Clara Bayarri4423d912015-03-02 19:42:48 +00001062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 /**
1064 * This hook is called when the user signals the desire to start a search.
1065 */
Alan Viverette48728c22016-04-01 15:00:10 -04001066 @Override
1067 public boolean onSearchRequested(@NonNull SearchEvent searchEvent) {
Tim Kilbourn6a975b32015-04-09 17:14:34 -07001068 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 Viverette48728c22016-04-01 15:00:10 -04001075 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 public boolean onSearchRequested() {
Karl Rosaen53d24af2009-07-14 14:58:10 -07001077 final SearchManager searchManager = (SearchManager) mContext
1078 .getSystemService(Context.SEARCH_SERVICE);
1079
Bjorn Bringertb8144a92010-02-22 20:48:57 +00001080 // associate search with owner activity
Karl Rosaen7bafed82009-09-04 11:15:21 -07001081 final ComponentName appName = getAssociatedActivity();
lge-aosp06ca9972011-03-15 10:25:57 +09001082 if (appName != null && searchManager.getSearchableInfo(appName) != null) {
Bjorn Bringertb8144a92010-02-22 20:48:57 +00001083 searchManager.startSearch(null, false, appName, null, false);
1084 dismiss();
1085 return true;
1086 } else {
1087 return false;
1088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 }
1090
Clara Bayarri4423d912015-03-02 19:42:48 +00001091 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07001092 * 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 Viverette48728c22016-04-01 15:00:10 -04001098 public final @Nullable SearchEvent getSearchEvent() {
Tim Kilbourn6a975b32015-04-09 17:14:34 -07001099 return mSearchEvent;
1100 }
1101
Clara Bayarri4423d912015-03-02 19:42:48 +00001102 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08001103 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00001104 if (mActionBar != null && mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
Adam Powelldec9dfd2010-08-09 15:27:54 -07001105 return mActionBar.startActionMode(callback);
1106 }
Adam Powell6e346362010-07-23 10:18:23 -07001107 return null;
1108 }
1109
Clara Bayarri4423d912015-03-02 19:42:48 +00001110 @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 Powellcfe9aee2011-11-01 14:56:27 -07001122 *
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 Viverette48728c22016-04-01 15:00:10 -04001126 @Override
Tor Norbyec615c6f2015-03-02 10:11:44 -08001127 @CallSuper
Adam Powelldebf3be2010-11-15 18:58:48 -08001128 public void onActionModeStarted(ActionMode mode) {
Adam Powellcfe9aee2011-11-01 14:56:27 -07001129 mActionMode = mode;
Adam Powelldebf3be2010-11-15 18:58:48 -08001130 }
1131
Adam Powellcfe9aee2011-11-01 14:56:27 -07001132 /**
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 Viverette48728c22016-04-01 15:00:10 -04001138 @Override
Tor Norbyec615c6f2015-03-02 10:11:44 -08001139 @CallSuper
Adam Powelldebf3be2010-11-15 18:58:48 -08001140 public void onActionModeFinished(ActionMode mode) {
Adam Powellcfe9aee2011-11-01 14:56:27 -07001141 if (mode == mActionMode) {
1142 mActionMode = null;
1143 }
Adam Powelldebf3be2010-11-15 18:58:48 -08001144 }
1145
Karl Rosaen7bafed82009-09-04 11:15:21 -07001146 /**
Adam Powelldec9dfd2010-08-09 15:27:54 -07001147 * @return The activity associated with this dialog, or null if there is no associated activity.
Karl Rosaen7bafed82009-09-04 11:15:21 -07001148 */
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 Project9066cfe2009-03-03 19:31:44 -08001164
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 Norbye7b9c9122013-05-30 16:48:33 -07001196 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 getWindow().setFeatureDrawableResource(featureId, resId);
1198 }
1199
1200 /**
1201 * Convenience for calling
1202 * {@link android.view.Window#setFeatureDrawableUri}.
1203 */
Alan Viverette48728c22016-04-01 15:00:10 -04001204 public final void setFeatureDrawableUri(int featureId, @Nullable Uri uri) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 getWindow().setFeatureDrawableUri(featureId, uri);
1206 }
1207
1208 /**
1209 * Convenience for calling
1210 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
1211 */
Alan Viverette48728c22016-04-01 15:00:10 -04001212 public final void setFeatureDrawable(int featureId, @Nullable Drawable drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 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 Viverette48728c22016-04-01 15:00:10 -04001224 public @NonNull LayoutInflater getLayoutInflater() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 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 Kwanf7964be2016-11-30 16:44:33 -08001234 updateWindowForCancelable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
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 Kwanf7964be2016-11-30 16:44:33 -08001248 updateWindowForCancelable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
1250
Dianne Hackborncfaf8872011-01-18 13:57:54 -08001251 mWindow.setCloseOnTouchOutside(cancel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
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 Viverette48728c22016-04-01 15:00:10 -04001258 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 public void cancel() {
Dianne Hackborn8b4cac12011-01-03 13:58:14 -08001260 if (!mCanceled && mCancelMessage != null) {
1261 mCanceled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 // 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 Powell7f02dc52012-08-27 13:35:16 -07001270 *
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 Project9066cfe2009-03-03 19:31:44 -08001276 *
1277 * @param listener The {@link DialogInterface.OnCancelListener} to use.
1278 */
Alan Viverette48728c22016-04-01 15:00:10 -04001279 public void setOnCancelListener(@Nullable OnCancelListener listener) {
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001280 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 Project9066cfe2009-03-03 19:31:44 -08001285 if (listener != null) {
Romain Guy045163a2009-07-14 13:59:33 -07001286 mCancelMessage = mListenersHandler.obtainMessage(CANCEL, listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 } 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 Viverette48728c22016-04-01 15:00:10 -04001297 public void setCancelMessage(@Nullable Message msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 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 Viverette48728c22016-04-01 15:00:10 -04001305 public void setOnDismissListener(@Nullable OnDismissListener listener) {
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001306 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 Project9066cfe2009-03-03 19:31:44 -08001311 if (listener != null) {
Romain Guy045163a2009-07-14 13:59:33 -07001312 mDismissMessage = mListenersHandler.obtainMessage(DISMISS, listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 } else {
1314 mDismissMessage = null;
1315 }
1316 }
1317
1318 /**
Romain Guy045163a2009-07-14 13:59:33 -07001319 * Sets a listener to be invoked when the dialog is shown.
Ficus Kirkpatrick130a8b72010-01-14 15:39:43 -08001320 * @param listener The {@link DialogInterface.OnShowListener} to use.
Romain Guy045163a2009-07-14 13:59:33 -07001321 */
Alan Viverette48728c22016-04-01 15:00:10 -04001322 public void setOnShowListener(@Nullable OnShowListener listener) {
Romain Guy045163a2009-07-14 13:59:33 -07001323 if (listener != null) {
1324 mShowMessage = mListenersHandler.obtainMessage(SHOW, listener);
1325 } else {
1326 mShowMessage = null;
1327 }
1328 }
1329
1330 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 * 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 Viverette48728c22016-04-01 15:00:10 -04001334 public void setDismissMessage(@Nullable Message msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 mDismissMessage = msg;
1336 }
1337
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001338 /** @hide */
Alan Viverette48728c22016-04-01 15:00:10 -04001339 public boolean takeCancelAndDismissListeners(@Nullable String msg,
1340 @Nullable OnCancelListener cancel, @Nullable OnDismissListener dismiss) {
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001341 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 Project9066cfe2009-03-03 19:31:44 -08001354 /**
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 Viverette48728c22016-04-01 15:00:10 -04001374 public void setOnKeyListener(@Nullable OnKeyListener onKeyListener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 mOnKeyListener = onKeyListener;
1376 }
1377
Romain Guy045163a2009-07-14 13:59:33 -07001378 private static final class ListenersHandler extends Handler {
Alan Viverette48728c22016-04-01 15:00:10 -04001379 private final WeakReference<DialogInterface> mDialog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
Romain Guy045163a2009-07-14 13:59:33 -07001381 public ListenersHandler(Dialog dialog) {
Alan Viverette48728c22016-04-01 15:00:10 -04001382 mDialog = new WeakReference<>(dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 }
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 Guy045163a2009-07-14 13:59:33 -07001394 case SHOW:
1395 ((OnShowListener) msg.obj).onShow(mDialog.get());
1396 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 }
1398 }
1399 }
Michael Kwanf7964be2016-11-30 16:44:33 -08001400
1401 private void updateWindowForCancelable() {
1402 mWindow.setCloseOnSwipeEnabled(mCancelable);
1403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404}