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