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