Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9cd14fc7 authored by Alan Viverette's avatar Alan Viverette
Browse files

Add support for tint attribute in VectorDrawable

Fixes NPE from previous change. Also fixes docs reference in TypedArray.

BUG: 15375203
Change-Id: Icf3b5759780276b0875a4fb70b29522ab7c8cb90
parent a427fcf2
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -885,9 +885,9 @@ public class TypedArray {


    /**
    /**
     * Extracts theme attributes from a typed array for later resolution using
     * Extracts theme attributes from a typed array for later resolution using
     * {@link Theme#resolveAttributes(int[], int[])}. Removes the entries from
     * {@link android.content.res.Resources.Theme#resolveAttributes(int[], int[])}.
     * the typed array so that subsequent calls to typed getters will return the
     * Removes the entries from the typed array so that subsequent calls to typed
     * default value without crashing.
     * getters will return the default value without crashing.
     *
     *
     * @return an array of length {@link #getIndexCount()} populated with theme
     * @return an array of length {@link #getIndexCount()} populated with theme
     *         attributes, or null if there are no theme attributes in the typed
     *         attributes, or null if there are no theme attributes in the typed
+10 −0
Original line number Original line Diff line number Diff line
@@ -4978,6 +4978,16 @@
    <!-- ========================== -->
    <!-- ========================== -->
    <eat-comment />
    <eat-comment />


    <!-- Drawable used to draw vector paths. -->
    <declare-styleable name="VectorDrawable">
        <!-- If set, specifies the color to apply to the drawable as a tint. By default,
             no tint is applied. May be a color state list. -->
        <attr name="tint" />
        <!-- When a tint color is set, specifies its Porter-Duff blending mode. The
             default value is src_in, which treats the drawable as an alpha mask. -->
        <attr name="tintMode" />
    </declare-styleable>

    <!-- Define the virtual size of the drawing surface paths will draw to. -->
    <!-- Define the virtual size of the drawing surface paths will draw to. -->
    <declare-styleable name="VectorDrawableViewport">
    <declare-styleable name="VectorDrawableViewport">
        <!-- The width of the canvas the drawing is on. -->
        <!-- The width of the canvas the drawing is on. -->
+36 −15
Original line number Original line Diff line number Diff line
@@ -285,13 +285,6 @@ public class VectorDrawable extends Drawable {
        }
        }
    }
    }


    @Override
    public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs, Theme theme)
            throws XmlPullParserException, IOException {
        final VPathRenderer p = inflateInternal(res, parser, attrs, theme);
        setPathRenderer(p);
    }

    @Override
    @Override
    public boolean canApplyTheme() {
    public boolean canApplyTheme() {
        return super.canApplyTheme() || mVectorState != null && mVectorState.canApplyTheme();
        return super.canApplyTheme() || mVectorState != null && mVectorState.canApplyTheme();
@@ -335,13 +328,44 @@ public class VectorDrawable extends Drawable {
        return color;
        return color;
    }
    }


    private VPathRenderer inflateInternal(Resources res, XmlPullParser parser, AttributeSet attrs,

            Theme theme) throws XmlPullParserException, IOException {
    @Override
    public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs, Theme theme)
            throws XmlPullParserException, IOException {
        final TypedArray a = obtainAttributes(res, theme,  attrs,R.styleable.VectorDrawable);
        updateStateFromTypedArray(a);
        a.recycle();

        final VectorDrawableState state = mVectorState;
        state.mVPathRenderer = inflateInternal(res, parser, attrs, theme);

        mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode);
        state.mVPathRenderer.setColorFilter(mTintFilter);
    }

    private void updateStateFromTypedArray(TypedArray a) {
        final VectorDrawableState state = mVectorState;

        // Extract the theme attributes, if any.
        state.mThemeAttrs = a.extractThemeAttrs();

        final int tintMode = a.getInt(R.styleable.VectorDrawable_tintMode, -1);
        if (tintMode != -1) {
            state.mTintMode = Drawable.parseTintMode(tintMode, Mode.SRC_IN);
        }

        final ColorStateList tint = a.getColorStateList(R.styleable.VectorDrawable_tint);
        if (tint != null) {
            state.mTint = tint;
        }
    }

    private VPathRenderer inflateInternal(Resources res, XmlPullParser parser, AttributeSet attrs, Theme theme)
            throws XmlPullParserException, IOException {
        final VPathRenderer pathRenderer = new VPathRenderer();
        final VPathRenderer pathRenderer = new VPathRenderer();


        boolean noSizeTag = true;
        boolean noSizeTag = true;
        boolean noViewportTag = true;
        boolean noViewportTag = true;
        boolean noGroupTag = true;
        boolean noPathTag = true;
        boolean noPathTag = true;


        // Use a stack to help to build the group tree.
        // Use a stack to help to build the group tree.
@@ -377,7 +401,6 @@ public class VectorDrawable extends Drawable {
                    if (newChildGroup.getGroupName() != null) {
                    if (newChildGroup.getGroupName() != null) {
                        mVGTargetsMap.put(newChildGroup.getGroupName(), newChildGroup);
                        mVGTargetsMap.put(newChildGroup.getGroupName(), newChildGroup);
                    }
                    }
                    noGroupTag = false;
                }
                }
            } else if (eventType == XmlPullParser.END_TAG) {
            } else if (eventType == XmlPullParser.END_TAG) {
                final String tagName = parser.getName();
                final String tagName = parser.getName();
@@ -435,11 +458,8 @@ public class VectorDrawable extends Drawable {
        }
        }
    }
    }


    private void setPathRenderer(VPathRenderer pathRenderer) {
        mVectorState.mVPathRenderer = pathRenderer;
    }

    private static class VectorDrawableState extends ConstantState {
    private static class VectorDrawableState extends ConstantState {
        int[] mThemeAttrs;
        int mChangingConfigurations;
        int mChangingConfigurations;
        VPathRenderer mVPathRenderer;
        VPathRenderer mVPathRenderer;
        Rect mPadding;
        Rect mPadding;
@@ -448,6 +468,7 @@ public class VectorDrawable extends Drawable {


        public VectorDrawableState(VectorDrawableState copy) {
        public VectorDrawableState(VectorDrawableState copy) {
            if (copy != null) {
            if (copy != null) {
                mThemeAttrs = copy.mThemeAttrs;
                mChangingConfigurations = copy.mChangingConfigurations;
                mChangingConfigurations = copy.mChangingConfigurations;
                // TODO: Make sure the constant state are handled correctly.
                // TODO: Make sure the constant state are handled correctly.
                mVPathRenderer = new VPathRenderer(copy.mVPathRenderer);
                mVPathRenderer = new VPathRenderer(copy.mVPathRenderer);