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