Dialog.java revision e579b347529a642dc837c2fc37fb483fb7a17fc7
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 /** 625 * Called when a generic motion event was not handled by any of the 626 * views inside of the dialog. 627 * <p> 628 * Generic motion events are dispatched to the focused view to describe 629 * the motions of input devices such as joysticks. The 630 * {@link MotionEvent#getSource() source} of the motion event specifies 631 * the class of input that was received. Implementations of this method 632 * must examine the bits in the source before processing the event. 633 * The following code example shows how this is done. 634 * </p> 635 * <code> 636 * public boolean onGenericMotionEvent(MotionEvent event) { 637 * if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { 638 * float x = event.getX(); 639 * float y = event.getY(); 640 * // process the joystick motion 641 * return true; 642 * } 643 * return super.onGenericMotionEvent(event); 644 * } 645 * </code> 646 * 647 * @param event The generic motion event being processed. 648 * 649 * @return Return true if you have consumed the event, false if you haven't. 650 * The default implementation always returns false. 651 */ 652 public boolean onGenericMotionEvent(MotionEvent event) { 653 return false; 654 } 655 656 public void onWindowAttributesChanged(WindowManager.LayoutParams params) { 657 if (mDecor != null) { 658 mWindowManager.updateViewLayout(mDecor, params); 659 } 660 } 661 662 public void onContentChanged() { 663 } 664 665 public void onWindowFocusChanged(boolean hasFocus) { 666 } 667 668 public void onAttachedToWindow() { 669 } 670 671 public void onDetachedFromWindow() { 672 } 673 674 /** 675 * Called to process key events. You can override this to intercept all 676 * key events before they are dispatched to the window. Be sure to call 677 * this implementation for key events that should be handled normally. 678 * 679 * @param event The key event. 680 * 681 * @return boolean Return true if this event was consumed. 682 */ 683 public boolean dispatchKeyEvent(KeyEvent event) { 684 if ((mOnKeyListener != null) && (mOnKeyListener.onKey(this, event.getKeyCode(), event))) { 685 return true; 686 } 687 if (mWindow.superDispatchKeyEvent(event)) { 688 return true; 689 } 690 return event.dispatch(this, mDecor != null 691 ? mDecor.getKeyDispatcherState() : null, this); 692 } 693 694 /** 695 * Called to process a key shortcut event. 696 * You can override this to intercept all key shortcut events before they are 697 * dispatched to the window. Be sure to call this implementation for key shortcut 698 * events that should be handled normally. 699 * 700 * @param event The key shortcut event. 701 * @return True if this event was consumed. 702 */ 703 public boolean dispatchKeyShortcutEvent(KeyEvent event) { 704 if (mWindow.superDispatchKeyShortcutEvent(event)) { 705 return true; 706 } 707 return onKeyShortcut(event.getKeyCode(), event); 708 } 709 710 /** 711 * Called to process touch screen events. You can override this to 712 * intercept all touch screen events before they are dispatched to the 713 * window. Be sure to call this implementation for touch screen events 714 * that should be handled normally. 715 * 716 * @param ev The touch screen event. 717 * 718 * @return boolean Return true if this event was consumed. 719 */ 720 public boolean dispatchTouchEvent(MotionEvent ev) { 721 if (mWindow.superDispatchTouchEvent(ev)) { 722 return true; 723 } 724 return onTouchEvent(ev); 725 } 726 727 /** 728 * Called to process trackball events. You can override this to 729 * intercept all trackball events before they are dispatched to the 730 * window. Be sure to call this implementation for trackball events 731 * that should be handled normally. 732 * 733 * @param ev The trackball event. 734 * 735 * @return boolean Return true if this event was consumed. 736 */ 737 public boolean dispatchTrackballEvent(MotionEvent ev) { 738 if (mWindow.superDispatchTrackballEvent(ev)) { 739 return true; 740 } 741 return onTrackballEvent(ev); 742 } 743 744 /** 745 * Called to process generic motion events. You can override this to 746 * intercept all generic motion events before they are dispatched to the 747 * window. Be sure to call this implementation for generic motion events 748 * that should be handled normally. 749 * 750 * @param ev The generic motion event. 751 * 752 * @return boolean Return true if this event was consumed. 753 */ 754 public boolean dispatchGenericMotionEvent(MotionEvent ev) { 755 if (mWindow.superDispatchGenericMotionEvent(ev)) { 756 return true; 757 } 758 return onGenericMotionEvent(ev); 759 } 760 761 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 762 event.setClassName(getClass().getName()); 763 event.setPackageName(mContext.getPackageName()); 764 765 LayoutParams params = getWindow().getAttributes(); 766 boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) && 767 (params.height == LayoutParams.MATCH_PARENT); 768 event.setFullScreen(isFullScreen); 769 770 return false; 771 } 772 773 /** 774 * @see Activity#onCreatePanelView(int) 775 */ 776 public View onCreatePanelView(int featureId) { 777 return null; 778 } 779 780 /** 781 * @see Activity#onCreatePanelMenu(int, Menu) 782 */ 783 public boolean onCreatePanelMenu(int featureId, Menu menu) { 784 if (featureId == Window.FEATURE_OPTIONS_PANEL) { 785 return onCreateOptionsMenu(menu); 786 } 787 788 return false; 789 } 790 791 /** 792 * @see Activity#onPreparePanel(int, View, Menu) 793 */ 794 public boolean onPreparePanel(int featureId, View view, Menu menu) { 795 if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { 796 boolean goforit = onPrepareOptionsMenu(menu); 797 return goforit && menu.hasVisibleItems(); 798 } 799 return true; 800 } 801 802 /** 803 * @see Activity#onMenuOpened(int, Menu) 804 */ 805 public boolean onMenuOpened(int featureId, Menu menu) { 806 if (featureId == Window.FEATURE_ACTION_BAR) { 807 mActionBar.dispatchMenuVisibilityChanged(true); 808 } 809 return true; 810 } 811 812 /** 813 * @see Activity#onMenuItemSelected(int, MenuItem) 814 */ 815 public boolean onMenuItemSelected(int featureId, MenuItem item) { 816 return false; 817 } 818 819 /** 820 * @see Activity#onPanelClosed(int, Menu) 821 */ 822 public void onPanelClosed(int featureId, Menu menu) { 823 if (featureId == Window.FEATURE_ACTION_BAR) { 824 mActionBar.dispatchMenuVisibilityChanged(false); 825 } 826 } 827 828 /** 829 * It is usually safe to proxy this call to the owner activity's 830 * {@link Activity#onCreateOptionsMenu(Menu)} if the client desires the same 831 * menu for this Dialog. 832 * 833 * @see Activity#onCreateOptionsMenu(Menu) 834 * @see #getOwnerActivity() 835 */ 836 public boolean onCreateOptionsMenu(Menu menu) { 837 return true; 838 } 839 840 /** 841 * It is usually safe to proxy this call to the owner activity's 842 * {@link Activity#onPrepareOptionsMenu(Menu)} if the client desires the 843 * same menu for this Dialog. 844 * 845 * @see Activity#onPrepareOptionsMenu(Menu) 846 * @see #getOwnerActivity() 847 */ 848 public boolean onPrepareOptionsMenu(Menu menu) { 849 return true; 850 } 851 852 /** 853 * @see Activity#onOptionsItemSelected(MenuItem) 854 */ 855 public boolean onOptionsItemSelected(MenuItem item) { 856 return false; 857 } 858 859 /** 860 * @see Activity#onOptionsMenuClosed(Menu) 861 */ 862 public void onOptionsMenuClosed(Menu menu) { 863 } 864 865 /** 866 * @see Activity#openOptionsMenu() 867 */ 868 public void openOptionsMenu() { 869 mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null); 870 } 871 872 /** 873 * @see Activity#closeOptionsMenu() 874 */ 875 public void closeOptionsMenu() { 876 mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL); 877 } 878 879 /** 880 * @see Activity#invalidateOptionsMenu() 881 */ 882 public void invalidateOptionsMenu() { 883 mWindow.invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL); 884 } 885 886 /** 887 * @see Activity#onCreateContextMenu(ContextMenu, View, ContextMenuInfo) 888 */ 889 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 890 } 891 892 /** 893 * @see Activity#registerForContextMenu(View) 894 */ 895 public void registerForContextMenu(View view) { 896 view.setOnCreateContextMenuListener(this); 897 } 898 899 /** 900 * @see Activity#unregisterForContextMenu(View) 901 */ 902 public void unregisterForContextMenu(View view) { 903 view.setOnCreateContextMenuListener(null); 904 } 905 906 /** 907 * @see Activity#openContextMenu(View) 908 */ 909 public void openContextMenu(View view) { 910 view.showContextMenu(); 911 } 912 913 /** 914 * @see Activity#onContextItemSelected(MenuItem) 915 */ 916 public boolean onContextItemSelected(MenuItem item) { 917 return false; 918 } 919 920 /** 921 * @see Activity#onContextMenuClosed(Menu) 922 */ 923 public void onContextMenuClosed(Menu menu) { 924 } 925 926 /** 927 * This hook is called when the user signals the desire to start a search. 928 */ 929 public boolean onSearchRequested() { 930 final SearchManager searchManager = (SearchManager) mContext 931 .getSystemService(Context.SEARCH_SERVICE); 932 933 // associate search with owner activity 934 final ComponentName appName = getAssociatedActivity(); 935 if (appName != null) { 936 searchManager.startSearch(null, false, appName, null, false); 937 dismiss(); 938 return true; 939 } else { 940 return false; 941 } 942 } 943 944 public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) { 945 if (mActionBar != null) { 946 return mActionBar.startActionMode(callback); 947 } 948 return null; 949 } 950 951 public void onActionModeStarted(ActionMode mode) { 952 } 953 954 public void onActionModeFinished(ActionMode mode) { 955 } 956 957 /** 958 * @return The activity associated with this dialog, or null if there is no associated activity. 959 */ 960 private ComponentName getAssociatedActivity() { 961 Activity activity = mOwnerActivity; 962 Context context = getContext(); 963 while (activity == null && context != null) { 964 if (context instanceof Activity) { 965 activity = (Activity) context; // found it! 966 } else { 967 context = (context instanceof ContextWrapper) ? 968 ((ContextWrapper) context).getBaseContext() : // unwrap one level 969 null; // done 970 } 971 } 972 return activity == null ? null : activity.getComponentName(); 973 } 974 975 976 /** 977 * Request that key events come to this dialog. Use this if your 978 * dialog has no views with focus, but the dialog still wants 979 * a chance to process key events. 980 * 981 * @param get true if the dialog should receive key events, false otherwise 982 * @see android.view.Window#takeKeyEvents 983 */ 984 public void takeKeyEvents(boolean get) { 985 mWindow.takeKeyEvents(get); 986 } 987 988 /** 989 * Enable extended window features. This is a convenience for calling 990 * {@link android.view.Window#requestFeature getWindow().requestFeature()}. 991 * 992 * @param featureId The desired feature as defined in 993 * {@link android.view.Window}. 994 * @return Returns true if the requested feature is supported and now 995 * enabled. 996 * 997 * @see android.view.Window#requestFeature 998 */ 999 public final boolean requestWindowFeature(int featureId) { 1000 return getWindow().requestFeature(featureId); 1001 } 1002 1003 /** 1004 * Convenience for calling 1005 * {@link android.view.Window#setFeatureDrawableResource}. 1006 */ 1007 public final void setFeatureDrawableResource(int featureId, int resId) { 1008 getWindow().setFeatureDrawableResource(featureId, resId); 1009 } 1010 1011 /** 1012 * Convenience for calling 1013 * {@link android.view.Window#setFeatureDrawableUri}. 1014 */ 1015 public final void setFeatureDrawableUri(int featureId, Uri uri) { 1016 getWindow().setFeatureDrawableUri(featureId, uri); 1017 } 1018 1019 /** 1020 * Convenience for calling 1021 * {@link android.view.Window#setFeatureDrawable(int, Drawable)}. 1022 */ 1023 public final void setFeatureDrawable(int featureId, Drawable drawable) { 1024 getWindow().setFeatureDrawable(featureId, drawable); 1025 } 1026 1027 /** 1028 * Convenience for calling 1029 * {@link android.view.Window#setFeatureDrawableAlpha}. 1030 */ 1031 public final void setFeatureDrawableAlpha(int featureId, int alpha) { 1032 getWindow().setFeatureDrawableAlpha(featureId, alpha); 1033 } 1034 1035 public LayoutInflater getLayoutInflater() { 1036 return getWindow().getLayoutInflater(); 1037 } 1038 1039 /** 1040 * Sets whether this dialog is cancelable with the 1041 * {@link KeyEvent#KEYCODE_BACK BACK} key. 1042 */ 1043 public void setCancelable(boolean flag) { 1044 mCancelable = flag; 1045 } 1046 1047 /** 1048 * Sets whether this dialog is canceled when touched outside the window's 1049 * bounds. If setting to true, the dialog is set to be cancelable if not 1050 * already set. 1051 * 1052 * @param cancel Whether the dialog should be canceled when touched outside 1053 * the window. 1054 */ 1055 public void setCanceledOnTouchOutside(boolean cancel) { 1056 if (cancel && !mCancelable) { 1057 mCancelable = true; 1058 } 1059 1060 mWindow.setCloseOnTouchOutside(cancel); 1061 } 1062 1063 /** 1064 * Cancel the dialog. This is essentially the same as calling {@link #dismiss()}, but it will 1065 * also call your {@link DialogInterface.OnCancelListener} (if registered). 1066 */ 1067 public void cancel() { 1068 if (!mCanceled && mCancelMessage != null) { 1069 mCanceled = true; 1070 // Obtain a new message so this dialog can be re-used 1071 Message.obtain(mCancelMessage).sendToTarget(); 1072 } 1073 dismiss(); 1074 } 1075 1076 /** 1077 * Set a listener to be invoked when the dialog is canceled. 1078 * <p> 1079 * This will only be invoked when the dialog is canceled, if the creator 1080 * needs to know when it is dismissed in general, use 1081 * {@link #setOnDismissListener}. 1082 * 1083 * @param listener The {@link DialogInterface.OnCancelListener} to use. 1084 */ 1085 public void setOnCancelListener(final OnCancelListener listener) { 1086 if (mCancelAndDismissTaken != null) { 1087 throw new IllegalStateException( 1088 "OnCancelListener is already taken by " 1089 + mCancelAndDismissTaken + " and can not be replaced."); 1090 } 1091 if (listener != null) { 1092 mCancelMessage = mListenersHandler.obtainMessage(CANCEL, listener); 1093 } else { 1094 mCancelMessage = null; 1095 } 1096 } 1097 1098 /** 1099 * Set a message to be sent when the dialog is canceled. 1100 * @param msg The msg to send when the dialog is canceled. 1101 * @see #setOnCancelListener(android.content.DialogInterface.OnCancelListener) 1102 */ 1103 public void setCancelMessage(final Message msg) { 1104 mCancelMessage = msg; 1105 } 1106 1107 /** 1108 * Set a listener to be invoked when the dialog is dismissed. 1109 * @param listener The {@link DialogInterface.OnDismissListener} to use. 1110 */ 1111 public void setOnDismissListener(final OnDismissListener listener) { 1112 if (mCancelAndDismissTaken != null) { 1113 throw new IllegalStateException( 1114 "OnDismissListener is already taken by " 1115 + mCancelAndDismissTaken + " and can not be replaced."); 1116 } 1117 if (listener != null) { 1118 mDismissMessage = mListenersHandler.obtainMessage(DISMISS, listener); 1119 } else { 1120 mDismissMessage = null; 1121 } 1122 } 1123 1124 /** 1125 * Sets a listener to be invoked when the dialog is shown. 1126 * @param listener The {@link DialogInterface.OnShowListener} to use. 1127 */ 1128 public void setOnShowListener(OnShowListener listener) { 1129 if (listener != null) { 1130 mShowMessage = mListenersHandler.obtainMessage(SHOW, listener); 1131 } else { 1132 mShowMessage = null; 1133 } 1134 } 1135 1136 /** 1137 * Set a message to be sent when the dialog is dismissed. 1138 * @param msg The msg to send when the dialog is dismissed. 1139 */ 1140 public void setDismissMessage(final Message msg) { 1141 mDismissMessage = msg; 1142 } 1143 1144 /** @hide */ 1145 public boolean takeCancelAndDismissListeners(String msg, final OnCancelListener cancel, 1146 final OnDismissListener dismiss) { 1147 if (mCancelAndDismissTaken != null) { 1148 mCancelAndDismissTaken = null; 1149 } else if (mCancelMessage != null || mDismissMessage != null) { 1150 return false; 1151 } 1152 1153 setOnCancelListener(cancel); 1154 setOnDismissListener(dismiss); 1155 mCancelAndDismissTaken = msg; 1156 1157 return true; 1158 } 1159 1160 /** 1161 * By default, this will use the owner Activity's suggested stream type. 1162 * 1163 * @see Activity#setVolumeControlStream(int) 1164 * @see #setOwnerActivity(Activity) 1165 */ 1166 public final void setVolumeControlStream(int streamType) { 1167 getWindow().setVolumeControlStream(streamType); 1168 } 1169 1170 /** 1171 * @see Activity#getVolumeControlStream() 1172 */ 1173 public final int getVolumeControlStream() { 1174 return getWindow().getVolumeControlStream(); 1175 } 1176 1177 /** 1178 * Sets the callback that will be called if a key is dispatched to the dialog. 1179 */ 1180 public void setOnKeyListener(final OnKeyListener onKeyListener) { 1181 mOnKeyListener = onKeyListener; 1182 } 1183 1184 private static final class ListenersHandler extends Handler { 1185 private WeakReference<DialogInterface> mDialog; 1186 1187 public ListenersHandler(Dialog dialog) { 1188 mDialog = new WeakReference<DialogInterface>(dialog); 1189 } 1190 1191 @Override 1192 public void handleMessage(Message msg) { 1193 switch (msg.what) { 1194 case DISMISS: 1195 ((OnDismissListener) msg.obj).onDismiss(mDialog.get()); 1196 break; 1197 case CANCEL: 1198 ((OnCancelListener) msg.obj).onCancel(mDialog.get()); 1199 break; 1200 case SHOW: 1201 ((OnShowListener) msg.obj).onShow(mDialog.get()); 1202 break; 1203 } 1204 } 1205 } 1206} 1207