Example usage for android.content.res XmlResourceParser getAttributeCount

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

Introduction

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

Prototype

public int getAttributeCount();

Source Link

Document

Returns the number of attributes available in the set.

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   www .  java 2 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:org.goseumdochi.android.leash.EfficientAnimation.java

private static void loadFromXml(final int resourceId, final Context context,
        final OnDrawableLoadedListener onDrawableLoadedListener) {
    new Thread(new Runnable() {
        @Override//  ww  w .  jav a  2  s .  c o m
        public void run() {
            final ArrayList<EaFrame> frames = new ArrayList<>();

            XmlResourceParser parser = context.getResources().getXml(resourceId);

            try {
                int eventType = parser.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_DOCUMENT) {

                    } else if (eventType == XmlPullParser.START_TAG) {

                        if (parser.getName().equals("item")) {
                            byte[] bytes = null;

                            for (int i = 0; i < parser.getAttributeCount(); i++) {
                                String attrName = parser.getAttributeName(i);
                                if (attrName.endsWith("drawable")) {
                                    int resId = Integer.parseInt(parser.getAttributeValue(i).substring(1));
                                    bytes = IOUtils.toByteArray(context.getResources().openRawResource(resId));
                                }
                            }

                            EaFrame frame = new EaFrame();
                            frame.bytes = bytes;
                            frames.add(frame);
                        }

                    } else if (eventType == XmlPullParser.END_TAG) {

                    } else if (eventType == XmlPullParser.TEXT) {

                    }

                    eventType = parser.next();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            // Run on UI Thread
            new Handler(context.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    if (onDrawableLoadedListener != null) {
                        onDrawableLoadedListener.onDrawableLoaded(frames);
                    }
                }
            });
        }
    }).run();
}

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/*from  ww  w.j  a v  a  2  s.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.wit.and.dialog.xml.XmlDialogParser.java

/**
 * <p>/*w  w  w .  j  ava2  s  .c om*/
 * </p>
 *
 * @param parser
 * @return
 */
public DialogFragment parseXmlDialog(XmlResourceParser parser) {
    O options = onCreateEmptyOptions();

    if (options != null) {
        // Set up default icon.
        options.icon(getDefaultIcon());

        final int attrCount = parser.getAttributeCount();
        // Parse global attributes.
        for (int i = 0; i < attrCount; i++) {
            onParseAttribute(parser, parser.getAttributeNameResource(i), i, options);
        }
    }

    // Create dialog.
    return onCreateDialog(options);
}

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

private static int loadUiOptionsFromManifest(Activity activity) {
    int uiOptions = 0;
    try {/* w ww .  j  a  v  a  2s .  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;
}

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.//from   w w  w  .  ja  v a2 s .co  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  ww  .  jav  a 2 s  .  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>&lt;Dialogs&gt&lt;/Dialogs&gt;</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;
}