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