Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 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.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.graphics.drawable.Drawable; |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 22 | import android.view.ActionProvider; |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 23 | import android.view.ContextMenu.ContextMenuInfo; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 24 | import android.view.MenuItem; |
| 25 | import android.view.SubMenu; |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 26 | import android.view.View; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * @hide |
| 30 | */ |
| 31 | public class ActionMenuItem implements MenuItem { |
| 32 | private final int mId; |
| 33 | private final int mGroup; |
| 34 | private final int mCategoryOrder; |
| 35 | private final int mOrdering; |
| 36 | |
| 37 | private CharSequence mTitle; |
| 38 | private CharSequence mTitleCondensed; |
| 39 | private Intent mIntent; |
| 40 | private char mShortcutNumericChar; |
| 41 | private char mShortcutAlphabeticChar; |
| 42 | |
| 43 | private Drawable mIconDrawable; |
| 44 | private int mIconResId = NO_ICON; |
| 45 | |
| 46 | private Context mContext; |
| 47 | |
| 48 | private MenuItem.OnMenuItemClickListener mClickListener; |
| 49 | |
| 50 | private static final int NO_ICON = 0; |
| 51 | |
| 52 | private int mFlags = ENABLED; |
| 53 | private static final int CHECKABLE = 0x00000001; |
| 54 | private static final int CHECKED = 0x00000002; |
| 55 | private static final int EXCLUSIVE = 0x00000004; |
| 56 | private static final int HIDDEN = 0x00000008; |
| 57 | private static final int ENABLED = 0x00000010; |
| 58 | |
| 59 | public ActionMenuItem(Context context, int group, int id, int categoryOrder, int ordering, |
| 60 | CharSequence title) { |
| 61 | mContext = context; |
| 62 | mId = id; |
| 63 | mGroup = group; |
| 64 | mCategoryOrder = categoryOrder; |
| 65 | mOrdering = ordering; |
| 66 | mTitle = title; |
| 67 | } |
| 68 | |
| 69 | public char getAlphabeticShortcut() { |
| 70 | return mShortcutAlphabeticChar; |
| 71 | } |
| 72 | |
| 73 | public int getGroupId() { |
| 74 | return mGroup; |
| 75 | } |
| 76 | |
| 77 | public Drawable getIcon() { |
| 78 | return mIconDrawable; |
| 79 | } |
| 80 | |
| 81 | public Intent getIntent() { |
| 82 | return mIntent; |
| 83 | } |
| 84 | |
| 85 | public int getItemId() { |
| 86 | return mId; |
| 87 | } |
| 88 | |
| 89 | public ContextMenuInfo getMenuInfo() { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | public char getNumericShortcut() { |
| 94 | return mShortcutNumericChar; |
| 95 | } |
| 96 | |
| 97 | public int getOrder() { |
| 98 | return mOrdering; |
| 99 | } |
| 100 | |
| 101 | public SubMenu getSubMenu() { |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | public CharSequence getTitle() { |
| 106 | return mTitle; |
| 107 | } |
| 108 | |
| 109 | public CharSequence getTitleCondensed() { |
Adam Powell | e0e2f4f | 2013-04-05 16:27:35 -0700 | [diff] [blame] | 110 | return mTitleCondensed != null ? mTitleCondensed : mTitle; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | public boolean hasSubMenu() { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | public boolean isCheckable() { |
| 118 | return (mFlags & CHECKABLE) != 0; |
| 119 | } |
| 120 | |
| 121 | public boolean isChecked() { |
| 122 | return (mFlags & CHECKED) != 0; |
| 123 | } |
| 124 | |
| 125 | public boolean isEnabled() { |
| 126 | return (mFlags & ENABLED) != 0; |
| 127 | } |
| 128 | |
| 129 | public boolean isVisible() { |
| 130 | return (mFlags & HIDDEN) == 0; |
| 131 | } |
| 132 | |
| 133 | public MenuItem setAlphabeticShortcut(char alphaChar) { |
| 134 | mShortcutAlphabeticChar = alphaChar; |
| 135 | return this; |
| 136 | } |
| 137 | |
| 138 | public MenuItem setCheckable(boolean checkable) { |
| 139 | mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0); |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | public ActionMenuItem setExclusiveCheckable(boolean exclusive) { |
| 144 | mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0); |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | public MenuItem setChecked(boolean checked) { |
| 149 | mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0); |
| 150 | return this; |
| 151 | } |
| 152 | |
| 153 | public MenuItem setEnabled(boolean enabled) { |
| 154 | mFlags = (mFlags & ~ENABLED) | (enabled ? ENABLED : 0); |
| 155 | return this; |
| 156 | } |
| 157 | |
| 158 | public MenuItem setIcon(Drawable icon) { |
| 159 | mIconDrawable = icon; |
| 160 | mIconResId = NO_ICON; |
| 161 | return this; |
| 162 | } |
| 163 | |
| 164 | public MenuItem setIcon(int iconRes) { |
| 165 | mIconResId = iconRes; |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 166 | mIconDrawable = mContext.getDrawable(iconRes); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 167 | return this; |
| 168 | } |
| 169 | |
| 170 | public MenuItem setIntent(Intent intent) { |
| 171 | mIntent = intent; |
| 172 | return this; |
| 173 | } |
| 174 | |
| 175 | public MenuItem setNumericShortcut(char numericChar) { |
| 176 | mShortcutNumericChar = numericChar; |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | public MenuItem setOnMenuItemClickListener(OnMenuItemClickListener menuItemClickListener) { |
| 181 | mClickListener = menuItemClickListener; |
| 182 | return this; |
| 183 | } |
| 184 | |
| 185 | public MenuItem setShortcut(char numericChar, char alphaChar) { |
| 186 | mShortcutNumericChar = numericChar; |
| 187 | mShortcutAlphabeticChar = alphaChar; |
| 188 | return this; |
| 189 | } |
| 190 | |
| 191 | public MenuItem setTitle(CharSequence title) { |
| 192 | mTitle = title; |
| 193 | return this; |
| 194 | } |
| 195 | |
| 196 | public MenuItem setTitle(int title) { |
| 197 | mTitle = mContext.getResources().getString(title); |
| 198 | return this; |
| 199 | } |
| 200 | |
| 201 | public MenuItem setTitleCondensed(CharSequence title) { |
| 202 | mTitleCondensed = title; |
| 203 | return this; |
| 204 | } |
| 205 | |
| 206 | public MenuItem setVisible(boolean visible) { |
| 207 | mFlags = (mFlags & HIDDEN) | (visible ? 0 : HIDDEN); |
| 208 | return this; |
| 209 | } |
| 210 | |
| 211 | public boolean invoke() { |
| 212 | if (mClickListener != null && mClickListener.onMenuItemClick(this)) { |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | if (mIntent != null) { |
| 217 | mContext.startActivity(mIntent); |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | return false; |
| 222 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 223 | |
| 224 | public void setShowAsAction(int show) { |
| 225 | // Do nothing. ActionMenuItems always show as action buttons. |
| 226 | } |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 227 | |
| 228 | public MenuItem setActionView(View actionView) { |
| 229 | throw new UnsupportedOperationException(); |
| 230 | } |
| 231 | |
| 232 | public View getActionView() { |
| 233 | return null; |
| 234 | } |
Adam Powell | 3f476b3 | 2011-01-03 19:25:36 -0800 | [diff] [blame] | 235 | |
| 236 | @Override |
| 237 | public MenuItem setActionView(int resId) { |
| 238 | throw new UnsupportedOperationException(); |
| 239 | } |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 240 | |
| 241 | @Override |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 242 | public ActionProvider getActionProvider() { |
| 243 | return null; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public MenuItem setActionProvider(ActionProvider actionProvider) { |
| 248 | throw new UnsupportedOperationException(); |
| 249 | } |
| 250 | |
| 251 | @Override |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 252 | public MenuItem setShowAsActionFlags(int actionEnum) { |
| 253 | setShowAsAction(actionEnum); |
| 254 | return this; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public boolean expandActionView() { |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public boolean collapseActionView() { |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public boolean isActionViewExpanded() { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public MenuItem setOnActionExpandListener(OnActionExpandListener listener) { |
| 274 | // No need to save the listener; ActionMenuItem does not support collapsing items. |
| 275 | return this; |
| 276 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 277 | } |