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