PhoneWindow.java revision 9b4bee0f14bbd137b0797127aff2df46a6321ec5
1/* 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16package com.android.internal.policy.impl; 17 18import static android.view.View.MeasureSpec.AT_MOST; 19import static android.view.View.MeasureSpec.EXACTLY; 20import static android.view.View.MeasureSpec.getMode; 21import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; 22import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; 23import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN; 24import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR; 25import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; 26import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER; 27import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH; 28 29import com.android.internal.view.RootViewSurfaceTaker; 30import com.android.internal.view.StandaloneActionMode; 31import com.android.internal.view.menu.ActionMenuView; 32import com.android.internal.view.menu.ContextMenuBuilder; 33import com.android.internal.view.menu.ListMenuPresenter; 34import com.android.internal.view.menu.IconMenuPresenter; 35import com.android.internal.view.menu.MenuBuilder; 36import com.android.internal.view.menu.MenuDialogHelper; 37import com.android.internal.view.menu.MenuItemImpl; 38import com.android.internal.view.menu.MenuPopupHelper; 39import com.android.internal.view.menu.MenuView; 40import com.android.internal.view.menu.MenuPresenter; 41import com.android.internal.view.menu.SubMenuBuilder; 42import com.android.internal.widget.ActionBarContextView; 43import com.android.internal.widget.ActionBarView; 44 45import android.app.KeyguardManager; 46import android.content.Context; 47import android.content.res.Configuration; 48import android.content.res.TypedArray; 49import android.graphics.Canvas; 50import android.graphics.PixelFormat; 51import android.graphics.Rect; 52import android.graphics.drawable.Drawable; 53import android.media.AudioManager; 54import android.net.Uri; 55import android.os.Bundle; 56import android.os.Parcel; 57import android.os.Parcelable; 58import android.util.AndroidRuntimeException; 59import android.util.DisplayMetrics; 60import android.util.EventLog; 61import android.util.Log; 62import android.util.SparseArray; 63import android.util.TypedValue; 64import android.view.ActionMode; 65import android.view.Gravity; 66import android.view.InputQueue; 67import android.view.KeyCharacterMap; 68import android.view.KeyEvent; 69import android.view.LayoutInflater; 70import android.view.Menu; 71import android.view.MenuItem; 72import android.view.MotionEvent; 73import android.view.SurfaceHolder; 74import android.view.View; 75import android.view.ViewGroup; 76import android.view.ViewManager; 77import android.view.ViewStub; 78import android.view.Window; 79import android.view.WindowManager; 80import android.view.accessibility.AccessibilityEvent; 81import android.view.accessibility.AccessibilityManager; 82import android.view.animation.Animation; 83import android.view.animation.AnimationUtils; 84import android.widget.FrameLayout; 85import android.widget.ImageView; 86import android.widget.PopupWindow; 87import android.widget.ProgressBar; 88import android.widget.TextView; 89 90/** 91 * Android-specific Window. 92 * <p> 93 * todo: need to pull the generic functionality out into a base class 94 * in android.widget. 95 */ 96public class PhoneWindow extends Window implements MenuBuilder.Callback { 97 98 private final static String TAG = "PhoneWindow"; 99 100 private final static boolean SWEEP_OPEN_MENU = false; 101 102 /** 103 * Simple callback used by the context menu and its submenus. The options 104 * menu submenus do not use this (their behavior is more complex). 105 */ 106 final DialogMenuCallback mContextMenuCallback = new DialogMenuCallback(FEATURE_CONTEXT_MENU); 107 108 final TypedValue mMinWidthMajor = new TypedValue(); 109 final TypedValue mMinWidthMinor = new TypedValue(); 110 111 // This is the top-level view of the window, containing the window decor. 112 private DecorView mDecor; 113 114 // This is the view in which the window contents are placed. It is either 115 // mDecor itself, or a child of mDecor where the contents go. 116 private ViewGroup mContentParent; 117 118 SurfaceHolder.Callback2 mTakeSurfaceCallback; 119 120 InputQueue.Callback mTakeInputQueueCallback; 121 122 private boolean mIsFloating; 123 124 private LayoutInflater mLayoutInflater; 125 126 private TextView mTitleView; 127 128 private ActionBarView mActionBar; 129 private ActionMenuPresenterCallback mActionMenuPresenterCallback; 130 private PanelMenuPresenterCallback mPanelMenuPresenterCallback; 131 132 private DrawableFeatureState[] mDrawables; 133 134 private PanelFeatureState[] mPanels; 135 136 /** 137 * The panel that is prepared or opened (the most recent one if there are 138 * multiple panels). Shortcuts will go to this panel. It gets set in 139 * {@link #preparePanel} and cleared in {@link #closePanel}. 140 */ 141 private PanelFeatureState mPreparedPanel; 142 143 /** 144 * The keycode that is currently held down (as a modifier) for chording. If 145 * this is 0, there is no key held down. 146 */ 147 private int mPanelChordingKey; 148 149 private ImageView mLeftIconView; 150 151 private ImageView mRightIconView; 152 153 private ProgressBar mCircularProgressBar; 154 155 private ProgressBar mHorizontalProgressBar; 156 157 private int mBackgroundResource = 0; 158 159 private Drawable mBackgroundDrawable; 160 161 private int mFrameResource = 0; 162 163 private int mTextColor = 0; 164 165 private CharSequence mTitle = null; 166 167 private int mTitleColor = 0; 168 169 private boolean mAlwaysReadCloseOnTouchAttr = false; 170 171 private ContextMenuBuilder mContextMenu; 172 private MenuDialogHelper mContextMenuHelper; 173 private boolean mClosingActionMenu; 174 175 private int mVolumeControlStreamType = AudioManager.USE_DEFAULT_STREAM_TYPE; 176 177 private AudioManager mAudioManager; 178 private KeyguardManager mKeyguardManager; 179 180 public PhoneWindow(Context context) { 181 super(context); 182 mLayoutInflater = LayoutInflater.from(context); 183 } 184 185 @Override 186 public final void setContainer(Window container) { 187 super.setContainer(container); 188 } 189 190 @Override 191 public boolean requestFeature(int featureId) { 192 if (mContentParent != null) { 193 throw new AndroidRuntimeException("requestFeature() must be called before adding content"); 194 } 195 final int features = getFeatures(); 196 if ((features != DEFAULT_FEATURES) && (featureId == FEATURE_CUSTOM_TITLE)) { 197 198 /* Another feature is enabled and the user is trying to enable the custom title feature */ 199 throw new AndroidRuntimeException("You cannot combine custom titles with other title features"); 200 } 201 if (((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) && 202 (featureId != FEATURE_CUSTOM_TITLE) && (featureId != FEATURE_ACTION_MODE_OVERLAY)) { 203 204 /* Custom title feature is enabled and the user is trying to enable another feature */ 205 throw new AndroidRuntimeException("You cannot combine custom titles with other title features"); 206 } 207 if ((features & (1 << FEATURE_NO_TITLE)) != 0 && featureId == FEATURE_ACTION_BAR) { 208 return false; // Ignore. No title dominates. 209 } 210 if ((features & (1 << FEATURE_ACTION_BAR)) != 0 && featureId == FEATURE_NO_TITLE) { 211 // Remove the action bar feature if we have no title. No title dominates. 212 removeFeature(FEATURE_ACTION_BAR); 213 } 214 return super.requestFeature(featureId); 215 } 216 217 @Override 218 public void setContentView(int layoutResID) { 219 if (mContentParent == null) { 220 installDecor(); 221 } else { 222 mContentParent.removeAllViews(); 223 } 224 mLayoutInflater.inflate(layoutResID, mContentParent); 225 final Callback cb = getCallback(); 226 if (cb != null && !isDestroyed()) { 227 cb.onContentChanged(); 228 } 229 } 230 231 @Override 232 public void setContentView(View view) { 233 setContentView(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); 234 } 235 236 @Override 237 public void setContentView(View view, ViewGroup.LayoutParams params) { 238 if (mContentParent == null) { 239 installDecor(); 240 } else { 241 mContentParent.removeAllViews(); 242 } 243 mContentParent.addView(view, params); 244 final Callback cb = getCallback(); 245 if (cb != null && !isDestroyed()) { 246 cb.onContentChanged(); 247 } 248 } 249 250 @Override 251 public void addContentView(View view, ViewGroup.LayoutParams params) { 252 if (mContentParent == null) { 253 installDecor(); 254 } 255 mContentParent.addView(view, params); 256 final Callback cb = getCallback(); 257 if (cb != null && !isDestroyed()) { 258 cb.onContentChanged(); 259 } 260 } 261 262 @Override 263 public View getCurrentFocus() { 264 return mDecor != null ? mDecor.findFocus() : null; 265 } 266 267 @Override 268 public void takeSurface(SurfaceHolder.Callback2 callback) { 269 mTakeSurfaceCallback = callback; 270 } 271 272 public void takeInputQueue(InputQueue.Callback callback) { 273 mTakeInputQueueCallback = callback; 274 } 275 276 @Override 277 public boolean isFloating() { 278 return mIsFloating; 279 } 280 281 /** 282 * Return a LayoutInflater instance that can be used to inflate XML view layout 283 * resources for use in this Window. 284 * 285 * @return LayoutInflater The shared LayoutInflater. 286 */ 287 @Override 288 public LayoutInflater getLayoutInflater() { 289 return mLayoutInflater; 290 } 291 292 @Override 293 public void setTitle(CharSequence title) { 294 if (mTitleView != null) { 295 mTitleView.setText(title); 296 } else if (mActionBar != null) { 297 mActionBar.setWindowTitle(title); 298 } 299 mTitle = title; 300 } 301 302 @Override 303 public void setTitleColor(int textColor) { 304 if (mTitleView != null) { 305 mTitleView.setTextColor(textColor); 306 } 307 mTitleColor = textColor; 308 } 309 310 /** 311 * Prepares the panel to either be opened or chorded. This creates the Menu 312 * instance for the panel and populates it via the Activity callbacks. 313 * 314 * @param st The panel state to prepare. 315 * @param event The event that triggered the preparing of the panel. 316 * @return Whether the panel was prepared. If the panel should not be shown, 317 * returns false. 318 */ 319 public final boolean preparePanel(PanelFeatureState st, KeyEvent event) { 320 if (isDestroyed()) { 321 return false; 322 } 323 324 // Already prepared (isPrepared will be reset to false later) 325 if (st.isPrepared) 326 return true; 327 328 if ((mPreparedPanel != null) && (mPreparedPanel != st)) { 329 // Another Panel is prepared and possibly open, so close it 330 closePanel(mPreparedPanel, false); 331 } 332 333 final Callback cb = getCallback(); 334 335 if (cb != null) { 336 st.createdPanelView = cb.onCreatePanelView(st.featureId); 337 } 338 339 if (st.createdPanelView == null) { 340 // Init the panel state's menu--return false if init failed 341 if (st.menu == null || st.refreshMenuContent) { 342 if (st.menu == null) { 343 if (!initializePanelMenu(st) || (st.menu == null)) { 344 return false; 345 } 346 } 347 348 // Call callback, and return if it doesn't want to display menu. 349 350 // Creating the panel menu will involve a lot of manipulation; 351 // don't dispatch change events to presenters until we're done. 352 st.menu.stopDispatchingItemsChanged(); 353 if ((cb == null) || !cb.onCreatePanelMenu(st.featureId, st.menu)) { 354 // Ditch the menu created above 355 st.menu = null; 356 357 return false; 358 } 359 360 st.refreshMenuContent = false; 361 362 if (mActionBar != null) { 363 if (mActionMenuPresenterCallback == null) { 364 mActionMenuPresenterCallback = new ActionMenuPresenterCallback(); 365 } 366 mActionBar.setMenu(st.menu, mActionMenuPresenterCallback); 367 } 368 } 369 370 // Callback and return if the callback does not want to show the menu 371 372 // Preparing the panel menu can involve a lot of manipulation; 373 // don't dispatch change events to presenters until we're done. 374 st.menu.stopDispatchingItemsChanged(); 375 if (!cb.onPreparePanel(st.featureId, st.createdPanelView, st.menu)) { 376 st.menu.startDispatchingItemsChanged(); 377 return false; 378 } 379 st.menu.startDispatchingItemsChanged(); 380 381 // Set the proper keymap 382 KeyCharacterMap kmap = KeyCharacterMap.load( 383 event != null ? event.getDeviceId() : KeyCharacterMap.VIRTUAL_KEYBOARD); 384 st.qwertyMode = kmap.getKeyboardType() != KeyCharacterMap.NUMERIC; 385 st.menu.setQwertyMode(st.qwertyMode); 386 } 387 388 // Set other state 389 st.isPrepared = true; 390 st.isHandled = false; 391 mPreparedPanel = st; 392 393 return true; 394 } 395 396 @Override 397 public void onConfigurationChanged(Configuration newConfig) { 398 // Action bars handle their own menu state 399 if (mActionBar == null) { 400 PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false); 401 if ((st != null) && (st.menu != null)) { 402 if (st.isOpen) { 403 // Freeze state 404 final Bundle state = new Bundle(); 405 if (st.iconMenuPresenter != null) { 406 st.iconMenuPresenter.saveHierarchyState(state); 407 } 408 if (st.expandedMenuPresenter != null) { 409 st.expandedMenuPresenter.saveHierarchyState(state); 410 } 411 412 // Remove the menu views since they need to be recreated 413 // according to the new configuration 414 clearMenuViews(st); 415 416 // Re-open the same menu 417 reopenMenu(false); 418 419 // Restore state 420 if (st.iconMenuPresenter != null) { 421 st.iconMenuPresenter.restoreHierarchyState(state); 422 } 423 if (st.expandedMenuPresenter != null) { 424 st.expandedMenuPresenter.restoreHierarchyState(state); 425 } 426 427 } else { 428 // Clear menu views so on next menu opening, it will use 429 // the proper layout 430 clearMenuViews(st); 431 } 432 } 433 } 434 } 435 436 private static void clearMenuViews(PanelFeatureState st) { 437 // This can be called on config changes, so we should make sure 438 // the views will be reconstructed based on the new orientation, etc. 439 440 // Allow the callback to create a new panel view 441 st.createdPanelView = null; 442 443 // Causes the decor view to be recreated 444 st.refreshDecorView = true; 445 446 st.clearMenuPresenters(); 447 } 448 449 @Override 450 public final void openPanel(int featureId, KeyEvent event) { 451 if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null && 452 mActionBar.isOverflowReserved()) { 453 if (mActionBar.getVisibility() == View.VISIBLE) { 454 // Invalidate the options menu, we want a prepare event that the app can respond to. 455 invalidatePanelMenu(FEATURE_OPTIONS_PANEL); 456 mActionBar.showOverflowMenu(); 457 } 458 } else { 459 openPanel(getPanelState(featureId, true), event); 460 } 461 } 462 463 private void openPanel(PanelFeatureState st, KeyEvent event) { 464 // System.out.println("Open panel: isOpen=" + st.isOpen); 465 466 // Already open, return 467 if (st.isOpen || isDestroyed()) { 468 return; 469 } 470 471 // Don't open an options panel for honeycomb apps on xlarge devices. 472 // (The app should be using an action bar for menu items.) 473 if (st.featureId == FEATURE_OPTIONS_PANEL) { 474 Context context = getContext(); 475 Configuration config = context.getResources().getConfiguration(); 476 boolean isXLarge = (config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 477 Configuration.SCREENLAYOUT_SIZE_XLARGE; 478 boolean isHoneycombApp = context.getApplicationInfo().targetSdkVersion >= 479 android.os.Build.VERSION_CODES.HONEYCOMB; 480 481 if (isXLarge && isHoneycombApp) { 482 return; 483 } 484 } 485 486 Callback cb = getCallback(); 487 if ((cb != null) && (!cb.onMenuOpened(st.featureId, st.menu))) { 488 // Callback doesn't want the menu to open, reset any state 489 closePanel(st, true); 490 return; 491 } 492 493 final WindowManager wm = getWindowManager(); 494 if (wm == null) { 495 return; 496 } 497 498 // Prepare panel (should have been done before, but just in case) 499 if (!preparePanel(st, event)) { 500 return; 501 } 502 503 if (st.decorView == null || st.refreshDecorView) { 504 if (st.decorView == null) { 505 // Initialize the panel decor, this will populate st.decorView 506 if (!initializePanelDecor(st) || (st.decorView == null)) 507 return; 508 } else if (st.refreshDecorView && (st.decorView.getChildCount() > 0)) { 509 // Decor needs refreshing, so remove its views 510 st.decorView.removeAllViews(); 511 } 512 513 // This will populate st.shownPanelView 514 if (!initializePanelContent(st) || !st.hasPanelItems()) { 515 return; 516 } 517 518 ViewGroup.LayoutParams lp = st.shownPanelView.getLayoutParams(); 519 if (lp == null) { 520 lp = new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT); 521 } 522 523 int backgroundResId; 524 if (lp.width == ViewGroup.LayoutParams.MATCH_PARENT) { 525 // If the contents is fill parent for the width, set the 526 // corresponding background 527 backgroundResId = st.fullBackground; 528 } else { 529 // Otherwise, set the normal panel background 530 backgroundResId = st.background; 531 } 532 st.decorView.setWindowBackground(getContext().getResources().getDrawable( 533 backgroundResId)); 534 535 536 st.decorView.addView(st.shownPanelView, lp); 537 538 /* 539 * Give focus to the view, if it or one of its children does not 540 * already have it. 541 */ 542 if (!st.shownPanelView.hasFocus()) { 543 st.shownPanelView.requestFocus(); 544 } 545 } 546 547 st.isOpen = true; 548 st.isHandled = false; 549 550 WindowManager.LayoutParams lp = new WindowManager.LayoutParams( 551 WRAP_CONTENT, WRAP_CONTENT, 552 st.x, st.y, WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG, 553 WindowManager.LayoutParams.FLAG_DITHER 554 | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM 555 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH, 556 st.decorView.mDefaultOpacity); 557 558 lp.gravity = st.gravity; 559 lp.windowAnimations = st.windowAnimations; 560 561 wm.addView(st.decorView, lp); 562 // Log.v(TAG, "Adding main menu to window manager."); 563 } 564 565 @Override 566 public final void closePanel(int featureId) { 567 if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null && 568 mActionBar.isOverflowReserved()) { 569 mActionBar.hideOverflowMenu(); 570 } else if (featureId == FEATURE_CONTEXT_MENU) { 571 closeContextMenu(); 572 } else { 573 closePanel(getPanelState(featureId, true), true); 574 } 575 } 576 577 /** 578 * Closes the given panel. 579 * 580 * @param st The panel to be closed. 581 * @param doCallback Whether to notify the callback that the panel was 582 * closed. If the panel is in the process of re-opening or 583 * opening another panel (e.g., menu opening a sub menu), the 584 * callback should not happen and this variable should be false. 585 * In addition, this method internally will only perform the 586 * callback if the panel is open. 587 */ 588 public final void closePanel(PanelFeatureState st, boolean doCallback) { 589 // System.out.println("Close panel: isOpen=" + st.isOpen); 590 if (doCallback && st.featureId == FEATURE_OPTIONS_PANEL && 591 mActionBar != null && mActionBar.isOverflowMenuShowing()) { 592 checkCloseActionMenu(st.menu); 593 return; 594 } 595 596 final ViewManager wm = getWindowManager(); 597 if ((wm != null) && st.isOpen) { 598 if (st.decorView != null) { 599 wm.removeView(st.decorView); 600 // Log.v(TAG, "Removing main menu from window manager."); 601 } 602 603 if (doCallback) { 604 callOnPanelClosed(st.featureId, st, null); 605 } 606 } 607 608 st.isPrepared = false; 609 st.isHandled = false; 610 st.isOpen = false; 611 612 // This view is no longer shown, so null it out 613 st.shownPanelView = null; 614 615 if (st.isInExpandedMode) { 616 // Next time the menu opens, it should not be in expanded mode, so 617 // force a refresh of the decor 618 st.refreshDecorView = true; 619 st.isInExpandedMode = false; 620 } 621 622 if (mPreparedPanel == st) { 623 mPreparedPanel = null; 624 mPanelChordingKey = 0; 625 } 626 } 627 628 private void checkCloseActionMenu(Menu menu) { 629 if (mClosingActionMenu) { 630 return; 631 } 632 633 mClosingActionMenu = true; 634 mActionBar.dismissPopupMenus(); 635 Callback cb = getCallback(); 636 if (cb != null && !isDestroyed()) { 637 cb.onPanelClosed(FEATURE_ACTION_BAR, menu); 638 } 639 mClosingActionMenu = false; 640 } 641 642 @Override 643 public final void togglePanel(int featureId, KeyEvent event) { 644 PanelFeatureState st = getPanelState(featureId, true); 645 if (st.isOpen) { 646 closePanel(st, true); 647 } else { 648 openPanel(st, event); 649 } 650 } 651 652 @Override 653 public void invalidatePanelMenu(int featureId) { 654 PanelFeatureState st = getPanelState(featureId, true); 655 if (st.menu != null) { 656 st.menu.clear(); 657 } 658 st.refreshMenuContent = true; 659 st.refreshDecorView = true; 660 661 // Prepare the options panel if we have an action bar 662 if ((featureId == FEATURE_ACTION_BAR || featureId == FEATURE_OPTIONS_PANEL) 663 && mActionBar != null) { 664 st = getPanelState(Window.FEATURE_OPTIONS_PANEL, false); 665 if (st != null) { 666 st.isPrepared = false; 667 preparePanel(st, null); 668 } 669 } 670 } 671 672 /** 673 * Called when the panel key is pushed down. 674 * @param featureId The feature ID of the relevant panel (defaults to FEATURE_OPTIONS_PANEL}. 675 * @param event The key event. 676 * @return Whether the key was handled. 677 */ 678 public final boolean onKeyDownPanel(int featureId, KeyEvent event) { 679 final int keyCode = event.getKeyCode(); 680 681 if (event.getRepeatCount() == 0) { 682 // The panel key was pushed, so set the chording key 683 mPanelChordingKey = keyCode; 684 685 PanelFeatureState st = getPanelState(featureId, true); 686 if (!st.isOpen) { 687 return preparePanel(st, event); 688 } 689 } 690 691 return false; 692 } 693 694 /** 695 * Called when the panel key is released. 696 * @param featureId The feature ID of the relevant panel (defaults to FEATURE_OPTIONS_PANEL}. 697 * @param event The key event. 698 */ 699 public final void onKeyUpPanel(int featureId, KeyEvent event) { 700 // The panel key was released, so clear the chording key 701 if (mPanelChordingKey != 0) { 702 mPanelChordingKey = 0; 703 704 if (event.isCanceled()) { 705 return; 706 } 707 708 boolean playSoundEffect = false; 709 final PanelFeatureState st = getPanelState(featureId, true); 710 if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null && 711 mActionBar.isOverflowReserved()) { 712 if (mActionBar.getVisibility() == View.VISIBLE) { 713 if (!mActionBar.isOverflowMenuShowing()) { 714 final Callback cb = getCallback(); 715 if (cb != null && !isDestroyed() && 716 cb.onPreparePanel(featureId, st.createdPanelView, st.menu)) { 717 playSoundEffect = mActionBar.showOverflowMenu(); 718 } 719 } else { 720 playSoundEffect = mActionBar.hideOverflowMenu(); 721 } 722 } 723 } else { 724 if (st.isOpen || st.isHandled) { 725 726 // Play the sound effect if the user closed an open menu (and not if 727 // they just released a menu shortcut) 728 playSoundEffect = st.isOpen; 729 730 // Close menu 731 closePanel(st, true); 732 733 } else if (st.isPrepared) { 734 735 // Write 'menu opened' to event log 736 EventLog.writeEvent(50001, 0); 737 738 // Show menu 739 openPanel(st, event); 740 741 playSoundEffect = true; 742 } 743 } 744 745 if (playSoundEffect) { 746 AudioManager audioManager = (AudioManager) getContext().getSystemService( 747 Context.AUDIO_SERVICE); 748 if (audioManager != null) { 749 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK); 750 } else { 751 Log.w(TAG, "Couldn't get audio manager"); 752 } 753 } 754 } 755 } 756 757 @Override 758 public final void closeAllPanels() { 759 final ViewManager wm = getWindowManager(); 760 if (wm == null) { 761 return; 762 } 763 764 final PanelFeatureState[] panels = mPanels; 765 final int N = panels != null ? panels.length : 0; 766 for (int i = 0; i < N; i++) { 767 final PanelFeatureState panel = panels[i]; 768 if (panel != null) { 769 closePanel(panel, true); 770 } 771 } 772 773 closeContextMenu(); 774 } 775 776 /** 777 * Closes the context menu. This notifies the menu logic of the close, along 778 * with dismissing it from the UI. 779 */ 780 private synchronized void closeContextMenu() { 781 if (mContextMenu != null) { 782 mContextMenu.close(); 783 dismissContextMenu(); 784 } 785 } 786 787 /** 788 * Dismisses just the context menu UI. To close the context menu, use 789 * {@link #closeContextMenu()}. 790 */ 791 private synchronized void dismissContextMenu() { 792 mContextMenu = null; 793 794 if (mContextMenuHelper != null) { 795 mContextMenuHelper.dismiss(); 796 mContextMenuHelper = null; 797 } 798 } 799 800 @Override 801 public boolean performPanelShortcut(int featureId, int keyCode, KeyEvent event, int flags) { 802 return performPanelShortcut(getPanelState(featureId, true), keyCode, event, flags); 803 } 804 805 private boolean performPanelShortcut(PanelFeatureState st, int keyCode, KeyEvent event, 806 int flags) { 807 if (event.isSystem() || (st == null)) { 808 return false; 809 } 810 811 boolean handled = false; 812 813 // Only try to perform menu shortcuts if preparePanel returned true (possible false 814 // return value from application not wanting to show the menu). 815 if ((st.isPrepared || preparePanel(st, event)) && st.menu != null) { 816 // The menu is prepared now, perform the shortcut on it 817 handled = st.menu.performShortcut(keyCode, event, flags); 818 } 819 820 if (handled) { 821 // Mark as handled 822 st.isHandled = true; 823 824 if ((flags & Menu.FLAG_PERFORM_NO_CLOSE) == 0) { 825 closePanel(st, true); 826 } 827 } 828 829 return handled; 830 } 831 832 @Override 833 public boolean performPanelIdentifierAction(int featureId, int id, int flags) { 834 835 PanelFeatureState st = getPanelState(featureId, true); 836 if (!preparePanel(st, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU))) { 837 return false; 838 } 839 if (st.menu == null) { 840 return false; 841 } 842 843 boolean res = st.menu.performIdentifierAction(id, flags); 844 845 closePanel(st, true); 846 847 return res; 848 } 849 850 public PanelFeatureState findMenuPanel(Menu menu) { 851 final PanelFeatureState[] panels = mPanels; 852 final int N = panels != null ? panels.length : 0; 853 for (int i = 0; i < N; i++) { 854 final PanelFeatureState panel = panels[i]; 855 if (panel != null && panel.menu == menu) { 856 return panel; 857 } 858 } 859 return null; 860 } 861 862 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { 863 final Callback cb = getCallback(); 864 if (cb != null && !isDestroyed()) { 865 final PanelFeatureState panel = findMenuPanel(menu.getRootMenu()); 866 if (panel != null) { 867 return cb.onMenuItemSelected(panel.featureId, item); 868 } 869 } 870 return false; 871 } 872 873 public void onMenuModeChange(MenuBuilder menu) { 874 reopenMenu(true); 875 } 876 877 private void reopenMenu(boolean toggleMenuMode) { 878 if (mActionBar != null && mActionBar.isOverflowReserved()) { 879 final Callback cb = getCallback(); 880 if (!mActionBar.isOverflowMenuShowing() || !toggleMenuMode) { 881 if (cb != null && !isDestroyed() && mActionBar.getVisibility() == View.VISIBLE) { 882 final PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true); 883 if (cb.onPreparePanel(FEATURE_OPTIONS_PANEL, st.createdPanelView, st.menu)) { 884 cb.onMenuOpened(FEATURE_ACTION_BAR, st.menu); 885 mActionBar.openOverflowMenu(); 886 } 887 } 888 } else { 889 mActionBar.hideOverflowMenu(); 890 if (cb != null && !isDestroyed()) { 891 final PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true); 892 cb.onPanelClosed(FEATURE_ACTION_BAR, st.menu); 893 } 894 } 895 return; 896 } 897 898 PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true); 899 900 // Save the future expanded mode state since closePanel will reset it 901 boolean newExpandedMode = toggleMenuMode ? !st.isInExpandedMode : st.isInExpandedMode; 902 903 st.refreshDecorView = true; 904 closePanel(st, false); 905 906 // Set the expanded mode state 907 st.isInExpandedMode = newExpandedMode; 908 909 openPanel(st, null); 910 } 911 912 /** 913 * Initializes the menu associated with the given panel feature state. You 914 * must at the very least set PanelFeatureState.menu to the Menu to be 915 * associated with the given panel state. The default implementation creates 916 * a new menu for the panel state. 917 * 918 * @param st The panel whose menu is being initialized. 919 * @return Whether the initialization was successful. 920 */ 921 protected boolean initializePanelMenu(final PanelFeatureState st) { 922 final MenuBuilder menu = new MenuBuilder(getContext()); 923 924 menu.setCallback(this); 925 st.setMenu(menu); 926 927 return true; 928 } 929 930 /** 931 * Perform initial setup of a panel. This should at the very least set the 932 * style information in the PanelFeatureState and must set 933 * PanelFeatureState.decor to the panel's window decor view. 934 * 935 * @param st The panel being initialized. 936 */ 937 protected boolean initializePanelDecor(PanelFeatureState st) { 938 st.decorView = new DecorView(getContext(), st.featureId); 939 st.gravity = Gravity.CENTER | Gravity.BOTTOM; 940 st.setStyle(getContext()); 941 942 return true; 943 } 944 945 /** 946 * Initializes the panel associated with the panel feature state. You must 947 * at the very least set PanelFeatureState.panel to the View implementing 948 * its contents. The default implementation gets the panel from the menu. 949 * 950 * @param st The panel state being initialized. 951 * @return Whether the initialization was successful. 952 */ 953 protected boolean initializePanelContent(PanelFeatureState st) { 954 if (st.createdPanelView != null) { 955 st.shownPanelView = st.createdPanelView; 956 return true; 957 } 958 959 if (st.menu == null) { 960 return false; 961 } 962 963 if (mPanelMenuPresenterCallback == null) { 964 mPanelMenuPresenterCallback = new PanelMenuPresenterCallback(); 965 } 966 967 MenuView menuView = st.isInExpandedMode 968 ? st.getExpandedMenuView(mPanelMenuPresenterCallback) 969 : st.getIconMenuView(mPanelMenuPresenterCallback); 970 971 st.shownPanelView = (View) menuView; 972 973 if (st.shownPanelView != null) { 974 // Use the menu View's default animations if it has any 975 final int defaultAnimations = menuView.getWindowAnimations(); 976 if (defaultAnimations != 0) { 977 st.windowAnimations = defaultAnimations; 978 } 979 return true; 980 } else { 981 return false; 982 } 983 } 984 985 @Override 986 public boolean performContextMenuIdentifierAction(int id, int flags) { 987 return (mContextMenu != null) ? mContextMenu.performIdentifierAction(id, flags) : false; 988 } 989 990 @Override 991 public final void setBackgroundDrawable(Drawable drawable) { 992 if (drawable != mBackgroundDrawable || mBackgroundResource != 0) { 993 mBackgroundResource = 0; 994 mBackgroundDrawable = drawable; 995 if (mDecor != null) { 996 mDecor.setWindowBackground(drawable); 997 } 998 } 999 } 1000 1001 @Override 1002 public final void setFeatureDrawableResource(int featureId, int resId) { 1003 if (resId != 0) { 1004 DrawableFeatureState st = getDrawableState(featureId, true); 1005 if (st.resid != resId) { 1006 st.resid = resId; 1007 st.uri = null; 1008 st.local = getContext().getResources().getDrawable(resId); 1009 updateDrawable(featureId, st, false); 1010 } 1011 } else { 1012 setFeatureDrawable(featureId, null); 1013 } 1014 } 1015 1016 @Override 1017 public final void setFeatureDrawableUri(int featureId, Uri uri) { 1018 if (uri != null) { 1019 DrawableFeatureState st = getDrawableState(featureId, true); 1020 if (st.uri == null || !st.uri.equals(uri)) { 1021 st.resid = 0; 1022 st.uri = uri; 1023 st.local = loadImageURI(uri); 1024 updateDrawable(featureId, st, false); 1025 } 1026 } else { 1027 setFeatureDrawable(featureId, null); 1028 } 1029 } 1030 1031 @Override 1032 public final void setFeatureDrawable(int featureId, Drawable drawable) { 1033 DrawableFeatureState st = getDrawableState(featureId, true); 1034 st.resid = 0; 1035 st.uri = null; 1036 if (st.local != drawable) { 1037 st.local = drawable; 1038 updateDrawable(featureId, st, false); 1039 } 1040 } 1041 1042 @Override 1043 public void setFeatureDrawableAlpha(int featureId, int alpha) { 1044 DrawableFeatureState st = getDrawableState(featureId, true); 1045 if (st.alpha != alpha) { 1046 st.alpha = alpha; 1047 updateDrawable(featureId, st, false); 1048 } 1049 } 1050 1051 protected final void setFeatureDefaultDrawable(int featureId, Drawable drawable) { 1052 DrawableFeatureState st = getDrawableState(featureId, true); 1053 if (st.def != drawable) { 1054 st.def = drawable; 1055 updateDrawable(featureId, st, false); 1056 } 1057 } 1058 1059 @Override 1060 public final void setFeatureInt(int featureId, int value) { 1061 // XXX Should do more management (as with drawable features) to 1062 // deal with interactions between multiple window policies. 1063 updateInt(featureId, value, false); 1064 } 1065 1066 /** 1067 * Update the state of a drawable feature. This should be called, for every 1068 * drawable feature supported, as part of onActive(), to make sure that the 1069 * contents of a containing window is properly updated. 1070 * 1071 * @see #onActive 1072 * @param featureId The desired drawable feature to change. 1073 * @param fromActive Always true when called from onActive(). 1074 */ 1075 protected final void updateDrawable(int featureId, boolean fromActive) { 1076 final DrawableFeatureState st = getDrawableState(featureId, false); 1077 if (st != null) { 1078 updateDrawable(featureId, st, fromActive); 1079 } 1080 } 1081 1082 /** 1083 * Called when a Drawable feature changes, for the window to update its 1084 * graphics. 1085 * 1086 * @param featureId The feature being changed. 1087 * @param drawable The new Drawable to show, or null if none. 1088 * @param alpha The new alpha blending of the Drawable. 1089 */ 1090 protected void onDrawableChanged(int featureId, Drawable drawable, int alpha) { 1091 ImageView view; 1092 if (featureId == FEATURE_LEFT_ICON) { 1093 view = getLeftIconView(); 1094 } else if (featureId == FEATURE_RIGHT_ICON) { 1095 view = getRightIconView(); 1096 } else { 1097 return; 1098 } 1099 1100 if (drawable != null) { 1101 drawable.setAlpha(alpha); 1102 view.setImageDrawable(drawable); 1103 view.setVisibility(View.VISIBLE); 1104 } else { 1105 view.setVisibility(View.GONE); 1106 } 1107 } 1108 1109 /** 1110 * Called when an int feature changes, for the window to update its 1111 * graphics. 1112 * 1113 * @param featureId The feature being changed. 1114 * @param value The new integer value. 1115 */ 1116 protected void onIntChanged(int featureId, int value) { 1117 if (featureId == FEATURE_PROGRESS || featureId == FEATURE_INDETERMINATE_PROGRESS) { 1118 updateProgressBars(value); 1119 } else if (featureId == FEATURE_CUSTOM_TITLE) { 1120 FrameLayout titleContainer = (FrameLayout) findViewById(com.android.internal.R.id.title_container); 1121 if (titleContainer != null) { 1122 mLayoutInflater.inflate(value, titleContainer); 1123 } 1124 } 1125 } 1126 1127 /** 1128 * Updates the progress bars that are shown in the title bar. 1129 * 1130 * @param value Can be one of {@link Window#PROGRESS_VISIBILITY_ON}, 1131 * {@link Window#PROGRESS_VISIBILITY_OFF}, 1132 * {@link Window#PROGRESS_INDETERMINATE_ON}, 1133 * {@link Window#PROGRESS_INDETERMINATE_OFF}, or a value 1134 * starting at {@link Window#PROGRESS_START} through 1135 * {@link Window#PROGRESS_END} for setting the default 1136 * progress (if {@link Window#PROGRESS_END} is given, 1137 * the progress bar widgets in the title will be hidden after an 1138 * animation), a value between 1139 * {@link Window#PROGRESS_SECONDARY_START} - 1140 * {@link Window#PROGRESS_SECONDARY_END} for the 1141 * secondary progress (if 1142 * {@link Window#PROGRESS_SECONDARY_END} is given, the 1143 * progress bar widgets will still be shown with the secondary 1144 * progress bar will be completely filled in.) 1145 */ 1146 private void updateProgressBars(int value) { 1147 ProgressBar circularProgressBar = getCircularProgressBar(true); 1148 ProgressBar horizontalProgressBar = getHorizontalProgressBar(true); 1149 1150 final int features = getLocalFeatures(); 1151 if (value == PROGRESS_VISIBILITY_ON) { 1152 if ((features & (1 << FEATURE_PROGRESS)) != 0) { 1153 int level = horizontalProgressBar.getProgress(); 1154 int visibility = (horizontalProgressBar.isIndeterminate() || level < 10000) ? 1155 View.VISIBLE : View.INVISIBLE; 1156 horizontalProgressBar.setVisibility(visibility); 1157 } 1158 if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) { 1159 circularProgressBar.setVisibility(View.VISIBLE); 1160 } 1161 } else if (value == PROGRESS_VISIBILITY_OFF) { 1162 if ((features & (1 << FEATURE_PROGRESS)) != 0) { 1163 horizontalProgressBar.setVisibility(View.GONE); 1164 } 1165 if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) { 1166 circularProgressBar.setVisibility(View.GONE); 1167 } 1168 } else if (value == PROGRESS_INDETERMINATE_ON) { 1169 horizontalProgressBar.setIndeterminate(true); 1170 } else if (value == PROGRESS_INDETERMINATE_OFF) { 1171 horizontalProgressBar.setIndeterminate(false); 1172 } else if (PROGRESS_START <= value && value <= PROGRESS_END) { 1173 // We want to set the progress value before testing for visibility 1174 // so that when the progress bar becomes visible again, it has the 1175 // correct level. 1176 horizontalProgressBar.setProgress(value - PROGRESS_START); 1177 1178 if (value < PROGRESS_END) { 1179 showProgressBars(horizontalProgressBar, circularProgressBar); 1180 } else { 1181 hideProgressBars(horizontalProgressBar, circularProgressBar); 1182 } 1183 } else if (PROGRESS_SECONDARY_START <= value && value <= PROGRESS_SECONDARY_END) { 1184 horizontalProgressBar.setSecondaryProgress(value - PROGRESS_SECONDARY_START); 1185 1186 showProgressBars(horizontalProgressBar, circularProgressBar); 1187 } 1188 1189 } 1190 1191 private void showProgressBars(ProgressBar horizontalProgressBar, ProgressBar spinnyProgressBar) { 1192 final int features = getLocalFeatures(); 1193 if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0 && 1194 spinnyProgressBar.getVisibility() == View.INVISIBLE) { 1195 spinnyProgressBar.setVisibility(View.VISIBLE); 1196 } 1197 // Only show the progress bars if the primary progress is not complete 1198 if ((features & (1 << FEATURE_PROGRESS)) != 0 && 1199 horizontalProgressBar.getProgress() < 10000) { 1200 horizontalProgressBar.setVisibility(View.VISIBLE); 1201 } 1202 } 1203 1204 private void hideProgressBars(ProgressBar horizontalProgressBar, ProgressBar spinnyProgressBar) { 1205 final int features = getLocalFeatures(); 1206 Animation anim = AnimationUtils.loadAnimation(getContext(), com.android.internal.R.anim.fade_out); 1207 anim.setDuration(1000); 1208 if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0 && 1209 spinnyProgressBar.getVisibility() == View.VISIBLE) { 1210 spinnyProgressBar.startAnimation(anim); 1211 spinnyProgressBar.setVisibility(View.INVISIBLE); 1212 } 1213 if ((features & (1 << FEATURE_PROGRESS)) != 0 && 1214 horizontalProgressBar.getVisibility() == View.VISIBLE) { 1215 horizontalProgressBar.startAnimation(anim); 1216 horizontalProgressBar.setVisibility(View.INVISIBLE); 1217 } 1218 } 1219 1220 /** 1221 * Request that key events come to this activity. Use this if your activity 1222 * has no views with focus, but the activity still wants a chance to process 1223 * key events. 1224 */ 1225 @Override 1226 public void takeKeyEvents(boolean get) { 1227 mDecor.setFocusable(get); 1228 } 1229 1230 @Override 1231 public boolean superDispatchKeyEvent(KeyEvent event) { 1232 return mDecor.superDispatchKeyEvent(event); 1233 } 1234 1235 @Override 1236 public boolean superDispatchKeyShortcutEvent(KeyEvent event) { 1237 return mDecor.superDispatchKeyShortcutEvent(event); 1238 } 1239 1240 @Override 1241 public boolean superDispatchTouchEvent(MotionEvent event) { 1242 return mDecor.superDispatchTouchEvent(event); 1243 } 1244 1245 @Override 1246 public boolean superDispatchTrackballEvent(MotionEvent event) { 1247 return mDecor.superDispatchTrackballEvent(event); 1248 } 1249 1250 @Override 1251 public boolean superDispatchGenericMotionEvent(MotionEvent event) { 1252 return mDecor.superDispatchGenericMotionEvent(event); 1253 } 1254 1255 /** 1256 * A key was pressed down and not handled by anything else in the window. 1257 * 1258 * @see #onKeyUp 1259 * @see android.view.KeyEvent 1260 */ 1261 protected boolean onKeyDown(int featureId, int keyCode, KeyEvent event) { 1262 /* **************************************************************************** 1263 * HOW TO DECIDE WHERE YOUR KEY HANDLING GOES. 1264 * 1265 * If your key handling must happen before the app gets a crack at the event, 1266 * it goes in PhoneWindowManager. 1267 * 1268 * If your key handling should happen in all windows, and does not depend on 1269 * the state of the current application, other than that the current 1270 * application can override the behavior by handling the event itself, it 1271 * should go in PhoneFallbackEventHandler. 1272 * 1273 * Only if your handling depends on the window, and the fact that it has 1274 * a DecorView, should it go here. 1275 * ****************************************************************************/ 1276 1277 final KeyEvent.DispatcherState dispatcher = 1278 mDecor != null ? mDecor.getKeyDispatcherState() : null; 1279 //Log.i(TAG, "Key down: repeat=" + event.getRepeatCount() 1280 // + " flags=0x" + Integer.toHexString(event.getFlags())); 1281 1282 switch (keyCode) { 1283 case KeyEvent.KEYCODE_VOLUME_UP: 1284 case KeyEvent.KEYCODE_VOLUME_DOWN: 1285 case KeyEvent.KEYCODE_VOLUME_MUTE: { 1286 // Similar code is in PhoneFallbackEventHandler in case the window 1287 // doesn't have one of these. In this case, we execute it here and 1288 // eat the event instead, because we have mVolumeControlStreamType 1289 // and they don't. 1290 getAudioManager().handleKeyDown(keyCode, mVolumeControlStreamType); 1291 return true; 1292 } 1293 1294 case KeyEvent.KEYCODE_MENU: { 1295 onKeyDownPanel((featureId < 0) ? FEATURE_OPTIONS_PANEL : featureId, event); 1296 return true; 1297 } 1298 1299 case KeyEvent.KEYCODE_BACK: { 1300 if (event.getRepeatCount() > 0) break; 1301 if (featureId < 0) break; 1302 // Currently don't do anything with long press. 1303 dispatcher.startTracking(event, this); 1304 return true; 1305 } 1306 1307 } 1308 1309 return false; 1310 } 1311 1312 private KeyguardManager getKeyguardManager() { 1313 if (mKeyguardManager == null) { 1314 mKeyguardManager = (KeyguardManager) getContext().getSystemService( 1315 Context.KEYGUARD_SERVICE); 1316 } 1317 return mKeyguardManager; 1318 } 1319 1320 AudioManager getAudioManager() { 1321 if (mAudioManager == null) { 1322 mAudioManager = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE); 1323 } 1324 return mAudioManager; 1325 } 1326 1327 /** 1328 * A key was released and not handled by anything else in the window. 1329 * 1330 * @see #onKeyDown 1331 * @see android.view.KeyEvent 1332 */ 1333 protected boolean onKeyUp(int featureId, int keyCode, KeyEvent event) { 1334 final KeyEvent.DispatcherState dispatcher = 1335 mDecor != null ? mDecor.getKeyDispatcherState() : null; 1336 if (dispatcher != null) { 1337 dispatcher.handleUpEvent(event); 1338 } 1339 //Log.i(TAG, "Key up: repeat=" + event.getRepeatCount() 1340 // + " flags=0x" + Integer.toHexString(event.getFlags())); 1341 1342 switch (keyCode) { 1343 case KeyEvent.KEYCODE_VOLUME_UP: 1344 case KeyEvent.KEYCODE_VOLUME_DOWN: 1345 case KeyEvent.KEYCODE_VOLUME_MUTE: { 1346 // Similar code is in PhoneFallbackEventHandler in case the window 1347 // doesn't have one of these. In this case, we execute it here and 1348 // eat the event instead, because we have mVolumeControlStreamType 1349 // and they don't. 1350 getAudioManager().handleKeyUp(keyCode, mVolumeControlStreamType); 1351 return true; 1352 } 1353 1354 case KeyEvent.KEYCODE_MENU: { 1355 onKeyUpPanel(featureId < 0 ? FEATURE_OPTIONS_PANEL : featureId, 1356 event); 1357 return true; 1358 } 1359 1360 case KeyEvent.KEYCODE_BACK: { 1361 if (featureId < 0) break; 1362 if (event.isTracking() && !event.isCanceled()) { 1363 if (featureId == FEATURE_OPTIONS_PANEL) { 1364 PanelFeatureState st = getPanelState(featureId, false); 1365 if (st != null && st.isInExpandedMode) { 1366 // If the user is in an expanded menu and hits back, it 1367 // should go back to the icon menu 1368 reopenMenu(true); 1369 return true; 1370 } 1371 } 1372 closePanel(featureId); 1373 return true; 1374 } 1375 break; 1376 } 1377 1378 case KeyEvent.KEYCODE_SEARCH: { 1379 /* 1380 * Do this in onKeyUp since the Search key is also used for 1381 * chording quick launch shortcuts. 1382 */ 1383 if (getKeyguardManager().inKeyguardRestrictedInputMode()) { 1384 break; 1385 } 1386 if (event.isTracking() && !event.isCanceled()) { 1387 launchDefaultSearch(); 1388 } 1389 return true; 1390 } 1391 } 1392 1393 return false; 1394 } 1395 1396 @Override 1397 protected void onActive() { 1398 } 1399 1400 @Override 1401 public final View getDecorView() { 1402 if (mDecor == null) { 1403 installDecor(); 1404 } 1405 return mDecor; 1406 } 1407 1408 @Override 1409 public final View peekDecorView() { 1410 return mDecor; 1411 } 1412 1413 static private final String FOCUSED_ID_TAG = "android:focusedViewId"; 1414 static private final String VIEWS_TAG = "android:views"; 1415 static private final String PANELS_TAG = "android:Panels"; 1416 static private final String ACTION_BAR_TAG = "android:ActionBar"; 1417 1418 /** {@inheritDoc} */ 1419 @Override 1420 public Bundle saveHierarchyState() { 1421 Bundle outState = new Bundle(); 1422 if (mContentParent == null) { 1423 return outState; 1424 } 1425 1426 SparseArray<Parcelable> states = new SparseArray<Parcelable>(); 1427 mContentParent.saveHierarchyState(states); 1428 outState.putSparseParcelableArray(VIEWS_TAG, states); 1429 1430 // save the focused view id 1431 View focusedView = mContentParent.findFocus(); 1432 if (focusedView != null) { 1433 if (focusedView.getId() != View.NO_ID) { 1434 outState.putInt(FOCUSED_ID_TAG, focusedView.getId()); 1435 } else { 1436 if (false) { 1437 Log.d(TAG, "couldn't save which view has focus because the focused view " 1438 + focusedView + " has no id."); 1439 } 1440 } 1441 } 1442 1443 // save the panels 1444 SparseArray<Parcelable> panelStates = new SparseArray<Parcelable>(); 1445 savePanelState(panelStates); 1446 if (panelStates.size() > 0) { 1447 outState.putSparseParcelableArray(PANELS_TAG, panelStates); 1448 } 1449 1450 if (mActionBar != null) { 1451 outState.putBoolean(ACTION_BAR_TAG, mActionBar.isOverflowMenuShowing()); 1452 } 1453 1454 return outState; 1455 } 1456 1457 /** {@inheritDoc} */ 1458 @Override 1459 public void restoreHierarchyState(Bundle savedInstanceState) { 1460 if (mContentParent == null) { 1461 return; 1462 } 1463 1464 SparseArray<Parcelable> savedStates 1465 = savedInstanceState.getSparseParcelableArray(VIEWS_TAG); 1466 if (savedStates != null) { 1467 mContentParent.restoreHierarchyState(savedStates); 1468 } 1469 1470 // restore the focused view 1471 int focusedViewId = savedInstanceState.getInt(FOCUSED_ID_TAG, View.NO_ID); 1472 if (focusedViewId != View.NO_ID) { 1473 View needsFocus = mContentParent.findViewById(focusedViewId); 1474 if (needsFocus != null) { 1475 needsFocus.requestFocus(); 1476 } else { 1477 Log.w(TAG, 1478 "Previously focused view reported id " + focusedViewId 1479 + " during save, but can't be found during restore."); 1480 } 1481 } 1482 1483 // restore the panels 1484 SparseArray<Parcelable> panelStates = savedInstanceState.getSparseParcelableArray(PANELS_TAG); 1485 if (panelStates != null) { 1486 restorePanelState(panelStates); 1487 } 1488 1489 if (mActionBar != null && savedInstanceState.getBoolean(ACTION_BAR_TAG)) { 1490 mActionBar.postShowOverflowMenu(); 1491 } 1492 } 1493 1494 /** 1495 * Invoked when the panels should freeze their state. 1496 * 1497 * @param icicles Save state into this. This is usually indexed by the 1498 * featureId. This will be given to {@link #restorePanelState} in the 1499 * future. 1500 */ 1501 private void savePanelState(SparseArray<Parcelable> icicles) { 1502 PanelFeatureState[] panels = mPanels; 1503 if (panels == null) { 1504 return; 1505 } 1506 1507 for (int curFeatureId = panels.length - 1; curFeatureId >= 0; curFeatureId--) { 1508 if (panels[curFeatureId] != null) { 1509 icicles.put(curFeatureId, panels[curFeatureId].onSaveInstanceState()); 1510 } 1511 } 1512 } 1513 1514 /** 1515 * Invoked when the panels should thaw their state from a previously frozen state. 1516 * 1517 * @param icicles The state saved by {@link #savePanelState} that needs to be thawed. 1518 */ 1519 private void restorePanelState(SparseArray<Parcelable> icicles) { 1520 PanelFeatureState st; 1521 for (int curFeatureId = icicles.size() - 1; curFeatureId >= 0; curFeatureId--) { 1522 st = getPanelState(curFeatureId, false /* required */); 1523 if (st == null) { 1524 // The panel must not have been required, and is currently not around, skip it 1525 continue; 1526 } 1527 1528 st.onRestoreInstanceState(icicles.get(curFeatureId)); 1529 } 1530 1531 /* 1532 * Implementation note: call openPanelsAfterRestore later to actually open the 1533 * restored panels. 1534 */ 1535 } 1536 1537 /** 1538 * Opens the panels that have had their state restored. This should be 1539 * called sometime after {@link #restorePanelState} when it is safe to add 1540 * to the window manager. 1541 */ 1542 private void openPanelsAfterRestore() { 1543 PanelFeatureState[] panels = mPanels; 1544 1545 if (panels == null) { 1546 return; 1547 } 1548 1549 PanelFeatureState st; 1550 for (int i = panels.length - 1; i >= 0; i--) { 1551 st = panels[i]; 1552 // We restore the panel if it was last open; we skip it if it 1553 // now is open, to avoid a race condition if the user immediately 1554 // opens it when we are resuming. 1555 if ((st != null) && !st.isOpen && st.wasLastOpen) { 1556 st.isInExpandedMode = st.wasLastExpanded; 1557 openPanel(st, null); 1558 } 1559 } 1560 } 1561 1562 private class PanelMenuPresenterCallback implements MenuPresenter.Callback { 1563 @Override 1564 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { 1565 final Menu parentMenu = menu.getRootMenu(); 1566 final boolean isSubMenu = parentMenu != menu; 1567 final PanelFeatureState panel = findMenuPanel(isSubMenu ? parentMenu : menu); 1568 if (panel != null) { 1569 if (isSubMenu) { 1570 callOnPanelClosed(panel.featureId, panel, parentMenu); 1571 closePanel(panel, true); 1572 } else { 1573 // Close the panel and only do the callback if the menu is being 1574 // closed completely, not if opening a sub menu 1575 closePanel(panel, allMenusAreClosing); 1576 } 1577 } 1578 } 1579 1580 @Override 1581 public boolean onOpenSubMenu(MenuBuilder subMenu) { 1582 if (subMenu == null && hasFeature(FEATURE_ACTION_BAR)) { 1583 Callback cb = getCallback(); 1584 if (cb != null && !isDestroyed()) { 1585 cb.onMenuOpened(FEATURE_ACTION_BAR, subMenu); 1586 } 1587 } 1588 1589 return true; 1590 } 1591 } 1592 1593 private final class ActionMenuPresenterCallback implements MenuPresenter.Callback { 1594 @Override 1595 public boolean onOpenSubMenu(MenuBuilder subMenu) { 1596 Callback cb = getCallback(); 1597 if (cb != null) { 1598 cb.onMenuOpened(FEATURE_ACTION_BAR, subMenu); 1599 return true; 1600 } 1601 return false; 1602 } 1603 1604 @Override 1605 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { 1606 checkCloseActionMenu(menu); 1607 } 1608 } 1609 1610 private final class DecorView extends FrameLayout implements RootViewSurfaceTaker { 1611 /* package */int mDefaultOpacity = PixelFormat.OPAQUE; 1612 1613 /** The feature ID of the panel, or -1 if this is the application's DecorView */ 1614 private final int mFeatureId; 1615 1616 private final Rect mDrawingBounds = new Rect(); 1617 1618 private final Rect mBackgroundPadding = new Rect(); 1619 1620 private final Rect mFramePadding = new Rect(); 1621 1622 private final Rect mFrameOffsets = new Rect(); 1623 1624 private boolean mChanging; 1625 1626 private Drawable mMenuBackground; 1627 private boolean mWatchingForMenu; 1628 private int mDownY; 1629 1630 private ActionMode mActionMode; 1631 private ActionBarContextView mActionModeView; 1632 private PopupWindow mActionModePopup; 1633 private Runnable mShowActionModePopup; 1634 1635 public DecorView(Context context, int featureId) { 1636 super(context); 1637 mFeatureId = featureId; 1638 } 1639 1640 @Override 1641 public boolean dispatchKeyEvent(KeyEvent event) { 1642 final int keyCode = event.getKeyCode(); 1643 final int action = event.getAction(); 1644 final boolean isDown = action == KeyEvent.ACTION_DOWN; 1645 1646 if (isDown && (event.getRepeatCount() == 0)) { 1647 // First handle chording of panel key: if a panel key is held 1648 // but not released, try to execute a shortcut in it. 1649 if ((mPanelChordingKey > 0) && (mPanelChordingKey != keyCode)) { 1650 boolean handled = dispatchKeyShortcutEvent(event); 1651 if (handled) { 1652 return true; 1653 } 1654 } 1655 1656 // If a panel is open, perform a shortcut on it without the 1657 // chorded panel key 1658 if ((mPreparedPanel != null) && mPreparedPanel.isOpen) { 1659 if (performPanelShortcut(mPreparedPanel, keyCode, event, 0)) { 1660 return true; 1661 } 1662 } 1663 } 1664 1665 // Back cancels action modes first. 1666 if (mActionMode != null && keyCode == KeyEvent.KEYCODE_BACK) { 1667 if (action == KeyEvent.ACTION_UP) { 1668 mActionMode.finish(); 1669 } 1670 return true; 1671 } 1672 1673 if (!isDestroyed()) { 1674 final Callback cb = getCallback(); 1675 final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event) 1676 : super.dispatchKeyEvent(event); 1677 if (handled) { 1678 return true; 1679 } 1680 } 1681 return isDown ? PhoneWindow.this.onKeyDown(mFeatureId, event.getKeyCode(), event) 1682 : PhoneWindow.this.onKeyUp(mFeatureId, event.getKeyCode(), event); 1683 } 1684 1685 @Override 1686 public boolean dispatchKeyShortcutEvent(KeyEvent ev) { 1687 // Perform the shortcut (mPreparedPanel can be null since 1688 // global shortcuts (such as search) don't rely on a 1689 // prepared panel or menu). 1690 boolean handled = performPanelShortcut(mPreparedPanel, ev.getKeyCode(), ev, 1691 Menu.FLAG_PERFORM_NO_CLOSE); 1692 if (handled) { 1693 if (mPreparedPanel != null) { 1694 mPreparedPanel.isHandled = true; 1695 } 1696 return true; 1697 } 1698 1699 // Shortcut not handled by the panel. Dispatch to the view hierarchy. 1700 final Callback cb = getCallback(); 1701 return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchKeyShortcutEvent(ev) 1702 : super.dispatchKeyShortcutEvent(ev); 1703 } 1704 1705 @Override 1706 public boolean dispatchTouchEvent(MotionEvent ev) { 1707 final Callback cb = getCallback(); 1708 return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev) 1709 : super.dispatchTouchEvent(ev); 1710 } 1711 1712 @Override 1713 public boolean dispatchTrackballEvent(MotionEvent ev) { 1714 final Callback cb = getCallback(); 1715 return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTrackballEvent(ev) 1716 : super.dispatchTrackballEvent(ev); 1717 } 1718 1719 @Override 1720 public boolean dispatchGenericMotionEvent(MotionEvent ev) { 1721 final Callback cb = getCallback(); 1722 return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchGenericMotionEvent(ev) 1723 : super.dispatchGenericMotionEvent(ev); 1724 } 1725 1726 public boolean superDispatchKeyEvent(KeyEvent event) { 1727 return super.dispatchKeyEvent(event); 1728 } 1729 1730 public boolean superDispatchKeyShortcutEvent(KeyEvent event) { 1731 return super.dispatchKeyShortcutEvent(event); 1732 } 1733 1734 public boolean superDispatchTouchEvent(MotionEvent event) { 1735 return super.dispatchTouchEvent(event); 1736 } 1737 1738 public boolean superDispatchTrackballEvent(MotionEvent event) { 1739 return super.dispatchTrackballEvent(event); 1740 } 1741 1742 public boolean superDispatchGenericMotionEvent(MotionEvent event) { 1743 return super.dispatchGenericMotionEvent(event); 1744 } 1745 1746 @Override 1747 public boolean onTouchEvent(MotionEvent event) { 1748 return onInterceptTouchEvent(event); 1749 } 1750 1751 private boolean isOutOfBounds(int x, int y) { 1752 return x < -5 || y < -5 || x > (getWidth() + 5) 1753 || y > (getHeight() + 5); 1754 } 1755 1756 @Override 1757 public boolean onInterceptTouchEvent(MotionEvent event) { 1758 int action = event.getAction(); 1759 if (mFeatureId >= 0) { 1760 if (action == MotionEvent.ACTION_DOWN) { 1761 int x = (int)event.getX(); 1762 int y = (int)event.getY(); 1763 if (isOutOfBounds(x, y)) { 1764 closePanel(mFeatureId); 1765 return true; 1766 } 1767 } 1768 } 1769 1770 if (!SWEEP_OPEN_MENU) { 1771 return false; 1772 } 1773 1774 if (mFeatureId >= 0) { 1775 if (action == MotionEvent.ACTION_DOWN) { 1776 Log.i(TAG, "Watchiing!"); 1777 mWatchingForMenu = true; 1778 mDownY = (int) event.getY(); 1779 return false; 1780 } 1781 1782 if (!mWatchingForMenu) { 1783 return false; 1784 } 1785 1786 int y = (int)event.getY(); 1787 if (action == MotionEvent.ACTION_MOVE) { 1788 if (y > (mDownY+30)) { 1789 Log.i(TAG, "Closing!"); 1790 closePanel(mFeatureId); 1791 mWatchingForMenu = false; 1792 return true; 1793 } 1794 } else if (action == MotionEvent.ACTION_UP) { 1795 mWatchingForMenu = false; 1796 } 1797 1798 return false; 1799 } 1800 1801 //Log.i(TAG, "Intercept: action=" + action + " y=" + event.getY() 1802 // + " (in " + getHeight() + ")"); 1803 1804 if (action == MotionEvent.ACTION_DOWN) { 1805 int y = (int)event.getY(); 1806 if (y >= (getHeight()-5) && !hasChildren()) { 1807 Log.i(TAG, "Watchiing!"); 1808 mWatchingForMenu = true; 1809 } 1810 return false; 1811 } 1812 1813 if (!mWatchingForMenu) { 1814 return false; 1815 } 1816 1817 int y = (int)event.getY(); 1818 if (action == MotionEvent.ACTION_MOVE) { 1819 if (y < (getHeight()-30)) { 1820 Log.i(TAG, "Opening!"); 1821 openPanel(FEATURE_OPTIONS_PANEL, new KeyEvent( 1822 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU)); 1823 mWatchingForMenu = false; 1824 return true; 1825 } 1826 } else if (action == MotionEvent.ACTION_UP) { 1827 mWatchingForMenu = false; 1828 } 1829 1830 return false; 1831 } 1832 1833 @Override 1834 public void sendAccessibilityEvent(int eventType) { 1835 if (!AccessibilityManager.getInstance(mContext).isEnabled()) { 1836 return; 1837 } 1838 1839 // if we are showing a feature that should be announced and one child 1840 // make this child the event source since this is the feature itself 1841 // otherwise the callback will take over and announce its client 1842 if ((mFeatureId == FEATURE_OPTIONS_PANEL || 1843 mFeatureId == FEATURE_CONTEXT_MENU || 1844 mFeatureId == FEATURE_PROGRESS || 1845 mFeatureId == FEATURE_INDETERMINATE_PROGRESS) 1846 && getChildCount() == 1) { 1847 getChildAt(0).sendAccessibilityEvent(eventType); 1848 } else { 1849 super.sendAccessibilityEvent(eventType); 1850 } 1851 } 1852 1853 @Override 1854 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 1855 final Callback cb = getCallback(); 1856 if (cb != null && !isDestroyed()) { 1857 if (cb.dispatchPopulateAccessibilityEvent(event)) { 1858 return true; 1859 } 1860 } 1861 return super.dispatchPopulateAccessibilityEvent(event); 1862 } 1863 1864 @Override 1865 protected boolean setFrame(int l, int t, int r, int b) { 1866 boolean changed = super.setFrame(l, t, r, b); 1867 if (changed) { 1868 final Rect drawingBounds = mDrawingBounds; 1869 getDrawingRect(drawingBounds); 1870 1871 Drawable fg = getForeground(); 1872 if (fg != null) { 1873 final Rect frameOffsets = mFrameOffsets; 1874 drawingBounds.left += frameOffsets.left; 1875 drawingBounds.top += frameOffsets.top; 1876 drawingBounds.right -= frameOffsets.right; 1877 drawingBounds.bottom -= frameOffsets.bottom; 1878 fg.setBounds(drawingBounds); 1879 final Rect framePadding = mFramePadding; 1880 drawingBounds.left += framePadding.left - frameOffsets.left; 1881 drawingBounds.top += framePadding.top - frameOffsets.top; 1882 drawingBounds.right -= framePadding.right - frameOffsets.right; 1883 drawingBounds.bottom -= framePadding.bottom - frameOffsets.bottom; 1884 } 1885 1886 Drawable bg = getBackground(); 1887 if (bg != null) { 1888 bg.setBounds(drawingBounds); 1889 } 1890 1891 if (SWEEP_OPEN_MENU) { 1892 if (mMenuBackground == null && mFeatureId < 0 1893 && getAttributes().height 1894 == WindowManager.LayoutParams.MATCH_PARENT) { 1895 mMenuBackground = getContext().getResources().getDrawable( 1896 com.android.internal.R.drawable.menu_background); 1897 } 1898 if (mMenuBackground != null) { 1899 mMenuBackground.setBounds(drawingBounds.left, 1900 drawingBounds.bottom-6, drawingBounds.right, 1901 drawingBounds.bottom+20); 1902 } 1903 } 1904 } 1905 return changed; 1906 } 1907 1908 @Override 1909 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 1910 final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); 1911 final boolean isPortrait = metrics.widthPixels < metrics.heightPixels; 1912 1913 final int widthMode = getMode(widthMeasureSpec); 1914 1915 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 1916 1917 int width = getMeasuredWidth(); 1918 boolean measure = false; 1919 1920 widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY); 1921 1922 final TypedValue tv = isPortrait ? mMinWidthMinor : mMinWidthMajor; 1923 1924 if (widthMode == AT_MOST && tv.type != TypedValue.TYPE_NULL) { 1925 final int min; 1926 if (tv.type == TypedValue.TYPE_DIMENSION) { 1927 min = (int)tv.getDimension(metrics); 1928 } else if (tv.type == TypedValue.TYPE_FRACTION) { 1929 min = (int)tv.getFraction(metrics.widthPixels, metrics.widthPixels); 1930 } else { 1931 min = 0; 1932 } 1933 1934 if (width < min) { 1935 widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY); 1936 measure = true; 1937 } 1938 } 1939 1940 // TODO: Support height? 1941 1942 if (measure) { 1943 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 1944 } 1945 } 1946 1947 @Override 1948 public void draw(Canvas canvas) { 1949 super.draw(canvas); 1950 1951 if (mMenuBackground != null) { 1952 mMenuBackground.draw(canvas); 1953 } 1954 } 1955 1956 1957 @Override 1958 public boolean showContextMenuForChild(View originalView) { 1959 // Reuse the context menu builder 1960 if (mContextMenu == null) { 1961 mContextMenu = new ContextMenuBuilder(getContext()); 1962 mContextMenu.setCallback(mContextMenuCallback); 1963 } else { 1964 mContextMenu.clearAll(); 1965 } 1966 1967 mContextMenuHelper = mContextMenu.show(originalView, originalView.getWindowToken()); 1968 return mContextMenuHelper != null; 1969 } 1970 1971 @Override 1972 public ActionMode startActionModeForChild(View originalView, 1973 ActionMode.Callback callback) { 1974 // originalView can be used here to be sure that we don't obscure 1975 // relevant content with the context mode UI. 1976 return startActionMode(callback); 1977 } 1978 1979 @Override 1980 public ActionMode startActionMode(ActionMode.Callback callback) { 1981 if (mActionMode != null) { 1982 mActionMode.finish(); 1983 } 1984 1985 final ActionMode.Callback wrappedCallback = new ActionModeCallbackWrapper(callback); 1986 ActionMode mode = null; 1987 if (getCallback() != null && !isDestroyed()) { 1988 try { 1989 mode = getCallback().onWindowStartingActionMode(wrappedCallback); 1990 } catch (AbstractMethodError ame) { 1991 // Older apps might not implement this callback method. 1992 } 1993 } 1994 if (mode != null) { 1995 mActionMode = mode; 1996 } else { 1997 if (mActionModeView == null) { 1998 if (hasFeature(FEATURE_ACTION_MODE_OVERLAY)) { 1999 mActionModeView = new ActionBarContextView(mContext); 2000 mActionModePopup = new PopupWindow(mContext, null, 2001 com.android.internal.R.attr.actionModePopupWindowStyle); 2002 mActionModePopup.setLayoutInScreenEnabled(true); 2003 mActionModePopup.setClippingEnabled(false); 2004 mActionModePopup.setContentView(mActionModeView); 2005 mActionModePopup.setWidth(MATCH_PARENT); 2006 2007 TypedValue heightValue = new TypedValue(); 2008 mContext.getTheme().resolveAttribute( 2009 com.android.internal.R.attr.actionBarSize, heightValue, false); 2010 final int height = TypedValue.complexToDimensionPixelSize(heightValue.data, 2011 mContext.getResources().getDisplayMetrics()); 2012 mActionModePopup.setHeight(height); 2013 mShowActionModePopup = new Runnable() { 2014 public void run() { 2015 mActionModePopup.showAtLocation(PhoneWindow.DecorView.this, 2016 Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0); 2017 } 2018 }; 2019 } else { 2020 ViewStub stub = (ViewStub) findViewById( 2021 com.android.internal.R.id.action_mode_bar_stub); 2022 if (stub != null) { 2023 mActionModeView = (ActionBarContextView) stub.inflate(); 2024 } 2025 } 2026 } 2027 2028 if (mActionModeView != null) { 2029 mActionModeView.killMode(); 2030 mode = new StandaloneActionMode(getContext(), mActionModeView, wrappedCallback); 2031 if (callback.onCreateActionMode(mode, mode.getMenu())) { 2032 mode.invalidate(); 2033 mActionModeView.initForMode(mode); 2034 mActionModeView.setVisibility(View.VISIBLE); 2035 mActionMode = mode; 2036 if (mActionModePopup != null) { 2037 post(mShowActionModePopup); 2038 } 2039 } else { 2040 mActionMode = null; 2041 } 2042 } 2043 } 2044 if (mActionMode != null && getCallback() != null && !isDestroyed()) { 2045 try { 2046 getCallback().onActionModeStarted(mActionMode); 2047 } catch (AbstractMethodError ame) { 2048 // Older apps might not implement this callback method. 2049 } 2050 } 2051 return mActionMode; 2052 } 2053 2054 public void startChanging() { 2055 mChanging = true; 2056 } 2057 2058 public void finishChanging() { 2059 mChanging = false; 2060 drawableChanged(); 2061 } 2062 2063 public void setWindowBackground(Drawable drawable) { 2064 if (getBackground() != drawable) { 2065 setBackgroundDrawable(drawable); 2066 if (drawable != null) { 2067 drawable.getPadding(mBackgroundPadding); 2068 } else { 2069 mBackgroundPadding.setEmpty(); 2070 } 2071 drawableChanged(); 2072 } 2073 } 2074 2075 @Override 2076 public void setBackgroundDrawable(Drawable d) { 2077 super.setBackgroundDrawable(d); 2078 if (getWindowToken() != null) { 2079 updateWindowResizeState(); 2080 } 2081 } 2082 2083 public void setWindowFrame(Drawable drawable) { 2084 if (getForeground() != drawable) { 2085 setForeground(drawable); 2086 if (drawable != null) { 2087 drawable.getPadding(mFramePadding); 2088 } else { 2089 mFramePadding.setEmpty(); 2090 } 2091 drawableChanged(); 2092 } 2093 } 2094 2095 @Override 2096 protected boolean fitSystemWindows(Rect insets) { 2097 mFrameOffsets.set(insets); 2098 if (getForeground() != null) { 2099 drawableChanged(); 2100 } 2101 return super.fitSystemWindows(insets); 2102 } 2103 2104 private void drawableChanged() { 2105 if (mChanging) { 2106 return; 2107 } 2108 2109 setPadding(mFramePadding.left + mBackgroundPadding.left, mFramePadding.top 2110 + mBackgroundPadding.top, mFramePadding.right + mBackgroundPadding.right, 2111 mFramePadding.bottom + mBackgroundPadding.bottom); 2112 requestLayout(); 2113 invalidate(); 2114 2115 int opacity = PixelFormat.OPAQUE; 2116 2117 // Note: if there is no background, we will assume opaque. The 2118 // common case seems to be that an application sets there to be 2119 // no background so it can draw everything itself. For that, 2120 // we would like to assume OPAQUE and let the app force it to 2121 // the slower TRANSLUCENT mode if that is really what it wants. 2122 Drawable bg = getBackground(); 2123 Drawable fg = getForeground(); 2124 if (bg != null) { 2125 if (fg == null) { 2126 opacity = bg.getOpacity(); 2127 } else if (mFramePadding.left <= 0 && mFramePadding.top <= 0 2128 && mFramePadding.right <= 0 && mFramePadding.bottom <= 0) { 2129 // If the frame padding is zero, then we can be opaque 2130 // if either the frame -or- the background is opaque. 2131 int fop = fg.getOpacity(); 2132 int bop = bg.getOpacity(); 2133 if (false) 2134 Log.v(TAG, "Background opacity: " + bop + ", Frame opacity: " + fop); 2135 if (fop == PixelFormat.OPAQUE || bop == PixelFormat.OPAQUE) { 2136 opacity = PixelFormat.OPAQUE; 2137 } else if (fop == PixelFormat.UNKNOWN) { 2138 opacity = bop; 2139 } else if (bop == PixelFormat.UNKNOWN) { 2140 opacity = fop; 2141 } else { 2142 opacity = Drawable.resolveOpacity(fop, bop); 2143 } 2144 } else { 2145 // For now we have to assume translucent if there is a 2146 // frame with padding... there is no way to tell if the 2147 // frame and background together will draw all pixels. 2148 if (false) 2149 Log.v(TAG, "Padding: " + mFramePadding); 2150 opacity = PixelFormat.TRANSLUCENT; 2151 } 2152 } 2153 2154 if (false) 2155 Log.v(TAG, "Background: " + bg + ", Frame: " + fg); 2156 if (false) 2157 Log.v(TAG, "Selected default opacity: " + opacity); 2158 2159 mDefaultOpacity = opacity; 2160 if (mFeatureId < 0) { 2161 setDefaultWindowFormat(opacity); 2162 } 2163 } 2164 2165 @Override 2166 public void onWindowFocusChanged(boolean hasWindowFocus) { 2167 super.onWindowFocusChanged(hasWindowFocus); 2168 2169 // If the user is chording a menu shortcut, release the chord since 2170 // this window lost focus 2171 if (!hasWindowFocus && mPanelChordingKey != 0) { 2172 closePanel(FEATURE_OPTIONS_PANEL); 2173 } 2174 2175 final Callback cb = getCallback(); 2176 if (cb != null && !isDestroyed() && mFeatureId < 0) { 2177 cb.onWindowFocusChanged(hasWindowFocus); 2178 } 2179 } 2180 2181 void updateWindowResizeState() { 2182 Drawable bg = getBackground(); 2183 hackTurnOffWindowResizeAnim(bg == null || bg.getOpacity() 2184 != PixelFormat.OPAQUE); 2185 } 2186 2187 @Override 2188 protected void onAttachedToWindow() { 2189 super.onAttachedToWindow(); 2190 2191 updateWindowResizeState(); 2192 2193 final Callback cb = getCallback(); 2194 if (cb != null && !isDestroyed() && mFeatureId < 0) { 2195 cb.onAttachedToWindow(); 2196 } 2197 2198 if (mFeatureId == -1) { 2199 /* 2200 * The main window has been attached, try to restore any panels 2201 * that may have been open before. This is called in cases where 2202 * an activity is being killed for configuration change and the 2203 * menu was open. When the activity is recreated, the menu 2204 * should be shown again. 2205 */ 2206 openPanelsAfterRestore(); 2207 } 2208 } 2209 2210 @Override 2211 protected void onDetachedFromWindow() { 2212 super.onDetachedFromWindow(); 2213 2214 final Callback cb = getCallback(); 2215 if (cb != null && mFeatureId < 0) { 2216 cb.onDetachedFromWindow(); 2217 } 2218 2219 if (mActionBar != null) { 2220 mActionBar.dismissPopupMenus(); 2221 } 2222 2223 if (mActionModePopup != null) { 2224 removeCallbacks(mShowActionModePopup); 2225 if (mActionModePopup.isShowing()) { 2226 mActionModePopup.dismiss(); 2227 } 2228 mActionModePopup = null; 2229 } 2230 } 2231 2232 @Override 2233 public void onCloseSystemDialogs(String reason) { 2234 if (mFeatureId >= 0) { 2235 closeAllPanels(); 2236 } 2237 } 2238 2239 public android.view.SurfaceHolder.Callback2 willYouTakeTheSurface() { 2240 return mFeatureId < 0 ? mTakeSurfaceCallback : null; 2241 } 2242 2243 public InputQueue.Callback willYouTakeTheInputQueue() { 2244 return mFeatureId < 0 ? mTakeInputQueueCallback : null; 2245 } 2246 2247 public void setSurfaceType(int type) { 2248 PhoneWindow.this.setType(type); 2249 } 2250 2251 public void setSurfaceFormat(int format) { 2252 PhoneWindow.this.setFormat(format); 2253 } 2254 2255 public void setSurfaceKeepScreenOn(boolean keepOn) { 2256 if (keepOn) PhoneWindow.this.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 2257 else PhoneWindow.this.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 2258 } 2259 2260 /** 2261 * Clears out internal reference when the action mode is destroyed. 2262 */ 2263 private class ActionModeCallbackWrapper implements ActionMode.Callback { 2264 private ActionMode.Callback mWrapped; 2265 2266 public ActionModeCallbackWrapper(ActionMode.Callback wrapped) { 2267 mWrapped = wrapped; 2268 } 2269 2270 public boolean onCreateActionMode(ActionMode mode, Menu menu) { 2271 return mWrapped.onCreateActionMode(mode, menu); 2272 } 2273 2274 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 2275 return mWrapped.onPrepareActionMode(mode, menu); 2276 } 2277 2278 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 2279 return mWrapped.onActionItemClicked(mode, item); 2280 } 2281 2282 public void onDestroyActionMode(ActionMode mode) { 2283 mWrapped.onDestroyActionMode(mode); 2284 if (mActionModePopup != null) { 2285 removeCallbacks(mShowActionModePopup); 2286 mActionModePopup.dismiss(); 2287 } else if (mActionModeView != null) { 2288 mActionModeView.setVisibility(GONE); 2289 } 2290 if (mActionModeView != null) { 2291 mActionModeView.removeAllViews(); 2292 } 2293 if (getCallback() != null && !isDestroyed()) { 2294 try { 2295 getCallback().onActionModeFinished(mActionMode); 2296 } catch (AbstractMethodError ame) { 2297 // Older apps might not implement this callback method. 2298 } 2299 } 2300 mActionMode = null; 2301 } 2302 } 2303 } 2304 2305 protected DecorView generateDecor() { 2306 return new DecorView(getContext(), -1); 2307 } 2308 2309 protected void setFeatureFromAttrs(int featureId, TypedArray attrs, 2310 int drawableAttr, int alphaAttr) { 2311 Drawable d = attrs.getDrawable(drawableAttr); 2312 if (d != null) { 2313 requestFeature(featureId); 2314 setFeatureDefaultDrawable(featureId, d); 2315 } 2316 if ((getFeatures() & (1 << featureId)) != 0) { 2317 int alpha = attrs.getInt(alphaAttr, -1); 2318 if (alpha >= 0) { 2319 setFeatureDrawableAlpha(featureId, alpha); 2320 } 2321 } 2322 } 2323 2324 protected ViewGroup generateLayout(DecorView decor) { 2325 // Apply data from current theme. 2326 2327 TypedArray a = getWindowStyle(); 2328 2329 if (false) { 2330 System.out.println("From style:"); 2331 String s = "Attrs:"; 2332 for (int i = 0; i < com.android.internal.R.styleable.Window.length; i++) { 2333 s = s + " " + Integer.toHexString(com.android.internal.R.styleable.Window[i]) + "=" 2334 + a.getString(i); 2335 } 2336 System.out.println(s); 2337 } 2338 2339 mIsFloating = a.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating, false); 2340 int flagsToUpdate = (FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR) 2341 & (~getForcedWindowFlags()); 2342 if (mIsFloating) { 2343 setLayout(WRAP_CONTENT, WRAP_CONTENT); 2344 setFlags(0, flagsToUpdate); 2345 } else { 2346 setFlags(FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR, flagsToUpdate); 2347 } 2348 2349 if (a.getBoolean(com.android.internal.R.styleable.Window_windowNoTitle, false)) { 2350 requestFeature(FEATURE_NO_TITLE); 2351 } else if (a.getBoolean(com.android.internal.R.styleable.Window_windowActionBar, false)) { 2352 // Don't allow an action bar if there is no title. 2353 requestFeature(FEATURE_ACTION_BAR); 2354 } 2355 2356 if (a.getBoolean(com.android.internal.R.styleable.Window_windowActionBarOverlay, false)) { 2357 requestFeature(FEATURE_ACTION_BAR_OVERLAY); 2358 } 2359 2360 if (a.getBoolean(com.android.internal.R.styleable.Window_windowActionModeOverlay, false)) { 2361 requestFeature(FEATURE_ACTION_MODE_OVERLAY); 2362 } 2363 2364 if (a.getBoolean(com.android.internal.R.styleable.Window_windowFullscreen, false)) { 2365 setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN&(~getForcedWindowFlags())); 2366 } 2367 2368 if (a.getBoolean(com.android.internal.R.styleable.Window_windowShowWallpaper, false)) { 2369 setFlags(FLAG_SHOW_WALLPAPER, FLAG_SHOW_WALLPAPER&(~getForcedWindowFlags())); 2370 } 2371 2372 if (a.getBoolean(com.android.internal.R.styleable.Window_windowEnableSplitTouch, 2373 getContext().getApplicationInfo().targetSdkVersion 2374 >= android.os.Build.VERSION_CODES.HONEYCOMB)) { 2375 setFlags(FLAG_SPLIT_TOUCH, FLAG_SPLIT_TOUCH&(~getForcedWindowFlags())); 2376 } 2377 2378 a.getValue(com.android.internal.R.styleable.Window_windowMinWidthMajor, mMinWidthMajor); 2379 a.getValue(com.android.internal.R.styleable.Window_windowMinWidthMinor, mMinWidthMinor); 2380 2381 if (getContext().getApplicationInfo().targetSdkVersion 2382 < android.os.Build.VERSION_CODES.HONEYCOMB) { 2383 addFlags(WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY); 2384 } 2385 2386 if (mAlwaysReadCloseOnTouchAttr || getContext().getApplicationInfo().targetSdkVersion 2387 >= android.os.Build.VERSION_CODES.HONEYCOMB) { 2388 if (a.getBoolean( 2389 com.android.internal.R.styleable.Window_windowCloseOnTouchOutside, 2390 false)) { 2391 setCloseOnTouchOutsideIfNotSet(true); 2392 } 2393 } 2394 2395 WindowManager.LayoutParams params = getAttributes(); 2396 2397 if (!hasSoftInputMode()) { 2398 params.softInputMode = a.getInt( 2399 com.android.internal.R.styleable.Window_windowSoftInputMode, 2400 params.softInputMode); 2401 } 2402 2403 if (a.getBoolean(com.android.internal.R.styleable.Window_backgroundDimEnabled, 2404 mIsFloating)) { 2405 /* All dialogs should have the window dimmed */ 2406 if ((getForcedWindowFlags()&WindowManager.LayoutParams.FLAG_DIM_BEHIND) == 0) { 2407 params.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND; 2408 } 2409 params.dimAmount = a.getFloat( 2410 android.R.styleable.Window_backgroundDimAmount, 0.5f); 2411 } 2412 2413 if (params.windowAnimations == 0) { 2414 params.windowAnimations = a.getResourceId( 2415 com.android.internal.R.styleable.Window_windowAnimationStyle, 0); 2416 } 2417 2418 // The rest are only done if this window is not embedded; otherwise, 2419 // the values are inherited from our container. 2420 if (getContainer() == null) { 2421 if (mBackgroundDrawable == null) { 2422 if (mBackgroundResource == 0) { 2423 mBackgroundResource = a.getResourceId( 2424 com.android.internal.R.styleable.Window_windowBackground, 0); 2425 } 2426 if (mFrameResource == 0) { 2427 mFrameResource = a.getResourceId(com.android.internal.R.styleable.Window_windowFrame, 0); 2428 } 2429 if (false) { 2430 System.out.println("Background: " 2431 + Integer.toHexString(mBackgroundResource) + " Frame: " 2432 + Integer.toHexString(mFrameResource)); 2433 } 2434 } 2435 mTextColor = a.getColor(com.android.internal.R.styleable.Window_textColor, 0xFF000000); 2436 } 2437 2438 // Inflate the window decor. 2439 2440 int layoutResource; 2441 int features = getLocalFeatures(); 2442 // System.out.println("Features: 0x" + Integer.toHexString(features)); 2443 if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) { 2444 if (mIsFloating) { 2445 TypedValue res = new TypedValue(); 2446 getContext().getTheme().resolveAttribute( 2447 com.android.internal.R.attr.dialogTitleIconsDecorLayout, res, true); 2448 layoutResource = res.resourceId; 2449 } else { 2450 layoutResource = com.android.internal.R.layout.screen_title_icons; 2451 } 2452 // XXX Remove this once action bar supports these features. 2453 removeFeature(FEATURE_ACTION_BAR); 2454 // System.out.println("Title Icons!"); 2455 } else if ((features & ((1 << FEATURE_PROGRESS) | (1 << FEATURE_INDETERMINATE_PROGRESS))) != 0 2456 && (features & (1 << FEATURE_ACTION_BAR)) == 0) { 2457 // Special case for a window with only a progress bar (and title). 2458 // XXX Need to have a no-title version of embedded windows. 2459 layoutResource = com.android.internal.R.layout.screen_progress; 2460 // System.out.println("Progress!"); 2461 } else if ((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) { 2462 // Special case for a window with a custom title. 2463 // If the window is floating, we need a dialog layout 2464 if (mIsFloating) { 2465 TypedValue res = new TypedValue(); 2466 getContext().getTheme().resolveAttribute( 2467 com.android.internal.R.attr.dialogCustomTitleDecorLayout, res, true); 2468 layoutResource = res.resourceId; 2469 } else { 2470 layoutResource = com.android.internal.R.layout.screen_custom_title; 2471 } 2472 // XXX Remove this once action bar supports these features. 2473 removeFeature(FEATURE_ACTION_BAR); 2474 } else if ((features & (1 << FEATURE_NO_TITLE)) == 0) { 2475 // If no other features and not embedded, only need a title. 2476 // If the window is floating, we need a dialog layout 2477 if (mIsFloating) { 2478 TypedValue res = new TypedValue(); 2479 getContext().getTheme().resolveAttribute( 2480 com.android.internal.R.attr.dialogTitleDecorLayout, res, true); 2481 layoutResource = res.resourceId; 2482 } else if ((features & (1 << FEATURE_ACTION_BAR)) != 0) { 2483 if ((features & (1 << FEATURE_ACTION_BAR_OVERLAY)) != 0) { 2484 layoutResource = com.android.internal.R.layout.screen_action_bar_overlay; 2485 } else { 2486 layoutResource = com.android.internal.R.layout.screen_action_bar; 2487 } 2488 } else { 2489 layoutResource = com.android.internal.R.layout.screen_title; 2490 } 2491 // System.out.println("Title!"); 2492 } else { 2493 // Embedded, so no decoration is needed. 2494 layoutResource = com.android.internal.R.layout.screen_simple; 2495 // System.out.println("Simple!"); 2496 } 2497 2498 mDecor.startChanging(); 2499 2500 View in = mLayoutInflater.inflate(layoutResource, null); 2501 decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); 2502 2503 ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT); 2504 if (contentParent == null) { 2505 throw new RuntimeException("Window couldn't find content container view"); 2506 } 2507 2508 if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) { 2509 ProgressBar progress = getCircularProgressBar(false); 2510 if (progress != null) { 2511 progress.setIndeterminate(true); 2512 } 2513 } 2514 2515 // Remaining setup -- of background and title -- that only applies 2516 // to top-level windows. 2517 if (getContainer() == null) { 2518 Drawable drawable = mBackgroundDrawable; 2519 if (mBackgroundResource != 0) { 2520 drawable = getContext().getResources().getDrawable(mBackgroundResource); 2521 } 2522 mDecor.setWindowBackground(drawable); 2523 drawable = null; 2524 if (mFrameResource != 0) { 2525 drawable = getContext().getResources().getDrawable(mFrameResource); 2526 } 2527 mDecor.setWindowFrame(drawable); 2528 2529 // System.out.println("Text=" + Integer.toHexString(mTextColor) + 2530 // " Sel=" + Integer.toHexString(mTextSelectedColor) + 2531 // " Title=" + Integer.toHexString(mTitleColor)); 2532 2533 if (mTitleColor == 0) { 2534 mTitleColor = mTextColor; 2535 } 2536 2537 if (mTitle != null) { 2538 setTitle(mTitle); 2539 } 2540 setTitleColor(mTitleColor); 2541 } 2542 2543 mDecor.finishChanging(); 2544 2545 return contentParent; 2546 } 2547 2548 /** @hide */ 2549 public void alwaysReadCloseOnTouchAttr() { 2550 mAlwaysReadCloseOnTouchAttr = true; 2551 } 2552 2553 private void installDecor() { 2554 if (mDecor == null) { 2555 mDecor = generateDecor(); 2556 mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); 2557 mDecor.setIsRootNamespace(true); 2558 } 2559 if (mContentParent == null) { 2560 mContentParent = generateLayout(mDecor); 2561 2562 mTitleView = (TextView)findViewById(com.android.internal.R.id.title); 2563 if (mTitleView != null) { 2564 if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) { 2565 View titleContainer = findViewById(com.android.internal.R.id.title_container); 2566 if (titleContainer != null) { 2567 titleContainer.setVisibility(View.GONE); 2568 } else { 2569 mTitleView.setVisibility(View.GONE); 2570 } 2571 if (mContentParent instanceof FrameLayout) { 2572 ((FrameLayout)mContentParent).setForeground(null); 2573 } 2574 } else { 2575 mTitleView.setText(mTitle); 2576 } 2577 } else { 2578 mActionBar = (ActionBarView) findViewById(com.android.internal.R.id.action_bar); 2579 if (mActionBar != null) { 2580 if (mActionBar.getTitle() == null) { 2581 mActionBar.setWindowTitle(mTitle); 2582 } 2583 final int localFeatures = getLocalFeatures(); 2584 if ((localFeatures & (1 << FEATURE_PROGRESS)) != 0) { 2585 mActionBar.initProgress(); 2586 } 2587 if ((localFeatures & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) { 2588 mActionBar.initIndeterminateProgress(); 2589 } 2590 2591 final boolean splitActionBar = getWindowStyle().getBoolean( 2592 com.android.internal.R.styleable.Window_windowSplitActionBar, false); 2593 if (splitActionBar) { 2594 final ViewGroup splitView = (ViewGroup) findViewById( 2595 com.android.internal.R.id.lower_action_context_bar); 2596 if (splitView != null) { 2597 mActionBar.setSplitActionBar(splitActionBar); 2598 mActionBar.setSplitView(splitView); 2599 } else { 2600 Log.e(TAG, "Window style requested split action bar with " + 2601 "incompatible window decor! Ignoring request."); 2602 } 2603 } 2604 2605 // Post the panel invalidate for later; avoid application onCreateOptionsMenu 2606 // being called in the middle of onCreate or similar. 2607 mDecor.post(new Runnable() { 2608 public void run() { 2609 if (!isDestroyed()) { 2610 invalidatePanelMenu(FEATURE_ACTION_BAR); 2611 } 2612 } 2613 }); 2614 } 2615 } 2616 } 2617 } 2618 2619 private Drawable loadImageURI(Uri uri) { 2620 try { 2621 return Drawable.createFromStream( 2622 getContext().getContentResolver().openInputStream(uri), null); 2623 } catch (Exception e) { 2624 Log.w(TAG, "Unable to open content: " + uri); 2625 } 2626 return null; 2627 } 2628 2629 private DrawableFeatureState getDrawableState(int featureId, boolean required) { 2630 if ((getFeatures() & (1 << featureId)) == 0) { 2631 if (!required) { 2632 return null; 2633 } 2634 throw new RuntimeException("The feature has not been requested"); 2635 } 2636 2637 DrawableFeatureState[] ar; 2638 if ((ar = mDrawables) == null || ar.length <= featureId) { 2639 DrawableFeatureState[] nar = new DrawableFeatureState[featureId + 1]; 2640 if (ar != null) { 2641 System.arraycopy(ar, 0, nar, 0, ar.length); 2642 } 2643 mDrawables = ar = nar; 2644 } 2645 2646 DrawableFeatureState st = ar[featureId]; 2647 if (st == null) { 2648 ar[featureId] = st = new DrawableFeatureState(featureId); 2649 } 2650 return st; 2651 } 2652 2653 /** 2654 * Gets a panel's state based on its feature ID. 2655 * 2656 * @param featureId The feature ID of the panel. 2657 * @param required Whether the panel is required (if it is required and it 2658 * isn't in our features, this throws an exception). 2659 * @return The panel state. 2660 */ 2661 private PanelFeatureState getPanelState(int featureId, boolean required) { 2662 return getPanelState(featureId, required, null); 2663 } 2664 2665 /** 2666 * Gets a panel's state based on its feature ID. 2667 * 2668 * @param featureId The feature ID of the panel. 2669 * @param required Whether the panel is required (if it is required and it 2670 * isn't in our features, this throws an exception). 2671 * @param convertPanelState Optional: If the panel state does not exist, use 2672 * this as the panel state. 2673 * @return The panel state. 2674 */ 2675 private PanelFeatureState getPanelState(int featureId, boolean required, 2676 PanelFeatureState convertPanelState) { 2677 if ((getFeatures() & (1 << featureId)) == 0) { 2678 if (!required) { 2679 return null; 2680 } 2681 throw new RuntimeException("The feature has not been requested"); 2682 } 2683 2684 PanelFeatureState[] ar; 2685 if ((ar = mPanels) == null || ar.length <= featureId) { 2686 PanelFeatureState[] nar = new PanelFeatureState[featureId + 1]; 2687 if (ar != null) { 2688 System.arraycopy(ar, 0, nar, 0, ar.length); 2689 } 2690 mPanels = ar = nar; 2691 } 2692 2693 PanelFeatureState st = ar[featureId]; 2694 if (st == null) { 2695 ar[featureId] = st = (convertPanelState != null) 2696 ? convertPanelState 2697 : new PanelFeatureState(featureId); 2698 } 2699 return st; 2700 } 2701 2702 @Override 2703 public final void setChildDrawable(int featureId, Drawable drawable) { 2704 DrawableFeatureState st = getDrawableState(featureId, true); 2705 st.child = drawable; 2706 updateDrawable(featureId, st, false); 2707 } 2708 2709 @Override 2710 public final void setChildInt(int featureId, int value) { 2711 updateInt(featureId, value, false); 2712 } 2713 2714 @Override 2715 public boolean isShortcutKey(int keyCode, KeyEvent event) { 2716 PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true); 2717 return st.menu != null && st.menu.isShortcutKey(keyCode, event); 2718 } 2719 2720 private void updateDrawable(int featureId, DrawableFeatureState st, boolean fromResume) { 2721 // Do nothing if the decor is not yet installed... an update will 2722 // need to be forced when we eventually become active. 2723 if (mContentParent == null) { 2724 return; 2725 } 2726 2727 final int featureMask = 1 << featureId; 2728 2729 if ((getFeatures() & featureMask) == 0 && !fromResume) { 2730 return; 2731 } 2732 2733 Drawable drawable = null; 2734 if (st != null) { 2735 drawable = st.child; 2736 if (drawable == null) 2737 drawable = st.local; 2738 if (drawable == null) 2739 drawable = st.def; 2740 } 2741 if ((getLocalFeatures() & featureMask) == 0) { 2742 if (getContainer() != null) { 2743 if (isActive() || fromResume) { 2744 getContainer().setChildDrawable(featureId, drawable); 2745 } 2746 } 2747 } else if (st != null && (st.cur != drawable || st.curAlpha != st.alpha)) { 2748 // System.out.println("Drawable changed: old=" + st.cur 2749 // + ", new=" + drawable); 2750 st.cur = drawable; 2751 st.curAlpha = st.alpha; 2752 onDrawableChanged(featureId, drawable, st.alpha); 2753 } 2754 } 2755 2756 private void updateInt(int featureId, int value, boolean fromResume) { 2757 2758 // Do nothing if the decor is not yet installed... an update will 2759 // need to be forced when we eventually become active. 2760 if (mContentParent == null) { 2761 return; 2762 } 2763 2764 final int featureMask = 1 << featureId; 2765 2766 if ((getFeatures() & featureMask) == 0 && !fromResume) { 2767 return; 2768 } 2769 2770 if ((getLocalFeatures() & featureMask) == 0) { 2771 if (getContainer() != null) { 2772 getContainer().setChildInt(featureId, value); 2773 } 2774 } else { 2775 onIntChanged(featureId, value); 2776 } 2777 } 2778 2779 private ImageView getLeftIconView() { 2780 if (mLeftIconView != null) { 2781 return mLeftIconView; 2782 } 2783 if (mContentParent == null) { 2784 installDecor(); 2785 } 2786 return (mLeftIconView = (ImageView)findViewById(com.android.internal.R.id.left_icon)); 2787 } 2788 2789 private ProgressBar getCircularProgressBar(boolean shouldInstallDecor) { 2790 if (mCircularProgressBar != null) { 2791 return mCircularProgressBar; 2792 } 2793 if (mContentParent == null && shouldInstallDecor) { 2794 installDecor(); 2795 } 2796 mCircularProgressBar = (ProgressBar) findViewById(com.android.internal.R.id.progress_circular); 2797 if (mCircularProgressBar != null) { 2798 mCircularProgressBar.setVisibility(View.INVISIBLE); 2799 } 2800 return mCircularProgressBar; 2801 } 2802 2803 private ProgressBar getHorizontalProgressBar(boolean shouldInstallDecor) { 2804 if (mHorizontalProgressBar != null) { 2805 return mHorizontalProgressBar; 2806 } 2807 if (mContentParent == null && shouldInstallDecor) { 2808 installDecor(); 2809 } 2810 mHorizontalProgressBar = (ProgressBar) findViewById(com.android.internal.R.id.progress_horizontal); 2811 if (mHorizontalProgressBar != null) { 2812 mHorizontalProgressBar.setVisibility(View.INVISIBLE); 2813 } 2814 return mHorizontalProgressBar; 2815 } 2816 2817 private ImageView getRightIconView() { 2818 if (mRightIconView != null) { 2819 return mRightIconView; 2820 } 2821 if (mContentParent == null) { 2822 installDecor(); 2823 } 2824 return (mRightIconView = (ImageView)findViewById(com.android.internal.R.id.right_icon)); 2825 } 2826 2827 /** 2828 * Helper method for calling the {@link Callback#onPanelClosed(int, Menu)} 2829 * callback. This method will grab whatever extra state is needed for the 2830 * callback that isn't given in the parameters. If the panel is not open, 2831 * this will not perform the callback. 2832 * 2833 * @param featureId Feature ID of the panel that was closed. Must be given. 2834 * @param panel Panel that was closed. Optional but useful if there is no 2835 * menu given. 2836 * @param menu The menu that was closed. Optional, but give if you have. 2837 */ 2838 private void callOnPanelClosed(int featureId, PanelFeatureState panel, Menu menu) { 2839 final Callback cb = getCallback(); 2840 if (cb == null) 2841 return; 2842 2843 // Try to get a menu 2844 if (menu == null) { 2845 // Need a panel to grab the menu, so try to get that 2846 if (panel == null) { 2847 if ((featureId >= 0) && (featureId < mPanels.length)) { 2848 panel = mPanels[featureId]; 2849 } 2850 } 2851 2852 if (panel != null) { 2853 // menu still may be null, which is okay--we tried our best 2854 menu = panel.menu; 2855 } 2856 } 2857 2858 // If the panel is not open, do not callback 2859 if ((panel != null) && (!panel.isOpen)) 2860 return; 2861 2862 if (!isDestroyed()) { 2863 cb.onPanelClosed(featureId, menu); 2864 } 2865 } 2866 2867 /** 2868 * Helper method for adding launch-search to most applications. Opens the 2869 * search window using default settings. 2870 * 2871 * @return true if search window opened 2872 */ 2873 private boolean launchDefaultSearch() { 2874 final Callback cb = getCallback(); 2875 if (cb == null || isDestroyed()) { 2876 return false; 2877 } else { 2878 sendCloseSystemWindows("search"); 2879 return cb.onSearchRequested(); 2880 } 2881 } 2882 2883 @Override 2884 public void setVolumeControlStream(int streamType) { 2885 mVolumeControlStreamType = streamType; 2886 } 2887 2888 @Override 2889 public int getVolumeControlStream() { 2890 return mVolumeControlStreamType; 2891 } 2892 2893 private static final class DrawableFeatureState { 2894 DrawableFeatureState(int _featureId) { 2895 featureId = _featureId; 2896 } 2897 2898 final int featureId; 2899 2900 int resid; 2901 2902 Uri uri; 2903 2904 Drawable local; 2905 2906 Drawable child; 2907 2908 Drawable def; 2909 2910 Drawable cur; 2911 2912 int alpha = 255; 2913 2914 int curAlpha = 255; 2915 } 2916 2917 private static final class PanelFeatureState { 2918 2919 /** Feature ID for this panel. */ 2920 int featureId; 2921 2922 // Information pulled from the style for this panel. 2923 2924 int background; 2925 2926 /** The background when the panel spans the entire available width. */ 2927 int fullBackground; 2928 2929 int gravity; 2930 2931 int x; 2932 2933 int y; 2934 2935 int windowAnimations; 2936 2937 /** Dynamic state of the panel. */ 2938 DecorView decorView; 2939 2940 /** The panel that was returned by onCreatePanelView(). */ 2941 View createdPanelView; 2942 2943 /** The panel that we are actually showing. */ 2944 View shownPanelView; 2945 2946 /** Use {@link #setMenu} to set this. */ 2947 MenuBuilder menu; 2948 2949 IconMenuPresenter iconMenuPresenter; 2950 ListMenuPresenter expandedMenuPresenter; 2951 2952 /** 2953 * Whether the panel has been prepared (see 2954 * {@link PhoneWindow#preparePanel}). 2955 */ 2956 boolean isPrepared; 2957 2958 /** 2959 * Whether an item's action has been performed. This happens in obvious 2960 * scenarios (user clicks on menu item), but can also happen with 2961 * chording menu+(shortcut key). 2962 */ 2963 boolean isHandled; 2964 2965 boolean isOpen; 2966 2967 /** 2968 * True if the menu is in expanded mode, false if the menu is in icon 2969 * mode 2970 */ 2971 boolean isInExpandedMode; 2972 2973 public boolean qwertyMode; 2974 2975 boolean refreshDecorView; 2976 2977 boolean refreshMenuContent; 2978 2979 boolean wasLastOpen; 2980 2981 boolean wasLastExpanded; 2982 2983 /** 2984 * Contains the state of the menu when told to freeze. 2985 */ 2986 Bundle frozenMenuState; 2987 2988 PanelFeatureState(int featureId) { 2989 this.featureId = featureId; 2990 2991 refreshDecorView = false; 2992 } 2993 2994 public boolean hasPanelItems() { 2995 if (shownPanelView == null) return false; 2996 2997 if (isInExpandedMode) { 2998 return expandedMenuPresenter.getAdapter().getCount() > 0; 2999 } else { 3000 return ((ViewGroup) shownPanelView).getChildCount() > 0; 3001 } 3002 } 3003 3004 /** 3005 * Unregister and free attached MenuPresenters. They will be recreated as needed. 3006 */ 3007 public void clearMenuPresenters() { 3008 if (menu != null) { 3009 menu.removeMenuPresenter(iconMenuPresenter); 3010 menu.removeMenuPresenter(expandedMenuPresenter); 3011 } 3012 iconMenuPresenter = null; 3013 expandedMenuPresenter = null; 3014 } 3015 3016 void setStyle(Context context) { 3017 TypedArray a = context.obtainStyledAttributes(com.android.internal.R.styleable.Theme); 3018 background = a.getResourceId( 3019 com.android.internal.R.styleable.Theme_panelBackground, 0); 3020 fullBackground = a.getResourceId( 3021 com.android.internal.R.styleable.Theme_panelFullBackground, 0); 3022 windowAnimations = a.getResourceId( 3023 com.android.internal.R.styleable.Theme_windowAnimationStyle, 0); 3024 a.recycle(); 3025 } 3026 3027 void setMenu(MenuBuilder menu) { 3028 this.menu = menu; 3029 } 3030 3031 MenuView getExpandedMenuView(MenuPresenter.Callback cb) { 3032 if (menu == null) return null; 3033 3034 getIconMenuView(cb); // Need this initialized to know where our offset goes 3035 3036 boolean init = false; 3037 if (expandedMenuPresenter == null) { 3038 expandedMenuPresenter = new ListMenuPresenter( 3039 com.android.internal.R.layout.list_menu_item_layout, 3040 com.android.internal.R.style.Theme_ExpandedMenu); 3041 expandedMenuPresenter.setCallback(cb); 3042 menu.addMenuPresenter(expandedMenuPresenter); 3043 init = true; 3044 } 3045 3046 expandedMenuPresenter.setItemIndexOffset(iconMenuPresenter.getNumActualItemsShown()); 3047 MenuView result = expandedMenuPresenter.getMenuView(decorView); 3048 3049 if (init && frozenMenuState != null) { 3050 expandedMenuPresenter.restoreHierarchyState(frozenMenuState); 3051 // Once we initialize the expanded menu we're done with the frozen state 3052 // since we will have also restored any icon menu state. 3053 frozenMenuState = null; 3054 } 3055 3056 return result; 3057 } 3058 3059 MenuView getIconMenuView(MenuPresenter.Callback cb) { 3060 if (menu == null) return null; 3061 3062 boolean init = false; 3063 if (iconMenuPresenter == null) { 3064 iconMenuPresenter = new IconMenuPresenter(); 3065 iconMenuPresenter.setCallback(cb); 3066 menu.addMenuPresenter(iconMenuPresenter); 3067 init = true; 3068 } 3069 3070 MenuView result = iconMenuPresenter.getMenuView(decorView); 3071 3072 if (init && frozenMenuState != null) { 3073 iconMenuPresenter.restoreHierarchyState(frozenMenuState); 3074 } 3075 3076 return result; 3077 } 3078 3079 Parcelable onSaveInstanceState() { 3080 SavedState savedState = new SavedState(); 3081 savedState.featureId = featureId; 3082 savedState.isOpen = isOpen; 3083 savedState.isInExpandedMode = isInExpandedMode; 3084 3085 if (menu != null) { 3086 savedState.menuState = new Bundle(); 3087 if (iconMenuPresenter != null) { 3088 iconMenuPresenter.saveHierarchyState(savedState.menuState); 3089 } 3090 if (expandedMenuPresenter != null) { 3091 expandedMenuPresenter.saveHierarchyState(savedState.menuState); 3092 } 3093 } 3094 3095 return savedState; 3096 } 3097 3098 void onRestoreInstanceState(Parcelable state) { 3099 SavedState savedState = (SavedState) state; 3100 featureId = savedState.featureId; 3101 wasLastOpen = savedState.isOpen; 3102 wasLastExpanded = savedState.isInExpandedMode; 3103 frozenMenuState = savedState.menuState; 3104 3105 /* 3106 * A LocalActivityManager keeps the same instance of this class around. 3107 * The first time the menu is being shown after restoring, the 3108 * Activity.onCreateOptionsMenu should be called. But, if it is the 3109 * same instance then menu != null and we won't call that method. 3110 * So, clear this. Also clear any cached views. 3111 */ 3112 menu = null; 3113 createdPanelView = null; 3114 shownPanelView = null; 3115 decorView = null; 3116 } 3117 3118 private static class SavedState implements Parcelable { 3119 int featureId; 3120 boolean isOpen; 3121 boolean isInExpandedMode; 3122 Bundle menuState; 3123 3124 public int describeContents() { 3125 return 0; 3126 } 3127 3128 public void writeToParcel(Parcel dest, int flags) { 3129 dest.writeInt(featureId); 3130 dest.writeInt(isOpen ? 1 : 0); 3131 dest.writeInt(isInExpandedMode ? 1 : 0); 3132 3133 if (isOpen) { 3134 dest.writeBundle(menuState); 3135 } 3136 } 3137 3138 private static SavedState readFromParcel(Parcel source) { 3139 SavedState savedState = new SavedState(); 3140 savedState.featureId = source.readInt(); 3141 savedState.isOpen = source.readInt() == 1; 3142 savedState.isInExpandedMode = source.readInt() == 1; 3143 3144 if (savedState.isOpen) { 3145 savedState.menuState = source.readBundle(); 3146 } 3147 3148 return savedState; 3149 } 3150 3151 public static final Parcelable.Creator<SavedState> CREATOR 3152 = new Parcelable.Creator<SavedState>() { 3153 public SavedState createFromParcel(Parcel in) { 3154 return readFromParcel(in); 3155 } 3156 3157 public SavedState[] newArray(int size) { 3158 return new SavedState[size]; 3159 } 3160 }; 3161 } 3162 3163 } 3164 3165 /** 3166 * Simple implementation of MenuBuilder.Callback that: 3167 * <li> Opens a submenu when selected. 3168 * <li> Calls back to the callback's onMenuItemSelected when an item is 3169 * selected. 3170 */ 3171 private final class DialogMenuCallback implements MenuBuilder.Callback { 3172 private int mFeatureId; 3173 private MenuDialogHelper mSubMenuHelper; 3174 3175 public DialogMenuCallback(int featureId) { 3176 mFeatureId = featureId; 3177 } 3178 3179 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { 3180 if (allMenusAreClosing) { 3181 Callback callback = getCallback(); 3182 if (callback != null && !isDestroyed()) { 3183 callback.onPanelClosed(mFeatureId, menu); 3184 } 3185 3186 if (menu == mContextMenu) { 3187 dismissContextMenu(); 3188 } 3189 3190 // Dismiss the submenu, if it is showing 3191 if (mSubMenuHelper != null) { 3192 mSubMenuHelper.dismiss(); 3193 mSubMenuHelper = null; 3194 } 3195 } 3196 } 3197 3198 public void onCloseSubMenu(SubMenuBuilder menu) { 3199 Callback callback = getCallback(); 3200 if (callback != null && !isDestroyed()) { 3201 callback.onPanelClosed(mFeatureId, menu.getRootMenu()); 3202 } 3203 } 3204 3205 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { 3206 Callback callback = getCallback(); 3207 return (callback != null && !isDestroyed()) 3208 && callback.onMenuItemSelected(mFeatureId, item); 3209 } 3210 3211 public void onMenuModeChange(MenuBuilder menu) { 3212 } 3213 3214 public boolean onSubMenuSelected(SubMenuBuilder subMenu) { 3215 // Set a simple callback for the submenu 3216 subMenu.setCallback(this); 3217 3218 // The window manager will give us a valid window token 3219 mSubMenuHelper = new MenuDialogHelper(subMenu); 3220 mSubMenuHelper.show(null); 3221 3222 return true; 3223 } 3224 } 3225 3226 void sendCloseSystemWindows() { 3227 PhoneWindowManager.sendCloseSystemWindows(getContext(), null); 3228 } 3229 3230 void sendCloseSystemWindows(String reason) { 3231 PhoneWindowManager.sendCloseSystemWindows(getContext(), reason); 3232 } 3233} 3234