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