blob: 9310d14ab6671f0e8371db0f378a1b1cbf5269b1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.internal.view.menu;
18
Adam Powell696cba52011-03-29 10:38:16 -070019import com.android.internal.view.menu.MenuView.ItemView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
Adam Powell151af192010-05-04 14:44:45 -070021import android.content.ActivityNotFoundException;
Adam Powell81cf3ec2011-05-17 10:20:33 -070022import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Intent;
24import android.graphics.drawable.Drawable;
Adam Powell151af192010-05-04 14:44:45 -070025import android.util.Log;
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -070026import android.view.ActionProvider;
Adam Powell696cba52011-03-29 10:38:16 -070027import android.view.ContextMenu.ContextMenuInfo;
Peeyush Agarwale631e322016-10-19 11:41:42 +010028import android.view.KeyEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.view.LayoutInflater;
30import android.view.MenuItem;
31import android.view.SubMenu;
32import android.view.View;
33import android.view.ViewDebug;
Adam Powell81cf3ec2011-05-17 10:20:33 -070034import android.widget.LinearLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36/**
37 * @hide
38 */
39public final class MenuItemImpl implements MenuItem {
Adam Powell151af192010-05-04 14:44:45 -070040 private static final String TAG = "MenuItemImpl";
41
Adam Powelld8404b22010-10-13 14:26:41 -070042 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 Project9066cfe2009-03-03 19:31:44 -080046 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 Agarwale631e322016-10-19 11:41:42 +010054 private int mShortcutNumericModifiers = KeyEvent.META_CTRL_ON;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 private char mShortcutAlphabeticChar;
Peeyush Agarwale631e322016-10-19 11:41:42 +010056 private int mShortcutAlphabeticModifiers = KeyEvent.META_CTRL_ON;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
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 Project9066cfe2009-03-03 19:31:44 -080066
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 Powell96675b12010-06-10 18:58:59 -070081 private static final int IS_ACTION = 0x00000020;
Adam Powell89e06452010-06-23 20:24:52 -070082
Adam Powell96675b12010-06-10 18:58:59 -070083 private int mShowAsAction = SHOW_AS_ACTION_NEVER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Adam Powellcf78b3e2010-09-12 18:25:23 -070085 private View mActionView;
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -070086 private ActionProvider mActionProvider;
Adam Powell8d02dea2011-05-31 21:35:13 -070087 private OnActionExpandListener mOnActionExpandListener;
88 private boolean mIsActionViewExpanded = false;
Adam Powellcf78b3e2010-09-12 18:25:23 -070089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /** 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 Kaznacheev7039cbc2017-01-04 10:15:31 -080096 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private ContextMenuInfo mMenuInfo;
Vladislav Kaznacheev7039cbc2017-01-04 10:15:31 -080098
99 private CharSequence mContentDescription;
Vladislav Kaznacheev6a944ca2017-01-19 11:02:12 -0800100 private CharSequence mTooltipText;
Vladislav Kaznacheev7039cbc2017-01-04 10:15:31 -0800101
Mattias Falkeb573412011-04-19 16:13:14 +0200102 private static String sLanguage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private static String sPrependShortcutLabel;
104 private static String sEnterShortcutLabel;
105 private static String sDeleteShortcutLabel;
106 private static String sSpaceShortcutLabel;
Vladislav Kaznacheev7039cbc2017-01-04 10:15:31 -0800107
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 /**
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700110 * Instantiates this menu item.
111 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * @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 Powell4d9861e2010-08-17 11:14:40 -0700122 CharSequence title, int showAsAction) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
Mattias Falkeb573412011-04-19 16:13:14 +0200124 String lang = menu.getContext().getResources().getConfiguration().locale.toString();
125 if (sPrependShortcutLabel == null || !lang.equals(sLanguage)) {
126 sLanguage = lang;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 // 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 Project9066cfe2009-03-03 19:31:44 -0800138 mMenu = menu;
139 mId = id;
140 mGroup = group;
141 mCategoryOrder = categoryOrder;
142 mOrdering = ordering;
143 mTitle = title;
Adam Powell4d9861e2010-08-17 11:14:40 -0700144 mShowAsAction = showAsAction;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 }
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 Viverette53023ff2015-09-18 13:20:20 -0400158 if (mMenu.dispatchMenuItemSelected(mMenu, this)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 return true;
160 }
161
162 if (mItemCallback != null) {
163 mItemCallback.run();
164 return true;
165 }
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 if (mIntent != null) {
Adam Powell151af192010-05-04 14:44:45 -0700168 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 Project9066cfe2009-03-03 19:31:44 -0800174 }
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700175
Adam Powell961dd112011-07-12 14:25:23 -0700176 if (mActionProvider != null && mActionProvider.onPerformDefaultAction()) {
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700177 return true;
178 }
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 return false;
181 }
182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 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 Powell696cba52011-03-29 10:38:16 -0700194 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
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 Agarwale631e322016-10-19 11:41:42 +0100233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 public char getAlphabeticShortcut() {
235 return mShortcutAlphabeticChar;
236 }
237
Peeyush Agarwale631e322016-10-19 11:41:42 +0100238 public int getAlphabeticModifiers() {
239 return mShortcutAlphabeticModifiers;
240 }
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 public MenuItem setAlphabeticShortcut(char alphaChar) {
243 if (mShortcutAlphabeticChar == alphaChar) return this;
Peeyush Agarwale631e322016-10-19 11:41:42 +0100244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
Peeyush Agarwale631e322016-10-19 11:41:42 +0100246
Adam Powell696cba52011-03-29 10:38:16 -0700247 mMenu.onItemsChanged(false);
Peeyush Agarwale631e322016-10-19 11:41:42 +0100248
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 Agarwal2ef1d0c2017-03-06 17:03:58 +0000258 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
Peeyush Agarwale631e322016-10-19 11:41:42 +0100259 mShortcutAlphabeticModifiers = KeyEvent.normalizeMetaState(alphaModifiers);
260
261 mMenu.onItemsChanged(false);
262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 return this;
264 }
265
266 public char getNumericShortcut() {
267 return mShortcutNumericChar;
268 }
269
Peeyush Agarwale631e322016-10-19 11:41:42 +0100270 public int getNumericModifiers() {
271 return mShortcutNumericModifiers;
272 }
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public MenuItem setNumericShortcut(char numericChar) {
275 if (mShortcutNumericChar == numericChar) return this;
276
277 mShortcutNumericChar = numericChar;
278
Adam Powell696cba52011-03-29 10:38:16 -0700279 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280
281 return this;
282 }
283
Peeyush Agarwale631e322016-10-19 11:41:42 +0100284 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 Project9066cfe2009-03-03 19:31:44 -0800297 public MenuItem setShortcut(char numericChar, char alphaChar) {
298 mShortcutNumericChar = numericChar;
299 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
300
Adam Powell696cba52011-03-29 10:38:16 -0700301 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
303 return this;
304 }
305
Peeyush Agarwale631e322016-10-19 11:41:42 +0100306 public MenuItem setShortcut(char numericChar, int numericModifiers, char alphaChar,
307 int alphaModifiers) {
308 mShortcutNumericChar = numericChar;
309 mShortcutNumericModifiers = KeyEvent.normalizeMetaState(numericModifiers);
Peeyush Agarwal2ef1d0c2017-03-06 17:03:58 +0000310 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
Peeyush Agarwale631e322016-10-19 11:41:42 +0100311 mShortcutAlphabeticModifiers = KeyEvent.normalizeMetaState(alphaModifiers);
312
313 mMenu.onItemsChanged(false);
314
315 return this;
316 }
317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 /**
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 Project9066cfe2009-03-03 19:31:44 -0800370 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 Project9066cfe2009-03-03 19:31:44 -0800379 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 Powell696cba52011-03-29 10:38:16 -0700405 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406
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 Powell696cba52011-03-29 10:38:16 -0700430 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431
432 return this;
433 }
434
435 public Drawable getIcon() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 if (mIconDrawable != null) {
437 return mIconDrawable;
438 }
439
440 if (mIconResId != NO_ICON) {
Chris Banesa41b7892015-06-09 13:36:44 +0000441 Drawable icon = mMenu.getContext().getDrawable(mIconResId);
Adam Powellc4852a32012-03-19 15:35:46 -0700442 mIconResId = NO_ICON;
Chris Banesa41b7892015-06-09 13:36:44 +0000443 mIconDrawable = icon;
444 return icon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
446
447 return null;
448 }
449
450 public MenuItem setIcon(Drawable icon) {
451 mIconResId = NO_ICON;
452 mIconDrawable = icon;
Adam Powell696cba52011-03-29 10:38:16 -0700453 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454
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 Powell696cba52011-03-29 10:38:16 -0700463 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464
465 return this;
466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467
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 Powell696cba52011-03-29 10:38:16 -0700476 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478
479 return this;
480 }
481
Adam Powell696cba52011-03-29 10:38:16 -0700482 public void setExclusiveCheckable(boolean exclusive) {
483 mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
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 Powell696cba52011-03-29 10:38:16 -0700510 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512 }
513
514 public boolean isVisible() {
Adam Powell130b4572012-06-15 19:21:34 -0700515 if (mActionProvider != null && mActionProvider.overridesItemVisibility()) {
516 return (mFlags & HIDDEN) == 0 && mActionProvider.isVisible();
517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 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 Project9066cfe2009-03-03 19:31:44 -0800549
550 @Override
551 public String toString() {
Chet Haasecb923d92014-06-30 17:59:15 -0700552 return mTitle != null ? mTitle.toString() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554
555 void setMenuInfo(ContextMenuInfo menuInfo) {
556 mMenuInfo = menuInfo;
557 }
558
559 public ContextMenuInfo getMenuInfo() {
560 return mMenuInfo;
561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562
Adam Powell35aecd52011-07-01 13:43:49 -0700563 public void actionFormatChanged() {
564 mMenu.onItemActionRequestChanged(this);
565 }
566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 /**
Adam Powell696cba52011-03-29 10:38:16 -0700568 * @return Whether the menu should show icons for menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 */
Adam Powell696cba52011-03-29 10:38:16 -0700570 public boolean shouldShowIcon() {
571 return mMenu.getOptionalIconsVisible();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
Adam Powell96675b12010-06-10 18:58:59 -0700573
574 public boolean isActionButton() {
Adam Powellea1ca952011-06-21 14:07:59 -0700575 return (mFlags & IS_ACTION) == IS_ACTION;
Adam Powell96675b12010-06-10 18:58:59 -0700576 }
577
578 public boolean requestsActionButton() {
Adam Powelld8404b22010-10-13 14:26:41 -0700579 return (mShowAsAction & SHOW_AS_ACTION_IF_ROOM) == SHOW_AS_ACTION_IF_ROOM;
Adam Powell96675b12010-06-10 18:58:59 -0700580 }
581
582 public boolean requiresActionButton() {
Adam Powelld8404b22010-10-13 14:26:41 -0700583 return (mShowAsAction & SHOW_AS_ACTION_ALWAYS) == SHOW_AS_ACTION_ALWAYS;
Adam Powell96675b12010-06-10 18:58:59 -0700584 }
585
586 public void setIsActionButton(boolean isActionButton) {
587 if (isActionButton) {
588 mFlags |= IS_ACTION;
589 } else {
590 mFlags &= ~IS_ACTION;
591 }
592 }
Adam Powell89e06452010-06-23 20:24:52 -0700593
Adam Powelld8404b22010-10-13 14:26:41 -0700594 public boolean showsTextAsAction() {
Adam Powell35aecd52011-07-01 13:43:49 -0700595 return (mShowAsAction & SHOW_AS_ACTION_WITH_TEXT) == SHOW_AS_ACTION_WITH_TEXT;
Adam Powelld8404b22010-10-13 14:26:41 -0700596 }
597
Adam Powell96675b12010-06-10 18:58:59 -0700598 public void setShowAsAction(int actionEnum) {
Adam Powelld8404b22010-10-13 14:26:41 -0700599 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 Powell96675b12010-06-10 18:58:59 -0700611 mShowAsAction = actionEnum;
612 mMenu.onItemActionRequestChanged(this);
613 }
Adam Powellcf78b3e2010-09-12 18:25:23 -0700614
615 public MenuItem setActionView(View view) {
616 mActionView = view;
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700617 mActionProvider = null;
Adam Powell038f1c82011-07-21 14:28:10 -0700618 if (view != null && view.getId() == View.NO_ID && mId > 0) {
619 view.setId(mId);
620 }
Adam Powellabbcc242011-01-24 11:48:54 -0800621 mMenu.onItemActionRequestChanged(this);
Adam Powellcf78b3e2010-09-12 18:25:23 -0700622 return this;
623 }
624
Adam Powell3f476b32011-01-03 19:25:36 -0800625 public MenuItem setActionView(int resId) {
Adam Powell81cf3ec2011-05-17 10:20:33 -0700626 final Context context = mMenu.getContext();
627 final LayoutInflater inflater = LayoutInflater.from(context);
Amith Yamasani10da5902011-07-26 16:14:26 -0700628 setActionView(inflater.inflate(resId, new LinearLayout(context), false));
Adam Powell3f476b32011-01-03 19:25:36 -0800629 return this;
630 }
631
Adam Powellcf78b3e2010-09-12 18:25:23 -0700632 public View getActionView() {
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700633 if (mActionView != null) {
634 return mActionView;
635 } else if (mActionProvider != null) {
Adam Powell690ffb42012-06-04 19:22:45 -0700636 mActionView = mActionProvider.onCreateActionView(this);
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700637 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 Powell39d5c612012-06-16 14:25:38 -0700648 if (mActionProvider != null) {
Chris Banesc8f6ecc2015-06-30 11:16:37 +0100649 mActionProvider.reset();
Adam Powell39d5c612012-06-16 14:25:38 -0700650 }
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700651 mActionView = null;
652 mActionProvider = actionProvider;
Adam Powell23f4cc02011-08-18 10:30:46 -0700653 mMenu.onItemsChanged(true); // Measurement can be changed
Adam Powelldcc55852013-05-03 11:03:10 -0700654 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 Ganov51ac0e92011-06-17 13:45:13 -0700661 return this;
Adam Powellcf78b3e2010-09-12 18:25:23 -0700662 }
Adam Powell8d02dea2011-05-31 21:35:13 -0700663
664 @Override
665 public MenuItem setShowAsActionFlags(int actionEnum) {
666 setShowAsAction(actionEnum);
667 return this;
668 }
669
670 @Override
671 public boolean expandActionView() {
Adam Powell2dc27f22013-07-16 11:35:17 -0700672 if (!hasCollapsibleActionView()) {
Adam Powell8d02dea2011-05-31 21:35:13 -0700673 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 Powellb4c8ba42013-07-11 16:17:10 -0700709 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 Powell8d02dea2011-05-31 21:35:13 -0700716 }
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 Kaznacheev7039cbc2017-01-04 10:15:31 -0800726
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 Kaznacheev6a944ca2017-01-19 11:02:12 -0800742 public MenuItem setTooltipText(CharSequence tooltipText) {
743 mTooltipText = tooltipText;
Vladislav Kaznacheev7039cbc2017-01-04 10:15:31 -0800744
745 mMenu.onItemsChanged(false);
746
747 return this;
748 }
749
750 @Override
Vladislav Kaznacheev6a944ca2017-01-19 11:02:12 -0800751 public CharSequence getTooltipText() {
752 return mTooltipText;
Vladislav Kaznacheev7039cbc2017-01-04 10:15:31 -0800753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754}