2 * Copyright (C) 2006 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.ActivityNotFoundException;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.graphics.drawable.Drawable;
23 import android.util.Log;
24 import android.view.ContextMenu.ContextMenuInfo;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewDebug;
28 import android.widget.LinearLayout;
30 import com.actionbarsherlock.view.ActionProvider;
31 import com.actionbarsherlock.view.MenuItem;
32 import com.actionbarsherlock.view.SubMenu;
37 public final class MenuItemImpl implements MenuItem {
38 private static final String TAG = "MenuItemImpl";
40 private static final int SHOW_AS_ACTION_MASK = SHOW_AS_ACTION_NEVER |
41 SHOW_AS_ACTION_IF_ROOM |
42 SHOW_AS_ACTION_ALWAYS;
44 private final int mId;
45 private final int mGroup;
46 private final int mCategoryOrder;
47 private final int mOrdering;
48 private CharSequence mTitle;
49 private CharSequence mTitleCondensed;
50 private Intent mIntent;
51 private char mShortcutNumericChar;
52 private char mShortcutAlphabeticChar;
54 /** The icon's drawable which is only created as needed */
55 private Drawable mIconDrawable;
57 * The icon's resource ID which is used to get the Drawable when it is
58 * needed (if the Drawable isn't already obtained--only one of the two is
61 private int mIconResId = NO_ICON;
63 /** The menu to which this item belongs */
64 private MenuBuilder mMenu;
65 /** If this item should launch a sub menu, this is the sub menu to launch */
66 private SubMenuBuilder mSubMenu;
68 private Runnable mItemCallback;
69 private MenuItem.OnMenuItemClickListener mClickListener;
71 private int mFlags = ENABLED;
72 private static final int CHECKABLE = 0x00000001;
73 private static final int CHECKED = 0x00000002;
74 private static final int EXCLUSIVE = 0x00000004;
75 private static final int HIDDEN = 0x00000008;
76 private static final int ENABLED = 0x00000010;
77 private static final int IS_ACTION = 0x00000020;
79 private int mShowAsAction = SHOW_AS_ACTION_NEVER;
81 private View mActionView;
82 private ActionProvider mActionProvider;
83 private OnActionExpandListener mOnActionExpandListener;
84 private boolean mIsActionViewExpanded = false;
86 /** Used for the icon resource ID if this item does not have an icon */
87 static final int NO_ICON = 0;
90 * Current use case is for context menu: Extra information linked to the
91 * View that added this item to the context menu.
93 private ContextMenuInfo mMenuInfo;
95 private static String sPrependShortcutLabel;
96 private static String sEnterShortcutLabel;
97 private static String sDeleteShortcutLabel;
98 private static String sSpaceShortcutLabel;
102 * Instantiates this menu item.
105 * @param group Item ordering grouping control. The item will be added after
106 * all other items whose order is <= this number, and before any
107 * that are larger than it. This can also be used to define
108 * groups of items for batch state changes. Normally use 0.
109 * @param id Unique item ID. Use 0 if you do not need a unique ID.
110 * @param categoryOrder The ordering for this item.
111 * @param title The text to display for the item.
113 MenuItemImpl(MenuBuilder menu, int group, int id, int categoryOrder, int ordering,
114 CharSequence title, int showAsAction) {
116 /* TODO if (sPrependShortcutLabel == null) {
117 // This is instantiated from the UI thread, so no chance of sync issues
118 sPrependShortcutLabel = menu.getContext().getResources().getString(
119 com.android.internal.R.string.prepend_shortcut_label);
120 sEnterShortcutLabel = menu.getContext().getResources().getString(
121 com.android.internal.R.string.menu_enter_shortcut_label);
122 sDeleteShortcutLabel = menu.getContext().getResources().getString(
123 com.android.internal.R.string.menu_delete_shortcut_label);
124 sSpaceShortcutLabel = menu.getContext().getResources().getString(
125 com.android.internal.R.string.menu_space_shortcut_label);
131 mCategoryOrder = categoryOrder;
132 mOrdering = ordering;
134 mShowAsAction = showAsAction;
138 * Invokes the item by calling various listeners or callbacks.
140 * @return true if the invocation was handled, false otherwise
142 public boolean invoke() {
143 if (mClickListener != null &&
144 mClickListener.onMenuItemClick(this)) {
148 if (mMenu.dispatchMenuItemSelected(mMenu.getRootMenu(), this)) {
152 if (mItemCallback != null) {
157 if (mIntent != null) {
159 mMenu.getContext().startActivity(mIntent);
161 } catch (ActivityNotFoundException e) {
162 Log.e(TAG, "Can't find activity to handle intent; ignoring", e);
166 if (mActionProvider != null && mActionProvider.onPerformDefaultAction()) {
173 public boolean isEnabled() {
174 return (mFlags & ENABLED) != 0;
177 public MenuItem setEnabled(boolean enabled) {
184 mMenu.onItemsChanged(false);
189 public int getGroupId() {
193 @ViewDebug.CapturedViewProperty
194 public int getItemId() {
198 public int getOrder() {
199 return mCategoryOrder;
202 public int getOrdering() {
206 public Intent getIntent() {
210 public MenuItem setIntent(Intent intent) {
215 Runnable getCallback() {
216 return mItemCallback;
219 public MenuItem setCallback(Runnable callback) {
220 mItemCallback = callback;
224 public char getAlphabeticShortcut() {
225 return mShortcutAlphabeticChar;
228 public MenuItem setAlphabeticShortcut(char alphaChar) {
229 if (mShortcutAlphabeticChar == alphaChar) return this;
231 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
233 mMenu.onItemsChanged(false);
238 public char getNumericShortcut() {
239 return mShortcutNumericChar;
242 public MenuItem setNumericShortcut(char numericChar) {
243 if (mShortcutNumericChar == numericChar) return this;
245 mShortcutNumericChar = numericChar;
247 mMenu.onItemsChanged(false);
252 public MenuItem setShortcut(char numericChar, char alphaChar) {
253 mShortcutNumericChar = numericChar;
254 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
256 mMenu.onItemsChanged(false);
262 * @return The active shortcut (based on QWERTY-mode of the menu).
265 return (mMenu.isQwertyMode() ? mShortcutAlphabeticChar : mShortcutNumericChar);
269 * @return The label to show for the shortcut. This includes the chording
270 * key (for example 'Menu+a'). Also, any non-human readable
271 * characters should be human readable (for example 'Menu+enter').
273 String getShortcutLabel() {
275 char shortcut = getShortcut();
280 StringBuilder sb = new StringBuilder(sPrependShortcutLabel);
284 sb.append(sEnterShortcutLabel);
288 sb.append(sDeleteShortcutLabel);
292 sb.append(sSpaceShortcutLabel);
300 return sb.toString();
304 * @return Whether this menu item should be showing shortcuts (depends on
305 * whether the menu should show shortcuts and whether this item has
306 * a shortcut defined)
308 boolean shouldShowShortcut() {
309 // Show shortcuts if the menu is supposed to show shortcuts AND this item has a shortcut
310 return mMenu.isShortcutsVisible() && (getShortcut() != 0);
313 public SubMenu getSubMenu() {
317 public boolean hasSubMenu() {
318 return mSubMenu != null;
321 void setSubMenu(SubMenuBuilder subMenu) {
324 subMenu.setHeaderTitle(getTitle());
327 @ViewDebug.CapturedViewProperty
328 public CharSequence getTitle() {
333 * Gets the title for a particular {@link ItemView}
335 * @param itemView The ItemView that is receiving the title
336 * @return Either the title or condensed title based on what the ItemView
339 CharSequence getTitleForItemView(MenuView.ItemView itemView) {
340 return ((itemView != null) && itemView.prefersCondensedTitle())
341 ? getTitleCondensed()
345 public MenuItem setTitle(CharSequence title) {
348 mMenu.onItemsChanged(false);
350 if (mSubMenu != null) {
351 mSubMenu.setHeaderTitle(title);
357 public MenuItem setTitle(int title) {
358 return setTitle(mMenu.getContext().getString(title));
361 public CharSequence getTitleCondensed() {
362 return mTitleCondensed != null ? mTitleCondensed : mTitle;
365 public MenuItem setTitleCondensed(CharSequence title) {
366 mTitleCondensed = title;
368 // Could use getTitle() in the loop below, but just cache what it would do here
373 mMenu.onItemsChanged(false);
378 public Drawable getIcon() {
379 if (mIconDrawable != null) {
380 return mIconDrawable;
383 if (mIconResId != NO_ICON) {
384 return mMenu.getResources().getDrawable(mIconResId);
390 public MenuItem setIcon(Drawable icon) {
391 mIconResId = NO_ICON;
392 mIconDrawable = icon;
393 mMenu.onItemsChanged(false);
398 public MenuItem setIcon(int iconResId) {
399 mIconDrawable = null;
400 mIconResId = iconResId;
402 // If we have a view, we need to push the Drawable to them
403 mMenu.onItemsChanged(false);
408 public boolean isCheckable() {
409 return (mFlags & CHECKABLE) == CHECKABLE;
412 public MenuItem setCheckable(boolean checkable) {
413 final int oldFlags = mFlags;
414 mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
415 if (oldFlags != mFlags) {
416 mMenu.onItemsChanged(false);
422 public void setExclusiveCheckable(boolean exclusive) {
423 mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
426 public boolean isExclusiveCheckable() {
427 return (mFlags & EXCLUSIVE) != 0;
430 public boolean isChecked() {
431 return (mFlags & CHECKED) == CHECKED;
434 public MenuItem setChecked(boolean checked) {
435 if ((mFlags & EXCLUSIVE) != 0) {
436 // Call the method on the Menu since it knows about the others in this
437 // exclusive checkable group
438 mMenu.setExclusiveItemChecked(this);
440 setCheckedInt(checked);
446 void setCheckedInt(boolean checked) {
447 final int oldFlags = mFlags;
448 mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
449 if (oldFlags != mFlags) {
450 mMenu.onItemsChanged(false);
454 public boolean isVisible() {
455 return (mFlags & HIDDEN) == 0;
459 * Changes the visibility of the item. This method DOES NOT notify the
460 * parent menu of a change in this item, so this should only be called from
461 * methods that will eventually trigger this change. If unsure, use {@link #setVisible(boolean)}
464 * @param shown Whether to show (true) or hide (false).
465 * @return Whether the item's shown state was changed
467 boolean setVisibleInt(boolean shown) {
468 final int oldFlags = mFlags;
469 mFlags = (mFlags & ~HIDDEN) | (shown ? 0 : HIDDEN);
470 return oldFlags != mFlags;
473 public MenuItem setVisible(boolean shown) {
474 // Try to set the shown state to the given state. If the shown state was changed
475 // (i.e. the previous state isn't the same as given state), notify the parent menu that
476 // the shown state has changed for this item
477 if (setVisibleInt(shown)) mMenu.onItemVisibleChanged(this);
482 public MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener clickListener) {
483 mClickListener = clickListener;
488 public String toString() {
489 return mTitle.toString();
492 void setMenuInfo(ContextMenuInfo menuInfo) {
493 mMenuInfo = menuInfo;
496 public ContextMenuInfo getMenuInfo() {
500 public void actionFormatChanged() {
501 mMenu.onItemActionRequestChanged(this);
505 * @return Whether the menu should show icons for menu items.
507 public boolean shouldShowIcon() {
508 return mMenu.getOptionalIconsVisible();
511 public boolean isActionButton() {
512 return (mFlags & IS_ACTION) == IS_ACTION;
515 public boolean requestsActionButton() {
516 return (mShowAsAction & SHOW_AS_ACTION_IF_ROOM) == SHOW_AS_ACTION_IF_ROOM;
519 public boolean requiresActionButton() {
520 return (mShowAsAction & SHOW_AS_ACTION_ALWAYS) == SHOW_AS_ACTION_ALWAYS;
523 public void setIsActionButton(boolean isActionButton) {
524 if (isActionButton) {
527 mFlags &= ~IS_ACTION;
531 public boolean showsTextAsAction() {
532 return (mShowAsAction & SHOW_AS_ACTION_WITH_TEXT) == SHOW_AS_ACTION_WITH_TEXT;
535 public void setShowAsAction(int actionEnum) {
536 switch (actionEnum & SHOW_AS_ACTION_MASK) {
537 case SHOW_AS_ACTION_ALWAYS:
538 case SHOW_AS_ACTION_IF_ROOM:
539 case SHOW_AS_ACTION_NEVER:
544 // Mutually exclusive options selected!
545 throw new IllegalArgumentException("SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM,"
546 + " and SHOW_AS_ACTION_NEVER are mutually exclusive.");
548 mShowAsAction = actionEnum;
549 mMenu.onItemActionRequestChanged(this);
552 public MenuItem setActionView(View view) {
554 mActionProvider = null;
555 if (view != null && view.getId() == View.NO_ID && mId > 0) {
558 mMenu.onItemActionRequestChanged(this);
562 public MenuItem setActionView(int resId) {
563 final Context context = mMenu.getContext();
564 final LayoutInflater inflater = LayoutInflater.from(context);
565 setActionView(inflater.inflate(resId, new LinearLayout(context), false));
569 public View getActionView() {
570 if (mActionView != null) {
572 } else if (mActionProvider != null) {
573 mActionView = mActionProvider.onCreateActionView();
580 public ActionProvider getActionProvider() {
581 return mActionProvider;
584 public MenuItem setActionProvider(ActionProvider actionProvider) {
586 mActionProvider = actionProvider;
587 mMenu.onItemsChanged(true); // Measurement can be changed
592 public MenuItem setShowAsActionFlags(int actionEnum) {
593 setShowAsAction(actionEnum);
598 public boolean expandActionView() {
599 if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) == 0 || mActionView == null) {
603 if (mOnActionExpandListener == null ||
604 mOnActionExpandListener.onMenuItemActionExpand(this)) {
605 return mMenu.expandItemActionView(this);
612 public boolean collapseActionView() {
613 if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) == 0) {
616 if (mActionView == null) {
617 // We're already collapsed if we have no action view.
621 if (mOnActionExpandListener == null ||
622 mOnActionExpandListener.onMenuItemActionCollapse(this)) {
623 return mMenu.collapseItemActionView(this);
630 public MenuItem setOnActionExpandListener(OnActionExpandListener listener) {
631 mOnActionExpandListener = listener;
635 public boolean hasCollapsibleActionView() {
636 return (mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) != 0 && mActionView != null;
639 public void setActionViewExpanded(boolean isExpanded) {
640 mIsActionViewExpanded = isExpanded;
641 mMenu.onItemsChanged(false);
644 public boolean isActionViewExpanded() {
645 return mIsActionViewExpanded;