ActionMenuPresenter.java revision a647640f85f415bd8fb8cb925e7687658fe9ae37
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.android.internal.view.menu;
18
19import com.android.internal.view.menu.ActionMenuView.ActionMenuChildView;
20
21import android.content.Context;
22import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.os.Parcel;
25import android.os.Parcelable;
26import android.util.SparseBooleanArray;
27import android.view.ActionProvider;
28import android.view.MenuItem;
29import android.view.SoundEffectConstants;
30import android.view.View;
31import android.view.View.MeasureSpec;
32import android.view.ViewConfiguration;
33import android.view.ViewGroup;
34import android.widget.ImageButton;
35
36import java.util.ArrayList;
37
38/**
39 * MenuPresenter for building action menus as seen in the action bar and action modes.
40 */
41public class ActionMenuPresenter extends BaseMenuPresenter
42        implements ActionProvider.SubUiVisibilityListener {
43    private static final String TAG = "ActionMenuPresenter";
44
45    private View mOverflowButton;
46    private boolean mReserveOverflow;
47    private boolean mReserveOverflowSet;
48    private int mWidthLimit;
49    private int mActionItemWidthLimit;
50    private int mMaxItems;
51    private boolean mMaxItemsSet;
52    private boolean mStrictWidthLimit;
53    private boolean mWidthLimitSet;
54    private boolean mExpandedActionViewsExclusive;
55
56    private int mMinCellSize;
57
58    // Group IDs that have been added as actions - used temporarily, allocated here for reuse.
59    private final SparseBooleanArray mActionButtonGroups = new SparseBooleanArray();
60
61    private View mScrapActionButtonView;
62
63    private OverflowPopup mOverflowPopup;
64    private ActionButtonSubmenu mActionButtonPopup;
65
66    private OpenOverflowRunnable mPostedOpenRunnable;
67
68    final PopupPresenterCallback mPopupPresenterCallback = new PopupPresenterCallback();
69    int mOpenSubMenuId;
70
71    public ActionMenuPresenter(Context context) {
72        super(context, com.android.internal.R.layout.action_menu_layout,
73                com.android.internal.R.layout.action_menu_item_layout);
74    }
75
76    @Override
77    public void initForMenu(Context context, MenuBuilder menu) {
78        super.initForMenu(context, menu);
79
80        final Resources res = context.getResources();
81
82        if (!mReserveOverflowSet) {
83            mReserveOverflow = !ViewConfiguration.get(context).hasPermanentMenuKey();
84        }
85
86        if (!mWidthLimitSet) {
87            mWidthLimit = res.getDisplayMetrics().widthPixels / 2;
88        }
89
90        // Measure for initial configuration
91        if (!mMaxItemsSet) {
92            mMaxItems = res.getInteger(com.android.internal.R.integer.max_action_buttons);
93        }
94
95        int width = mWidthLimit;
96        if (mReserveOverflow) {
97            if (mOverflowButton == null) {
98                mOverflowButton = new OverflowMenuButton(mSystemContext);
99                final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
100                mOverflowButton.measure(spec, spec);
101            }
102            width -= mOverflowButton.getMeasuredWidth();
103        } else {
104            mOverflowButton = null;
105        }
106
107        mActionItemWidthLimit = width;
108
109        mMinCellSize = (int) (ActionMenuView.MIN_CELL_SIZE * res.getDisplayMetrics().density);
110
111        // Drop a scrap view as it may no longer reflect the proper context/config.
112        mScrapActionButtonView = null;
113    }
114
115    public void onConfigurationChanged(Configuration newConfig) {
116        if (!mMaxItemsSet) {
117            mMaxItems = mContext.getResources().getInteger(
118                    com.android.internal.R.integer.max_action_buttons);
119            if (mMenu != null) {
120                mMenu.onItemsChanged(true);
121            }
122        }
123    }
124
125    public void setWidthLimit(int width, boolean strict) {
126        mWidthLimit = width;
127        mStrictWidthLimit = strict;
128        mWidthLimitSet = true;
129    }
130
131    public void setReserveOverflow(boolean reserveOverflow) {
132        mReserveOverflow = reserveOverflow;
133        mReserveOverflowSet = true;
134    }
135
136    public void setItemLimit(int itemCount) {
137        mMaxItems = itemCount;
138        mMaxItemsSet = true;
139    }
140
141    public void setExpandedActionViewsExclusive(boolean isExclusive) {
142        mExpandedActionViewsExclusive = isExclusive;
143    }
144
145    @Override
146    public MenuView getMenuView(ViewGroup root) {
147        MenuView result = super.getMenuView(root);
148        ((ActionMenuView) result).setPresenter(this);
149        return result;
150    }
151
152    @Override
153    public View getItemView(MenuItemImpl item, View convertView, ViewGroup parent) {
154        View actionView = item.getActionView();
155        if (actionView == null || item.hasCollapsibleActionView()) {
156            if (!(convertView instanceof ActionMenuItemView)) {
157                convertView = null;
158            }
159            actionView = super.getItemView(item, convertView, parent);
160        }
161        actionView.setVisibility(item.isActionViewExpanded() ? View.GONE : View.VISIBLE);
162
163        final ActionMenuView menuParent = (ActionMenuView) parent;
164        final ViewGroup.LayoutParams lp = actionView.getLayoutParams();
165        if (!menuParent.checkLayoutParams(lp)) {
166            actionView.setLayoutParams(menuParent.generateLayoutParams(lp));
167        }
168        return actionView;
169    }
170
171    @Override
172    public void bindItemView(MenuItemImpl item, MenuView.ItemView itemView) {
173        itemView.initialize(item, 0);
174
175        final ActionMenuView menuView = (ActionMenuView) mMenuView;
176        ActionMenuItemView actionItemView = (ActionMenuItemView) itemView;
177        actionItemView.setItemInvoker(menuView);
178    }
179
180    @Override
181    public boolean shouldIncludeItem(int childIndex, MenuItemImpl item) {
182        return item.isActionButton();
183    }
184
185    @Override
186    public void updateMenuView(boolean cleared) {
187        super.updateMenuView(cleared);
188
189        if (mMenu != null) {
190            final ArrayList<MenuItemImpl> actionItems = mMenu.getActionItems();
191            final int count = actionItems.size();
192            for (int i = 0; i < count; i++) {
193                final ActionProvider provider = actionItems.get(i).getActionProvider();
194                if (provider != null) {
195                    provider.setSubUiVisibilityListener(this);
196                }
197            }
198        }
199
200        final ArrayList<MenuItemImpl> nonActionItems = mMenu != null ?
201                mMenu.getNonActionItems() : null;
202
203        boolean hasOverflow = false;
204        if (mReserveOverflow && nonActionItems != null) {
205            final int count = nonActionItems.size();
206            if (count == 1) {
207                hasOverflow = !nonActionItems.get(0).isActionViewExpanded();
208            } else {
209                hasOverflow = count > 0;
210            }
211        }
212
213        if (hasOverflow) {
214            if (mOverflowButton == null) {
215                mOverflowButton = new OverflowMenuButton(mSystemContext);
216            }
217            ViewGroup parent = (ViewGroup) mOverflowButton.getParent();
218            if (parent != mMenuView) {
219                if (parent != null) {
220                    parent.removeView(mOverflowButton);
221                }
222                ActionMenuView menuView = (ActionMenuView) mMenuView;
223                menuView.addView(mOverflowButton, menuView.generateOverflowButtonLayoutParams());
224            }
225        } else if (mOverflowButton != null && mOverflowButton.getParent() == mMenuView) {
226            ((ViewGroup) mMenuView).removeView(mOverflowButton);
227        }
228
229        ((ActionMenuView) mMenuView).setOverflowReserved(mReserveOverflow);
230    }
231
232    @Override
233    public boolean filterLeftoverView(ViewGroup parent, int childIndex) {
234        if (parent.getChildAt(childIndex) == mOverflowButton) return false;
235        return super.filterLeftoverView(parent, childIndex);
236    }
237
238    public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
239        if (!subMenu.hasVisibleItems()) return false;
240
241        SubMenuBuilder topSubMenu = subMenu;
242        while (topSubMenu.getParentMenu() != mMenu) {
243            topSubMenu = (SubMenuBuilder) topSubMenu.getParentMenu();
244        }
245        View anchor = findViewForItem(topSubMenu.getItem());
246        if (anchor == null) {
247            if (mOverflowButton == null) return false;
248            anchor = mOverflowButton;
249        }
250
251        mOpenSubMenuId = subMenu.getItem().getItemId();
252        mActionButtonPopup = new ActionButtonSubmenu(mContext, subMenu);
253        mActionButtonPopup.setAnchorView(anchor);
254        mActionButtonPopup.show();
255        super.onSubMenuSelected(subMenu);
256        return true;
257    }
258
259    private View findViewForItem(MenuItem item) {
260        final ViewGroup parent = (ViewGroup) mMenuView;
261        if (parent == null) return null;
262
263        final int count = parent.getChildCount();
264        for (int i = 0; i < count; i++) {
265            final View child = parent.getChildAt(i);
266            if (child instanceof MenuView.ItemView &&
267                    ((MenuView.ItemView) child).getItemData() == item) {
268                return child;
269            }
270        }
271        return null;
272    }
273
274    /**
275     * Display the overflow menu if one is present.
276     * @return true if the overflow menu was shown, false otherwise.
277     */
278    public boolean showOverflowMenu() {
279        if (mReserveOverflow && !isOverflowMenuShowing() && mMenu != null && mMenuView != null &&
280                mPostedOpenRunnable == null && !mMenu.getNonActionItems().isEmpty()) {
281            OverflowPopup popup = new OverflowPopup(mContext, mMenu, mOverflowButton, true);
282            mPostedOpenRunnable = new OpenOverflowRunnable(popup);
283            // Post this for later; we might still need a layout for the anchor to be right.
284            ((View) mMenuView).post(mPostedOpenRunnable);
285
286            // ActionMenuPresenter uses null as a callback argument here
287            // to indicate overflow is opening.
288            super.onSubMenuSelected(null);
289
290            return true;
291        }
292        return false;
293    }
294
295    /**
296     * Hide the overflow menu if it is currently showing.
297     *
298     * @return true if the overflow menu was hidden, false otherwise.
299     */
300    public boolean hideOverflowMenu() {
301        if (mPostedOpenRunnable != null && mMenuView != null) {
302            ((View) mMenuView).removeCallbacks(mPostedOpenRunnable);
303            mPostedOpenRunnable = null;
304            return true;
305        }
306
307        MenuPopupHelper popup = mOverflowPopup;
308        if (popup != null) {
309            popup.dismiss();
310            return true;
311        }
312        return false;
313    }
314
315    /**
316     * Dismiss all popup menus - overflow and submenus.
317     * @return true if popups were dismissed, false otherwise. (This can be because none were open.)
318     */
319    public boolean dismissPopupMenus() {
320        boolean result = hideOverflowMenu();
321        result |= hideSubMenus();
322        return result;
323    }
324
325    /**
326     * Dismiss all submenu popups.
327     *
328     * @return true if popups were dismissed, false otherwise. (This can be because none were open.)
329     */
330    public boolean hideSubMenus() {
331        if (mActionButtonPopup != null) {
332            mActionButtonPopup.dismiss();
333            return true;
334        }
335        return false;
336    }
337
338    /**
339     * @return true if the overflow menu is currently showing
340     */
341    public boolean isOverflowMenuShowing() {
342        return mOverflowPopup != null && mOverflowPopup.isShowing();
343    }
344
345    /**
346     * @return true if space has been reserved in the action menu for an overflow item.
347     */
348    public boolean isOverflowReserved() {
349        return mReserveOverflow;
350    }
351
352    public boolean flagActionItems() {
353        final ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems();
354        final int itemsSize = visibleItems.size();
355        int maxActions = mMaxItems;
356        int widthLimit = mActionItemWidthLimit;
357        final int querySpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
358        final ViewGroup parent = (ViewGroup) mMenuView;
359
360        int requiredItems = 0;
361        int requestedItems = 0;
362        int firstActionWidth = 0;
363        boolean hasOverflow = false;
364        for (int i = 0; i < itemsSize; i++) {
365            MenuItemImpl item = visibleItems.get(i);
366            if (item.requiresActionButton()) {
367                requiredItems++;
368            } else if (item.requestsActionButton()) {
369                requestedItems++;
370            } else {
371                hasOverflow = true;
372            }
373            if (mExpandedActionViewsExclusive && item.isActionViewExpanded()) {
374                // Overflow everything if we have an expanded action view and we're
375                // space constrained.
376                maxActions = 0;
377            }
378        }
379
380        // Reserve a spot for the overflow item if needed.
381        if (mReserveOverflow &&
382                (hasOverflow || requiredItems + requestedItems > maxActions)) {
383            maxActions--;
384        }
385        maxActions -= requiredItems;
386
387        final SparseBooleanArray seenGroups = mActionButtonGroups;
388        seenGroups.clear();
389
390        int cellSize = 0;
391        int cellsRemaining = 0;
392        if (mStrictWidthLimit) {
393            cellsRemaining = widthLimit / mMinCellSize;
394            final int cellSizeRemaining = widthLimit % mMinCellSize;
395            cellSize = mMinCellSize + cellSizeRemaining / cellsRemaining;
396        }
397
398        // Flag as many more requested items as will fit.
399        for (int i = 0; i < itemsSize; i++) {
400            MenuItemImpl item = visibleItems.get(i);
401
402            if (item.requiresActionButton()) {
403                View v = getItemView(item, mScrapActionButtonView, parent);
404                if (mScrapActionButtonView == null) {
405                    mScrapActionButtonView = v;
406                }
407                if (mStrictWidthLimit) {
408                    cellsRemaining -= ActionMenuView.measureChildForCells(v,
409                            cellSize, cellsRemaining, querySpec, 0);
410                } else {
411                    v.measure(querySpec, querySpec);
412                }
413                final int measuredWidth = v.getMeasuredWidth();
414                widthLimit -= measuredWidth;
415                if (firstActionWidth == 0) {
416                    firstActionWidth = measuredWidth;
417                }
418                final int groupId = item.getGroupId();
419                if (groupId != 0) {
420                    seenGroups.put(groupId, true);
421                }
422                item.setIsActionButton(true);
423            } else if (item.requestsActionButton()) {
424                // Items in a group with other items that already have an action slot
425                // can break the max actions rule, but not the width limit.
426                final int groupId = item.getGroupId();
427                final boolean inGroup = seenGroups.get(groupId);
428                boolean isAction = (maxActions > 0 || inGroup) && widthLimit > 0 &&
429                        (!mStrictWidthLimit || cellsRemaining > 0);
430
431                if (isAction) {
432                    View v = getItemView(item, mScrapActionButtonView, parent);
433                    if (mScrapActionButtonView == null) {
434                        mScrapActionButtonView = v;
435                    }
436                    if (mStrictWidthLimit) {
437                        final int cells = ActionMenuView.measureChildForCells(v,
438                                cellSize, cellsRemaining, querySpec, 0);
439                        cellsRemaining -= cells;
440                        if (cells == 0) {
441                            isAction = false;
442                        }
443                    } else {
444                        v.measure(querySpec, querySpec);
445                    }
446                    final int measuredWidth = v.getMeasuredWidth();
447                    widthLimit -= measuredWidth;
448                    if (firstActionWidth == 0) {
449                        firstActionWidth = measuredWidth;
450                    }
451
452                    if (mStrictWidthLimit) {
453                        isAction &= widthLimit >= 0;
454                    } else {
455                        // Did this push the entire first item past the limit?
456                        isAction &= widthLimit + firstActionWidth > 0;
457                    }
458                }
459
460                if (isAction && groupId != 0) {
461                    seenGroups.put(groupId, true);
462                } else if (inGroup) {
463                    // We broke the width limit. Demote the whole group, they all overflow now.
464                    seenGroups.put(groupId, false);
465                    for (int j = 0; j < i; j++) {
466                        MenuItemImpl areYouMyGroupie = visibleItems.get(j);
467                        if (areYouMyGroupie.getGroupId() == groupId) {
468                            // Give back the action slot
469                            if (areYouMyGroupie.isActionButton()) maxActions++;
470                            areYouMyGroupie.setIsActionButton(false);
471                        }
472                    }
473                }
474
475                if (isAction) maxActions--;
476
477                item.setIsActionButton(isAction);
478            }
479        }
480        return true;
481    }
482
483    @Override
484    public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
485        dismissPopupMenus();
486        super.onCloseMenu(menu, allMenusAreClosing);
487    }
488
489    @Override
490    public Parcelable onSaveInstanceState() {
491        SavedState state = new SavedState();
492        state.openSubMenuId = mOpenSubMenuId;
493        return state;
494    }
495
496    @Override
497    public void onRestoreInstanceState(Parcelable state) {
498        SavedState saved = (SavedState) state;
499        if (saved.openSubMenuId > 0) {
500            MenuItem item = mMenu.findItem(saved.openSubMenuId);
501            if (item != null) {
502                SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
503                onSubMenuSelected(subMenu);
504            }
505        }
506    }
507
508    @Override
509    public void onSubUiVisibilityChanged(boolean isVisible) {
510        if (isVisible) {
511            // Not a submenu, but treat it like one.
512            super.onSubMenuSelected(null);
513        } else {
514            mMenu.close(false);
515        }
516    }
517
518    private static class SavedState implements Parcelable {
519        public int openSubMenuId;
520
521        SavedState() {
522        }
523
524        SavedState(Parcel in) {
525            openSubMenuId = in.readInt();
526        }
527
528        @Override
529        public int describeContents() {
530            return 0;
531        }
532
533        @Override
534        public void writeToParcel(Parcel dest, int flags) {
535            dest.writeInt(openSubMenuId);
536        }
537
538        public static final Parcelable.Creator<SavedState> CREATOR
539                = new Parcelable.Creator<SavedState>() {
540            public SavedState createFromParcel(Parcel in) {
541                return new SavedState(in);
542            }
543
544            public SavedState[] newArray(int size) {
545                return new SavedState[size];
546            }
547        };
548    }
549
550    private class OverflowMenuButton extends ImageButton implements ActionMenuChildView {
551        public OverflowMenuButton(Context context) {
552            super(context, null, com.android.internal.R.attr.actionOverflowButtonStyle);
553
554            setClickable(true);
555            setFocusable(true);
556            setVisibility(VISIBLE);
557            setEnabled(true);
558        }
559
560        @Override
561        public boolean performClick() {
562            if (super.performClick()) {
563                return true;
564            }
565
566            playSoundEffect(SoundEffectConstants.CLICK);
567            showOverflowMenu();
568            return true;
569        }
570
571        public boolean needsDividerBefore() {
572            return false;
573        }
574
575        public boolean needsDividerAfter() {
576            return false;
577        }
578    }
579
580    private class OverflowPopup extends MenuPopupHelper {
581        public OverflowPopup(Context context, MenuBuilder menu, View anchorView,
582                boolean overflowOnly) {
583            super(context, menu, anchorView, overflowOnly);
584            setCallback(mPopupPresenterCallback);
585        }
586
587        @Override
588        public void onDismiss() {
589            super.onDismiss();
590            mMenu.close();
591            mOverflowPopup = null;
592        }
593    }
594
595    private class ActionButtonSubmenu extends MenuPopupHelper {
596        private SubMenuBuilder mSubMenu;
597
598        public ActionButtonSubmenu(Context context, SubMenuBuilder subMenu) {
599            super(context, subMenu);
600            mSubMenu = subMenu;
601
602            MenuItemImpl item = (MenuItemImpl) subMenu.getItem();
603            if (!item.isActionButton()) {
604                // Give a reasonable anchor to nested submenus.
605                setAnchorView(mOverflowButton == null ? (View) mMenuView : mOverflowButton);
606            }
607
608            setCallback(mPopupPresenterCallback);
609
610            boolean preserveIconSpacing = false;
611            final int count = subMenu.size();
612            for (int i = 0; i < count; i++) {
613                MenuItem childItem = subMenu.getItem(i);
614                if (childItem.isVisible() && childItem.getIcon() != null) {
615                    preserveIconSpacing = true;
616                    break;
617                }
618            }
619            setForceShowIcon(preserveIconSpacing);
620        }
621
622        @Override
623        public void onDismiss() {
624            super.onDismiss();
625            mActionButtonPopup = null;
626            mOpenSubMenuId = 0;
627        }
628    }
629
630    private class PopupPresenterCallback implements MenuPresenter.Callback {
631
632        @Override
633        public boolean onOpenSubMenu(MenuBuilder subMenu) {
634            if (subMenu == null) return false;
635
636            mOpenSubMenuId = ((SubMenuBuilder) subMenu).getItem().getItemId();
637            return false;
638        }
639
640        @Override
641        public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
642            if (menu instanceof SubMenuBuilder) {
643                ((SubMenuBuilder) menu).getRootMenu().close(false);
644            }
645        }
646    }
647
648    private class OpenOverflowRunnable implements Runnable {
649        private OverflowPopup mPopup;
650
651        public OpenOverflowRunnable(OverflowPopup popup) {
652            mPopup = popup;
653        }
654
655        public void run() {
656            mMenu.changeMenuMode();
657            final View menuView = (View) mMenuView;
658            if (menuView != null && menuView.getWindowToken() != null && mPopup.tryShow()) {
659                mOverflowPopup = mPopup;
660            }
661            mPostedOpenRunnable = null;
662        }
663    }
664}
665