blob: 2eabd86c5da976551c0fdb83b9207deebe2fbeed [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
Alan Viverette48728c22016-04-01 15:00:10 -040019import com.android.internal.R;
20import com.android.internal.app.WindowDecorActionBar;
21import com.android.internal.policy.PhoneWindow;
22
Tor Norbyec615c6f2015-03-02 10:11:44 -080023import android.annotation.CallSuper;
Tor Norbye7b9c9122013-05-30 16:48:33 -070024import android.annotation.DrawableRes;
25import android.annotation.IdRes;
26import android.annotation.LayoutRes;
Alan Viverette682a4332015-04-10 11:05:50 -070027import android.annotation.NonNull;
Scott Kennedyc0519552015-02-11 15:33:10 -080028import android.annotation.Nullable;
Clara Bayarri75e09792015-07-29 16:20:40 +010029import android.annotation.StringRes;
Alan Viverette682a4332015-04-10 11:05:50 -070030import android.annotation.StyleRes;
Karl Rosaen53d24af2009-07-14 14:58:10 -070031import android.content.ComponentName;
Adam Powell6e346362010-07-23 10:18:23 -070032import android.content.Context;
Karl Rosaen7bafed82009-09-04 11:15:21 -070033import android.content.ContextWrapper;
Adam Powell6e346362010-07-23 10:18:23 -070034import android.content.DialogInterface;
Robert Carrd7dbec72016-10-05 13:13:21 -070035import android.content.res.Configuration;
Jorim Jaggib10e33f2015-02-04 21:57:40 +010036import android.content.pm.ApplicationInfo;
Adam Lesinski9553fb32017-05-23 18:53:44 -070037import android.content.res.ResourceId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.drawable.Drawable;
39import android.net.Uri;
svetoslavganov75986cf2009-05-14 22:28:01 -070040import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Handler;
Svetoslav Ganov44bfdd82012-01-26 10:00:18 -080042import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Message;
Adam Powell89fc3ac2011-11-01 18:00:44 -070044import android.util.Log;
Adam Powell2fbf4de62010-09-30 15:46:46 -070045import android.util.TypedValue;
Adam Powelldec9dfd2010-08-09 15:27:54 -070046import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.view.ContextMenu;
Adam Powell6e346362010-07-23 10:18:23 -070048import android.view.ContextMenu.ContextMenuInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.view.ContextThemeWrapper;
50import android.view.Gravity;
51import android.view.KeyEvent;
svetoslavganov75986cf2009-05-14 22:28:01 -070052import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.view.Menu;
54import android.view.MenuItem;
55import android.view.MotionEvent;
Tim Kilbourn6a975b32015-04-09 17:14:34 -070056import android.view.SearchEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.view.View;
Adam Powell6e346362010-07-23 10:18:23 -070058import android.view.View.OnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ViewGroup;
Adam Powell6e346362010-07-23 10:18:23 -070060import android.view.ViewGroup.LayoutParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.Window;
62import android.view.WindowManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070063import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
65import 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 }
tiansiming [田思明]690b3692018-02-10 15:53:53 +0800294 if (mDecor.getVisibility() != View.VISIBLE) {
295 mDecor.setVisibility(View.VISIBLE);
296 sendShowMessage();
297 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 return;
300 }
301
Dianne Hackborn8b4cac12011-01-03 13:58:14 -0800302 mCanceled = false;
Robert Carrd7dbec72016-10-05 13:13:21 -0700303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 if (!mCreated) {
305 dispatchOnCreate(null);
Robert Carrd7dbec72016-10-05 13:13:21 -0700306 } 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 Project9066cfe2009-03-03 19:31:44 -0800311 }
312
313 onStart();
314 mDecor = mWindow.getDecorView();
Adam Powelldec9dfd2010-08-09 15:27:54 -0700315
316 if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
Adam Powell04fe6eb2013-05-31 14:39:48 -0700317 final ApplicationInfo info = mContext.getApplicationInfo();
318 mWindow.setDefaultIcon(info.icon);
319 mWindow.setDefaultLogo(info.logo);
Adam Powelle43340c2014-03-17 19:10:43 -0700320 mActionBar = new WindowDecorActionBar(this);
Adam Powelldec9dfd2010-08-09 15:27:54 -0700321 }
322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 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 Guy045163a2009-07-14 13:59:33 -0700332
Alan Viverette48728c22016-04-01 15:00:10 -0400333 mWindowManager.addView(mDecor, l);
334 mShowing = true;
335
336 sendShowMessage();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
338
339 /**
340 * Hide the dialog, but do not dismiss it.
341 */
342 public void hide() {
svetoslavganov75986cf2009-05-14 22:28:01 -0700343 if (mDecor != null) {
344 mDecor.setVisibility(View.GONE);
345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
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 Mautner92098c72013-06-10 11:27:26 -0700354 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 public void dismiss() {
Svetoslav Ganov44bfdd82012-01-26 10:00:18 -0800356 if (Looper.myLooper() == mHandler.getLooper()) {
357 dismissDialog();
358 } else {
359 mHandler.post(mDismissAction);
360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362
Adam Powell89fc3ac2011-11-01 18:00:44 -0700363 void dismissDialog() {
Romain Guy08a4ac32010-03-16 11:40:40 -0700364 if (mDecor == null || !mShowing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 return;
366 }
367
Adam Powell89fc3ac2011-11-01 18:00:44 -0700368 if (mWindow.isDestroyed()) {
369 Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!");
370 return;
371 }
372
Romain Guyd2671e12010-03-11 18:06:42 -0800373 try {
Craig Mautner92098c72013-06-10 11:27:26 -0700374 mWindowManager.removeViewImmediate(mDecor);
Romain Guyd2671e12010-03-11 18:06:42 -0800375 } finally {
Adam Powellcfe9aee2011-11-01 14:56:27 -0700376 if (mActionMode != null) {
377 mActionMode.finish();
378 }
Romain Guyd2671e12010-03-11 18:06:42 -0800379 mDecor = null;
380 mWindow.closeAllPanels();
381 onStop();
382 mShowing = false;
Craig Mautner92098c72013-06-10 11:27:26 -0700383
Romain Guyd2671e12010-03-11 18:06:42 -0800384 sendDismissMessage();
385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 }
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 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700394
Romain Guy045163a2009-07-14 13:59:33 -0700395 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 Viverette48728c22016-04-01 15:00:10 -0400402 // internal method to make sure mCreated is set properly without requiring
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 // users to call through to super in onCreate
404 void dispatchOnCreate(Bundle savedInstanceState) {
Alan Viverettef34ef742013-12-11 15:59:53 -0800405 if (!mCreated) {
Romain Guy6de4aed2009-07-08 10:54:45 -0700406 onCreate(savedInstanceState);
407 mCreated = true;
408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410
411 /**
Chet Haase49afa5b2010-08-23 11:39:53 -0700412 * Similar to {@link Activity#onCreate}, you should initialize your dialog
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 * in this method, including calling {@link #setContentView}.
Alan Viverette48728c22016-04-01 15:00:10 -0400414 * @param savedInstanceState If this dialog is being reinitialized after a
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 * 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 Powell50efbed2011-02-08 16:20:15 -0800426 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
428
429 /**
430 * Called to tell you that you're stopping.
431 */
432 protected void onStop() {
Adam Powell50efbed2011-02-08 16:20:15 -0800433 if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 }
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 Viverette48728c22016-04-01 15:00:10 -0400447 public @NonNull Bundle onSaveInstanceState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 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 Viverette48728c22016-04-01 15:00:10 -0400466 public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 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 Viverette48728c22016-04-01 15:00:10 -0400487 public @Nullable Window getWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 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 Viverette48728c22016-04-01 15:00:10 -0400500 public @Nullable View getCurrentFocus() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 return mWindow != null ? mWindow.getCurrentFocus() : null;
502 }
503
504 /**
Alan Viverettedb7423c2017-03-31 13:13:58 -0400505 * 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 Project9066cfe2009-03-03 19:31:44 -0800514 *
Alan Viverettedb7423c2017-03-31 13:13:58 -0400515 * @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 Project9066cfe2009-03-03 19:31:44 -0800518 */
Alan Viverettedb7423c2017-03-31 13:13:58 -0400519 @Nullable
520 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 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 Norbye7b9c9122013-05-30 16:48:33 -0700530 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 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 Viveretteec186702013-12-05 11:10:31 -0800537 * view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 *
539 * @param view The desired content to display.
540 */
Alan Viverette48728c22016-04-01 15:00:10 -0400541 public void setContentView(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 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 Viverette48728c22016-04-01 15:00:10 -0400548 * view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 *
550 * @param view The desired content to display.
551 * @param params Layout parameters for the view.
552 */
Alan Viverette48728c22016-04-01 15:00:10 -0400553 public void setContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 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 Viverette48728c22016-04-01 15:00:10 -0400564 public void addContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 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 Viverette48728c22016-04-01 15:00:10 -0400573 public void setTitle(@Nullable CharSequence title) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 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 Norbye7b9c9122013-05-30 16:48:33 -0700584 public void setTitle(@StringRes int titleId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 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 Hackborn83fe3f52009-09-12 23:38:30 -0700593 * <p>The default implementation consumed the KEYCODE_BACK to later
594 * handle it in {@link #onKeyUp}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 *
596 * @see #onKeyUp
597 * @see android.view.KeyEvent
598 */
Alan Viverette48728c22016-04-01 15:00:10 -0400599 @Override
600 public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700602 event.startTracking();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 return true;
604 }
605
606 return false;
607 }
608
609 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700610 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
611 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
612 * the event).
613 */
Alan Viverette48728c22016-04-01 15:00:10 -0400614 @Override
615 public boolean onKeyLongPress(int keyCode, @NonNull KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700616 return false;
617 }
618
619 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * A key was released.
621 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700622 * <p>The default implementation handles KEYCODE_BACK to close the
623 * dialog.
624 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @see #onKeyDown
626 * @see KeyEvent
627 */
Alan Viverette48728c22016-04-01 15:00:10 -0400628 @Override
629 public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700630 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
631 && !event.isCanceled()) {
632 onBackPressed();
633 return true;
634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 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 Viverette48728c22016-04-01 15:00:10 -0400643 @Override
644 public boolean onKeyMultiple(int keyCode, int repeatCount, @NonNull KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 return false;
646 }
647
648 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700649 * 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 Brown64da12a2011-01-04 19:57:47 -0800658
659 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900660 * Called when a key shortcut event is not handled by any of the views in the Dialog.
Jeff Brown64da12a2011-01-04 19:57:47 -0800661 * 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 Viverette48728c22016-04-01 15:00:10 -0400669 public boolean onKeyShortcut(int keyCode, @NonNull KeyEvent event) {
Jeff Brown64da12a2011-01-04 19:57:47 -0800670 return false;
671 }
672
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700673 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 * 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 Viverette48728c22016-04-01 15:00:10 -0400683 public boolean onTouchEvent(@NonNull MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -0800684 if (mCancelable && mShowing && mWindow.shouldCloseOnTouch(mContext, event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 cancel();
686 return true;
687 }
688
689 return false;
690 }
691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 /**
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 Viverette48728c22016-04-01 15:00:10 -0400706 public boolean onTrackballEvent(@NonNull MotionEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 return false;
708 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800709
710 /**
711 * Called when a generic motion event was not handled by any of the
712 * views inside of the dialog.
713 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -0800714 * Generic motion events describe joystick movements, mouse hovers, track pad
715 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -0800716 * {@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 Brown33bbfd22011-02-24 20:55:35 -0800720 * </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 Browncb1404e2011-01-15 18:14:15 -0800728 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -0800729 *
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 Viverette48728c22016-04-01 15:00:10 -0400735 public boolean onGenericMotionEvent(@NonNull MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -0800736 return false;
737 }
738
Alan Viverette48728c22016-04-01 15:00:10 -0400739 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
741 if (mDecor != null) {
742 mWindowManager.updateViewLayout(mDecor, params);
743 }
744 }
745
Alan Viverette48728c22016-04-01 15:00:10 -0400746 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 public void onContentChanged() {
748 }
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700749
Alan Viverette48728c22016-04-01 15:00:10 -0400750 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 public void onWindowFocusChanged(boolean hasFocus) {
752 }
753
Alan Viverette48728c22016-04-01 15:00:10 -0400754 @Override
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700755 public void onAttachedToWindow() {
756 }
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700757
Alan Viverette48728c22016-04-01 15:00:10 -0400758 @Override
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700759 public void onDetachedFromWindow() {
760 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -0800761
Adam Powell117b6952014-05-05 18:14:56 -0700762 /** @hide */
763 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -0500764 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Will Haldean Brownca6234e2014-02-12 10:23:41 -0800765 dismiss();
766 }
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 /**
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700769 * 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 Project9066cfe2009-03-03 19:31:44 -0800771 * this implementation for key events that should be handled normally.
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700772 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 * @param event The key event.
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700774 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 * @return boolean Return true if this event was consumed.
776 */
Alan Viverette48728c22016-04-01 15:00:10 -0400777 @Override
778 public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 if ((mOnKeyListener != null) && (mOnKeyListener.onKey(this, event.getKeyCode(), event))) {
780 return true;
781 }
782 if (mWindow.superDispatchKeyEvent(event)) {
783 return true;
784 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700785 return event.dispatch(this, mDecor != null
786 ? mDecor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 }
788
789 /**
Jeff Brown64da12a2011-01-04 19:57:47 -0800790 * 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 Viverette48728c22016-04-01 15:00:10 -0400798 @Override
799 public boolean dispatchKeyShortcutEvent(@NonNull KeyEvent event) {
Jeff Brown64da12a2011-01-04 19:57:47 -0800800 if (mWindow.superDispatchKeyShortcutEvent(event)) {
801 return true;
802 }
803 return onKeyShortcut(event.getKeyCode(), event);
804 }
805
806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 * 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 Viverette48728c22016-04-01 15:00:10 -0400816 @Override
817 public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 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 Viverette48728c22016-04-01 15:00:10 -0400834 @Override
835 public boolean dispatchTrackballEvent(@NonNull MotionEvent ev) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 if (mWindow.superDispatchTrackballEvent(ev)) {
837 return true;
838 }
839 return onTrackballEvent(ev);
840 }
841
Jeff Browncb1404e2011-01-15 18:14:15 -0800842 /**
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 Viverette48728c22016-04-01 15:00:10 -0400852 @Override
853 public boolean dispatchGenericMotionEvent(@NonNull MotionEvent ev) {
Jeff Browncb1404e2011-01-15 18:14:15 -0800854 if (mWindow.superDispatchGenericMotionEvent(ev)) {
855 return true;
856 }
857 return onGenericMotionEvent(ev);
858 }
859
Alan Viverette48728c22016-04-01 15:00:10 -0400860 @Override
861 public boolean dispatchPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700862 event.setClassName(getClass().getName());
863 event.setPackageName(mContext.getPackageName());
864
865 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -0800866 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
867 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -0700868 event.setFullScreen(isFullScreen);
869
870 return false;
871 }
872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 /**
874 * @see Activity#onCreatePanelView(int)
875 */
Alan Viverette48728c22016-04-01 15:00:10 -0400876 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 public View onCreatePanelView(int featureId) {
878 return null;
879 }
880
881 /**
882 * @see Activity#onCreatePanelMenu(int, Menu)
883 */
Alan Viverette48728c22016-04-01 15:00:10 -0400884 @Override
885 public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 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 Viverette48728c22016-04-01 15:00:10 -0400896 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 public boolean onPreparePanel(int featureId, View view, Menu menu) {
898 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
Alan Viverette48728c22016-04-01 15:00:10 -0400899 return onPrepareOptionsMenu(menu) && menu.hasVisibleItems();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 }
901 return true;
902 }
903
904 /**
905 * @see Activity#onMenuOpened(int, Menu)
906 */
Alan Viverette48728c22016-04-01 15:00:10 -0400907 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -0800909 if (featureId == Window.FEATURE_ACTION_BAR) {
910 mActionBar.dispatchMenuVisibilityChanged(true);
911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 return true;
913 }
914
915 /**
916 * @see Activity#onMenuItemSelected(int, MenuItem)
917 */
Alan Viverette48728c22016-04-01 15:00:10 -0400918 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 public boolean onMenuItemSelected(int featureId, MenuItem item) {
920 return false;
921 }
922
923 /**
924 * @see Activity#onPanelClosed(int, Menu)
925 */
Alan Viverette48728c22016-04-01 15:00:10 -0400926 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 public void onPanelClosed(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -0800928 if (featureId == Window.FEATURE_ACTION_BAR) {
929 mActionBar.dispatchMenuVisibilityChanged(false);
930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 }
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 Viverette48728c22016-04-01 15:00:10 -0400941 public boolean onCreateOptionsMenu(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 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 Viverette48728c22016-04-01 15:00:10 -0400953 public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 return true;
955 }
956
957 /**
958 * @see Activity#onOptionsItemSelected(MenuItem)
959 */
Alan Viverette48728c22016-04-01 15:00:10 -0400960 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 return false;
962 }
963
964 /**
965 * @see Activity#onOptionsMenuClosed(Menu)
966 */
Alan Viverette48728c22016-04-01 15:00:10 -0400967 public void onOptionsMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 }
969
970 /**
971 * @see Activity#openOptionsMenu()
972 */
973 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -0800974 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
975 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 }
Jose Lima7a22fc62015-01-23 17:24:22 -0800978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 /**
980 * @see Activity#closeOptionsMenu()
981 */
982 public void closeOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -0800983 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
984 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 }
987
988 /**
Adam Powelle67a9dc2010-08-17 17:28:56 -0700989 * @see Activity#invalidateOptionsMenu()
990 */
991 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -0800992 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
993 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
994 }
Adam Powelle67a9dc2010-08-17 17:28:56 -0700995 }
996
997 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 * @see Activity#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)
999 */
Alan Viverette48728c22016-04-01 15:00:10 -04001000 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1002 }
1003
1004 /**
1005 * @see Activity#registerForContextMenu(View)
1006 */
Alan Viverette48728c22016-04-01 15:00:10 -04001007 public void registerForContextMenu(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 view.setOnCreateContextMenuListener(this);
1009 }
1010
1011 /**
1012 * @see Activity#unregisterForContextMenu(View)
1013 */
Alan Viverette48728c22016-04-01 15:00:10 -04001014 public void unregisterForContextMenu(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 view.setOnCreateContextMenuListener(null);
1016 }
1017
1018 /**
1019 * @see Activity#openContextMenu(View)
1020 */
Alan Viverette48728c22016-04-01 15:00:10 -04001021 public void openContextMenu(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 view.showContextMenu();
1023 }
1024
1025 /**
1026 * @see Activity#onContextItemSelected(MenuItem)
1027 */
Alan Viverette48728c22016-04-01 15:00:10 -04001028 public boolean onContextItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 return false;
1030 }
Clara Bayarri4423d912015-03-02 19:42:48 +00001031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 /**
1033 * @see Activity#onContextMenuClosed(Menu)
1034 */
Alan Viverette48728c22016-04-01 15:00:10 -04001035 public void onContextMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 }
Clara Bayarri4423d912015-03-02 19:42:48 +00001037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 /**
1039 * This hook is called when the user signals the desire to start a search.
1040 */
Alan Viverette48728c22016-04-01 15:00:10 -04001041 @Override
1042 public boolean onSearchRequested(@NonNull SearchEvent searchEvent) {
Tim Kilbourn6a975b32015-04-09 17:14:34 -07001043 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 Viverette48728c22016-04-01 15:00:10 -04001050 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 public boolean onSearchRequested() {
Karl Rosaen53d24af2009-07-14 14:58:10 -07001052 final SearchManager searchManager = (SearchManager) mContext
1053 .getSystemService(Context.SEARCH_SERVICE);
1054
Bjorn Bringertb8144a92010-02-22 20:48:57 +00001055 // associate search with owner activity
Karl Rosaen7bafed82009-09-04 11:15:21 -07001056 final ComponentName appName = getAssociatedActivity();
lge-aosp06ca9972011-03-15 10:25:57 +09001057 if (appName != null && searchManager.getSearchableInfo(appName) != null) {
Bjorn Bringertb8144a92010-02-22 20:48:57 +00001058 searchManager.startSearch(null, false, appName, null, false);
1059 dismiss();
1060 return true;
1061 } else {
1062 return false;
1063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 }
1065
Clara Bayarri4423d912015-03-02 19:42:48 +00001066 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07001067 * 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 Viverette48728c22016-04-01 15:00:10 -04001073 public final @Nullable SearchEvent getSearchEvent() {
Tim Kilbourn6a975b32015-04-09 17:14:34 -07001074 return mSearchEvent;
1075 }
1076
Clara Bayarri4423d912015-03-02 19:42:48 +00001077 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08001078 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00001079 if (mActionBar != null && mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
Adam Powelldec9dfd2010-08-09 15:27:54 -07001080 return mActionBar.startActionMode(callback);
1081 }
Adam Powell6e346362010-07-23 10:18:23 -07001082 return null;
1083 }
1084
Clara Bayarri4423d912015-03-02 19:42:48 +00001085 @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 Powellcfe9aee2011-11-01 14:56:27 -07001097 *
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 Viverette48728c22016-04-01 15:00:10 -04001101 @Override
Tor Norbyec615c6f2015-03-02 10:11:44 -08001102 @CallSuper
Adam Powelldebf3be2010-11-15 18:58:48 -08001103 public void onActionModeStarted(ActionMode mode) {
Adam Powellcfe9aee2011-11-01 14:56:27 -07001104 mActionMode = mode;
Adam Powelldebf3be2010-11-15 18:58:48 -08001105 }
1106
Adam Powellcfe9aee2011-11-01 14:56:27 -07001107 /**
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 Viverette48728c22016-04-01 15:00:10 -04001113 @Override
Tor Norbyec615c6f2015-03-02 10:11:44 -08001114 @CallSuper
Adam Powelldebf3be2010-11-15 18:58:48 -08001115 public void onActionModeFinished(ActionMode mode) {
Adam Powellcfe9aee2011-11-01 14:56:27 -07001116 if (mode == mActionMode) {
1117 mActionMode = null;
1118 }
Adam Powelldebf3be2010-11-15 18:58:48 -08001119 }
1120
Karl Rosaen7bafed82009-09-04 11:15:21 -07001121 /**
Adam Powelldec9dfd2010-08-09 15:27:54 -07001122 * @return The activity associated with this dialog, or null if there is no associated activity.
Karl Rosaen7bafed82009-09-04 11:15:21 -07001123 */
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 Project9066cfe2009-03-03 19:31:44 -08001139
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 Norbye7b9c9122013-05-30 16:48:33 -07001171 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 getWindow().setFeatureDrawableResource(featureId, resId);
1173 }
1174
1175 /**
1176 * Convenience for calling
1177 * {@link android.view.Window#setFeatureDrawableUri}.
1178 */
Alan Viverette48728c22016-04-01 15:00:10 -04001179 public final void setFeatureDrawableUri(int featureId, @Nullable Uri uri) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 getWindow().setFeatureDrawableUri(featureId, uri);
1181 }
1182
1183 /**
1184 * Convenience for calling
1185 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
1186 */
Alan Viverette48728c22016-04-01 15:00:10 -04001187 public final void setFeatureDrawable(int featureId, @Nullable Drawable drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 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 Viverette48728c22016-04-01 15:00:10 -04001199 public @NonNull LayoutInflater getLayoutInflater() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 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 Kwanf7964be2016-11-30 16:44:33 -08001209 updateWindowForCancelable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
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 Kwanf7964be2016-11-30 16:44:33 -08001223 updateWindowForCancelable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
1225
Dianne Hackborncfaf8872011-01-18 13:57:54 -08001226 mWindow.setCloseOnTouchOutside(cancel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
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 Viverette48728c22016-04-01 15:00:10 -04001233 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public void cancel() {
Dianne Hackborn8b4cac12011-01-03 13:58:14 -08001235 if (!mCanceled && mCancelMessage != null) {
1236 mCanceled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 // 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 Powell7f02dc52012-08-27 13:35:16 -07001245 *
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 Project9066cfe2009-03-03 19:31:44 -08001251 *
1252 * @param listener The {@link DialogInterface.OnCancelListener} to use.
1253 */
Alan Viverette48728c22016-04-01 15:00:10 -04001254 public void setOnCancelListener(@Nullable OnCancelListener listener) {
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001255 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 Project9066cfe2009-03-03 19:31:44 -08001260 if (listener != null) {
Romain Guy045163a2009-07-14 13:59:33 -07001261 mCancelMessage = mListenersHandler.obtainMessage(CANCEL, listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 } 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 Viverette48728c22016-04-01 15:00:10 -04001272 public void setCancelMessage(@Nullable Message msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 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 Viverette48728c22016-04-01 15:00:10 -04001280 public void setOnDismissListener(@Nullable OnDismissListener listener) {
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001281 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 Project9066cfe2009-03-03 19:31:44 -08001286 if (listener != null) {
Romain Guy045163a2009-07-14 13:59:33 -07001287 mDismissMessage = mListenersHandler.obtainMessage(DISMISS, listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 } else {
1289 mDismissMessage = null;
1290 }
1291 }
1292
1293 /**
Romain Guy045163a2009-07-14 13:59:33 -07001294 * Sets a listener to be invoked when the dialog is shown.
Ficus Kirkpatrick130a8b72010-01-14 15:39:43 -08001295 * @param listener The {@link DialogInterface.OnShowListener} to use.
Romain Guy045163a2009-07-14 13:59:33 -07001296 */
Alan Viverette48728c22016-04-01 15:00:10 -04001297 public void setOnShowListener(@Nullable OnShowListener listener) {
Romain Guy045163a2009-07-14 13:59:33 -07001298 if (listener != null) {
1299 mShowMessage = mListenersHandler.obtainMessage(SHOW, listener);
1300 } else {
1301 mShowMessage = null;
1302 }
1303 }
1304
1305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 * 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 Viverette48728c22016-04-01 15:00:10 -04001309 public void setDismissMessage(@Nullable Message msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 mDismissMessage = msg;
1311 }
1312
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001313 /** @hide */
Alan Viverette48728c22016-04-01 15:00:10 -04001314 public boolean takeCancelAndDismissListeners(@Nullable String msg,
1315 @Nullable OnCancelListener cancel, @Nullable OnDismissListener dismiss) {
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001316 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 Project9066cfe2009-03-03 19:31:44 -08001329 /**
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 Viverette48728c22016-04-01 15:00:10 -04001349 public void setOnKeyListener(@Nullable OnKeyListener onKeyListener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 mOnKeyListener = onKeyListener;
1351 }
1352
Romain Guy045163a2009-07-14 13:59:33 -07001353 private static final class ListenersHandler extends Handler {
Alan Viverette48728c22016-04-01 15:00:10 -04001354 private final WeakReference<DialogInterface> mDialog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355
Romain Guy045163a2009-07-14 13:59:33 -07001356 public ListenersHandler(Dialog dialog) {
Alan Viverette48728c22016-04-01 15:00:10 -04001357 mDialog = new WeakReference<>(dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
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 Guy045163a2009-07-14 13:59:33 -07001369 case SHOW:
1370 ((OnShowListener) msg.obj).onShow(mDialog.get());
1371 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 }
1373 }
1374 }
Michael Kwanf7964be2016-11-30 16:44:33 -08001375
1376 private void updateWindowForCancelable() {
1377 mWindow.setCloseOnSwipeEnabled(mCancelable);
1378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379}