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