2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.actionbarsherlock.internal.view.menu;
19 import static com.actionbarsherlock.internal.ResourcesCompat.getResources_getInteger;
20 import java.util.ArrayList;
21 import java.util.HashSet;
23 import android.content.Context;
24 import android.content.res.Configuration;
25 import android.content.res.Resources;
26 import android.content.res.TypedArray;
27 import android.os.Build;
28 import android.os.Parcel;
29 import android.os.Parcelable;
30 import android.util.SparseBooleanArray;
31 import android.view.SoundEffectConstants;
32 import android.view.View;
33 import android.view.View.MeasureSpec;
34 import android.view.ViewConfiguration;
35 import android.view.ViewGroup;
36 import android.widget.ImageButton;
37 import com.actionbarsherlock.R;
38 import com.actionbarsherlock.internal.view.View_HasStateListenerSupport;
39 import com.actionbarsherlock.internal.view.View_OnAttachStateChangeListener;
40 import com.actionbarsherlock.internal.view.menu.ActionMenuView.ActionMenuChildView;
41 import com.actionbarsherlock.view.ActionProvider;
42 import com.actionbarsherlock.view.MenuItem;
45 * MenuPresenter for building action menus as seen in the action bar and action modes.
47 public class ActionMenuPresenter extends BaseMenuPresenter
48 implements ActionProvider.SubUiVisibilityListener {
49 //UNUSED private static final String TAG = "ActionMenuPresenter";
51 private View mOverflowButton;
52 private boolean mReserveOverflow;
53 private boolean mReserveOverflowSet;
54 private int mWidthLimit;
55 private int mActionItemWidthLimit;
56 private int mMaxItems;
57 private boolean mMaxItemsSet;
58 private boolean mStrictWidthLimit;
59 private boolean mWidthLimitSet;
60 private boolean mExpandedActionViewsExclusive;
62 private int mMinCellSize;
64 // Group IDs that have been added as actions - used temporarily, allocated here for reuse.
65 private final SparseBooleanArray mActionButtonGroups = new SparseBooleanArray();
67 private View mScrapActionButtonView;
69 private OverflowPopup mOverflowPopup;
70 private ActionButtonSubmenu mActionButtonPopup;
72 private OpenOverflowRunnable mPostedOpenRunnable;
74 final PopupPresenterCallback mPopupPresenterCallback = new PopupPresenterCallback();
77 public ActionMenuPresenter(Context context) {
78 super(context, R.layout.abs__action_menu_layout,
79 R.layout.abs__action_menu_item_layout);
83 public void initForMenu(Context context, MenuBuilder menu) {
84 super.initForMenu(context, menu);
86 final Resources res = context.getResources();
88 if (!mReserveOverflowSet) {
89 mReserveOverflow = reserveOverflow(mContext);
92 if (!mWidthLimitSet) {
93 mWidthLimit = res.getDisplayMetrics().widthPixels / 2;
96 // Measure for initial configuration
98 mMaxItems = getResources_getInteger(context, R.integer.abs__max_action_buttons);
101 int width = mWidthLimit;
102 if (mReserveOverflow) {
103 if (mOverflowButton == null) {
104 mOverflowButton = new OverflowMenuButton(mSystemContext);
105 final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
106 mOverflowButton.measure(spec, spec);
108 width -= mOverflowButton.getMeasuredWidth();
110 mOverflowButton = null;
113 mActionItemWidthLimit = width;
115 mMinCellSize = (int) (ActionMenuView.MIN_CELL_SIZE * res.getDisplayMetrics().density);
117 // Drop a scrap view as it may no longer reflect the proper context/config.
118 mScrapActionButtonView = null;
121 public static boolean reserveOverflow(Context context) {
122 //Check for theme-forced overflow action item
123 TypedArray a = context.getTheme().obtainStyledAttributes(R.styleable.SherlockTheme);
124 boolean result = a.getBoolean(R.styleable.SherlockTheme_absForceOverflow, false);
130 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
131 return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB);
133 return !HasPermanentMenuKey.get(context);
137 private static class HasPermanentMenuKey {
138 public static boolean get(Context context) {
139 return ViewConfiguration.get(context).hasPermanentMenuKey();
143 public void onConfigurationChanged(Configuration newConfig) {
145 mMaxItems = getResources_getInteger(mContext,
146 R.integer.abs__max_action_buttons);
148 mMenu.onItemsChanged(true);
153 public void setWidthLimit(int width, boolean strict) {
155 mStrictWidthLimit = strict;
156 mWidthLimitSet = true;
159 public void setReserveOverflow(boolean reserveOverflow) {
160 mReserveOverflow = reserveOverflow;
161 mReserveOverflowSet = true;
164 public void setItemLimit(int itemCount) {
165 mMaxItems = itemCount;
169 public void setExpandedActionViewsExclusive(boolean isExclusive) {
170 mExpandedActionViewsExclusive = isExclusive;
174 public MenuView getMenuView(ViewGroup root) {
175 MenuView result = super.getMenuView(root);
176 ((ActionMenuView) result).setPresenter(this);
181 public View getItemView(MenuItemImpl item, View convertView, ViewGroup parent) {
182 View actionView = item.getActionView();
183 if (actionView == null || item.hasCollapsibleActionView()) {
184 if (!(convertView instanceof ActionMenuItemView)) {
187 actionView = super.getItemView(item, convertView, parent);
189 actionView.setVisibility(item.isActionViewExpanded() ? View.GONE : View.VISIBLE);
191 final ActionMenuView menuParent = (ActionMenuView) parent;
192 final ViewGroup.LayoutParams lp = actionView.getLayoutParams();
193 if (!menuParent.checkLayoutParams(lp)) {
194 actionView.setLayoutParams(menuParent.generateLayoutParams(lp));
200 public void bindItemView(MenuItemImpl item, MenuView.ItemView itemView) {
201 itemView.initialize(item, 0);
203 final ActionMenuView menuView = (ActionMenuView) mMenuView;
204 ActionMenuItemView actionItemView = (ActionMenuItemView) itemView;
205 actionItemView.setItemInvoker(menuView);
209 public boolean shouldIncludeItem(int childIndex, MenuItemImpl item) {
210 return item.isActionButton();
214 public void updateMenuView(boolean cleared) {
215 super.updateMenuView(cleared);
218 final ArrayList<MenuItemImpl> actionItems = mMenu.getActionItems();
219 final int count = actionItems.size();
220 for (int i = 0; i < count; i++) {
221 final ActionProvider provider = actionItems.get(i).getActionProvider();
222 if (provider != null) {
223 provider.setSubUiVisibilityListener(this);
228 final ArrayList<MenuItemImpl> nonActionItems = mMenu != null ?
229 mMenu.getNonActionItems() : null;
231 boolean hasOverflow = false;
232 if (mReserveOverflow && nonActionItems != null) {
233 final int count = nonActionItems.size();
235 hasOverflow = !nonActionItems.get(0).isActionViewExpanded();
237 hasOverflow = count > 0;
242 if (mOverflowButton == null) {
243 mOverflowButton = new OverflowMenuButton(mSystemContext);
245 ViewGroup parent = (ViewGroup) mOverflowButton.getParent();
246 if (parent != mMenuView) {
247 if (parent != null) {
248 parent.removeView(mOverflowButton);
250 ActionMenuView menuView = (ActionMenuView) mMenuView;
251 menuView.addView(mOverflowButton, menuView.generateOverflowButtonLayoutParams());
253 } else if (mOverflowButton != null && mOverflowButton.getParent() == mMenuView) {
254 ((ViewGroup) mMenuView).removeView(mOverflowButton);
257 ((ActionMenuView) mMenuView).setOverflowReserved(mReserveOverflow);
261 public boolean filterLeftoverView(ViewGroup parent, int childIndex) {
262 if (parent.getChildAt(childIndex) == mOverflowButton) return false;
263 return super.filterLeftoverView(parent, childIndex);
266 public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
267 if (!subMenu.hasVisibleItems()) return false;
269 SubMenuBuilder topSubMenu = subMenu;
270 while (topSubMenu.getParentMenu() != mMenu) {
271 topSubMenu = (SubMenuBuilder) topSubMenu.getParentMenu();
273 View anchor = findViewForItem(topSubMenu.getItem());
274 if (anchor == null) {
275 if (mOverflowButton == null) return false;
276 anchor = mOverflowButton;
279 mOpenSubMenuId = subMenu.getItem().getItemId();
280 mActionButtonPopup = new ActionButtonSubmenu(mContext, subMenu);
281 mActionButtonPopup.setAnchorView(anchor);
282 mActionButtonPopup.show();
283 super.onSubMenuSelected(subMenu);
287 private View findViewForItem(MenuItem item) {
288 final ViewGroup parent = (ViewGroup) mMenuView;
289 if (parent == null) return null;
291 final int count = parent.getChildCount();
292 for (int i = 0; i < count; i++) {
293 final View child = parent.getChildAt(i);
294 if (child instanceof MenuView.ItemView &&
295 ((MenuView.ItemView) child).getItemData() == item) {
303 * Display the overflow menu if one is present.
304 * @return true if the overflow menu was shown, false otherwise.
306 public boolean showOverflowMenu() {
307 if (mReserveOverflow && !isOverflowMenuShowing() && mMenu != null && mMenuView != null &&
308 mPostedOpenRunnable == null && !mMenu.getNonActionItems().isEmpty()) {
309 OverflowPopup popup = new OverflowPopup(mContext, mMenu, mOverflowButton, true);
310 mPostedOpenRunnable = new OpenOverflowRunnable(popup);
311 // Post this for later; we might still need a layout for the anchor to be right.
312 ((View) mMenuView).post(mPostedOpenRunnable);
314 // ActionMenuPresenter uses null as a callback argument here
315 // to indicate overflow is opening.
316 super.onSubMenuSelected(null);
324 * Hide the overflow menu if it is currently showing.
326 * @return true if the overflow menu was hidden, false otherwise.
328 public boolean hideOverflowMenu() {
329 if (mPostedOpenRunnable != null && mMenuView != null) {
330 ((View) mMenuView).removeCallbacks(mPostedOpenRunnable);
331 mPostedOpenRunnable = null;
335 MenuPopupHelper popup = mOverflowPopup;
344 * Dismiss all popup menus - overflow and submenus.
345 * @return true if popups were dismissed, false otherwise. (This can be because none were open.)
347 public boolean dismissPopupMenus() {
348 boolean result = hideOverflowMenu();
349 result |= hideSubMenus();
354 * Dismiss all submenu popups.
356 * @return true if popups were dismissed, false otherwise. (This can be because none were open.)
358 public boolean hideSubMenus() {
359 if (mActionButtonPopup != null) {
360 mActionButtonPopup.dismiss();
367 * @return true if the overflow menu is currently showing
369 public boolean isOverflowMenuShowing() {
370 return mOverflowPopup != null && mOverflowPopup.isShowing();
374 * @return true if space has been reserved in the action menu for an overflow item.
376 public boolean isOverflowReserved() {
377 return mReserveOverflow;
380 public boolean flagActionItems() {
381 final ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems();
382 final int itemsSize = visibleItems.size();
383 int maxActions = mMaxItems;
384 int widthLimit = mActionItemWidthLimit;
385 final int querySpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
386 final ViewGroup parent = (ViewGroup) mMenuView;
388 int requiredItems = 0;
389 int requestedItems = 0;
390 int firstActionWidth = 0;
391 boolean hasOverflow = false;
392 for (int i = 0; i < itemsSize; i++) {
393 MenuItemImpl item = visibleItems.get(i);
394 if (item.requiresActionButton()) {
396 } else if (item.requestsActionButton()) {
401 if (mExpandedActionViewsExclusive && item.isActionViewExpanded()) {
402 // Overflow everything if we have an expanded action view and we're
403 // space constrained.
408 // Reserve a spot for the overflow item if needed.
409 if (mReserveOverflow &&
410 (hasOverflow || requiredItems + requestedItems > maxActions)) {
413 maxActions -= requiredItems;
415 final SparseBooleanArray seenGroups = mActionButtonGroups;
419 int cellsRemaining = 0;
420 if (mStrictWidthLimit) {
421 cellsRemaining = widthLimit / mMinCellSize;
422 final int cellSizeRemaining = widthLimit % mMinCellSize;
423 cellSize = mMinCellSize + cellSizeRemaining / cellsRemaining;
426 // Flag as many more requested items as will fit.
427 for (int i = 0; i < itemsSize; i++) {
428 MenuItemImpl item = visibleItems.get(i);
430 if (item.requiresActionButton()) {
431 View v = getItemView(item, mScrapActionButtonView, parent);
432 if (mScrapActionButtonView == null) {
433 mScrapActionButtonView = v;
435 if (mStrictWidthLimit) {
436 cellsRemaining -= ActionMenuView.measureChildForCells(v,
437 cellSize, cellsRemaining, querySpec, 0);
439 v.measure(querySpec, querySpec);
441 final int measuredWidth = v.getMeasuredWidth();
442 widthLimit -= measuredWidth;
443 if (firstActionWidth == 0) {
444 firstActionWidth = measuredWidth;
446 final int groupId = item.getGroupId();
448 seenGroups.put(groupId, true);
450 item.setIsActionButton(true);
451 } else if (item.requestsActionButton()) {
452 // Items in a group with other items that already have an action slot
453 // can break the max actions rule, but not the width limit.
454 final int groupId = item.getGroupId();
455 final boolean inGroup = seenGroups.get(groupId);
456 boolean isAction = (maxActions > 0 || inGroup) && widthLimit > 0 &&
457 (!mStrictWidthLimit || cellsRemaining > 0);
460 View v = getItemView(item, mScrapActionButtonView, parent);
461 if (mScrapActionButtonView == null) {
462 mScrapActionButtonView = v;
464 if (mStrictWidthLimit) {
465 final int cells = ActionMenuView.measureChildForCells(v,
466 cellSize, cellsRemaining, querySpec, 0);
467 cellsRemaining -= cells;
472 v.measure(querySpec, querySpec);
474 final int measuredWidth = v.getMeasuredWidth();
475 widthLimit -= measuredWidth;
476 if (firstActionWidth == 0) {
477 firstActionWidth = measuredWidth;
480 if (mStrictWidthLimit) {
481 isAction &= widthLimit >= 0;
483 // Did this push the entire first item past the limit?
484 isAction &= widthLimit + firstActionWidth > 0;
488 if (isAction && groupId != 0) {
489 seenGroups.put(groupId, true);
490 } else if (inGroup) {
491 // We broke the width limit. Demote the whole group, they all overflow now.
492 seenGroups.put(groupId, false);
493 for (int j = 0; j < i; j++) {
494 MenuItemImpl areYouMyGroupie = visibleItems.get(j);
495 if (areYouMyGroupie.getGroupId() == groupId) {
496 // Give back the action slot
497 if (areYouMyGroupie.isActionButton()) maxActions++;
498 areYouMyGroupie.setIsActionButton(false);
503 if (isAction) maxActions--;
505 item.setIsActionButton(isAction);
512 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
514 super.onCloseMenu(menu, allMenusAreClosing);
518 public Parcelable onSaveInstanceState() {
519 SavedState state = new SavedState();
520 state.openSubMenuId = mOpenSubMenuId;
525 public void onRestoreInstanceState(Parcelable state) {
526 SavedState saved = (SavedState) state;
527 if (saved.openSubMenuId > 0) {
528 MenuItem item = mMenu.findItem(saved.openSubMenuId);
530 SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
531 onSubMenuSelected(subMenu);
537 public void onSubUiVisibilityChanged(boolean isVisible) {
539 // Not a submenu, but treat it like one.
540 super.onSubMenuSelected(null);
546 private static class SavedState implements Parcelable {
547 public int openSubMenuId;
552 SavedState(Parcel in) {
553 openSubMenuId = in.readInt();
557 public int describeContents() {
562 public void writeToParcel(Parcel dest, int flags) {
563 dest.writeInt(openSubMenuId);
566 @SuppressWarnings("unused")
567 public static final Parcelable.Creator<SavedState> CREATOR
568 = new Parcelable.Creator<SavedState>() {
569 public SavedState createFromParcel(Parcel in) {
570 return new SavedState(in);
573 public SavedState[] newArray(int size) {
574 return new SavedState[size];
579 private class OverflowMenuButton extends ImageButton implements ActionMenuChildView, View_HasStateListenerSupport {
580 private final Set<View_OnAttachStateChangeListener> mListeners = new HashSet<View_OnAttachStateChangeListener>();
582 public OverflowMenuButton(Context context) {
583 super(context, null, R.attr.actionOverflowButtonStyle);
587 setVisibility(VISIBLE);
592 public boolean performClick() {
593 if (super.performClick()) {
597 playSoundEffect(SoundEffectConstants.CLICK);
602 public boolean needsDividerBefore() {
606 public boolean needsDividerAfter() {
611 protected void onAttachedToWindow() {
612 super.onAttachedToWindow();
613 for (View_OnAttachStateChangeListener listener : mListeners) {
614 listener.onViewAttachedToWindow(this);
619 protected void onDetachedFromWindow() {
620 super.onDetachedFromWindow();
621 for (View_OnAttachStateChangeListener listener : mListeners) {
622 listener.onViewDetachedFromWindow(this);
627 public void addOnAttachStateChangeListener(View_OnAttachStateChangeListener listener) {
628 mListeners.add(listener);
632 public void removeOnAttachStateChangeListener(View_OnAttachStateChangeListener listener) {
633 mListeners.remove(listener);
637 private class OverflowPopup extends MenuPopupHelper {
638 public OverflowPopup(Context context, MenuBuilder menu, View anchorView,
639 boolean overflowOnly) {
640 super(context, menu, anchorView, overflowOnly);
641 setCallback(mPopupPresenterCallback);
645 public void onDismiss() {
648 mOverflowPopup = null;
652 private class ActionButtonSubmenu extends MenuPopupHelper {
653 //UNUSED private SubMenuBuilder mSubMenu;
655 public ActionButtonSubmenu(Context context, SubMenuBuilder subMenu) {
656 super(context, subMenu);
657 //UNUSED mSubMenu = subMenu;
659 MenuItemImpl item = (MenuItemImpl) subMenu.getItem();
660 if (!item.isActionButton()) {
661 // Give a reasonable anchor to nested submenus.
662 setAnchorView(mOverflowButton == null ? (View) mMenuView : mOverflowButton);
665 setCallback(mPopupPresenterCallback);
667 boolean preserveIconSpacing = false;
668 final int count = subMenu.size();
669 for (int i = 0; i < count; i++) {
670 MenuItem childItem = subMenu.getItem(i);
671 if (childItem.isVisible() && childItem.getIcon() != null) {
672 preserveIconSpacing = true;
676 setForceShowIcon(preserveIconSpacing);
680 public void onDismiss() {
682 mActionButtonPopup = null;
687 private class PopupPresenterCallback implements MenuPresenter.Callback {
690 public boolean onOpenSubMenu(MenuBuilder subMenu) {
691 if (subMenu == null) return false;
693 mOpenSubMenuId = ((SubMenuBuilder) subMenu).getItem().getItemId();
698 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
699 if (menu instanceof SubMenuBuilder) {
700 ((SubMenuBuilder) menu).getRootMenu().close(false);
705 private class OpenOverflowRunnable implements Runnable {
706 private OverflowPopup mPopup;
708 public OpenOverflowRunnable(OverflowPopup popup) {
713 mMenu.changeMenuMode();
714 final View menuView = (View) mMenuView;
715 if (menuView != null && menuView.getWindowToken() != null && mPopup.tryShow()) {
716 mOverflowPopup = mPopup;
718 mPostedOpenRunnable = null;