Source code
package android.support.v7.internal.view.menu;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.support.v4.internal.view.SupportMenuItem;
import android.support.v4.view.ActionProvider;
import android.support.v4.view.ActionProvider.VisibilityListener;
import android.support.v4.view.MenuItemCompat.OnActionExpandListener;
import android.support.v7.internal.view.menu.MenuView.ItemView;
import android.support.v7.internal.widget.TintManager;
import android.util.Log;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewDebug.CapturedViewProperty;
import android.widget.LinearLayout;
public final class MenuItemImpl implements SupportMenuItem {
private static final int CHECKABLE = 1;
private static final int CHECKED = 2;
private static final int ENABLED = 16;
private static final int EXCLUSIVE = 4;
private static final int HIDDEN = 8;
private static final int IS_ACTION = 32;
static final int NO_ICON = 0;
private static final int SHOW_AS_ACTION_MASK = 3;
private static final String TAG = "MenuItemImpl";
private static String sDeleteShortcutLabel;
private static String sEnterShortcutLabel;
private static String sPrependShortcutLabel;
private static String sSpaceShortcutLabel;
private ActionProvider mActionProvider;
private View mActionView;
private final int mCategoryOrder;
private OnMenuItemClickListener mClickListener;
private int mFlags = 16;
private final int mGroup;
private Drawable mIconDrawable;
private int mIconResId = 0;
private final int mId;
private Intent mIntent;
private boolean mIsActionViewExpanded = false;
private Runnable mItemCallback;
private MenuBuilder mMenu;
private ContextMenuInfo mMenuInfo;
private OnActionExpandListener mOnActionExpandListener;
private final int mOrdering;
private char mShortcutAlphabeticChar;
private char mShortcutNumericChar;
private int mShowAsAction = 0;
private SubMenuBuilder mSubMenu;
private CharSequence mTitle;
private CharSequence mTitleCondensed;
MenuItemImpl(MenuBuilder menu, int group, int id, int categoryOrder, int ordering, CharSequence title, int showAsAction) {
this.mMenu = menu;
this.mId = id;
this.mGroup = group;
this.mCategoryOrder = categoryOrder;
this.mOrdering = ordering;
this.mTitle = title;
this.mShowAsAction = showAsAction;
}
public boolean invoke() {
if ((this.mClickListener != null && this.mClickListener.onMenuItemClick(this)) || this.mMenu.dispatchMenuItemSelected(this.mMenu.getRootMenu(), this)) {
return true;
}
if (this.mItemCallback != null) {
this.mItemCallback.run();
return true;
}
if (this.mIntent != null) {
try {
this.mMenu.getContext().startActivity(this.mIntent);
return true;
} catch (ActivityNotFoundException e) {
Log.e(TAG, "Can't find activity to handle intent; ignoring", e);
}
}
if (this.mActionProvider == null || !this.mActionProvider.onPerformDefaultAction()) {
return false;
}
return true;
}
public boolean isEnabled() {
return (this.mFlags & 16) != 0;
}
public MenuItem setEnabled(boolean enabled) {
if (enabled) {
this.mFlags |= 16;
} else {
this.mFlags &= -17;
}
this.mMenu.onItemsChanged(false);
return this;
}
public int getGroupId() {
return this.mGroup;
}
@CapturedViewProperty
public int getItemId() {
return this.mId;
}
public int getOrder() {
return this.mCategoryOrder;
}
public int getOrdering() {
return this.mOrdering;
}
public Intent getIntent() {
return this.mIntent;
}
public MenuItem setIntent(Intent intent) {
this.mIntent = intent;
return this;
}
Runnable getCallback() {
return this.mItemCallback;
}
public MenuItem setCallback(Runnable callback) {
this.mItemCallback = callback;
return this;
}
public char getAlphabeticShortcut() {
return this.mShortcutAlphabeticChar;
}
public MenuItem setAlphabeticShortcut(char alphaChar) {
if (this.mShortcutAlphabeticChar != alphaChar) {
this.mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
this.mMenu.onItemsChanged(false);
}
return this;
}
public char getNumericShortcut() {
return this.mShortcutNumericChar;
}
public MenuItem setNumericShortcut(char numericChar) {
if (this.mShortcutNumericChar != numericChar) {
this.mShortcutNumericChar = numericChar;
this.mMenu.onItemsChanged(false);
}
return this;
}
public MenuItem setShortcut(char numericChar, char alphaChar) {
this.mShortcutNumericChar = numericChar;
this.mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
this.mMenu.onItemsChanged(false);
return this;
}
char getShortcut() {
return this.mMenu.isQwertyMode() ? this.mShortcutAlphabeticChar : this.mShortcutNumericChar;
}
String getShortcutLabel() {
char shortcut = getShortcut();
if (shortcut == '\u0000') {
return "";
}
StringBuilder sb = new StringBuilder(sPrependShortcutLabel);
switch (shortcut) {
case '\b':
sb.append(sDeleteShortcutLabel);
break;
case '\n':
sb.append(sEnterShortcutLabel);
break;
case ' ':
sb.append(sSpaceShortcutLabel);
break;
default:
sb.append(shortcut);
break;
}
return sb.toString();
}
boolean shouldShowShortcut() {
return this.mMenu.isShortcutsVisible() && getShortcut() != '\u0000';
}
public SubMenu getSubMenu() {
return this.mSubMenu;
}
public boolean hasSubMenu() {
return this.mSubMenu != null;
}
public void setSubMenu(SubMenuBuilder subMenu) {
this.mSubMenu = subMenu;
subMenu.setHeaderTitle(getTitle());
}
@CapturedViewProperty
public CharSequence getTitle() {
return this.mTitle;
}
CharSequence getTitleForItemView(ItemView itemView) {
return (itemView == null || !itemView.prefersCondensedTitle()) ? getTitle() : getTitleCondensed();
}
public MenuItem setTitle(CharSequence title) {
this.mTitle = title;
this.mMenu.onItemsChanged(false);
if (this.mSubMenu != null) {
this.mSubMenu.setHeaderTitle(title);
}
return this;
}
public MenuItem setTitle(int title) {
return setTitle(this.mMenu.getContext().getString(title));
}
public CharSequence getTitleCondensed() {
CharSequence ctitle = this.mTitleCondensed != null ? this.mTitleCondensed : this.mTitle;
if (VERSION.SDK_INT >= 18 || ctitle == null || (ctitle instanceof String)) {
return ctitle;
}
return ctitle.toString();
}
public MenuItem setTitleCondensed(CharSequence title) {
this.mTitleCondensed = title;
if (title == null) {
title = this.mTitle;
}
this.mMenu.onItemsChanged(false);
return this;
}
public Drawable getIcon() {
if (this.mIconDrawable != null) {
return this.mIconDrawable;
}
if (this.mIconResId == 0) {
return null;
}
Drawable icon = TintManager.getDrawable(this.mMenu.getContext(), this.mIconResId);
this.mIconResId = 0;
this.mIconDrawable = icon;
return icon;
}
public MenuItem setIcon(Drawable icon) {
this.mIconResId = 0;
this.mIconDrawable = icon;
this.mMenu.onItemsChanged(false);
return this;
}
public MenuItem setIcon(int iconResId) {
this.mIconDrawable = null;
this.mIconResId = iconResId;
this.mMenu.onItemsChanged(false);
return this;
}
public boolean isCheckable() {
return (this.mFlags & 1) == 1;
}
public MenuItem setCheckable(boolean checkable) {
int oldFlags = this.mFlags;
this.mFlags = (checkable ? 1 : 0) | (this.mFlags & -2);
if (oldFlags != this.mFlags) {
this.mMenu.onItemsChanged(false);
}
return this;
}
public void setExclusiveCheckable(boolean exclusive) {
this.mFlags = (exclusive ? 4 : 0) | (this.mFlags & -5);
}
public boolean isExclusiveCheckable() {
return (this.mFlags & 4) != 0;
}
public boolean isChecked() {
return (this.mFlags & 2) == 2;
}
public MenuItem setChecked(boolean checked) {
if ((this.mFlags & 4) != 0) {
this.mMenu.setExclusiveItemChecked(this);
} else {
setCheckedInt(checked);
}
return this;
}
void setCheckedInt(boolean checked) {
int i;
int oldFlags = this.mFlags;
int i2 = this.mFlags & -3;
if (checked) {
i = 2;
} else {
i = 0;
}
this.mFlags = i | i2;
if (oldFlags != this.mFlags) {
this.mMenu.onItemsChanged(false);
}
}
public boolean isVisible() {
if (this.mActionProvider == null || !this.mActionProvider.overridesItemVisibility()) {
if ((this.mFlags & 8) != 0) {
return false;
}
return true;
} else if ((this.mFlags & 8) == 0 && this.mActionProvider.isVisible()) {
return true;
} else {
return false;
}
}
boolean setVisibleInt(boolean shown) {
int oldFlags = this.mFlags;
this.mFlags = (shown ? 0 : 8) | (this.mFlags & -9);
if (oldFlags != this.mFlags) {
return true;
}
return false;
}
public MenuItem setVisible(boolean shown) {
if (setVisibleInt(shown)) {
this.mMenu.onItemVisibleChanged(this);
}
return this;
}
public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener clickListener) {
this.mClickListener = clickListener;
return this;
}
public String toString() {
return this.mTitle != null ? this.mTitle.toString() : null;
}
void setMenuInfo(ContextMenuInfo menuInfo) {
this.mMenuInfo = menuInfo;
}
public ContextMenuInfo getMenuInfo() {
return this.mMenuInfo;
}
public void actionFormatChanged() {
this.mMenu.onItemActionRequestChanged(this);
}
public boolean shouldShowIcon() {
return this.mMenu.getOptionalIconsVisible();
}
public boolean isActionButton() {
return (this.mFlags & 32) == 32;
}
public boolean requestsActionButton() {
return (this.mShowAsAction & 1) == 1;
}
public boolean requiresActionButton() {
return (this.mShowAsAction & 2) == 2;
}
public void setIsActionButton(boolean isActionButton) {
if (isActionButton) {
this.mFlags |= 32;
} else {
this.mFlags &= -33;
}
}
public boolean showsTextAsAction() {
return (this.mShowAsAction & 4) == 4;
}
public void setShowAsAction(int actionEnum) {
switch (actionEnum & 3) {
case 0:
case 1:
case 2:
this.mShowAsAction = actionEnum;
this.mMenu.onItemActionRequestChanged(this);
return;
default:
throw new IllegalArgumentException("SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, and SHOW_AS_ACTION_NEVER are mutually exclusive.");
}
}
public SupportMenuItem setActionView(View view) {
this.mActionView = view;
this.mActionProvider = null;
if (view != null && view.getId() == -1 && this.mId > 0) {
view.setId(this.mId);
}
this.mMenu.onItemActionRequestChanged(this);
return this;
}
public SupportMenuItem setActionView(int resId) {
Context context = this.mMenu.getContext();
setActionView(LayoutInflater.from(context).inflate(resId, new LinearLayout(context), false));
return this;
}
public View getActionView() {
if (this.mActionView != null) {
return this.mActionView;
}
if (this.mActionProvider == null) {
return null;
}
this.mActionView = this.mActionProvider.onCreateActionView(this);
return this.mActionView;
}
public MenuItem setActionProvider(android.view.ActionProvider actionProvider) {
throw new UnsupportedOperationException("This is not supported, use MenuItemCompat.setActionProvider()");
}
public android.view.ActionProvider getActionProvider() {
throw new UnsupportedOperationException("This is not supported, use MenuItemCompat.getActionProvider()");
}
public ActionProvider getSupportActionProvider() {
return this.mActionProvider;
}
public SupportMenuItem setSupportActionProvider(ActionProvider actionProvider) {
if (this.mActionProvider != null) {
this.mActionProvider.reset();
}
this.mActionView = null;
this.mActionProvider = actionProvider;
this.mMenu.onItemsChanged(true);
if (this.mActionProvider != null) {
this.mActionProvider.setVisibilityListener(new VisibilityListener() {
public void onActionProviderVisibilityChanged(boolean isVisible) {
MenuItemImpl.this.mMenu.onItemVisibleChanged(MenuItemImpl.this);
}
});
}
return this;
}
public SupportMenuItem setShowAsActionFlags(int actionEnum) {
setShowAsAction(actionEnum);
return this;
}
public boolean expandActionView() {
if (!hasCollapsibleActionView()) {
return false;
}
if (this.mOnActionExpandListener == null || this.mOnActionExpandListener.onMenuItemActionExpand(this)) {
return this.mMenu.expandItemActionView(this);
}
return false;
}
public boolean collapseActionView() {
if ((this.mShowAsAction & 8) == 0) {
return false;
}
if (this.mActionView == null) {
return true;
}
if (this.mOnActionExpandListener == null || this.mOnActionExpandListener.onMenuItemActionCollapse(this)) {
return this.mMenu.collapseItemActionView(this);
}
return false;
}
public SupportMenuItem setSupportOnActionExpandListener(OnActionExpandListener listener) {
this.mOnActionExpandListener = listener;
return this;
}
public boolean hasCollapsibleActionView() {
if ((this.mShowAsAction & 8) == 0) {
return false;
}
if (this.mActionView == null && this.mActionProvider != null) {
this.mActionView = this.mActionProvider.onCreateActionView(this);
}
if (this.mActionView != null) {
return true;
}
return false;
}
public void setActionViewExpanded(boolean isExpanded) {
this.mIsActionViewExpanded = isExpanded;
this.mMenu.onItemsChanged(false);
}
public boolean isActionViewExpanded() {
return this.mIsActionViewExpanded;
}
public MenuItem setOnActionExpandListener(MenuItem.OnActionExpandListener listener) {
throw new UnsupportedOperationException("This is not supported, use MenuItemCompat.setOnActionExpandListener()");
}
}