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.annotation.Nullable; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.res.ColorStateList; 23 import android.graphics.PorterDuff; 24 import android.graphics.drawable.Drawable; 25 import android.view.ActionProvider; 26 import android.view.ContextMenu.ContextMenuInfo; 27 import android.view.KeyEvent; 28 import android.view.MenuItem; 29 import android.view.SubMenu; 30 import android.view.View; 31 32 /** 33 * @hide 34 */ 35 public class ActionMenuItem implements MenuItem { 36 private final int mId; 37 private final int mGroup; 38 private final int mCategoryOrder; 39 private final int mOrdering; 40 41 private CharSequence mTitle; 42 private CharSequence mTitleCondensed; 43 private Intent mIntent; 44 private char mShortcutNumericChar; 45 private int mShortcutNumericModifiers = KeyEvent.META_CTRL_ON; 46 private char mShortcutAlphabeticChar; 47 private int mShortcutAlphabeticModifiers = KeyEvent.META_CTRL_ON; 48 49 private Drawable mIconDrawable; 50 private int mIconResId = NO_ICON; 51 private ColorStateList mIconTintList = null; 52 private PorterDuff.Mode mIconTintMode = null; 53 private boolean mHasIconTint = false; 54 private boolean mHasIconTintMode = false; 55 56 private Context mContext; 57 58 private MenuItem.OnMenuItemClickListener mClickListener; 59 60 private CharSequence mContentDescription; 61 private CharSequence mTooltipText; 62 63 private static final int NO_ICON = 0; 64 65 private int mFlags = ENABLED; 66 private static final int CHECKABLE = 0x00000001; 67 private static final int CHECKED = 0x00000002; 68 private static final int EXCLUSIVE = 0x00000004; 69 private static final int HIDDEN = 0x00000008; 70 private static final int ENABLED = 0x00000010; 71 ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering, CharSequence title)72 public ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering, 73 CharSequence title) { 74 mContext = context; 75 mId = id; 76 mGroup = group; 77 mCategoryOrder = categoryOrder; 78 mOrdering = ordering; 79 mTitle = title; 80 } 81 getAlphabeticShortcut()82 public char getAlphabeticShortcut() { 83 return mShortcutAlphabeticChar; 84 } 85 getAlphabeticModifiers()86 public int getAlphabeticModifiers() { 87 return mShortcutAlphabeticModifiers; 88 } 89 getGroupId()90 public int getGroupId() { 91 return mGroup; 92 } 93 getIcon()94 public Drawable getIcon() { 95 return mIconDrawable; 96 } 97 getIntent()98 public Intent getIntent() { 99 return mIntent; 100 } 101 getItemId()102 public int getItemId() { 103 return mId; 104 } 105 getMenuInfo()106 public ContextMenuInfo getMenuInfo() { 107 return null; 108 } 109 getNumericShortcut()110 public char getNumericShortcut() { 111 return mShortcutNumericChar; 112 } 113 getNumericModifiers()114 public int getNumericModifiers() { 115 return mShortcutNumericModifiers; 116 } 117 getOrder()118 public int getOrder() { 119 return mOrdering; 120 } 121 getSubMenu()122 public SubMenu getSubMenu() { 123 return null; 124 } 125 getTitle()126 public CharSequence getTitle() { 127 return mTitle; 128 } 129 getTitleCondensed()130 public CharSequence getTitleCondensed() { 131 return mTitleCondensed != null ? mTitleCondensed : mTitle; 132 } 133 hasSubMenu()134 public boolean hasSubMenu() { 135 return false; 136 } 137 isCheckable()138 public boolean isCheckable() { 139 return (mFlags & CHECKABLE) != 0; 140 } 141 isChecked()142 public boolean isChecked() { 143 return (mFlags & CHECKED) != 0; 144 } 145 isEnabled()146 public boolean isEnabled() { 147 return (mFlags & ENABLED) != 0; 148 } 149 isVisible()150 public boolean isVisible() { 151 return (mFlags & HIDDEN) == 0; 152 } 153 setAlphabeticShortcut(char alphaChar)154 public MenuItem setAlphabeticShortcut(char alphaChar) { 155 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar); 156 return this; 157 } 158 setAlphabeticShortcut(char alphachar, int alphaModifiers)159 public MenuItem setAlphabeticShortcut(char alphachar, int alphaModifiers) { 160 mShortcutAlphabeticChar = Character.toLowerCase(alphachar); 161 mShortcutAlphabeticModifiers = KeyEvent.normalizeMetaState(alphaModifiers); 162 return this; 163 } 164 setCheckable(boolean checkable)165 public MenuItem setCheckable(boolean checkable) { 166 mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0); 167 return this; 168 } 169 setExclusiveCheckable(boolean exclusive)170 public ActionMenuItem setExclusiveCheckable(boolean exclusive) { 171 mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0); 172 return this; 173 } 174 setChecked(boolean checked)175 public MenuItem setChecked(boolean checked) { 176 mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0); 177 return this; 178 } 179 setEnabled(boolean enabled)180 public MenuItem setEnabled(boolean enabled) { 181 mFlags = (mFlags & ~ENABLED) | (enabled ? ENABLED : 0); 182 return this; 183 } 184 setIcon(Drawable icon)185 public MenuItem setIcon(Drawable icon) { 186 mIconDrawable = icon; 187 mIconResId = NO_ICON; 188 189 applyIconTint(); 190 return this; 191 } 192 setIcon(int iconRes)193 public MenuItem setIcon(int iconRes) { 194 mIconResId = iconRes; 195 mIconDrawable = mContext.getDrawable(iconRes); 196 197 applyIconTint(); 198 return this; 199 } 200 201 @Override setIconTintList(@ullable ColorStateList iconTintList)202 public MenuItem setIconTintList(@Nullable ColorStateList iconTintList) { 203 mIconTintList = iconTintList; 204 mHasIconTint = true; 205 206 applyIconTint(); 207 208 return this; 209 } 210 211 @Nullable 212 @Override getIconTintList()213 public ColorStateList getIconTintList() { 214 return mIconTintList; 215 } 216 217 @Override setIconTintMode(PorterDuff.Mode iconTintMode)218 public MenuItem setIconTintMode(PorterDuff.Mode iconTintMode) { 219 mIconTintMode = iconTintMode; 220 mHasIconTintMode = true; 221 222 applyIconTint(); 223 224 return this; 225 } 226 227 @Nullable 228 @Override getIconTintMode()229 public PorterDuff.Mode getIconTintMode() { 230 return mIconTintMode; 231 } 232 applyIconTint()233 private void applyIconTint() { 234 if (mIconDrawable != null && (mHasIconTint || mHasIconTintMode)) { 235 mIconDrawable = mIconDrawable.mutate(); 236 237 if (mHasIconTint) { 238 mIconDrawable.setTintList(mIconTintList); 239 } 240 241 if (mHasIconTintMode) { 242 mIconDrawable.setTintMode(mIconTintMode); 243 } 244 } 245 } 246 setIntent(Intent intent)247 public MenuItem setIntent(Intent intent) { 248 mIntent = intent; 249 return this; 250 } 251 setNumericShortcut(char numericChar)252 public MenuItem setNumericShortcut(char numericChar) { 253 mShortcutNumericChar = numericChar; 254 return this; 255 } 256 setNumericShortcut(char numericChar, int numericModifiers)257 public MenuItem setNumericShortcut(char numericChar, int numericModifiers) { 258 mShortcutNumericChar = numericChar; 259 mShortcutNumericModifiers = KeyEvent.normalizeMetaState(numericModifiers); 260 return this; 261 } 262 setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener)263 public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) { 264 mClickListener = menuItemClickListener; 265 return this; 266 } 267 setShortcut(char numericChar, char alphaChar)268 public MenuItem setShortcut(char numericChar, char alphaChar) { 269 mShortcutNumericChar = numericChar; 270 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar); 271 return this; 272 } 273 setShortcut(char numericChar, char alphaChar, int numericModifiers, int alphaModifiers)274 public MenuItem setShortcut(char numericChar, char alphaChar, int numericModifiers, 275 int alphaModifiers) { 276 mShortcutNumericChar = numericChar; 277 mShortcutNumericModifiers = KeyEvent.normalizeMetaState(numericModifiers); 278 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar); 279 mShortcutAlphabeticModifiers = KeyEvent.normalizeMetaState(alphaModifiers); 280 return this; 281 } 282 setTitle(CharSequence title)283 public MenuItem setTitle(CharSequence title) { 284 mTitle = title; 285 return this; 286 } 287 setTitle(int title)288 public MenuItem setTitle(int title) { 289 mTitle = mContext.getResources().getString(title); 290 return this; 291 } 292 setTitleCondensed(CharSequence title)293 public MenuItem setTitleCondensed(CharSequence title) { 294 mTitleCondensed = title; 295 return this; 296 } 297 setVisible(boolean visible)298 public MenuItem setVisible(boolean visible) { 299 mFlags = (mFlags & HIDDEN) | (visible ? 0 : HIDDEN); 300 return this; 301 } 302 invoke()303 public boolean invoke() { 304 if (mClickListener != null && mClickListener.onMenuItemClick(this)) { 305 return true; 306 } 307 308 if (mIntent != null) { 309 mContext.startActivity(mIntent); 310 return true; 311 } 312 313 return false; 314 } 315 setShowAsAction(int show)316 public void setShowAsAction(int show) { 317 // Do nothing. ActionMenuItems always show as action buttons. 318 } 319 setActionView(View actionView)320 public MenuItem setActionView(View actionView) { 321 throw new UnsupportedOperationException(); 322 } 323 getActionView()324 public View getActionView() { 325 return null; 326 } 327 328 @Override setActionView(int resId)329 public MenuItem setActionView(int resId) { 330 throw new UnsupportedOperationException(); 331 } 332 333 @Override getActionProvider()334 public ActionProvider getActionProvider() { 335 return null; 336 } 337 338 @Override setActionProvider(ActionProvider actionProvider)339 public MenuItem setActionProvider(ActionProvider actionProvider) { 340 throw new UnsupportedOperationException(); 341 } 342 343 @Override setShowAsActionFlags(int actionEnum)344 public MenuItem setShowAsActionFlags(int actionEnum) { 345 setShowAsAction(actionEnum); 346 return this; 347 } 348 349 @Override expandActionView()350 public boolean expandActionView() { 351 return false; 352 } 353 354 @Override collapseActionView()355 public boolean collapseActionView() { 356 return false; 357 } 358 359 @Override isActionViewExpanded()360 public boolean isActionViewExpanded() { 361 return false; 362 } 363 364 @Override setOnActionExpandListener(OnActionExpandListener listener)365 public MenuItem setOnActionExpandListener(OnActionExpandListener listener) { 366 // No need to save the listener; ActionMenuItem does not support collapsing items. 367 return this; 368 } 369 370 @Override setContentDescription(CharSequence contentDescription)371 public MenuItem setContentDescription(CharSequence contentDescription) { 372 mContentDescription = contentDescription; 373 return this; 374 } 375 376 @Override getContentDescription()377 public CharSequence getContentDescription() { 378 return mContentDescription; 379 } 380 381 @Override setTooltipText(CharSequence tooltipText)382 public MenuItem setTooltipText(CharSequence tooltipText) { 383 mTooltipText = tooltipText; 384 return this; 385 } 386 387 @Override getTooltipText()388 public CharSequence getTooltipText() { 389 return mTooltipText; 390 } 391 } 392