blob: b162cb165fbaf3738ae3b297dbeaba2661e1fa90 [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 }
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)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 */
Alan Viverettedb7423c2017-03-31 13:13:58 -0400516 @Nullable
517 public <T extends View> T findViewById(@IdRes int id) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 return mWindow.findViewById(id);
519 }
520
521 /**
522 * Set the screen content from a layout resource. The resource will be
523 * inflated, adding all top-level views to the screen.
524 *
525 * @param layoutResID Resource ID to be inflated.
526 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700527 public void setContentView(@LayoutRes int layoutResID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 mWindow.setContentView(layoutResID);
529 }
530
531 /**
532 * Set the screen content to an explicit view. This view is placed
533 * directly into the screen's view hierarchy. It can itself be a complex
Alan Viveretteec186702013-12-05 11:10:31 -0800534 * view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 *
536 * @param view The desired content to display.
537 */
Alan Viverette48728c22016-04-01 15:00:10 -0400538 public void setContentView(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 mWindow.setContentView(view);
540 }
541
542 /**
543 * Set the screen content to an explicit view. This view is placed
544 * directly into the screen's view hierarchy. It can itself be a complex
Alan Viverette48728c22016-04-01 15:00:10 -0400545 * view hierarchy.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 *
547 * @param view The desired content to display.
548 * @param params Layout parameters for the view.
549 */
Alan Viverette48728c22016-04-01 15:00:10 -0400550 public void setContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 mWindow.setContentView(view, params);
552 }
553
554 /**
555 * Add an additional content view to the screen. Added after any existing
556 * ones in the screen -- existing views are NOT removed.
557 *
558 * @param view The desired content to display.
559 * @param params Layout parameters for the view.
560 */
Alan Viverette48728c22016-04-01 15:00:10 -0400561 public void addContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 mWindow.addContentView(view, params);
563 }
564
565 /**
566 * Set the title text for this dialog's window.
567 *
568 * @param title The new text to display in the title.
569 */
Alan Viverette48728c22016-04-01 15:00:10 -0400570 public void setTitle(@Nullable CharSequence title) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 mWindow.setTitle(title);
572 mWindow.getAttributes().setTitle(title);
573 }
574
575 /**
576 * Set the title text for this dialog's window. The text is retrieved
577 * from the resources with the supplied identifier.
578 *
579 * @param titleId the title's text resource identifier
580 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700581 public void setTitle(@StringRes int titleId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 setTitle(mContext.getText(titleId));
583 }
584
585 /**
586 * A key was pressed down.
587 *
588 * <p>If the focused view didn't want this event, this method is called.
589 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700590 * <p>The default implementation consumed the KEYCODE_BACK to later
591 * handle it in {@link #onKeyUp}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 *
593 * @see #onKeyUp
594 * @see android.view.KeyEvent
595 */
Alan Viverette48728c22016-04-01 15:00:10 -0400596 @Override
597 public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 if (keyCode == KeyEvent.KEYCODE_BACK) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700599 event.startTracking();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 return true;
601 }
602
603 return false;
604 }
605
606 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700607 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
608 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
609 * the event).
610 */
Alan Viverette48728c22016-04-01 15:00:10 -0400611 @Override
612 public boolean onKeyLongPress(int keyCode, @NonNull KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700613 return false;
614 }
615
616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * A key was released.
618 *
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700619 * <p>The default implementation handles KEYCODE_BACK to close the
620 * dialog.
621 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 * @see #onKeyDown
623 * @see KeyEvent
624 */
Alan Viverette48728c22016-04-01 15:00:10 -0400625 @Override
626 public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700627 if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
628 && !event.isCanceled()) {
629 onBackPressed();
630 return true;
631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 return false;
633 }
634
635 /**
636 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
637 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
638 * the event).
639 */
Alan Viverette48728c22016-04-01 15:00:10 -0400640 @Override
641 public boolean onKeyMultiple(int keyCode, int repeatCount, @NonNull KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 return false;
643 }
644
645 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700646 * Called when the dialog has detected the user's press of the back
647 * key. The default implementation simply cancels the dialog (only if
648 * it is cancelable), but you can override this to do whatever you want.
649 */
650 public void onBackPressed() {
651 if (mCancelable) {
652 cancel();
653 }
654 }
Jeff Brown64da12a2011-01-04 19:57:47 -0800655
656 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900657 * Called when a key shortcut event is not handled by any of the views in the Dialog.
Jeff Brown64da12a2011-01-04 19:57:47 -0800658 * Override this method to implement global key shortcuts for the Dialog.
659 * Key shortcuts can also be implemented by setting the
660 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
661 *
662 * @param keyCode The value in event.getKeyCode().
663 * @param event Description of the key event.
664 * @return True if the key shortcut was handled.
665 */
Alan Viverette48728c22016-04-01 15:00:10 -0400666 public boolean onKeyShortcut(int keyCode, @NonNull KeyEvent event) {
Jeff Brown64da12a2011-01-04 19:57:47 -0800667 return false;
668 }
669
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 * Called when a touch screen event was not handled by any of the views
672 * under it. This is most useful to process touch events that happen outside
673 * of your window bounds, where there is no view to receive it.
674 *
675 * @param event The touch screen event being processed.
676 * @return Return true if you have consumed the event, false if you haven't.
677 * The default implementation will cancel the dialog when a touch
678 * happens outside of the window bounds.
679 */
Alan Viverette48728c22016-04-01 15:00:10 -0400680 public boolean onTouchEvent(@NonNull MotionEvent event) {
Dianne Hackborncfaf8872011-01-18 13:57:54 -0800681 if (mCancelable && mShowing && mWindow.shouldCloseOnTouch(mContext, event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 cancel();
683 return true;
684 }
685
686 return false;
687 }
688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 /**
690 * Called when the trackball was moved and not handled by any of the
691 * views inside of the activity. So, for example, if the trackball moves
692 * while focus is on a button, you will receive a call here because
693 * buttons do not normally do anything with trackball events. The call
694 * here happens <em>before</em> trackball movements are converted to
695 * DPAD key events, which then get sent back to the view hierarchy, and
696 * will be processed at the point for things like focus navigation.
697 *
698 * @param event The trackball event being processed.
699 *
700 * @return Return true if you have consumed the event, false if you haven't.
701 * The default implementation always returns false.
702 */
Alan Viverette48728c22016-04-01 15:00:10 -0400703 public boolean onTrackballEvent(@NonNull MotionEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 return false;
705 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800706
707 /**
708 * Called when a generic motion event was not handled by any of the
709 * views inside of the dialog.
710 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -0800711 * Generic motion events describe joystick movements, mouse hovers, track pad
712 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -0800713 * {@link MotionEvent#getSource() source} of the motion event specifies
714 * the class of input that was received. Implementations of this method
715 * must examine the bits in the source before processing the event.
716 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -0800717 * </p><p>
718 * Generic motion events with source class
719 * {@link android.view.InputDevice#SOURCE_CLASS_POINTER}
720 * are delivered to the view under the pointer. All other generic motion events are
721 * delivered to the focused view.
722 * </p><p>
723 * See {@link View#onGenericMotionEvent(MotionEvent)} for an example of how to
724 * handle this event.
Jeff Browncb1404e2011-01-15 18:14:15 -0800725 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -0800726 *
727 * @param event The generic motion event being processed.
728 *
729 * @return Return true if you have consumed the event, false if you haven't.
730 * The default implementation always returns false.
731 */
Alan Viverette48728c22016-04-01 15:00:10 -0400732 public boolean onGenericMotionEvent(@NonNull MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -0800733 return false;
734 }
735
Alan Viverette48728c22016-04-01 15:00:10 -0400736 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
738 if (mDecor != null) {
739 mWindowManager.updateViewLayout(mDecor, params);
740 }
741 }
742
Alan Viverette48728c22016-04-01 15:00:10 -0400743 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 public void onContentChanged() {
745 }
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700746
Alan Viverette48728c22016-04-01 15:00:10 -0400747 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 public void onWindowFocusChanged(boolean hasFocus) {
749 }
750
Alan Viverette48728c22016-04-01 15:00:10 -0400751 @Override
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700752 public void onAttachedToWindow() {
753 }
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700754
Alan Viverette48728c22016-04-01 15:00:10 -0400755 @Override
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700756 public void onDetachedFromWindow() {
757 }
Will Haldean Brownca6234e2014-02-12 10:23:41 -0800758
Adam Powell117b6952014-05-05 18:14:56 -0700759 /** @hide */
760 @Override
Ned Burns7d6cb912016-12-02 17:25:33 -0500761 public void onWindowDismissed(boolean finishTask, boolean suppressWindowTransition) {
Will Haldean Brownca6234e2014-02-12 10:23:41 -0800762 dismiss();
763 }
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 /**
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700766 * Called to process key events. You can override this to intercept all
767 * key events before they are dispatched to the window. Be sure to call
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 * this implementation for key events that should be handled normally.
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700769 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 * @param event The key event.
Wale Ogunwaleba7881c2015-08-01 19:28:29 -0700771 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 * @return boolean Return true if this event was consumed.
773 */
Alan Viverette48728c22016-04-01 15:00:10 -0400774 @Override
775 public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 if ((mOnKeyListener != null) && (mOnKeyListener.onKey(this, event.getKeyCode(), event))) {
777 return true;
778 }
779 if (mWindow.superDispatchKeyEvent(event)) {
780 return true;
781 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -0700782 return event.dispatch(this, mDecor != null
783 ? mDecor.getKeyDispatcherState() : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785
786 /**
Jeff Brown64da12a2011-01-04 19:57:47 -0800787 * Called to process a key shortcut event.
788 * You can override this to intercept all key shortcut events before they are
789 * dispatched to the window. Be sure to call this implementation for key shortcut
790 * events that should be handled normally.
791 *
792 * @param event The key shortcut event.
793 * @return True if this event was consumed.
794 */
Alan Viverette48728c22016-04-01 15:00:10 -0400795 @Override
796 public boolean dispatchKeyShortcutEvent(@NonNull KeyEvent event) {
Jeff Brown64da12a2011-01-04 19:57:47 -0800797 if (mWindow.superDispatchKeyShortcutEvent(event)) {
798 return true;
799 }
800 return onKeyShortcut(event.getKeyCode(), event);
801 }
802
803 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 * Called to process touch screen events. You can override this to
805 * intercept all touch screen events before they are dispatched to the
806 * window. Be sure to call this implementation for touch screen events
807 * that should be handled normally.
808 *
809 * @param ev The touch screen event.
810 *
811 * @return boolean Return true if this event was consumed.
812 */
Alan Viverette48728c22016-04-01 15:00:10 -0400813 @Override
814 public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 if (mWindow.superDispatchTouchEvent(ev)) {
816 return true;
817 }
818 return onTouchEvent(ev);
819 }
820
821 /**
822 * Called to process trackball events. You can override this to
823 * intercept all trackball events before they are dispatched to the
824 * window. Be sure to call this implementation for trackball events
825 * that should be handled normally.
826 *
827 * @param ev The trackball event.
828 *
829 * @return boolean Return true if this event was consumed.
830 */
Alan Viverette48728c22016-04-01 15:00:10 -0400831 @Override
832 public boolean dispatchTrackballEvent(@NonNull MotionEvent ev) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 if (mWindow.superDispatchTrackballEvent(ev)) {
834 return true;
835 }
836 return onTrackballEvent(ev);
837 }
838
Jeff Browncb1404e2011-01-15 18:14:15 -0800839 /**
840 * Called to process generic motion events. You can override this to
841 * intercept all generic motion events before they are dispatched to the
842 * window. Be sure to call this implementation for generic motion events
843 * that should be handled normally.
844 *
845 * @param ev The generic motion event.
846 *
847 * @return boolean Return true if this event was consumed.
848 */
Alan Viverette48728c22016-04-01 15:00:10 -0400849 @Override
850 public boolean dispatchGenericMotionEvent(@NonNull MotionEvent ev) {
Jeff Browncb1404e2011-01-15 18:14:15 -0800851 if (mWindow.superDispatchGenericMotionEvent(ev)) {
852 return true;
853 }
854 return onGenericMotionEvent(ev);
855 }
856
Alan Viverette48728c22016-04-01 15:00:10 -0400857 @Override
858 public boolean dispatchPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700859 event.setClassName(getClass().getName());
860 event.setPackageName(mContext.getPackageName());
861
862 LayoutParams params = getWindow().getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -0800863 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) &&
864 (params.height == LayoutParams.MATCH_PARENT);
svetoslavganov75986cf2009-05-14 22:28:01 -0700865 event.setFullScreen(isFullScreen);
866
867 return false;
868 }
869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 /**
871 * @see Activity#onCreatePanelView(int)
872 */
Alan Viverette48728c22016-04-01 15:00:10 -0400873 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 public View onCreatePanelView(int featureId) {
875 return null;
876 }
877
878 /**
879 * @see Activity#onCreatePanelMenu(int, Menu)
880 */
Alan Viverette48728c22016-04-01 15:00:10 -0400881 @Override
882 public boolean onCreatePanelMenu(int featureId, @NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
884 return onCreateOptionsMenu(menu);
885 }
886
887 return false;
888 }
889
890 /**
891 * @see Activity#onPreparePanel(int, View, Menu)
892 */
Alan Viverette48728c22016-04-01 15:00:10 -0400893 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 public boolean onPreparePanel(int featureId, View view, Menu menu) {
895 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) {
Alan Viverette48728c22016-04-01 15:00:10 -0400896 return onPrepareOptionsMenu(menu) && menu.hasVisibleItems();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
898 return true;
899 }
900
901 /**
902 * @see Activity#onMenuOpened(int, Menu)
903 */
Alan Viverette48728c22016-04-01 15:00:10 -0400904 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 public boolean onMenuOpened(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -0800906 if (featureId == Window.FEATURE_ACTION_BAR) {
907 mActionBar.dispatchMenuVisibilityChanged(true);
908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 return true;
910 }
911
912 /**
913 * @see Activity#onMenuItemSelected(int, MenuItem)
914 */
Alan Viverette48728c22016-04-01 15:00:10 -0400915 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 public boolean onMenuItemSelected(int featureId, MenuItem item) {
917 return false;
918 }
919
920 /**
921 * @see Activity#onPanelClosed(int, Menu)
922 */
Alan Viverette48728c22016-04-01 15:00:10 -0400923 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 public void onPanelClosed(int featureId, Menu menu) {
Adam Powell8515ee82010-11-30 14:09:55 -0800925 if (featureId == Window.FEATURE_ACTION_BAR) {
926 mActionBar.dispatchMenuVisibilityChanged(false);
927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
929
930 /**
931 * It is usually safe to proxy this call to the owner activity's
932 * {@link Activity#onCreateOptionsMenu(Menu)} if the client desires the same
933 * menu for this Dialog.
934 *
935 * @see Activity#onCreateOptionsMenu(Menu)
936 * @see #getOwnerActivity()
937 */
Alan Viverette48728c22016-04-01 15:00:10 -0400938 public boolean onCreateOptionsMenu(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 return true;
940 }
941
942 /**
943 * It is usually safe to proxy this call to the owner activity's
944 * {@link Activity#onPrepareOptionsMenu(Menu)} if the client desires the
945 * same menu for this Dialog.
946 *
947 * @see Activity#onPrepareOptionsMenu(Menu)
948 * @see #getOwnerActivity()
949 */
Alan Viverette48728c22016-04-01 15:00:10 -0400950 public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 return true;
952 }
953
954 /**
955 * @see Activity#onOptionsItemSelected(MenuItem)
956 */
Alan Viverette48728c22016-04-01 15:00:10 -0400957 public boolean onOptionsItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 return false;
959 }
960
961 /**
962 * @see Activity#onOptionsMenuClosed(Menu)
963 */
Alan Viverette48728c22016-04-01 15:00:10 -0400964 public void onOptionsMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 }
966
967 /**
968 * @see Activity#openOptionsMenu()
969 */
970 public void openOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -0800971 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
972 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null);
973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 }
Jose Lima7a22fc62015-01-23 17:24:22 -0800975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 /**
977 * @see Activity#closeOptionsMenu()
978 */
979 public void closeOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -0800980 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
981 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL);
982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984
985 /**
Adam Powelle67a9dc2010-08-17 17:28:56 -0700986 * @see Activity#invalidateOptionsMenu()
987 */
988 public void invalidateOptionsMenu() {
Jose Lima7a22fc62015-01-23 17:24:22 -0800989 if (mWindow.hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
990 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
991 }
Adam Powelle67a9dc2010-08-17 17:28:56 -0700992 }
993
994 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 * @see Activity#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)
996 */
Alan Viverette48728c22016-04-01 15:00:10 -0400997 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
999 }
1000
1001 /**
1002 * @see Activity#registerForContextMenu(View)
1003 */
Alan Viverette48728c22016-04-01 15:00:10 -04001004 public void registerForContextMenu(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 view.setOnCreateContextMenuListener(this);
1006 }
1007
1008 /**
1009 * @see Activity#unregisterForContextMenu(View)
1010 */
Alan Viverette48728c22016-04-01 15:00:10 -04001011 public void unregisterForContextMenu(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 view.setOnCreateContextMenuListener(null);
1013 }
1014
1015 /**
1016 * @see Activity#openContextMenu(View)
1017 */
Alan Viverette48728c22016-04-01 15:00:10 -04001018 public void openContextMenu(@NonNull View view) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 view.showContextMenu();
1020 }
1021
1022 /**
1023 * @see Activity#onContextItemSelected(MenuItem)
1024 */
Alan Viverette48728c22016-04-01 15:00:10 -04001025 public boolean onContextItemSelected(@NonNull MenuItem item) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 return false;
1027 }
Clara Bayarri4423d912015-03-02 19:42:48 +00001028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 /**
1030 * @see Activity#onContextMenuClosed(Menu)
1031 */
Alan Viverette48728c22016-04-01 15:00:10 -04001032 public void onContextMenuClosed(@NonNull Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
Clara Bayarri4423d912015-03-02 19:42:48 +00001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 /**
1036 * This hook is called when the user signals the desire to start a search.
1037 */
Alan Viverette48728c22016-04-01 15:00:10 -04001038 @Override
1039 public boolean onSearchRequested(@NonNull SearchEvent searchEvent) {
Tim Kilbourn6a975b32015-04-09 17:14:34 -07001040 mSearchEvent = searchEvent;
1041 return onSearchRequested();
1042 }
1043
1044 /**
1045 * This hook is called when the user signals the desire to start a search.
1046 */
Alan Viverette48728c22016-04-01 15:00:10 -04001047 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 public boolean onSearchRequested() {
Karl Rosaen53d24af2009-07-14 14:58:10 -07001049 final SearchManager searchManager = (SearchManager) mContext
1050 .getSystemService(Context.SEARCH_SERVICE);
1051
Bjorn Bringertb8144a92010-02-22 20:48:57 +00001052 // associate search with owner activity
Karl Rosaen7bafed82009-09-04 11:15:21 -07001053 final ComponentName appName = getAssociatedActivity();
lge-aosp06ca9972011-03-15 10:25:57 +09001054 if (appName != null && searchManager.getSearchableInfo(appName) != null) {
Bjorn Bringertb8144a92010-02-22 20:48:57 +00001055 searchManager.startSearch(null, false, appName, null, false);
1056 dismiss();
1057 return true;
1058 } else {
1059 return false;
1060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
1062
Clara Bayarri4423d912015-03-02 19:42:48 +00001063 /**
Tim Kilbourn6a975b32015-04-09 17:14:34 -07001064 * During the onSearchRequested() callbacks, this function will return the
1065 * {@link SearchEvent} that triggered the callback, if it exists.
1066 *
1067 * @return SearchEvent The SearchEvent that triggered the {@link
1068 * #onSearchRequested} callback.
1069 */
Alan Viverette48728c22016-04-01 15:00:10 -04001070 public final @Nullable SearchEvent getSearchEvent() {
Tim Kilbourn6a975b32015-04-09 17:14:34 -07001071 return mSearchEvent;
1072 }
1073
Clara Bayarri4423d912015-03-02 19:42:48 +00001074 @Override
Adam Powelldebf3be2010-11-15 18:58:48 -08001075 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) {
Clara Bayarri4423d912015-03-02 19:42:48 +00001076 if (mActionBar != null && mActionModeTypeStarting == ActionMode.TYPE_PRIMARY) {
Adam Powelldec9dfd2010-08-09 15:27:54 -07001077 return mActionBar.startActionMode(callback);
1078 }
Adam Powell6e346362010-07-23 10:18:23 -07001079 return null;
1080 }
1081
Clara Bayarri4423d912015-03-02 19:42:48 +00001082 @Override
1083 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) {
1084 try {
1085 mActionModeTypeStarting = type;
1086 return onWindowStartingActionMode(callback);
1087 } finally {
1088 mActionModeTypeStarting = ActionMode.TYPE_PRIMARY;
1089 }
1090 }
1091
1092 /**
1093 * {@inheritDoc}
Adam Powellcfe9aee2011-11-01 14:56:27 -07001094 *
1095 * Note that if you override this method you should always call through
1096 * to the superclass implementation by calling super.onActionModeStarted(mode).
1097 */
Alan Viverette48728c22016-04-01 15:00:10 -04001098 @Override
Tor Norbyec615c6f2015-03-02 10:11:44 -08001099 @CallSuper
Adam Powelldebf3be2010-11-15 18:58:48 -08001100 public void onActionModeStarted(ActionMode mode) {
Adam Powellcfe9aee2011-11-01 14:56:27 -07001101 mActionMode = mode;
Adam Powelldebf3be2010-11-15 18:58:48 -08001102 }
1103
Adam Powellcfe9aee2011-11-01 14:56:27 -07001104 /**
1105 * {@inheritDoc}
1106 *
1107 * Note that if you override this method you should always call through
1108 * to the superclass implementation by calling super.onActionModeFinished(mode).
1109 */
Alan Viverette48728c22016-04-01 15:00:10 -04001110 @Override
Tor Norbyec615c6f2015-03-02 10:11:44 -08001111 @CallSuper
Adam Powelldebf3be2010-11-15 18:58:48 -08001112 public void onActionModeFinished(ActionMode mode) {
Adam Powellcfe9aee2011-11-01 14:56:27 -07001113 if (mode == mActionMode) {
1114 mActionMode = null;
1115 }
Adam Powelldebf3be2010-11-15 18:58:48 -08001116 }
1117
Karl Rosaen7bafed82009-09-04 11:15:21 -07001118 /**
Adam Powelldec9dfd2010-08-09 15:27:54 -07001119 * @return The activity associated with this dialog, or null if there is no associated activity.
Karl Rosaen7bafed82009-09-04 11:15:21 -07001120 */
1121 private ComponentName getAssociatedActivity() {
1122 Activity activity = mOwnerActivity;
1123 Context context = getContext();
1124 while (activity == null && context != null) {
1125 if (context instanceof Activity) {
1126 activity = (Activity) context; // found it!
1127 } else {
1128 context = (context instanceof ContextWrapper) ?
1129 ((ContextWrapper) context).getBaseContext() : // unwrap one level
1130 null; // done
1131 }
1132 }
1133 return activity == null ? null : activity.getComponentName();
1134 }
1135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136
1137 /**
1138 * Request that key events come to this dialog. Use this if your
1139 * dialog has no views with focus, but the dialog still wants
1140 * a chance to process key events.
1141 *
1142 * @param get true if the dialog should receive key events, false otherwise
1143 * @see android.view.Window#takeKeyEvents
1144 */
1145 public void takeKeyEvents(boolean get) {
1146 mWindow.takeKeyEvents(get);
1147 }
1148
1149 /**
1150 * Enable extended window features. This is a convenience for calling
1151 * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
1152 *
1153 * @param featureId The desired feature as defined in
1154 * {@link android.view.Window}.
1155 * @return Returns true if the requested feature is supported and now
1156 * enabled.
1157 *
1158 * @see android.view.Window#requestFeature
1159 */
1160 public final boolean requestWindowFeature(int featureId) {
1161 return getWindow().requestFeature(featureId);
1162 }
1163
1164 /**
1165 * Convenience for calling
1166 * {@link android.view.Window#setFeatureDrawableResource}.
1167 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07001168 public final void setFeatureDrawableResource(int featureId, @DrawableRes int resId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 getWindow().setFeatureDrawableResource(featureId, resId);
1170 }
1171
1172 /**
1173 * Convenience for calling
1174 * {@link android.view.Window#setFeatureDrawableUri}.
1175 */
Alan Viverette48728c22016-04-01 15:00:10 -04001176 public final void setFeatureDrawableUri(int featureId, @Nullable Uri uri) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 getWindow().setFeatureDrawableUri(featureId, uri);
1178 }
1179
1180 /**
1181 * Convenience for calling
1182 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}.
1183 */
Alan Viverette48728c22016-04-01 15:00:10 -04001184 public final void setFeatureDrawable(int featureId, @Nullable Drawable drawable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 getWindow().setFeatureDrawable(featureId, drawable);
1186 }
1187
1188 /**
1189 * Convenience for calling
1190 * {@link android.view.Window#setFeatureDrawableAlpha}.
1191 */
1192 public final void setFeatureDrawableAlpha(int featureId, int alpha) {
1193 getWindow().setFeatureDrawableAlpha(featureId, alpha);
1194 }
1195
Alan Viverette48728c22016-04-01 15:00:10 -04001196 public @NonNull LayoutInflater getLayoutInflater() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 return getWindow().getLayoutInflater();
1198 }
1199
1200 /**
1201 * Sets whether this dialog is cancelable with the
1202 * {@link KeyEvent#KEYCODE_BACK BACK} key.
1203 */
1204 public void setCancelable(boolean flag) {
1205 mCancelable = flag;
Michael Kwanf7964be2016-11-30 16:44:33 -08001206 updateWindowForCancelable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 }
1208
1209 /**
1210 * Sets whether this dialog is canceled when touched outside the window's
1211 * bounds. If setting to true, the dialog is set to be cancelable if not
1212 * already set.
1213 *
1214 * @param cancel Whether the dialog should be canceled when touched outside
1215 * the window.
1216 */
1217 public void setCanceledOnTouchOutside(boolean cancel) {
1218 if (cancel && !mCancelable) {
1219 mCancelable = true;
Michael Kwanf7964be2016-11-30 16:44:33 -08001220 updateWindowForCancelable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
1222
Dianne Hackborncfaf8872011-01-18 13:57:54 -08001223 mWindow.setCloseOnTouchOutside(cancel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
1225
1226 /**
1227 * Cancel the dialog. This is essentially the same as calling {@link #dismiss()}, but it will
1228 * also call your {@link DialogInterface.OnCancelListener} (if registered).
1229 */
Alan Viverette48728c22016-04-01 15:00:10 -04001230 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 public void cancel() {
Dianne Hackborn8b4cac12011-01-03 13:58:14 -08001232 if (!mCanceled && mCancelMessage != null) {
1233 mCanceled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 // Obtain a new message so this dialog can be re-used
1235 Message.obtain(mCancelMessage).sendToTarget();
1236 }
1237 dismiss();
1238 }
1239
1240 /**
1241 * Set a listener to be invoked when the dialog is canceled.
Adam Powell7f02dc52012-08-27 13:35:16 -07001242 *
1243 * <p>This will only be invoked when the dialog is canceled.
1244 * Cancel events alone will not capture all ways that
1245 * the dialog might be dismissed. If the creator needs
1246 * to know when a dialog is dismissed in general, use
1247 * {@link #setOnDismissListener}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 *
1249 * @param listener The {@link DialogInterface.OnCancelListener} to use.
1250 */
Alan Viverette48728c22016-04-01 15:00:10 -04001251 public void setOnCancelListener(@Nullable OnCancelListener listener) {
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001252 if (mCancelAndDismissTaken != null) {
1253 throw new IllegalStateException(
1254 "OnCancelListener is already taken by "
1255 + mCancelAndDismissTaken + " and can not be replaced.");
1256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 if (listener != null) {
Romain Guy045163a2009-07-14 13:59:33 -07001258 mCancelMessage = mListenersHandler.obtainMessage(CANCEL, listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 } else {
1260 mCancelMessage = null;
1261 }
1262 }
1263
1264 /**
1265 * Set a message to be sent when the dialog is canceled.
1266 * @param msg The msg to send when the dialog is canceled.
1267 * @see #setOnCancelListener(android.content.DialogInterface.OnCancelListener)
1268 */
Alan Viverette48728c22016-04-01 15:00:10 -04001269 public void setCancelMessage(@Nullable Message msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 mCancelMessage = msg;
1271 }
1272
1273 /**
1274 * Set a listener to be invoked when the dialog is dismissed.
1275 * @param listener The {@link DialogInterface.OnDismissListener} to use.
1276 */
Alan Viverette48728c22016-04-01 15:00:10 -04001277 public void setOnDismissListener(@Nullable OnDismissListener listener) {
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001278 if (mCancelAndDismissTaken != null) {
1279 throw new IllegalStateException(
1280 "OnDismissListener is already taken by "
1281 + mCancelAndDismissTaken + " and can not be replaced.");
1282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 if (listener != null) {
Romain Guy045163a2009-07-14 13:59:33 -07001284 mDismissMessage = mListenersHandler.obtainMessage(DISMISS, listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 } else {
1286 mDismissMessage = null;
1287 }
1288 }
1289
1290 /**
Romain Guy045163a2009-07-14 13:59:33 -07001291 * Sets a listener to be invoked when the dialog is shown.
Ficus Kirkpatrick130a8b72010-01-14 15:39:43 -08001292 * @param listener The {@link DialogInterface.OnShowListener} to use.
Romain Guy045163a2009-07-14 13:59:33 -07001293 */
Alan Viverette48728c22016-04-01 15:00:10 -04001294 public void setOnShowListener(@Nullable OnShowListener listener) {
Romain Guy045163a2009-07-14 13:59:33 -07001295 if (listener != null) {
1296 mShowMessage = mListenersHandler.obtainMessage(SHOW, listener);
1297 } else {
1298 mShowMessage = null;
1299 }
1300 }
1301
1302 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 * Set a message to be sent when the dialog is dismissed.
1304 * @param msg The msg to send when the dialog is dismissed.
1305 */
Alan Viverette48728c22016-04-01 15:00:10 -04001306 public void setDismissMessage(@Nullable Message msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 mDismissMessage = msg;
1308 }
1309
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001310 /** @hide */
Alan Viverette48728c22016-04-01 15:00:10 -04001311 public boolean takeCancelAndDismissListeners(@Nullable String msg,
1312 @Nullable OnCancelListener cancel, @Nullable OnDismissListener dismiss) {
Dianne Hackbornf812fee2011-01-25 14:54:29 -08001313 if (mCancelAndDismissTaken != null) {
1314 mCancelAndDismissTaken = null;
1315 } else if (mCancelMessage != null || mDismissMessage != null) {
1316 return false;
1317 }
1318
1319 setOnCancelListener(cancel);
1320 setOnDismissListener(dismiss);
1321 mCancelAndDismissTaken = msg;
1322
1323 return true;
1324 }
1325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 /**
1327 * By default, this will use the owner Activity's suggested stream type.
1328 *
1329 * @see Activity#setVolumeControlStream(int)
1330 * @see #setOwnerActivity(Activity)
1331 */
1332 public final void setVolumeControlStream(int streamType) {
1333 getWindow().setVolumeControlStream(streamType);
1334 }
1335
1336 /**
1337 * @see Activity#getVolumeControlStream()
1338 */
1339 public final int getVolumeControlStream() {
1340 return getWindow().getVolumeControlStream();
1341 }
1342
1343 /**
1344 * Sets the callback that will be called if a key is dispatched to the dialog.
1345 */
Alan Viverette48728c22016-04-01 15:00:10 -04001346 public void setOnKeyListener(@Nullable OnKeyListener onKeyListener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 mOnKeyListener = onKeyListener;
1348 }
1349
Romain Guy045163a2009-07-14 13:59:33 -07001350 private static final class ListenersHandler extends Handler {
Alan Viverette48728c22016-04-01 15:00:10 -04001351 private final WeakReference<DialogInterface> mDialog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352
Romain Guy045163a2009-07-14 13:59:33 -07001353 public ListenersHandler(Dialog dialog) {
Alan Viverette48728c22016-04-01 15:00:10 -04001354 mDialog = new WeakReference<>(dialog);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 }
1356
1357 @Override
1358 public void handleMessage(Message msg) {
1359 switch (msg.what) {
1360 case DISMISS:
1361 ((OnDismissListener) msg.obj).onDismiss(mDialog.get());
1362 break;
1363 case CANCEL:
1364 ((OnCancelListener) msg.obj).onCancel(mDialog.get());
1365 break;
Romain Guy045163a2009-07-14 13:59:33 -07001366 case SHOW:
1367 ((OnShowListener) msg.obj).onShow(mDialog.get());
1368 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 }
1370 }
1371 }
Michael Kwanf7964be2016-11-30 16:44:33 -08001372
1373 private void updateWindowForCancelable() {
1374 mWindow.setCloseOnSwipeEnabled(mCancelable);
1375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376}