List of usage examples for android.util Xml asAttributeSet
public static AttributeSet asAttributeSet(XmlPullParser parser)
From source file:com.wnafee.vector.compat.AnimatedVectorDrawable.java
public static AnimatedVectorDrawable create(Context c, Resources resources, int rid) { try {/*ww w .j a v a 2 s . c om*/ final XmlPullParser parser = resources.getXml(rid); final AttributeSet attrs = Xml.asAttributeSet(parser); int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty loop } if (type != XmlPullParser.START_TAG) { throw new XmlPullParserException("No start tag found"); } else if (!ANIMATED_VECTOR.equals(parser.getName())) { throw new IllegalArgumentException("root node must start with: " + ANIMATED_VECTOR); } final AnimatedVectorDrawable drawable = new AnimatedVectorDrawable(); drawable.inflate(c, resources, parser, attrs, null); return drawable; } catch (XmlPullParserException e) { Log.e(LOGTAG, "parser error", e); } catch (IOException e) { Log.e(LOGTAG, "parser error", e); } return null; }
From source file:com.actionbarsherlock.internal.view.menu.MenuInflaterImpl.java
/** * Inflate a menu hierarchy from the specified XML resource. Throws * {@link InflateException} if there is an error. * * @param menuRes Resource ID for an XML layout resource to load (e.g., * <code>R.menu.main_activity</code>) * @param menu The Menu to inflate into. The items and submenus will be * added to this Menu./* ww w . jav a 2 s .co m*/ */ public void inflate(int menuRes, android.view.Menu menu) { if (!(menu instanceof MenuBuilder)) { mNativeMenuInflater.inflate(menuRes, menu); return; } MenuBuilder actionBarMenu = (MenuBuilder) menu; XmlResourceParser parser = null; try { parser = mContext.getResources().getLayout(menuRes); AttributeSet attrs = Xml.asAttributeSet(parser); parseMenu(parser, attrs, actionBarMenu); } 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(); } }
From source file:android.content.res.VectorResources.java
private Drawable createDrawableFromXml(XmlResourceParser parser) throws XmlPullParserException, IOException { AttributeSet attrs = Xml.asAttributeSet(parser); int type;// ww w .j ava2 s .co m while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty loop } if (type != XmlPullParser.START_TAG) { throw new XmlPullParserException("No start tag found"); } Drawable drawable = createDrawableFromXmlInner(parser, attrs); if (drawable == null) { throw new RuntimeException("Unknown initial tag: " + parser.getName()); } return drawable; }
From source file:com.androzic.Preferences.java
/** * Parse the given XML file as a header description, adding each * parsed Header into the target list.//from ww w.ja v a 2 s. c om * * @param resid * The XML resource to load and parse. * @param target * The list in which the parsed headers should be placed. */ public void loadHeadersFromResource(int resid, List<Header> target) { Androzic application = Androzic.getApplication(); XmlResourceParser parser = null; try { Resources resources = getResources(); parser = resources.getXml(resid); AttributeSet attrs = Xml.asAttributeSet(parser); int type; //noinspection StatementWithEmptyBody while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) { // Parse next until start tag is found } String nodeName = parser.getName(); if (!"preference-headers".equals(nodeName)) { throw new RuntimeException("XML document must start with <preference-headers> tag; found" + nodeName + " at " + parser.getPositionDescription()); } Bundle curBundle = null; final int outerDepth = parser.getDepth(); while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } nodeName = parser.getName(); if ("header".equals(nodeName)) { Header header = new Header(); TypedArray sa = resources.obtainAttributes(attrs, R.styleable.PreferenceHeader); header.id = sa.getResourceId(R.styleable.PreferenceHeader_id, (int) HEADER_ID_UNDEFINED); TypedValue tv = sa.peekValue(R.styleable.PreferenceHeader_title); if (tv != null && tv.type == TypedValue.TYPE_STRING) { if (tv.resourceId != 0) { header.titleRes = tv.resourceId; } else { header.title = tv.string; } } tv = sa.peekValue(R.styleable.PreferenceHeader_summary); if (tv != null && tv.type == TypedValue.TYPE_STRING) { if (tv.resourceId != 0) { header.summaryRes = tv.resourceId; } else { header.summary = tv.string; } } tv = sa.peekValue(R.styleable.PreferenceHeader_breadCrumbTitle); if (tv != null && tv.type == TypedValue.TYPE_STRING) { if (tv.resourceId != 0) { header.breadCrumbTitleRes = tv.resourceId; } else { header.breadCrumbTitle = tv.string; } } tv = sa.peekValue(R.styleable.PreferenceHeader_breadCrumbShortTitle); if (tv != null && tv.type == TypedValue.TYPE_STRING) { if (tv.resourceId != 0) { header.breadCrumbShortTitleRes = tv.resourceId; } else { header.breadCrumbShortTitle = tv.string; } } header.iconRes = sa.getResourceId(R.styleable.PreferenceHeader_icon, 0); header.fragment = sa.getString(R.styleable.PreferenceHeader_fragment); header.help = sa.getResourceId(R.styleable.PreferenceHeader_help, 0); sa.recycle(); if (curBundle == null) { curBundle = new Bundle(); } final int innerDepth = parser.getDepth(); while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) { if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } String innerNodeName = parser.getName(); if (innerNodeName.equals("extra")) { resources.parseBundleExtra(innerNodeName, attrs, curBundle); XmlUtils.skipCurrentTag(parser); } else if (innerNodeName.equals("intent")) { header.intent = Intent.parseIntent(resources, parser, attrs); } else { XmlUtils.skipCurrentTag(parser); } } if (curBundle.size() > 0) { header.fragmentArguments = curBundle; curBundle = null; } if (header.id == R.id.pref_plugins && application.getPluginsPreferences().size() == 0) continue; target.add(header); } else { XmlUtils.skipCurrentTag(parser); } } } catch (XmlPullParserException | IOException e) { throw new RuntimeException("Error parsing headers", e); } finally { if (parser != null) parser.close(); } }
From source file:im.ene.ribbon.MenuParser.java
@SuppressWarnings("checkstyle:cyclomaticcomplexity") protected static Menu inflateMenu(final Context context, @MenuRes int menuRes) { List<ActionTab> tabs = new ArrayList<>(); MenuParser menuParser = new MenuParser(); try {//from ww w. j a v a 2 s. co m final XmlResourceParser parser = context.getResources().getLayout(menuRes); AttributeSet attrs = Xml.asAttributeSet(parser); String tagName; int eventType = parser.getEventType(); boolean lookingForEndOfUnknownTag = false; String unknownTagName = null; do { if (eventType == XmlPullParser.START_TAG) { tagName = parser.getName(); if (tagName.equals("menu")) { menuParser.readMenu(context, attrs); eventType = parser.next(); break; } throw new RuntimeException("Expecting menu, got " + tagName); } eventType = parser.next(); } while (eventType != XmlPullParser.END_DOCUMENT); boolean reachedEndOfMenu = false; while (!reachedEndOfMenu) { switch (eventType) { case XmlPullParser.START_TAG: if (lookingForEndOfUnknownTag) { break; } tagName = parser.getName(); if (tagName.equals("item")) { menuParser.readItem(context, attrs); } else { lookingForEndOfUnknownTag = true; unknownTagName = tagName; } break; case XmlPullParser.END_TAG: tagName = parser.getName(); if (lookingForEndOfUnknownTag && tagName.equals(unknownTagName)) { lookingForEndOfUnknownTag = false; unknownTagName = null; } else if (tagName.equals("item")) { if (menuParser.hasItem()) { MenuItem item = menuParser.pullItem(); ActionTab tab = new ActionTab(item.getItemId(), item.getItemIcon(), String.valueOf(item.getItemTitle())); tab.setEnabled(item.isItemEnabled()); tab.setColor(item.getItemColor()); tabs.add(tab); } } else if (tagName.equals("menu")) { reachedEndOfMenu = true; } break; case XmlPullParser.END_DOCUMENT: throw new RuntimeException("Unexpected end of document"); default: break; } eventType = parser.next(); } } catch (Exception e) { return null; } if (menuParser.hasMenu()) { Menu menu = menuParser.pullMenu(); menu.setItems(tabs.toArray(new ActionTab[tabs.size()])); return menu; } return null; }
From source file:com.wnafee.vector.compat.VectorDrawable.java
public static VectorDrawable create(Resources resources, int rid) { try {/* ww w . ja v a2 s . c o m*/ final XmlPullParser parser = resources.getXml(rid); final AttributeSet attrs = Xml.asAttributeSet(parser); int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty loop } if (type != XmlPullParser.START_TAG) { throw new XmlPullParserException("No start tag found"); } else if (!SHAPE_VECTOR.equals(parser.getName())) { throw new IllegalArgumentException("root node must start with: " + SHAPE_VECTOR); } final VectorDrawable drawable = new VectorDrawable(); drawable.inflate(resources, parser, attrs, null); return drawable; } catch (XmlPullParserException e) { Log.e(LOGTAG, "parser error", e); } catch (IOException e) { Log.e(LOGTAG, "parser error", e); } return null; }
From source file:com.hippo.vector.AnimatedVectorDrawable.java
public static AnimatedVectorDrawable create(Context context, int rid) { try {/* w ww . ja va 2 s. co m*/ final XmlPullParser parser = context.getResources().getXml(rid); final AttributeSet attrs = Xml.asAttributeSet(parser); int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty loop } if (type != XmlPullParser.START_TAG) { throw new XmlPullParserException("No start tag found"); } final String name = parser.getName(); if (!ANIMATED_VECTOR.equals(name)) { throw new IllegalStateException("It is not animated-vector"); } final AnimatedVectorDrawable drawable = new AnimatedVectorDrawable(); drawable.inflate(context, parser, attrs); return drawable; } catch (XmlPullParserException e) { Log.e(LOGTAG, "parser error", e); } catch (IOException e) { Log.e(LOGTAG, "parser error", e); } return null; }
From source file:com.justplay1.shoppist.features.search.widget.FloatingSearchView.java
public void inflateMenu(@MenuRes int menuRes) { if (menuRes == 0) return;//w w w.j a v a2 s . c o m getActivity().getMenuInflater().inflate(menuRes, actionMenu.getMenu()); XmlResourceParser parser = null; try { //noinspection ResourceType parser = getResources().getLayout(menuRes); AttributeSet attrs = Xml.asAttributeSet(parser); parseMenu(parser, attrs); } catch (XmlPullParserException | IOException e) { // should not happens throw new InflateException("Error parsing menu XML", e); } finally { if (parser != null) parser.close(); } }
From source file:android.support.v7ox.widget.AppCompatDrawableManager.java
private Drawable loadDrawableFromDelegates(@NonNull Context context, @DrawableRes int resId) { if (mDelegates != null && !mDelegates.isEmpty()) { if (mKnownDrawableIdTags != null) { final String cachedTagName = mKnownDrawableIdTags.get(resId); if (SKIP_DRAWABLE_TAG.equals(cachedTagName) || (cachedTagName != null && mDelegates.get(cachedTagName) == null)) { // If we don't have a delegate for the drawable tag, or we've been set to // skip it, fail fast and return null if (DEBUG) { Log.d(TAG, "[loadDrawableFromDelegates] Skipping drawable: " + context.getResources().getResourceName(resId)); }/* ww w . ja v a2 s. c om*/ return null; } } else { // Create an id cache as we'll need one later mKnownDrawableIdTags = new SparseArray<>(); } if (mTypedValue == null) { mTypedValue = new TypedValue(); } final TypedValue tv = mTypedValue; final Resources res = context.getResources(); res.getValue(resId, tv, true); final long key = (((long) tv.assetCookie) << 32) | tv.data; Drawable dr = getCachedDelegateDrawable(context, key); if (dr != null) { if (DEBUG) { Log.i(TAG, "[loadDrawableFromDelegates] Returning cached drawable: " + context.getResources().getResourceName(resId)); } // We have a cached drawable, return it! return dr; } if (tv.string != null && tv.string.toString().endsWith(".xml")) { // If the resource is an XML file, let's try and parse it try { final XmlPullParser parser = res.getXml(resId); final AttributeSet attrs = Xml.asAttributeSet(parser); int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty loop } if (type != XmlPullParser.START_TAG) { throw new XmlPullParserException("No start tag found"); } final String tagName = parser.getName(); // Add the tag name to the cache mKnownDrawableIdTags.append(resId, tagName); // Now try and find a delegate for the tag name and inflate if found final InflateDelegate delegate = mDelegates.get(tagName); if (delegate != null) { dr = delegate.createFromXmlInner(context, parser, attrs, context.getTheme()); } if (dr != null) { // Add it to the drawable cache dr.setChangingConfigurations(tv.changingConfigurations); if (addCachedDelegateDrawable(context, key, dr) && DEBUG) { Log.i(TAG, "[loadDrawableFromDelegates] Saved drawable to cache: " + context.getResources().getResourceName(resId)); } } } catch (Exception e) { Log.e(TAG, "Exception while inflating drawable", e); } } if (dr == null) { // If we reach here then the delegate inflation of the resource failed. Mark it as // bad so we skip the id next time mKnownDrawableIdTags.append(resId, SKIP_DRAWABLE_TAG); } return dr; } return null; }
From source file:com.hippo.vectorold.drawable.VectorDrawable.java
/** @hide */ public static VectorDrawable create(Resources resources, int rid) { try {/* w ww.j a v a2 s . com*/ final XmlPullParser parser = resources.getXml(rid); final AttributeSet attrs = Xml.asAttributeSet(parser); int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty loop } if (type != XmlPullParser.START_TAG) { throw new XmlPullParserException("No start tag found"); } final VectorDrawable drawable = new VectorDrawable(); drawable.inflate(resources, parser, attrs); return drawable; } catch (XmlPullParserException e) { Log.e(LOGTAG, "parser error", e); } catch (IOException e) { Log.e(LOGTAG, "parser error", e); } return null; }