blob: b49a59edbf983b9f3d4cd07695c16c1a1e6c5888 [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 android.view;
18
Adam Powellcf78b3e2010-09-12 18:25:23 -070019import com.android.internal.view.menu.MenuItemImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
21import org.xmlpull.v1.XmlPullParser;
22import org.xmlpull.v1.XmlPullParserException;
23
Tor Norbye7b9c9122013-05-30 16:48:33 -070024import android.annotation.MenuRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.app.Activity;
26import android.content.Context;
Yigit Boyarb8c19b12014-09-17 19:23:21 -070027import android.content.ContextWrapper;
Chris Banes9cc36ca2015-02-23 12:30:13 +000028import android.content.res.ColorStateList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.res.TypedArray;
30import android.content.res.XmlResourceParser;
Chris Banes9cc36ca2015-02-23 12:30:13 +000031import android.graphics.PorterDuff;
32import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.util.AttributeSet;
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -070034import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.util.Xml;
36
Adam Powellcf78b3e2010-09-12 18:25:23 -070037import java.io.IOException;
38import java.lang.reflect.Constructor;
39import java.lang.reflect.Method;
Adam Powell33b97432010-04-20 10:01:14 -070040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041/**
42 * This class is used to instantiate menu XML files into Menu objects.
43 * <p>
44 * For performance reasons, menu inflation relies heavily on pre-processing of
45 * XML files that is done at build time. Therefore, it is not currently possible
46 * to use MenuInflater with an XmlPullParser over a plain XML file at runtime;
47 * it only works with an XmlPullParser returned from a compiled resource (R.
48 * <em>something</em> file.)
49 */
50public class MenuInflater {
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -070051 private static final String LOG_TAG = "MenuInflater";
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 /** Menu tag name in XML. */
54 private static final String XML_MENU = "menu";
55
56 /** Group tag name in XML. */
57 private static final String XML_GROUP = "group";
58
59 /** Item tag name in XML. */
60 private static final String XML_ITEM = "item";
61
62 private static final int NO_ID = 0;
63
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -070064 private static final Class<?>[] ACTION_VIEW_CONSTRUCTOR_SIGNATURE = new Class[] {Context.class};
65
66 private static final Class<?>[] ACTION_PROVIDER_CONSTRUCTOR_SIGNATURE = ACTION_VIEW_CONSTRUCTOR_SIGNATURE;
67
68 private final Object[] mActionViewConstructorArguments;
69
70 private final Object[] mActionProviderConstructorArguments;
Adam Powellcf78b3e2010-09-12 18:25:23 -070071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 private Context mContext;
Dianne Hackborn92751972012-05-18 19:22:14 -070073 private Object mRealOwner;
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -070074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 /**
76 * Constructs a menu inflater.
77 *
78 * @see Activity#getMenuInflater()
79 */
80 public MenuInflater(Context context) {
81 mContext = context;
Dianne Hackborn92751972012-05-18 19:22:14 -070082 mActionViewConstructorArguments = new Object[] {context};
83 mActionProviderConstructorArguments = mActionViewConstructorArguments;
84 }
85
86 /**
87 * Constructs a menu inflater.
88 *
89 * @see Activity#getMenuInflater()
90 * @hide
91 */
92 public MenuInflater(Context context, Object realOwner) {
93 mContext = context;
94 mRealOwner = realOwner;
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -070095 mActionViewConstructorArguments = new Object[] {context};
96 mActionProviderConstructorArguments = mActionViewConstructorArguments;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
98
99 /**
100 * Inflate a menu hierarchy from the specified XML resource. Throws
101 * {@link InflateException} if there is an error.
102 *
103 * @param menuRes Resource ID for an XML layout resource to load (e.g.,
104 * <code>R.menu.main_activity</code>)
105 * @param menu The Menu to inflate into. The items and submenus will be
106 * added to this Menu.
107 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700108 public void inflate(@MenuRes int menuRes, Menu menu) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 XmlResourceParser parser = null;
110 try {
111 parser = mContext.getResources().getLayout(menuRes);
112 AttributeSet attrs = Xml.asAttributeSet(parser);
113
114 parseMenu(parser, attrs, menu);
115 } catch (XmlPullParserException e) {
116 throw new InflateException("Error inflating menu XML", e);
117 } catch (IOException e) {
118 throw new InflateException("Error inflating menu XML", e);
119 } finally {
120 if (parser != null) parser.close();
121 }
122 }
123
124 /**
125 * Called internally to fill the given menu. If a sub menu is seen, it will
126 * call this recursively.
127 */
128 private void parseMenu(XmlPullParser parser, AttributeSet attrs, Menu menu)
129 throws XmlPullParserException, IOException {
130 MenuState menuState = new MenuState(menu);
131
132 int eventType = parser.getEventType();
133 String tagName;
134 boolean lookingForEndOfUnknownTag = false;
135 String unknownTagName = null;
136
137 // This loop will skip to the menu start tag
138 do {
139 if (eventType == XmlPullParser.START_TAG) {
140 tagName = parser.getName();
141 if (tagName.equals(XML_MENU)) {
142 // Go to next tag
143 eventType = parser.next();
144 break;
145 }
146
147 throw new RuntimeException("Expecting menu, got " + tagName);
148 }
149 eventType = parser.next();
150 } while (eventType != XmlPullParser.END_DOCUMENT);
151
152 boolean reachedEndOfMenu = false;
153 while (!reachedEndOfMenu) {
154 switch (eventType) {
155 case XmlPullParser.START_TAG:
156 if (lookingForEndOfUnknownTag) {
157 break;
158 }
159
160 tagName = parser.getName();
161 if (tagName.equals(XML_GROUP)) {
162 menuState.readGroup(attrs);
163 } else if (tagName.equals(XML_ITEM)) {
164 menuState.readItem(attrs);
165 } else if (tagName.equals(XML_MENU)) {
166 // A menu start tag denotes a submenu for an item
167 SubMenu subMenu = menuState.addSubMenuItem();
Deepanshu Gupta10019612014-04-18 12:32:38 -0700168 registerMenu(subMenu, attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
170 // Parse the submenu into returned SubMenu
171 parseMenu(parser, attrs, subMenu);
172 } else {
173 lookingForEndOfUnknownTag = true;
174 unknownTagName = tagName;
175 }
176 break;
177
178 case XmlPullParser.END_TAG:
179 tagName = parser.getName();
180 if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) {
181 lookingForEndOfUnknownTag = false;
182 unknownTagName = null;
183 } else if (tagName.equals(XML_GROUP)) {
184 menuState.resetGroup();
185 } else if (tagName.equals(XML_ITEM)) {
186 // Add the item if it hasn't been added (if the item was
187 // a submenu, it would have been added already)
188 if (!menuState.hasAddedItem()) {
Adam Powell961dd112011-07-12 14:25:23 -0700189 if (menuState.itemActionProvider != null &&
190 menuState.itemActionProvider.hasSubMenu()) {
Deepanshu Gupta10019612014-04-18 12:32:38 -0700191 registerMenu(menuState.addSubMenuItem(), attrs);
Adam Powell961dd112011-07-12 14:25:23 -0700192 } else {
Deepanshu Gupta10019612014-04-18 12:32:38 -0700193 registerMenu(menuState.addItem(), attrs);
Adam Powell961dd112011-07-12 14:25:23 -0700194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 }
196 } else if (tagName.equals(XML_MENU)) {
197 reachedEndOfMenu = true;
198 }
199 break;
200
201 case XmlPullParser.END_DOCUMENT:
202 throw new RuntimeException("Unexpected end of document");
203 }
204
205 eventType = parser.next();
206 }
207 }
Deepanshu Gupta10019612014-04-18 12:32:38 -0700208
209 /**
210 * The method is a hook for layoutlib to do its magic.
211 * Nothing is needed outside of LayoutLib. However, it should not be deleted because it
212 * appears to do nothing.
213 */
214 private void registerMenu(@SuppressWarnings("unused") MenuItem item,
215 @SuppressWarnings("unused") AttributeSet set) {
216 }
217
218 /**
219 * The method is a hook for layoutlib to do its magic.
220 * Nothing is needed outside of LayoutLib. However, it should not be deleted because it
221 * appears to do nothing.
222 */
223 private void registerMenu(@SuppressWarnings("unused") SubMenu subMenu,
224 @SuppressWarnings("unused") AttributeSet set) {
225 }
226
227 // Needed by layoutlib.
228 /*package*/ Context getContext() {
229 return mContext;
230 }
231
Adam Powell33b97432010-04-20 10:01:14 -0700232 private static class InflatedOnMenuItemClickListener
233 implements MenuItem.OnMenuItemClickListener {
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700234 private static final Class<?>[] PARAM_TYPES = new Class[] { MenuItem.class };
Adam Powell33b97432010-04-20 10:01:14 -0700235
Dianne Hackborn92751972012-05-18 19:22:14 -0700236 private Object mRealOwner;
Adam Powell33b97432010-04-20 10:01:14 -0700237 private Method mMethod;
238
Dianne Hackborn92751972012-05-18 19:22:14 -0700239 public InflatedOnMenuItemClickListener(Object realOwner, String methodName) {
240 mRealOwner = realOwner;
241 Class<?> c = realOwner.getClass();
Adam Powell33b97432010-04-20 10:01:14 -0700242 try {
243 mMethod = c.getMethod(methodName, PARAM_TYPES);
244 } catch (Exception e) {
245 InflateException ex = new InflateException(
246 "Couldn't resolve menu item onClick handler " + methodName +
247 " in class " + c.getName());
248 ex.initCause(e);
249 throw ex;
250 }
251 }
252
253 public boolean onMenuItemClick(MenuItem item) {
254 try {
255 if (mMethod.getReturnType() == Boolean.TYPE) {
Dianne Hackborn92751972012-05-18 19:22:14 -0700256 return (Boolean) mMethod.invoke(mRealOwner, item);
Adam Powell33b97432010-04-20 10:01:14 -0700257 } else {
Dianne Hackborn92751972012-05-18 19:22:14 -0700258 mMethod.invoke(mRealOwner, item);
Adam Powell33b97432010-04-20 10:01:14 -0700259 return true;
260 }
261 } catch (Exception e) {
262 throw new RuntimeException(e);
263 }
264 }
265 }
Yigit Boyarb8c19b12014-09-17 19:23:21 -0700266
267 private Object getRealOwner() {
268 if (mRealOwner == null) {
269 mRealOwner = findRealOwner(mContext);
270 }
271 return mRealOwner;
272 }
273
274 private Object findRealOwner(Object owner) {
275 if (owner instanceof Activity) {
276 return owner;
277 }
278 if (owner instanceof ContextWrapper) {
279 return findRealOwner(((ContextWrapper) owner).getBaseContext());
280 }
281 return owner;
282 }
Adam Powell33b97432010-04-20 10:01:14 -0700283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * State for the current menu.
286 * <p>
287 * Groups can not be nested unless there is another menu (which will have
288 * its state class).
289 */
290 private class MenuState {
291 private Menu menu;
292
293 /*
294 * Group state is set on items as they are added, allowing an item to
295 * override its group state. (As opposed to set on items at the group end tag.)
296 */
297 private int groupId;
298 private int groupCategory;
299 private int groupOrder;
300 private int groupCheckable;
301 private boolean groupVisible;
302 private boolean groupEnabled;
303
304 private boolean itemAdded;
305 private int itemId;
306 private int itemCategoryOrder;
Adam Powell66501852011-01-27 17:10:40 -0800307 private CharSequence itemTitle;
308 private CharSequence itemTitleCondensed;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 private int itemIconResId;
310 private char itemAlphabeticShortcut;
311 private char itemNumericShortcut;
312 /**
313 * Sync to attrs.xml enum:
314 * - 0: none
315 * - 1: all
316 * - 2: exclusive
317 */
318 private int itemCheckable;
319 private boolean itemChecked;
320 private boolean itemVisible;
321 private boolean itemEnabled;
322
Adam Powell96675b12010-06-10 18:58:59 -0700323 /**
Adam Powell89e06452010-06-23 20:24:52 -0700324 * Sync to attrs.xml enum, values in MenuItem:
Adam Powell96675b12010-06-10 18:58:59 -0700325 * - 0: never
326 * - 1: ifRoom
327 * - 2: always
Adam Powell4d9861e2010-08-17 11:14:40 -0700328 * - -1: Safe sentinel for "no value".
Adam Powell96675b12010-06-10 18:58:59 -0700329 */
Adam Powellfbb72fd2010-08-16 18:01:21 -0700330 private int itemShowAsAction;
Adam Powellcf78b3e2010-09-12 18:25:23 -0700331
332 private int itemActionViewLayout;
333 private String itemActionViewClassName;
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700334 private String itemActionProviderClassName;
335
Adam Powell33b97432010-04-20 10:01:14 -0700336 private String itemListenerMethodName;
337
Adam Powell961dd112011-07-12 14:25:23 -0700338 private ActionProvider itemActionProvider;
339
Chris Banes9cc36ca2015-02-23 12:30:13 +0000340 private ColorStateList itemIconTintList;
341 private boolean itemIconTintListSet;
342 private PorterDuff.Mode itemIconTintMode;
343 private boolean itemIconTintModeSet;
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 private static final int defaultGroupId = NO_ID;
346 private static final int defaultItemId = NO_ID;
347 private static final int defaultItemCategory = 0;
348 private static final int defaultItemOrder = 0;
349 private static final int defaultItemCheckable = 0;
350 private static final boolean defaultItemChecked = false;
351 private static final boolean defaultItemVisible = true;
352 private static final boolean defaultItemEnabled = true;
353
354 public MenuState(final Menu menu) {
355 this.menu = menu;
356
357 resetGroup();
358 }
359
360 public void resetGroup() {
361 groupId = defaultGroupId;
362 groupCategory = defaultItemCategory;
363 groupOrder = defaultItemOrder;
364 groupCheckable = defaultItemCheckable;
365 groupVisible = defaultItemVisible;
366 groupEnabled = defaultItemEnabled;
367 }
368
369 /**
370 * Called when the parser is pointing to a group tag.
371 */
372 public void readGroup(AttributeSet attrs) {
373 TypedArray a = mContext.obtainStyledAttributes(attrs,
374 com.android.internal.R.styleable.MenuGroup);
375
376 groupId = a.getResourceId(com.android.internal.R.styleable.MenuGroup_id, defaultGroupId);
377 groupCategory = a.getInt(com.android.internal.R.styleable.MenuGroup_menuCategory, defaultItemCategory);
378 groupOrder = a.getInt(com.android.internal.R.styleable.MenuGroup_orderInCategory, defaultItemOrder);
379 groupCheckable = a.getInt(com.android.internal.R.styleable.MenuGroup_checkableBehavior, defaultItemCheckable);
380 groupVisible = a.getBoolean(com.android.internal.R.styleable.MenuGroup_visible, defaultItemVisible);
381 groupEnabled = a.getBoolean(com.android.internal.R.styleable.MenuGroup_enabled, defaultItemEnabled);
382
383 a.recycle();
384 }
385
386 /**
387 * Called when the parser is pointing to an item tag.
388 */
389 public void readItem(AttributeSet attrs) {
390 TypedArray a = mContext.obtainStyledAttributes(attrs,
391 com.android.internal.R.styleable.MenuItem);
392
393 // Inherit attributes from the group as default value
394 itemId = a.getResourceId(com.android.internal.R.styleable.MenuItem_id, defaultItemId);
395 final int category = a.getInt(com.android.internal.R.styleable.MenuItem_menuCategory, groupCategory);
396 final int order = a.getInt(com.android.internal.R.styleable.MenuItem_orderInCategory, groupOrder);
397 itemCategoryOrder = (category & Menu.CATEGORY_MASK) | (order & Menu.USER_MASK);
Adam Powell66501852011-01-27 17:10:40 -0800398 itemTitle = a.getText(com.android.internal.R.styleable.MenuItem_title);
399 itemTitleCondensed = a.getText(com.android.internal.R.styleable.MenuItem_titleCondensed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 itemIconResId = a.getResourceId(com.android.internal.R.styleable.MenuItem_icon, 0);
401 itemAlphabeticShortcut =
402 getShortcut(a.getString(com.android.internal.R.styleable.MenuItem_alphabeticShortcut));
403 itemNumericShortcut =
404 getShortcut(a.getString(com.android.internal.R.styleable.MenuItem_numericShortcut));
405 if (a.hasValue(com.android.internal.R.styleable.MenuItem_checkable)) {
406 // Item has attribute checkable, use it
407 itemCheckable = a.getBoolean(com.android.internal.R.styleable.MenuItem_checkable, false) ? 1 : 0;
408 } else {
409 // Item does not have attribute, use the group's (group can have one more state
410 // for checkable that represents the exclusive checkable)
411 itemCheckable = groupCheckable;
412 }
413 itemChecked = a.getBoolean(com.android.internal.R.styleable.MenuItem_checked, defaultItemChecked);
414 itemVisible = a.getBoolean(com.android.internal.R.styleable.MenuItem_visible, groupVisible);
415 itemEnabled = a.getBoolean(com.android.internal.R.styleable.MenuItem_enabled, groupEnabled);
Adam Powell4d9861e2010-08-17 11:14:40 -0700416 itemShowAsAction = a.getInt(com.android.internal.R.styleable.MenuItem_showAsAction, -1);
Adam Powell33b97432010-04-20 10:01:14 -0700417 itemListenerMethodName = a.getString(com.android.internal.R.styleable.MenuItem_onClick);
Adam Powellcf78b3e2010-09-12 18:25:23 -0700418 itemActionViewLayout = a.getResourceId(com.android.internal.R.styleable.MenuItem_actionLayout, 0);
419 itemActionViewClassName = a.getString(com.android.internal.R.styleable.MenuItem_actionViewClass);
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700420 itemActionProviderClassName = a.getString(com.android.internal.R.styleable.MenuItem_actionProviderClass);
421
Adam Powell961dd112011-07-12 14:25:23 -0700422 final boolean hasActionProvider = itemActionProviderClassName != null;
423 if (hasActionProvider && itemActionViewLayout == 0 && itemActionViewClassName == null) {
424 itemActionProvider = newInstance(itemActionProviderClassName,
425 ACTION_PROVIDER_CONSTRUCTOR_SIGNATURE,
426 mActionProviderConstructorArguments);
427 } else {
428 if (hasActionProvider) {
429 Log.w(LOG_TAG, "Ignoring attribute 'actionProviderClass'."
430 + " Action view already specified.");
431 }
432 itemActionProvider = null;
433 }
434
Chris Banes9cc36ca2015-02-23 12:30:13 +0000435 if (a.hasValueOrEmpty(com.android.internal.R.styleable.MenuItem_iconTint)) {
436 itemIconTintList = a.getColorStateList(
437 com.android.internal.R.styleable.MenuItem_iconTint);
438 itemIconTintListSet = true;
439 } else {
440 itemIconTintList = null;
441 itemIconTintListSet = false;
442 }
443 if (a.hasValueOrEmpty(com.android.internal.R.styleable.MenuItem_iconTintMode)) {
444 itemIconTintMode = Drawable.parseTintMode(
445 a.getInt(com.android.internal.R.styleable.MenuItem_iconTintMode, -1), null);
446 itemIconTintModeSet = true;
447 } else {
448 itemIconTintMode = null;
449 itemIconTintModeSet = false;
450 }
451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 a.recycle();
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 itemAdded = false;
455 }
456
457 private char getShortcut(String shortcutString) {
458 if (shortcutString == null) {
459 return 0;
460 } else {
461 return shortcutString.charAt(0);
462 }
463 }
464
465 private void setItem(MenuItem item) {
466 item.setChecked(itemChecked)
467 .setVisible(itemVisible)
468 .setEnabled(itemEnabled)
469 .setCheckable(itemCheckable >= 1)
470 .setTitleCondensed(itemTitleCondensed)
471 .setIcon(itemIconResId)
472 .setAlphabeticShortcut(itemAlphabeticShortcut)
Adam Powell4d9861e2010-08-17 11:14:40 -0700473 .setNumericShortcut(itemNumericShortcut);
474
475 if (itemShowAsAction >= 0) {
476 item.setShowAsAction(itemShowAsAction);
477 }
Adam Powell33b97432010-04-20 10:01:14 -0700478
479 if (itemListenerMethodName != null) {
Adam Powell5d279772010-07-27 16:34:07 -0700480 if (mContext.isRestricted()) {
481 throw new IllegalStateException("The android:onClick attribute cannot "
482 + "be used within a restricted context");
483 }
Adam Powell33b97432010-04-20 10:01:14 -0700484 item.setOnMenuItemClickListener(
Yigit Boyarb8c19b12014-09-17 19:23:21 -0700485 new InflatedOnMenuItemClickListener(getRealOwner(), itemListenerMethodName));
Adam Powell33b97432010-04-20 10:01:14 -0700486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
Adam Powell96675b12010-06-10 18:58:59 -0700488 if (item instanceof MenuItemImpl) {
489 MenuItemImpl impl = (MenuItemImpl) item;
490 if (itemCheckable >= 2) {
491 impl.setExclusiveCheckable(true);
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
Adam Powellcf78b3e2010-09-12 18:25:23 -0700494
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700495 boolean actionViewSpecified = false;
Adam Powellcf78b3e2010-09-12 18:25:23 -0700496 if (itemActionViewClassName != null) {
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700497 View actionView = (View) newInstance(itemActionViewClassName,
498 ACTION_VIEW_CONSTRUCTOR_SIGNATURE, mActionViewConstructorArguments);
499 item.setActionView(actionView);
500 actionViewSpecified = true;
501 }
502 if (itemActionViewLayout > 0) {
503 if (!actionViewSpecified) {
504 item.setActionView(itemActionViewLayout);
505 actionViewSpecified = true;
506 } else {
507 Log.w(LOG_TAG, "Ignoring attribute 'itemActionViewLayout'."
508 + " Action view already specified.");
Adam Powellcf78b3e2010-09-12 18:25:23 -0700509 }
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700510 }
Adam Powell961dd112011-07-12 14:25:23 -0700511 if (itemActionProvider != null) {
512 item.setActionProvider(itemActionProvider);
Adam Powellcf78b3e2010-09-12 18:25:23 -0700513 }
Chris Banes9cc36ca2015-02-23 12:30:13 +0000514
515 if (itemIconTintListSet) {
516 item.setIconTintList(itemIconTintList);
517 }
518 if (itemIconTintModeSet) {
519 item.setIconTintMode(itemIconTintMode);
520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700522
Deepanshu Gupta10019612014-04-18 12:32:38 -0700523 public MenuItem addItem() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 itemAdded = true;
Deepanshu Gupta10019612014-04-18 12:32:38 -0700525 MenuItem item = menu.add(groupId, itemId, itemCategoryOrder, itemTitle);
526 setItem(item);
527 return item;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529
530 public SubMenu addSubMenuItem() {
531 itemAdded = true;
532 SubMenu subMenu = menu.addSubMenu(groupId, itemId, itemCategoryOrder, itemTitle);
533 setItem(subMenu.getItem());
534 return subMenu;
535 }
536
537 public boolean hasAddedItem() {
538 return itemAdded;
539 }
Svetoslav Ganov51ac0e92011-06-17 13:45:13 -0700540
541 @SuppressWarnings("unchecked")
542 private <T> T newInstance(String className, Class<?>[] constructorSignature,
543 Object[] arguments) {
544 try {
545 Class<?> clazz = mContext.getClassLoader().loadClass(className);
546 Constructor<?> constructor = clazz.getConstructor(constructorSignature);
547 return (T) constructor.newInstance(arguments);
548 } catch (Exception e) {
549 Log.w(LOG_TAG, "Cannot instantiate class: " + className, e);
550 }
551 return null;
552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554}