The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 19 | import android.annotation.MenuRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.app.Activity; |
| 21 | import android.content.Context; |
Yigit Boyar | b8c19b1 | 2014-09-17 19:23:21 -0700 | [diff] [blame] | 22 | import android.content.ContextWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
| 24 | import android.content.res.XmlResourceParser; |
| 25 | import android.util.AttributeSet; |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 26 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.util.Xml; |
| 28 | |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 29 | import com.android.internal.view.menu.MenuItemImpl; |
| 30 | |
| 31 | import org.xmlpull.v1.XmlPullParser; |
| 32 | import org.xmlpull.v1.XmlPullParserException; |
| 33 | |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 34 | import java.io.IOException; |
| 35 | import java.lang.reflect.Constructor; |
| 36 | import java.lang.reflect.Method; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | /** |
| 39 | * This class is used to instantiate menu XML files into Menu objects. |
| 40 | * <p> |
| 41 | * For performance reasons, menu inflation relies heavily on pre-processing of |
| 42 | * XML files that is done at build time. Therefore, it is not currently possible |
| 43 | * to use MenuInflater with an XmlPullParser over a plain XML file at runtime; |
| 44 | * it only works with an XmlPullParser returned from a compiled resource (R. |
| 45 | * <em>something</em> file.) |
| 46 | */ |
| 47 | public class MenuInflater { |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 48 | private static final String LOG_TAG = "MenuInflater"; |
| 49 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | /** Menu tag name in XML. */ |
| 51 | private static final String XML_MENU = "menu"; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 52 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | /** Group tag name in XML. */ |
| 54 | private static final String XML_GROUP = "group"; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 55 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | /** Item tag name in XML. */ |
| 57 | private static final String XML_ITEM = "item"; |
| 58 | |
| 59 | private static final int NO_ID = 0; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 60 | |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 61 | private static final Class<?>[] ACTION_VIEW_CONSTRUCTOR_SIGNATURE = new Class[] {Context.class}; |
| 62 | |
| 63 | private static final Class<?>[] ACTION_PROVIDER_CONSTRUCTOR_SIGNATURE = ACTION_VIEW_CONSTRUCTOR_SIGNATURE; |
| 64 | |
| 65 | private final Object[] mActionViewConstructorArguments; |
| 66 | |
| 67 | private final Object[] mActionProviderConstructorArguments; |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | private Context mContext; |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 70 | private Object mRealOwner; |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Constructs a menu inflater. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 74 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * @see Activity#getMenuInflater() |
| 76 | */ |
| 77 | public MenuInflater(Context context) { |
| 78 | mContext = context; |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 79 | mActionViewConstructorArguments = new Object[] {context}; |
| 80 | mActionProviderConstructorArguments = mActionViewConstructorArguments; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Constructs a menu inflater. |
| 85 | * |
| 86 | * @see Activity#getMenuInflater() |
| 87 | * @hide |
| 88 | */ |
| 89 | public MenuInflater(Context context, Object realOwner) { |
| 90 | mContext = context; |
| 91 | mRealOwner = realOwner; |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 92 | mActionViewConstructorArguments = new Object[] {context}; |
| 93 | mActionProviderConstructorArguments = mActionViewConstructorArguments; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Inflate a menu hierarchy from the specified XML resource. Throws |
| 98 | * {@link InflateException} if there is an error. |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 99 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | * @param menuRes Resource ID for an XML layout resource to load (e.g., |
| 101 | * <code>R.menu.main_activity</code>) |
| 102 | * @param menu The Menu to inflate into. The items and submenus will be |
| 103 | * added to this Menu. |
| 104 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 105 | public void inflate(@MenuRes int menuRes, Menu menu) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | XmlResourceParser parser = null; |
| 107 | try { |
| 108 | parser = mContext.getResources().getLayout(menuRes); |
| 109 | AttributeSet attrs = Xml.asAttributeSet(parser); |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | parseMenu(parser, attrs, menu); |
| 112 | } catch (XmlPullParserException e) { |
| 113 | throw new InflateException("Error inflating menu XML", e); |
| 114 | } catch (IOException e) { |
| 115 | throw new InflateException("Error inflating menu XML", e); |
| 116 | } finally { |
| 117 | if (parser != null) parser.close(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Called internally to fill the given menu. If a sub menu is seen, it will |
| 123 | * call this recursively. |
| 124 | */ |
| 125 | private void parseMenu(XmlPullParser parser, AttributeSet attrs, Menu menu) |
| 126 | throws XmlPullParserException, IOException { |
| 127 | MenuState menuState = new MenuState(menu); |
| 128 | |
| 129 | int eventType = parser.getEventType(); |
| 130 | String tagName; |
| 131 | boolean lookingForEndOfUnknownTag = false; |
| 132 | String unknownTagName = null; |
| 133 | |
| 134 | // This loop will skip to the menu start tag |
| 135 | do { |
| 136 | if (eventType == XmlPullParser.START_TAG) { |
| 137 | tagName = parser.getName(); |
| 138 | if (tagName.equals(XML_MENU)) { |
| 139 | // Go to next tag |
| 140 | eventType = parser.next(); |
| 141 | break; |
| 142 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 143 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | throw new RuntimeException("Expecting menu, got " + tagName); |
| 145 | } |
| 146 | eventType = parser.next(); |
| 147 | } while (eventType != XmlPullParser.END_DOCUMENT); |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | boolean reachedEndOfMenu = false; |
| 150 | while (!reachedEndOfMenu) { |
| 151 | switch (eventType) { |
| 152 | case XmlPullParser.START_TAG: |
| 153 | if (lookingForEndOfUnknownTag) { |
| 154 | break; |
| 155 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | tagName = parser.getName(); |
| 158 | if (tagName.equals(XML_GROUP)) { |
| 159 | menuState.readGroup(attrs); |
| 160 | } else if (tagName.equals(XML_ITEM)) { |
| 161 | menuState.readItem(attrs); |
| 162 | } else if (tagName.equals(XML_MENU)) { |
| 163 | // A menu start tag denotes a submenu for an item |
| 164 | SubMenu subMenu = menuState.addSubMenuItem(); |
Deepanshu Gupta | 1001961 | 2014-04-18 12:32:38 -0700 | [diff] [blame] | 165 | registerMenu(subMenu, attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | |
| 167 | // Parse the submenu into returned SubMenu |
| 168 | parseMenu(parser, attrs, subMenu); |
| 169 | } else { |
| 170 | lookingForEndOfUnknownTag = true; |
| 171 | unknownTagName = tagName; |
| 172 | } |
| 173 | break; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 174 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | case XmlPullParser.END_TAG: |
| 176 | tagName = parser.getName(); |
| 177 | if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) { |
| 178 | lookingForEndOfUnknownTag = false; |
| 179 | unknownTagName = null; |
| 180 | } else if (tagName.equals(XML_GROUP)) { |
| 181 | menuState.resetGroup(); |
| 182 | } else if (tagName.equals(XML_ITEM)) { |
| 183 | // Add the item if it hasn't been added (if the item was |
| 184 | // a submenu, it would have been added already) |
| 185 | if (!menuState.hasAddedItem()) { |
Adam Powell | 961dd11 | 2011-07-12 14:25:23 -0700 | [diff] [blame] | 186 | if (menuState.itemActionProvider != null && |
| 187 | menuState.itemActionProvider.hasSubMenu()) { |
Deepanshu Gupta | 1001961 | 2014-04-18 12:32:38 -0700 | [diff] [blame] | 188 | registerMenu(menuState.addSubMenuItem(), attrs); |
Adam Powell | 961dd11 | 2011-07-12 14:25:23 -0700 | [diff] [blame] | 189 | } else { |
Deepanshu Gupta | 1001961 | 2014-04-18 12:32:38 -0700 | [diff] [blame] | 190 | registerMenu(menuState.addItem(), attrs); |
Adam Powell | 961dd11 | 2011-07-12 14:25:23 -0700 | [diff] [blame] | 191 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | } |
| 193 | } else if (tagName.equals(XML_MENU)) { |
| 194 | reachedEndOfMenu = true; |
| 195 | } |
| 196 | break; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | case XmlPullParser.END_DOCUMENT: |
| 199 | throw new RuntimeException("Unexpected end of document"); |
| 200 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | eventType = parser.next(); |
| 203 | } |
| 204 | } |
Deepanshu Gupta | 1001961 | 2014-04-18 12:32:38 -0700 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * The method is a hook for layoutlib to do its magic. |
| 208 | * Nothing is needed outside of LayoutLib. However, it should not be deleted because it |
| 209 | * appears to do nothing. |
| 210 | */ |
| 211 | private void registerMenu(@SuppressWarnings("unused") MenuItem item, |
| 212 | @SuppressWarnings("unused") AttributeSet set) { |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * The method is a hook for layoutlib to do its magic. |
| 217 | * Nothing is needed outside of LayoutLib. However, it should not be deleted because it |
| 218 | * appears to do nothing. |
| 219 | */ |
| 220 | private void registerMenu(@SuppressWarnings("unused") SubMenu subMenu, |
| 221 | @SuppressWarnings("unused") AttributeSet set) { |
| 222 | } |
| 223 | |
| 224 | // Needed by layoutlib. |
| 225 | /*package*/ Context getContext() { |
| 226 | return mContext; |
| 227 | } |
| 228 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 229 | private static class InflatedOnMenuItemClickListener |
| 230 | implements MenuItem.OnMenuItemClickListener { |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 231 | private static final Class<?>[] PARAM_TYPES = new Class[] { MenuItem.class }; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 232 | |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 233 | private Object mRealOwner; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 234 | private Method mMethod; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 235 | |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 236 | public InflatedOnMenuItemClickListener(Object realOwner, String methodName) { |
| 237 | mRealOwner = realOwner; |
| 238 | Class<?> c = realOwner.getClass(); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 239 | try { |
| 240 | mMethod = c.getMethod(methodName, PARAM_TYPES); |
| 241 | } catch (Exception e) { |
| 242 | InflateException ex = new InflateException( |
| 243 | "Couldn't resolve menu item onClick handler " + methodName + |
| 244 | " in class " + c.getName()); |
| 245 | ex.initCause(e); |
| 246 | throw ex; |
| 247 | } |
| 248 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 249 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 250 | public boolean onMenuItemClick(MenuItem item) { |
| 251 | try { |
| 252 | if (mMethod.getReturnType() == Boolean.TYPE) { |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 253 | return (Boolean) mMethod.invoke(mRealOwner, item); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 254 | } else { |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 255 | mMethod.invoke(mRealOwner, item); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 256 | return true; |
| 257 | } |
| 258 | } catch (Exception e) { |
| 259 | throw new RuntimeException(e); |
| 260 | } |
| 261 | } |
| 262 | } |
Yigit Boyar | b8c19b1 | 2014-09-17 19:23:21 -0700 | [diff] [blame] | 263 | |
| 264 | private Object getRealOwner() { |
| 265 | if (mRealOwner == null) { |
| 266 | mRealOwner = findRealOwner(mContext); |
| 267 | } |
| 268 | return mRealOwner; |
| 269 | } |
| 270 | |
| 271 | private Object findRealOwner(Object owner) { |
| 272 | if (owner instanceof Activity) { |
| 273 | return owner; |
| 274 | } |
| 275 | if (owner instanceof ContextWrapper) { |
| 276 | return findRealOwner(((ContextWrapper) owner).getBaseContext()); |
| 277 | } |
| 278 | return owner; |
| 279 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 280 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | /** |
| 282 | * State for the current menu. |
| 283 | * <p> |
| 284 | * Groups can not be nested unless there is another menu (which will have |
| 285 | * its state class). |
| 286 | */ |
| 287 | private class MenuState { |
| 288 | private Menu menu; |
| 289 | |
| 290 | /* |
| 291 | * Group state is set on items as they are added, allowing an item to |
| 292 | * override its group state. (As opposed to set on items at the group end tag.) |
| 293 | */ |
| 294 | private int groupId; |
| 295 | private int groupCategory; |
| 296 | private int groupOrder; |
| 297 | private int groupCheckable; |
| 298 | private boolean groupVisible; |
| 299 | private boolean groupEnabled; |
| 300 | |
| 301 | private boolean itemAdded; |
| 302 | private int itemId; |
| 303 | private int itemCategoryOrder; |
Adam Powell | 6650185 | 2011-01-27 17:10:40 -0800 | [diff] [blame] | 304 | private CharSequence itemTitle; |
| 305 | private CharSequence itemTitleCondensed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | private int itemIconResId; |
| 307 | private char itemAlphabeticShortcut; |
| 308 | private char itemNumericShortcut; |
| 309 | /** |
| 310 | * Sync to attrs.xml enum: |
| 311 | * - 0: none |
| 312 | * - 1: all |
| 313 | * - 2: exclusive |
| 314 | */ |
| 315 | private int itemCheckable; |
| 316 | private boolean itemChecked; |
| 317 | private boolean itemVisible; |
| 318 | private boolean itemEnabled; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 319 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 320 | /** |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 321 | * Sync to attrs.xml enum, values in MenuItem: |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 322 | * - 0: never |
| 323 | * - 1: ifRoom |
| 324 | * - 2: always |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 325 | * - -1: Safe sentinel for "no value". |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 326 | */ |
Adam Powell | fbb72fd | 2010-08-16 18:01:21 -0700 | [diff] [blame] | 327 | private int itemShowAsAction; |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 328 | |
| 329 | private int itemActionViewLayout; |
| 330 | private String itemActionViewClassName; |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 331 | private String itemActionProviderClassName; |
| 332 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 333 | private String itemListenerMethodName; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 334 | |
Adam Powell | 961dd11 | 2011-07-12 14:25:23 -0700 | [diff] [blame] | 335 | private ActionProvider itemActionProvider; |
| 336 | |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 337 | private CharSequence itemContentDescription; |
Vladislav Kaznacheev | 6a944ca | 2017-01-19 11:02:12 -0800 | [diff] [blame] | 338 | private CharSequence itemTooltipText; |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 339 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | private static final int defaultGroupId = NO_ID; |
| 341 | private static final int defaultItemId = NO_ID; |
| 342 | private static final int defaultItemCategory = 0; |
| 343 | private static final int defaultItemOrder = 0; |
| 344 | private static final int defaultItemCheckable = 0; |
| 345 | private static final boolean defaultItemChecked = false; |
| 346 | private static final boolean defaultItemVisible = true; |
| 347 | private static final boolean defaultItemEnabled = true; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 348 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | public MenuState(final Menu menu) { |
| 350 | this.menu = menu; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 351 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | resetGroup(); |
| 353 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 354 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | public void resetGroup() { |
| 356 | groupId = defaultGroupId; |
| 357 | groupCategory = defaultItemCategory; |
| 358 | groupOrder = defaultItemOrder; |
| 359 | groupCheckable = defaultItemCheckable; |
| 360 | groupVisible = defaultItemVisible; |
| 361 | groupEnabled = defaultItemEnabled; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Called when the parser is pointing to a group tag. |
| 366 | */ |
| 367 | public void readGroup(AttributeSet attrs) { |
| 368 | TypedArray a = mContext.obtainStyledAttributes(attrs, |
| 369 | com.android.internal.R.styleable.MenuGroup); |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | groupId = a.getResourceId(com.android.internal.R.styleable.MenuGroup_id, defaultGroupId); |
| 372 | groupCategory = a.getInt(com.android.internal.R.styleable.MenuGroup_menuCategory, defaultItemCategory); |
| 373 | groupOrder = a.getInt(com.android.internal.R.styleable.MenuGroup_orderInCategory, defaultItemOrder); |
| 374 | groupCheckable = a.getInt(com.android.internal.R.styleable.MenuGroup_checkableBehavior, defaultItemCheckable); |
| 375 | groupVisible = a.getBoolean(com.android.internal.R.styleable.MenuGroup_visible, defaultItemVisible); |
| 376 | groupEnabled = a.getBoolean(com.android.internal.R.styleable.MenuGroup_enabled, defaultItemEnabled); |
| 377 | |
| 378 | a.recycle(); |
| 379 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 380 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | /** |
| 382 | * Called when the parser is pointing to an item tag. |
| 383 | */ |
| 384 | public void readItem(AttributeSet attrs) { |
| 385 | TypedArray a = mContext.obtainStyledAttributes(attrs, |
| 386 | com.android.internal.R.styleable.MenuItem); |
| 387 | |
| 388 | // Inherit attributes from the group as default value |
| 389 | itemId = a.getResourceId(com.android.internal.R.styleable.MenuItem_id, defaultItemId); |
| 390 | final int category = a.getInt(com.android.internal.R.styleable.MenuItem_menuCategory, groupCategory); |
| 391 | final int order = a.getInt(com.android.internal.R.styleable.MenuItem_orderInCategory, groupOrder); |
| 392 | itemCategoryOrder = (category & Menu.CATEGORY_MASK) | (order & Menu.USER_MASK); |
Adam Powell | 6650185 | 2011-01-27 17:10:40 -0800 | [diff] [blame] | 393 | itemTitle = a.getText(com.android.internal.R.styleable.MenuItem_title); |
| 394 | itemTitleCondensed = a.getText(com.android.internal.R.styleable.MenuItem_titleCondensed); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | itemIconResId = a.getResourceId(com.android.internal.R.styleable.MenuItem_icon, 0); |
| 396 | itemAlphabeticShortcut = |
| 397 | getShortcut(a.getString(com.android.internal.R.styleable.MenuItem_alphabeticShortcut)); |
| 398 | itemNumericShortcut = |
| 399 | getShortcut(a.getString(com.android.internal.R.styleable.MenuItem_numericShortcut)); |
| 400 | if (a.hasValue(com.android.internal.R.styleable.MenuItem_checkable)) { |
| 401 | // Item has attribute checkable, use it |
| 402 | itemCheckable = a.getBoolean(com.android.internal.R.styleable.MenuItem_checkable, false) ? 1 : 0; |
| 403 | } else { |
| 404 | // Item does not have attribute, use the group's (group can have one more state |
| 405 | // for checkable that represents the exclusive checkable) |
| 406 | itemCheckable = groupCheckable; |
| 407 | } |
| 408 | itemChecked = a.getBoolean(com.android.internal.R.styleable.MenuItem_checked, defaultItemChecked); |
| 409 | itemVisible = a.getBoolean(com.android.internal.R.styleable.MenuItem_visible, groupVisible); |
| 410 | itemEnabled = a.getBoolean(com.android.internal.R.styleable.MenuItem_enabled, groupEnabled); |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 411 | itemShowAsAction = a.getInt(com.android.internal.R.styleable.MenuItem_showAsAction, -1); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 412 | itemListenerMethodName = a.getString(com.android.internal.R.styleable.MenuItem_onClick); |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 413 | itemActionViewLayout = a.getResourceId(com.android.internal.R.styleable.MenuItem_actionLayout, 0); |
| 414 | itemActionViewClassName = a.getString(com.android.internal.R.styleable.MenuItem_actionViewClass); |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 415 | itemActionProviderClassName = a.getString(com.android.internal.R.styleable.MenuItem_actionProviderClass); |
| 416 | |
Adam Powell | 961dd11 | 2011-07-12 14:25:23 -0700 | [diff] [blame] | 417 | final boolean hasActionProvider = itemActionProviderClassName != null; |
| 418 | if (hasActionProvider && itemActionViewLayout == 0 && itemActionViewClassName == null) { |
| 419 | itemActionProvider = newInstance(itemActionProviderClassName, |
| 420 | ACTION_PROVIDER_CONSTRUCTOR_SIGNATURE, |
| 421 | mActionProviderConstructorArguments); |
| 422 | } else { |
| 423 | if (hasActionProvider) { |
| 424 | Log.w(LOG_TAG, "Ignoring attribute 'actionProviderClass'." |
| 425 | + " Action view already specified."); |
| 426 | } |
| 427 | itemActionProvider = null; |
| 428 | } |
| 429 | |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 430 | itemContentDescription = |
| 431 | a.getText(com.android.internal.R.styleable.MenuItem_contentDescription); |
Vladislav Kaznacheev | 6a944ca | 2017-01-19 11:02:12 -0800 | [diff] [blame] | 432 | itemTooltipText = a.getText(com.android.internal.R.styleable.MenuItem_tooltipText); |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 433 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | a.recycle(); |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 435 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | itemAdded = false; |
| 437 | } |
| 438 | |
| 439 | private char getShortcut(String shortcutString) { |
| 440 | if (shortcutString == null) { |
| 441 | return 0; |
| 442 | } else { |
| 443 | return shortcutString.charAt(0); |
| 444 | } |
| 445 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 446 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | private void setItem(MenuItem item) { |
| 448 | item.setChecked(itemChecked) |
| 449 | .setVisible(itemVisible) |
| 450 | .setEnabled(itemEnabled) |
| 451 | .setCheckable(itemCheckable >= 1) |
| 452 | .setTitleCondensed(itemTitleCondensed) |
| 453 | .setIcon(itemIconResId) |
| 454 | .setAlphabeticShortcut(itemAlphabeticShortcut) |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 455 | .setNumericShortcut(itemNumericShortcut); |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 456 | |
Adam Powell | 4d9861e | 2010-08-17 11:14:40 -0700 | [diff] [blame] | 457 | if (itemShowAsAction >= 0) { |
| 458 | item.setShowAsAction(itemShowAsAction); |
| 459 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 460 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 461 | if (itemListenerMethodName != null) { |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 462 | if (mContext.isRestricted()) { |
| 463 | throw new IllegalStateException("The android:onClick attribute cannot " |
| 464 | + "be used within a restricted context"); |
| 465 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 466 | item.setOnMenuItemClickListener( |
Yigit Boyar | b8c19b1 | 2014-09-17 19:23:21 -0700 | [diff] [blame] | 467 | new InflatedOnMenuItemClickListener(getRealOwner(), itemListenerMethodName)); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 470 | if (item instanceof MenuItemImpl) { |
| 471 | MenuItemImpl impl = (MenuItemImpl) item; |
| 472 | if (itemCheckable >= 2) { |
| 473 | impl.setExclusiveCheckable(true); |
| 474 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | } |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 476 | |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 477 | boolean actionViewSpecified = false; |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 478 | if (itemActionViewClassName != null) { |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 479 | View actionView = (View) newInstance(itemActionViewClassName, |
| 480 | ACTION_VIEW_CONSTRUCTOR_SIGNATURE, mActionViewConstructorArguments); |
| 481 | item.setActionView(actionView); |
| 482 | actionViewSpecified = true; |
| 483 | } |
| 484 | if (itemActionViewLayout > 0) { |
| 485 | if (!actionViewSpecified) { |
| 486 | item.setActionView(itemActionViewLayout); |
| 487 | actionViewSpecified = true; |
| 488 | } else { |
| 489 | Log.w(LOG_TAG, "Ignoring attribute 'itemActionViewLayout'." |
| 490 | + " Action view already specified."); |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 491 | } |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 492 | } |
Adam Powell | 961dd11 | 2011-07-12 14:25:23 -0700 | [diff] [blame] | 493 | if (itemActionProvider != null) { |
| 494 | item.setActionProvider(itemActionProvider); |
Adam Powell | cf78b3e | 2010-09-12 18:25:23 -0700 | [diff] [blame] | 495 | } |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 496 | |
| 497 | item.setContentDescription(itemContentDescription); |
Vladislav Kaznacheev | 6a944ca | 2017-01-19 11:02:12 -0800 | [diff] [blame] | 498 | item.setTooltipText(itemTooltipText); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | } |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 500 | |
Deepanshu Gupta | 1001961 | 2014-04-18 12:32:38 -0700 | [diff] [blame] | 501 | public MenuItem addItem() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | itemAdded = true; |
Deepanshu Gupta | 1001961 | 2014-04-18 12:32:38 -0700 | [diff] [blame] | 503 | MenuItem item = menu.add(groupId, itemId, itemCategoryOrder, itemTitle); |
| 504 | setItem(item); |
| 505 | return item; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | public SubMenu addSubMenuItem() { |
| 509 | itemAdded = true; |
| 510 | SubMenu subMenu = menu.addSubMenu(groupId, itemId, itemCategoryOrder, itemTitle); |
| 511 | setItem(subMenu.getItem()); |
| 512 | return subMenu; |
| 513 | } |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 514 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | public boolean hasAddedItem() { |
| 516 | return itemAdded; |
| 517 | } |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 518 | |
| 519 | @SuppressWarnings("unchecked") |
| 520 | private <T> T newInstance(String className, Class<?>[] constructorSignature, |
| 521 | Object[] arguments) { |
| 522 | try { |
| 523 | Class<?> clazz = mContext.getClassLoader().loadClass(className); |
| 524 | Constructor<?> constructor = clazz.getConstructor(constructorSignature); |
Alan Viverette | 904de2e | 2015-05-04 10:32:57 -0700 | [diff] [blame] | 525 | constructor.setAccessible(true); |
Svetoslav Ganov | 51ac0e9 | 2011-06-17 13:45:13 -0700 | [diff] [blame] | 526 | return (T) constructor.newInstance(arguments); |
| 527 | } catch (Exception e) { |
| 528 | Log.w(LOG_TAG, "Cannot instantiate class: " + className, e); |
| 529 | } |
| 530 | return null; |
| 531 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | } |