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

Commit 446d988b authored by Alan Viverette's avatar Alan Viverette
Browse files

Fix ShapeDrawable mutate, add clone() support to ArcShape, OvalShape

Also cleans up stray white space and fixes docs. Adds getters to ArcShape
properties so that we can write proper CTS tests.

Fixes: 35419960
Test: ArcShapeTest, OvalShape#testClone, ShapeDrawableTest
Change-Id: Icf1bae7f79f9294b2a60c49b85ce8749473ed61e
parent e20e7ebb
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -14011,6 +14011,8 @@ package android.graphics.drawable.shapes {
  public class ArcShape extends android.graphics.drawable.shapes.RectShape {
    ctor public ArcShape(float, float);
    method public final float getStartAngle();
    method public final float getSweepAngle();
  }
  public class OvalShape extends android.graphics.drawable.shapes.RectShape {
+2 −0
Original line number Diff line number Diff line
@@ -14647,6 +14647,8 @@ package android.graphics.drawable.shapes {
  public class ArcShape extends android.graphics.drawable.shapes.RectShape {
    ctor public ArcShape(float, float);
    method public final float getStartAngle();
    method public final float getSweepAngle();
  }
  public class OvalShape extends android.graphics.drawable.shapes.RectShape {
+2 −0
Original line number Diff line number Diff line
@@ -14049,6 +14049,8 @@ package android.graphics.drawable.shapes {
  public class ArcShape extends android.graphics.drawable.shapes.RectShape {
    ctor public ArcShape(float, float);
    method public final float getStartAngle();
    method public final float getSweepAngle();
  }
  public class OvalShape extends android.graphics.drawable.shapes.RectShape {
+57 −49
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package android.graphics.drawable;

import android.annotation.NonNull;
import android.content.pm.ActivityInfo.Config;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
@@ -31,10 +33,10 @@ import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.shapes.Shape;
import android.content.res.Resources.Theme;
import android.util.AttributeSet;

import com.android.internal.R;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

@@ -69,7 +71,7 @@ import java.io.IOException;
 * @attr ref android.R.styleable#ShapeDrawable_height
 */
public class ShapeDrawable extends Drawable {
    private ShapeState mShapeState;
    private @NonNull ShapeState mShapeState;
    private PorterDuffColorFilter mTintFilter;
    private boolean mMutated;

@@ -77,7 +79,7 @@ public class ShapeDrawable extends Drawable {
     * ShapeDrawable constructor.
     */
    public ShapeDrawable() {
        this(new ShapeState(null), null);
        this(new ShapeState(), null);
    }

    /**
@@ -86,7 +88,7 @@ public class ShapeDrawable extends Drawable {
     * @param s the Shape that this ShapeDrawable should be
     */
    public ShapeDrawable(Shape s) {
        this(new ShapeState(null), null);
        this(new ShapeState(), null);

        mShapeState.mShape = s;
    }
@@ -402,7 +404,7 @@ public class ShapeDrawable extends Drawable {
        }

        // Update local properties.
        updateLocalState(r);
        updateLocalState();
    }

    @Override
@@ -426,7 +428,7 @@ public class ShapeDrawable extends Drawable {
        }

        // Update local properties.
        updateLocalState(t.getResources());
        updateLocalState();
    }

    private void updateStateFromTypedArray(TypedArray a) {
@@ -447,10 +449,10 @@ public class ShapeDrawable extends Drawable {
        dither = a.getBoolean(R.styleable.ShapeDrawable_dither, dither);
        paint.setDither(dither);

        setIntrinsicWidth((int) a.getDimension(
                R.styleable.ShapeDrawable_width, state.mIntrinsicWidth));
        setIntrinsicHeight((int) a.getDimension(
                R.styleable.ShapeDrawable_height, state.mIntrinsicHeight));
        state.mIntrinsicWidth = (int) a.getDimension(
                R.styleable.ShapeDrawable_width, state.mIntrinsicWidth);
        state.mIntrinsicHeight = (int) a.getDimension(
                R.styleable.ShapeDrawable_height, state.mIntrinsicHeight);

        final int tintMode = a.getInt(R.styleable.ShapeDrawable_tintMode, -1);
        if (tintMode != -1) {
@@ -494,21 +496,8 @@ public class ShapeDrawable extends Drawable {
    @Override
    public Drawable mutate() {
        if (!mMutated && super.mutate() == this) {
            if (mShapeState.mPaint != null) {
                mShapeState.mPaint = new Paint(mShapeState.mPaint);
            } else {
                mShapeState.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            }
            if (mShapeState.mPadding != null) {
                mShapeState.mPadding = new Rect(mShapeState.mPadding);
            } else {
                mShapeState.mPadding = new Rect();
            }
            try {
                mShapeState.mShape = mShapeState.mShape.clone();
            } catch (CloneNotSupportedException e) {
                return null;
            }
            mShapeState = new ShapeState(mShapeState);
            updateLocalState();
            mMutated = true;
        }
        return this;
@@ -525,12 +514,13 @@ public class ShapeDrawable extends Drawable {
    /**
     * Defines the intrinsic properties of this ShapeDrawable's Shape.
     */
    final static class ShapeState extends ConstantState {
        int[] mThemeAttrs;
    static final class ShapeState extends ConstantState {
        final @NonNull Paint mPaint;

        @Config int mChangingConfigurations;
        Paint mPaint;
        int[] mThemeAttrs;
        Shape mShape;
        ColorStateList mTint = null;
        ColorStateList mTint;
        Mode mTintMode = DEFAULT_TINT_MODE;
        Rect mPadding;
        int mIntrinsicWidth;
@@ -538,21 +528,43 @@ public class ShapeDrawable extends Drawable {
        int mAlpha = 255;
        ShaderFactory mShaderFactory;

        ShapeState(ShapeState orig) {
            if (orig != null) {
        /**
         * Constructs a new ShapeState.
         */
        ShapeState() {
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        }

        /**
         * Constructs a new ShapeState that contains a deep copy of the
         * specified ShapeState.
         *
         * @param orig the state to create a deep copy of
         */
        ShapeState(@NonNull ShapeState orig) {
            mChangingConfigurations = orig.mChangingConfigurations;
            mPaint = new Paint(orig.mPaint);
            mThemeAttrs = orig.mThemeAttrs;
                mPaint = orig.mPaint;
            if (mShape != null) {
                try {
                    mShape = orig.mShape.clone();
                } catch (CloneNotSupportedException e) {
                    // Well, at least we tried.
                    mShape = orig.mShape;
                }
            }
            mTint = orig.mTint;
            mTintMode = orig.mTintMode;
                mPadding = orig.mPadding;
            if (orig.mPadding != null) {
                mPadding = new Rect(orig.mPadding);
            }
            mIntrinsicWidth = orig.mIntrinsicWidth;
            mIntrinsicHeight = orig.mIntrinsicHeight;
            mAlpha = orig.mAlpha;

            // We don't have any way to clone a shader factory, so hopefully
            // this class doesn't contain any local state.
            mShaderFactory = orig.mShaderFactory;
            } else {
                mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            }
        }

        @Override
@@ -585,7 +597,7 @@ public class ShapeDrawable extends Drawable {
    private ShapeDrawable(ShapeState state, Resources res) {
        mShapeState = state;

        updateLocalState(res);
        updateLocalState();
    }

    /**
@@ -593,7 +605,7 @@ public class ShapeDrawable extends Drawable {
     * after significant state changes, e.g. from the One True Constructor and
     * after inflating or applying a theme.
     */
    private void updateLocalState(Resources res) {
    private void updateLocalState() {
        mTintFilter = updateTintFilter(mTintFilter, mShapeState.mTint, mShapeState.mTintMode);
    }

@@ -617,8 +629,4 @@ public class ShapeDrawable extends Drawable {
         */
        public abstract Shader resize(int width, int height);
    }

    // other subclass could wack the Shader's localmatrix based on the
    // resize params (e.g. scaletofit, etc.). This could be used to scale
    // a bitmap to fill the bounds without needing any other special casing.
}
+36 −15
Original line number Diff line number Diff line
@@ -21,15 +21,17 @@ import android.graphics.Outline;
import android.graphics.Paint;

/**
 * Creates an arc shape. The arc shape starts at a specified
 * angle and sweeps clockwise, drawing slices of pie.
 * The arc can be drawn to a Canvas with its own draw() method,
 * but more graphical control is available if you instead pass
 * the ArcShape to a {@link android.graphics.drawable.ShapeDrawable}.
 * Creates an arc shape. The arc shape starts at a specified angle and sweeps
 * clockwise, drawing slices of pie.
 * <p>
 * The arc can be drawn to a {@link Canvas} with its own
 * {@link #draw(Canvas, Paint)} method, but more graphical control is available
 * if you instead pass the ArcShape to a
 * {@link android.graphics.drawable.ShapeDrawable}.
 */
public class ArcShape extends RectShape {
    private float mStart;
    private float mSweep;
    private final float mStartAngle;
    private final float mSweepAngle;

    /**
     * ArcShape constructor.
@@ -39,13 +41,27 @@ public class ArcShape extends RectShape {
     *                   greater than 360 results in a complete circle/oval.
     */
    public ArcShape(float startAngle, float sweepAngle) {
        mStart = startAngle;
        mSweep = sweepAngle;
        mStartAngle = startAngle;
        mSweepAngle = sweepAngle;
    }

    /**
     * @return the angle (in degrees) where the arc begins
     */
    public final float getStartAngle() {
        return mStartAngle;
    }

    /**
     * @return the sweep angle (in degrees)
     */
    public final float getSweepAngle() {
        return mSweepAngle;
    }

    @Override
    public void draw(Canvas canvas, Paint paint) {
        canvas.drawArc(rect(), mStart, mSweep, true, paint);
        canvas.drawArc(rect(), mStartAngle, mSweepAngle, true, paint);
    }

    @Override
@@ -53,5 +69,10 @@ public class ArcShape extends RectShape {
        // Since we don't support concave outlines, arc shape does not attempt
        // to provide an outline.
    }

    @Override
    public ArcShape clone() throws CloneNotSupportedException {
        return (ArcShape) super.clone();
    }
}
Loading