Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.menu; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.R; |
michael@0 | 8 | |
michael@0 | 9 | import org.xmlpull.v1.XmlPullParser; |
michael@0 | 10 | import org.xmlpull.v1.XmlPullParserException; |
michael@0 | 11 | |
michael@0 | 12 | import android.content.Context; |
michael@0 | 13 | import android.content.res.TypedArray; |
michael@0 | 14 | import android.content.res.XmlResourceParser; |
michael@0 | 15 | import android.os.Build; |
michael@0 | 16 | import android.util.AttributeSet; |
michael@0 | 17 | import android.util.Xml; |
michael@0 | 18 | import android.view.InflateException; |
michael@0 | 19 | import android.view.Menu; |
michael@0 | 20 | import android.view.MenuInflater; |
michael@0 | 21 | import android.view.MenuItem; |
michael@0 | 22 | import android.view.SubMenu; |
michael@0 | 23 | |
michael@0 | 24 | import java.io.IOException; |
michael@0 | 25 | |
michael@0 | 26 | public class GeckoMenuInflater extends MenuInflater { |
michael@0 | 27 | private static final String LOGTAG = "GeckoMenuInflater"; |
michael@0 | 28 | |
michael@0 | 29 | private static final String TAG_MENU = "menu"; |
michael@0 | 30 | private static final String TAG_ITEM = "item"; |
michael@0 | 31 | private static final int NO_ID = 0; |
michael@0 | 32 | |
michael@0 | 33 | private Context mContext; |
michael@0 | 34 | |
michael@0 | 35 | // Private class to hold the parsed menu item. |
michael@0 | 36 | private class ParsedItem { |
michael@0 | 37 | public int id; |
michael@0 | 38 | public int order; |
michael@0 | 39 | public CharSequence title; |
michael@0 | 40 | public int iconRes; |
michael@0 | 41 | public boolean checkable; |
michael@0 | 42 | public boolean checked; |
michael@0 | 43 | public boolean visible; |
michael@0 | 44 | public boolean enabled; |
michael@0 | 45 | public int showAsAction; |
michael@0 | 46 | public boolean hasSubMenu; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | public GeckoMenuInflater(Context context) { |
michael@0 | 50 | super(context); |
michael@0 | 51 | mContext = context; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | @Override |
michael@0 | 55 | public void inflate(int menuRes, Menu menu) { |
michael@0 | 56 | |
michael@0 | 57 | // This does not check for a well-formed XML. |
michael@0 | 58 | |
michael@0 | 59 | XmlResourceParser parser = null; |
michael@0 | 60 | try { |
michael@0 | 61 | parser = mContext.getResources().getXml(menuRes); |
michael@0 | 62 | AttributeSet attrs = Xml.asAttributeSet(parser); |
michael@0 | 63 | |
michael@0 | 64 | parseMenu(parser, attrs, menu); |
michael@0 | 65 | |
michael@0 | 66 | } catch (XmlPullParserException e) { |
michael@0 | 67 | throw new InflateException("Error inflating menu XML", e); |
michael@0 | 68 | } catch (IOException e) { |
michael@0 | 69 | throw new InflateException("Error inflating menu XML", e); |
michael@0 | 70 | } finally { |
michael@0 | 71 | if (parser != null) |
michael@0 | 72 | parser.close(); |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | private void parseMenu(XmlResourceParser parser, AttributeSet attrs, Menu menu) |
michael@0 | 77 | throws XmlPullParserException, IOException { |
michael@0 | 78 | ParsedItem item = null; |
michael@0 | 79 | |
michael@0 | 80 | String tag; |
michael@0 | 81 | int eventType = parser.getEventType(); |
michael@0 | 82 | |
michael@0 | 83 | do { |
michael@0 | 84 | tag = parser.getName(); |
michael@0 | 85 | |
michael@0 | 86 | switch (eventType) { |
michael@0 | 87 | case XmlPullParser.START_TAG: |
michael@0 | 88 | if (tag.equals(TAG_ITEM)) { |
michael@0 | 89 | // Parse the menu item. |
michael@0 | 90 | item = new ParsedItem(); |
michael@0 | 91 | parseItem(item, attrs); |
michael@0 | 92 | } else if (tag.equals(TAG_MENU)) { |
michael@0 | 93 | if (item != null) { |
michael@0 | 94 | // Add the submenu item. |
michael@0 | 95 | SubMenu subMenu = menu.addSubMenu(NO_ID, item.id, item.order, item.title); |
michael@0 | 96 | item.hasSubMenu = true; |
michael@0 | 97 | |
michael@0 | 98 | // Set the menu item in main menu. |
michael@0 | 99 | MenuItem menuItem = subMenu.getItem(); |
michael@0 | 100 | setValues(item, menuItem); |
michael@0 | 101 | |
michael@0 | 102 | // Start parsing the sub menu. |
michael@0 | 103 | parseMenu(parser, attrs, subMenu); |
michael@0 | 104 | } |
michael@0 | 105 | } |
michael@0 | 106 | break; |
michael@0 | 107 | |
michael@0 | 108 | case XmlPullParser.END_TAG: |
michael@0 | 109 | if (parser.getName().equals(TAG_ITEM)) { |
michael@0 | 110 | if (!item.hasSubMenu) { |
michael@0 | 111 | // Add the item. |
michael@0 | 112 | MenuItem menuItem = menu.add(NO_ID, item.id, item.order, item.title); |
michael@0 | 113 | setValues(item, menuItem); |
michael@0 | 114 | } |
michael@0 | 115 | } else if (tag.equals(TAG_MENU)) { |
michael@0 | 116 | return; |
michael@0 | 117 | } |
michael@0 | 118 | break; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | eventType = parser.next(); |
michael@0 | 122 | |
michael@0 | 123 | } while (eventType != XmlPullParser.END_DOCUMENT); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | public void parseItem(ParsedItem item, AttributeSet attrs) { |
michael@0 | 127 | TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.MenuItem); |
michael@0 | 128 | |
michael@0 | 129 | item.id = a.getResourceId(R.styleable.MenuItem_android_id, NO_ID); |
michael@0 | 130 | item.order = a.getInt(R.styleable.MenuItem_android_orderInCategory, 0); |
michael@0 | 131 | item.title = a.getText(R.styleable.MenuItem_android_title); |
michael@0 | 132 | item.iconRes = a.getResourceId(R.styleable.MenuItem_android_icon, 0); |
michael@0 | 133 | item.checkable = a.getBoolean(R.styleable.MenuItem_android_checkable, false); |
michael@0 | 134 | item.checked = a.getBoolean(R.styleable.MenuItem_android_checked, false); |
michael@0 | 135 | item.visible = a.getBoolean(R.styleable.MenuItem_android_visible, true); |
michael@0 | 136 | item.enabled = a.getBoolean(R.styleable.MenuItem_android_enabled, true); |
michael@0 | 137 | item.hasSubMenu = false; |
michael@0 | 138 | |
michael@0 | 139 | if (Build.VERSION.SDK_INT >= 11) |
michael@0 | 140 | item.showAsAction = a.getInt(R.styleable.MenuItem_android_showAsAction, 0); |
michael@0 | 141 | |
michael@0 | 142 | a.recycle(); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | public void setValues(ParsedItem item, MenuItem menuItem) { |
michael@0 | 146 | menuItem.setChecked(item.checked) |
michael@0 | 147 | .setVisible(item.visible) |
michael@0 | 148 | .setEnabled(item.enabled) |
michael@0 | 149 | .setCheckable(item.checkable) |
michael@0 | 150 | .setIcon(item.iconRes); |
michael@0 | 151 | |
michael@0 | 152 | if (Build.VERSION.SDK_INT >= 11) |
michael@0 | 153 | menuItem.setShowAsAction(item.showAsAction); |
michael@0 | 154 | } |
michael@0 | 155 | } |