| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.frameworkperf; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
|
| 21 |
import org.xmlpull.v1.XmlPullParser; |
| 22 |
import org.xmlpull.v1.XmlPullParserException; |
| 23 |
|
| 24 |
import android.content.Context; |
| 25 |
import android.content.res.TypedArray; |
| 26 |
import android.content.res.XmlResourceParser; |
| 27 |
import android.util.AttributeSet; |
| 28 |
import android.util.Xml; |
| 29 |
import android.view.InflateException; |
| 30 |
|
| 31 |
public class SimpleInflater { |
| 32 |
|
| 33 |
private static final String XML_MENU = "menu"; |
| 34 |
|
| 35 |
|
| 36 |
private static final String XML_GROUP = "group"; |
| 37 |
|
| 38 |
|
| 39 |
private static final String XML_ITEM = "item"; |
| 40 |
|
| 41 |
private Context mContext; |
| 42 |
|
| 43 |
public SimpleInflater(Context context) { |
| 44 |
mContext = context; |
| 45 |
} |
| 46 |
|
| 47 |
public void inflate(int menuRes) { |
| 48 |
XmlResourceParser parser = null; |
| 49 |
try { |
| 50 |
parser = mContext.getResources().getLayout(menuRes); |
| 51 |
AttributeSet attrs = Xml.asAttributeSet(parser); |
| 52 |
|
| 53 |
parseMenu(parser, attrs); |
| 54 |
} catch (XmlPullParserException e) { |
| 55 |
throw new InflateException("Error inflating menu XML", e); |
| 56 |
} catch (IOException e) { |
| 57 |
throw new InflateException("Error inflating menu XML", e); |
| 58 |
} finally { |
| 59 |
if (parser != null) parser.close(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
private void parseMenu(XmlPullParser parser, AttributeSet attrs) |
| 64 |
throws XmlPullParserException, IOException { |
| 65 |
int eventType = parser.getEventType(); |
| 66 |
String tagName; |
| 67 |
boolean lookingForEndOfUnknownTag = false; |
| 68 |
String unknownTagName = null; |
| 69 |
|
| 70 |
|
| 71 |
do { |
| 72 |
if (eventType == XmlPullParser.START_TAG) { |
| 73 |
tagName = parser.getName(); |
| 74 |
if (tagName.equals(XML_MENU)) { |
| 75 |
|
| 76 |
eventType = parser.next(); |
| 77 |
break; |
| 78 |
} |
| 79 |
|
| 80 |
throw new RuntimeException("Expecting menu, got " + tagName); |
| 81 |
} |
| 82 |
eventType = parser.next(); |
| 83 |
} while (eventType != XmlPullParser.END_DOCUMENT); |
| 84 |
|
| 85 |
boolean reachedEndOfMenu = false; |
| 86 |
while (!reachedEndOfMenu) { |
| 87 |
switch (eventType) { |
| 88 |
case XmlPullParser.START_TAG: |
| 89 |
if (lookingForEndOfUnknownTag) { |
| 90 |
break; |
| 91 |
} |
| 92 |
|
| 93 |
tagName = parser.getName(); |
| 94 |
if (tagName.equals(XML_ITEM)) { |
| 95 |
readItem(attrs); |
| 96 |
} else if (tagName.equals(XML_MENU)) { |
| 97 |
parseMenu(parser, attrs); |
| 98 |
} else { |
| 99 |
lookingForEndOfUnknownTag = true; |
| 100 |
unknownTagName = tagName; |
| 101 |
} |
| 102 |
break; |
| 103 |
|
| 104 |
case XmlPullParser.END_TAG: |
| 105 |
tagName = parser.getName(); |
| 106 |
if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) { |
| 107 |
lookingForEndOfUnknownTag = false; |
| 108 |
unknownTagName = null; |
| 109 |
} else if (tagName.equals(XML_ITEM)) { |
| 110 |
} else if (tagName.equals(XML_MENU)) { |
| 111 |
reachedEndOfMenu = true; |
| 112 |
} |
| 113 |
break; |
| 114 |
|
| 115 |
case XmlPullParser.END_DOCUMENT: |
| 116 |
throw new RuntimeException("Unexpected end of document"); |
| 117 |
} |
| 118 |
|
| 119 |
eventType = parser.next(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public void readItem(AttributeSet attrs) { |
| 124 |
TypedArray a = mContext.obtainStyledAttributes(attrs, |
| 125 |
com.android.internal.R.styleable.MenuItem); |
| 126 |
|
| 127 |
|
| 128 |
int itemId = a.getResourceId(R.styleable.MenuItem_android_id, 0); |
| 129 |
final int category = a.getInt(R.styleable.MenuItem_android_menuCategory, 0); |
| 130 |
final int order = a.getInt(R.styleable.MenuItem_android_orderInCategory, 0); |
| 131 |
CharSequence itemTitle = a.getText(R.styleable.MenuItem_android_title); |
| 132 |
CharSequence itemTitleCondensed = a.getText(R.styleable.MenuItem_android_titleCondensed); |
| 133 |
int itemIconResId = a.getResourceId(R.styleable.MenuItem_android_icon, 0); |
| 134 |
String itemAlphabeticShortcut = a.getString(R.styleable.MenuItem_android_alphabeticShortcut); |
| 135 |
String itemNumericShortcut = a.getString(R.styleable.MenuItem_android_numericShortcut); |
| 136 |
int itemCheckable = 0; |
| 137 |
if (a.hasValue(R.styleable.MenuItem_android_checkable)) { |
| 138 |
|
| 139 |
itemCheckable = a.getBoolean(R.styleable.MenuItem_android_checkable, false) ? 1 : 0; |
| 140 |
} |
| 141 |
boolean itemChecked = a.getBoolean(R.styleable.MenuItem_android_checked, false); |
| 142 |
boolean itemVisible = a.getBoolean(R.styleable.MenuItem_android_visible, false); |
| 143 |
boolean itemEnabled = a.getBoolean(R.styleable.MenuItem_android_enabled, false); |
| 144 |
|
| 145 |
a.recycle(); |
| 146 |
} |
| 147 |
} |