/**
   * Invokes the item by calling various listeners or callbacks.
   *
   * @return true if the invocation was handled, false otherwise
   */
  public boolean invoke() {
    if (mClickListener != null && mClickListener.onMenuItemClick(this)) {
      return true;
    }

    if (mMenu.dispatchMenuItemSelected(mMenu.getRootMenu(), this)) {
      return true;
    }

    if (mItemCallback != null) {
      mItemCallback.run();
      return true;
    }

    if (mIntent != null) {
      try {
        mMenu.getContext().startActivity(mIntent);
        return true;
      } catch (ActivityNotFoundException e) {
        Log.e(TAG, "Can't find activity to handle intent; ignoring", e);
      }
    }

    if (mActionProvider != null && mActionProvider.onPerformDefaultAction()) {
      return true;
    }

    return false;
  }
 void setCheckedInt(boolean checked) {
   final int oldFlags = mFlags;
   mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
   if (oldFlags != mFlags) {
     mMenu.onItemsChanged(false);
   }
 }
  public MenuItem setIcon(Drawable icon) {
    mIconResId = NO_ICON;
    mIconDrawable = icon;
    mMenu.onItemsChanged(false);

    return this;
  }
  public MenuItem setVisible(boolean shown) {
    // Try to set the shown state to the given state. If the shown state was changed
    // (i.e. the previous state isn't the same as given state), notify the parent menu that
    // the shown state has changed for this item
    if (setVisibleInt(shown)) mMenu.onItemVisibleChanged(this);

    return this;
  }
  public MenuItem setShortcut(char numericChar, char alphaChar) {
    mShortcutNumericChar = numericChar;
    mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);

    mMenu.onItemsChanged(false);

    return this;
  }
  public MenuItem setIcon(int iconResId) {
    mIconDrawable = null;
    mIconResId = iconResId;

    // If we have a view, we need to push the Drawable to them
    mMenu.onItemsChanged(false);

    return this;
  }
 public MenuItem setActionView(View view) {
   mActionView = view;
   mActionProvider = null;
   if (view != null && view.getId() == View.NO_ID && mId > 0) {
     view.setId(mId);
   }
   mMenu.onItemActionRequestChanged(this);
   return this;
 }
  public MenuItem setNumericShortcut(char numericChar) {
    if (mShortcutNumericChar == numericChar) return this;

    mShortcutNumericChar = numericChar;

    mMenu.onItemsChanged(false);

    return this;
  }
  public MenuItem setAlphabeticShortcut(char alphaChar) {
    if (mShortcutAlphabeticChar == alphaChar) return this;

    mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);

    mMenu.onItemsChanged(false);

    return this;
  }
  public MenuItem setCheckable(boolean checkable) {
    final int oldFlags = mFlags;
    mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
    if (oldFlags != mFlags) {
      mMenu.onItemsChanged(false);
    }

    return this;
  }
  public MenuItem setTitle(CharSequence title) {
    mTitle = title;

    mMenu.onItemsChanged(false);

    if (mSubMenu != null) {
      mSubMenu.setHeaderTitle(title);
    }

    return this;
  }
  public MenuItem setEnabled(boolean enabled) {
    if (enabled) {
      mFlags |= ENABLED;
    } else {
      mFlags &= ~ENABLED;
    }

    mMenu.onItemsChanged(false);

    return this;
  }
  public MenuItem setChecked(boolean checked) {
    if ((mFlags & EXCLUSIVE) != 0) {
      // Call the method on the Menu since it knows about the others in this
      // exclusive checkable group
      mMenu.setExclusiveItemChecked(this);
    } else {
      setCheckedInt(checked);
    }

    return this;
  }
  public MenuItem setTitleCondensed(CharSequence title) {
    mTitleCondensed = title;

    // Could use getTitle() in the loop below, but just cache what it would do here
    if (title == null) {
      title = mTitle;
    }

    mMenu.onItemsChanged(false);

    return this;
  }
  @Override
  public boolean expandActionView() {
    if (!hasCollapsibleActionView()) {
      return false;
    }

    if (mOnActionExpandListener == null || mOnActionExpandListener.onMenuItemActionExpand(this)) {
      return mMenu.expandItemActionView(this);
    }

    return false;
  }
  /**
   * Instantiates this menu item.
   *
   * @param menu
   * @param group Item ordering grouping control. The item will be added after all other items whose
   *     order is <= this number, and before any that are larger than it. This can also be used to
   *     define groups of items for batch state changes. Normally use 0.
   * @param id Unique item ID. Use 0 if you do not need a unique ID.
   * @param categoryOrder The ordering for this item.
   * @param title The text to display for the item.
   */
  MenuItemImpl(
      MenuBuilder menu,
      int group,
      int id,
      int categoryOrder,
      int ordering,
      CharSequence title,
      int showAsAction) {

    String lang = menu.getContext().getResources().getConfiguration().locale.toString();
    if (sPrependShortcutLabel == null || !lang.equals(sLanguage)) {
      sLanguage = lang;
      // This is instantiated from the UI thread, so no chance of sync issues
      sPrependShortcutLabel =
          menu.getContext()
              .getResources()
              .getString(com.android.internal.R.string.prepend_shortcut_label);
      sEnterShortcutLabel =
          menu.getContext()
              .getResources()
              .getString(com.android.internal.R.string.menu_enter_shortcut_label);
      sDeleteShortcutLabel =
          menu.getContext()
              .getResources()
              .getString(com.android.internal.R.string.menu_delete_shortcut_label);
      sSpaceShortcutLabel =
          menu.getContext()
              .getResources()
              .getString(com.android.internal.R.string.menu_space_shortcut_label);
    }

    mMenu = menu;
    mId = id;
    mGroup = group;
    mCategoryOrder = categoryOrder;
    mOrdering = ordering;
    mTitle = title;
    mShowAsAction = showAsAction;
  }
  /** Reuses item views when it can */
  public void updateMenuView(boolean cleared) {
    final ViewGroup parent = (ViewGroup) mMenuView;
    if (parent == null) return;

    int childIndex = 0;
    if (mMenu != null) {
      mMenu.flagActionItems();
      ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems();
      final int itemCount = visibleItems.size();
      for (int i = 0; i < itemCount; i++) {
        MenuItemImpl item = visibleItems.get(i);
        if (shouldIncludeItem(childIndex, item)) {
          final View convertView = parent.getChildAt(childIndex);
          final MenuItemImpl oldItem =
              convertView instanceof MenuView.ItemView
                  ? ((MenuView.ItemView) convertView).getItemData()
                  : null;
          final View itemView = getItemView(item, convertView, parent);
          if (item != oldItem) {
            // Don't let old states linger with new data.
            itemView.setPressed(false);
            itemView.jumpDrawablesToCurrentState();
          }
          if (itemView != convertView) {
            addItemView(itemView, childIndex);
          }
          childIndex++;
        }
      }
    }

    // Remove leftover views.
    while (childIndex < parent.getChildCount()) {
      if (!filterLeftoverView(parent, childIndex)) {
        childIndex++;
      }
    }
  }
  public Drawable getIcon() {
    if (mIconDrawable != null) {
      return mIconDrawable;
    }

    if (mIconResId != NO_ICON) {
      Drawable icon = mMenu.getContext().getDrawable(mIconResId);
      mIconResId = NO_ICON;
      mIconDrawable = icon;
      return icon;
    }

    return null;
  }
  @Override
  public boolean collapseActionView() {
    if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) == 0) {
      return false;
    }
    if (mActionView == null) {
      // We're already collapsed if we have no action view.
      return true;
    }

    if (mOnActionExpandListener == null || mOnActionExpandListener.onMenuItemActionCollapse(this)) {
      return mMenu.collapseItemActionView(this);
    }

    return false;
  }
  public void setShowAsAction(int actionEnum) {
    switch (actionEnum & SHOW_AS_ACTION_MASK) {
      case SHOW_AS_ACTION_ALWAYS:
      case SHOW_AS_ACTION_IF_ROOM:
      case SHOW_AS_ACTION_NEVER:
        // Looks good!
        break;

      default:
        // Mutually exclusive options selected!
        throw new IllegalArgumentException(
            "SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM,"
                + " and SHOW_AS_ACTION_NEVER are mutually exclusive.");
    }
    mShowAsAction = actionEnum;
    mMenu.onItemActionRequestChanged(this);
  }
 public MenuItem setActionProvider(ActionProvider actionProvider) {
   if (mActionProvider != null) {
     mActionProvider.setVisibilityListener(null);
   }
   mActionView = null;
   mActionProvider = actionProvider;
   mMenu.onItemsChanged(true); // Measurement can be changed
   if (mActionProvider != null) {
     mActionProvider.setVisibilityListener(
         new ActionProvider.VisibilityListener() {
           @Override
           public void onActionProviderVisibilityChanged(boolean isVisible) {
             mMenu.onItemVisibleChanged(MenuItemImpl.this);
           }
         });
   }
   return this;
 }
 public void setActionViewExpanded(boolean isExpanded) {
   mIsActionViewExpanded = isExpanded;
   mMenu.onItemsChanged(false);
 }
 /** @return The active shortcut (based on QWERTY-mode of the menu). */
 char getShortcut() {
   return (mMenu.isQwertyMode() ? mShortcutAlphabeticChar : mShortcutNumericChar);
 }
 public MenuItem setActionView(int resId) {
   final Context context = mMenu.getContext();
   final LayoutInflater inflater = LayoutInflater.from(context);
   setActionView(inflater.inflate(resId, new LinearLayout(context), false));
   return this;
 }
 /**
  * @return Whether this menu item should be showing shortcuts (depends on whether the menu should
  *     show shortcuts and whether this item has a shortcut defined)
  */
 boolean shouldShowShortcut() {
   // Show shortcuts if the menu is supposed to show shortcuts AND this item has a shortcut
   return mMenu.isShortcutsVisible() && (getShortcut() != 0);
 }
 /** @return Whether the menu should show icons for menu items. */
 public boolean shouldShowIcon() {
   return mMenu.getOptionalIconsVisible();
 }
 public void actionFormatChanged() {
   mMenu.onItemActionRequestChanged(this);
 }
 public MenuItem setTitle(int title) {
   return setTitle(mMenu.getContext().getString(title));
 }