1bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/*
2bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Copyright (C) 2006 The Android Open Source Project
3bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
4bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * you may not use this file except in compliance with the License.
6bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * You may obtain a copy of the License at
7bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
8bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
10bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Unless required by applicable law or agreed to in writing, software
11bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * See the License for the specific language governing permissions and
14bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * limitations under the License.
15bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */
16bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
17da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brownpackage android.support.v7.internal.view.menu;
18bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
19bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.ActivityNotFoundException;
2030837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.content.Context;
21bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.Intent;
22bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.graphics.drawable.Drawable;
2330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.support.v4.view.ActionProvider;
2430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.support.v4.internal.view.SupportMenuItem;
2530837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.support.v4.view.MenuItemCompat;
26bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.util.Log;
27bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.ContextMenu.ContextMenuInfo;
281ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johnsimport android.view.LayoutInflater;
2930837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.view.MenuItem;
3030837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.view.SubMenu;
31bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.View;
32bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.ViewDebug;
331ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johnsimport android.widget.LinearLayout;
34bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
35bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/**
36bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * @hide
37bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */
3830837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellpublic final class MenuItemImpl implements SupportMenuItem {
3920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns
40ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final String TAG = "MenuItemImpl";
41ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
42ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final int SHOW_AS_ACTION_MASK = SHOW_AS_ACTION_NEVER |
4320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            SHOW_AS_ACTION_IF_ROOM |
4420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            SHOW_AS_ACTION_ALWAYS;
45ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
46ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private final int mId;
47ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private final int mGroup;
48ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private final int mCategoryOrder;
49ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private final int mOrdering;
50ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private CharSequence mTitle;
51ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private CharSequence mTitleCondensed;
52ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Intent mIntent;
53ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private char mShortcutNumericChar;
54ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private char mShortcutAlphabeticChar;
55ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
56ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
57ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * The icon's drawable which is only created as needed
58ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
59ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Drawable mIconDrawable;
6020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns
61ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
6220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * The icon's resource ID which is used to get the Drawable when it is needed (if the Drawable
6320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * isn't already obtained--only one of the two is needed).
64ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
65ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mIconResId = NO_ICON;
66ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
67ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
68ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * The menu to which this item belongs
69ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
70ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private MenuBuilder mMenu;
7120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns
72ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
73ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * If this item should launch a sub menu, this is the sub menu to launch
74ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
75ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private SubMenuBuilder mSubMenu;
76ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
77ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Runnable mItemCallback;
7830837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    private SupportMenuItem.OnMenuItemClickListener mClickListener;
79ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
80ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mFlags = ENABLED;
81ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final int CHECKABLE = 0x00000001;
82ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final int CHECKED = 0x00000002;
83ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final int EXCLUSIVE = 0x00000004;
84ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final int HIDDEN = 0x00000008;
85ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final int ENABLED = 0x00000010;
86ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final int IS_ACTION = 0x00000020;
87ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
88ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mShowAsAction = SHOW_AS_ACTION_NEVER;
89ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
90ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private View mActionView;
91ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private ActionProvider mActionProvider;
9230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    private MenuItemCompat.OnActionExpandListener mOnActionExpandListener;
93ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private boolean mIsActionViewExpanded = false;
94ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
95ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
96ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * Used for the icon resource ID if this item does not have an icon
97ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
98ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    static final int NO_ICON = 0;
99ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
100ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
10120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * Current use case is for context menu: Extra information linked to the View that added this
10220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * item to the context menu.
103ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
104ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private ContextMenuInfo mMenuInfo;
105ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
106ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static String sPrependShortcutLabel;
107ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static String sEnterShortcutLabel;
108ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static String sDeleteShortcutLabel;
109ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static String sSpaceShortcutLabel;
110ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
111ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
112ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
113ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * Instantiates this menu item.
114ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     *
11520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @param group         Item ordering grouping control. The item will be added after all other
11620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *                      items whose order is <= this number, and before any that are larger than
11720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *                      it. This can also be used to define groups of items for batch state
11820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *                      changes. Normally use 0.
119ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * @param id            Unique item ID. Use 0 if you do not need a unique ID.
120ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * @param categoryOrder The ordering for this item.
121ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * @param title         The text to display for the item.
122ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
123ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    MenuItemImpl(MenuBuilder menu, int group, int id, int categoryOrder, int ordering,
12420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            CharSequence title, int showAsAction) {
125ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
126ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        /*if (sPrependShortcutLabel == null) {
127ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani          // This is instantiated from the UI thread, so no chance of sync issues
128ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani          sPrependShortcutLabel = menu.getContext().getResources().getString(
129ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani              com.android.internal.R.string.prepend_shortcut_label);
130ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani          sEnterShortcutLabel = menu.getContext().getResources().getString(
131ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani              com.android.internal.R.string.menu_enter_shortcut_label);
132ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani          sDeleteShortcutLabel = menu.getContext().getResources().getString(
133ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani              com.android.internal.R.string.menu_delete_shortcut_label);
134ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani          sSpaceShortcutLabel = menu.getContext().getResources().getString(
135ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani              com.android.internal.R.string.menu_space_shortcut_label);
136ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }*/
137ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
138ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mMenu = menu;
139ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mId = id;
140ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mGroup = group;
141ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mCategoryOrder = categoryOrder;
142ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mOrdering = ordering;
143ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTitle = title;
144ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mShowAsAction = showAsAction;
145ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
146ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
147ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
148ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * Invokes the item by calling various listeners or callbacks.
149ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     *
150ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * @return true if the invocation was handled, false otherwise
151ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
152ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean invoke() {
153ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mClickListener != null &&
15420ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                mClickListener.onMenuItemClick(this)) {
155ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return true;
156ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
157ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
1581ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        if (mMenu.dispatchMenuItemSelected(mMenu.getRootMenu(), this)) {
1591ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns          return true;
1601ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        }
161ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
162ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mItemCallback != null) {
163ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mItemCallback.run();
164ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return true;
165ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
166ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
167ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIntent != null) {
168ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            try {
1691ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns                mMenu.getContext().startActivity(mIntent);
170ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                return true;
171ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            } catch (ActivityNotFoundException e) {
172ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                Log.e(TAG, "Can't find activity to handle intent; ignoring", e);
173ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
174ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
175ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
176ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mActionProvider != null && mActionProvider.onPerformDefaultAction()) {
177ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return true;
178ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
179ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
180ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return false;
181ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
182ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
1836e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
184ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean isEnabled() {
185ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return (mFlags & ENABLED) != 0;
186ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
187ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
1886e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
189ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setEnabled(boolean enabled) {
190ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (enabled) {
191ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mFlags |= ENABLED;
192ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
193ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mFlags &= ~ENABLED;
194ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
195ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
1961ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemsChanged(false);
197ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
198ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
199ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
200ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2016e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
202ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public int getGroupId() {
203ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mGroup;
204ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
205ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2066e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
207ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @ViewDebug.CapturedViewProperty
208ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public int getItemId() {
209ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mId;
210ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
211ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2126e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
213ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public int getOrder() {
214ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mCategoryOrder;
215ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
216ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
217ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public int getOrdering() {
218ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mOrdering;
219ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
220ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2216e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
222ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public Intent getIntent() {
223ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mIntent;
224ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
225ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2266e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
227ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setIntent(Intent intent) {
228ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mIntent = intent;
229ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
230ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
231ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
232ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    Runnable getCallback() {
233ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mItemCallback;
234ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
235ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
236ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setCallback(Runnable callback) {
237ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mItemCallback = callback;
238ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
239ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
240ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2416e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
242ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public char getAlphabeticShortcut() {
243ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mShortcutAlphabeticChar;
244ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
245ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2466e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
247ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setAlphabeticShortcut(char alphaChar) {
24820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        if (mShortcutAlphabeticChar == alphaChar) {
24920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            return this;
25020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        }
251ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
252ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
253ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2541ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemsChanged(false);
255ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
256ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
257ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
258ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2596e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
260ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public char getNumericShortcut() {
261ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mShortcutNumericChar;
262ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
263ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2646e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
265ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setNumericShortcut(char numericChar) {
26620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        if (mShortcutNumericChar == numericChar) {
26720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            return this;
26820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns        }
269ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
270ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mShortcutNumericChar = numericChar;
271ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2721ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemsChanged(false);
273ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
274ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
275ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
276ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2776e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
278ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setShortcut(char numericChar, char alphaChar) {
279ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mShortcutNumericChar = numericChar;
280ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
281ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
2821ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemsChanged(false);
283ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
284ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
285ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
286ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
287ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
288ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * @return The active shortcut (based on QWERTY-mode of the menu).
289ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
290ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    char getShortcut() {
291ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        //return (mMenu.isQwertyMode() ? mShortcutAlphabeticChar : mShortcutNumericChar);
292ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mShortcutAlphabeticChar;
293ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
294ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
295ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
29620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @return The label to show for the shortcut. This includes the chording key (for example
29720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *         'Menu+a'). Also, any non-human readable characters should be human readable (for
29820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *         example 'Menu+enter').
299ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
300ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    String getShortcutLabel() {
301ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
302ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        char shortcut = getShortcut();
303ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (shortcut == 0) {
304ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return "";
305ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
306ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
307ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        StringBuilder sb = new StringBuilder(sPrependShortcutLabel);
308ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        switch (shortcut) {
309ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
310ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            case '\n':
311ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                sb.append(sEnterShortcutLabel);
312ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                break;
313ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
314ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            case '\b':
315ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                sb.append(sDeleteShortcutLabel);
316ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                break;
317ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
318ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            case ' ':
319ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                sb.append(sSpaceShortcutLabel);
320ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                break;
321bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
322ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            default:
323ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                sb.append(shortcut);
324ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                break;
325ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
326bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
327ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return sb.toString();
328ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
329bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
330ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
33120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @return Whether this menu item should be showing shortcuts (depends on whether the menu
33220ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     *         should show shortcuts and whether this item has a shortcut defined)
333ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
334ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    boolean shouldShowShortcut() {
335ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // Show shortcuts if the menu is supposed to show shortcuts AND this item has a shortcut
336ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mMenu.isShortcutsVisible() && (getShortcut() != 0);
337ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
338bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
3396e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
340ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public SubMenu getSubMenu() {
341ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mSubMenu;
342ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
343bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
3446e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
345ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean hasSubMenu() {
346ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mSubMenu != null;
347ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
348bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
349ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    void setSubMenu(SubMenuBuilder subMenu) {
350ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mSubMenu = subMenu;
351bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
352ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        subMenu.setHeaderTitle(getTitle());
353ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
354bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
3556e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
356ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @ViewDebug.CapturedViewProperty
357ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public CharSequence getTitle() {
358ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mTitle;
359ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
360bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
361ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
36230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell     * Gets the title for a particular {@link MenuView.ItemView}
363ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     *
364ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * @param itemView The ItemView that is receiving the title
36520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * @return Either the title or condensed title based on what the ItemView prefers
366ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
367ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    CharSequence getTitleForItemView(MenuView.ItemView itemView) {
368ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return ((itemView != null) && itemView.prefersCondensedTitle())
36920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                ? getTitleCondensed()
37020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                : getTitle();
371ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
372bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
3736e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
374ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setTitle(CharSequence title) {
375ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTitle = title;
376bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
3771ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemsChanged(false);
378bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
379ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mSubMenu != null) {
380ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mSubMenu.setHeaderTitle(title);
381ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
382bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
383ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
384ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
385ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
3866e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
387ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setTitle(int title) {
388ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return setTitle(mMenu.getContext().getString(title));
389ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
390bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
3916e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
392ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public CharSequence getTitleCondensed() {
393ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mTitleCondensed != null ? mTitleCondensed : mTitle;
394ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
395ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
3966e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
397ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setTitleCondensed(CharSequence title) {
398ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTitleCondensed = title;
399bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
400ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // Could use getTitle() in the loop below, but just cache what it would do here
401ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (title == null) {
402ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            title = mTitle;
403ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
404bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
4051ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemsChanged(false);
406bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
407ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
408ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
409bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
4106e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
411ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public Drawable getIcon() {
412ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconDrawable != null) {
413ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return mIconDrawable;
414ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
415bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
416ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconResId != NO_ICON) {
4171ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns            Drawable icon =  mMenu.getResources().getDrawable(mIconResId);
4181ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns            mIconResId = NO_ICON;
4191ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns            mIconDrawable = icon;
4201ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns            return icon;
421ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
422bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
423ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return null;
424bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
425bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
4266e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
427ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setIcon(Drawable icon) {
428ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mIconResId = NO_ICON;
429ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mIconDrawable = icon;
4301ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemsChanged(false);
431bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
432ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
433ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
434bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
4356e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
436ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setIcon(int iconResId) {
437ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mIconDrawable = null;
438ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mIconResId = iconResId;
439bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
440ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // If we have a view, we need to push the Drawable to them
4411ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemsChanged(false);
442bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
443ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
444bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
445bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
4466e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
447ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean isCheckable() {
448ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return (mFlags & CHECKABLE) == CHECKABLE;
449ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
450bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
4516e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
452ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setCheckable(boolean checkable) {
453ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int oldFlags = mFlags;
454ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
455ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (oldFlags != mFlags) {
4561ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns            mMenu.onItemsChanged(false);
457ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
458bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
459ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
460ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
461bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
462ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setExclusiveCheckable(boolean exclusive) {
463ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
464ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
465bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
466ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean isExclusiveCheckable() {
467ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return (mFlags & EXCLUSIVE) != 0;
468ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
469bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
4706e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
471ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean isChecked() {
472ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return (mFlags & CHECKED) == CHECKED;
473ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
474bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
4756e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
476ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setChecked(boolean checked) {
477ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if ((mFlags & EXCLUSIVE) != 0) {
478ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Call the method on the Menu since it knows about the others in this
479ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // exclusive checkable group
4801ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns            mMenu.setExclusiveItemChecked(this);
481ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
482ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            setCheckedInt(checked);
483ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
484bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
485ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
486ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
487bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
488ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    void setCheckedInt(boolean checked) {
489ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int oldFlags = mFlags;
490ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
491ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (oldFlags != mFlags) {
4921ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns            mMenu.onItemsChanged(false);
493ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
494ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
495bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
4966e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
497ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean isVisible() {
498d713f4f0643a5f6409b431f643f1ad25dba6c22fChris Banes        if (mActionProvider != null && mActionProvider.overridesItemVisibility()) {
499d713f4f0643a5f6409b431f643f1ad25dba6c22fChris Banes            return (mFlags & HIDDEN) == 0 && mActionProvider.isVisible();
500d713f4f0643a5f6409b431f643f1ad25dba6c22fChris Banes        }
501ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return (mFlags & HIDDEN) == 0;
502ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
503bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
504ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
50520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * Changes the visibility of the item. This method DOES NOT notify the parent menu of a change
50620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * in this item, so this should only be called from methods that will eventually trigger this
50720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns     * change.  If unsure, use {@link #setVisible(boolean)} instead.
508ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     *
509ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * @param shown Whether to show (true) or hide (false).
510ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * @return Whether the item's shown state was changed
511ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
512ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    boolean setVisibleInt(boolean shown) {
513ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int oldFlags = mFlags;
514ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mFlags = (mFlags & ~HIDDEN) | (shown ? 0 : HIDDEN);
515ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return oldFlags != mFlags;
516bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
517bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
5186e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
519ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setVisible(boolean shown) {
520ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // Try to set the shown state to the given state. If the shown state was changed
521ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // (i.e. the previous state isn't the same as given state), notify the parent menu that
522ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // the shown state has changed for this item
5231ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        if (setVisibleInt(shown)) mMenu.onItemVisibleChanged(this);
524bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
525ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
526ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
527bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
5286e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
529ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener clickListener) {
530ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mClickListener = clickListener;
531ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
532ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
533bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
534ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
535ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public String toString() {
536ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mTitle.toString();
537ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
538bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
539ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    void setMenuInfo(ContextMenuInfo menuInfo) {
540ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mMenuInfo = menuInfo;
541bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
542bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
5436e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
544ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public ContextMenuInfo getMenuInfo() {
545ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mMenuInfo;
546ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
547bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
548ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void actionFormatChanged() {
5491ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemActionRequestChanged(this);
550ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
551bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
552ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    /**
553ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     * @return Whether the menu should show icons for menu items.
554ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani     */
555ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean shouldShowIcon() {
556ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mMenu.getOptionalIconsVisible();
557bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
558bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
559ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean isActionButton() {
560ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return (mFlags & IS_ACTION) == IS_ACTION;
561bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
562bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
563ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean requestsActionButton() {
564ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return (mShowAsAction & SHOW_AS_ACTION_IF_ROOM) == SHOW_AS_ACTION_IF_ROOM;
565ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
566bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
567ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean requiresActionButton() {
568ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return (mShowAsAction & SHOW_AS_ACTION_ALWAYS) == SHOW_AS_ACTION_ALWAYS;
569ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
570bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
571ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setIsActionButton(boolean isActionButton) {
572ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (isActionButton) {
573ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mFlags |= IS_ACTION;
574ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
575ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mFlags &= ~IS_ACTION;
576ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
577ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
578bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
579ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean showsTextAsAction() {
580ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return (mShowAsAction & SHOW_AS_ACTION_WITH_TEXT) == SHOW_AS_ACTION_WITH_TEXT;
581ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
582bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
5836e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
584ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setShowAsAction(int actionEnum) {
585ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        switch (actionEnum & SHOW_AS_ACTION_MASK) {
586ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            case SHOW_AS_ACTION_ALWAYS:
587ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            case SHOW_AS_ACTION_IF_ROOM:
588ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            case SHOW_AS_ACTION_NEVER:
589ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                // Looks good!
590ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                break;
591ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
592ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            default:
593ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                // Mutually exclusive options selected!
594ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                throw new IllegalArgumentException("SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM,"
59520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                        + " and SHOW_AS_ACTION_NEVER are mutually exclusive.");
596ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
597ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mShowAsAction = actionEnum;
5981ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemActionRequestChanged(this);
599ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
600bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
6016e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
60230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    public SupportMenuItem setActionView(View view) {
603ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mActionView = view;
604ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mActionProvider = null;
605ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (view != null && view.getId() == View.NO_ID && mId > 0) {
606ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            view.setId(mId);
607ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
6081ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemActionRequestChanged(this);
609ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
610bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
611bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
6126e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
61330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    public SupportMenuItem setActionView(int resId) {
6141ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        final Context context = mMenu.getContext();
6151ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        final LayoutInflater inflater = LayoutInflater.from(context);
6161ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        setActionView(inflater.inflate(resId, new LinearLayout(context), false));
617ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
618ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
619ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
6206e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
621ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public View getActionView() {
622ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mActionView != null) {
623ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return mActionView;
624ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else if (mActionProvider != null) {
62534452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell            mActionView = mActionProvider.onCreateActionView(this);
626ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return mActionView;
627ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
628ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return null;
629ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
630ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
631ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
63230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    @Override
63330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    public MenuItem setActionProvider(android.view.ActionProvider actionProvider) {
63430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        throw new UnsupportedOperationException(
63530837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell                "Implementation should use setSupportActionProvider!");
63630837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    }
63730837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell
63830837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    @Override
63930837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    public android.view.ActionProvider getActionProvider() {
64030837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        throw new UnsupportedOperationException(
64130837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell                "Implementation should use getSupportActionProvider!");
64230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    }
64330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell
6446e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
64530837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    public ActionProvider getSupportActionProvider() {
646ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mActionProvider;
647ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
648ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
6496e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
65030837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    public SupportMenuItem setSupportActionProvider(ActionProvider actionProvider) {
65134452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        if (mActionProvider == actionProvider) {
65234452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell            return this;
65334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        }
65434452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell
655ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mActionView = null;
65634452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        if (mActionProvider != null) {
65734452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell            mActionProvider.setVisibilityListener(null);
65834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        }
659ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mActionProvider = actionProvider;
6601ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemsChanged(true); // Measurement can be changed
66134452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        if (actionProvider != null) {
66234452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell            actionProvider.setVisibilityListener(new ActionProvider.VisibilityListener() {
66334452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell                @Override
66434452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell                public void onActionProviderVisibilityChanged(boolean isVisible) {
665d713f4f0643a5f6409b431f643f1ad25dba6c22fChris Banes                    mMenu.onItemVisibleChanged(MenuItemImpl.this);
66634452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell                }
66734452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell            });
66834452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell        }
669ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
670ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
671bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
672ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
67330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    public SupportMenuItem setShowAsActionFlags(int actionEnum) {
674ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setShowAsAction(actionEnum);
675ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
676bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
677bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
678ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
679ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean expandActionView() {
680ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) == 0 || mActionView == null) {
681ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return false;
682ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
683bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
684ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mOnActionExpandListener == null ||
68520ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                mOnActionExpandListener.onMenuItemActionExpand(this)) {
6861ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns            return mMenu.expandItemActionView(this);
687ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
688ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
689ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return false;
690ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
691ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
692ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
693ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean collapseActionView() {
694ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) == 0) {
695ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return false;
696ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
697ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mActionView == null) {
698ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // We're already collapsed if we have no action view.
699ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return true;
700ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
701ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
702ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mOnActionExpandListener == null ||
70320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                mOnActionExpandListener.onMenuItemActionCollapse(this)) {
704ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return mMenu.collapseItemActionView(this);
705ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
706ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
707ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return false;
708ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
709ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
7106e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
7116e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    public SupportMenuItem setSupportOnActionExpandListener(
7126e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown            MenuItemCompat.OnActionExpandListener listener) {
713ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mOnActionExpandListener = listener;
714ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return this;
715ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
716ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
717ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean hasCollapsibleActionView() {
718ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return (mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) != 0 && mActionView != null;
719ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
720ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
721ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setActionViewExpanded(boolean isExpanded) {
722ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mIsActionViewExpanded = isExpanded;
7231ead4e393273917ffa2bc27e7624ce359ba955f6Trevor Johns        mMenu.onItemsChanged(false);
724ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
725ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
7266e6abe6eb358db4c3932752249c8ac61dd3dab21Jeff Brown    @Override
727ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean isActionViewExpanded() {
728ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mIsActionViewExpanded;
729ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
73030837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell
73130837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    @Override
73230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    public MenuItem setOnActionExpandListener(MenuItem.OnActionExpandListener listener) {
73330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        throw new UnsupportedOperationException(
73430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell                "Implementation should use setSupportOnActionExpandListener!");
73530837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell    }
736bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell}
737