Example usage for android.content.res XmlResourceParser getAttributeName

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

Introduction

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

Prototype

public String getAttributeName(int index);

Source Link

Document

Returns the name of the specified attribute.

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.j  a v  a2  s. co 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//from   w w w. j a va 2s .c  om
        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 o  m*/
        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.android.mms.MmsConfig.java

private static void loadMmsSettings(Context context) {
    XmlResourceParser parser = context.getResources().getXml(R.xml.mms_config);

    try {//from   w  ww . ja va2s .c o  m
        beginDocument(parser, "mms_config");

        while (true) {
            nextElement(parser);
            String tag = parser.getName();
            if (tag == null) {
                break;
            }
            String name = parser.getAttributeName(0);
            String value = parser.getAttributeValue(0);
            String text = null;
            if (parser.next() == XmlPullParser.TEXT) {
                text = parser.getText();
            }

            if (DEBUG) {
                Log.v(TAG, "tag: " + tag + " value: " + value + " - " + text);
            }
            if ("name".equalsIgnoreCase(name)) {
                if ("bool".equals(tag)) {
                    // bool config tags go here
                    if ("enabledMMS".equalsIgnoreCase(value)) {
                        mMmsEnabled = "true".equalsIgnoreCase(text) ? 1 : 0;
                    } else if ("enabledTransID".equalsIgnoreCase(value)) {
                        mTransIdEnabled = "true".equalsIgnoreCase(text);
                    } else if ("enabledNotifyWapMMSC".equalsIgnoreCase(value)) {
                        mNotifyWapMMSC = "true".equalsIgnoreCase(text);
                    } else if ("aliasEnabled".equalsIgnoreCase(value)) {
                        mAliasEnabled = "true".equalsIgnoreCase(text);
                    } else if ("allowAttachAudio".equalsIgnoreCase(value)) {
                        mAllowAttachAudio = "true".equalsIgnoreCase(text);
                    } else if ("enableMultipartSMS".equalsIgnoreCase(value)) {
                        mEnableMultipartSMS = "true".equalsIgnoreCase(text);
                    } else if ("enableSlideDuration".equalsIgnoreCase(value)) {
                        mEnableSlideDuration = "true".equalsIgnoreCase(text);
                    } else if ("enableMMSReadReports".equalsIgnoreCase(value)) {
                        mEnableMMSReadReports = "true".equalsIgnoreCase(text);
                    } else if ("enableSMSDeliveryReports".equalsIgnoreCase(value)) {
                        mEnableSMSDeliveryReports = "true".equalsIgnoreCase(text);
                    } else if ("enableMMSDeliveryReports".equalsIgnoreCase(value)) {
                        mEnableMMSDeliveryReports = "true".equalsIgnoreCase(text);
                        /// M: google jb.mr1 patch, group mms
                    } else if ("enableGroupMms".equalsIgnoreCase(value)) {
                        mEnableGroupMms = "true".equalsIgnoreCase(text);
                    }
                } else if ("int".equals(tag)) {
                    // int config tags go here
                    if ("maxMessageSize".equalsIgnoreCase(value)) {
                        mMaxMessageSize = Integer.parseInt(text);
                    } else if ("maxImageHeight".equalsIgnoreCase(value)) {
                        mMaxImageHeight = Integer.parseInt(text);
                    } else if ("maxImageWidth".equalsIgnoreCase(value)) {
                        mMaxImageWidth = Integer.parseInt(text);
                    }
                    /// M: @{
                    else if ("maxRestrictedImageHeight".equalsIgnoreCase(value)) {
                        mMaxRestrictedImageHeight = Integer.parseInt(text);
                    } else if ("maxRestrictedImageWidth".equalsIgnoreCase(value)) {
                        mMaxRestrictedImageWidth = Integer.parseInt(text);
                    }
                    /// @}
                    else if ("defaultSMSMessagesPerThread".equalsIgnoreCase(value)) {
                        mDefaultSMSMessagesPerThread = Integer.parseInt(text);
                    } else if ("defaultMMSMessagesPerThread".equalsIgnoreCase(value)) {
                        mDefaultMMSMessagesPerThread = Integer.parseInt(text);
                    } else if ("minMessageCountPerThread".equalsIgnoreCase(value)) {
                        mMinMessageCountPerThread = Integer.parseInt(text);
                    } else if ("maxMessageCountPerThread".equalsIgnoreCase(value)) {
                        mMaxMessageCountPerThread = Integer.parseInt(text);
                    } else if ("smsToMmsTextThreshold".equalsIgnoreCase(value)) {
                        /// M: Operator Plugin
                        mMmsConfigPlugin.setSmsToMmsTextThreshold(Integer.parseInt(text));
                    } else if ("recipientLimit".equalsIgnoreCase(value)) {
                        /// M: Operator Plugin
                        mMmsConfigPlugin.setMmsRecipientLimit(Integer.parseInt(text));
                    } else if ("httpSocketTimeout".equalsIgnoreCase(value)) {
                        mMmsConfigPlugin.setHttpSocketTimeout(Integer.parseInt(text));
                    } else if ("minimumSlideElementDuration".equalsIgnoreCase(value)) {
                        mMinimumSlideElementDuration = Integer.parseInt(text);
                    } else if ("maxSizeScaleForPendingMmsAllowed".equalsIgnoreCase(value)) {
                        mMaxSizeScaleForPendingMmsAllowed = Integer.parseInt(text);
                    } else if ("aliasMinChars".equalsIgnoreCase(value)) {
                        mAliasRuleMinChars = Integer.parseInt(text);
                    } else if ("aliasMaxChars".equalsIgnoreCase(value)) {
                        mAliasRuleMaxChars = Integer.parseInt(text);
                    } else if ("maxMessageTextSize".equalsIgnoreCase(value)) {
                        /// M: Operator Plugin
                        mMmsConfigPlugin.setMaxTextLimit(Integer.parseInt(text));
                    } else if ("maxSubjectLength".equalsIgnoreCase(value)) {
                        mMaxSubjectLength = Integer.parseInt(text);
                    }
                } else if ("string".equals(tag)) {
                    // string config tags go here
                    if ("userAgent".equalsIgnoreCase(value)) {
                        mUserAgent = text;
                    } else if ("uaProfTagName".equalsIgnoreCase(value)) {
                        mUaProfTagName = text;
                    } else if ("uaProfUrl".equalsIgnoreCase(value)) {
                        mUaProfUrl = text;
                    } else if ("httpParams".equalsIgnoreCase(value)) {
                        mHttpParams = text;
                    } else if ("httpParamsLine1Key".equalsIgnoreCase(value)) {
                        mHttpParamsLine1Key = text;
                    } else if ("emailGatewayNumber".equalsIgnoreCase(value)) {
                        mEmailGateway = text;
                    }
                }
            }
        }
    } catch (XmlPullParserException e) {
        Log.e(TAG, "loadMmsSettings caught ", e);
    } catch (NumberFormatException e) {
        Log.e(TAG, "loadMmsSettings caught ", e);
    } catch (IOException e) {
        Log.e(TAG, "loadMmsSettings caught ", e);
    } finally {
        parser.close();
    }

    String errorStr = null;

    if (getMmsEnabled() && mUaProfUrl == null) {
        errorStr = "uaProfUrl";
    }

    if (errorStr != null) {
        String err = String.format("MmsConfig.loadMmsSettings mms_config.xml missing %s setting", errorStr);
        Log.e(TAG, err);
    }
}

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

private static int loadUiOptionsFromManifest(Activity activity) {
    int uiOptions = 0;
    try {//from  w  w  w  .  jav  a2  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;
}

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./*w  w  w  .j  ava  2  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;
}