The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 19 | import com.android.internal.view.menu.MenuView.ItemView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Adam Powell | 151af19 | 2010-05-04 14:44:45 -0700 | [diff] [blame] | 21 | import android.content.ActivityNotFoundException; |
Adam Powell | 81cf3ec | 2011-05-17 10:20:33 -0700 | [diff] [blame] | 22 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.graphics.drawable.Drawable; |
Adam Powell | 151af19 | 2010-05-04 14:44:45 -0700 | [diff] [blame] | 25 | import android.util.Log; |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 26 | import android.view.ActionProvider; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 27 | import android.view.ContextMenu.ContextMenuInfo; |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 28 | import android.view.KeyEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.view.LayoutInflater; |
| 30 | import android.view.MenuItem; |
| 31 | import android.view.SubMenu; |
| 32 | import android.view.View; |
| 33 | import android.view.ViewDebug; |
Adam Powell | 81cf3ec | 2011-05-17 10:20:33 -0700 | [diff] [blame] | 34 | import android.widget.LinearLayout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * @hide |
| 38 | */ |
| 39 | public final class MenuItemImpl implements MenuItem { |
Adam Powell | 151af19 | 2010-05-04 14:44:45 -0700 | [diff] [blame] | 40 | private static final String TAG = "MenuItemImpl"; |
| 41 | |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 42 | private static final int SHOW_AS_ACTION_MASK = SHOW_AS_ACTION_NEVER | |
| 43 | SHOW_AS_ACTION_IF_ROOM | |
| 44 | SHOW_AS_ACTION_ALWAYS; |
| 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | private final int mId; |
| 47 | private final int mGroup; |
| 48 | private final int mCategoryOrder; |
| 49 | private final int mOrdering; |
| 50 | private CharSequence mTitle; |
| 51 | private CharSequence mTitleCondensed; |
| 52 | private Intent mIntent; |
| 53 | private char mShortcutNumericChar; |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 54 | private int mShortcutNumericModifiers = KeyEvent.META_CTRL_ON; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | private char mShortcutAlphabeticChar; |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 56 | private int mShortcutAlphabeticModifiers = KeyEvent.META_CTRL_ON; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | |
| 58 | /** The icon's drawable which is only created as needed */ |
| 59 | private Drawable mIconDrawable; |
| 60 | /** |
| 61 | * The icon's resource ID which is used to get the Drawable when it is |
| 62 | * needed (if the Drawable isn't already obtained--only one of the two is |
| 63 | * needed). |
| 64 | */ |
| 65 | private int mIconResId = NO_ICON; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | |
| 67 | /** The menu to which this item belongs */ |
| 68 | private MenuBuilder mMenu; |
| 69 | /** If this item should launch a sub menu, this is the sub menu to launch */ |
| 70 | private SubMenuBuilder mSubMenu; |
| 71 | |
| 72 | private Runnable mItemCallback; |
| 73 | private MenuItem.OnMenuItemClickListener mClickListener; |
| 74 | |
| 75 | private int mFlags = ENABLED; |
| 76 | private static final int CHECKABLE = 0x00000001; |
| 77 | private static final int CHECKED = 0x00000002; |
| 78 | private static final int EXCLUSIVE = 0x00000004; |
| 79 | private static final int HIDDEN = 0x00000008; |
| 80 | private static final int ENABLED = 0x00000010; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 81 | private static final int IS_ACTION = 0x00000020; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 82 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 83 | private int mShowAsAction = SHOW_AS_ACTION_NEVER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 85 | private View mActionView; |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 86 | private ActionProvider mActionProvider; |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 87 | private OnActionExpandListener mOnActionExpandListener; |
| 88 | private boolean mIsActionViewExpanded = false; |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 89 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | /** Used for the icon resource ID if this item does not have an icon */ |
| 91 | static final int NO_ICON = 0; |
| 92 | |
| 93 | /** |
| 94 | * Current use case is for context menu: Extra information linked to the |
| 95 | * View that added this item to the context menu. |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 96 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | private ContextMenuInfo mMenuInfo; |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 98 | |
| 99 | private CharSequence mContentDescription; |
Vladislav Kaznacheev | 6a944ca | 2017-01-19 11:02:12 -0800 | [diff] [blame] | 100 | private CharSequence mTooltipText; |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 101 | |
Mattias Falk | eb57341 | 2011-04-19 16:13:14 +0200 | [diff] [blame] | 102 | private static String sLanguage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | private static String sPrependShortcutLabel; |
| 104 | private static String sEnterShortcutLabel; |
| 105 | private static String sDeleteShortcutLabel; |
| 106 | private static String sSpaceShortcutLabel; |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 107 | |
| 108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | /** |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 110 | * Instantiates this menu item. |
| 111 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | * @param menu |
| 113 | * @param group Item ordering grouping control. The item will be added after |
| 114 | * all other items whose order is <= this number, and before any |
| 115 | * that are larger than it. This can also be used to define |
| 116 | * groups of items for batch state changes. Normally use 0. |
| 117 | * @param id Unique item ID. Use 0 if you do not need a unique ID. |
| 118 | * @param categoryOrder The ordering for this item. |
| 119 | * @param title The text to display for the item. |
| 120 | */ |
| 121 | MenuItemImpl(MenuBuilder menu, int group, int id, int categoryOrder, int ordering, |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 122 | CharSequence title, int showAsAction) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | |
Mattias Falk | eb57341 | 2011-04-19 16:13:14 +0200 | [diff] [blame] | 124 | String lang = menu.getContext().getResources().getConfiguration().locale.toString(); |
| 125 | if (sPrependShortcutLabel == null || !lang.equals(sLanguage)) { |
| 126 | sLanguage = lang; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | // This is instantiated from the UI thread, so no chance of sync issues |
| 128 | sPrependShortcutLabel = menu.getContext().getResources().getString( |
| 129 | com.android.internal.R.string.prepend_shortcut_label); |
| 130 | sEnterShortcutLabel = menu.getContext().getResources().getString( |
| 131 | com.android.internal.R.string.menu_enter_shortcut_label); |
| 132 | sDeleteShortcutLabel = menu.getContext().getResources().getString( |
| 133 | com.android.internal.R.string.menu_delete_shortcut_label); |
| 134 | sSpaceShortcutLabel = menu.getContext().getResources().getString( |
| 135 | com.android.internal.R.string.menu_space_shortcut_label); |
| 136 | } |
| 137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | mMenu = menu; |
| 139 | mId = id; |
| 140 | mGroup = group; |
| 141 | mCategoryOrder = categoryOrder; |
| 142 | mOrdering = ordering; |
| 143 | mTitle = title; |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 144 | mShowAsAction = showAsAction; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Invokes the item by calling various listeners or callbacks. |
| 149 | * |
| 150 | * @return true if the invocation was handled, false otherwise |
| 151 | */ |
| 152 | public boolean invoke() { |
| 153 | if (mClickListener != null && |
| 154 | mClickListener.onMenuItemClick(this)) { |
| 155 | return true; |
| 156 | } |
| 157 | |
Alan Viverette | 53023ff | 2015-09-18 13:20:20 -0400 | [diff] [blame] | 158 | if (mMenu.dispatchMenuItemSelected(mMenu, this)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
| 162 | if (mItemCallback != null) { |
| 163 | mItemCallback.run(); |
| 164 | return true; |
| 165 | } |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | if (mIntent != null) { |
Adam Powell | 151af19 | 2010-05-04 14:44:45 -0700 | [diff] [blame] | 168 | try { |
| 169 | mMenu.getContext().startActivity(mIntent); |
| 170 | return true; |
| 171 | } catch (ActivityNotFoundException e) { |
| 172 | Log.e(TAG, "Can't find activity to handle intent; ignoring", e); |
| 173 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | } |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 175 | |
Adam Powell | 961dd11 | 2011-07-12 14:25:23 -0700 | [diff] [blame] | 176 | if (mActionProvider != null && mActionProvider.onPerformDefaultAction()) { |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | public boolean isEnabled() { |
| 184 | return (mFlags & ENABLED) != 0; |
| 185 | } |
| 186 | |
| 187 | public MenuItem setEnabled(boolean enabled) { |
| 188 | if (enabled) { |
| 189 | mFlags |= ENABLED; |
| 190 | } else { |
| 191 | mFlags &= ~ENABLED; |
| 192 | } |
| 193 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 194 | mMenu.onItemsChanged(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | |
| 196 | return this; |
| 197 | } |
| 198 | |
| 199 | public int getGroupId() { |
| 200 | return mGroup; |
| 201 | } |
| 202 | |
| 203 | @ViewDebug.CapturedViewProperty |
| 204 | public int getItemId() { |
| 205 | return mId; |
| 206 | } |
| 207 | |
| 208 | public int getOrder() { |
| 209 | return mCategoryOrder; |
| 210 | } |
| 211 | |
| 212 | public int getOrdering() { |
| 213 | return mOrdering; |
| 214 | } |
| 215 | |
| 216 | public Intent getIntent() { |
| 217 | return mIntent; |
| 218 | } |
| 219 | |
| 220 | public MenuItem setIntent(Intent intent) { |
| 221 | mIntent = intent; |
| 222 | return this; |
| 223 | } |
| 224 | |
| 225 | Runnable getCallback() { |
| 226 | return mItemCallback; |
| 227 | } |
| 228 | |
| 229 | public MenuItem setCallback(Runnable callback) { |
| 230 | mItemCallback = callback; |
| 231 | return this; |
| 232 | } |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 233 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | public char getAlphabeticShortcut() { |
| 235 | return mShortcutAlphabeticChar; |
| 236 | } |
| 237 | |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 238 | public int getAlphabeticModifiers() { |
| 239 | return mShortcutAlphabeticModifiers; |
| 240 | } |
| 241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | public MenuItem setAlphabeticShortcut(char alphaChar) { |
| 243 | if (mShortcutAlphabeticChar == alphaChar) return this; |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | mShortcutAlphabeticChar = Character.toLowerCase(alphaChar); |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 246 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 247 | mMenu.onItemsChanged(false); |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 248 | |
| 249 | return this; |
| 250 | } |
| 251 | |
| 252 | public MenuItem setAlphabeticShortcut(char alphaChar, int alphaModifiers){ |
| 253 | if (mShortcutAlphabeticChar == alphaChar && |
| 254 | mShortcutAlphabeticModifiers == alphaModifiers) { |
| 255 | return this; |
| 256 | } |
| 257 | |
Peeyush Agarwal | 2ef1d0c | 2017-03-06 17:03:58 +0000 | [diff] [blame^] | 258 | mShortcutAlphabeticChar = Character.toLowerCase(alphaChar); |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 259 | mShortcutAlphabeticModifiers = KeyEvent.normalizeMetaState(alphaModifiers); |
| 260 | |
| 261 | mMenu.onItemsChanged(false); |
| 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | return this; |
| 264 | } |
| 265 | |
| 266 | public char getNumericShortcut() { |
| 267 | return mShortcutNumericChar; |
| 268 | } |
| 269 | |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 270 | public int getNumericModifiers() { |
| 271 | return mShortcutNumericModifiers; |
| 272 | } |
| 273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | public MenuItem setNumericShortcut(char numericChar) { |
| 275 | if (mShortcutNumericChar == numericChar) return this; |
| 276 | |
| 277 | mShortcutNumericChar = numericChar; |
| 278 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 279 | mMenu.onItemsChanged(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | |
| 281 | return this; |
| 282 | } |
| 283 | |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 284 | public MenuItem setNumericShortcut(char numericChar, int numericModifiers){ |
| 285 | if (mShortcutNumericChar == numericChar && mShortcutNumericModifiers == numericModifiers) { |
| 286 | return this; |
| 287 | } |
| 288 | |
| 289 | mShortcutNumericChar = numericChar; |
| 290 | mShortcutNumericModifiers = KeyEvent.normalizeMetaState(numericModifiers); |
| 291 | |
| 292 | mMenu.onItemsChanged(false); |
| 293 | |
| 294 | return this; |
| 295 | } |
| 296 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | public MenuItem setShortcut(char numericChar, char alphaChar) { |
| 298 | mShortcutNumericChar = numericChar; |
| 299 | mShortcutAlphabeticChar = Character.toLowerCase(alphaChar); |
| 300 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 301 | mMenu.onItemsChanged(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | |
| 303 | return this; |
| 304 | } |
| 305 | |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 306 | public MenuItem setShortcut(char numericChar, int numericModifiers, char alphaChar, |
| 307 | int alphaModifiers) { |
| 308 | mShortcutNumericChar = numericChar; |
| 309 | mShortcutNumericModifiers = KeyEvent.normalizeMetaState(numericModifiers); |
Peeyush Agarwal | 2ef1d0c | 2017-03-06 17:03:58 +0000 | [diff] [blame^] | 310 | mShortcutAlphabeticChar = Character.toLowerCase(alphaChar); |
Peeyush Agarwal | e631e32 | 2016-10-19 11:41:42 +0100 | [diff] [blame] | 311 | mShortcutAlphabeticModifiers = KeyEvent.normalizeMetaState(alphaModifiers); |
| 312 | |
| 313 | mMenu.onItemsChanged(false); |
| 314 | |
| 315 | return this; |
| 316 | } |
| 317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | /** |
| 319 | * @return The active shortcut (based on QWERTY-mode of the menu). |
| 320 | */ |
| 321 | char getShortcut() { |
| 322 | return (mMenu.isQwertyMode() ? mShortcutAlphabeticChar : mShortcutNumericChar); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * @return The label to show for the shortcut. This includes the chording |
| 327 | * key (for example 'Menu+a'). Also, any non-human readable |
| 328 | * characters should be human readable (for example 'Menu+enter'). |
| 329 | */ |
| 330 | String getShortcutLabel() { |
| 331 | |
| 332 | char shortcut = getShortcut(); |
| 333 | if (shortcut == 0) { |
| 334 | return ""; |
| 335 | } |
| 336 | |
| 337 | StringBuilder sb = new StringBuilder(sPrependShortcutLabel); |
| 338 | switch (shortcut) { |
| 339 | |
| 340 | case '\n': |
| 341 | sb.append(sEnterShortcutLabel); |
| 342 | break; |
| 343 | |
| 344 | case '\b': |
| 345 | sb.append(sDeleteShortcutLabel); |
| 346 | break; |
| 347 | |
| 348 | case ' ': |
| 349 | sb.append(sSpaceShortcutLabel); |
| 350 | break; |
| 351 | |
| 352 | default: |
| 353 | sb.append(shortcut); |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | return sb.toString(); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * @return Whether this menu item should be showing shortcuts (depends on |
| 362 | * whether the menu should show shortcuts and whether this item has |
| 363 | * a shortcut defined) |
| 364 | */ |
| 365 | boolean shouldShowShortcut() { |
| 366 | // Show shortcuts if the menu is supposed to show shortcuts AND this item has a shortcut |
| 367 | return mMenu.isShortcutsVisible() && (getShortcut() != 0); |
| 368 | } |
| 369 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | public SubMenu getSubMenu() { |
| 371 | return mSubMenu; |
| 372 | } |
| 373 | |
| 374 | public boolean hasSubMenu() { |
| 375 | return mSubMenu != null; |
| 376 | } |
| 377 | |
| 378 | void setSubMenu(SubMenuBuilder subMenu) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | mSubMenu = subMenu; |
| 380 | |
| 381 | subMenu.setHeaderTitle(getTitle()); |
| 382 | } |
| 383 | |
| 384 | @ViewDebug.CapturedViewProperty |
| 385 | public CharSequence getTitle() { |
| 386 | return mTitle; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Gets the title for a particular {@link ItemView} |
| 391 | * |
| 392 | * @param itemView The ItemView that is receiving the title |
| 393 | * @return Either the title or condensed title based on what the ItemView |
| 394 | * prefers |
| 395 | */ |
| 396 | CharSequence getTitleForItemView(MenuView.ItemView itemView) { |
| 397 | return ((itemView != null) && itemView.prefersCondensedTitle()) |
| 398 | ? getTitleCondensed() |
| 399 | : getTitle(); |
| 400 | } |
| 401 | |
| 402 | public MenuItem setTitle(CharSequence title) { |
| 403 | mTitle = title; |
| 404 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 405 | mMenu.onItemsChanged(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | |
| 407 | if (mSubMenu != null) { |
| 408 | mSubMenu.setHeaderTitle(title); |
| 409 | } |
| 410 | |
| 411 | return this; |
| 412 | } |
| 413 | |
| 414 | public MenuItem setTitle(int title) { |
| 415 | return setTitle(mMenu.getContext().getString(title)); |
| 416 | } |
| 417 | |
| 418 | public CharSequence getTitleCondensed() { |
| 419 | return mTitleCondensed != null ? mTitleCondensed : mTitle; |
| 420 | } |
| 421 | |
| 422 | public MenuItem setTitleCondensed(CharSequence title) { |
| 423 | mTitleCondensed = title; |
| 424 | |
| 425 | // Could use getTitle() in the loop below, but just cache what it would do here |
| 426 | if (title == null) { |
| 427 | title = mTitle; |
| 428 | } |
| 429 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 430 | mMenu.onItemsChanged(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | |
| 432 | return this; |
| 433 | } |
| 434 | |
| 435 | public Drawable getIcon() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | if (mIconDrawable != null) { |
| 437 | return mIconDrawable; |
| 438 | } |
| 439 | |
| 440 | if (mIconResId != NO_ICON) { |
Chris Banes | a41b789 | 2015-06-09 13:36:44 +0000 | [diff] [blame] | 441 | Drawable icon = mMenu.getContext().getDrawable(mIconResId); |
Adam Powell | c4852a3 | 2012-03-19 15:35:46 -0700 | [diff] [blame] | 442 | mIconResId = NO_ICON; |
Chris Banes | a41b789 | 2015-06-09 13:36:44 +0000 | [diff] [blame] | 443 | mIconDrawable = icon; |
| 444 | return icon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | return null; |
| 448 | } |
| 449 | |
| 450 | public MenuItem setIcon(Drawable icon) { |
| 451 | mIconResId = NO_ICON; |
| 452 | mIconDrawable = icon; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 453 | mMenu.onItemsChanged(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | |
| 455 | return this; |
| 456 | } |
| 457 | |
| 458 | public MenuItem setIcon(int iconResId) { |
| 459 | mIconDrawable = null; |
| 460 | mIconResId = iconResId; |
| 461 | |
| 462 | // If we have a view, we need to push the Drawable to them |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 463 | mMenu.onItemsChanged(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | |
| 465 | return this; |
| 466 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | |
| 468 | public boolean isCheckable() { |
| 469 | return (mFlags & CHECKABLE) == CHECKABLE; |
| 470 | } |
| 471 | |
| 472 | public MenuItem setCheckable(boolean checkable) { |
| 473 | final int oldFlags = mFlags; |
| 474 | mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0); |
| 475 | if (oldFlags != mFlags) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 476 | mMenu.onItemsChanged(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | return this; |
| 480 | } |
| 481 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 482 | public void setExclusiveCheckable(boolean exclusive) { |
| 483 | mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | public boolean isExclusiveCheckable() { |
| 487 | return (mFlags & EXCLUSIVE) != 0; |
| 488 | } |
| 489 | |
| 490 | public boolean isChecked() { |
| 491 | return (mFlags & CHECKED) == CHECKED; |
| 492 | } |
| 493 | |
| 494 | public MenuItem setChecked(boolean checked) { |
| 495 | if ((mFlags & EXCLUSIVE) != 0) { |
| 496 | // Call the method on the Menu since it knows about the others in this |
| 497 | // exclusive checkable group |
| 498 | mMenu.setExclusiveItemChecked(this); |
| 499 | } else { |
| 500 | setCheckedInt(checked); |
| 501 | } |
| 502 | |
| 503 | return this; |
| 504 | } |
| 505 | |
| 506 | void setCheckedInt(boolean checked) { |
| 507 | final int oldFlags = mFlags; |
| 508 | mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0); |
| 509 | if (oldFlags != mFlags) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 510 | mMenu.onItemsChanged(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | public boolean isVisible() { |
Adam Powell | 130b457 | 2012-06-15 19:21:34 -0700 | [diff] [blame] | 515 | if (mActionProvider != null && mActionProvider.overridesItemVisibility()) { |
| 516 | return (mFlags & HIDDEN) == 0 && mActionProvider.isVisible(); |
| 517 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | return (mFlags & HIDDEN) == 0; |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Changes the visibility of the item. This method DOES NOT notify the |
| 523 | * parent menu of a change in this item, so this should only be called from |
| 524 | * methods that will eventually trigger this change. If unsure, use {@link #setVisible(boolean)} |
| 525 | * instead. |
| 526 | * |
| 527 | * @param shown Whether to show (true) or hide (false). |
| 528 | * @return Whether the item's shown state was changed |
| 529 | */ |
| 530 | boolean setVisibleInt(boolean shown) { |
| 531 | final int oldFlags = mFlags; |
| 532 | mFlags = (mFlags & ~HIDDEN) | (shown ? 0 : HIDDEN); |
| 533 | return oldFlags != mFlags; |
| 534 | } |
| 535 | |
| 536 | public MenuItem setVisible(boolean shown) { |
| 537 | // Try to set the shown state to the given state. If the shown state was changed |
| 538 | // (i.e. the previous state isn't the same as given state), notify the parent menu that |
| 539 | // the shown state has changed for this item |
| 540 | if (setVisibleInt(shown)) mMenu.onItemVisibleChanged(this); |
| 541 | |
| 542 | return this; |
| 543 | } |
| 544 | |
| 545 | public MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener clickListener) { |
| 546 | mClickListener = clickListener; |
| 547 | return this; |
| 548 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | |
| 550 | @Override |
| 551 | public String toString() { |
Chet Haase | cb923d9 | 2014-06-30 17:59:15 -0700 | [diff] [blame] | 552 | return mTitle != null ? mTitle.toString() : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | void setMenuInfo(ContextMenuInfo menuInfo) { |
| 556 | mMenuInfo = menuInfo; |
| 557 | } |
| 558 | |
| 559 | public ContextMenuInfo getMenuInfo() { |
| 560 | return mMenuInfo; |
| 561 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 563 | public void actionFormatChanged() { |
| 564 | mMenu.onItemActionRequestChanged(this); |
| 565 | } |
| 566 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | /** |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 568 | * @return Whether the menu should show icons for menu items. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | */ |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 570 | public boolean shouldShowIcon() { |
| 571 | return mMenu.getOptionalIconsVisible(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 573 | |
| 574 | public boolean isActionButton() { |
Adam Powell | ea1ca95 | 2011-06-21 14:07:59 -0700 | [diff] [blame] | 575 | return (mFlags & IS_ACTION) == IS_ACTION; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | public boolean requestsActionButton() { |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 579 | return (mShowAsAction & SHOW_AS_ACTION_IF_ROOM) == SHOW_AS_ACTION_IF_ROOM; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | public boolean requiresActionButton() { |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 583 | return (mShowAsAction & SHOW_AS_ACTION_ALWAYS) == SHOW_AS_ACTION_ALWAYS; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | public void setIsActionButton(boolean isActionButton) { |
| 587 | if (isActionButton) { |
| 588 | mFlags |= IS_ACTION; |
| 589 | } else { |
| 590 | mFlags &= ~IS_ACTION; |
| 591 | } |
| 592 | } |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 593 | |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 594 | public boolean showsTextAsAction() { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 595 | return (mShowAsAction & SHOW_AS_ACTION_WITH_TEXT) == SHOW_AS_ACTION_WITH_TEXT; |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 598 | public void setShowAsAction(int actionEnum) { |
Adam Powell | d8404b2 | 2010-10-13 14:26:41 -0700 | [diff] [blame] | 599 | switch (actionEnum & SHOW_AS_ACTION_MASK) { |
| 600 | case SHOW_AS_ACTION_ALWAYS: |
| 601 | case SHOW_AS_ACTION_IF_ROOM: |
| 602 | case SHOW_AS_ACTION_NEVER: |
| 603 | // Looks good! |
| 604 | break; |
| 605 | |
| 606 | default: |
| 607 | // Mutually exclusive options selected! |
| 608 | throw new IllegalArgumentException("SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM," |
| 609 | + " and SHOW_AS_ACTION_NEVER are mutually exclusive."); |
| 610 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 611 | mShowAsAction = actionEnum; |
| 612 | mMenu.onItemActionRequestChanged(this); |
| 613 | } |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 614 | |
| 615 | public MenuItem setActionView(View view) { |
| 616 | mActionView = view; |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 617 | mActionProvider = null; |
Adam Powell | 038f1c8 | 2011-07-21 14:28:10 -0700 | [diff] [blame] | 618 | if (view != null && view.getId() == View.NO_ID && mId > 0) { |
| 619 | view.setId(mId); |
| 620 | } |
Adam Powell | abbcc24 | 2011-01-24 11:48:54 -0800 | [diff] [blame] | 621 | mMenu.onItemActionRequestChanged(this); |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 622 | return this; |
| 623 | } |
| 624 | |
Adam Powell | 3f476b3 | 2011-01-03 19:25:36 -0800 | [diff] [blame] | 625 | public MenuItem setActionView(int resId) { |
Adam Powell | 81cf3ec | 2011-05-17 10:20:33 -0700 | [diff] [blame] | 626 | final Context context = mMenu.getContext(); |
| 627 | final LayoutInflater inflater = LayoutInflater.from(context); |
Amith Yamasani | 10da590 | 2011-07-26 16:14:26 -0700 | [diff] [blame] | 628 | setActionView(inflater.inflate(resId, new LinearLayout(context), false)); |
Adam Powell | 3f476b3 | 2011-01-03 19:25:36 -0800 | [diff] [blame] | 629 | return this; |
| 630 | } |
| 631 | |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 632 | public View getActionView() { |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 633 | if (mActionView != null) { |
| 634 | return mActionView; |
| 635 | } else if (mActionProvider != null) { |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 636 | mActionView = mActionProvider.onCreateActionView(this); |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 637 | return mActionView; |
| 638 | } else { |
| 639 | return null; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | public ActionProvider getActionProvider() { |
| 644 | return mActionProvider; |
| 645 | } |
| 646 | |
| 647 | public MenuItem setActionProvider(ActionProvider actionProvider) { |
Adam Powell | 39d5c61 | 2012-06-16 14:25:38 -0700 | [diff] [blame] | 648 | if (mActionProvider != null) { |
Chris Banes | c8f6ecc | 2015-06-30 11:16:37 +0100 | [diff] [blame] | 649 | mActionProvider.reset(); |
Adam Powell | 39d5c61 | 2012-06-16 14:25:38 -0700 | [diff] [blame] | 650 | } |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 651 | mActionView = null; |
| 652 | mActionProvider = actionProvider; |
Adam Powell | 23f4cc0 | 2011-08-18 10:30:46 -0700 | [diff] [blame] | 653 | mMenu.onItemsChanged(true); // Measurement can be changed |
Adam Powell | dcc5585 | 2013-05-03 11:03:10 -0700 | [diff] [blame] | 654 | if (mActionProvider != null) { |
| 655 | mActionProvider.setVisibilityListener(new ActionProvider.VisibilityListener() { |
| 656 | @Override public void onActionProviderVisibilityChanged(boolean isVisible) { |
| 657 | mMenu.onItemVisibleChanged(MenuItemImpl.this); |
| 658 | } |
| 659 | }); |
| 660 | } |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 661 | return this; |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 662 | } |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 663 | |
| 664 | @Override |
| 665 | public MenuItem setShowAsActionFlags(int actionEnum) { |
| 666 | setShowAsAction(actionEnum); |
| 667 | return this; |
| 668 | } |
| 669 | |
| 670 | @Override |
| 671 | public boolean expandActionView() { |
Adam Powell | 2dc27f2 | 2013-07-16 11:35:17 -0700 | [diff] [blame] | 672 | if (!hasCollapsibleActionView()) { |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 673 | return false; |
| 674 | } |
| 675 | |
| 676 | if (mOnActionExpandListener == null || |
| 677 | mOnActionExpandListener.onMenuItemActionExpand(this)) { |
| 678 | return mMenu.expandItemActionView(this); |
| 679 | } |
| 680 | |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | @Override |
| 685 | public boolean collapseActionView() { |
| 686 | if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) == 0) { |
| 687 | return false; |
| 688 | } |
| 689 | if (mActionView == null) { |
| 690 | // We're already collapsed if we have no action view. |
| 691 | return true; |
| 692 | } |
| 693 | |
| 694 | if (mOnActionExpandListener == null || |
| 695 | mOnActionExpandListener.onMenuItemActionCollapse(this)) { |
| 696 | return mMenu.collapseItemActionView(this); |
| 697 | } |
| 698 | |
| 699 | return false; |
| 700 | } |
| 701 | |
| 702 | @Override |
| 703 | public MenuItem setOnActionExpandListener(OnActionExpandListener listener) { |
| 704 | mOnActionExpandListener = listener; |
| 705 | return this; |
| 706 | } |
| 707 | |
| 708 | public boolean hasCollapsibleActionView() { |
Adam Powell | b4c8ba4 | 2013-07-11 16:17:10 -0700 | [diff] [blame] | 709 | if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) != 0) { |
| 710 | if (mActionView == null && mActionProvider != null) { |
| 711 | mActionView = mActionProvider.onCreateActionView(this); |
| 712 | } |
| 713 | return mActionView != null; |
| 714 | } |
| 715 | return false; |
Adam Powell | 8d02dea | 2011-05-31 21:35:13 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | public void setActionViewExpanded(boolean isExpanded) { |
| 719 | mIsActionViewExpanded = isExpanded; |
| 720 | mMenu.onItemsChanged(false); |
| 721 | } |
| 722 | |
| 723 | public boolean isActionViewExpanded() { |
| 724 | return mIsActionViewExpanded; |
| 725 | } |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 726 | |
| 727 | @Override |
| 728 | public MenuItem setContentDescription(CharSequence contentDescription) { |
| 729 | mContentDescription = contentDescription; |
| 730 | |
| 731 | mMenu.onItemsChanged(false); |
| 732 | |
| 733 | return this; |
| 734 | } |
| 735 | |
| 736 | @Override |
| 737 | public CharSequence getContentDescription() { |
| 738 | return mContentDescription; |
| 739 | } |
| 740 | |
| 741 | @Override |
Vladislav Kaznacheev | 6a944ca | 2017-01-19 11:02:12 -0800 | [diff] [blame] | 742 | public MenuItem setTooltipText(CharSequence tooltipText) { |
| 743 | mTooltipText = tooltipText; |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 744 | |
| 745 | mMenu.onItemsChanged(false); |
| 746 | |
| 747 | return this; |
| 748 | } |
| 749 | |
| 750 | @Override |
Vladislav Kaznacheev | 6a944ca | 2017-01-19 11:02:12 -0800 | [diff] [blame] | 751 | public CharSequence getTooltipText() { |
| 752 | return mTooltipText; |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 753 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | } |