SupportMenuInflater.java revision 89208232f3b5d1451408d787872504a190bc7ee0
18262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns/*
28262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * Copyright (C) 2013 The Android Open Source Project
38262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns *
48262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * Licensed under the Apache License, Version 2.0 (the "License");
58262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * you may not use this file except in compliance with the License.
68262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * You may obtain a copy of the License at
78262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns *
88262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns *      http://www.apache.org/licenses/LICENSE-2.0
98262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns *
108262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * Unless required by applicable law or agreed to in writing, software
118262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * distributed under the License is distributed on an "AS IS" BASIS,
128262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * See the License for the specific language governing permissions and
148262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * limitations under the License.
158262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns */
168262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
17da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brownpackage android.support.v7.internal.view;
188262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
198262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.app.Activity;
208262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.content.Context;
218262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.content.res.TypedArray;
228262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.content.res.XmlResourceParser;
2330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.support.v4.view.MenuItemCompat;
2430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.support.v7.appcompat.R;
2530837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.support.v7.internal.view.menu.MenuItemImpl;
2630837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.support.v4.view.ActionProvider;
2730837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.support.v4.internal.view.SupportMenu;
288262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.util.AttributeSet;
298262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.util.Log;
308262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.util.Xml;
318262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.view.InflateException;
3230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.view.Menu;
3330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.view.MenuInflater;
3430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.view.MenuItem;
3530837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport android.view.SubMenu;
368262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport android.view.View;
3730837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport org.xmlpull.v1.XmlPullParser;
3830837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellimport org.xmlpull.v1.XmlPullParserException;
398262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
408262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport java.io.IOException;
418262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport java.lang.reflect.Constructor;
428262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johnsimport java.lang.reflect.Method;
438262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
448262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns/**
458262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * This class is used to instantiate menu XML files into Menu objects.
468262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * <p>
478262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * For performance reasons, menu inflation relies heavily on pre-processing of
488262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * XML files that is done at build time. Therefore, it is not currently possible
49e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes * to use SupportMenuInflater with an XmlPullParser over a plain XML file at runtime;
508262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * it only works with an XmlPullParser returned from a compiled resource (R.
518262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns * <em>something</em> file.)
5289208232f3b5d1451408d787872504a190bc7ee0Chris Banes *
5389208232f3b5d1451408d787872504a190bc7ee0Chris Banes * @hide
548262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns */
5530837f1095c803f332f4a1c3f0917c8afdd50156Adam Powellpublic class SupportMenuInflater extends MenuInflater {
56e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes    private static final String LOG_TAG = "SupportMenuInflater";
578262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
588262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    /** Menu tag name in XML. */
598262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private static final String XML_MENU = "menu";
608262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
618262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    /** Group tag name in XML. */
628262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private static final String XML_GROUP = "group";
638262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
648262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    /** Item tag name in XML. */
658262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private static final String XML_ITEM = "item";
668262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
678262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private static final int NO_ID = 0;
688262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
698262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private static final Class<?>[] ACTION_VIEW_CONSTRUCTOR_SIGNATURE = new Class[] {Context.class};
708262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
718262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private static final Class<?>[] ACTION_PROVIDER_CONSTRUCTOR_SIGNATURE =
728262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            ACTION_VIEW_CONSTRUCTOR_SIGNATURE;
738262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
748262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private final Object[] mActionViewConstructorArguments;
758262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
768262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private final Object[] mActionProviderConstructorArguments;
778262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
788262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private Context mContext;
798262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private Object mRealOwner;
808262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
818262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    /**
828262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * Constructs a menu inflater.
838262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     *
848262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * @see Activity#getMenuInflater()
858262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     */
86e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes    public SupportMenuInflater(Context context) {
8730837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell        super(context);
888262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        mContext = context;
898262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        mRealOwner = context;
908262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        mActionViewConstructorArguments = new Object[] {context};
918262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        mActionProviderConstructorArguments = mActionViewConstructorArguments;
928262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    }
938262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
948262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    /**
958262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * Inflate a menu hierarchy from the specified XML resource. Throws
968262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * {@link InflateException} if there is an error.
978262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     *
988262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * @param menuRes Resource ID for an XML layout resource to load (e.g.,
998262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     *            <code>R.menu.main_activity</code>)
1008262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * @param menu The Menu to inflate into. The items and submenus will be
1018262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     *            added to this Menu.
1028262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     */
103e0f27d39b0a4f0ef30ef6446e7b675279961cc94Chris Banes    @Override
1048262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    public void inflate(int menuRes, Menu menu) {
10503527a1c5ca870353671fd8b97a08a989fe62012Chris Banes        // If we're not dealing with a SupportMenu instance, let super handle
10603527a1c5ca870353671fd8b97a08a989fe62012Chris Banes        if (!(menu instanceof SupportMenu)) {
10703527a1c5ca870353671fd8b97a08a989fe62012Chris Banes            super.inflate(menuRes, menu);
10803527a1c5ca870353671fd8b97a08a989fe62012Chris Banes            return;
10903527a1c5ca870353671fd8b97a08a989fe62012Chris Banes        }
11003527a1c5ca870353671fd8b97a08a989fe62012Chris Banes
1118262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        XmlResourceParser parser = null;
1128262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        try {
1138262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            parser = mContext.getResources().getLayout(menuRes);
1148262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            AttributeSet attrs = Xml.asAttributeSet(parser);
1158262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
1168262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            parseMenu(parser, attrs, menu);
1178262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        } catch (XmlPullParserException e) {
1188262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            throw new InflateException("Error inflating menu XML", e);
1198262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        } catch (IOException e) {
1208262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            throw new InflateException("Error inflating menu XML", e);
1218262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        } finally {
1228262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            if (parser != null) parser.close();
1238262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
1248262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    }
1258262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
1268262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    /**
1278262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * Called internally to fill the given menu. If a sub menu is seen, it will
1288262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * call this recursively.
1298262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     */
1308262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private void parseMenu(XmlPullParser parser, AttributeSet attrs, Menu menu)
1318262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            throws XmlPullParserException, IOException {
1328262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        MenuState menuState = new MenuState(menu);
1338262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
1348262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        int eventType = parser.getEventType();
1358262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        String tagName;
1368262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        boolean lookingForEndOfUnknownTag = false;
1378262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        String unknownTagName = null;
1388262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
1398262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        // This loop will skip to the menu start tag
1408262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        do {
1418262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            if (eventType == XmlPullParser.START_TAG) {
1428262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                tagName = parser.getName();
1438262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                if (tagName.equals(XML_MENU)) {
1448262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    // Go to next tag
1458262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    eventType = parser.next();
1468262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    break;
1478262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                }
1488262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
1498262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                throw new RuntimeException("Expecting menu, got " + tagName);
1508262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
1518262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            eventType = parser.next();
1528262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        } while (eventType != XmlPullParser.END_DOCUMENT);
1538262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
1548262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        boolean reachedEndOfMenu = false;
1558262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        while (!reachedEndOfMenu) {
1568262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            switch (eventType) {
1578262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                case XmlPullParser.START_TAG:
1588262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    if (lookingForEndOfUnknownTag) {
1598262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        break;
1608262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    }
1618262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
1628262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    tagName = parser.getName();
1638262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    if (tagName.equals(XML_GROUP)) {
1648262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        menuState.readGroup(attrs);
1658262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    } else if (tagName.equals(XML_ITEM)) {
1668262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        menuState.readItem(attrs);
1678262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    } else if (tagName.equals(XML_MENU)) {
1688262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        // A menu start tag denotes a submenu for an item
1698262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        SubMenu subMenu = menuState.addSubMenuItem();
1708262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
1718262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        // Parse the submenu into returned SubMenu
1728262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        parseMenu(parser, attrs, subMenu);
1738262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    } else {
1748262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        lookingForEndOfUnknownTag = true;
1758262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        unknownTagName = tagName;
1768262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    }
1778262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    break;
1788262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
1798262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                case XmlPullParser.END_TAG:
1808262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    tagName = parser.getName();
1818262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) {
1828262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        lookingForEndOfUnknownTag = false;
1838262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        unknownTagName = null;
1848262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    } else if (tagName.equals(XML_GROUP)) {
1858262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        menuState.resetGroup();
1868262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    } else if (tagName.equals(XML_ITEM)) {
1878262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        // Add the item if it hasn't been added (if the item was
1888262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        // a submenu, it would have been added already)
1898262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        if (!menuState.hasAddedItem()) {
1908262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                            if (menuState.itemActionProvider != null &&
1918262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                                    menuState.itemActionProvider.hasSubMenu()) {
1928262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                                menuState.addSubMenuItem();
1938262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                            } else {
1948262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                                menuState.addItem();
1958262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                            }
1968262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        }
1978262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    } else if (tagName.equals(XML_MENU)) {
1988262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        reachedEndOfMenu = true;
1998262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    }
2008262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    break;
2018262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2028262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                case XmlPullParser.END_DOCUMENT:
2038262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    throw new RuntimeException("Unexpected end of document");
2048262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
2058262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2068262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            eventType = parser.next();
2078262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
2088262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    }
2098262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2108262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private static class InflatedOnMenuItemClickListener
2118262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            implements MenuItem.OnMenuItemClickListener {
2128262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private static final Class<?>[] PARAM_TYPES = new Class[] { MenuItem.class };
2138262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2148262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private Object mRealOwner;
2158262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private Method mMethod;
2168262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2178262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        public InflatedOnMenuItemClickListener(Object realOwner, String methodName) {
2188262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            mRealOwner = realOwner;
2198262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            Class<?> c = realOwner.getClass();
2208262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            try {
2218262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                mMethod = c.getMethod(methodName, PARAM_TYPES);
2228262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            } catch (Exception e) {
2238262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                InflateException ex = new InflateException(
2248262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        "Couldn't resolve menu item onClick handler " + methodName +
2258262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                                " in class " + c.getName());
2268262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                ex.initCause(e);
2278262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                throw ex;
2288262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
2298262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
2308262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2318262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        public boolean onMenuItemClick(MenuItem item) {
2328262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            try {
2338262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                if (mMethod.getReturnType() == Boolean.TYPE) {
2348262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    return (Boolean) mMethod.invoke(mRealOwner, item);
2358262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                } else {
2368262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    mMethod.invoke(mRealOwner, item);
2378262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    return true;
2388262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                }
2398262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            } catch (Exception e) {
2408262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                throw new RuntimeException(e);
2418262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
2428262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
2438262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    }
2448262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2458262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    /**
2468262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * State for the current menu.
2478262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * <p>
2488262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * Groups can not be nested unless there is another menu (which will have
2498262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     * its state class).
2508262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns     */
2518262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    private class MenuState {
2528262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private Menu menu;
2538262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2548262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        /*
2558262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * Group state is set on items as they are added, allowing an item to
2568262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * override its group state. (As opposed to set on items at the group end tag.)
2578262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         */
2588262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private int groupId;
2598262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private int groupCategory;
2608262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private int groupOrder;
2618262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private int groupCheckable;
2628262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private boolean groupVisible;
2638262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private boolean groupEnabled;
2648262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2658262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private boolean itemAdded;
2668262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private int itemId;
2678262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private int itemCategoryOrder;
2688262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private CharSequence itemTitle;
2698262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private CharSequence itemTitleCondensed;
2708262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private int itemIconResId;
2718262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private char itemAlphabeticShortcut;
2728262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private char itemNumericShortcut;
2738262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        /**
2748262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * Sync to attrs.xml enum:
2758262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * - 0: none
2768262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * - 1: all
2778262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * - 2: exclusive
2788262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         */
2798262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private int itemCheckable;
2808262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private boolean itemChecked;
2818262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private boolean itemVisible;
2828262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private boolean itemEnabled;
2838262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2848262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        /**
2858262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * Sync to attrs.xml enum, values in MenuItem:
2868262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * - 0: never
2878262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * - 1: ifRoom
2888262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * - 2: always
2898262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * - -1: Safe sentinel for "no value".
2908262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         */
2918262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private int itemShowAsAction;
2928262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2938262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private int itemActionViewLayout;
2948262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private String itemActionViewClassName;
2958262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private String itemActionProviderClassName;
2968262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2978262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private String itemListenerMethodName;
2988262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
2998262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private ActionProvider itemActionProvider;
3008262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3018262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private static final int defaultGroupId = NO_ID;
3028262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private static final int defaultItemId = NO_ID;
3038262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private static final int defaultItemCategory = 0;
3048262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private static final int defaultItemOrder = 0;
3058262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private static final int defaultItemCheckable = 0;
3068262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private static final boolean defaultItemChecked = false;
3078262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private static final boolean defaultItemVisible = true;
3088262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private static final boolean defaultItemEnabled = true;
3098262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3108262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        public MenuState(final Menu menu) {
3118262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            this.menu = menu;
3128262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3138262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            resetGroup();
3148262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
3158262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3168262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        public void resetGroup() {
3178262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupId = defaultGroupId;
3188262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupCategory = defaultItemCategory;
3198262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupOrder = defaultItemOrder;
3208262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupCheckable = defaultItemCheckable;
3218262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupVisible = defaultItemVisible;
3228262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupEnabled = defaultItemEnabled;
3238262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
3248262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3258262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        /**
3268262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * Called when the parser is pointing to a group tag.
3278262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         */
3288262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        public void readGroup(AttributeSet attrs) {
3298262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.MenuGroup);
3308262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3318262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupId = a.getResourceId(R.styleable.MenuGroup_android_id, defaultGroupId);
3328262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupCategory = a.getInt(
3338262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    R.styleable.MenuGroup_android_menuCategory, defaultItemCategory);
3348262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupOrder = a.getInt(R.styleable.MenuGroup_android_orderInCategory, defaultItemOrder);
3358262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupCheckable = a.getInt(
3368262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    R.styleable.MenuGroup_android_checkableBehavior, defaultItemCheckable);
3378262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupVisible = a.getBoolean(R.styleable.MenuGroup_android_visible, defaultItemVisible);
3388262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            groupEnabled = a.getBoolean(R.styleable.MenuGroup_android_enabled, defaultItemEnabled);
3398262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3408262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            a.recycle();
3418262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
3428262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3438262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        /**
3448262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         * Called when the parser is pointing to an item tag.
3458262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns         */
3468262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        public void readItem(AttributeSet attrs) {
3478262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.MenuItem);
3488262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3498262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            // Inherit attributes from the group as default value
3508262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemId = a.getResourceId(R.styleable.MenuItem_android_id, defaultItemId);
3518262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            final int category = a.getInt(R.styleable.MenuItem_android_menuCategory, groupCategory);
3528262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            final int order = a.getInt(R.styleable.MenuItem_android_orderInCategory, groupOrder);
35330837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell            itemCategoryOrder = (category & SupportMenu.CATEGORY_MASK) |
35430837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell                    (order & SupportMenu.USER_MASK);
3558262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemTitle = a.getText(R.styleable.MenuItem_android_title);
3568262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemTitleCondensed = a.getText(R.styleable.MenuItem_android_titleCondensed);
3578262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemIconResId = a.getResourceId(R.styleable.MenuItem_android_icon, 0);
3588262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemAlphabeticShortcut =
3598262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    getShortcut(a.getString(R.styleable.MenuItem_android_alphabeticShortcut));
3608262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemNumericShortcut =
3618262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    getShortcut(a.getString(R.styleable.MenuItem_android_numericShortcut));
3628262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            if (a.hasValue(R.styleable.MenuItem_android_checkable)) {
3638262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                // Item has attribute checkable, use it
3648262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                itemCheckable = a.getBoolean(R.styleable.MenuItem_android_checkable, false) ? 1 : 0;
3658262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            } else {
3668262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                // Item does not have attribute, use the group's (group can have one more state
3678262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                // for checkable that represents the exclusive checkable)
3688262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                itemCheckable = groupCheckable;
3698262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
3708262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemChecked = a.getBoolean(R.styleable.MenuItem_android_checked, defaultItemChecked);
3718262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemVisible = a.getBoolean(R.styleable.MenuItem_android_visible, groupVisible);
3728262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemEnabled = a.getBoolean(R.styleable.MenuItem_android_enabled, groupEnabled);
3738262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemShowAsAction = a.getInt(R.styleable.MenuItem_showAsAction, -1);
3748262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemListenerMethodName = a.getString(R.styleable.MenuItem_android_onClick);
3758262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemActionViewLayout = a.getResourceId(R.styleable.MenuItem_actionLayout, 0);
3768262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemActionViewClassName = a.getString(R.styleable.MenuItem_actionViewClass);
3778262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemActionProviderClassName = a.getString(R.styleable.MenuItem_actionProviderClass);
3788262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3798262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            final boolean hasActionProvider = itemActionProviderClassName != null;
3808262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            if (hasActionProvider && itemActionViewLayout == 0 && itemActionViewClassName == null) {
3818262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                itemActionProvider = newInstance(itemActionProviderClassName,
3828262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        ACTION_PROVIDER_CONSTRUCTOR_SIGNATURE,
3838262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        mActionProviderConstructorArguments);
3848262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            } else {
3858262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                if (hasActionProvider) {
3868262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    Log.w(LOG_TAG, "Ignoring attribute 'actionProviderClass'."
3878262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                            + " Action view already specified.");
3888262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                }
3898262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                itemActionProvider = null;
3908262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
3918262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3928262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            a.recycle();
3938262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3948262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemAdded = false;
3958262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
3968262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
3978262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private char getShortcut(String shortcutString) {
3988262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            if (shortcutString == null) {
3998262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                return 0;
4008262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            } else {
4018262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                return shortcutString.charAt(0);
4028262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
4038262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
4048262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
4058262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private void setItem(MenuItem item) {
4068262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            item.setChecked(itemChecked)
4078262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    .setVisible(itemVisible)
4088262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    .setEnabled(itemEnabled)
4098262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    .setCheckable(itemCheckable >= 1)
4108262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    .setTitleCondensed(itemTitleCondensed)
4118262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    .setIcon(itemIconResId)
4128262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    .setAlphabeticShortcut(itemAlphabeticShortcut)
4138262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    .setNumericShortcut(itemNumericShortcut);
4148262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
4158262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            if (itemShowAsAction >= 0) {
41630837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell                MenuItemCompat.setShowAsAction(item, itemShowAsAction);
4178262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
4188262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
4198262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            if (itemListenerMethodName != null) {
4208262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                if (mContext.isRestricted()) {
4218262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    throw new IllegalStateException("The android:onClick attribute cannot "
4228262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                            + "be used within a restricted context");
4238262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                }
4248262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                item.setOnMenuItemClickListener(
4258262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        new InflatedOnMenuItemClickListener(mRealOwner, itemListenerMethodName));
4268262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
4278262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
42830837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell            final MenuItemImpl impl = item instanceof MenuItemImpl ? (MenuItemImpl) item : null;
42930837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell            if (impl != null && itemCheckable >= 2) {
43030837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell                impl.setExclusiveCheckable(true);
4318262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
4328262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
4338262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            boolean actionViewSpecified = false;
4348262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            if (itemActionViewClassName != null) {
4358262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                View actionView = (View) newInstance(itemActionViewClassName,
4368262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                        ACTION_VIEW_CONSTRUCTOR_SIGNATURE, mActionViewConstructorArguments);
43730837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell                MenuItemCompat.setActionView(item, actionView);
4388262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                actionViewSpecified = true;
4398262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
4408262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            if (itemActionViewLayout > 0) {
4418262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                if (!actionViewSpecified) {
44230837f1095c803f332f4a1c3f0917c8afdd50156Adam Powell                    MenuItemCompat.setActionView(item, itemActionViewLayout);
4438262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    actionViewSpecified = true;
4448262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                } else {
4458262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                    Log.w(LOG_TAG, "Ignoring attribute 'itemActionViewLayout'."
4468262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                            + " Action view already specified.");
4478262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                }
4488262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
44934452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell            if (itemActionProvider != null) {
45034452b0d1034da026b8a1d6fe2fe4399844379d6Adam Powell                MenuItemCompat.setActionProvider(item, itemActionProvider);
4518262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
4528262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
4538262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
4548262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        public void addItem() {
4558262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemAdded = true;
4568262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            setItem(menu.add(groupId, itemId, itemCategoryOrder, itemTitle));
4578262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
4588262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
4598262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        public SubMenu addSubMenuItem() {
4608262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            itemAdded = true;
4618262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            SubMenu subMenu = menu.addSubMenu(groupId, itemId, itemCategoryOrder, itemTitle);
4628262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            setItem(subMenu.getItem());
4638262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            return subMenu;
4648262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
4658262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
4668262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        public boolean hasAddedItem() {
4678262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            return itemAdded;
4688262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
4698262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns
4708262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        @SuppressWarnings("unchecked")
4718262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        private <T> T newInstance(String className, Class<?>[] constructorSignature,
4728262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                Object[] arguments) {
4738262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            try {
4748262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                Class<?> clazz = mContext.getClassLoader().loadClass(className);
4758262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                Constructor<?> constructor = clazz.getConstructor(constructorSignature);
4768262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                return (T) constructor.newInstance(arguments);
4778262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            } catch (Exception e) {
4788262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                Log.w(LOG_TAG, "Cannot instantiate class: " + className, e);
4798262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            }
4808262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns            return null;
4818262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        }
4828262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns    }
4838262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns}
484