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