List of usage examples for android.content Intent parseIntent
public static @NonNull Intent parseIntent(@NonNull Resources resources, @NonNull XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException
From source file:com.androzic.Preferences.java
/** * Parse the given XML file as a header description, adding each * parsed Header into the target list.// w w w. java 2 s . co m * * @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:com.achep.base.ui.activities.SettingsActivity.java
/** * Parse the given XML file as a categories description, adding each * parsed categories and tiles into the target list. * * @param resourceId The XML resource to load and parse. * @param target The list in which the parsed categories and tiles should be placed. */// ww w .j a va 2 s . c o m protected final void loadDashboardFromResource(@XmlRes int resourceId, @NonNull List<DashboardCategory> target) { XmlResourceParser parser = null; try { parser = getResources().getXml(resourceId); AttributeSet attrs = Xml.asAttributeSet(parser); int type; for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) { if (type == XmlPullParser.START_TAG) break; } String nodeName = parser.getName(); if (!RESOURCE_TAG_DASHBOARD.equals(nodeName)) throw new RuntimeException(String.format("XML document must start with <%s> tag; found %s at %s", RESOURCE_TAG_DASHBOARD, nodeName, parser.getPositionDescription())); for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) { if (type == XmlPullParser.END_TAG && parser.getDepth() <= 1 /* root tag */) break; if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } switch (parser.getName()) { case RESOURCE_TAG_DASHBOARD_CATEGORY: DashboardCategory category = new DashboardCategory(this, attrs); final int categoryDepth = parser.getDepth(); for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) { if (type == XmlPullParser.END_TAG && parser.getDepth() <= categoryDepth) break; if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } switch (parser.getName()) { case RESOURCE_TAG_DASHBOARD_TILE: DashboardTile tile = new DashboardTile(this, attrs); Bundle bundle = null; final int tileDepth = parser.getDepth(); for (type = parser.next(); type != XmlPullParser.END_DOCUMENT; type = parser.next()) { if (type == XmlPullParser.END_TAG && parser.getDepth() <= tileDepth) break; if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } switch (parser.getName()) { case RESOURCE_TAG_DASHBOARD_TILE_EXTRA: if (bundle == null) { bundle = new Bundle(); } getResources().parseBundleExtra(RESOURCE_TAG_DASHBOARD_TILE_EXTRA, attrs, bundle); XmlUtils.skipCurrentTag(parser); break; case RESOURCE_TAG_DASHBOARD_TILE_INTENT: tile.intent = Intent.parseIntent(getResources(), parser, attrs); break; default: XmlUtils.skipCurrentTag(parser); } } tile.fragmentArguments = bundle; category.add(tile); break; default: XmlUtils.skipCurrentTag(parser); } } target.add(category); break; default: XmlUtils.skipCurrentTag(parser); } } } catch (XmlPullParserException | IOException e) { throw new RuntimeException("Error parsing categories", e); } finally { if (parser != null) parser.close(); } }