From f26382fd9c8efd90c07aa70174cdda5a43c4ae21 Mon Sep 17 00:00:00 2001 From: Nader Jawad Date: Wed, 27 Feb 2019 13:03:11 -0800 Subject: Implemented API-review suggested changes on GradientDrawable --Added @Px annotations to pixel related parameters in added APIs --Added @FloatRange annotation to float based APIs (thickness/inner)Ratio --Added error checking to invalid ratio arguments to set(Thickness/Inner)ratio Bug: 126375868 Bug: 126701500 Test: Added CTS tests to verify parameter validation logic Change-Id: I8ca70f38edd18f68c168d4d6e2c93bcb9b64c488 --- .../graphics/drawable/GradientDrawable.java | 28 +++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'graphics/java') diff --git a/graphics/java/android/graphics/drawable/GradientDrawable.java b/graphics/java/android/graphics/drawable/GradientDrawable.java index 6ecb62140532..5c2524dde948 100644 --- a/graphics/java/android/graphics/drawable/GradientDrawable.java +++ b/graphics/java/android/graphics/drawable/GradientDrawable.java @@ -17,9 +17,11 @@ package android.graphics.drawable; import android.annotation.ColorInt; +import android.annotation.FloatRange; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; +import android.annotation.Px; import android.annotation.UnsupportedAppUsage; import android.content.pm.ActivityInfo.Config; import android.content.res.ColorStateList; @@ -670,7 +672,7 @@ public class GradientDrawable extends Drawable { * @see #mutate() * @see #setColor(int) */ - public void setColors(@ColorInt int[] colors) { + public void setColors(@Nullable @ColorInt int[] colors) { setColors(colors, null); } @@ -690,7 +692,7 @@ public class GradientDrawable extends Drawable { * @see #mutate() * @see #setColors(int[]) */ - public void setColors(@ColorInt int[] colors, @Nullable float[] offsets) { + public void setColors(@Nullable @ColorInt int[] colors, @Nullable float[] offsets) { mGradientState.setGradientColors(colors); mGradientState.mPositions = offsets; mGradientIsDirty = true; @@ -877,7 +879,11 @@ public class GradientDrawable extends Drawable { * @see #getInnerRadiusRatio() * @attr ref android.R.styleable#GradientDrawable_innerRadiusRatio */ - public void setInnerRadiusRatio(float innerRadiusRatio) { + public void setInnerRadiusRatio( + @FloatRange(from = 0.0f, fromInclusive = false) float innerRadiusRatio) { + if (innerRadiusRatio <= 0) { + throw new IllegalArgumentException("Ratio must be greater than zero"); + } mGradientState.mInnerRadiusRatio = innerRadiusRatio; mPathIsDirty = true; invalidateSelf(); @@ -899,7 +905,7 @@ public class GradientDrawable extends Drawable { * @see #getInnerRadius() * @attr ref android.R.styleable#GradientDrawable_innerRadius */ - public void setInnerRadius(int innerRadius) { + public void setInnerRadius(@Px int innerRadius) { mGradientState.mInnerRadius = innerRadius; mPathIsDirty = true; invalidateSelf(); @@ -911,7 +917,7 @@ public class GradientDrawable extends Drawable { * @see #setInnerRadius(int) * @attr ref android.R.styleable#GradientDrawable_innerRadius */ - public int getInnerRadius() { + public @Px int getInnerRadius() { return mGradientState.mInnerRadius; } @@ -921,7 +927,11 @@ public class GradientDrawable extends Drawable { * @see #getThicknessRatio() * @attr ref android.R.styleable#GradientDrawable_thicknessRatio */ - public void setThicknessRatio(float thicknessRatio) { + public void setThicknessRatio( + @FloatRange(from = 0.0f, fromInclusive = false) float thicknessRatio) { + if (thicknessRatio <= 0) { + throw new IllegalArgumentException("Ratio must be greater than zero"); + } mGradientState.mThicknessRatio = thicknessRatio; mPathIsDirty = true; invalidateSelf(); @@ -942,7 +952,7 @@ public class GradientDrawable extends Drawable { * * @attr ref android.R.styleable#GradientDrawable_thickness */ - public void setThickness(int thickness) { + public void setThickness(@Px int thickness) { mGradientState.mThickness = thickness; mPathIsDirty = true; invalidateSelf(); @@ -954,7 +964,7 @@ public class GradientDrawable extends Drawable { * @see #setThickness(int) * @attr ref android.R.styleable#GradientDrawable_thickness */ - public int getThickness() { + public @Px int getThickness() { return mGradientState.mThickness; } @@ -970,7 +980,7 @@ public class GradientDrawable extends Drawable { * @attr ref android.R.styleable#GradientDrawablePadding_right * @attr ref android.R.styleable#GradientDrawablePadding_bottom */ - public void setPadding(int left, int top, int right, int bottom) { + public void setPadding(@Px int left, @Px int top, @Px int right, @Px int bottom) { if (mGradientState.mPadding == null) { mGradientState.mPadding = new Rect(); } -- cgit v1.2.3-59-g8ed1b