List of usage examples for android.content.res XmlResourceParser getAttributeResourceValue
public int getAttributeResourceValue(int index, int defaultValue);
From source file:com.vsmartcard.acardemulator.emulators.EmulatorSingleton.java
public static String[] getRegisteredAids(Context context) { List<String> aidList = new ArrayList<>(); XmlResourceParser aidXmlParser = context.getResources().getXml(R.xml.aid_list); try {/*from w w w. j a v a2s .co m*/ while (aidXmlParser.getEventType() != XmlPullParser.END_DOCUMENT) { if (aidXmlParser.getEventType() == XmlPullParser.START_TAG) { if (aidXmlParser.getName().equals("aid-filter")) { int aid = aidXmlParser.getAttributeResourceValue(0, -1); if (aid != -1) { aidList.add(context.getResources().getString(aid)); } } } aidXmlParser.next(); } aidXmlParser.close(); } catch (XmlPullParserException | IOException e) { Log.e(TAG.substring(0, 23), "Couldn't parse aid xml list."); } return aidList.toArray(new String[0]); }
From source file:com.roughike.bottombar.TabParser.java
@NonNull private String getTitleValue(@NonNull XmlResourceParser parser, @IntRange(from = 0) int attrIndex) { int titleResource = parser.getAttributeResourceValue(attrIndex, 0); return titleResource == RESOURCE_NOT_FOUND ? parser.getAttributeValue(attrIndex) : context.getString(titleResource); }
From source file:com.roughike.bottombar.TabParser.java
@ColorInt private int getColorValue(@NonNull XmlResourceParser parser, @IntRange(from = 0) int attrIndex) { int colorResource = parser.getAttributeResourceValue(attrIndex, 0); if (colorResource == RESOURCE_NOT_FOUND) { try {/* w w w.j a v a 2 s .c om*/ String colorValue = parser.getAttributeValue(attrIndex); return Color.parseColor(colorValue); } catch (Exception ignored) { return COLOR_NOT_SET; } } return ContextCompat.getColor(context, colorResource); }
From source file:com.wit.and.dialog.internal.xml.XmlEditDialog.java
/** *//*from w w w .ja va2 s. co m*/ @Override protected void onParseAttribute(XmlResourceParser xmlParser, int attr, int index, EditDialog.EditOptions options) { if (attr == android.R.attr.editTextStyle) { options.editStyle(xmlParser.getAttributeResourceValue(index, 0)); } else if (attr == android.R.attr.hint) { options.hint(resolveString(xmlParser, index)); } else { super.onParseAttribute(xmlParser, attr, index, options); } }
From source file:com.wit.and.dialog.xml.XmlDialogParser.java
/** * Resolves situation, if at the current parsed position of Xml parser is string resource or * raw string value./* w w w. ja v a 2 s. c o m*/ * * @param parser Parser at position of resolving string. * @param currentIndex Index of resolving string in the parser. * @return Resolved string. */ protected final String resolveString(XmlResourceParser parser, int currentIndex) { String string; final int stringRes = parser.getAttributeResourceValue(currentIndex, -1); switch (stringRes) { case -1: string = parser.getAttributeValue(currentIndex); break; case 0: string = null; break; default: string = getResources().getString(stringRes); break; } return string; }
From source file:com.wit.and.dialog.xml.XmlDialogParser.java
/** * <p>//from www .j a v a 2 s . c om * Invoked to parse value of specific attribute from currently being parsed XML. * </p> * * @param xmlParser Xml resource parser at position of current attribute which value should be obtained. * @param attr Id of attribute which value is placed at the <var>index</var> position in the <var>xmlParser</var>. * @param index Index of value which should be obtained form the <var>xmlParser</var>. * @param options Dialog options to set up obtained value from parser. */ protected void onParseAttribute(XmlResourceParser xmlParser, int attr, int index, O options) { if (attr == android.R.attr.title) { options.title(resolveString(xmlParser, index)); } else if (attr == android.R.attr.icon) { options.icon(xmlParser.getAttributeResourceValue(index, 0)); } else if (attr == android.R.attr.text) { options.message(resolveString(xmlParser, index)); } else if (attr == R.attr.dialogButtonNegative) { options.negative(resolveString(xmlParser, index)); } else if (attr == R.attr.dialogButtonNeutral) { options.neutral(resolveString(xmlParser, index)); } else if (attr == R.attr.dialogButtonPositive) { options.positive(resolveString(xmlParser, index)); } else if (attr == R.attr.dialogTheme) { options.dialogTheme(xmlParser.getAttributeResourceValue(index, 0)); } else if (attr == R.attr.dialogCancelable) { options.cancelable(xmlParser.getAttributeBooleanValue(index, true)); } else if (attr == R.attr.dialogDismissOnRestore) { options.dismissOnRestore(xmlParser.getAttributeBooleanValue(index, false)); } }
From source file:com.roughike.bottombar.TabParser.java
@NonNull private BottomBarTab parseNewTab(@NonNull XmlResourceParser parser, @IntRange(from = 0) int containerPosition) { BottomBarTab workingTab = tabWithDefaults(); workingTab.setIndexInContainer(containerPosition); final int numberOfAttributes = parser.getAttributeCount(); for (int i = 0; i < numberOfAttributes; i++) { @TabAttribute// ww w . ja v a 2s . c om String attrName = parser.getAttributeName(i); switch (attrName) { case ID: workingTab.setId(parser.getIdAttributeResourceValue(i)); break; case ICON: workingTab.setIconResId(parser.getAttributeResourceValue(i, RESOURCE_NOT_FOUND)); break; case TITLE: workingTab.setTitle(getTitleValue(parser, i)); break; case INACTIVE_COLOR: int inactiveColor = getColorValue(parser, i); if (inactiveColor == COLOR_NOT_SET) continue; workingTab.setInActiveColor(inactiveColor); break; case ACTIVE_COLOR: int activeColor = getColorValue(parser, i); if (activeColor == COLOR_NOT_SET) continue; workingTab.setActiveColor(activeColor); break; case BAR_COLOR_WHEN_SELECTED: int barColorWhenSelected = getColorValue(parser, i); if (barColorWhenSelected == COLOR_NOT_SET) continue; workingTab.setBarColorWhenSelected(barColorWhenSelected); break; case BADGE_BACKGROUND_COLOR: int badgeBackgroundColor = getColorValue(parser, i); if (badgeBackgroundColor == COLOR_NOT_SET) continue; workingTab.setBadgeBackgroundColor(badgeBackgroundColor); break; case BADGE_HIDES_WHEN_ACTIVE: boolean badgeHidesWhenActive = parser.getAttributeBooleanValue(i, true); workingTab.setBadgeHidesWhenActive(badgeHidesWhenActive); break; case IS_TITLELESS: boolean isTitleless = parser.getAttributeBooleanValue(i, false); workingTab.setIsTitleless(isTitleless); break; } } return workingTab; }
From source file:com.actionbarsherlock.internal.widget.ActionBarView.java
/** * Attempt to programmatically load the logo from the manifest file of an * activity by using an XML pull parser. This should allow us to read the * logo attribute regardless of the platform it is being run on. * * @param activity Activity instance./* ww w .ja v a2 s. c o m*/ * @return Logo resource ID. */ private static int loadLogoFromManifest(Activity activity) { int logo = 0; try { final String thisPackage = activity.getClass().getName(); if (DEBUG) Log.i(TAG, "Parsing AndroidManifest.xml for " + thisPackage); final String packageName = activity.getApplicationInfo().packageName; final AssetManager am = activity.createPackageContext(packageName, 0).getAssets(); final XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml"); int eventType = xml.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { String name = xml.getName(); if ("application".equals(name)) { //Check if the <application> has the attribute if (DEBUG) Log.d(TAG, "Got <application>"); for (int i = xml.getAttributeCount() - 1; i >= 0; i--) { if (DEBUG) Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i)); if ("logo".equals(xml.getAttributeName(i))) { logo = xml.getAttributeResourceValue(i, 0); break; //out of for loop } } } else if ("activity".equals(name)) { //Check if the <activity> is us and has the attribute if (DEBUG) Log.d(TAG, "Got <activity>"); Integer activityLogo = null; String activityPackage = null; boolean isOurActivity = false; for (int i = xml.getAttributeCount() - 1; i >= 0; i--) { if (DEBUG) Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i)); //We need both uiOptions and name attributes String attrName = xml.getAttributeName(i); if ("logo".equals(attrName)) { activityLogo = xml.getAttributeResourceValue(i, 0); } else if ("name".equals(attrName)) { activityPackage = ActionBarSherlockCompat.cleanActivityName(packageName, xml.getAttributeValue(i)); if (!thisPackage.equals(activityPackage)) { break; //on to the next } isOurActivity = true; } //Make sure we have both attributes before processing if ((activityLogo != null) && (activityPackage != null)) { //Our activity, logo specified, override with our value logo = activityLogo.intValue(); } } if (isOurActivity) { //If we matched our activity but it had no logo don't //do any more processing of the manifest break; } } } eventType = xml.nextToken(); } } catch (Exception e) { e.printStackTrace(); } if (DEBUG) Log.i(TAG, "Returning " + Integer.toHexString(logo)); return logo; }
From source file:com.wit.and.dialog.internal.xml.XmlDialogInflater.java
/** * <p>// w w w .ja v a 2s .co m * Inflates dialog fragment instance form the given XML dialogs set resource using one * of the registered XML dialog parsers. * </p> * * @param dialogID Id of dialog in the given <var>xmlSetRes</var> to inflate. * @param xmlSetRes Resource of XML file placed in the application resources. All XML dialogs in this XML file * must be placed inside <b><Dialogs></Dialogs></b> root tag. * @return New instance of {@link DialogFragment} or its derived classes, depends on the XML dialog * root tag. * @throws XmlDialogInflater.UnsupportedXmlDialogTagException * @throws java.security.InvalidParameterException * @throws java.lang.IllegalStateException * @see #registerParser(Class, String) */ public DialogFragment inflateDialog(int dialogID, int xmlSetRes) { XmlResourceParser xmlParser = mResources.getXml(xmlSetRes); if (xmlParser == null) return null; DialogFragment dialog = null; long time; if (DEBUG) { time = System.currentTimeMillis(); } try { int xmlEvent; boolean dialogsTagResolved = false; while ((xmlEvent = xmlParser.getEventType()) != XmlResourceParser.END_DOCUMENT) { switch (xmlEvent) { case XmlResourceParser.START_DOCUMENT: break; case XmlResourceParser.START_TAG: String tag = xmlParser.getName(); if (!dialogsTagResolved) { // Check valid root tag. if (!tag.equals(DIALOGS_SET_ROOT_TAG)) { throw new UnsupportedXmlDialogTagException( "Only 'Dialogs' tag is allowed as root tag of XML dialogs set."); } dialogsTagResolved = true; } else { // Check for empty dialog. if (xmlParser.getAttributeCount() == 0) { throw new IllegalStateException("Empty dialogs are not allowed."); } // Find the dialog with requested id. // Note, that first attribute of each dialog must be its "id" to preserve // fast parsing of xml file. final int attr = xmlParser.getAttributeNameResource(0); if (attr != android.R.attr.id) { throw new InvalidParameterException( "First attribute of XML dialog must be always 'android:id'."); } // Finally check the dialog id. final int id = xmlParser.getAttributeResourceValue(0, 0); if (dialogID == id) { dialog = parseDialogInner(xmlParser); } } break; } if (dialog != null) { break; } xmlParser.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (DEBUG) { Log.d(TAG, "Parsing of XML dialog from dialogs set in " + Long.toString(System.currentTimeMillis() - time) + "ms"); } return dialog; }