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