ActionMenuItem.java revision cf78b3e5101349fdddbde14b3a55140f9562ae66
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 com.android.internal.view.menu; 18 19import android.content.Context; 20import android.content.Intent; 21import android.graphics.drawable.Drawable; 22import android.view.ContextMenu.ContextMenuInfo; 23import android.view.MenuItem; 24import android.view.SubMenu; 25import android.view.View; 26 27/** 28 * @hide 29 */ 30public class ActionMenuItem implements MenuItem { 31 private final int mId; 32 private final int mGroup; 33 private final int mCategoryOrder; 34 private final int mOrdering; 35 36 private CharSequence mTitle; 37 private CharSequence mTitleCondensed; 38 private Intent mIntent; 39 private char mShortcutNumericChar; 40 private char mShortcutAlphabeticChar; 41 42 private Drawable mIconDrawable; 43 private int mIconResId = NO_ICON; 44 45 private Context mContext; 46 47 private MenuItem.OnMenuItemClickListener mClickListener; 48 49 private static final int NO_ICON = 0; 50 51 private int mFlags = ENABLED; 52 private static final int CHECKABLE = 0x00000001; 53 private static final int CHECKED = 0x00000002; 54 private static final int EXCLUSIVE = 0x00000004; 55 private static final int HIDDEN = 0x00000008; 56 private static final int ENABLED = 0x00000010; 57 58 public ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering, 59 CharSequence title) { 60 mContext = context; 61 mId = id; 62 mGroup = group; 63 mCategoryOrder = categoryOrder; 64 mOrdering = ordering; 65 mTitle = title; 66 } 67 68 public char getAlphabeticShortcut() { 69 return mShortcutAlphabeticChar; 70 } 71 72 public int getGroupId() { 73 return mGroup; 74 } 75 76 public Drawable getIcon() { 77 return mIconDrawable; 78 } 79 80 public Intent getIntent() { 81 return mIntent; 82 } 83 84 public int getItemId() { 85 return mId; 86 } 87 88 public ContextMenuInfo getMenuInfo() { 89 return null; 90 } 91 92 public char getNumericShortcut() { 93 return mShortcutNumericChar; 94 } 95 96 public int getOrder() { 97 return mOrdering; 98 } 99 100 public SubMenu getSubMenu() { 101 return null; 102 } 103 104 public CharSequence getTitle() { 105 return mTitle; 106 } 107 108 public CharSequence getTitleCondensed() { 109 return mTitleCondensed; 110 } 111 112 public boolean hasSubMenu() { 113 return false; 114 } 115 116 public boolean isCheckable() { 117 return (mFlags & CHECKABLE) != 0; 118 } 119 120 public boolean isChecked() { 121 return (mFlags & CHECKED) != 0; 122 } 123 124 public boolean isEnabled() { 125 return (mFlags & ENABLED) != 0; 126 } 127 128 public boolean isVisible() { 129 return (mFlags & HIDDEN) == 0; 130 } 131 132 public MenuItem setAlphabeticShortcut(char alphaChar) { 133 mShortcutAlphabeticChar = alphaChar; 134 return this; 135 } 136 137 public MenuItem setCheckable(boolean checkable) { 138 mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0); 139 return this; 140 } 141 142 public ActionMenuItem setExclusiveCheckable(boolean exclusive) { 143 mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0); 144 return this; 145 } 146 147 public MenuItem setChecked(boolean checked) { 148 mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0); 149 return this; 150 } 151 152 public MenuItem setEnabled(boolean enabled) { 153 mFlags = (mFlags & ~ENABLED) | (enabled ? ENABLED : 0); 154 return this; 155 } 156 157 public MenuItem setIcon(Drawable icon) { 158 mIconDrawable = icon; 159 mIconResId = NO_ICON; 160 return this; 161 } 162 163 public MenuItem setIcon(int iconRes) { 164 mIconResId = iconRes; 165 mIconDrawable = mContext.getResources().getDrawable(iconRes); 166 return this; 167 } 168 169 public MenuItem setIntent(Intent intent) { 170 mIntent = intent; 171 return this; 172 } 173 174 public MenuItem setNumericShortcut(char numericChar) { 175 mShortcutNumericChar = numericChar; 176 return this; 177 } 178 179 public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) { 180 mClickListener = menuItemClickListener; 181 return this; 182 } 183 184 public MenuItem setShortcut(char numericChar, char alphaChar) { 185 mShortcutNumericChar = numericChar; 186 mShortcutAlphabeticChar = alphaChar; 187 return this; 188 } 189 190 public MenuItem setTitle(CharSequence title) { 191 mTitle = title; 192 return this; 193 } 194 195 public MenuItem setTitle(int title) { 196 mTitle = mContext.getResources().getString(title); 197 return this; 198 } 199 200 public MenuItem setTitleCondensed(CharSequence title) { 201 mTitleCondensed = title; 202 return this; 203 } 204 205 public MenuItem setVisible(boolean visible) { 206 mFlags = (mFlags & HIDDEN) | (visible ? 0 : HIDDEN); 207 return this; 208 } 209 210 public boolean invoke() { 211 if (mClickListener != null && mClickListener.onMenuItemClick(this)) { 212 return true; 213 } 214 215 if (mIntent != null) { 216 mContext.startActivity(mIntent); 217 return true; 218 } 219 220 return false; 221 } 222 223 public void setShowAsAction(int show) { 224 // Do nothing. ActionMenuItems always show as action buttons. 225 } 226 227 public MenuItem setActionView(View actionView) { 228 throw new UnsupportedOperationException(); 229 } 230 231 public View getActionView() { 232 return null; 233 } 234} 235