Example usage for android.content.res XmlResourceParser getAttributeValue

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

Introduction

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

Prototype

public String getAttributeValue(int index);

Source Link

Document

Returns the value of the specified attribute as a string representation.

Usage

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/*  w  w  w .  j  a  v  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 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  ava 2s.  c  o  m*/
            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.xml.XmlDialogParser.java

/**
 * Resolves situation, if at the current parsed position of Xml parser is string resource or
 * raw string value.//from   www  .jav  a 2s  . 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.android.mms.MmsConfig.java

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

    try {//from www . jav a 2 s . 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 ww w  .ja va  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.j a  va  2s .  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;
}