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