Search in sources :

Example 16 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project android_frameworks_base by ParanoidAndroid.

the class XmlUtils method readThisMapXml.

/**
     * Read a HashMap object from an XmlPullParser.  The XML data could
     * previously have been generated by writeMapXml().  The XmlPullParser
     * must be positioned <em>after</em> the tag that begins the map.
     *
     * @param parser The XmlPullParser from which to read the map data.
     * @param endTag Name of the tag that will end the map, usually "map".
     * @param name An array of one string, used to return the name attribute
     *             of the map's tag.
     *
     * @return HashMap The newly generated map.
     *
     * @see #readMapXml
     */
public static final HashMap readThisMapXml(XmlPullParser parser, String endTag, String[] name) throws XmlPullParserException, java.io.IOException {
    HashMap map = new HashMap();
    int eventType = parser.getEventType();
    do {
        if (eventType == parser.START_TAG) {
            Object val = readThisValueXml(parser, name);
            if (name[0] != null) {
                //System.out.println("Adding to map: " + name + " -> " + val);
                map.put(name[0], val);
            } else {
                throw new XmlPullParserException("Map value without name attribute: " + parser.getName());
            }
        } else if (eventType == parser.END_TAG) {
            if (parser.getName().equals(endTag)) {
                return map;
            }
            throw new XmlPullParserException("Expected " + endTag + " end tag at: " + parser.getName());
        }
        eventType = parser.next();
    } while (eventType != parser.END_DOCUMENT);
    throw new XmlPullParserException("Document ended before " + endTag + " end tag");
}
Also used : HashMap(java.util.HashMap) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 17 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project android_frameworks_base by ParanoidAndroid.

the class XmlUtils method readMapXml.

/**
     * Read a HashMap from an InputStream containing XML.  The stream can
     * previously have been written by writeMapXml().
     *
     * @param in The InputStream from which to read.
     *
     * @return HashMap The resulting map.
     *
     * @see #readListXml
     * @see #readValueXml
     * @see #readThisMapXml
     * #see #writeMapXml
     */
public static final HashMap readMapXml(InputStream in) throws XmlPullParserException, java.io.IOException {
    XmlPullParser parser = Xml.newPullParser();
    parser.setInput(in, null);
    return (HashMap) readValueXml(parser, new String[1]);
}
Also used : HashMap(java.util.HashMap) XmlPullParser(org.xmlpull.v1.XmlPullParser)

Example 18 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project android_frameworks_base by ParanoidAndroid.

the class Drawable method createFromXml.

/**
     * Create a drawable from an XML document. For more information on how to
     * create resources in XML, see
     * <a href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>.
     */
public static Drawable createFromXml(Resources r, XmlPullParser parser) throws XmlPullParserException, IOException {
    AttributeSet attrs = Xml.asAttributeSet(parser);
    int type;
    while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
    // Empty loop
    }
    if (type != XmlPullParser.START_TAG) {
        throw new XmlPullParserException("No start tag found");
    }
    Drawable drawable = createFromXmlInner(r, parser, attrs);
    if (drawable == null) {
        throw new RuntimeException("Unknown initial tag: " + parser.getName());
    }
    return drawable;
}
Also used : AttributeSet(android.util.AttributeSet) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 19 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project android_frameworks_base by ParanoidAndroid.

the class GradientDrawable method inflate.

@Override
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException {
    final GradientState st = mGradientState;
    TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.GradientDrawable);
    super.inflateWithAttributes(r, parser, a, com.android.internal.R.styleable.GradientDrawable_visible);
    int shapeType = a.getInt(com.android.internal.R.styleable.GradientDrawable_shape, RECTANGLE);
    boolean dither = a.getBoolean(com.android.internal.R.styleable.GradientDrawable_dither, false);
    if (shapeType == RING) {
        st.mInnerRadius = a.getDimensionPixelSize(com.android.internal.R.styleable.GradientDrawable_innerRadius, -1);
        if (st.mInnerRadius == -1) {
            st.mInnerRadiusRatio = a.getFloat(com.android.internal.R.styleable.GradientDrawable_innerRadiusRatio, 3.0f);
        }
        st.mThickness = a.getDimensionPixelSize(com.android.internal.R.styleable.GradientDrawable_thickness, -1);
        if (st.mThickness == -1) {
            st.mThicknessRatio = a.getFloat(com.android.internal.R.styleable.GradientDrawable_thicknessRatio, 9.0f);
        }
        st.mUseLevelForShape = a.getBoolean(com.android.internal.R.styleable.GradientDrawable_useLevel, true);
    }
    a.recycle();
    setShape(shapeType);
    setDither(dither);
    int type;
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        if (depth > innerDepth) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("size")) {
            a = r.obtainAttributes(attrs, com.android.internal.R.styleable.GradientDrawableSize);
            int width = a.getDimensionPixelSize(com.android.internal.R.styleable.GradientDrawableSize_width, -1);
            int height = a.getDimensionPixelSize(com.android.internal.R.styleable.GradientDrawableSize_height, -1);
            a.recycle();
            setSize(width, height);
        } else if (name.equals("gradient")) {
            a = r.obtainAttributes(attrs, com.android.internal.R.styleable.GradientDrawableGradient);
            int startColor = a.getColor(com.android.internal.R.styleable.GradientDrawableGradient_startColor, 0);
            boolean hasCenterColor = a.hasValue(com.android.internal.R.styleable.GradientDrawableGradient_centerColor);
            int centerColor = a.getColor(com.android.internal.R.styleable.GradientDrawableGradient_centerColor, 0);
            int endColor = a.getColor(com.android.internal.R.styleable.GradientDrawableGradient_endColor, 0);
            int gradientType = a.getInt(com.android.internal.R.styleable.GradientDrawableGradient_type, LINEAR_GRADIENT);
            st.mCenterX = getFloatOrFraction(a, com.android.internal.R.styleable.GradientDrawableGradient_centerX, 0.5f);
            st.mCenterY = getFloatOrFraction(a, com.android.internal.R.styleable.GradientDrawableGradient_centerY, 0.5f);
            st.mUseLevel = a.getBoolean(com.android.internal.R.styleable.GradientDrawableGradient_useLevel, false);
            st.mGradient = gradientType;
            if (gradientType == LINEAR_GRADIENT) {
                int angle = (int) a.getFloat(com.android.internal.R.styleable.GradientDrawableGradient_angle, 0);
                angle %= 360;
                if (angle % 45 != 0) {
                    throw new XmlPullParserException(a.getPositionDescription() + "<gradient> tag requires 'angle' attribute to " + "be a multiple of 45");
                }
                switch(angle) {
                    case 0:
                        st.mOrientation = Orientation.LEFT_RIGHT;
                        break;
                    case 45:
                        st.mOrientation = Orientation.BL_TR;
                        break;
                    case 90:
                        st.mOrientation = Orientation.BOTTOM_TOP;
                        break;
                    case 135:
                        st.mOrientation = Orientation.BR_TL;
                        break;
                    case 180:
                        st.mOrientation = Orientation.RIGHT_LEFT;
                        break;
                    case 225:
                        st.mOrientation = Orientation.TR_BL;
                        break;
                    case 270:
                        st.mOrientation = Orientation.TOP_BOTTOM;
                        break;
                    case 315:
                        st.mOrientation = Orientation.TL_BR;
                        break;
                }
            } else {
                TypedValue tv = a.peekValue(com.android.internal.R.styleable.GradientDrawableGradient_gradientRadius);
                if (tv != null) {
                    boolean radiusRel = tv.type == TypedValue.TYPE_FRACTION;
                    st.mGradientRadius = radiusRel ? tv.getFraction(1.0f, 1.0f) : tv.getFloat();
                } else if (gradientType == RADIAL_GRADIENT) {
                    throw new XmlPullParserException(a.getPositionDescription() + "<gradient> tag requires 'gradientRadius' " + "attribute with radial type");
                }
            }
            a.recycle();
            if (hasCenterColor) {
                st.mColors = new int[3];
                st.mColors[0] = startColor;
                st.mColors[1] = centerColor;
                st.mColors[2] = endColor;
                st.mPositions = new float[3];
                st.mPositions[0] = 0.0f;
                // Since 0.5f is default value, try to take the one that isn't 0.5f
                st.mPositions[1] = st.mCenterX != 0.5f ? st.mCenterX : st.mCenterY;
                st.mPositions[2] = 1f;
            } else {
                st.mColors = new int[2];
                st.mColors[0] = startColor;
                st.mColors[1] = endColor;
            }
        } else if (name.equals("solid")) {
            a = r.obtainAttributes(attrs, com.android.internal.R.styleable.GradientDrawableSolid);
            int argb = a.getColor(com.android.internal.R.styleable.GradientDrawableSolid_color, 0);
            a.recycle();
            setColor(argb);
        } else if (name.equals("stroke")) {
            a = r.obtainAttributes(attrs, com.android.internal.R.styleable.GradientDrawableStroke);
            int width = a.getDimensionPixelSize(com.android.internal.R.styleable.GradientDrawableStroke_width, 0);
            int color = a.getColor(com.android.internal.R.styleable.GradientDrawableStroke_color, 0);
            float dashWidth = a.getDimension(com.android.internal.R.styleable.GradientDrawableStroke_dashWidth, 0);
            if (dashWidth != 0.0f) {
                float dashGap = a.getDimension(com.android.internal.R.styleable.GradientDrawableStroke_dashGap, 0);
                setStroke(width, color, dashWidth, dashGap);
            } else {
                setStroke(width, color);
            }
            a.recycle();
        } else if (name.equals("corners")) {
            a = r.obtainAttributes(attrs, com.android.internal.R.styleable.DrawableCorners);
            int radius = a.getDimensionPixelSize(com.android.internal.R.styleable.DrawableCorners_radius, 0);
            setCornerRadius(radius);
            int topLeftRadius = a.getDimensionPixelSize(com.android.internal.R.styleable.DrawableCorners_topLeftRadius, radius);
            int topRightRadius = a.getDimensionPixelSize(com.android.internal.R.styleable.DrawableCorners_topRightRadius, radius);
            int bottomLeftRadius = a.getDimensionPixelSize(com.android.internal.R.styleable.DrawableCorners_bottomLeftRadius, radius);
            int bottomRightRadius = a.getDimensionPixelSize(com.android.internal.R.styleable.DrawableCorners_bottomRightRadius, radius);
            if (topLeftRadius != radius || topRightRadius != radius || bottomLeftRadius != radius || bottomRightRadius != radius) {
                // The corner radii are specified in clockwise order (see Path.addRoundRect())
                setCornerRadii(new float[] { topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius });
            }
            a.recycle();
        } else if (name.equals("padding")) {
            a = r.obtainAttributes(attrs, com.android.internal.R.styleable.GradientDrawablePadding);
            mPadding = new Rect(a.getDimensionPixelOffset(com.android.internal.R.styleable.GradientDrawablePadding_left, 0), a.getDimensionPixelOffset(com.android.internal.R.styleable.GradientDrawablePadding_top, 0), a.getDimensionPixelOffset(com.android.internal.R.styleable.GradientDrawablePadding_right, 0), a.getDimensionPixelOffset(com.android.internal.R.styleable.GradientDrawablePadding_bottom, 0));
            a.recycle();
            mGradientState.mPadding = mPadding;
        } else {
            Log.w("drawable", "Bad element under <shape>: " + name);
        }
    }
    mGradientState.computeOpacity();
}
Also used : Rect(android.graphics.Rect) TypedArray(android.content.res.TypedArray) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Paint(android.graphics.Paint) TypedValue(android.util.TypedValue)

Example 20 with XmlPullParser

use of org.xmlpull.v1.XmlPullParser in project android_frameworks_base by ParanoidAndroid.

the class InsetDrawable method inflate.

@Override
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException {
    int type;
    TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.InsetDrawable);
    super.inflateWithAttributes(r, parser, a, com.android.internal.R.styleable.InsetDrawable_visible);
    int drawableRes = a.getResourceId(com.android.internal.R.styleable.InsetDrawable_drawable, 0);
    int inLeft = a.getDimensionPixelOffset(com.android.internal.R.styleable.InsetDrawable_insetLeft, 0);
    int inTop = a.getDimensionPixelOffset(com.android.internal.R.styleable.InsetDrawable_insetTop, 0);
    int inRight = a.getDimensionPixelOffset(com.android.internal.R.styleable.InsetDrawable_insetRight, 0);
    int inBottom = a.getDimensionPixelOffset(com.android.internal.R.styleable.InsetDrawable_insetBottom, 0);
    a.recycle();
    Drawable dr;
    if (drawableRes != 0) {
        dr = r.getDrawable(drawableRes);
    } else {
        while ((type = parser.next()) == XmlPullParser.TEXT) {
        }
        if (type != XmlPullParser.START_TAG) {
            throw new XmlPullParserException(parser.getPositionDescription() + ": <inset> tag requires a 'drawable' attribute or " + "child tag defining a drawable");
        }
        dr = Drawable.createFromXmlInner(r, parser, attrs);
    }
    if (dr == null) {
        Log.w("drawable", "No drawable specified for <inset>");
    }
    mInsetState.mDrawable = dr;
    mInsetState.mInsetLeft = inLeft;
    mInsetState.mInsetRight = inRight;
    mInsetState.mInsetTop = inTop;
    mInsetState.mInsetBottom = inBottom;
    if (dr != null) {
        dr.setCallback(this);
    }
}
Also used : TypedArray(android.content.res.TypedArray) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Aggregations

XmlPullParser (org.xmlpull.v1.XmlPullParser)665 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)614 IOException (java.io.IOException)376 FileNotFoundException (java.io.FileNotFoundException)185 FileInputStream (java.io.FileInputStream)184 File (java.io.File)107 ArrayList (java.util.ArrayList)75 StringReader (java.io.StringReader)65 AttributeSet (android.util.AttributeSet)61 Test (org.junit.Test)57 TypedArray (android.content.res.TypedArray)56 InputStream (java.io.InputStream)48 AtomicFile (android.util.AtomicFile)47 HashMap (java.util.HashMap)42 BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)39 FileReader (java.io.FileReader)36 BufferedInputStream (java.io.BufferedInputStream)30 XmlPullParserFactory (org.xmlpull.v1.XmlPullParserFactory)29 RemoteException (android.os.RemoteException)28 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)28