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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.internal.policy.impl;
18 import static android.view.View.MeasureSpec.AT_MOST;
19 import static android.view.View.MeasureSpec.EXACTLY;
20 import static android.view.View.MeasureSpec.getMode;
21 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
22 import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
23 import static android.view.WindowManager.LayoutParams.*;
25 import android.view.ViewConfiguration;
26 import com.android.internal.view.RootViewSurfaceTaker;
27 import com.android.internal.view.StandaloneActionMode;
28 import com.android.internal.view.menu.ContextMenuBuilder;
29 import com.android.internal.view.menu.IconMenuPresenter;
30 import com.android.internal.view.menu.ListMenuPresenter;
31 import com.android.internal.view.menu.MenuBuilder;
32 import com.android.internal.view.menu.MenuDialogHelper;
33 import com.android.internal.view.menu.MenuPresenter;
34 import com.android.internal.view.menu.MenuView;
35 import com.android.internal.widget.ActionBarContainer;
36 import com.android.internal.widget.ActionBarContextView;
37 import com.android.internal.widget.ActionBarOverlayLayout;
38 import com.android.internal.widget.ActionBarView;
40 import android.app.KeyguardManager;
41 import android.content.Context;
42 import android.content.pm.ActivityInfo;
43 import android.content.res.Configuration;
44 import android.content.res.Resources;
45 import android.content.res.TypedArray;
46 import android.graphics.Canvas;
47 import android.graphics.PixelFormat;
48 import android.graphics.Rect;
49 import android.graphics.drawable.Drawable;
50 import android.media.AudioManager;
51 import android.net.Uri;
52 import android.os.Bundle;
53 import android.os.Debug;
54 import android.os.Handler;
55 import android.os.Parcel;
56 import android.os.Parcelable;
57 import android.os.RemoteException;
58 import android.os.ServiceManager;
59 import android.util.AndroidRuntimeException;
60 import android.util.DisplayMetrics;
61 import android.util.EventLog;
62 import android.util.Log;
63 import android.util.Slog;
64 import android.util.SparseArray;
65 import android.util.TypedValue;
66 import android.view.ActionMode;
67 import android.view.ContextThemeWrapper;
68 import android.view.Gravity;
69 import android.view.IRotationWatcher;
70 import android.view.IWindowManager;
71 import android.view.InputEvent;
72 import android.view.InputQueue;
73 import android.view.KeyCharacterMap;
74 import android.view.KeyEvent;
75 import android.view.LayoutInflater;
76 import android.view.Menu;
77 import android.view.MenuItem;
78 import android.view.MotionEvent;
79 import android.view.SurfaceHolder;
80 import android.view.View;
81 import android.view.ViewGroup;
82 import android.view.ViewManager;
83 import android.view.ViewParent;
84 import android.view.ViewRootImpl;
85 import android.view.ViewStub;
86 import android.view.Window;
87 import android.view.WindowManager;
88 import android.view.accessibility.AccessibilityEvent;
89 import android.view.accessibility.AccessibilityManager;
90 import android.view.animation.Animation;
91 import android.view.animation.AnimationUtils;
92 import android.widget.FrameLayout;
93 import android.widget.ImageView;
94 import android.widget.PopupWindow;
95 import android.widget.ProgressBar;
96 import android.widget.TextView;
98 import java.lang.ref.WeakReference;
99 import java.util.ArrayList;
102 * Android-specific Window.
104 * todo: need to pull the generic functionality out into a base class
107 public class PhoneWindow extends Window implements MenuBuilder.Callback {
109 private final static String TAG = "PhoneWindow";
111 private final static boolean SWEEP_OPEN_MENU = false;
114 * Simple callback used by the context menu and its submenus. The options
115 * menu submenus do not use this (their behavior is more complex).
117 final DialogMenuCallback mContextMenuCallback = new DialogMenuCallback(FEATURE_CONTEXT_MENU);
119 final TypedValue mMinWidthMajor = new TypedValue();
120 final TypedValue mMinWidthMinor = new TypedValue();
121 TypedValue mFixedWidthMajor;
122 TypedValue mFixedWidthMinor;
123 TypedValue mFixedHeightMajor;
124 TypedValue mFixedHeightMinor;
126 // This is the top-level view of the window, containing the window decor.
127 private DecorView mDecor;
129 // This is the view in which the window contents are placed. It is either
130 // mDecor itself, or a child of mDecor where the contents go.
131 private ViewGroup mContentParent;
133 SurfaceHolder.Callback2 mTakeSurfaceCallback;
135 InputQueue.Callback mTakeInputQueueCallback;
137 private boolean mIsFloating;
139 private LayoutInflater mLayoutInflater;
141 private TextView mTitleView;
143 private ActionBarView mActionBar;
144 private ActionMenuPresenterCallback mActionMenuPresenterCallback;
145 private PanelMenuPresenterCallback mPanelMenuPresenterCallback;
147 // The icon resource has been explicitly set elsewhere
148 // and should not be overwritten with a default.
149 static final int FLAG_RESOURCE_SET_ICON = 1 << 0;
151 // The logo resource has been explicitly set elsewhere
152 // and should not be overwritten with a default.
153 static final int FLAG_RESOURCE_SET_LOGO = 1 << 1;
155 // The icon resource is currently configured to use the system fallback
156 // as no default was previously specified. Anything can override this.
157 static final int FLAG_RESOURCE_SET_ICON_FALLBACK = 1 << 2;
159 int mResourcesSetFlags;
163 private DrawableFeatureState[] mDrawables;
165 private PanelFeatureState[] mPanels;
168 * The panel that is prepared or opened (the most recent one if there are
169 * multiple panels). Shortcuts will go to this panel. It gets set in
170 * {@link #preparePanel} and cleared in {@link #closePanel}.
172 private PanelFeatureState mPreparedPanel;
175 * The keycode that is currently held down (as a modifier) for chording. If
176 * this is 0, there is no key held down.
178 private int mPanelChordingKey;
180 private ImageView mLeftIconView;
182 private ImageView mRightIconView;
184 private ProgressBar mCircularProgressBar;
186 private ProgressBar mHorizontalProgressBar;
188 private int mBackgroundResource = 0;
190 private Drawable mBackgroundDrawable;
192 private int mFrameResource = 0;
194 private int mTextColor = 0;
196 private CharSequence mTitle = null;
198 private int mTitleColor = 0;
200 private boolean mAlwaysReadCloseOnTouchAttr = false;
202 private ContextMenuBuilder mContextMenu;
203 private MenuDialogHelper mContextMenuHelper;
204 private boolean mClosingActionMenu;
206 private int mVolumeControlStreamType = AudioManager.USE_DEFAULT_STREAM_TYPE;
208 private AudioManager mAudioManager;
209 private KeyguardManager mKeyguardManager;
211 private int mUiOptions = 0;
213 private boolean mInvalidatePanelMenuPosted;
214 private int mInvalidatePanelMenuFeatures;
215 private final Runnable mInvalidatePanelMenuRunnable = new Runnable() {
216 @Override public void run() {
217 for (int i = 0; i <= FEATURE_MAX; i++) {
218 if ((mInvalidatePanelMenuFeatures & 1 << i) != 0) {
219 doInvalidatePanelMenu(i);
222 mInvalidatePanelMenuPosted = false;
223 mInvalidatePanelMenuFeatures = 0;
227 static class WindowManagerHolder {
228 static final IWindowManager sWindowManager = IWindowManager.Stub.asInterface(
229 ServiceManager.getService("window"));
232 static final RotationWatcher sRotationWatcher = new RotationWatcher();
234 public PhoneWindow(Context context) {
236 mLayoutInflater = LayoutInflater.from(context);
240 public final void setContainer(Window container) {
241 super.setContainer(container);
245 public boolean requestFeature(int featureId) {
246 if (mContentParent != null) {
247 throw new AndroidRuntimeException("requestFeature() must be called before adding content");
249 final int features = getFeatures();
250 if ((features != DEFAULT_FEATURES) && (featureId == FEATURE_CUSTOM_TITLE)) {
252 /* Another feature is enabled and the user is trying to enable the custom title feature */
253 throw new AndroidRuntimeException("You cannot combine custom titles with other title features");
255 if (((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) &&
256 (featureId != FEATURE_CUSTOM_TITLE) && (featureId != FEATURE_ACTION_MODE_OVERLAY)) {
258 /* Custom title feature is enabled and the user is trying to enable another feature */
259 throw new AndroidRuntimeException("You cannot combine custom titles with other title features");
261 if ((features & (1 << FEATURE_NO_TITLE)) != 0 && featureId == FEATURE_ACTION_BAR) {
262 return false; // Ignore. No title dominates.
264 if ((features & (1 << FEATURE_ACTION_BAR)) != 0 && featureId == FEATURE_NO_TITLE) {
265 // Remove the action bar feature if we have no title. No title dominates.
266 removeFeature(FEATURE_ACTION_BAR);
268 return super.requestFeature(featureId);
272 public void setUiOptions(int uiOptions) {
273 mUiOptions = uiOptions;
277 public void setUiOptions(int uiOptions, int mask) {
278 mUiOptions = (mUiOptions & ~mask) | (uiOptions & mask);
282 public void setContentView(int layoutResID) {
283 if (mContentParent == null) {
286 mContentParent.removeAllViews();
288 mLayoutInflater.inflate(layoutResID, mContentParent);
289 final Callback cb = getCallback();
290 if (cb != null && !isDestroyed()) {
291 cb.onContentChanged();
296 public void setContentView(View view) {
297 setContentView(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
301 public void setContentView(View view, ViewGroup.LayoutParams params) {
302 if (mContentParent == null) {
305 mContentParent.removeAllViews();
307 mContentParent.addView(view, params);
308 final Callback cb = getCallback();
309 if (cb != null && !isDestroyed()) {
310 cb.onContentChanged();
315 public void addContentView(View view, ViewGroup.LayoutParams params) {
316 if (mContentParent == null) {
319 mContentParent.addView(view, params);
320 final Callback cb = getCallback();
321 if (cb != null && !isDestroyed()) {
322 cb.onContentChanged();
327 public View getCurrentFocus() {
328 return mDecor != null ? mDecor.findFocus() : null;
332 public void takeSurface(SurfaceHolder.Callback2 callback) {
333 mTakeSurfaceCallback = callback;
336 public void takeInputQueue(InputQueue.Callback callback) {
337 mTakeInputQueueCallback = callback;
341 public boolean isFloating() {
346 * Return a LayoutInflater instance that can be used to inflate XML view layout
347 * resources for use in this Window.
349 * @return LayoutInflater The shared LayoutInflater.
352 public LayoutInflater getLayoutInflater() {
353 return mLayoutInflater;
357 public void setTitle(CharSequence title) {
358 if (mTitleView != null) {
359 mTitleView.setText(title);
360 } else if (mActionBar != null) {
361 mActionBar.setWindowTitle(title);
367 public void setTitleColor(int textColor) {
368 if (mTitleView != null) {
369 mTitleView.setTextColor(textColor);
371 mTitleColor = textColor;
375 * Prepares the panel to either be opened or chorded. This creates the Menu
376 * instance for the panel and populates it via the Activity callbacks.
378 * @param st The panel state to prepare.
379 * @param event The event that triggered the preparing of the panel.
380 * @return Whether the panel was prepared. If the panel should not be shown,
383 public final boolean preparePanel(PanelFeatureState st, KeyEvent event) {
388 // Already prepared (isPrepared will be reset to false later)
393 if ((mPreparedPanel != null) && (mPreparedPanel != st)) {
394 // Another Panel is prepared and possibly open, so close it
395 closePanel(mPreparedPanel, false);
398 final Callback cb = getCallback();
401 st.createdPanelView = cb.onCreatePanelView(st.featureId);
404 final boolean isActionBarMenu =
405 (st.featureId == FEATURE_OPTIONS_PANEL || st.featureId == FEATURE_ACTION_BAR);
407 if (isActionBarMenu && mActionBar != null) {
408 // Enforce ordering guarantees around events so that the action bar never
409 // dispatches menu-related events before the panel is prepared.
410 mActionBar.setMenuPrepared();
413 if (st.createdPanelView == null) {
414 // Init the panel state's menu--return false if init failed
415 if (st.menu == null || st.refreshMenuContent) {
416 if (st.menu == null) {
417 if (!initializePanelMenu(st) || (st.menu == null)) {
422 if (isActionBarMenu && mActionBar != null) {
423 if (mActionMenuPresenterCallback == null) {
424 mActionMenuPresenterCallback = new ActionMenuPresenterCallback();
426 mActionBar.setMenu(st.menu, mActionMenuPresenterCallback);
429 // Call callback, and return if it doesn't want to display menu.
431 // Creating the panel menu will involve a lot of manipulation;
432 // don't dispatch change events to presenters until we're done.
433 st.menu.stopDispatchingItemsChanged();
434 if ((cb == null) || !cb.onCreatePanelMenu(st.featureId, st.menu)) {
435 // Ditch the menu created above
438 if (isActionBarMenu && mActionBar != null) {
439 // Don't show it in the action bar either
440 mActionBar.setMenu(null, mActionMenuPresenterCallback);
446 st.refreshMenuContent = false;
449 // Callback and return if the callback does not want to show the menu
451 // Preparing the panel menu can involve a lot of manipulation;
452 // don't dispatch change events to presenters until we're done.
453 st.menu.stopDispatchingItemsChanged();
455 // Restore action view state before we prepare. This gives apps
456 // an opportunity to override frozen/restored state in onPrepare.
457 if (st.frozenActionViewState != null) {
458 st.menu.restoreActionViewStates(st.frozenActionViewState);
459 st.frozenActionViewState = null;
462 if (!cb.onPreparePanel(st.featureId, st.createdPanelView, st.menu)) {
463 if (isActionBarMenu && mActionBar != null) {
464 // The app didn't want to show the menu for now but it still exists.
465 // Clear it out of the action bar.
466 mActionBar.setMenu(null, mActionMenuPresenterCallback);
468 st.menu.startDispatchingItemsChanged();
472 // Set the proper keymap
473 KeyCharacterMap kmap = KeyCharacterMap.load(
474 event != null ? event.getDeviceId() : KeyCharacterMap.VIRTUAL_KEYBOARD);
475 st.qwertyMode = kmap.getKeyboardType() != KeyCharacterMap.NUMERIC;
476 st.menu.setQwertyMode(st.qwertyMode);
477 st.menu.startDispatchingItemsChanged();
481 st.isPrepared = true;
482 st.isHandled = false;
489 public void onConfigurationChanged(Configuration newConfig) {
490 // Action bars handle their own menu state
491 if (mActionBar == null) {
492 PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false);
493 if ((st != null) && (st.menu != null)) {
496 final Bundle state = new Bundle();
497 if (st.iconMenuPresenter != null) {
498 st.iconMenuPresenter.saveHierarchyState(state);
500 if (st.listMenuPresenter != null) {
501 st.listMenuPresenter.saveHierarchyState(state);
504 // Remove the menu views since they need to be recreated
505 // according to the new configuration
508 // Re-open the same menu
512 if (st.iconMenuPresenter != null) {
513 st.iconMenuPresenter.restoreHierarchyState(state);
515 if (st.listMenuPresenter != null) {
516 st.listMenuPresenter.restoreHierarchyState(state);
520 // Clear menu views so on next menu opening, it will use
528 private static void clearMenuViews(PanelFeatureState st) {
529 // This can be called on config changes, so we should make sure
530 // the views will be reconstructed based on the new orientation, etc.
532 // Allow the callback to create a new panel view
533 st.createdPanelView = null;
535 // Causes the decor view to be recreated
536 st.refreshDecorView = true;
538 st.clearMenuPresenters();
542 public final void openPanel(int featureId, KeyEvent event) {
543 if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null &&
544 mActionBar.isOverflowReserved() &&
545 !ViewConfiguration.get(getContext()).hasPermanentMenuKey()) {
546 if (mActionBar.getVisibility() == View.VISIBLE) {
547 mActionBar.showOverflowMenu();
550 openPanel(getPanelState(featureId, true), event);
554 private void openPanel(final PanelFeatureState st, KeyEvent event) {
555 // System.out.println("Open panel: isOpen=" + st.isOpen);
557 // Already open, return
558 if (st.isOpen || isDestroyed()) {
562 // Don't open an options panel for honeycomb apps on xlarge devices.
563 // (The app should be using an action bar for menu items.)
564 if (st.featureId == FEATURE_OPTIONS_PANEL) {
565 Context context = getContext();
566 Configuration config = context.getResources().getConfiguration();
567 boolean isXLarge = (config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) ==
568 Configuration.SCREENLAYOUT_SIZE_XLARGE;
569 boolean isHoneycombApp = context.getApplicationInfo().targetSdkVersion >=
570 android.os.Build.VERSION_CODES.HONEYCOMB;
572 if (isXLarge && isHoneycombApp) {
577 Callback cb = getCallback();
578 if ((cb != null) && (!cb.onMenuOpened(st.featureId, st.menu))) {
579 // Callback doesn't want the menu to open, reset any state
580 closePanel(st, true);
584 final WindowManager wm = getWindowManager();
589 // Prepare panel (should have been done before, but just in case)
590 if (!preparePanel(st, event)) {
594 int width = WRAP_CONTENT;
595 if (st.decorView == null || st.refreshDecorView) {
596 if (st.decorView == null) {
597 // Initialize the panel decor, this will populate st.decorView
598 if (!initializePanelDecor(st) || (st.decorView == null))
600 } else if (st.refreshDecorView && (st.decorView.getChildCount() > 0)) {
601 // Decor needs refreshing, so remove its views
602 st.decorView.removeAllViews();
605 // This will populate st.shownPanelView
606 if (!initializePanelContent(st) || !st.hasPanelItems()) {
610 ViewGroup.LayoutParams lp = st.shownPanelView.getLayoutParams();
612 lp = new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
616 if (lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {
617 // If the contents is fill parent for the width, set the
618 // corresponding background
619 backgroundResId = st.fullBackground;
620 width = MATCH_PARENT;
622 // Otherwise, set the normal panel background
623 backgroundResId = st.background;
625 st.decorView.setWindowBackground(getContext().getResources().getDrawable(
628 ViewParent shownPanelParent = st.shownPanelView.getParent();
629 if (shownPanelParent != null && shownPanelParent instanceof ViewGroup) {
630 ((ViewGroup) shownPanelParent).removeView(st.shownPanelView);
632 st.decorView.addView(st.shownPanelView, lp);
635 * Give focus to the view, if it or one of its children does not
638 if (!st.shownPanelView.hasFocus()) {
639 st.shownPanelView.requestFocus();
641 } else if (!st.isInListMode()) {
642 width = MATCH_PARENT;
643 } else if (st.createdPanelView != null) {
644 // If we already had a panel view, carry width=MATCH_PARENT through
645 // as we did above when it was created.
646 ViewGroup.LayoutParams lp = st.createdPanelView.getLayoutParams();
647 if (lp != null && lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {
648 width = MATCH_PARENT;
652 st.isHandled = false;
654 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
656 st.x, st.y, WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG,
657 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
658 | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
659 st.decorView.mDefaultOpacity);
662 lp.gravity = getOptionsPanelGravity();
663 sRotationWatcher.addWindow(this);
665 lp.gravity = st.gravity;
668 lp.windowAnimations = st.windowAnimations;
670 wm.addView(st.decorView, lp);
672 // Log.v(TAG, "Adding main menu to window manager.");
676 public final void closePanel(int featureId) {
677 if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null &&
678 mActionBar.isOverflowReserved() &&
679 !ViewConfiguration.get(getContext()).hasPermanentMenuKey()) {
680 mActionBar.hideOverflowMenu();
681 } else if (featureId == FEATURE_CONTEXT_MENU) {
684 closePanel(getPanelState(featureId, true), true);
689 * Closes the given panel.
691 * @param st The panel to be closed.
692 * @param doCallback Whether to notify the callback that the panel was
693 * closed. If the panel is in the process of re-opening or
694 * opening another panel (e.g., menu opening a sub menu), the
695 * callback should not happen and this variable should be false.
696 * In addition, this method internally will only perform the
697 * callback if the panel is open.
699 public final void closePanel(PanelFeatureState st, boolean doCallback) {
700 // System.out.println("Close panel: isOpen=" + st.isOpen);
701 if (doCallback && st.featureId == FEATURE_OPTIONS_PANEL &&
702 mActionBar != null && mActionBar.isOverflowMenuShowing()) {
703 checkCloseActionMenu(st.menu);
707 final ViewManager wm = getWindowManager();
708 if ((wm != null) && st.isOpen) {
709 if (st.decorView != null) {
710 wm.removeView(st.decorView);
711 // Log.v(TAG, "Removing main menu from window manager.");
713 sRotationWatcher.removeWindow(this);
718 callOnPanelClosed(st.featureId, st, null);
722 st.isPrepared = false;
723 st.isHandled = false;
726 // This view is no longer shown, so null it out
727 st.shownPanelView = null;
729 if (st.isInExpandedMode) {
730 // Next time the menu opens, it should not be in expanded mode, so
731 // force a refresh of the decor
732 st.refreshDecorView = true;
733 st.isInExpandedMode = false;
736 if (mPreparedPanel == st) {
737 mPreparedPanel = null;
738 mPanelChordingKey = 0;
742 void checkCloseActionMenu(Menu menu) {
743 if (mClosingActionMenu) {
747 mClosingActionMenu = true;
748 mActionBar.dismissPopupMenus();
749 Callback cb = getCallback();
750 if (cb != null && !isDestroyed()) {
751 cb.onPanelClosed(FEATURE_ACTION_BAR, menu);
753 mClosingActionMenu = false;
757 public final void togglePanel(int featureId, KeyEvent event) {
758 PanelFeatureState st = getPanelState(featureId, true);
760 closePanel(st, true);
762 openPanel(st, event);
767 public void invalidatePanelMenu(int featureId) {
768 mInvalidatePanelMenuFeatures |= 1 << featureId;
770 if (!mInvalidatePanelMenuPosted && mDecor != null) {
771 mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
772 mInvalidatePanelMenuPosted = true;
776 void doInvalidatePanelMenu(int featureId) {
777 PanelFeatureState st = getPanelState(featureId, true);
778 Bundle savedActionViewStates = null;
779 if (st.menu != null) {
780 savedActionViewStates = new Bundle();
781 st.menu.saveActionViewStates(savedActionViewStates);
782 if (savedActionViewStates.size() > 0) {
783 st.frozenActionViewState = savedActionViewStates;
785 // This will be started again when the panel is prepared.
786 st.menu.stopDispatchingItemsChanged();
789 st.refreshMenuContent = true;
790 st.refreshDecorView = true;
792 // Prepare the options panel if we have an action bar
793 if ((featureId == FEATURE_ACTION_BAR || featureId == FEATURE_OPTIONS_PANEL)
794 && mActionBar != null) {
795 st = getPanelState(Window.FEATURE_OPTIONS_PANEL, false);
797 st.isPrepared = false;
798 preparePanel(st, null);
804 * Called when the panel key is pushed down.
805 * @param featureId The feature ID of the relevant panel (defaults to FEATURE_OPTIONS_PANEL}.
806 * @param event The key event.
807 * @return Whether the key was handled.
809 public final boolean onKeyDownPanel(int featureId, KeyEvent event) {
810 final int keyCode = event.getKeyCode();
812 if (event.getRepeatCount() == 0) {
813 // The panel key was pushed, so set the chording key
814 mPanelChordingKey = keyCode;
816 PanelFeatureState st = getPanelState(featureId, true);
818 return preparePanel(st, event);
826 * Called when the panel key is released.
827 * @param featureId The feature ID of the relevant panel (defaults to FEATURE_OPTIONS_PANEL}.
828 * @param event The key event.
830 public final void onKeyUpPanel(int featureId, KeyEvent event) {
831 // The panel key was released, so clear the chording key
832 if (mPanelChordingKey != 0) {
833 mPanelChordingKey = 0;
835 if (event.isCanceled() || (mDecor != null && mDecor.mActionMode != null)) {
839 boolean playSoundEffect = false;
840 final PanelFeatureState st = getPanelState(featureId, true);
841 if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null &&
842 mActionBar.isOverflowReserved() &&
843 !ViewConfiguration.get(getContext()).hasPermanentMenuKey()) {
844 if (mActionBar.getVisibility() == View.VISIBLE) {
845 if (!mActionBar.isOverflowMenuShowing()) {
846 if (!isDestroyed() && preparePanel(st, event)) {
847 playSoundEffect = mActionBar.showOverflowMenu();
850 playSoundEffect = mActionBar.hideOverflowMenu();
854 if (st.isOpen || st.isHandled) {
856 // Play the sound effect if the user closed an open menu (and not if
857 // they just released a menu shortcut)
858 playSoundEffect = st.isOpen;
861 closePanel(st, true);
863 } else if (st.isPrepared) {
865 if (st.refreshMenuContent) {
866 // Something may have invalidated the menu since we prepared it.
867 // Re-prepare it to refresh.
868 st.isPrepared = false;
869 show = preparePanel(st, event);
873 // Write 'menu opened' to event log
874 EventLog.writeEvent(50001, 0);
877 openPanel(st, event);
879 playSoundEffect = true;
884 if (playSoundEffect) {
885 AudioManager audioManager = (AudioManager) getContext().getSystemService(
886 Context.AUDIO_SERVICE);
887 if (audioManager != null) {
888 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
890 Log.w(TAG, "Couldn't get audio manager");
897 public final void closeAllPanels() {
898 final ViewManager wm = getWindowManager();
903 final PanelFeatureState[] panels = mPanels;
904 final int N = panels != null ? panels.length : 0;
905 for (int i = 0; i < N; i++) {
906 final PanelFeatureState panel = panels[i];
908 closePanel(panel, true);
916 * Closes the context menu. This notifies the menu logic of the close, along
917 * with dismissing it from the UI.
919 private synchronized void closeContextMenu() {
920 if (mContextMenu != null) {
921 mContextMenu.close();
922 dismissContextMenu();
927 * Dismisses just the context menu UI. To close the context menu, use
928 * {@link #closeContextMenu()}.
930 private synchronized void dismissContextMenu() {
933 if (mContextMenuHelper != null) {
934 mContextMenuHelper.dismiss();
935 mContextMenuHelper = null;
940 public boolean performPanelShortcut(int featureId, int keyCode, KeyEvent event, int flags) {
941 return performPanelShortcut(getPanelState(featureId, true), keyCode, event, flags);
944 private boolean performPanelShortcut(PanelFeatureState st, int keyCode, KeyEvent event,
946 if (event.isSystem() || (st == null)) {
950 boolean handled = false;
952 // Only try to perform menu shortcuts if preparePanel returned true (possible false
953 // return value from application not wanting to show the menu).
954 if ((st.isPrepared || preparePanel(st, event)) && st.menu != null) {
955 // The menu is prepared now, perform the shortcut on it
956 handled = st.menu.performShortcut(keyCode, event, flags);
963 // Only close down the menu if we don't have an action bar keeping it open.
964 if ((flags & Menu.FLAG_PERFORM_NO_CLOSE) == 0 && mActionBar == null) {
965 closePanel(st, true);
973 public boolean performPanelIdentifierAction(int featureId, int id, int flags) {
975 PanelFeatureState st = getPanelState(featureId, true);
976 if (!preparePanel(st, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU))) {
979 if (st.menu == null) {
983 boolean res = st.menu.performIdentifierAction(id, flags);
985 // Only close down the menu if we don't have an action bar keeping it open.
986 if (mActionBar == null) {
987 closePanel(st, true);
993 public PanelFeatureState findMenuPanel(Menu menu) {
994 final PanelFeatureState[] panels = mPanels;
995 final int N = panels != null ? panels.length : 0;
996 for (int i = 0; i < N; i++) {
997 final PanelFeatureState panel = panels[i];
998 if (panel != null && panel.menu == menu) {
1005 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
1006 final Callback cb = getCallback();
1007 if (cb != null && !isDestroyed()) {
1008 final PanelFeatureState panel = findMenuPanel(menu.getRootMenu());
1009 if (panel != null) {
1010 return cb.onMenuItemSelected(panel.featureId, item);
1016 public void onMenuModeChange(MenuBuilder menu) {
1020 private void reopenMenu(boolean toggleMenuMode) {
1021 if (mActionBar != null && mActionBar.isOverflowReserved() &&
1022 (!ViewConfiguration.get(getContext()).hasPermanentMenuKey() ||
1023 mActionBar.isOverflowMenuShowPending())) {
1024 final Callback cb = getCallback();
1025 if (!mActionBar.isOverflowMenuShowing() || !toggleMenuMode) {
1026 if (cb != null && !isDestroyed() && mActionBar.getVisibility() == View.VISIBLE) {
1027 // If we have a menu invalidation pending, do it now.
1028 if (mInvalidatePanelMenuPosted &&
1029 (mInvalidatePanelMenuFeatures & (1 << FEATURE_OPTIONS_PANEL)) != 0) {
1030 mDecor.removeCallbacks(mInvalidatePanelMenuRunnable);
1031 mInvalidatePanelMenuRunnable.run();
1034 final PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true);
1036 // If we don't have a menu or we're waiting for a full content refresh,
1037 // forget it. This is a lingering event that no longer matters.
1038 if (st.menu != null && !st.refreshMenuContent &&
1039 cb.onPreparePanel(FEATURE_OPTIONS_PANEL, st.createdPanelView, st.menu)) {
1040 cb.onMenuOpened(FEATURE_ACTION_BAR, st.menu);
1041 mActionBar.showOverflowMenu();
1045 mActionBar.hideOverflowMenu();
1046 if (cb != null && !isDestroyed()) {
1047 final PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true);
1048 cb.onPanelClosed(FEATURE_ACTION_BAR, st.menu);
1054 PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true);
1056 // Save the future expanded mode state since closePanel will reset it
1057 boolean newExpandedMode = toggleMenuMode ? !st.isInExpandedMode : st.isInExpandedMode;
1059 st.refreshDecorView = true;
1060 closePanel(st, false);
1062 // Set the expanded mode state
1063 st.isInExpandedMode = newExpandedMode;
1065 openPanel(st, null);
1069 * Initializes the menu associated with the given panel feature state. You
1070 * must at the very least set PanelFeatureState.menu to the Menu to be
1071 * associated with the given panel state. The default implementation creates
1072 * a new menu for the panel state.
1074 * @param st The panel whose menu is being initialized.
1075 * @return Whether the initialization was successful.
1077 protected boolean initializePanelMenu(final PanelFeatureState st) {
1078 Context context = getContext();
1080 // If we have an action bar, initialize the menu with a context themed for it.
1081 if ((st.featureId == FEATURE_OPTIONS_PANEL || st.featureId == FEATURE_ACTION_BAR) &&
1082 mActionBar != null) {
1083 TypedValue outValue = new TypedValue();
1084 Resources.Theme currentTheme = context.getTheme();
1085 currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme,
1087 final int targetThemeRes = outValue.resourceId;
1089 if (targetThemeRes != 0 && context.getThemeResId() != targetThemeRes) {
1090 context = new ContextThemeWrapper(context, targetThemeRes);
1094 final MenuBuilder menu = new MenuBuilder(context);
1096 menu.setCallback(this);
1103 * Perform initial setup of a panel. This should at the very least set the
1104 * style information in the PanelFeatureState and must set
1105 * PanelFeatureState.decor to the panel's window decor view.
1107 * @param st The panel being initialized.
1109 protected boolean initializePanelDecor(PanelFeatureState st) {
1110 st.decorView = new DecorView(getContext(), st.featureId);
1111 st.gravity = Gravity.CENTER | Gravity.BOTTOM;
1112 st.setStyle(getContext());
1118 * Determine the gravity value for the options panel. This can
1119 * differ in compact mode.
1121 * @return gravity value to use for the panel window
1123 private int getOptionsPanelGravity() {
1125 return WindowManagerHolder.sWindowManager.getPreferredOptionsPanelGravity();
1126 } catch (RemoteException ex) {
1127 Log.e(TAG, "Couldn't getOptionsPanelGravity; using default", ex);
1128 return Gravity.CENTER | Gravity.BOTTOM;
1132 void onOptionsPanelRotationChanged() {
1133 final PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false);
1134 if (st == null) return;
1136 final WindowManager.LayoutParams lp = st.decorView != null ?
1137 (WindowManager.LayoutParams) st.decorView.getLayoutParams() : null;
1139 lp.gravity = getOptionsPanelGravity();
1140 final ViewManager wm = getWindowManager();
1142 wm.updateViewLayout(st.decorView, lp);
1148 * Initializes the panel associated with the panel feature state. You must
1149 * at the very least set PanelFeatureState.panel to the View implementing
1150 * its contents. The default implementation gets the panel from the menu.
1152 * @param st The panel state being initialized.
1153 * @return Whether the initialization was successful.
1155 protected boolean initializePanelContent(PanelFeatureState st) {
1156 if (st.createdPanelView != null) {
1157 st.shownPanelView = st.createdPanelView;
1161 if (st.menu == null) {
1165 if (mPanelMenuPresenterCallback == null) {
1166 mPanelMenuPresenterCallback = new PanelMenuPresenterCallback();
1169 MenuView menuView = st.isInListMode()
1170 ? st.getListMenuView(getContext(), mPanelMenuPresenterCallback)
1171 : st.getIconMenuView(getContext(), mPanelMenuPresenterCallback);
1173 st.shownPanelView = (View) menuView;
1175 if (st.shownPanelView != null) {
1176 // Use the menu View's default animations if it has any
1177 final int defaultAnimations = menuView.getWindowAnimations();
1178 if (defaultAnimations != 0) {
1179 st.windowAnimations = defaultAnimations;
1188 public boolean performContextMenuIdentifierAction(int id, int flags) {
1189 return (mContextMenu != null) ? mContextMenu.performIdentifierAction(id, flags) : false;
1193 public final void setBackgroundDrawable(Drawable drawable) {
1194 if (drawable != mBackgroundDrawable || mBackgroundResource != 0) {
1195 mBackgroundResource = 0;
1196 mBackgroundDrawable = drawable;
1197 if (mDecor != null) {
1198 mDecor.setWindowBackground(drawable);
1204 public final void setFeatureDrawableResource(int featureId, int resId) {
1206 DrawableFeatureState st = getDrawableState(featureId, true);
1207 if (st.resid != resId) {
1210 st.local = getContext().getResources().getDrawable(resId);
1211 updateDrawable(featureId, st, false);
1214 setFeatureDrawable(featureId, null);
1219 public final void setFeatureDrawableUri(int featureId, Uri uri) {
1221 DrawableFeatureState st = getDrawableState(featureId, true);
1222 if (st.uri == null || !st.uri.equals(uri)) {
1225 st.local = loadImageURI(uri);
1226 updateDrawable(featureId, st, false);
1229 setFeatureDrawable(featureId, null);
1234 public final void setFeatureDrawable(int featureId, Drawable drawable) {
1235 DrawableFeatureState st = getDrawableState(featureId, true);
1238 if (st.local != drawable) {
1239 st.local = drawable;
1240 updateDrawable(featureId, st, false);
1245 public void setFeatureDrawableAlpha(int featureId, int alpha) {
1246 DrawableFeatureState st = getDrawableState(featureId, true);
1247 if (st.alpha != alpha) {
1249 updateDrawable(featureId, st, false);
1253 protected final void setFeatureDefaultDrawable(int featureId, Drawable drawable) {
1254 DrawableFeatureState st = getDrawableState(featureId, true);
1255 if (st.def != drawable) {
1257 updateDrawable(featureId, st, false);
1262 public final void setFeatureInt(int featureId, int value) {
1263 // XXX Should do more management (as with drawable features) to
1264 // deal with interactions between multiple window policies.
1265 updateInt(featureId, value, false);
1269 * Update the state of a drawable feature. This should be called, for every
1270 * drawable feature supported, as part of onActive(), to make sure that the
1271 * contents of a containing window is properly updated.
1274 * @param featureId The desired drawable feature to change.
1275 * @param fromActive Always true when called from onActive().
1277 protected final void updateDrawable(int featureId, boolean fromActive) {
1278 final DrawableFeatureState st = getDrawableState(featureId, false);
1280 updateDrawable(featureId, st, fromActive);
1285 * Called when a Drawable feature changes, for the window to update its
1288 * @param featureId The feature being changed.
1289 * @param drawable The new Drawable to show, or null if none.
1290 * @param alpha The new alpha blending of the Drawable.
1292 protected void onDrawableChanged(int featureId, Drawable drawable, int alpha) {
1294 if (featureId == FEATURE_LEFT_ICON) {
1295 view = getLeftIconView();
1296 } else if (featureId == FEATURE_RIGHT_ICON) {
1297 view = getRightIconView();
1302 if (drawable != null) {
1303 drawable.setAlpha(alpha);
1304 view.setImageDrawable(drawable);
1305 view.setVisibility(View.VISIBLE);
1307 view.setVisibility(View.GONE);
1312 * Called when an int feature changes, for the window to update its
1315 * @param featureId The feature being changed.
1316 * @param value The new integer value.
1318 protected void onIntChanged(int featureId, int value) {
1319 if (featureId == FEATURE_PROGRESS || featureId == FEATURE_INDETERMINATE_PROGRESS) {
1320 updateProgressBars(value);
1321 } else if (featureId == FEATURE_CUSTOM_TITLE) {
1322 FrameLayout titleContainer = (FrameLayout) findViewById(com.android.internal.R.id.title_container);
1323 if (titleContainer != null) {
1324 mLayoutInflater.inflate(value, titleContainer);
1330 * Updates the progress bars that are shown in the title bar.
1332 * @param value Can be one of {@link Window#PROGRESS_VISIBILITY_ON},
1333 * {@link Window#PROGRESS_VISIBILITY_OFF},
1334 * {@link Window#PROGRESS_INDETERMINATE_ON},
1335 * {@link Window#PROGRESS_INDETERMINATE_OFF}, or a value
1336 * starting at {@link Window#PROGRESS_START} through
1337 * {@link Window#PROGRESS_END} for setting the default
1338 * progress (if {@link Window#PROGRESS_END} is given,
1339 * the progress bar widgets in the title will be hidden after an
1340 * animation), a value between
1341 * {@link Window#PROGRESS_SECONDARY_START} -
1342 * {@link Window#PROGRESS_SECONDARY_END} for the
1343 * secondary progress (if
1344 * {@link Window#PROGRESS_SECONDARY_END} is given, the
1345 * progress bar widgets will still be shown with the secondary
1346 * progress bar will be completely filled in.)
1348 private void updateProgressBars(int value) {
1349 ProgressBar circularProgressBar = getCircularProgressBar(true);
1350 ProgressBar horizontalProgressBar = getHorizontalProgressBar(true);
1352 final int features = getLocalFeatures();
1353 if (value == PROGRESS_VISIBILITY_ON) {
1354 if ((features & (1 << FEATURE_PROGRESS)) != 0) {
1355 int level = horizontalProgressBar.getProgress();
1356 int visibility = (horizontalProgressBar.isIndeterminate() || level < 10000) ?
1357 View.VISIBLE : View.INVISIBLE;
1358 horizontalProgressBar.setVisibility(visibility);
1360 if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
1361 circularProgressBar.setVisibility(View.VISIBLE);
1363 } else if (value == PROGRESS_VISIBILITY_OFF) {
1364 if ((features & (1 << FEATURE_PROGRESS)) != 0) {
1365 horizontalProgressBar.setVisibility(View.GONE);
1367 if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
1368 circularProgressBar.setVisibility(View.GONE);
1370 } else if (value == PROGRESS_INDETERMINATE_ON) {
1371 horizontalProgressBar.setIndeterminate(true);
1372 } else if (value == PROGRESS_INDETERMINATE_OFF) {
1373 horizontalProgressBar.setIndeterminate(false);
1374 } else if (PROGRESS_START <= value && value <= PROGRESS_END) {
1375 // We want to set the progress value before testing for visibility
1376 // so that when the progress bar becomes visible again, it has the
1378 horizontalProgressBar.setProgress(value - PROGRESS_START);
1380 if (value < PROGRESS_END) {
1381 showProgressBars(horizontalProgressBar, circularProgressBar);
1383 hideProgressBars(horizontalProgressBar, circularProgressBar);
1385 } else if (PROGRESS_SECONDARY_START <= value && value <= PROGRESS_SECONDARY_END) {
1386 horizontalProgressBar.setSecondaryProgress(value - PROGRESS_SECONDARY_START);
1388 showProgressBars(horizontalProgressBar, circularProgressBar);
1393 private void showProgressBars(ProgressBar horizontalProgressBar, ProgressBar spinnyProgressBar) {
1394 final int features = getLocalFeatures();
1395 if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0 &&
1396 spinnyProgressBar.getVisibility() == View.INVISIBLE) {
1397 spinnyProgressBar.setVisibility(View.VISIBLE);
1399 // Only show the progress bars if the primary progress is not complete
1400 if ((features & (1 << FEATURE_PROGRESS)) != 0 &&
1401 horizontalProgressBar.getProgress() < 10000) {
1402 horizontalProgressBar.setVisibility(View.VISIBLE);
1406 private void hideProgressBars(ProgressBar horizontalProgressBar, ProgressBar spinnyProgressBar) {
1407 final int features = getLocalFeatures();
1408 Animation anim = AnimationUtils.loadAnimation(getContext(), com.android.internal.R.anim.fade_out);
1409 anim.setDuration(1000);
1410 if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0 &&
1411 spinnyProgressBar.getVisibility() == View.VISIBLE) {
1412 spinnyProgressBar.startAnimation(anim);
1413 spinnyProgressBar.setVisibility(View.INVISIBLE);
1415 if ((features & (1 << FEATURE_PROGRESS)) != 0 &&
1416 horizontalProgressBar.getVisibility() == View.VISIBLE) {
1417 horizontalProgressBar.startAnimation(anim);
1418 horizontalProgressBar.setVisibility(View.INVISIBLE);
1423 public void setIcon(int resId) {
1425 mResourcesSetFlags |= FLAG_RESOURCE_SET_ICON;
1426 mResourcesSetFlags &= ~FLAG_RESOURCE_SET_ICON_FALLBACK;
1427 if (mActionBar != null) {
1428 mActionBar.setIcon(resId);
1433 public void setDefaultIcon(int resId) {
1434 if ((mResourcesSetFlags & FLAG_RESOURCE_SET_ICON) != 0) {
1438 if (mActionBar != null && (!mActionBar.hasIcon() ||
1439 (mResourcesSetFlags & FLAG_RESOURCE_SET_ICON_FALLBACK) != 0)) {
1441 mActionBar.setIcon(resId);
1442 mResourcesSetFlags &= ~FLAG_RESOURCE_SET_ICON_FALLBACK;
1444 mActionBar.setIcon(getContext().getPackageManager().getDefaultActivityIcon());
1445 mResourcesSetFlags |= FLAG_RESOURCE_SET_ICON_FALLBACK;
1451 public void setLogo(int resId) {
1453 mResourcesSetFlags |= FLAG_RESOURCE_SET_LOGO;
1454 if (mActionBar != null) {
1455 mActionBar.setLogo(resId);
1460 public void setDefaultLogo(int resId) {
1461 if ((mResourcesSetFlags & FLAG_RESOURCE_SET_LOGO) != 0) {
1465 if (mActionBar != null && !mActionBar.hasLogo()) {
1466 mActionBar.setLogo(resId);
1471 public void setLocalFocus(boolean hasFocus, boolean inTouchMode) {
1472 getViewRootImpl().windowFocusChanged(hasFocus, inTouchMode);
1477 public void injectInputEvent(InputEvent event) {
1478 getViewRootImpl().dispatchInputEvent(event);
1481 private ViewRootImpl getViewRootImpl() {
1482 if (mDecor != null) {
1483 ViewRootImpl viewRootImpl = mDecor.getViewRootImpl();
1484 if (viewRootImpl != null) {
1485 return viewRootImpl;
1488 throw new IllegalStateException("view not added");
1492 * Request that key events come to this activity. Use this if your activity
1493 * has no views with focus, but the activity still wants a chance to process
1497 public void takeKeyEvents(boolean get) {
1498 mDecor.setFocusable(get);
1502 public boolean superDispatchKeyEvent(KeyEvent event) {
1503 return mDecor.superDispatchKeyEvent(event);
1507 public boolean superDispatchKeyShortcutEvent(KeyEvent event) {
1508 return mDecor.superDispatchKeyShortcutEvent(event);
1512 public boolean superDispatchTouchEvent(MotionEvent event) {
1513 return mDecor.superDispatchTouchEvent(event);
1517 public boolean superDispatchTrackballEvent(MotionEvent event) {
1518 return mDecor.superDispatchTrackballEvent(event);
1522 public boolean superDispatchGenericMotionEvent(MotionEvent event) {
1523 return mDecor.superDispatchGenericMotionEvent(event);
1527 * A key was pressed down and not handled by anything else in the window.
1530 * @see android.view.KeyEvent
1532 protected boolean onKeyDown(int featureId, int keyCode, KeyEvent event) {
1533 /* ****************************************************************************
1534 * HOW TO DECIDE WHERE YOUR KEY HANDLING GOES.
1536 * If your key handling must happen before the app gets a crack at the event,
1537 * it goes in PhoneWindowManager.
1539 * If your key handling should happen in all windows, and does not depend on
1540 * the state of the current application, other than that the current
1541 * application can override the behavior by handling the event itself, it
1542 * should go in PhoneFallbackEventHandler.
1544 * Only if your handling depends on the window, and the fact that it has
1545 * a DecorView, should it go here.
1546 * ****************************************************************************/
1548 final KeyEvent.DispatcherState dispatcher =
1549 mDecor != null ? mDecor.getKeyDispatcherState() : null;
1550 //Log.i(TAG, "Key down: repeat=" + event.getRepeatCount()
1551 // + " flags=0x" + Integer.toHexString(event.getFlags()));
1554 case KeyEvent.KEYCODE_VOLUME_UP:
1555 case KeyEvent.KEYCODE_VOLUME_DOWN:
1556 case KeyEvent.KEYCODE_VOLUME_MUTE: {
1557 // Similar code is in PhoneFallbackEventHandler in case the window
1558 // doesn't have one of these. In this case, we execute it here and
1559 // eat the event instead, because we have mVolumeControlStreamType
1561 getAudioManager().handleKeyDown(event, mVolumeControlStreamType);
1565 case KeyEvent.KEYCODE_MENU: {
1566 onKeyDownPanel((featureId < 0) ? FEATURE_OPTIONS_PANEL : featureId, event);
1570 case KeyEvent.KEYCODE_BACK: {
1571 if (event.getRepeatCount() > 0) break;
1572 if (featureId < 0) break;
1573 // Currently don't do anything with long press.
1574 if (dispatcher != null) {
1575 dispatcher.startTracking(event, this);
1585 private KeyguardManager getKeyguardManager() {
1586 if (mKeyguardManager == null) {
1587 mKeyguardManager = (KeyguardManager) getContext().getSystemService(
1588 Context.KEYGUARD_SERVICE);
1590 return mKeyguardManager;
1593 AudioManager getAudioManager() {
1594 if (mAudioManager == null) {
1595 mAudioManager = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
1597 return mAudioManager;
1601 * A key was released and not handled by anything else in the window.
1604 * @see android.view.KeyEvent
1606 protected boolean onKeyUp(int featureId, int keyCode, KeyEvent event) {
1607 final KeyEvent.DispatcherState dispatcher =
1608 mDecor != null ? mDecor.getKeyDispatcherState() : null;
1609 if (dispatcher != null) {
1610 dispatcher.handleUpEvent(event);
1612 //Log.i(TAG, "Key up: repeat=" + event.getRepeatCount()
1613 // + " flags=0x" + Integer.toHexString(event.getFlags()));
1616 case KeyEvent.KEYCODE_VOLUME_UP:
1617 case KeyEvent.KEYCODE_VOLUME_DOWN:
1618 case KeyEvent.KEYCODE_VOLUME_MUTE: {
1619 // Similar code is in PhoneFallbackEventHandler in case the window
1620 // doesn't have one of these. In this case, we execute it here and
1621 // eat the event instead, because we have mVolumeControlStreamType
1623 getAudioManager().handleKeyUp(event, mVolumeControlStreamType);
1627 case KeyEvent.KEYCODE_MENU: {
1628 onKeyUpPanel(featureId < 0 ? FEATURE_OPTIONS_PANEL : featureId,
1633 case KeyEvent.KEYCODE_BACK: {
1634 if (featureId < 0) break;
1635 if (event.isTracking() && !event.isCanceled()) {
1636 if (featureId == FEATURE_OPTIONS_PANEL) {
1637 PanelFeatureState st = getPanelState(featureId, false);
1638 if (st != null && st.isInExpandedMode) {
1639 // If the user is in an expanded menu and hits back, it
1640 // should go back to the icon menu
1645 closePanel(featureId);
1651 case KeyEvent.KEYCODE_SEARCH: {
1653 * Do this in onKeyUp since the Search key is also used for
1654 * chording quick launch shortcuts.
1656 if (getKeyguardManager().inKeyguardRestrictedInputMode()) {
1659 if (event.isTracking() && !event.isCanceled()) {
1660 launchDefaultSearch();
1670 protected void onActive() {
1674 public final View getDecorView() {
1675 if (mDecor == null) {
1682 public final View peekDecorView() {
1686 static private final String FOCUSED_ID_TAG = "android:focusedViewId";
1687 static private final String VIEWS_TAG = "android:views";
1688 static private final String PANELS_TAG = "android:Panels";
1689 static private final String ACTION_BAR_TAG = "android:ActionBar";
1691 /** {@inheritDoc} */
1693 public Bundle saveHierarchyState() {
1694 Bundle outState = new Bundle();
1695 if (mContentParent == null) {
1699 SparseArray<Parcelable> states = new SparseArray<Parcelable>();
1700 mContentParent.saveHierarchyState(states);
1701 outState.putSparseParcelableArray(VIEWS_TAG, states);
1703 // save the focused view id
1704 View focusedView = mContentParent.findFocus();
1705 if (focusedView != null) {
1706 if (focusedView.getId() != View.NO_ID) {
1707 outState.putInt(FOCUSED_ID_TAG, focusedView.getId());
1710 Log.d(TAG, "couldn't save which view has focus because the focused view "
1711 + focusedView + " has no id.");
1717 SparseArray<Parcelable> panelStates = new SparseArray<Parcelable>();
1718 savePanelState(panelStates);
1719 if (panelStates.size() > 0) {
1720 outState.putSparseParcelableArray(PANELS_TAG, panelStates);
1723 if (mActionBar != null) {
1724 SparseArray<Parcelable> actionBarStates = new SparseArray<Parcelable>();
1725 mActionBar.saveHierarchyState(actionBarStates);
1726 outState.putSparseParcelableArray(ACTION_BAR_TAG, actionBarStates);
1732 /** {@inheritDoc} */
1734 public void restoreHierarchyState(Bundle savedInstanceState) {
1735 if (mContentParent == null) {
1739 SparseArray<Parcelable> savedStates
1740 = savedInstanceState.getSparseParcelableArray(VIEWS_TAG);
1741 if (savedStates != null) {
1742 mContentParent.restoreHierarchyState(savedStates);
1745 // restore the focused view
1746 int focusedViewId = savedInstanceState.getInt(FOCUSED_ID_TAG, View.NO_ID);
1747 if (focusedViewId != View.NO_ID) {
1748 View needsFocus = mContentParent.findViewById(focusedViewId);
1749 if (needsFocus != null) {
1750 needsFocus.requestFocus();
1753 "Previously focused view reported id " + focusedViewId
1754 + " during save, but can't be found during restore.");
1758 // restore the panels
1759 SparseArray<Parcelable> panelStates = savedInstanceState.getSparseParcelableArray(PANELS_TAG);
1760 if (panelStates != null) {
1761 restorePanelState(panelStates);
1764 if (mActionBar != null) {
1765 SparseArray<Parcelable> actionBarStates =
1766 savedInstanceState.getSparseParcelableArray(ACTION_BAR_TAG);
1767 if (actionBarStates != null) {
1768 mActionBar.restoreHierarchyState(actionBarStates);
1770 Log.w(TAG, "Missing saved instance states for action bar views! " +
1771 "State will not be restored.");
1777 * Invoked when the panels should freeze their state.
1779 * @param icicles Save state into this. This is usually indexed by the
1780 * featureId. This will be given to {@link #restorePanelState} in the
1783 private void savePanelState(SparseArray<Parcelable> icicles) {
1784 PanelFeatureState[] panels = mPanels;
1785 if (panels == null) {
1789 for (int curFeatureId = panels.length - 1; curFeatureId >= 0; curFeatureId--) {
1790 if (panels[curFeatureId] != null) {
1791 icicles.put(curFeatureId, panels[curFeatureId].onSaveInstanceState());
1797 * Invoked when the panels should thaw their state from a previously frozen state.
1799 * @param icicles The state saved by {@link #savePanelState} that needs to be thawed.
1801 private void restorePanelState(SparseArray<Parcelable> icicles) {
1802 PanelFeatureState st;
1804 for (int i = icicles.size() - 1; i >= 0; i--) {
1805 curFeatureId = icicles.keyAt(i);
1806 st = getPanelState(curFeatureId, false /* required */);
1808 // The panel must not have been required, and is currently not around, skip it
1812 st.onRestoreInstanceState(icicles.get(curFeatureId));
1813 invalidatePanelMenu(curFeatureId);
1817 * Implementation note: call openPanelsAfterRestore later to actually open the
1823 * Opens the panels that have had their state restored. This should be
1824 * called sometime after {@link #restorePanelState} when it is safe to add
1825 * to the window manager.
1827 private void openPanelsAfterRestore() {
1828 PanelFeatureState[] panels = mPanels;
1830 if (panels == null) {
1834 PanelFeatureState st;
1835 for (int i = panels.length - 1; i >= 0; i--) {
1837 // We restore the panel if it was last open; we skip it if it
1838 // now is open, to avoid a race condition if the user immediately
1839 // opens it when we are resuming.
1841 st.applyFrozenState();
1842 if (!st.isOpen && st.wasLastOpen) {
1843 st.isInExpandedMode = st.wasLastExpanded;
1844 openPanel(st, null);
1850 private class PanelMenuPresenterCallback implements MenuPresenter.Callback {
1852 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
1853 final Menu parentMenu = menu.getRootMenu();
1854 final boolean isSubMenu = parentMenu != menu;
1855 final PanelFeatureState panel = findMenuPanel(isSubMenu ? parentMenu : menu);
1856 if (panel != null) {
1858 callOnPanelClosed(panel.featureId, panel, parentMenu);
1859 closePanel(panel, true);
1861 // Close the panel and only do the callback if the menu is being
1862 // closed completely, not if opening a sub menu
1863 closePanel(panel, allMenusAreClosing);
1869 public boolean onOpenSubMenu(MenuBuilder subMenu) {
1870 if (subMenu == null && hasFeature(FEATURE_ACTION_BAR)) {
1871 Callback cb = getCallback();
1872 if (cb != null && !isDestroyed()) {
1873 cb.onMenuOpened(FEATURE_ACTION_BAR, subMenu);
1881 private final class ActionMenuPresenterCallback implements MenuPresenter.Callback {
1883 public boolean onOpenSubMenu(MenuBuilder subMenu) {
1884 Callback cb = getCallback();
1886 cb.onMenuOpened(FEATURE_ACTION_BAR, subMenu);
1893 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
1894 checkCloseActionMenu(menu);
1898 private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {
1899 /* package */int mDefaultOpacity = PixelFormat.OPAQUE;
1901 /** The feature ID of the panel, or -1 if this is the application's DecorView */
1902 private final int mFeatureId;
1904 private final Rect mDrawingBounds = new Rect();
1906 private final Rect mBackgroundPadding = new Rect();
1908 private final Rect mFramePadding = new Rect();
1910 private final Rect mFrameOffsets = new Rect();
1912 private boolean mChanging;
1914 private Drawable mMenuBackground;
1915 private boolean mWatchingForMenu;
1918 private ActionMode mActionMode;
1919 private ActionBarContextView mActionModeView;
1920 private PopupWindow mActionModePopup;
1921 private Runnable mShowActionModePopup;
1923 public DecorView(Context context, int featureId) {
1925 mFeatureId = featureId;
1929 public boolean dispatchKeyEvent(KeyEvent event) {
1930 final int keyCode = event.getKeyCode();
1931 final int action = event.getAction();
1932 final boolean isDown = action == KeyEvent.ACTION_DOWN;
1934 if (isDown && (event.getRepeatCount() == 0)) {
1935 // First handle chording of panel key: if a panel key is held
1936 // but not released, try to execute a shortcut in it.
1937 if ((mPanelChordingKey > 0) && (mPanelChordingKey != keyCode)) {
1938 boolean handled = dispatchKeyShortcutEvent(event);
1944 // If a panel is open, perform a shortcut on it without the
1945 // chorded panel key
1946 if ((mPreparedPanel != null) && mPreparedPanel.isOpen) {
1947 if (performPanelShortcut(mPreparedPanel, keyCode, event, 0)) {
1953 if (!isDestroyed()) {
1954 final Callback cb = getCallback();
1955 final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event)
1956 : super.dispatchKeyEvent(event);
1962 return isDown ? PhoneWindow.this.onKeyDown(mFeatureId, event.getKeyCode(), event)
1963 : PhoneWindow.this.onKeyUp(mFeatureId, event.getKeyCode(), event);
1967 public boolean dispatchKeyShortcutEvent(KeyEvent ev) {
1968 // If the panel is already prepared, then perform the shortcut using it.
1970 if (mPreparedPanel != null) {
1971 handled = performPanelShortcut(mPreparedPanel, ev.getKeyCode(), ev,
1972 Menu.FLAG_PERFORM_NO_CLOSE);
1974 if (mPreparedPanel != null) {
1975 mPreparedPanel.isHandled = true;
1981 // Shortcut not handled by the panel. Dispatch to the view hierarchy.
1982 final Callback cb = getCallback();
1983 handled = cb != null && !isDestroyed() && mFeatureId < 0
1984 ? cb.dispatchKeyShortcutEvent(ev) : super.dispatchKeyShortcutEvent(ev);
1989 // If the panel is not prepared, then we may be trying to handle a shortcut key
1990 // combination such as Control+C. Temporarily prepare the panel then mark it
1991 // unprepared again when finished to ensure that the panel will again be prepared
1992 // the next time it is shown for real.
1993 if (mPreparedPanel == null) {
1994 PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true);
1995 preparePanel(st, ev);
1996 handled = performPanelShortcut(st, ev.getKeyCode(), ev,
1997 Menu.FLAG_PERFORM_NO_CLOSE);
1998 st.isPrepared = false;
2007 public boolean dispatchTouchEvent(MotionEvent ev) {
2008 final Callback cb = getCallback();
2009 return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev)
2010 : super.dispatchTouchEvent(ev);
2014 public boolean dispatchTrackballEvent(MotionEvent ev) {
2015 final Callback cb = getCallback();
2016 return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTrackballEvent(ev)
2017 : super.dispatchTrackballEvent(ev);
2021 public boolean dispatchGenericMotionEvent(MotionEvent ev) {
2022 final Callback cb = getCallback();
2023 return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchGenericMotionEvent(ev)
2024 : super.dispatchGenericMotionEvent(ev);
2027 public boolean superDispatchKeyEvent(KeyEvent event) {
2028 if (super.dispatchKeyEvent(event)) {
2032 // Not handled by the view hierarchy, does the action bar want it
2033 // to cancel out of something special?
2034 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
2035 final int action = event.getAction();
2036 // Back cancels action modes first.
2037 if (mActionMode != null) {
2038 if (action == KeyEvent.ACTION_UP) {
2039 mActionMode.finish();
2044 // Next collapse any expanded action views.
2045 if (mActionBar != null && mActionBar.hasExpandedActionView()) {
2046 if (action == KeyEvent.ACTION_UP) {
2047 mActionBar.collapseActionView();
2056 public boolean superDispatchKeyShortcutEvent(KeyEvent event) {
2057 return super.dispatchKeyShortcutEvent(event);
2060 public boolean superDispatchTouchEvent(MotionEvent event) {
2061 return super.dispatchTouchEvent(event);
2064 public boolean superDispatchTrackballEvent(MotionEvent event) {
2065 return super.dispatchTrackballEvent(event);
2068 public boolean superDispatchGenericMotionEvent(MotionEvent event) {
2069 return super.dispatchGenericMotionEvent(event);
2073 public boolean onTouchEvent(MotionEvent event) {
2074 return onInterceptTouchEvent(event);
2077 private boolean isOutOfBounds(int x, int y) {
2078 return x < -5 || y < -5 || x > (getWidth() + 5)
2079 || y > (getHeight() + 5);
2083 public boolean onInterceptTouchEvent(MotionEvent event) {
2084 int action = event.getAction();
2085 if (mFeatureId >= 0) {
2086 if (action == MotionEvent.ACTION_DOWN) {
2087 int x = (int)event.getX();
2088 int y = (int)event.getY();
2089 if (isOutOfBounds(x, y)) {
2090 closePanel(mFeatureId);
2096 if (!SWEEP_OPEN_MENU) {
2100 if (mFeatureId >= 0) {
2101 if (action == MotionEvent.ACTION_DOWN) {
2102 Log.i(TAG, "Watchiing!");
2103 mWatchingForMenu = true;
2104 mDownY = (int) event.getY();
2108 if (!mWatchingForMenu) {
2112 int y = (int)event.getY();
2113 if (action == MotionEvent.ACTION_MOVE) {
2114 if (y > (mDownY+30)) {
2115 Log.i(TAG, "Closing!");
2116 closePanel(mFeatureId);
2117 mWatchingForMenu = false;
2120 } else if (action == MotionEvent.ACTION_UP) {
2121 mWatchingForMenu = false;
2127 //Log.i(TAG, "Intercept: action=" + action + " y=" + event.getY()
2128 // + " (in " + getHeight() + ")");
2130 if (action == MotionEvent.ACTION_DOWN) {
2131 int y = (int)event.getY();
2132 if (y >= (getHeight()-5) && !hasChildren()) {
2133 Log.i(TAG, "Watchiing!");
2134 mWatchingForMenu = true;
2139 if (!mWatchingForMenu) {
2143 int y = (int)event.getY();
2144 if (action == MotionEvent.ACTION_MOVE) {
2145 if (y < (getHeight()-30)) {
2146 Log.i(TAG, "Opening!");
2147 openPanel(FEATURE_OPTIONS_PANEL, new KeyEvent(
2148 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU));
2149 mWatchingForMenu = false;
2152 } else if (action == MotionEvent.ACTION_UP) {
2153 mWatchingForMenu = false;
2160 public void sendAccessibilityEvent(int eventType) {
2161 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
2165 // if we are showing a feature that should be announced and one child
2166 // make this child the event source since this is the feature itself
2167 // otherwise the callback will take over and announce its client
2168 if ((mFeatureId == FEATURE_OPTIONS_PANEL ||
2169 mFeatureId == FEATURE_CONTEXT_MENU ||
2170 mFeatureId == FEATURE_PROGRESS ||
2171 mFeatureId == FEATURE_INDETERMINATE_PROGRESS)
2172 && getChildCount() == 1) {
2173 getChildAt(0).sendAccessibilityEvent(eventType);
2175 super.sendAccessibilityEvent(eventType);
2180 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2181 final Callback cb = getCallback();
2182 if (cb != null && !isDestroyed()) {
2183 if (cb.dispatchPopulateAccessibilityEvent(event)) {
2187 return super.dispatchPopulateAccessibilityEvent(event);
2191 protected boolean setFrame(int l, int t, int r, int b) {
2192 boolean changed = super.setFrame(l, t, r, b);
2194 final Rect drawingBounds = mDrawingBounds;
2195 getDrawingRect(drawingBounds);
2197 Drawable fg = getForeground();
2199 final Rect frameOffsets = mFrameOffsets;
2200 drawingBounds.left += frameOffsets.left;
2201 drawingBounds.top += frameOffsets.top;
2202 drawingBounds.right -= frameOffsets.right;
2203 drawingBounds.bottom -= frameOffsets.bottom;
2204 fg.setBounds(drawingBounds);
2205 final Rect framePadding = mFramePadding;
2206 drawingBounds.left += framePadding.left - frameOffsets.left;
2207 drawingBounds.top += framePadding.top - frameOffsets.top;
2208 drawingBounds.right -= framePadding.right - frameOffsets.right;
2209 drawingBounds.bottom -= framePadding.bottom - frameOffsets.bottom;
2212 Drawable bg = getBackground();
2214 bg.setBounds(drawingBounds);
2217 if (SWEEP_OPEN_MENU) {
2218 if (mMenuBackground == null && mFeatureId < 0
2219 && getAttributes().height
2220 == WindowManager.LayoutParams.MATCH_PARENT) {
2221 mMenuBackground = getContext().getResources().getDrawable(
2222 com.android.internal.R.drawable.menu_background);
2224 if (mMenuBackground != null) {
2225 mMenuBackground.setBounds(drawingBounds.left,
2226 drawingBounds.bottom-6, drawingBounds.right,
2227 drawingBounds.bottom+20);
2235 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2236 final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
2237 final boolean isPortrait = metrics.widthPixels < metrics.heightPixels;
2239 final int widthMode = getMode(widthMeasureSpec);
2240 final int heightMode = getMode(heightMeasureSpec);
2242 boolean fixedWidth = false;
2243 if (widthMode == AT_MOST) {
2244 final TypedValue tvw = isPortrait ? mFixedWidthMinor : mFixedWidthMajor;
2245 if (tvw != null && tvw.type != TypedValue.TYPE_NULL) {
2247 if (tvw.type == TypedValue.TYPE_DIMENSION) {
2248 w = (int) tvw.getDimension(metrics);
2249 } else if (tvw.type == TypedValue.TYPE_FRACTION) {
2250 w = (int) tvw.getFraction(metrics.widthPixels, metrics.widthPixels);
2256 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
2257 widthMeasureSpec = MeasureSpec.makeMeasureSpec(
2258 Math.min(w, widthSize), EXACTLY);
2264 if (heightMode == AT_MOST) {
2265 final TypedValue tvh = isPortrait ? mFixedHeightMajor : mFixedHeightMinor;
2266 if (tvh != null && tvh.type != TypedValue.TYPE_NULL) {
2268 if (tvh.type == TypedValue.TYPE_DIMENSION) {
2269 h = (int) tvh.getDimension(metrics);
2270 } else if (tvh.type == TypedValue.TYPE_FRACTION) {
2271 h = (int) tvh.getFraction(metrics.heightPixels, metrics.heightPixels);
2277 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
2278 heightMeasureSpec = MeasureSpec.makeMeasureSpec(
2279 Math.min(h, heightSize), EXACTLY);
2284 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
2286 int width = getMeasuredWidth();
2287 boolean measure = false;
2289 widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY);
2291 if (!fixedWidth && widthMode == AT_MOST) {
2292 final TypedValue tv = isPortrait ? mMinWidthMinor : mMinWidthMajor;
2293 if (tv.type != TypedValue.TYPE_NULL) {
2295 if (tv.type == TypedValue.TYPE_DIMENSION) {
2296 min = (int)tv.getDimension(metrics);
2297 } else if (tv.type == TypedValue.TYPE_FRACTION) {
2298 min = (int)tv.getFraction(metrics.widthPixels, metrics.widthPixels);
2304 widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY);
2310 // TODO: Support height?
2313 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
2318 public void draw(Canvas canvas) {
2321 if (mMenuBackground != null) {
2322 mMenuBackground.draw(canvas);
2328 public boolean showContextMenuForChild(View originalView) {
2329 // Reuse the context menu builder
2330 if (mContextMenu == null) {
2331 mContextMenu = new ContextMenuBuilder(getContext());
2332 mContextMenu.setCallback(mContextMenuCallback);
2334 mContextMenu.clearAll();
2337 final MenuDialogHelper helper = mContextMenu.show(originalView,
2338 originalView.getWindowToken());
2339 if (helper != null) {
2340 helper.setPresenterCallback(mContextMenuCallback);
2342 mContextMenuHelper = helper;
2343 return helper != null;
2347 public ActionMode startActionModeForChild(View originalView,
2348 ActionMode.Callback callback) {
2349 // originalView can be used here to be sure that we don't obscure
2350 // relevant content with the context mode UI.
2351 return startActionMode(callback);
2355 public ActionMode startActionMode(ActionMode.Callback callback) {
2356 if (mActionMode != null) {
2357 mActionMode.finish();
2360 final ActionMode.Callback wrappedCallback = new ActionModeCallbackWrapper(callback);
2361 ActionMode mode = null;
2362 if (getCallback() != null && !isDestroyed()) {
2364 mode = getCallback().onWindowStartingActionMode(wrappedCallback);
2365 } catch (AbstractMethodError ame) {
2366 // Older apps might not implement this callback method.
2372 if (mActionModeView == null) {
2374 mActionModeView = new ActionBarContextView(mContext);
2375 mActionModePopup = new PopupWindow(mContext, null,
2376 com.android.internal.R.attr.actionModePopupWindowStyle);
2377 mActionModePopup.setWindowLayoutType(
2378 WindowManager.LayoutParams.TYPE_APPLICATION);
2379 mActionModePopup.setContentView(mActionModeView);
2380 mActionModePopup.setWidth(MATCH_PARENT);
2382 TypedValue heightValue = new TypedValue();
2383 mContext.getTheme().resolveAttribute(
2384 com.android.internal.R.attr.actionBarSize, heightValue, true);
2385 final int height = TypedValue.complexToDimensionPixelSize(heightValue.data,
2386 mContext.getResources().getDisplayMetrics());
2387 mActionModeView.setContentHeight(height);
2388 mActionModePopup.setHeight(WRAP_CONTENT);
2389 mShowActionModePopup = new Runnable() {
2391 mActionModePopup.showAtLocation(
2392 mActionModeView.getApplicationWindowToken(),
2393 Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
2397 ViewStub stub = (ViewStub) findViewById(
2398 com.android.internal.R.id.action_mode_bar_stub);
2400 mActionModeView = (ActionBarContextView) stub.inflate();
2405 if (mActionModeView != null) {
2406 mActionModeView.killMode();
2407 mode = new StandaloneActionMode(getContext(), mActionModeView, wrappedCallback,
2408 mActionModePopup == null);
2409 if (callback.onCreateActionMode(mode, mode.getMenu())) {
2411 mActionModeView.initForMode(mode);
2412 mActionModeView.setVisibility(View.VISIBLE);
2414 if (mActionModePopup != null) {
2415 post(mShowActionModePopup);
2417 mActionModeView.sendAccessibilityEvent(
2418 AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2424 if (mActionMode != null && getCallback() != null && !isDestroyed()) {
2426 getCallback().onActionModeStarted(mActionMode);
2427 } catch (AbstractMethodError ame) {
2428 // Older apps might not implement this callback method.
2434 public void startChanging() {
2438 public void finishChanging() {
2443 public void setWindowBackground(Drawable drawable) {
2444 if (getBackground() != drawable) {
2445 setBackgroundDrawable(drawable);
2446 if (drawable != null) {
2447 drawable.getPadding(mBackgroundPadding);
2449 mBackgroundPadding.setEmpty();
2456 public void setBackgroundDrawable(Drawable d) {
2457 super.setBackgroundDrawable(d);
2458 if (getWindowToken() != null) {
2459 updateWindowResizeState();
2463 public void setWindowFrame(Drawable drawable) {
2464 if (getForeground() != drawable) {
2465 setForeground(drawable);
2466 if (drawable != null) {
2467 drawable.getPadding(mFramePadding);
2469 mFramePadding.setEmpty();
2476 protected boolean fitSystemWindows(Rect insets) {
2477 mFrameOffsets.set(insets);
2478 if (getForeground() != null) {
2481 return super.fitSystemWindows(insets);
2484 private void drawableChanged() {
2489 setPadding(mFramePadding.left + mBackgroundPadding.left, mFramePadding.top
2490 + mBackgroundPadding.top, mFramePadding.right + mBackgroundPadding.right,
2491 mFramePadding.bottom + mBackgroundPadding.bottom);
2495 int opacity = PixelFormat.OPAQUE;
2497 // Note: if there is no background, we will assume opaque. The
2498 // common case seems to be that an application sets there to be
2499 // no background so it can draw everything itself. For that,
2500 // we would like to assume OPAQUE and let the app force it to
2501 // the slower TRANSLUCENT mode if that is really what it wants.
2502 Drawable bg = getBackground();
2503 Drawable fg = getForeground();
2506 opacity = bg.getOpacity();
2507 } else if (mFramePadding.left <= 0 && mFramePadding.top <= 0
2508 && mFramePadding.right <= 0 && mFramePadding.bottom <= 0) {
2509 // If the frame padding is zero, then we can be opaque
2510 // if either the frame -or- the background is opaque.
2511 int fop = fg.getOpacity();
2512 int bop = bg.getOpacity();
2514 Log.v(TAG, "Background opacity: " + bop + ", Frame opacity: " + fop);
2515 if (fop == PixelFormat.OPAQUE || bop == PixelFormat.OPAQUE) {
2516 opacity = PixelFormat.OPAQUE;
2517 } else if (fop == PixelFormat.UNKNOWN) {
2519 } else if (bop == PixelFormat.UNKNOWN) {
2522 opacity = Drawable.resolveOpacity(fop, bop);
2525 // For now we have to assume translucent if there is a
2526 // frame with padding... there is no way to tell if the
2527 // frame and background together will draw all pixels.
2529 Log.v(TAG, "Padding: " + mFramePadding);
2530 opacity = PixelFormat.TRANSLUCENT;
2535 Log.v(TAG, "Background: " + bg + ", Frame: " + fg);
2537 Log.v(TAG, "Selected default opacity: " + opacity);
2539 mDefaultOpacity = opacity;
2540 if (mFeatureId < 0) {
2541 setDefaultWindowFormat(opacity);
2546 public void onWindowFocusChanged(boolean hasWindowFocus) {
2547 super.onWindowFocusChanged(hasWindowFocus);
2549 // If the user is chording a menu shortcut, release the chord since
2550 // this window lost focus
2551 if (!hasWindowFocus && mPanelChordingKey != 0) {
2552 closePanel(FEATURE_OPTIONS_PANEL);
2555 final Callback cb = getCallback();
2556 if (cb != null && !isDestroyed() && mFeatureId < 0) {
2557 cb.onWindowFocusChanged(hasWindowFocus);
2561 void updateWindowResizeState() {
2562 Drawable bg = getBackground();
2563 hackTurnOffWindowResizeAnim(bg == null || bg.getOpacity()
2564 != PixelFormat.OPAQUE);
2568 protected void onAttachedToWindow() {
2569 super.onAttachedToWindow();
2571 updateWindowResizeState();
2573 final Callback cb = getCallback();
2574 if (cb != null && !isDestroyed() && mFeatureId < 0) {
2575 cb.onAttachedToWindow();
2578 if (mFeatureId == -1) {
2580 * The main window has been attached, try to restore any panels
2581 * that may have been open before. This is called in cases where
2582 * an activity is being killed for configuration change and the
2583 * menu was open. When the activity is recreated, the menu
2584 * should be shown again.
2586 openPanelsAfterRestore();
2591 protected void onDetachedFromWindow() {
2592 super.onDetachedFromWindow();
2594 final Callback cb = getCallback();
2595 if (cb != null && mFeatureId < 0) {
2596 cb.onDetachedFromWindow();
2599 if (mActionBar != null) {
2600 mActionBar.dismissPopupMenus();
2603 if (mActionModePopup != null) {
2604 removeCallbacks(mShowActionModePopup);
2605 if (mActionModePopup.isShowing()) {
2606 mActionModePopup.dismiss();
2608 mActionModePopup = null;
2611 PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false);
2612 if (st != null && st.menu != null && mFeatureId < 0) {
2618 public void onCloseSystemDialogs(String reason) {
2619 if (mFeatureId >= 0) {
2624 public android.view.SurfaceHolder.Callback2 willYouTakeTheSurface() {
2625 return mFeatureId < 0 ? mTakeSurfaceCallback : null;
2628 public InputQueue.Callback willYouTakeTheInputQueue() {
2629 return mFeatureId < 0 ? mTakeInputQueueCallback : null;
2632 public void setSurfaceType(int type) {
2633 PhoneWindow.this.setType(type);
2636 public void setSurfaceFormat(int format) {
2637 PhoneWindow.this.setFormat(format);
2640 public void setSurfaceKeepScreenOn(boolean keepOn) {
2641 if (keepOn) PhoneWindow.this.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2642 else PhoneWindow.this.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
2646 * Clears out internal reference when the action mode is destroyed.
2648 private class ActionModeCallbackWrapper implements ActionMode.Callback {
2649 private ActionMode.Callback mWrapped;
2651 public ActionModeCallbackWrapper(ActionMode.Callback wrapped) {
2655 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
2656 return mWrapped.onCreateActionMode(mode, menu);
2659 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
2660 return mWrapped.onPrepareActionMode(mode, menu);
2663 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
2664 return mWrapped.onActionItemClicked(mode, item);
2667 public void onDestroyActionMode(ActionMode mode) {
2668 mWrapped.onDestroyActionMode(mode);
2669 if (mActionModePopup != null) {
2670 removeCallbacks(mShowActionModePopup);
2671 mActionModePopup.dismiss();
2672 } else if (mActionModeView != null) {
2673 mActionModeView.setVisibility(GONE);
2675 if (mActionModeView != null) {
2676 mActionModeView.removeAllViews();
2678 if (getCallback() != null && !isDestroyed()) {
2680 getCallback().onActionModeFinished(mActionMode);
2681 } catch (AbstractMethodError ame) {
2682 // Older apps might not implement this callback method.
2690 protected DecorView generateDecor() {
2691 return new DecorView(getContext(), -1);
2694 protected void setFeatureFromAttrs(int featureId, TypedArray attrs,
2695 int drawableAttr, int alphaAttr) {
2696 Drawable d = attrs.getDrawable(drawableAttr);
2698 requestFeature(featureId);
2699 setFeatureDefaultDrawable(featureId, d);
2701 if ((getFeatures() & (1 << featureId)) != 0) {
2702 int alpha = attrs.getInt(alphaAttr, -1);
2704 setFeatureDrawableAlpha(featureId, alpha);
2709 protected ViewGroup generateLayout(DecorView decor) {
2710 // Apply data from current theme.
2712 TypedArray a = getWindowStyle();
2715 System.out.println("From style:");
2716 String s = "Attrs:";
2717 for (int i = 0; i < com.android.internal.R.styleable.Window.length; i++) {
2718 s = s + " " + Integer.toHexString(com.android.internal.R.styleable.Window[i]) + "="
2721 System.out.println(s);
2724 mIsFloating = a.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating, false);
2725 int flagsToUpdate = (FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR)
2726 & (~getForcedWindowFlags());
2728 setLayout(WRAP_CONTENT, WRAP_CONTENT);
2729 setFlags(0, flagsToUpdate);
2731 setFlags(FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR, flagsToUpdate);
2734 if (a.getBoolean(com.android.internal.R.styleable.Window_windowNoTitle, false)) {
2735 requestFeature(FEATURE_NO_TITLE);
2736 } else if (a.getBoolean(com.android.internal.R.styleable.Window_windowActionBar, false)) {
2737 // Don't allow an action bar if there is no title.
2738 requestFeature(FEATURE_ACTION_BAR);
2741 if (a.getBoolean(com.android.internal.R.styleable.Window_windowActionBarOverlay, false)) {
2742 requestFeature(FEATURE_ACTION_BAR_OVERLAY);
2745 if (a.getBoolean(com.android.internal.R.styleable.Window_windowActionModeOverlay, false)) {
2746 requestFeature(FEATURE_ACTION_MODE_OVERLAY);
2749 if (a.getBoolean(com.android.internal.R.styleable.Window_windowFullscreen, false)) {
2750 setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN & (~getForcedWindowFlags()));
2753 if (a.getBoolean(com.android.internal.R.styleable.Window_windowTranslucentStatus,
2755 setFlags(FLAG_TRANSLUCENT_STATUS, FLAG_TRANSLUCENT_STATUS
2756 & (~getForcedWindowFlags()));
2759 if (a.getBoolean(com.android.internal.R.styleable.Window_windowTranslucentNavigation,
2761 setFlags(FLAG_TRANSLUCENT_NAVIGATION, FLAG_TRANSLUCENT_NAVIGATION
2762 & (~getForcedWindowFlags()));
2765 if (a.getBoolean(com.android.internal.R.styleable.Window_windowOverscan, false)) {
2766 setFlags(FLAG_LAYOUT_IN_OVERSCAN, FLAG_LAYOUT_IN_OVERSCAN&(~getForcedWindowFlags()));
2769 if (a.getBoolean(com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
2770 setFlags(FLAG_SHOW_WALLPAPER, FLAG_SHOW_WALLPAPER&(~getForcedWindowFlags()));
2773 if (a.getBoolean(com.android.internal.R.styleable.Window_windowEnableSplitTouch,
2774 getContext().getApplicationInfo().targetSdkVersion
2775 >= android.os.Build.VERSION_CODES.HONEYCOMB)) {
2776 setFlags(FLAG_SPLIT_TOUCH, FLAG_SPLIT_TOUCH&(~getForcedWindowFlags()));
2779 a.getValue(com.android.internal.R.styleable.Window_windowMinWidthMajor, mMinWidthMajor);
2780 a.getValue(com.android.internal.R.styleable.Window_windowMinWidthMinor, mMinWidthMinor);
2781 if (a.hasValue(com.android.internal.R.styleable.Window_windowFixedWidthMajor)) {
2782 if (mFixedWidthMajor == null) mFixedWidthMajor = new TypedValue();
2783 a.getValue(com.android.internal.R.styleable.Window_windowFixedWidthMajor,
2786 if (a.hasValue(com.android.internal.R.styleable.Window_windowFixedWidthMinor)) {
2787 if (mFixedWidthMinor == null) mFixedWidthMinor = new TypedValue();
2788 a.getValue(com.android.internal.R.styleable.Window_windowFixedWidthMinor,
2791 if (a.hasValue(com.android.internal.R.styleable.Window_windowFixedHeightMajor)) {
2792 if (mFixedHeightMajor == null) mFixedHeightMajor = new TypedValue();
2793 a.getValue(com.android.internal.R.styleable.Window_windowFixedHeightMajor,
2796 if (a.hasValue(com.android.internal.R.styleable.Window_windowFixedHeightMinor)) {
2797 if (mFixedHeightMinor == null) mFixedHeightMinor = new TypedValue();
2798 a.getValue(com.android.internal.R.styleable.Window_windowFixedHeightMinor,
2802 final Context context = getContext();
2803 final int targetSdk = context.getApplicationInfo().targetSdkVersion;
2804 final boolean targetPreHoneycomb = targetSdk < android.os.Build.VERSION_CODES.HONEYCOMB;
2805 final boolean targetPreIcs = targetSdk < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
2806 final boolean targetHcNeedsOptions = context.getResources().getBoolean(
2807 com.android.internal.R.bool.target_honeycomb_needs_options_menu);
2808 final boolean noActionBar = !hasFeature(FEATURE_ACTION_BAR) || hasFeature(FEATURE_NO_TITLE);
2810 if (targetPreHoneycomb || (targetPreIcs && targetHcNeedsOptions && noActionBar)) {
2811 addFlags(WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY);
2813 clearFlags(WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY);
2816 if (mAlwaysReadCloseOnTouchAttr || getContext().getApplicationInfo().targetSdkVersion
2817 >= android.os.Build.VERSION_CODES.HONEYCOMB) {
2819 com.android.internal.R.styleable.Window_windowCloseOnTouchOutside,
2821 setCloseOnTouchOutsideIfNotSet(true);
2825 WindowManager.LayoutParams params = getAttributes();
2827 if (!hasSoftInputMode()) {
2828 params.softInputMode = a.getInt(
2829 com.android.internal.R.styleable.Window_windowSoftInputMode,
2830 params.softInputMode);
2833 if (a.getBoolean(com.android.internal.R.styleable.Window_backgroundDimEnabled,
2835 /* All dialogs should have the window dimmed */
2836 if ((getForcedWindowFlags()&WindowManager.LayoutParams.FLAG_DIM_BEHIND) == 0) {
2837 params.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
2839 if (!haveDimAmount()) {
2840 params.dimAmount = a.getFloat(
2841 android.R.styleable.Window_backgroundDimAmount, 0.5f);
2845 if (params.windowAnimations == 0) {
2846 params.windowAnimations = a.getResourceId(
2847 com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
2850 // The rest are only done if this window is not embedded; otherwise,
2851 // the values are inherited from our container.
2852 if (getContainer() == null) {
2853 if (mBackgroundDrawable == null) {
2854 if (mBackgroundResource == 0) {
2855 mBackgroundResource = a.getResourceId(
2856 com.android.internal.R.styleable.Window_windowBackground, 0);
2858 if (mFrameResource == 0) {
2859 mFrameResource = a.getResourceId(com.android.internal.R.styleable.Window_windowFrame, 0);
2862 System.out.println("Background: "
2863 + Integer.toHexString(mBackgroundResource) + " Frame: "
2864 + Integer.toHexString(mFrameResource));
2867 mTextColor = a.getColor(com.android.internal.R.styleable.Window_textColor, 0xFF000000);
2870 // Inflate the window decor.
2873 int features = getLocalFeatures();
2874 // System.out.println("Features: 0x" + Integer.toHexString(features));
2875 if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) {
2877 TypedValue res = new TypedValue();
2878 getContext().getTheme().resolveAttribute(
2879 com.android.internal.R.attr.dialogTitleIconsDecorLayout, res, true);
2880 layoutResource = res.resourceId;
2882 layoutResource = com.android.internal.R.layout.screen_title_icons;
2884 // XXX Remove this once action bar supports these features.
2885 removeFeature(FEATURE_ACTION_BAR);
2886 // System.out.println("Title Icons!");
2887 } else if ((features & ((1 << FEATURE_PROGRESS) | (1 << FEATURE_INDETERMINATE_PROGRESS))) != 0
2888 && (features & (1 << FEATURE_ACTION_BAR)) == 0) {
2889 // Special case for a window with only a progress bar (and title).
2890 // XXX Need to have a no-title version of embedded windows.
2891 layoutResource = com.android.internal.R.layout.screen_progress;
2892 // System.out.println("Progress!");
2893 } else if ((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) {
2894 // Special case for a window with a custom title.
2895 // If the window is floating, we need a dialog layout
2897 TypedValue res = new TypedValue();
2898 getContext().getTheme().resolveAttribute(
2899 com.android.internal.R.attr.dialogCustomTitleDecorLayout, res, true);
2900 layoutResource = res.resourceId;
2902 layoutResource = com.android.internal.R.layout.screen_custom_title;
2904 // XXX Remove this once action bar supports these features.
2905 removeFeature(FEATURE_ACTION_BAR);
2906 } else if ((features & (1 << FEATURE_NO_TITLE)) == 0) {
2907 // If no other features and not embedded, only need a title.
2908 // If the window is floating, we need a dialog layout
2910 TypedValue res = new TypedValue();
2911 getContext().getTheme().resolveAttribute(
2912 com.android.internal.R.attr.dialogTitleDecorLayout, res, true);
2913 layoutResource = res.resourceId;
2914 } else if ((features & (1 << FEATURE_ACTION_BAR)) != 0) {
2915 layoutResource = com.android.internal.R.layout.screen_action_bar;
2917 layoutResource = com.android.internal.R.layout.screen_title;
2919 // System.out.println("Title!");
2920 } else if ((features & (1 << FEATURE_ACTION_MODE_OVERLAY)) != 0) {
2921 layoutResource = com.android.internal.R.layout.screen_simple_overlay_action_mode;
2923 // Embedded, so no decoration is needed.
2924 layoutResource = com.android.internal.R.layout.screen_simple;
2925 // System.out.println("Simple!");
2928 mDecor.startChanging();
2930 View in = mLayoutInflater.inflate(layoutResource, null);
2931 decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
2933 ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
2934 if (contentParent == null) {
2935 throw new RuntimeException("Window couldn't find content container view");
2938 if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
2939 ProgressBar progress = getCircularProgressBar(false);
2940 if (progress != null) {
2941 progress.setIndeterminate(true);
2945 // Remaining setup -- of background and title -- that only applies
2946 // to top-level windows.
2947 if (getContainer() == null) {
2948 Drawable drawable = mBackgroundDrawable;
2949 if (mBackgroundResource != 0) {
2950 drawable = getContext().getResources().getDrawable(mBackgroundResource);
2952 mDecor.setWindowBackground(drawable);
2954 if (mFrameResource != 0) {
2955 drawable = getContext().getResources().getDrawable(mFrameResource);
2957 mDecor.setWindowFrame(drawable);
2959 // System.out.println("Text=" + Integer.toHexString(mTextColor) +
2960 // " Sel=" + Integer.toHexString(mTextSelectedColor) +
2961 // " Title=" + Integer.toHexString(mTitleColor));
2963 if (mTitleColor == 0) {
2964 mTitleColor = mTextColor;
2967 if (mTitle != null) {
2970 setTitleColor(mTitleColor);
2973 mDecor.finishChanging();
2975 return contentParent;
2979 public void alwaysReadCloseOnTouchAttr() {
2980 mAlwaysReadCloseOnTouchAttr = true;
2983 private void installDecor() {
2984 if (mDecor == null) {
2985 mDecor = generateDecor();
2986 mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
2987 mDecor.setIsRootNamespace(true);
2988 if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
2989 mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
2992 if (mContentParent == null) {
2993 mContentParent = generateLayout(mDecor);
2995 // Set up decor part of UI to ignore fitsSystemWindows if appropriate.
2996 mDecor.makeOptionalFitsSystemWindows();
2998 mTitleView = (TextView)findViewById(com.android.internal.R.id.title);
2999 if (mTitleView != null) {
3000 mTitleView.setLayoutDirection(mDecor.getLayoutDirection());
3001 if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) {
3002 View titleContainer = findViewById(com.android.internal.R.id.title_container);
3003 if (titleContainer != null) {
3004 titleContainer.setVisibility(View.GONE);
3006 mTitleView.setVisibility(View.GONE);
3008 if (mContentParent instanceof FrameLayout) {
3009 ((FrameLayout)mContentParent).setForeground(null);
3012 mTitleView.setText(mTitle);
3015 mActionBar = (ActionBarView) findViewById(com.android.internal.R.id.action_bar);
3016 if (mActionBar != null) {
3017 mActionBar.setWindowCallback(getCallback());
3018 if (mActionBar.getTitle() == null) {
3019 mActionBar.setWindowTitle(mTitle);
3021 final int localFeatures = getLocalFeatures();
3022 if ((localFeatures & (1 << FEATURE_PROGRESS)) != 0) {
3023 mActionBar.initProgress();
3025 if ((localFeatures & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
3026 mActionBar.initIndeterminateProgress();
3029 final ActionBarOverlayLayout abol = (ActionBarOverlayLayout) findViewById(
3030 com.android.internal.R.id.action_bar_overlay_layout);
3032 abol.setOverlayMode(
3033 (localFeatures & (1 << FEATURE_ACTION_BAR_OVERLAY)) != 0);
3036 boolean splitActionBar = false;
3037 final boolean splitWhenNarrow =
3038 (mUiOptions & ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW) != 0;
3039 if (splitWhenNarrow) {
3040 splitActionBar = getContext().getResources().getBoolean(
3041 com.android.internal.R.bool.split_action_bar_is_narrow);
3043 splitActionBar = getWindowStyle().getBoolean(
3044 com.android.internal.R.styleable.Window_windowSplitActionBar, false);
3046 final ActionBarContainer splitView = (ActionBarContainer) findViewById(
3047 com.android.internal.R.id.split_action_bar);
3048 if (splitView != null) {
3049 mActionBar.setSplitView(splitView);
3050 mActionBar.setSplitActionBar(splitActionBar);
3051 mActionBar.setSplitWhenNarrow(splitWhenNarrow);
3053 final ActionBarContextView cab = (ActionBarContextView) findViewById(
3054 com.android.internal.R.id.action_context_bar);
3055 cab.setSplitView(splitView);
3056 cab.setSplitActionBar(splitActionBar);
3057 cab.setSplitWhenNarrow(splitWhenNarrow);
3058 } else if (splitActionBar) {
3059 Log.e(TAG, "Requested split action bar with " +
3060 "incompatible window decor! Ignoring request.");
3063 if ((mResourcesSetFlags & FLAG_RESOURCE_SET_ICON) != 0 ||
3064 (mIconRes != 0 && !mActionBar.hasIcon())) {
3065 mActionBar.setIcon(mIconRes);
3066 } else if ((mResourcesSetFlags & FLAG_RESOURCE_SET_ICON) == 0 &&
3067 mIconRes == 0 && !mActionBar.hasIcon()) {
3069 getContext().getPackageManager().getDefaultActivityIcon());
3070 mResourcesSetFlags |= FLAG_RESOURCE_SET_ICON_FALLBACK;
3072 if ((mResourcesSetFlags & FLAG_RESOURCE_SET_LOGO) != 0 ||
3073 (mLogoRes != 0 && !mActionBar.hasLogo())) {
3074 mActionBar.setLogo(mLogoRes);
3077 // Post the panel invalidate for later; avoid application onCreateOptionsMenu
3078 // being called in the middle of onCreate or similar.
3079 mDecor.post(new Runnable() {
3081 // Invalidate if the panel menu hasn't been created before this.
3082 PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false);
3083 if (!isDestroyed() && (st == null || st.menu == null)) {
3084 invalidatePanelMenu(FEATURE_ACTION_BAR);
3093 private Drawable loadImageURI(Uri uri) {
3095 return Drawable.createFromStream(
3096 getContext().getContentResolver().openInputStream(uri), null);
3097 } catch (Exception e) {
3098 Log.w(TAG, "Unable to open content: " + uri);
3103 private DrawableFeatureState getDrawableState(int featureId, boolean required) {
3104 if ((getFeatures() & (1 << featureId)) == 0) {
3108 throw new RuntimeException("The feature has not been requested");
3111 DrawableFeatureState[] ar;
3112 if ((ar = mDrawables) == null || ar.length <= featureId) {
3113 DrawableFeatureState[] nar = new DrawableFeatureState[featureId + 1];
3115 System.arraycopy(ar, 0, nar, 0, ar.length);
3117 mDrawables = ar = nar;
3120 DrawableFeatureState st = ar[featureId];
3122 ar[featureId] = st = new DrawableFeatureState(featureId);
3128 * Gets a panel's state based on its feature ID.
3130 * @param featureId The feature ID of the panel.
3131 * @param required Whether the panel is required (if it is required and it
3132 * isn't in our features, this throws an exception).
3133 * @return The panel state.
3135 private PanelFeatureState getPanelState(int featureId, boolean required) {
3136 return getPanelState(featureId, required, null);
3140 * Gets a panel's state based on its feature ID.
3142 * @param featureId The feature ID of the panel.
3143 * @param required Whether the panel is required (if it is required and it
3144 * isn't in our features, this throws an exception).
3145 * @param convertPanelState Optional: If the panel state does not exist, use
3146 * this as the panel state.
3147 * @return The panel state.
3149 private PanelFeatureState getPanelState(int featureId, boolean required,
3150 PanelFeatureState convertPanelState) {
3151 if ((getFeatures() & (1 << featureId)) == 0) {
3155 throw new RuntimeException("The feature has not been requested");
3158 PanelFeatureState[] ar;
3159 if ((ar = mPanels) == null || ar.length <= featureId) {
3160 PanelFeatureState[] nar = new PanelFeatureState[featureId + 1];
3162 System.arraycopy(ar, 0, nar, 0, ar.length);
3167 PanelFeatureState st = ar[featureId];
3169 ar[featureId] = st = (convertPanelState != null)
3171 : new PanelFeatureState(featureId);
3177 public final void setChildDrawable(int featureId, Drawable drawable) {
3178 DrawableFeatureState st = getDrawableState(featureId, true);
3179 st.child = drawable;
3180 updateDrawable(featureId, st, false);
3184 public final void setChildInt(int featureId, int value) {
3185 updateInt(featureId, value, false);
3189 public boolean isShortcutKey(int keyCode, KeyEvent event) {
3190 PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true);
3191 return st.menu != null && st.menu.isShortcutKey(keyCode, event);
3194 private void updateDrawable(int featureId, DrawableFeatureState st, boolean fromResume) {
3195 // Do nothing if the decor is not yet installed... an update will
3196 // need to be forced when we eventually become active.
3197 if (mContentParent == null) {
3201 final int featureMask = 1 << featureId;
3203 if ((getFeatures() & featureMask) == 0 && !fromResume) {
3207 Drawable drawable = null;
3209 drawable = st.child;
3210 if (drawable == null)
3211 drawable = st.local;
3212 if (drawable == null)
3215 if ((getLocalFeatures() & featureMask) == 0) {
3216 if (getContainer() != null) {
3217 if (isActive() || fromResume) {
3218 getContainer().setChildDrawable(featureId, drawable);
3221 } else if (st != null && (st.cur != drawable || st.curAlpha != st.alpha)) {
3222 // System.out.println("Drawable changed: old=" + st.cur
3223 // + ", new=" + drawable);
3225 st.curAlpha = st.alpha;
3226 onDrawableChanged(featureId, drawable, st.alpha);
3230 private void updateInt(int featureId, int value, boolean fromResume) {
3232 // Do nothing if the decor is not yet installed... an update will
3233 // need to be forced when we eventually become active.
3234 if (mContentParent == null) {
3238 final int featureMask = 1 << featureId;
3240 if ((getFeatures() & featureMask) == 0 && !fromResume) {
3244 if ((getLocalFeatures() & featureMask) == 0) {
3245 if (getContainer() != null) {
3246 getContainer().setChildInt(featureId, value);
3249 onIntChanged(featureId, value);
3253 private ImageView getLeftIconView() {
3254 if (mLeftIconView != null) {
3255 return mLeftIconView;
3257 if (mContentParent == null) {
3260 return (mLeftIconView = (ImageView)findViewById(com.android.internal.R.id.left_icon));
3263 private ProgressBar getCircularProgressBar(boolean shouldInstallDecor) {
3264 if (mCircularProgressBar != null) {
3265 return mCircularProgressBar;
3267 if (mContentParent == null && shouldInstallDecor) {
3270 mCircularProgressBar = (ProgressBar) findViewById(com.android.internal.R.id.progress_circular);
3271 if (mCircularProgressBar != null) {
3272 mCircularProgressBar.setVisibility(View.INVISIBLE);
3274 return mCircularProgressBar;
3277 private ProgressBar getHorizontalProgressBar(boolean shouldInstallDecor) {
3278 if (mHorizontalProgressBar != null) {
3279 return mHorizontalProgressBar;
3281 if (mContentParent == null && shouldInstallDecor) {
3284 mHorizontalProgressBar = (ProgressBar) findViewById(com.android.internal.R.id.progress_horizontal);
3285 if (mHorizontalProgressBar != null) {
3286 mHorizontalProgressBar.setVisibility(View.INVISIBLE);
3288 return mHorizontalProgressBar;
3291 private ImageView getRightIconView() {
3292 if (mRightIconView != null) {
3293 return mRightIconView;
3295 if (mContentParent == null) {
3298 return (mRightIconView = (ImageView)findViewById(com.android.internal.R.id.right_icon));
3302 * Helper method for calling the {@link Callback#onPanelClosed(int, Menu)}
3303 * callback. This method will grab whatever extra state is needed for the
3304 * callback that isn't given in the parameters. If the panel is not open,
3305 * this will not perform the callback.
3307 * @param featureId Feature ID of the panel that was closed. Must be given.
3308 * @param panel Panel that was closed. Optional but useful if there is no
3310 * @param menu The menu that was closed. Optional, but give if you have.
3312 private void callOnPanelClosed(int featureId, PanelFeatureState panel, Menu menu) {
3313 final Callback cb = getCallback();
3317 // Try to get a menu
3319 // Need a panel to grab the menu, so try to get that
3320 if (panel == null) {
3321 if ((featureId >= 0) && (featureId < mPanels.length)) {
3322 panel = mPanels[featureId];
3326 if (panel != null) {
3327 // menu still may be null, which is okay--we tried our best
3332 // If the panel is not open, do not callback
3333 if ((panel != null) && (!panel.isOpen))
3336 if (!isDestroyed()) {
3337 cb.onPanelClosed(featureId, menu);
3342 * Helper method for adding launch-search to most applications. Opens the
3343 * search window using default settings.
3345 * @return true if search window opened
3347 private boolean launchDefaultSearch() {
3348 final Callback cb = getCallback();
3349 if (cb == null || isDestroyed()) {
3352 sendCloseSystemWindows("search");
3353 return cb.onSearchRequested();
3358 public void setVolumeControlStream(int streamType) {
3359 mVolumeControlStreamType = streamType;
3363 public int getVolumeControlStream() {
3364 return mVolumeControlStreamType;
3367 private static final class DrawableFeatureState {
3368 DrawableFeatureState(int _featureId) {
3369 featureId = _featureId;
3372 final int featureId;
3391 private static final class PanelFeatureState {
3393 /** Feature ID for this panel. */
3396 // Information pulled from the style for this panel.
3400 /** The background when the panel spans the entire available width. */
3409 int windowAnimations;
3411 /** Dynamic state of the panel. */
3412 DecorView decorView;
3414 /** The panel that was returned by onCreatePanelView(). */
3415 View createdPanelView;
3417 /** The panel that we are actually showing. */
3418 View shownPanelView;
3420 /** Use {@link #setMenu} to set this. */
3423 IconMenuPresenter iconMenuPresenter;
3424 ListMenuPresenter listMenuPresenter;
3426 /** true if this menu will show in single-list compact mode */
3429 /** Theme resource ID for list elements of the panel menu */
3430 int listPresenterTheme;
3433 * Whether the panel has been prepared (see
3434 * {@link PhoneWindow#preparePanel}).
3439 * Whether an item's action has been performed. This happens in obvious
3440 * scenarios (user clicks on menu item), but can also happen with
3441 * chording menu+(shortcut key).
3448 * True if the menu is in expanded mode, false if the menu is in icon
3451 boolean isInExpandedMode;
3453 public boolean qwertyMode;
3455 boolean refreshDecorView;
3457 boolean refreshMenuContent;
3459 boolean wasLastOpen;
3461 boolean wasLastExpanded;
3464 * Contains the state of the menu when told to freeze.
3466 Bundle frozenMenuState;
3469 * Contains the state of associated action views when told to freeze.
3470 * These are saved across invalidations.
3472 Bundle frozenActionViewState;
3474 PanelFeatureState(int featureId) {
3475 this.featureId = featureId;
3477 refreshDecorView = false;
3480 public boolean isInListMode() {
3481 return isInExpandedMode || isCompact;
3484 public boolean hasPanelItems() {
3485 if (shownPanelView == null) return false;
3486 if (createdPanelView != null) return true;
3488 if (isCompact || isInExpandedMode) {
3489 return listMenuPresenter.getAdapter().getCount() > 0;
3491 return ((ViewGroup) shownPanelView).getChildCount() > 0;
3496 * Unregister and free attached MenuPresenters. They will be recreated as needed.
3498 public void clearMenuPresenters() {
3500 menu.removeMenuPresenter(iconMenuPresenter);
3501 menu.removeMenuPresenter(listMenuPresenter);
3503 iconMenuPresenter = null;
3504 listMenuPresenter = null;
3507 void setStyle(Context context) {
3508 TypedArray a = context.obtainStyledAttributes(com.android.internal.R.styleable.Theme);
3509 background = a.getResourceId(
3510 com.android.internal.R.styleable.Theme_panelBackground, 0);
3511 fullBackground = a.getResourceId(
3512 com.android.internal.R.styleable.Theme_panelFullBackground, 0);
3513 windowAnimations = a.getResourceId(
3514 com.android.internal.R.styleable.Theme_windowAnimationStyle, 0);
3515 isCompact = a.getBoolean(
3516 com.android.internal.R.styleable.Theme_panelMenuIsCompact, false);
3517 listPresenterTheme = a.getResourceId(
3518 com.android.internal.R.styleable.Theme_panelMenuListTheme,
3519 com.android.internal.R.style.Theme_ExpandedMenu);
3523 void setMenu(MenuBuilder menu) {
3524 if (menu == this.menu) return;
3526 if (this.menu != null) {
3527 this.menu.removeMenuPresenter(iconMenuPresenter);
3528 this.menu.removeMenuPresenter(listMenuPresenter);
3532 if (iconMenuPresenter != null) menu.addMenuPresenter(iconMenuPresenter);
3533 if (listMenuPresenter != null) menu.addMenuPresenter(listMenuPresenter);
3537 MenuView getListMenuView(Context context, MenuPresenter.Callback cb) {
3538 if (menu == null) return null;
3541 getIconMenuView(context, cb); // Need this initialized to know where our offset goes
3544 if (listMenuPresenter == null) {
3545 listMenuPresenter = new ListMenuPresenter(
3546 com.android.internal.R.layout.list_menu_item_layout, listPresenterTheme);
3547 listMenuPresenter.setCallback(cb);
3548 listMenuPresenter.setId(com.android.internal.R.id.list_menu_presenter);
3549 menu.addMenuPresenter(listMenuPresenter);
3552 if (iconMenuPresenter != null) {
3553 listMenuPresenter.setItemIndexOffset(
3554 iconMenuPresenter.getNumActualItemsShown());
3556 MenuView result = listMenuPresenter.getMenuView(decorView);
3561 MenuView getIconMenuView(Context context, MenuPresenter.Callback cb) {
3562 if (menu == null) return null;
3564 if (iconMenuPresenter == null) {
3565 iconMenuPresenter = new IconMenuPresenter(context);
3566 iconMenuPresenter.setCallback(cb);
3567 iconMenuPresenter.setId(com.android.internal.R.id.icon_menu_presenter);
3568 menu.addMenuPresenter(iconMenuPresenter);
3571 MenuView result = iconMenuPresenter.getMenuView(decorView);
3576 Parcelable onSaveInstanceState() {
3577 SavedState savedState = new SavedState();
3578 savedState.featureId = featureId;
3579 savedState.isOpen = isOpen;
3580 savedState.isInExpandedMode = isInExpandedMode;
3583 savedState.menuState = new Bundle();
3584 menu.savePresenterStates(savedState.menuState);
3590 void onRestoreInstanceState(Parcelable state) {
3591 SavedState savedState = (SavedState) state;
3592 featureId = savedState.featureId;
3593 wasLastOpen = savedState.isOpen;
3594 wasLastExpanded = savedState.isInExpandedMode;
3595 frozenMenuState = savedState.menuState;
3598 * A LocalActivityManager keeps the same instance of this class around.
3599 * The first time the menu is being shown after restoring, the
3600 * Activity.onCreateOptionsMenu should be called. But, if it is the
3601 * same instance then menu != null and we won't call that method.
3602 * We clear any cached views here. The caller should invalidatePanelMenu.
3604 createdPanelView = null;
3605 shownPanelView = null;
3609 void applyFrozenState() {
3610 if (menu != null && frozenMenuState != null) {
3611 menu.restorePresenterStates(frozenMenuState);
3612 frozenMenuState = null;
3616 private static class SavedState implements Parcelable {
3619 boolean isInExpandedMode;
3622 public int describeContents() {
3626 public void writeToParcel(Parcel dest, int flags) {
3627 dest.writeInt(featureId);
3628 dest.writeInt(isOpen ? 1 : 0);
3629 dest.writeInt(isInExpandedMode ? 1 : 0);
3632 dest.writeBundle(menuState);
3636 private static SavedState readFromParcel(Parcel source) {
3637 SavedState savedState = new SavedState();
3638 savedState.featureId = source.readInt();
3639 savedState.isOpen = source.readInt() == 1;
3640 savedState.isInExpandedMode = source.readInt() == 1;
3642 if (savedState.isOpen) {
3643 savedState.menuState = source.readBundle();
3649 public static final Parcelable.Creator<SavedState> CREATOR
3650 = new Parcelable.Creator<SavedState>() {
3651 public SavedState createFromParcel(Parcel in) {
3652 return readFromParcel(in);
3655 public SavedState[] newArray(int size) {
3656 return new SavedState[size];
3663 static class RotationWatcher extends IRotationWatcher.Stub {
3664 private Handler mHandler;
3665 private final Runnable mRotationChanged = new Runnable() {
3667 dispatchRotationChanged();
3670 private final ArrayList<WeakReference<PhoneWindow>> mWindows =
3671 new ArrayList<WeakReference<PhoneWindow>>();
3672 private boolean mIsWatching;
3675 public void onRotationChanged(int rotation) throws RemoteException {
3676 mHandler.post(mRotationChanged);
3679 public void addWindow(PhoneWindow phoneWindow) {
3680 synchronized (mWindows) {
3683 WindowManagerHolder.sWindowManager.watchRotation(this);
3684 mHandler = new Handler();
3686 } catch (RemoteException ex) {
3687 Log.e(TAG, "Couldn't start watching for device rotation", ex);
3690 mWindows.add(new WeakReference<PhoneWindow>(phoneWindow));
3694 public void removeWindow(PhoneWindow phoneWindow) {
3695 synchronized (mWindows) {
3697 while (i < mWindows.size()) {
3698 final WeakReference<PhoneWindow> ref = mWindows.get(i);
3699 final PhoneWindow win = ref.get();
3700 if (win == null || win == phoneWindow) {
3709 void dispatchRotationChanged() {
3710 synchronized (mWindows) {
3712 while (i < mWindows.size()) {
3713 final WeakReference<PhoneWindow> ref = mWindows.get(i);
3714 final PhoneWindow win = ref.get();
3716 win.onOptionsPanelRotationChanged();
3727 * Simple implementation of MenuBuilder.Callback that:
3728 * <li> Opens a submenu when selected.
3729 * <li> Calls back to the callback's onMenuItemSelected when an item is
3732 private final class DialogMenuCallback implements MenuBuilder.Callback, MenuPresenter.Callback {
3733 private int mFeatureId;
3734 private MenuDialogHelper mSubMenuHelper;
3736 public DialogMenuCallback(int featureId) {
3737 mFeatureId = featureId;
3740 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
3741 if (menu.getRootMenu() != menu) {
3742 onCloseSubMenu(menu);
3745 if (allMenusAreClosing) {
3746 Callback callback = getCallback();
3747 if (callback != null && !isDestroyed()) {
3748 callback.onPanelClosed(mFeatureId, menu);
3751 if (menu == mContextMenu) {
3752 dismissContextMenu();
3755 // Dismiss the submenu, if it is showing
3756 if (mSubMenuHelper != null) {
3757 mSubMenuHelper.dismiss();
3758 mSubMenuHelper = null;
3763 public void onCloseSubMenu(MenuBuilder menu) {
3764 Callback callback = getCallback();
3765 if (callback != null && !isDestroyed()) {
3766 callback.onPanelClosed(mFeatureId, menu.getRootMenu());
3770 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
3771 Callback callback = getCallback();
3772 return (callback != null && !isDestroyed())
3773 && callback.onMenuItemSelected(mFeatureId, item);
3776 public void onMenuModeChange(MenuBuilder menu) {
3779 public boolean onOpenSubMenu(MenuBuilder subMenu) {
3780 if (subMenu == null) return false;
3782 // Set a simple callback for the submenu
3783 subMenu.setCallback(this);
3785 // The window manager will give us a valid window token
3786 mSubMenuHelper = new MenuDialogHelper(subMenu);
3787 mSubMenuHelper.show(null);
3793 void sendCloseSystemWindows() {
3794 PhoneWindowManager.sendCloseSystemWindows(getContext(), null);
3797 void sendCloseSystemWindows(String reason) {
3798 PhoneWindowManager.sendCloseSystemWindows(getContext(), reason);