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