Example usage for android.content.res XmlResourceParser getAttributeIntValue

List of usage examples for android.content.res XmlResourceParser getAttributeIntValue

Introduction

In this page you can find the example usage for android.content.res XmlResourceParser getAttributeIntValue.

Prototype

public int getAttributeIntValue(int index, int defaultValue);

Source Link

Document

Return the integer value of attribute at 'index'.

Usage

From source file:Main.java

/**
 * Parses AndroidManifest.xml of the given apkFile and returns the value of
 * minSdkVersion using undocumented API which is marked as
 * "not to be used by applications"/*from ww w.j  av  a2  s .  c o  m*/
 * 
 * @return minSdkVersion or -1 if not found in AndroidManifest.xml
 */
public static int getMinSdkVersion(File apkFile) {
    if (apkFile == null) {
        return -1;
    }

    try {
        XmlResourceParser parser = getParserForManifest(apkFile);
        if (parser == null) {
            return -1;
        }
        while (parser.next() != XmlPullParser.END_DOCUMENT) {
            if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("uses-sdk")) {
                for (int i = 0; i < parser.getAttributeCount(); ++i) {
                    if (parser.getAttributeName(i).equals("minSdkVersion")) {
                        return parser.getAttributeIntValue(i, -1);
                    }
                }
            }
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return -1;
}

From source file:com.wit.and.dialog.internal.xml.XmlProgressDialog.java

/**
  *//*from ww  w . ja v a2 s  . co  m*/
@Override
protected void onParseAttribute(XmlResourceParser xmlParser, int attr, int index,
        ProgressDialog.ProgressOptions options) {
    if (attr == R.attr.dialogProgressType) {
        final int typeOrdinal = xmlParser.getAttributeIntValue(index,
                ProgressDialog.ProgressType.CIRCLE.ordinal());
        options.progressType(ProgressDialog.ProgressType.values()[typeOrdinal]);
    } else if (attr == R.attr.dialogProgressIndicator) {
        final int indicatorOrdinal = xmlParser.getAttributeIntValue(index,
                ProgressDialog.ProgressIndicator.NONE.ordinal());
        options.progressIndicator(ProgressDialog.ProgressIndicator.values()[indicatorOrdinal]);
    } else if (attr == R.attr.dialogProgressFormat) {
        options.progressFormat(resolveString(xmlParser, index));
    } else if (attr == R.attr.dialogTimeFormat) {
        options.timeFormat(resolveString(xmlParser, index));
    } else {
        super.onParseAttribute(xmlParser, attr, index, options);
    }
}

From source file:com.actionbarsherlock.internal.ActionBarSherlockCompat.java

private static int loadUiOptionsFromManifest(Activity activity) {
    int uiOptions = 0;
    try {//from   w w  w. ja va 2 s  .c o  m
        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 ("uiOptions".equals(xml.getAttributeName(i))) {
                            uiOptions = xml.getAttributeIntValue(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 activityUiOptions = 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 ("uiOptions".equals(attrName)) {
                            activityUiOptions = xml.getAttributeIntValue(i, 0);
                        } else if ("name".equals(attrName)) {
                            activityPackage = cleanActivityName(packageName, xml.getAttributeValue(i));
                            if (!thisPackage.equals(activityPackage)) {
                                break; //out of for loop
                            }
                            isOurActivity = true;
                        }

                        //Make sure we have both attributes before processing
                        if ((activityUiOptions != null) && (activityPackage != null)) {
                            //Our activity, uiOptions specified, override with our value
                            uiOptions = activityUiOptions.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(uiOptions));
    return uiOptions;
}