2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.actionbarsherlock.internal.view.menu;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.graphics.drawable.Drawable;
22 import android.view.ContextMenu.ContextMenuInfo;
23 import android.view.View;
25 import com.actionbarsherlock.view.ActionProvider;
26 import com.actionbarsherlock.view.MenuItem;
27 import com.actionbarsherlock.view.SubMenu;
32 public class ActionMenuItem implements MenuItem {
33 private final int mId;
34 private final int mGroup;
35 //UNUSED private final int mCategoryOrder;
36 private final int mOrdering;
38 private CharSequence mTitle;
39 private CharSequence mTitleCondensed;
40 private Intent mIntent;
41 private char mShortcutNumericChar;
42 private char mShortcutAlphabeticChar;
44 private Drawable mIconDrawable;
45 //UNUSED private int mIconResId = NO_ICON;
47 private Context mContext;
49 private MenuItem.OnMenuItemClickListener mClickListener;
51 //UNUSED private static final int NO_ICON = 0;
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;
60 public ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering,
65 //UNUSED mCategoryOrder = categoryOrder;
70 public char getAlphabeticShortcut() {
71 return mShortcutAlphabeticChar;
74 public int getGroupId() {
78 public Drawable getIcon() {
82 public Intent getIntent() {
86 public int getItemId() {
90 public ContextMenuInfo getMenuInfo() {
94 public char getNumericShortcut() {
95 return mShortcutNumericChar;
98 public int getOrder() {
102 public SubMenu getSubMenu() {
106 public CharSequence getTitle() {
110 public CharSequence getTitleCondensed() {
111 return mTitleCondensed;
114 public boolean hasSubMenu() {
118 public boolean isCheckable() {
119 return (mFlags & CHECKABLE) != 0;
122 public boolean isChecked() {
123 return (mFlags & CHECKED) != 0;
126 public boolean isEnabled() {
127 return (mFlags & ENABLED) != 0;
130 public boolean isVisible() {
131 return (mFlags & HIDDEN) == 0;
134 public MenuItem setAlphabeticShortcut(char alphaChar) {
135 mShortcutAlphabeticChar = alphaChar;
139 public MenuItem setCheckable(boolean checkable) {
140 mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
144 public ActionMenuItem setExclusiveCheckable(boolean exclusive) {
145 mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
149 public MenuItem setChecked(boolean checked) {
150 mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
154 public MenuItem setEnabled(boolean enabled) {
155 mFlags = (mFlags & ~ENABLED) | (enabled ? ENABLED : 0);
159 public MenuItem setIcon(Drawable icon) {
160 mIconDrawable = icon;
161 //UNUSED mIconResId = NO_ICON;
165 public MenuItem setIcon(int iconRes) {
166 //UNUSED mIconResId = iconRes;
167 mIconDrawable = mContext.getResources().getDrawable(iconRes);
171 public MenuItem setIntent(Intent intent) {
176 public MenuItem setNumericShortcut(char numericChar) {
177 mShortcutNumericChar = numericChar;
181 public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) {
182 mClickListener = menuItemClickListener;
186 public MenuItem setShortcut(char numericChar, char alphaChar) {
187 mShortcutNumericChar = numericChar;
188 mShortcutAlphabeticChar = alphaChar;
192 public MenuItem setTitle(CharSequence title) {
197 public MenuItem setTitle(int title) {
198 mTitle = mContext.getResources().getString(title);
202 public MenuItem setTitleCondensed(CharSequence title) {
203 mTitleCondensed = title;
207 public MenuItem setVisible(boolean visible) {
208 mFlags = (mFlags & HIDDEN) | (visible ? 0 : HIDDEN);
212 public boolean invoke() {
213 if (mClickListener != null && mClickListener.onMenuItemClick(this)) {
217 if (mIntent != null) {
218 mContext.startActivity(mIntent);
225 public void setShowAsAction(int show) {
226 // Do nothing. ActionMenuItems always show as action buttons.
229 public MenuItem setActionView(View actionView) {
230 throw new UnsupportedOperationException();
233 public View getActionView() {
238 public MenuItem setActionView(int resId) {
239 throw new UnsupportedOperationException();
243 public ActionProvider getActionProvider() {
248 public MenuItem setActionProvider(ActionProvider actionProvider) {
249 throw new UnsupportedOperationException();
253 public MenuItem setShowAsActionFlags(int actionEnum) {
254 setShowAsAction(actionEnum);
259 public boolean expandActionView() {
264 public boolean collapseActionView() {
269 public boolean isActionViewExpanded() {
274 public MenuItem setOnActionExpandListener(OnActionExpandListener listener) {
275 // No need to save the listener; ActionMenuItem does not support collapsing items.