blob: ed676bb703e9dbb7ec55020287bc79ab97204aa4 [file] [log] [blame]
Adam Powell33b97432010-04-20 10:01:14 -07001/*
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 com.android.internal.view.menu;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.drawable.Drawable;
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -070022import android.view.ActionProvider;
Adam Powellcf78b3e2010-09-12 18:25:23 -070023import android.view.ContextMenu.ContextMenuInfo;
Adam Powell33b97432010-04-20 10:01:14 -070024import android.view.MenuItem;
25import android.view.SubMenu;
Adam Powellcf78b3e2010-09-12 18:25:23 -070026import android.view.View;
Adam Powell33b97432010-04-20 10:01:14 -070027
28/**
29 * @hide
30 */
31public 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
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
69 public char getAlphabeticShortcut() {
70 return mShortcutAlphabeticChar;
71 }
72
73 public int getGroupId() {
74 return mGroup;
75 }
76
77 public Drawable getIcon() {
78 return mIconDrawable;
79 }
80
81 public Intent getIntent() {
82 return mIntent;
83 }
84
85 public int getItemId() {
86 return mId;
87 }
88
89 public ContextMenuInfo getMenuInfo() {
90 return null;
91 }
92
93 public char getNumericShortcut() {
94 return mShortcutNumericChar;
95 }
96
97 public int getOrder() {
98 return mOrdering;
99 }
100
101 public SubMenu getSubMenu() {
102 return null;
103 }
104
105 public CharSequence getTitle() {
106 return mTitle;
107 }
108
109 public CharSequence getTitleCondensed() {
Adam Powelle0e2f4f2013-04-05 16:27:35 -0700110 return mTitleCondensed != null ? mTitleCondensed : mTitle;
Adam Powell33b97432010-04-20 10:01:14 -0700111 }
112
113 public boolean hasSubMenu() {
114 return false;
115 }
116
117 public boolean isCheckable() {
118 return (mFlags & CHECKABLE) != 0;
119 }
120
121 public boolean isChecked() {
122 return (mFlags & CHECKED) != 0;
123 }
124
125 public boolean isEnabled() {
126 return (mFlags & ENABLED) != 0;
127 }
128
129 public boolean isVisible() {
130 return (mFlags & HIDDEN) == 0;
131 }
132
133 public MenuItem setAlphabeticShortcut(char alphaChar) {
134 mShortcutAlphabeticChar = alphaChar;
135 return this;
136 }
137
138 public MenuItem setCheckable(boolean checkable) {
139 mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
140 return this;
141 }
142
143 public ActionMenuItem setExclusiveCheckable(boolean exclusive) {
144 mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
145 return this;
146 }
147
148 public MenuItem setChecked(boolean checked) {
149 mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
150 return this;
151 }
152
153 public MenuItem setEnabled(boolean enabled) {
154 mFlags = (mFlags & ~ENABLED) | (enabled ? ENABLED : 0);
155 return this;
156 }
157
158 public MenuItem setIcon(Drawable icon) {
159 mIconDrawable = icon;
160 mIconResId = NO_ICON;
161 return this;
162 }
163
164 public MenuItem setIcon(int iconRes) {
165 mIconResId = iconRes;
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800166 mIconDrawable = mContext.getDrawable(iconRes);
Adam Powell33b97432010-04-20 10:01:14 -0700167 return this;
168 }
169
170 public MenuItem setIntent(Intent intent) {
171 mIntent = intent;
172 return this;
173 }
174
175 public MenuItem setNumericShortcut(char numericChar) {
176 mShortcutNumericChar = numericChar;
177 return this;
178 }
179
180 public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {
181 mClickListener = menuItemClickListener;
182 return this;
183 }
184
185 public MenuItem setShortcut(char numericChar, char alphaChar) {
186 mShortcutNumericChar = numericChar;
187 mShortcutAlphabeticChar = alphaChar;
188 return this;
189 }
190
191 public MenuItem setTitle(CharSequence title) {
192 mTitle = title;
193 return this;
194 }
195
196 public MenuItem setTitle(int title) {
197 mTitle = mContext.getResources().getString(title);
198 return this;
199 }
200
201 public MenuItem setTitleCondensed(CharSequence title) {
202 mTitleCondensed = title;
203 return this;
204 }
205
206 public MenuItem setVisible(boolean visible) {
207 mFlags = (mFlags & HIDDEN) | (visible ? 0 : HIDDEN);
208 return this;
209 }
210
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 }
Adam Powell89e06452010-06-23 20:24:52 -0700223
224 public void setShowAsAction(int show) {
225 // Do nothing. ActionMenuItems always show as action buttons.
226 }
Adam Powellcf78b3e2010-09-12 18:25:23 -0700227
228 public MenuItem setActionView(View actionView) {
229 throw new UnsupportedOperationException();
230 }
231
232 public View getActionView() {
233 return null;
234 }
Adam Powell3f476b32011-01-03 19:25:36 -0800235
236 @Override
237 public MenuItem setActionView(int resId) {
238 throw new UnsupportedOperationException();
239 }
Adam Powell8d02dea2011-05-31 21:35:13 -0700240
241 @Override
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700242 public ActionProvider getActionProvider() {
243 return null;
244 }
245
246 @Override
247 public MenuItem setActionProvider(ActionProvider actionProvider) {
248 throw new UnsupportedOperationException();
249 }
250
251 @Override
Adam Powell8d02dea2011-05-31 21:35:13 -0700252 public MenuItem setShowAsActionFlags(int actionEnum) {
253 setShowAsAction(actionEnum);
254 return this;
255 }
256
257 @Override
258 public boolean expandActionView() {
259 return false;
260 }
261
262 @Override
263 public boolean collapseActionView() {
264 return false;
265 }
266
267 @Override
268 public boolean isActionViewExpanded() {
269 return false;
270 }
271
272 @Override
273 public MenuItem setOnActionExpandListener(OnActionExpandListener listener) {
274 // No need to save the listener; ActionMenuItem does not support collapsing items.
275 return this;
276 }
Adam Powell33b97432010-04-20 10:01:14 -0700277}