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