blob: 4d0a3260d4792863c27254bf341d8eb4998ba98f [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.view.LayoutInflater;
29import android.view.MenuItem;
30import android.view.SubMenu;
31import android.view.View;
32import android.view.ViewDebug;
Adam Powell81cf3ec2011-05-17 10:20:33 -070033import android.widget.LinearLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35/**
36 * @hide
37 */
38public final class MenuItemImpl implements MenuItem {
Adam Powell151af192010-05-04 14:44:45 -070039 private static final String TAG = "MenuItemImpl";
40
Adam Powelld8404b22010-10-13 14:26:41 -070041 private static final int SHOW_AS_ACTION_MASK = SHOW_AS_ACTION_NEVER |
42 SHOW_AS_ACTION_IF_ROOM |
43 SHOW_AS_ACTION_ALWAYS;
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 private final int mId;
46 private final int mGroup;
47 private final int mCategoryOrder;
48 private final int mOrdering;
49 private CharSequence mTitle;
50 private CharSequence mTitleCondensed;
51 private Intent mIntent;
52 private char mShortcutNumericChar;
53 private char mShortcutAlphabeticChar;
54
55 /** The icon's drawable which is only created as needed */
56 private Drawable mIconDrawable;
57 /**
58 * The icon's resource ID which is used to get the Drawable when it is
59 * needed (if the Drawable isn't already obtained--only one of the two is
60 * needed).
61 */
62 private int mIconResId = NO_ICON;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
64 /** The menu to which this item belongs */
65 private MenuBuilder mMenu;
66 /** If this item should launch a sub menu, this is the sub menu to launch */
67 private SubMenuBuilder mSubMenu;
68
69 private Runnable mItemCallback;
70 private MenuItem.OnMenuItemClickListener mClickListener;
71
72 private int mFlags = ENABLED;
73 private static final int CHECKABLE = 0x00000001;
74 private static final int CHECKED = 0x00000002;
75 private static final int EXCLUSIVE = 0x00000004;
76 private static final int HIDDEN = 0x00000008;
77 private static final int ENABLED = 0x00000010;
Adam Powell96675b12010-06-10 18:58:59 -070078 private static final int IS_ACTION = 0x00000020;
Adam Powell89e06452010-06-23 20:24:52 -070079
Adam Powell96675b12010-06-10 18:58:59 -070080 private int mShowAsAction = SHOW_AS_ACTION_NEVER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
Adam Powellcf78b3e2010-09-12 18:25:23 -070082 private View mActionView;
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -070083 private ActionProvider mActionProvider;
Adam Powell8d02dea2011-05-31 21:35:13 -070084 private OnActionExpandListener mOnActionExpandListener;
85 private boolean mIsActionViewExpanded = false;
Adam Powellcf78b3e2010-09-12 18:25:23 -070086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 /** Used for the icon resource ID if this item does not have an icon */
88 static final int NO_ICON = 0;
89
90 /**
91 * Current use case is for context menu: Extra information linked to the
92 * View that added this item to the context menu.
93 */
94 private ContextMenuInfo mMenuInfo;
95
Mattias Falkeb573412011-04-19 16:13:14 +020096 private static String sLanguage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private static String sPrependShortcutLabel;
98 private static String sEnterShortcutLabel;
99 private static String sDeleteShortcutLabel;
100 private static String sSpaceShortcutLabel;
101
102
103 /**
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700104 * Instantiates this menu item.
105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * @param menu
107 * @param group Item ordering grouping control. The item will be added after
108 * all other items whose order is <= this number, and before any
109 * that are larger than it. This can also be used to define
110 * groups of items for batch state changes. Normally use 0.
111 * @param id Unique item ID. Use 0 if you do not need a unique ID.
112 * @param categoryOrder The ordering for this item.
113 * @param title The text to display for the item.
114 */
115 MenuItemImpl(MenuBuilder menu, int group, int id, int categoryOrder, int ordering,
Adam Powell4d9861e2010-08-17 11:14:40 -0700116 CharSequence title, int showAsAction) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Mattias Falkeb573412011-04-19 16:13:14 +0200118 String lang = menu.getContext().getResources().getConfiguration().locale.toString();
119 if (sPrependShortcutLabel == null || !lang.equals(sLanguage)) {
120 sLanguage = lang;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 // This is instantiated from the UI thread, so no chance of sync issues
122 sPrependShortcutLabel = menu.getContext().getResources().getString(
123 com.android.internal.R.string.prepend_shortcut_label);
124 sEnterShortcutLabel = menu.getContext().getResources().getString(
125 com.android.internal.R.string.menu_enter_shortcut_label);
126 sDeleteShortcutLabel = menu.getContext().getResources().getString(
127 com.android.internal.R.string.menu_delete_shortcut_label);
128 sSpaceShortcutLabel = menu.getContext().getResources().getString(
129 com.android.internal.R.string.menu_space_shortcut_label);
130 }
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 mMenu = menu;
133 mId = id;
134 mGroup = group;
135 mCategoryOrder = categoryOrder;
136 mOrdering = ordering;
137 mTitle = title;
Adam Powell4d9861e2010-08-17 11:14:40 -0700138 mShowAsAction = showAsAction;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 }
140
141 /**
142 * Invokes the item by calling various listeners or callbacks.
143 *
144 * @return true if the invocation was handled, false otherwise
145 */
146 public boolean invoke() {
147 if (mClickListener != null &&
148 mClickListener.onMenuItemClick(this)) {
149 return true;
150 }
151
Adam Powell696cba52011-03-29 10:38:16 -0700152 if (mMenu.dispatchMenuItemSelected(mMenu.getRootMenu(), this)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 return true;
154 }
155
156 if (mItemCallback != null) {
157 mItemCallback.run();
158 return true;
159 }
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 if (mIntent != null) {
Adam Powell151af192010-05-04 14:44:45 -0700162 try {
163 mMenu.getContext().startActivity(mIntent);
164 return true;
165 } catch (ActivityNotFoundException e) {
166 Log.e(TAG, "Can't find activity to handle intent; ignoring", e);
167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 }
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700169
Adam Powell961dd112011-07-12 14:25:23 -0700170 if (mActionProvider != null && mActionProvider.onPerformDefaultAction()) {
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700171 return true;
172 }
173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 return false;
175 }
176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 public boolean isEnabled() {
178 return (mFlags & ENABLED) != 0;
179 }
180
181 public MenuItem setEnabled(boolean enabled) {
182 if (enabled) {
183 mFlags |= ENABLED;
184 } else {
185 mFlags &= ~ENABLED;
186 }
187
Adam Powell696cba52011-03-29 10:38:16 -0700188 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
190 return this;
191 }
192
193 public int getGroupId() {
194 return mGroup;
195 }
196
197 @ViewDebug.CapturedViewProperty
198 public int getItemId() {
199 return mId;
200 }
201
202 public int getOrder() {
203 return mCategoryOrder;
204 }
205
206 public int getOrdering() {
207 return mOrdering;
208 }
209
210 public Intent getIntent() {
211 return mIntent;
212 }
213
214 public MenuItem setIntent(Intent intent) {
215 mIntent = intent;
216 return this;
217 }
218
219 Runnable getCallback() {
220 return mItemCallback;
221 }
222
223 public MenuItem setCallback(Runnable callback) {
224 mItemCallback = callback;
225 return this;
226 }
227
228 public char getAlphabeticShortcut() {
229 return mShortcutAlphabeticChar;
230 }
231
232 public MenuItem setAlphabeticShortcut(char alphaChar) {
233 if (mShortcutAlphabeticChar == alphaChar) return this;
234
235 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
236
Adam Powell696cba52011-03-29 10:38:16 -0700237 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
239 return this;
240 }
241
242 public char getNumericShortcut() {
243 return mShortcutNumericChar;
244 }
245
246 public MenuItem setNumericShortcut(char numericChar) {
247 if (mShortcutNumericChar == numericChar) return this;
248
249 mShortcutNumericChar = numericChar;
250
Adam Powell696cba52011-03-29 10:38:16 -0700251 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 return this;
254 }
255
256 public MenuItem setShortcut(char numericChar, char alphaChar) {
257 mShortcutNumericChar = numericChar;
258 mShortcutAlphabeticChar = Character.toLowerCase(alphaChar);
259
Adam Powell696cba52011-03-29 10:38:16 -0700260 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261
262 return this;
263 }
264
265 /**
266 * @return The active shortcut (based on QWERTY-mode of the menu).
267 */
268 char getShortcut() {
269 return (mMenu.isQwertyMode() ? mShortcutAlphabeticChar : mShortcutNumericChar);
270 }
271
272 /**
273 * @return The label to show for the shortcut. This includes the chording
274 * key (for example 'Menu+a'). Also, any non-human readable
275 * characters should be human readable (for example 'Menu+enter').
276 */
277 String getShortcutLabel() {
278
279 char shortcut = getShortcut();
280 if (shortcut == 0) {
281 return "";
282 }
283
284 StringBuilder sb = new StringBuilder(sPrependShortcutLabel);
285 switch (shortcut) {
286
287 case '\n':
288 sb.append(sEnterShortcutLabel);
289 break;
290
291 case '\b':
292 sb.append(sDeleteShortcutLabel);
293 break;
294
295 case ' ':
296 sb.append(sSpaceShortcutLabel);
297 break;
298
299 default:
300 sb.append(shortcut);
301 break;
302 }
303
304 return sb.toString();
305 }
306
307 /**
308 * @return Whether this menu item should be showing shortcuts (depends on
309 * whether the menu should show shortcuts and whether this item has
310 * a shortcut defined)
311 */
312 boolean shouldShowShortcut() {
313 // Show shortcuts if the menu is supposed to show shortcuts AND this item has a shortcut
314 return mMenu.isShortcutsVisible() && (getShortcut() != 0);
315 }
316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 public SubMenu getSubMenu() {
318 return mSubMenu;
319 }
320
321 public boolean hasSubMenu() {
322 return mSubMenu != null;
323 }
324
325 void setSubMenu(SubMenuBuilder subMenu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 mSubMenu = subMenu;
327
328 subMenu.setHeaderTitle(getTitle());
329 }
330
331 @ViewDebug.CapturedViewProperty
332 public CharSequence getTitle() {
333 return mTitle;
334 }
335
336 /**
337 * Gets the title for a particular {@link ItemView}
338 *
339 * @param itemView The ItemView that is receiving the title
340 * @return Either the title or condensed title based on what the ItemView
341 * prefers
342 */
343 CharSequence getTitleForItemView(MenuView.ItemView itemView) {
344 return ((itemView != null) && itemView.prefersCondensedTitle())
345 ? getTitleCondensed()
346 : getTitle();
347 }
348
349 public MenuItem setTitle(CharSequence title) {
350 mTitle = title;
351
Adam Powell696cba52011-03-29 10:38:16 -0700352 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353
354 if (mSubMenu != null) {
355 mSubMenu.setHeaderTitle(title);
356 }
357
358 return this;
359 }
360
361 public MenuItem setTitle(int title) {
362 return setTitle(mMenu.getContext().getString(title));
363 }
364
365 public CharSequence getTitleCondensed() {
366 return mTitleCondensed != null ? mTitleCondensed : mTitle;
367 }
368
369 public MenuItem setTitleCondensed(CharSequence title) {
370 mTitleCondensed = title;
371
372 // Could use getTitle() in the loop below, but just cache what it would do here
373 if (title == null) {
374 title = mTitle;
375 }
376
Adam Powell696cba52011-03-29 10:38:16 -0700377 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378
379 return this;
380 }
381
382 public Drawable getIcon() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 if (mIconDrawable != null) {
384 return mIconDrawable;
385 }
386
387 if (mIconResId != NO_ICON) {
Adam Powellc4852a32012-03-19 15:35:46 -0700388 Drawable icon = mMenu.getResources().getDrawable(mIconResId);
389 mIconResId = NO_ICON;
390 mIconDrawable = icon;
391 return icon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 }
393
394 return null;
395 }
396
397 public MenuItem setIcon(Drawable icon) {
398 mIconResId = NO_ICON;
399 mIconDrawable = icon;
Adam Powell696cba52011-03-29 10:38:16 -0700400 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401
402 return this;
403 }
404
405 public MenuItem setIcon(int iconResId) {
406 mIconDrawable = null;
407 mIconResId = iconResId;
408
409 // If we have a view, we need to push the Drawable to them
Adam Powell696cba52011-03-29 10:38:16 -0700410 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411
412 return this;
413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414
415 public boolean isCheckable() {
416 return (mFlags & CHECKABLE) == CHECKABLE;
417 }
418
419 public MenuItem setCheckable(boolean checkable) {
420 final int oldFlags = mFlags;
421 mFlags = (mFlags & ~CHECKABLE) | (checkable ? CHECKABLE : 0);
422 if (oldFlags != mFlags) {
Adam Powell696cba52011-03-29 10:38:16 -0700423 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425
426 return this;
427 }
428
Adam Powell696cba52011-03-29 10:38:16 -0700429 public void setExclusiveCheckable(boolean exclusive) {
430 mFlags = (mFlags & ~EXCLUSIVE) | (exclusive ? EXCLUSIVE : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 }
432
433 public boolean isExclusiveCheckable() {
434 return (mFlags & EXCLUSIVE) != 0;
435 }
436
437 public boolean isChecked() {
438 return (mFlags & CHECKED) == CHECKED;
439 }
440
441 public MenuItem setChecked(boolean checked) {
442 if ((mFlags & EXCLUSIVE) != 0) {
443 // Call the method on the Menu since it knows about the others in this
444 // exclusive checkable group
445 mMenu.setExclusiveItemChecked(this);
446 } else {
447 setCheckedInt(checked);
448 }
449
450 return this;
451 }
452
453 void setCheckedInt(boolean checked) {
454 final int oldFlags = mFlags;
455 mFlags = (mFlags & ~CHECKED) | (checked ? CHECKED : 0);
456 if (oldFlags != mFlags) {
Adam Powell696cba52011-03-29 10:38:16 -0700457 mMenu.onItemsChanged(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
459 }
460
461 public boolean isVisible() {
Adam Powell130b4572012-06-15 19:21:34 -0700462 if (mActionProvider != null && mActionProvider.overridesItemVisibility()) {
463 return (mFlags & HIDDEN) == 0 && mActionProvider.isVisible();
464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 return (mFlags & HIDDEN) == 0;
466 }
467
468 /**
469 * Changes the visibility of the item. This method DOES NOT notify the
470 * parent menu of a change in this item, so this should only be called from
471 * methods that will eventually trigger this change. If unsure, use {@link #setVisible(boolean)}
472 * instead.
473 *
474 * @param shown Whether to show (true) or hide (false).
475 * @return Whether the item's shown state was changed
476 */
477 boolean setVisibleInt(boolean shown) {
478 final int oldFlags = mFlags;
479 mFlags = (mFlags & ~HIDDEN) | (shown ? 0 : HIDDEN);
480 return oldFlags != mFlags;
481 }
482
483 public MenuItem setVisible(boolean shown) {
484 // Try to set the shown state to the given state. If the shown state was changed
485 // (i.e. the previous state isn't the same as given state), notify the parent menu that
486 // the shown state has changed for this item
487 if (setVisibleInt(shown)) mMenu.onItemVisibleChanged(this);
488
489 return this;
490 }
491
492 public MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener clickListener) {
493 mClickListener = clickListener;
494 return this;
495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496
497 @Override
498 public String toString() {
499 return mTitle.toString();
500 }
501
502 void setMenuInfo(ContextMenuInfo menuInfo) {
503 mMenuInfo = menuInfo;
504 }
505
506 public ContextMenuInfo getMenuInfo() {
507 return mMenuInfo;
508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509
Adam Powell35aecd52011-07-01 13:43:49 -0700510 public void actionFormatChanged() {
511 mMenu.onItemActionRequestChanged(this);
512 }
513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 /**
Adam Powell696cba52011-03-29 10:38:16 -0700515 * @return Whether the menu should show icons for menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 */
Adam Powell696cba52011-03-29 10:38:16 -0700517 public boolean shouldShowIcon() {
518 return mMenu.getOptionalIconsVisible();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
Adam Powell96675b12010-06-10 18:58:59 -0700520
521 public boolean isActionButton() {
Adam Powellea1ca952011-06-21 14:07:59 -0700522 return (mFlags & IS_ACTION) == IS_ACTION;
Adam Powell96675b12010-06-10 18:58:59 -0700523 }
524
525 public boolean requestsActionButton() {
Adam Powelld8404b22010-10-13 14:26:41 -0700526 return (mShowAsAction & SHOW_AS_ACTION_IF_ROOM) == SHOW_AS_ACTION_IF_ROOM;
Adam Powell96675b12010-06-10 18:58:59 -0700527 }
528
529 public boolean requiresActionButton() {
Adam Powelld8404b22010-10-13 14:26:41 -0700530 return (mShowAsAction & SHOW_AS_ACTION_ALWAYS) == SHOW_AS_ACTION_ALWAYS;
Adam Powell96675b12010-06-10 18:58:59 -0700531 }
532
533 public void setIsActionButton(boolean isActionButton) {
534 if (isActionButton) {
535 mFlags |= IS_ACTION;
536 } else {
537 mFlags &= ~IS_ACTION;
538 }
539 }
Adam Powell89e06452010-06-23 20:24:52 -0700540
Adam Powelld8404b22010-10-13 14:26:41 -0700541 public boolean showsTextAsAction() {
Adam Powell35aecd52011-07-01 13:43:49 -0700542 return (mShowAsAction & SHOW_AS_ACTION_WITH_TEXT) == SHOW_AS_ACTION_WITH_TEXT;
Adam Powelld8404b22010-10-13 14:26:41 -0700543 }
544
Adam Powell96675b12010-06-10 18:58:59 -0700545 public void setShowAsAction(int actionEnum) {
Adam Powelld8404b22010-10-13 14:26:41 -0700546 switch (actionEnum & SHOW_AS_ACTION_MASK) {
547 case SHOW_AS_ACTION_ALWAYS:
548 case SHOW_AS_ACTION_IF_ROOM:
549 case SHOW_AS_ACTION_NEVER:
550 // Looks good!
551 break;
552
553 default:
554 // Mutually exclusive options selected!
555 throw new IllegalArgumentException("SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM,"
556 + " and SHOW_AS_ACTION_NEVER are mutually exclusive.");
557 }
Adam Powell96675b12010-06-10 18:58:59 -0700558 mShowAsAction = actionEnum;
559 mMenu.onItemActionRequestChanged(this);
560 }
Adam Powellcf78b3e2010-09-12 18:25:23 -0700561
562 public MenuItem setActionView(View view) {
563 mActionView = view;
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700564 mActionProvider = null;
Adam Powell038f1c82011-07-21 14:28:10 -0700565 if (view != null && view.getId() == View.NO_ID && mId > 0) {
566 view.setId(mId);
567 }
Adam Powellabbcc242011-01-24 11:48:54 -0800568 mMenu.onItemActionRequestChanged(this);
Adam Powellcf78b3e2010-09-12 18:25:23 -0700569 return this;
570 }
571
Adam Powell3f476b32011-01-03 19:25:36 -0800572 public MenuItem setActionView(int resId) {
Adam Powell81cf3ec2011-05-17 10:20:33 -0700573 final Context context = mMenu.getContext();
574 final LayoutInflater inflater = LayoutInflater.from(context);
Amith Yamasani10da5902011-07-26 16:14:26 -0700575 setActionView(inflater.inflate(resId, new LinearLayout(context), false));
Adam Powell3f476b32011-01-03 19:25:36 -0800576 return this;
577 }
578
Adam Powellcf78b3e2010-09-12 18:25:23 -0700579 public View getActionView() {
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700580 if (mActionView != null) {
581 return mActionView;
582 } else if (mActionProvider != null) {
Adam Powell690ffb42012-06-04 19:22:45 -0700583 mActionView = mActionProvider.onCreateActionView(this);
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700584 return mActionView;
585 } else {
586 return null;
587 }
588 }
589
590 public ActionProvider getActionProvider() {
591 return mActionProvider;
592 }
593
594 public MenuItem setActionProvider(ActionProvider actionProvider) {
Adam Powell39d5c612012-06-16 14:25:38 -0700595 if (mActionProvider != null) {
596 mActionProvider.setVisibilityListener(null);
597 }
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700598 mActionView = null;
599 mActionProvider = actionProvider;
Adam Powell23f4cc02011-08-18 10:30:46 -0700600 mMenu.onItemsChanged(true); // Measurement can be changed
Adam Powelldcc55852013-05-03 11:03:10 -0700601 if (mActionProvider != null) {
602 mActionProvider.setVisibilityListener(new ActionProvider.VisibilityListener() {
603 @Override public void onActionProviderVisibilityChanged(boolean isVisible) {
604 mMenu.onItemVisibleChanged(MenuItemImpl.this);
605 }
606 });
607 }
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700608 return this;
Adam Powellcf78b3e2010-09-12 18:25:23 -0700609 }
Adam Powell8d02dea2011-05-31 21:35:13 -0700610
611 @Override
612 public MenuItem setShowAsActionFlags(int actionEnum) {
613 setShowAsAction(actionEnum);
614 return this;
615 }
616
617 @Override
618 public boolean expandActionView() {
Adam Powell2dc27f22013-07-16 11:35:17 -0700619 if (!hasCollapsibleActionView()) {
Adam Powell8d02dea2011-05-31 21:35:13 -0700620 return false;
621 }
622
623 if (mOnActionExpandListener == null ||
624 mOnActionExpandListener.onMenuItemActionExpand(this)) {
625 return mMenu.expandItemActionView(this);
626 }
627
628 return false;
629 }
630
631 @Override
632 public boolean collapseActionView() {
633 if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) == 0) {
634 return false;
635 }
636 if (mActionView == null) {
637 // We're already collapsed if we have no action view.
638 return true;
639 }
640
641 if (mOnActionExpandListener == null ||
642 mOnActionExpandListener.onMenuItemActionCollapse(this)) {
643 return mMenu.collapseItemActionView(this);
644 }
645
646 return false;
647 }
648
649 @Override
650 public MenuItem setOnActionExpandListener(OnActionExpandListener listener) {
651 mOnActionExpandListener = listener;
652 return this;
653 }
654
655 public boolean hasCollapsibleActionView() {
Adam Powellb4c8ba42013-07-11 16:17:10 -0700656 if ((mShowAsAction & SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW) != 0) {
657 if (mActionView == null && mActionProvider != null) {
658 mActionView = mActionProvider.onCreateActionView(this);
659 }
660 return mActionView != null;
661 }
662 return false;
Adam Powell8d02dea2011-05-31 21:35:13 -0700663 }
664
665 public void setActionViewExpanded(boolean isExpanded) {
666 mIsActionViewExpanded = isExpanded;
667 mMenu.onItemsChanged(false);
668 }
669
670 public boolean isActionViewExpanded() {
671 return mIsActionViewExpanded;
672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673}