Bug 848909: A submenu of submenus. [r=mfinkle]
authorSriram Ramasubramanian <sriram@mozilla.com>
Thu, 07 Mar 2013 11:52:57 -0800
changeset 137815 09a18d4a9c0e2bdfc35ca431de0b7b2d93fde5ed
parent 137814 342b14e63229a20547ff17a040bf4fa186a44a35
child 137816 740cea2dbf4596548e85c2725ec4cfbb7e603b65
push id24939
push userryanvm@gmail.com
push dateWed, 10 Jul 2013 17:49:39 +0000
treeherdermozilla-central@dde4dcd6fa46 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs848909
milestone25.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 848909: A submenu of submenus. [r=mfinkle]
mobile/android/base/menu/GeckoMenuInflater.java
mobile/android/base/resources/menu-large-v11/browser_app_menu.xml
mobile/android/base/resources/menu-v11/browser_app_menu.xml
mobile/android/base/resources/menu-xlarge-v11/browser_app_menu.xml
--- a/mobile/android/base/menu/GeckoMenuInflater.java
+++ b/mobile/android/base/menu/GeckoMenuInflater.java
@@ -27,122 +27,119 @@ public class GeckoMenuInflater extends M
     private static final String LOGTAG = "GeckoMenuInflater";
 
     private static final String TAG_MENU = "menu";
     private static final String TAG_ITEM = "item";
     private static final int NO_ID = 0;
 
     private Context mContext;
 
-    private boolean isSubMenu;
-
     // Private class to hold the parsed menu item. 
     private class ParsedItem {
         public int id;
         public int order;
         public CharSequence title;
         public int iconRes;
         public boolean checkable;
         public boolean checked;
         public boolean visible;
         public boolean enabled;
         public int showAsAction;
+        public boolean hasSubMenu;
     }
 
     public GeckoMenuInflater(Context context) {
         super(context);
         mContext = context;
-
-        isSubMenu = false;
     }
 
     @Override
     public void inflate(int menuRes, Menu menu) {
 
         // This does not check for a well-formed XML.
 
         XmlResourceParser parser = null;
         try {
             parser = mContext.getResources().getXml(menuRes);
             AttributeSet attrs = Xml.asAttributeSet(parser);
 
-            ParsedItem item = null;
-            SubMenu subMenu = null;
-            MenuItem menuItem = null;
-   
-            String tag;
-            int eventType = parser.getEventType();
-
-            do {
-                tag = parser.getName();
-    
-                switch (eventType) {
-                    case XmlPullParser.START_TAG:
-                        if (tag.equals(TAG_ITEM)) {
-                            // Parse the menu item.
-                            item = new ParsedItem();
-                            parseItem(item, attrs);
-                         } else if (tag.equals(TAG_MENU)) {
-                            if (item != null) {
-                                // Start parsing the sub menu.
-                                isSubMenu = true;
-                                subMenu = menu.addSubMenu(NO_ID, item.id, item.order, item.title);
-                                menuItem = subMenu.getItem();
-
-                                // Set the menu item in main menu.
-                                setValues(item, menuItem);
-                            }
-                        }
-                        break;
-                        
-                    case XmlPullParser.END_TAG:
-                        if (parser.getName().equals(TAG_ITEM)) {
-                            if (isSubMenu && subMenu == null) {
-                                isSubMenu = false;
-                            } else {
-                                // Add the item.
-                                if (subMenu == null)
-                                    menuItem = menu.add(NO_ID, item.id, item.order, item.title);
-                                else
-                                    menuItem = subMenu.add(NO_ID, item.id, item.order, item.title);
-
-                                setValues(item, menuItem);
-                            }
-                        } else if (tag.equals(TAG_MENU)) {
-                            // End of sub menu.
-                            subMenu = null;
-                        }
-                        break;
-                }
-
-                eventType = parser.next();
-
-            } while (eventType != XmlPullParser.END_DOCUMENT);
+            parseMenu(parser, attrs, menu);
 
         } catch (XmlPullParserException e) {
             throw new InflateException("Error inflating menu XML", e);
         } catch (IOException e) {
             throw new InflateException("Error inflating menu XML", e);
         } finally {
             if (parser != null)
                 parser.close();
         }
     }
 
+    private void parseMenu(XmlResourceParser parser, AttributeSet attrs, Menu menu) 
+                           throws XmlPullParserException, IOException {
+        ParsedItem item = null;
+   
+        String tag;
+        int eventType = parser.getEventType();
+
+        do {
+            tag = parser.getName();
+    
+            switch (eventType) {
+                case XmlPullParser.START_TAG:
+                    if (tag.equals(TAG_ITEM)) {
+                        // Parse the menu item.
+                        item = new ParsedItem();
+                        parseItem(item, attrs);
+                     } else if (tag.equals(TAG_MENU)) {
+                        if (item != null) {
+                            // Add the submenu item.
+                            SubMenu subMenu = menu.addSubMenu(NO_ID, item.id, item.order, item.title);
+                            item.hasSubMenu = true;
+
+                            // Set the menu item in main menu.
+                            MenuItem menuItem = subMenu.getItem();
+                            setValues(item, menuItem);
+
+                            // Start parsing the sub menu.
+                            parseMenu(parser, attrs, subMenu);
+                        }
+                    }
+                    break;
+
+                case XmlPullParser.END_TAG:
+                    if (parser.getName().equals(TAG_ITEM)) {
+                        if (!item.hasSubMenu) {
+                            // Add the item.
+                            MenuItem menuItem = menu.add(NO_ID, item.id, item.order, item.title);
+                            setValues(item, menuItem);
+                        }
+                    } else if (tag.equals(TAG_MENU)) {
+                        return;
+                    }
+                    break;
+            }
+
+            eventType = parser.next();
+
+        } while (eventType != XmlPullParser.END_DOCUMENT);
+    }
+
     public void parseItem(ParsedItem item, AttributeSet attrs) {
         TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.MenuItem);
 
         item.id = a.getResourceId(R.styleable.MenuItem_android_id, NO_ID);
         item.order = a.getInt(R.styleable.MenuItem_android_orderInCategory, 0);
         item.title = a.getText(R.styleable.MenuItem_android_title);
         item.iconRes = a.getResourceId(R.styleable.MenuItem_android_icon, 0);
         item.checkable = a.getBoolean(R.styleable.MenuItem_android_checkable, false);
         item.checked = a.getBoolean(R.styleable.MenuItem_android_checked, false);
         item.visible = a.getBoolean(R.styleable.MenuItem_android_visible, true);
         item.enabled = a.getBoolean(R.styleable.MenuItem_android_enabled, true);
+        item.hasSubMenu = false;
 
         if (Build.VERSION.SDK_INT >= 11)
             item.showAsAction = a.getInt(R.styleable.MenuItem_android_showAsAction, 0);
 
         a.recycle();
     }
         
     public void setValues(ParsedItem item, MenuItem menuItem) {
--- a/mobile/android/base/resources/menu-large-v11/browser_app_menu.xml
+++ b/mobile/android/base/resources/menu-large-v11/browser_app_menu.xml
@@ -55,16 +55,17 @@
 
             <item android:id="@+id/addons"
                   android:icon="@drawable/ic_menu_addons"
                   android:title="@string/addons"/>
 
             <item android:id="@+id/apps"
                   android:icon="@drawable/ic_menu_apps"
                   android:title="@string/apps"/>
+
         </menu>
 
     </item>
 
     <item android:id="@+id/char_encoding"
           android:icon="@drawable/ic_menu_character_encoding"
           android:visible="false"
           android:title="@string/char_encoding"/>
--- a/mobile/android/base/resources/menu-v11/browser_app_menu.xml
+++ b/mobile/android/base/resources/menu-v11/browser_app_menu.xml
@@ -56,16 +56,17 @@
 
             <item android:id="@+id/addons"
                   android:icon="@drawable/ic_menu_addons"
                   android:title="@string/addons"/>
 
             <item android:id="@+id/apps"
                   android:icon="@drawable/ic_menu_apps"
                   android:title="@string/apps"/>
+
         </menu>
 
     </item>
 
     <item android:id="@+id/char_encoding"
           android:icon="@drawable/ic_menu_character_encoding"
           android:visible="false"
           android:title="@string/char_encoding"/>
--- a/mobile/android/base/resources/menu-xlarge-v11/browser_app_menu.xml
+++ b/mobile/android/base/resources/menu-xlarge-v11/browser_app_menu.xml
@@ -56,16 +56,17 @@
 
             <item android:id="@+id/addons"
                   android:icon="@drawable/ic_menu_addons"
                   android:title="@string/addons"/>
 
             <item android:id="@+id/apps"
                   android:icon="@drawable/ic_menu_apps"
                   android:title="@string/apps"/>
+
         </menu>
 
     </item>
 
     <item android:id="@+id/char_encoding"
           android:icon="@drawable/ic_menu_character_encoding"
           android:visible="false"
           android:title="@string/char_encoding"/>