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