ActionMenuItem.java revision da10fdd1400ecfd8d7f2e55651dd528d0614dfc5
1/*
2 * Copyright (C) 2010 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 android.support.v7.internal.view.menu;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.drawable.Drawable;
22import android.support.v7.view.ActionProvider;
23import android.support.v7.view.MenuItem;
24import android.support.v7.view.SubMenu;
25import android.view.ContextMenu.ContextMenuInfo;
26import android.view.View;
27
28/**
29 * @hide
30 */
31public class ActionMenuItem implements MenuItem {
32
33    private final int mId;
34    private final int mGroup;
35    private final int mCategoryOrder;
36    private final int mOrdering;
37
38    private CharSequence mTitle;
39    private CharSequence mTitleCondensed;
40    private Intent mIntent;
41    private char mShortcutNumericChar;
42    private char mShortcutAlphabeticChar;
43
44    private Drawable mIconDrawable;
45    private int mIconResId = NO_ICON;
46
47    private Context mContext;
48
49    private MenuItem.OnMenuItemClickListener mClickListener;
50
51    private static final int NO_ICON = 0;
52
53    private int mFlags = ENABLED;
54    private static final int CHECKABLE = 0x00000001;
55    private static final int CHECKED = 0x00000002;
56    private static final int EXCLUSIVE = 0x00000004;
57    private static final int HIDDEN = 0x00000008;
58    private static final int ENABLED = 0x00000010;
59
60    public ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering,
61            CharSequence title) {
62        mContext = context;
63        mId = id;
64        mGroup = group;
65        mCategoryOrder = categoryOrder;
66        mOrdering = ordering;
67        mTitle = title;
68    }
69
70    public char getAlphabeticShortcut() {
71        return mShortcutAlphabeticChar;
72    }
73
74    public int getGroupId() {
75        return mGroup;
76    }
77
78    public Drawable getIcon() {
79        return mIconDrawable;
80    }
81
82    public Intent getIntent() {
83        return mIntent;
84    }
85
86    public int getItemId() {
87        return mId;
88    }
89
90    public ContextMenuInfo getMenuInfo() {
91        return null;
92    }
93
94    public char getNumericShortcut() {
95        return mShortcutNumericChar;
96    }
97
98    public int getOrder() {
99        return mOrdering;
100    }
101
102    public SubMenu getSubMenu() {
103        return null;
104    }
105
106    public CharSequence getTitle() {
107        return mTitle;
108    }
109
110    public CharSequence getTitleCondensed() {
111        return mTitleCondensed;
112    }
113
114    public boolean hasSubMenu() {
115        return false;
116    }
117
118    public boolean isCheckable() {
119        return (mFlags & CHECKABLE) != 0;
120    }
121
122    public boolean isChecked() {
123        return (mFlags & CHECKED) != 0;
124    }
125
126    public boolean isEnabled() {
127        return (mFlags & ENABLED) != 0;
128    }
129
130    public boolean isVisible() {
131        return (mFlags & HIDDEN) == 0;
132    }
133
134    public MenuItem setAlphabeticShortcut(char alphaChar) {
135        mShortcutAlphabeticChar = alphaChar;
136        return this;
137    }
138
139    public MenuItem setCheckable(boolean checkable) {
140        mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
141        return this;
142    }
143
144    public ActionMenuItem setExclusiveCheckable(boolean exclusive) {
145        mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
146        return this;
147    }
148
149    public MenuItem setChecked(boolean checked) {
150        mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
151        return this;
152    }
153
154    public MenuItem setEnabled(boolean enabled) {
155        mFlags = (mFlags & ~ENABLED) | (enabled ? ENABLED : 0);
156        return this;
157    }
158
159    public MenuItem setIcon(Drawable icon) {
160        mIconDrawable = icon;
161        mIconResId = NO_ICON;
162        return this;
163    }
164
165    public MenuItem setIcon(int iconRes) {
166        mIconResId = iconRes;
167        mIconDrawable = mContext.getResources().getDrawable(iconRes);
168        return this;
169    }
170
171    public MenuItem setIntent(Intent intent) {
172        mIntent = intent;
173        return this;
174    }
175
176    public MenuItem setNumericShortcut(char numericChar) {
177        mShortcutNumericChar = numericChar;
178        return this;
179    }
180
181    public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {
182        mClickListener = menuItemClickListener;
183        return this;
184    }
185
186    public MenuItem setShortcut(char numericChar, char alphaChar) {
187        mShortcutNumericChar = numericChar;
188        mShortcutAlphabeticChar = alphaChar;
189        return this;
190    }
191
192    public MenuItem setTitle(CharSequence title) {
193        mTitle = title;
194        return this;
195    }
196
197    public MenuItem setTitle(int title) {
198        mTitle = mContext.getResources().getString(title);
199        return this;
200    }
201
202    public MenuItem setTitleCondensed(CharSequence title) {
203        mTitleCondensed = title;
204        return this;
205    }
206
207    public MenuItem setVisible(boolean visible) {
208        mFlags = (mFlags & HIDDEN) | (visible ? 0 : HIDDEN);
209        return this;
210    }
211
212    public boolean invoke() {
213        if (mClickListener != null && mClickListener.onMenuItemClick(this)) {
214            return true;
215        }
216
217        if (mIntent != null) {
218            mContext.startActivity(mIntent);
219            return true;
220        }
221
222        return false;
223    }
224
225    public void setShowAsAction(int show) {
226        // Do nothing. ActionMenuItems always show as action buttons.
227    }
228
229    public MenuItem setActionView(View actionView) {
230        throw new UnsupportedOperationException();
231    }
232
233    public View getActionView() {
234        return null;
235    }
236
237    @Override
238    public MenuItem setActionView(int resId) {
239        throw new UnsupportedOperationException();
240    }
241
242    @Override
243    public ActionProvider getActionProvider() {
244        return null;
245    }
246
247    @Override
248    public MenuItem setActionProvider(ActionProvider actionProvider) {
249        throw new UnsupportedOperationException();
250    }
251
252    @Override
253    public MenuItem setShowAsActionFlags(int actionEnum) {
254        setShowAsAction(actionEnum);
255        return this;
256    }
257
258    @Override
259    public boolean expandActionView() {
260        return false;
261    }
262
263    @Override
264    public boolean collapseActionView() {
265        return false;
266    }
267
268    @Override
269    public boolean isActionViewExpanded() {
270        return false;
271    }
272
273    @Override
274    public MenuItem setOnActionExpandListener(OnActionExpandListener listener) {
275        // No need to save the listener; ActionMenuItem does not support collapsing items.
276        return this;
277    }
278}
279