From 4dac4ab1c69887c3fb4ad389421227a987f0928f Mon Sep 17 00:00:00 2001 From: Zhang Hai Date: Sat, 27 Oct 2018 22:59:19 -0700 Subject: [PATCH] [Refactor] Add annotations and final wherever possible. --- ...IndeterminateCircularProgressDrawable.java | 1 + .../materialprogressbar/Animators.java | 13 ++-- .../materialprogressbar/BaseDrawable.java | 23 +++++-- .../BaseIndeterminateProgressDrawable.java | 6 +- .../BasePaintDrawable.java | 11 +++- .../BaseProgressLayerDrawable.java | 15 +++-- .../BaseSingleCircularProgressDrawable.java | 11 ++-- .../BaseSingleHorizontalProgressDrawable.java | 15 ++++- .../CircularProgressBackgroundDrawable.java | 6 +- .../CircularProgressDrawable.java | 4 +- .../HorizontalProgressBackgroundDrawable.java | 6 +- .../HorizontalProgressDrawable.java | 4 +- ...IndeterminateCircularProgressDrawable.java | 28 ++++++--- ...determinateHorizontalProgressDrawable.java | 25 ++++++-- .../MaterialProgressBar.java | 60 ++++++++++++------- .../SingleCircularProgressDrawable.java | 7 ++- .../SingleHorizontalProgressDrawable.java | 9 ++- ...IndeterminateCircularProgressDrawable.java | 13 +++- .../internal/DrawableCompat.java | 5 +- .../internal/ObjectAnimatorCompat.java | 54 +++++++++++------ .../internal/ObjectAnimatorCompatBase.java | 45 +++++++++----- .../ObjectAnimatorCompatLollipop.java | 36 +++++++---- .../internal/ThemeUtils.java | 11 +++- 23 files changed, 285 insertions(+), 123 deletions(-) diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/AnimationScaleIndeterminateCircularProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/AnimationScaleIndeterminateCircularProgressDrawable.java index 4a4311e..250bf60 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/AnimationScaleIndeterminateCircularProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/AnimationScaleIndeterminateCircularProgressDrawable.java @@ -27,6 +27,7 @@ public void setUseIntrinsicPadding(boolean useIntrinsicPadding) { getIntrinsicPaddingDrawable().setUseIntrinsicPadding(useIntrinsicPadding); } + @NonNull private IntrinsicPaddingDrawable getIntrinsicPaddingDrawable() { return (IntrinsicPaddingDrawable) getCurrent(); } diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/Animators.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/Animators.java index dc354d9..c906cc5 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/Animators.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/Animators.java @@ -12,6 +12,7 @@ import android.annotation.SuppressLint; import android.graphics.Path; +import androidx.annotation.NonNull; import me.zhanghai.android.materialprogressbar.internal.ObjectAnimatorCompat; /** @@ -81,7 +82,8 @@ private Animators() {} * @param target The object whose properties are to be animated. * @return An Animator object that is set up to behave the same as the its native counterpart. */ - public static Animator createIndeterminateHorizontalRect1(Object target) { + @NonNull + public static Animator createIndeterminateHorizontalRect1(@NonNull Object target) { ObjectAnimator translateXAnimator = ObjectAnimatorCompat.ofFloat(target, "translateX", null, PATH_INDETERMINATE_HORIZONTAL_RECT1_TRANSLATE_X); @@ -109,7 +111,8 @@ public static Animator createIndeterminateHorizontalRect1(Object target) { * @param target The object whose properties are to be animated. * @return An Animator object that is set up to behave the same as the its native counterpart. */ - public static Animator createIndeterminateHorizontalRect2(Object target) { + @NonNull + public static Animator createIndeterminateHorizontalRect2(@NonNull Object target) { ObjectAnimator translateXAnimator = ObjectAnimatorCompat.ofFloat(target, "translateX", null, PATH_INDETERMINATE_HORIZONTAL_RECT2_TRANSLATE_X); @@ -136,7 +139,8 @@ public static Animator createIndeterminateHorizontalRect2(Object target) { * @param target The object whose properties are to be animated. * @return An Animator object that is set up to behave the same as the its native counterpart. */ - public static Animator createIndeterminate(Object target) { + @NonNull + public static Animator createIndeterminate(@NonNull Object target) { @SuppressLint("ObjectAnimatorBinding") ObjectAnimator trimPathStartAnimator = ObjectAnimator.ofFloat(target, "trimPathStart", 0, @@ -172,7 +176,8 @@ public static Animator createIndeterminate(Object target) { * @param target The object whose properties are to be animated. * @return An Animator object that is set up to behave the same as the its native counterpart. */ - public static Animator createIndeterminateRotation(Object target) { + @NonNull + public static Animator createIndeterminateRotation(@NonNull Object target) { @SuppressLint("ObjectAnimatorBinding") ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(target, "rotation", 0, 720); rotationAnimator.setDuration(6665); diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseDrawable.java index bacf02d..74b81b3 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseDrawable.java @@ -16,19 +16,27 @@ import android.graphics.drawable.Drawable; import androidx.annotation.ColorInt; +import androidx.annotation.IntRange; import androidx.annotation.NonNull; import androidx.annotation.Nullable; abstract class BaseDrawable extends Drawable implements TintableDrawable { - protected int mAlpha = 0xFF; + @IntRange(from = 0, to = 255) + protected int mAlpha = 255; + @Nullable protected ColorFilter mColorFilter; + @Nullable protected ColorStateList mTintList; + @NonNull protected PorterDuff.Mode mTintMode = PorterDuff.Mode.SRC_IN; + @Nullable protected PorterDuffColorFilter mTintFilter; - private DummyConstantState mConstantState = new DummyConstantState(); + @NonNull + private final DummyConstantState mConstantState = new DummyConstantState(); + @IntRange(from = 0, to = 255) @Override public int getAlpha() { return mAlpha; @@ -38,7 +46,7 @@ public int getAlpha() { * {@inheritDoc} */ @Override - public void setAlpha(int alpha) { + public void setAlpha(@IntRange(from = 0, to = 255) int alpha) { if (mAlpha != alpha) { mAlpha = alpha; invalidateSelf(); @@ -48,6 +56,7 @@ public void setAlpha(int alpha) { /** * {@inheritDoc} */ + @Nullable @Override public ColorFilter getColorFilter() { return mColorFilter; @@ -98,7 +107,7 @@ public boolean isStateful() { } @Override - protected boolean onStateChange(int[] state) { + protected boolean onStateChange(@NonNull int[] state) { return updateTintFilter(); } @@ -129,7 +138,7 @@ public int getOpacity() { * {@inheritDoc} */ @Override - public void draw(Canvas canvas) { + public void draw(@NonNull Canvas canvas) { Rect bounds = getBounds(); if (bounds.width() == 0 || bounds.height() == 0) { @@ -142,17 +151,19 @@ public void draw(Canvas canvas) { canvas.restoreToCount(saveCount); } + @Nullable protected ColorFilter getColorFilterForDrawing() { return mColorFilter != null ? mColorFilter : mTintFilter; } - protected abstract void onDraw(Canvas canvas, int width, int height); + protected abstract void onDraw(@NonNull Canvas canvas, int width, int height); // Workaround LayerDrawable.ChildDrawable which calls getConstantState().newDrawable() // without checking for null. // We are never inflated from XML so the protocol of ConstantState does not apply to us. In // order to make LayerDrawable happy, we return ourselves from DummyConstantState.newDrawable(). + @NonNull @Override public ConstantState getConstantState() { return mConstantState; diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseIndeterminateProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseIndeterminateProgressDrawable.java index 9eccfdb..bb61d37 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseIndeterminateProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseIndeterminateProgressDrawable.java @@ -12,15 +12,17 @@ import android.graphics.Color; import android.graphics.drawable.Animatable; +import androidx.annotation.NonNull; import me.zhanghai.android.materialprogressbar.internal.ThemeUtils; abstract class BaseIndeterminateProgressDrawable extends BaseProgressDrawable implements Animatable { + @NonNull protected Animator[] mAnimators; @SuppressLint("NewApi") - public BaseIndeterminateProgressDrawable(Context context) { + public BaseIndeterminateProgressDrawable(@NonNull Context context) { int controlActivatedColor = ThemeUtils.getColorFromAttrRes(R.attr.colorControlActivated, Color.BLACK, context); // setTint() has been overridden for compatibility; DrawableCompat won't work because @@ -32,7 +34,7 @@ public BaseIndeterminateProgressDrawable(Context context) { * {@inheritDoc} */ @Override - public void draw(Canvas canvas) { + public void draw(@NonNull Canvas canvas) { super.draw(canvas); if (isStarted()) { diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/BasePaintDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/BasePaintDrawable.java index 01c79ce..b6d82a4 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/BasePaintDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/BasePaintDrawable.java @@ -9,12 +9,16 @@ import android.graphics.Color; import android.graphics.Paint; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + abstract class BasePaintDrawable extends BaseDrawable { + @Nullable private Paint mPaint; @Override - protected final void onDraw(Canvas canvas, int width, int height) { + protected final void onDraw(@NonNull Canvas canvas, int width, int height) { if (mPaint == null) { mPaint = new Paint(); @@ -28,7 +32,8 @@ protected final void onDraw(Canvas canvas, int width, int height) { onDraw(canvas, width, height, mPaint); } - protected abstract void onPreparePaint(Paint paint); + protected abstract void onPreparePaint(@NonNull Paint paint); - protected abstract void onDraw(Canvas canvas, int width, int height, Paint paint); + protected abstract void onDraw(@NonNull Canvas canvas, int width, int height, + @NonNull Paint paint); } diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseProgressLayerDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseProgressLayerDrawable.java index 16d931d..9c04903 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseProgressLayerDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseProgressLayerDrawable.java @@ -15,6 +15,7 @@ import android.util.Log; import androidx.annotation.ColorInt; +import androidx.annotation.FloatRange; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.graphics.ColorUtils; @@ -27,13 +28,17 @@ class BaseProgressLayerDrawable< extends LayerDrawable implements IntrinsicPaddingDrawable, MaterialProgressDrawable, ShowBackgroundDrawable, TintableDrawable { + @FloatRange(from = 0, to = 1) private float mBackgroundAlpha; - private BackgroundDrawableType mBackgroundDrawable; - private ProgressDrawableType mSecondaryProgressDrawable; - private ProgressDrawableType mProgressDrawable; + @NonNull + private final BackgroundDrawableType mBackgroundDrawable; + @NonNull + private final ProgressDrawableType mSecondaryProgressDrawable; + @NonNull + private final ProgressDrawableType mProgressDrawable; - public BaseProgressLayerDrawable(Drawable[] layers, Context context) { + public BaseProgressLayerDrawable(@NonNull Drawable[] layers, @NonNull Context context) { super(layers); mBackgroundAlpha = ThemeUtils.getFloatFromAttrRes(android.R.attr.disabledAlpha, 0, context); @@ -116,7 +121,7 @@ public void setTintList(@Nullable ColorStateList tint) { Log.w(getClass().getSimpleName(), "setTintList() called with a non-opaque" + " ColorStateList, its original alpha will be discarded"); } - backgroundTint = tint.withAlpha(Math.round(0xFF * mBackgroundAlpha)); + backgroundTint = tint.withAlpha(Math.round(mBackgroundAlpha * 255)); } else { backgroundTint = null; } diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseSingleCircularProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseSingleCircularProgressDrawable.java index a3fabe0..59cf577 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseSingleCircularProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseSingleCircularProgressDrawable.java @@ -9,6 +9,8 @@ import android.graphics.Paint; import android.graphics.RectF; +import androidx.annotation.NonNull; + abstract class BaseSingleCircularProgressDrawable extends BaseProgressDrawable { private static final RectF RECT_BOUND = new RectF(-21, -21, 21, 21); @@ -16,13 +18,13 @@ abstract class BaseSingleCircularProgressDrawable extends BaseProgressDrawable { private static final RectF RECT_PROGRESS = new RectF(-19, -19, 19, 19); @Override - protected void onPreparePaint(Paint paint) { + protected void onPreparePaint(@NonNull Paint paint) { paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(4); } @Override - protected void onDraw(Canvas canvas, int width, int height, Paint paint) { + protected void onDraw(@NonNull Canvas canvas, int width, int height, @NonNull Paint paint) { if (mUseIntrinsicPadding) { canvas.scale(width / RECT_PADDED_BOUND.width(), height / RECT_PADDED_BOUND.height()); @@ -35,9 +37,10 @@ protected void onDraw(Canvas canvas, int width, int height, Paint paint) { onDrawRing(canvas, paint); } - protected abstract void onDrawRing(Canvas canvas, Paint paint); + protected abstract void onDrawRing(@NonNull Canvas canvas, @NonNull Paint paint); - protected void drawRing(Canvas canvas, Paint paint, float startAngle, float sweepAngle) { + protected void drawRing(@NonNull Canvas canvas, @NonNull Paint paint, float startAngle, + float sweepAngle) { // startAngle starts at 3 o'clock on a watch. canvas.drawArc(RECT_PROGRESS, -90 + startAngle, sweepAngle, false, paint); } diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseSingleHorizontalProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseSingleHorizontalProgressDrawable.java index 77b433f..820da62 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseSingleHorizontalProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/BaseSingleHorizontalProgressDrawable.java @@ -10,23 +10,32 @@ import android.graphics.Paint; import android.graphics.RectF; +import androidx.annotation.Dimension; +import androidx.annotation.NonNull; +import androidx.annotation.Px; + class BaseSingleHorizontalProgressDrawable extends BaseProgressDrawable { + @Dimension(unit = Dimension.DP) private static final int PROGRESS_INTRINSIC_HEIGHT_DP = 4; + @Dimension(unit = Dimension.DP) private static final int PADDED_INTRINSIC_HEIGHT_DP = 16; protected static final RectF RECT_BOUND = new RectF(-180, -1, 180, 1); private static final RectF RECT_PADDED_BOUND = new RectF(-180, -4, 180, 4); - private int mProgressIntrinsicHeight; - private int mPaddedIntrinsicHeight; + @Px + private final int mProgressIntrinsicHeight; + @Px + private final int mPaddedIntrinsicHeight; - public BaseSingleHorizontalProgressDrawable(Context context) { + public BaseSingleHorizontalProgressDrawable(@NonNull Context context) { float density = context.getResources().getDisplayMetrics().density; mProgressIntrinsicHeight = Math.round(PROGRESS_INTRINSIC_HEIGHT_DP * density); mPaddedIntrinsicHeight = Math.round(PADDED_INTRINSIC_HEIGHT_DP * density); } @Override + @Px public int getIntrinsicHeight() { return mUseIntrinsicPadding ? mPaddedIntrinsicHeight : mProgressIntrinsicHeight; } diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/CircularProgressBackgroundDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/CircularProgressBackgroundDrawable.java index 4b6d553..4ce777e 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/CircularProgressBackgroundDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/CircularProgressBackgroundDrawable.java @@ -8,6 +8,8 @@ import android.graphics.Canvas; import android.graphics.Paint; +import androidx.annotation.NonNull; + class CircularProgressBackgroundDrawable extends BaseSingleCircularProgressDrawable implements ShowBackgroundDrawable { @@ -27,14 +29,14 @@ public void setShowBackground(boolean show) { } @Override - public void draw(Canvas canvas) { + public void draw(@NonNull Canvas canvas) { if (mShow) { super.draw(canvas); } } @Override - protected void onDrawRing(Canvas canvas, Paint paint) { + protected void onDrawRing(@NonNull Canvas canvas, @NonNull Paint paint) { drawRing(canvas, paint, 0, 360); } } diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/CircularProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/CircularProgressDrawable.java index f00e97e..590b58b 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/CircularProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/CircularProgressDrawable.java @@ -8,6 +8,8 @@ import android.content.Context; import android.graphics.drawable.Drawable; +import androidx.annotation.NonNull; + /** * A new {@code Drawable} for determinate circular {@code ProgressBar}. */ @@ -19,7 +21,7 @@ public class CircularProgressDrawable extends BaseProgressLayerDrawable< * * @param context the {@code Context} for retrieving style information. */ - public CircularProgressDrawable(int style, Context context) { + public CircularProgressDrawable(int style, @NonNull Context context) { super(new Drawable[] { new CircularProgressBackgroundDrawable(), new SingleCircularProgressDrawable(style), diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/HorizontalProgressBackgroundDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/HorizontalProgressBackgroundDrawable.java index 7c5f37b..eed1a06 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/HorizontalProgressBackgroundDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/HorizontalProgressBackgroundDrawable.java @@ -8,12 +8,14 @@ import android.content.Context; import android.graphics.Canvas; +import androidx.annotation.NonNull; + class HorizontalProgressBackgroundDrawable extends BaseSingleHorizontalProgressDrawable implements ShowBackgroundDrawable { private boolean mShow = true; - public HorizontalProgressBackgroundDrawable(Context context) { + public HorizontalProgressBackgroundDrawable(@NonNull Context context) { super(context); } @@ -31,7 +33,7 @@ public void setShowBackground(boolean show) { } @Override - public void draw(Canvas canvas) { + public void draw(@NonNull Canvas canvas) { if (mShow) { super.draw(canvas); } diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/HorizontalProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/HorizontalProgressDrawable.java index 09ec775..d9925b8 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/HorizontalProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/HorizontalProgressDrawable.java @@ -8,6 +8,8 @@ import android.content.Context; import android.graphics.drawable.Drawable; +import androidx.annotation.NonNull; + /** * A backported {@code Drawable} for determinate horizontal {@code ProgressBar}. */ @@ -19,7 +21,7 @@ public class HorizontalProgressDrawable extends BaseProgressLayerDrawable< * * @param context the {@code Context} for retrieving style information. */ - public HorizontalProgressDrawable(Context context) { + public HorizontalProgressDrawable(@NonNull Context context) { super(new Drawable[] { new HorizontalProgressBackgroundDrawable(context), new SingleHorizontalProgressDrawable(context), diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/IndeterminateCircularProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/IndeterminateCircularProgressDrawable.java index f03eedf..9075af2 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/IndeterminateCircularProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/IndeterminateCircularProgressDrawable.java @@ -11,7 +11,10 @@ import android.graphics.Paint; import android.graphics.RectF; +import androidx.annotation.Dimension; import androidx.annotation.Keep; +import androidx.annotation.NonNull; +import androidx.annotation.Px; /** * A backported {@code Drawable} for indeterminate circular {@code ProgressBar}. @@ -19,24 +22,30 @@ public class IndeterminateCircularProgressDrawable extends BaseIndeterminateProgressDrawable implements MaterialProgressDrawable { + @Dimension(unit = Dimension.DP) private static final int PROGRESS_INTRINSIC_SIZE_DP = 42; + @Dimension(unit = Dimension.DP) private static final int PADDED_INTRINSIC_SIZE_DP = 48; private static final RectF RECT_BOUND = new RectF(-21, -21, 21, 21); private static final RectF RECT_PADDED_BOUND = new RectF(-24, -24, 24, 24); private static final RectF RECT_PROGRESS = new RectF(-19, -19, 19, 19); - private int mProgressIntrinsicSize; - private int mPaddedIntrinsicSize; + @Px + private final int mProgressIntrinsicSize; + @Px + private final int mPaddedIntrinsicSize; - private RingPathTransform mRingPathTransform = new RingPathTransform(); - private RingRotation mRingRotation = new RingRotation(); + @NonNull + private final RingPathTransform mRingPathTransform = new RingPathTransform(); + @NonNull + private final RingRotation mRingRotation = new RingRotation(); /** * Create a new {@code IndeterminateCircularProgressDrawable}. * * @param context the {@code Context} for retrieving style information. */ - public IndeterminateCircularProgressDrawable(Context context) { + public IndeterminateCircularProgressDrawable(@NonNull Context context) { super(context); float density = context.getResources().getDisplayMetrics().density; @@ -49,6 +58,7 @@ public IndeterminateCircularProgressDrawable(Context context) { }; } + @Px private int getIntrinsicSize() { return mUseIntrinsicPadding ? mPaddedIntrinsicSize : mProgressIntrinsicSize; } @@ -57,6 +67,7 @@ private int getIntrinsicSize() { * {@inheritDoc} */ @Override + @Px public int getIntrinsicWidth() { return getIntrinsicSize(); } @@ -65,12 +76,13 @@ public int getIntrinsicWidth() { * {@inheritDoc} */ @Override + @Px public int getIntrinsicHeight() { return getIntrinsicSize(); } @Override - protected void onPreparePaint(Paint paint) { + protected void onPreparePaint(@NonNull Paint paint) { paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(4); paint.setStrokeCap(Paint.Cap.SQUARE); @@ -78,7 +90,7 @@ protected void onPreparePaint(Paint paint) { } @Override - protected void onDraw(Canvas canvas, int width, int height, Paint paint) { + protected void onDraw(@NonNull Canvas canvas, int width, int height, @NonNull Paint paint) { if (mUseIntrinsicPadding) { canvas.scale(width / RECT_PADDED_BOUND.width(), height / RECT_PADDED_BOUND.height()); @@ -91,7 +103,7 @@ protected void onDraw(Canvas canvas, int width, int height, Paint paint) { drawRing(canvas, paint); } - private void drawRing(Canvas canvas, Paint paint) { + private void drawRing(@NonNull Canvas canvas, @NonNull Paint paint) { int saveCount = canvas.save(); canvas.rotate(mRingRotation.mRotation); diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/IndeterminateHorizontalProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/IndeterminateHorizontalProgressDrawable.java index fbe7d6b..8c1d287 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/IndeterminateHorizontalProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/IndeterminateHorizontalProgressDrawable.java @@ -11,7 +11,11 @@ import android.graphics.Paint; import android.graphics.RectF; +import androidx.annotation.Dimension; +import androidx.annotation.FloatRange; import androidx.annotation.Keep; +import androidx.annotation.NonNull; +import androidx.annotation.Px; import me.zhanghai.android.materialprogressbar.internal.ThemeUtils; /** @@ -20,7 +24,9 @@ public class IndeterminateHorizontalProgressDrawable extends BaseIndeterminateProgressDrawable implements MaterialProgressDrawable, ShowBackgroundDrawable { + @Dimension(unit = Dimension.DP) private static final int PROGRESS_INTRINSIC_HEIGHT_DP = 4; + @Dimension(unit = Dimension.DP) private static final int PADDED_INTRINSIC_HEIGHT_DP = 16; private static final RectF RECT_BOUND = new RectF(-180, -1, 180, 1); private static final RectF RECT_PADDED_BOUND = new RectF(-180, -4, 180, 4); @@ -28,20 +34,26 @@ public class IndeterminateHorizontalProgressDrawable extends BaseIndeterminatePr private static final RectTransformX RECT_1_TRANSFORM_X = new RectTransformX(-522.6f, 0.1f); private static final RectTransformX RECT_2_TRANSFORM_X = new RectTransformX(-197.6f, 0.1f); - private int mProgressIntrinsicHeight; - private int mPaddedIntrinsicHeight; - private boolean mShowBackground = true; + @Px + private final int mProgressIntrinsicHeight; + @Px + private final int mPaddedIntrinsicHeight; + @FloatRange(from = 0, to = 1) private float mBackgroundAlpha; - private RectTransformX mRect1TransformX = new RectTransformX(RECT_1_TRANSFORM_X); - private RectTransformX mRect2TransformX = new RectTransformX(RECT_2_TRANSFORM_X); + private boolean mShowBackground = true; + + @NonNull + private final RectTransformX mRect1TransformX = new RectTransformX(RECT_1_TRANSFORM_X); + @NonNull + private final RectTransformX mRect2TransformX = new RectTransformX(RECT_2_TRANSFORM_X); /** * Create a new {@code IndeterminateHorizontalProgressDrawable}. * * @param context the {@code Context} for retrieving style information. */ - public IndeterminateHorizontalProgressDrawable(Context context) { + public IndeterminateHorizontalProgressDrawable(@NonNull Context context) { super(context); float density = context.getResources().getDisplayMetrics().density; @@ -79,6 +91,7 @@ public void setShowBackground(boolean show) { * {@inheritDoc} */ @Override + @Px public int getIntrinsicHeight() { return mUseIntrinsicPadding ? mPaddedIntrinsicHeight : mProgressIntrinsicHeight; } diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/MaterialProgressBar.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/MaterialProgressBar.java index b4e9313..1c573f2 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/MaterialProgressBar.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/MaterialProgressBar.java @@ -17,6 +17,7 @@ import android.util.Log; import android.widget.ProgressBar; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.TintTypedArray; import me.zhanghai.android.materialprogressbar.internal.DrawableCompat; @@ -38,36 +39,38 @@ public class MaterialProgressBar extends ProgressBar { @SuppressWarnings("FieldCanBeLocal") private boolean mSuperInitialized = true; private int mProgressStyle; - private TintInfo mProgressTintInfo = new TintInfo(); + @NonNull + private final TintInfo mProgressTintInfo = new TintInfo(); - public MaterialProgressBar(Context context) { + public MaterialProgressBar(@NonNull Context context) { super(context); init(null, 0, 0); } - public MaterialProgressBar(Context context, AttributeSet attrs) { + public MaterialProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(attrs, 0, 0); } - public MaterialProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { + public MaterialProgressBar(@NonNull Context context, @Nullable AttributeSet attrs, + int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs, defStyleAttr, 0); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) - public MaterialProgressBar(Context context, AttributeSet attrs, int defStyleAttr, - int defStyleRes) { + public MaterialProgressBar(@NonNull Context context, @Nullable AttributeSet attrs, + int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(attrs, defStyleAttr, defStyleRes); } @SuppressWarnings("RestrictedApi") - private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) { + private void init(@Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { Context context = getContext(); TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, @@ -200,6 +203,7 @@ public int getProgressStyle() { * * @return The current drawable. */ + @Nullable public Drawable getCurrentDrawable() { return isIndeterminate() ? getIndeterminateDrawable() : getProgressDrawable(); } @@ -207,16 +211,15 @@ public Drawable getCurrentDrawable() { /** * Get whether the current drawable is using an intrinsic padding. The default is {@code true}. * - * @return Whether the current drawable is using an intrinsic padding. - * @throws IllegalStateException If the current drawable does not implement - * {@link IntrinsicPaddingDrawable}. + * @return Whether the current drawable is using an intrinsic padding, or {@code false} if the + * drawable does not implement {@link IntrinsicPaddingDrawable}. */ public boolean getUseIntrinsicPadding() { Drawable drawable = getCurrentDrawable(); if (drawable instanceof IntrinsicPaddingDrawable) { return ((IntrinsicPaddingDrawable) drawable).getUseIntrinsicPadding(); } else { - throw new IllegalStateException("Drawable does not implement IntrinsicPaddingDrawable"); + return false; } } @@ -224,9 +227,9 @@ public boolean getUseIntrinsicPadding() { * Set whether the current drawable should use an intrinsic padding. The default is * {@code true}. * - * @param useIntrinsicPadding Whether the current drawable should use its intrinsic padding. - * @throws IllegalStateException If the current drawable does not implement - * {@link IntrinsicPaddingDrawable}. + * @param useIntrinsicPadding Whether the current drawable should use its intrinsic padding, or + * ignored if the progress drawable does not implement + * {@link IntrinsicPaddingDrawable}. */ public void setUseIntrinsicPadding(boolean useIntrinsicPadding) { Drawable drawable = getCurrentDrawable(); @@ -244,7 +247,7 @@ public void setUseIntrinsicPadding(boolean useIntrinsicPadding) { * Get whether the current drawable is showing a background. The default is {@code true}. * * @return Whether the current drawable is showing a background, or {@code false} if the - * drawable does not implement {@link ShowBackgroundDrawable}. + * drawable does not implement {@link ShowBackgroundDrawable}. */ public boolean getShowProgressBackground() { Drawable drawable = getCurrentDrawable(); @@ -258,8 +261,8 @@ public boolean getShowProgressBackground() { /** * Set whether the current drawable should show a background. The default is {@code true}. * - * @param show Whether background should be shown. Does nothing if the progress drawable does - * not implement {@link ShowBackgroundDrawable}. + * @param show Whether background should be shown, or ignored if the progress drawable does not + * implement {@link ShowBackgroundDrawable}. */ public void setShowProgressBackground(boolean show) { Drawable drawable = getCurrentDrawable(); @@ -273,20 +276,22 @@ public void setShowProgressBackground(boolean show) { } @Override - public void setProgressDrawable(Drawable d) { - super.setProgressDrawable(d); + public void setProgressDrawable(@Nullable Drawable drawable) { + super.setProgressDrawable(drawable); // mProgressTintInfo can be null during super class initialization. + //noinspection ConstantConditions if (mProgressTintInfo != null) { applyProgressTints(); } } @Override - public void setIndeterminateDrawable(Drawable d) { - super.setIndeterminateDrawable(d); + public void setIndeterminateDrawable(@Nullable Drawable drawable) { + super.setIndeterminateDrawable(drawable); // mProgressTintInfo can be null during super class initialization. + //noinspection ConstantConditions if (mProgressTintInfo != null) { applyIndeterminateTint(); } @@ -653,6 +658,7 @@ private void applyProgressBackgroundTint() { } } + @Nullable private Drawable getTintTargetFromProgressDrawable(int layerId, boolean shouldFallback) { Drawable progressDrawable = getProgressDrawable(); if (progressDrawable == null) { @@ -687,8 +693,8 @@ private void applyIndeterminateTint() { // Progress drawables in this library has already rewritten tint related methods for // compatibility. @SuppressLint("NewApi") - private void applyTintForDrawable(Drawable drawable, ColorStateList tint, - boolean hasTint, PorterDuff.Mode tintMode, + private void applyTintForDrawable(@NonNull Drawable drawable, @Nullable ColorStateList tint, + boolean hasTint, @Nullable PorterDuff.Mode tintMode, boolean hasTintMode) { if (hasTint || hasTintMode) { @@ -732,22 +738,30 @@ private void logDrawableTintWarning() { private static class TintInfo { + @Nullable public ColorStateList mProgressTint; + @Nullable public PorterDuff.Mode mProgressTintMode; public boolean mHasProgressTint; public boolean mHasProgressTintMode; + @Nullable public ColorStateList mSecondaryProgressTint; + @Nullable public PorterDuff.Mode mSecondaryProgressTintMode; public boolean mHasSecondaryProgressTint; public boolean mHasSecondaryProgressTintMode; + @Nullable public ColorStateList mProgressBackgroundTint; + @Nullable public PorterDuff.Mode mProgressBackgroundTintMode; public boolean mHasProgressBackgroundTint; public boolean mHasProgressBackgroundTintMode; + @Nullable public ColorStateList mIndeterminateTint; + @Nullable public PorterDuff.Mode mIndeterminateTintMode; public boolean mHasIndeterminateTint; public boolean mHasIndeterminateTintMode; diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/SingleCircularProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/SingleCircularProgressDrawable.java index c029dad..8aa9993 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/SingleCircularProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/SingleCircularProgressDrawable.java @@ -9,6 +9,9 @@ import android.graphics.Paint; import android.graphics.drawable.Drawable; +import androidx.annotation.IntRange; +import androidx.annotation.NonNull; + class SingleCircularProgressDrawable extends BaseSingleCircularProgressDrawable implements ShowBackgroundDrawable { @@ -39,7 +42,7 @@ class SingleCircularProgressDrawable extends BaseSingleCircularProgressDrawable } @Override - protected boolean onLevelChange(int level) { + protected boolean onLevelChange(@IntRange(from = 0, to = LEVEL_MAX) int level) { invalidateSelf(); return true; } @@ -58,7 +61,7 @@ public void setShowBackground(boolean show) { } @Override - protected void onDrawRing(Canvas canvas, Paint paint) { + protected void onDrawRing(@NonNull Canvas canvas, @NonNull Paint paint) { int level = getLevel(); if (level == 0) { diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/SingleHorizontalProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/SingleHorizontalProgressDrawable.java index 57e0158..9cded1d 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/SingleHorizontalProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/SingleHorizontalProgressDrawable.java @@ -10,6 +10,9 @@ import android.graphics.Paint; import android.graphics.drawable.Drawable; +import androidx.annotation.IntRange; +import androidx.annotation.NonNull; + class SingleHorizontalProgressDrawable extends BaseSingleHorizontalProgressDrawable implements ShowBackgroundDrawable { @@ -20,12 +23,12 @@ class SingleHorizontalProgressDrawable extends BaseSingleHorizontalProgressDrawa private boolean mShowBackground; - public SingleHorizontalProgressDrawable(Context context) { + public SingleHorizontalProgressDrawable(@NonNull Context context) { super(context); } @Override - protected boolean onLevelChange(int level) { + protected boolean onLevelChange(@IntRange(from = 0, to = LEVEL_MAX) int level) { invalidateSelf(); return true; } @@ -44,7 +47,7 @@ public void setShowBackground(boolean show) { } @Override - protected void onDrawRect(Canvas canvas, Paint paint) { + protected void onDrawRect(@NonNull Canvas canvas, @NonNull Paint paint) { int level = getLevel(); if (level == 0) { diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/StaticIndeterminateCircularProgressDrawable.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/StaticIndeterminateCircularProgressDrawable.java index dea5267..d4a0ea6 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/StaticIndeterminateCircularProgressDrawable.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/StaticIndeterminateCircularProgressDrawable.java @@ -8,7 +8,9 @@ import android.graphics.Path; import android.graphics.RectF; +import androidx.annotation.Dimension; import androidx.annotation.NonNull; +import androidx.annotation.Px; import me.zhanghai.android.materialprogressbar.internal.ThemeUtils; public class StaticIndeterminateCircularProgressDrawable extends BaseProgressDrawable { @@ -64,13 +66,17 @@ public class StaticIndeterminateCircularProgressDrawable extends BaseProgressDra PATH_PROGRESS.close(); } + @Dimension(unit = Dimension.DP) private static final int PROGRESS_INTRINSIC_SIZE_DP = 42; + @Dimension(unit = Dimension.DP) private static final int PADDED_INTRINSIC_SIZE_DP = 48; // To keep the same ratio as indeterminate circular progress drawable. private static final RectF RECT_PROGRESS_BOUND = new RectF(3, 3, 21, 21); private static final RectF RECT_PADDED_BOUND = new RectF(0, 0, 24, 24); + @Px private final int mProgressIntrinsicSize; + @Px private final int mPaddedIntrinsicSize; @NonNull @@ -95,6 +101,7 @@ public StaticIndeterminateCircularProgressDrawable(@NonNull Context context) { setTint(controlActivatedColor); } + @Px private int getIntrinsicSize() { return mUseIntrinsicPadding ? mPaddedIntrinsicSize : mProgressIntrinsicSize; } @@ -103,6 +110,7 @@ private int getIntrinsicSize() { * {@inheritDoc} */ @Override + @Px public int getIntrinsicWidth() { return getIntrinsicSize(); } @@ -111,17 +119,18 @@ public int getIntrinsicWidth() { * {@inheritDoc} */ @Override + @Px public int getIntrinsicHeight() { return getIntrinsicSize(); } @Override - protected void onPreparePaint(Paint paint) { + protected void onPreparePaint(@NonNull Paint paint) { paint.setStyle(Paint.Style.FILL); } @Override - protected void onDraw(Canvas canvas, int width, int height, Paint paint) { + protected void onDraw(@NonNull Canvas canvas, int width, int height, @NonNull Paint paint) { // Drawing the transformed path makes rendering much less blurry than using canvas transform // directly. See https://stackoverflow.com/a/16091390 . RectF bound = mUseIntrinsicPadding ? RECT_PADDED_BOUND : RECT_PROGRESS_BOUND; diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/DrawableCompat.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/DrawableCompat.java index 177ae0a..c1f871e 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/DrawableCompat.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/DrawableCompat.java @@ -7,12 +7,15 @@ import android.graphics.PorterDuff; +import androidx.annotation.Nullable; + public class DrawableCompat { /** * Parses a {@link android.graphics.PorterDuff.Mode} from a tintMode attribute's enum value. */ - public static PorterDuff.Mode parseTintMode(int value, PorterDuff.Mode defaultMode) { + public static @Nullable PorterDuff.Mode parseTintMode(int value, + @Nullable PorterDuff.Mode defaultMode) { switch (value) { case 3: return PorterDuff.Mode.SRC_OVER; case 5: return PorterDuff.Mode.SRC_IN; diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompat.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompat.java index dfd5f8f..d8dbdae 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompat.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompat.java @@ -10,6 +10,9 @@ import android.os.Build; import android.util.Property; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + /** * Helper for accessing features in {@link ObjectAnimator} introduced after API level 11 (for * {@link android.animation.PropertyValuesHolder}) in a backward compatible fashion. @@ -32,11 +35,14 @@ private ObjectAnimatorCompat() {} * @param values A set of values that the animation will animate between over time. * @return An ObjectAnimator object that is set up to animate between the given values. */ - public static ObjectAnimator ofArgb(Object target, String propertyName, int... values) { + @NonNull + public static ObjectAnimator ofArgb(@Nullable Object target, @NonNull String propertyName, + int... values) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return ObjectAnimatorCompatLollipop.ofArgb(target, propertyName, values); + } else { + return ObjectAnimatorCompatBase.ofArgb(target, propertyName, values); } - return ObjectAnimatorCompatBase.ofArgb(target, propertyName, values); } /** @@ -51,12 +57,14 @@ public static ObjectAnimator ofArgb(Object target, String propertyName, int... v * @param values A set of values that the animation will animate between over time. * @return An ObjectAnimator object that is set up to animate between the given values. */ - public static ObjectAnimator ofArgb(T target, Property property, - int... values) { + @NonNull + public static ObjectAnimator ofArgb(@Nullable T target, + @NonNull Property property, int... values) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return ObjectAnimatorCompatLollipop.ofArgb(target, property, values); + } else { + return ObjectAnimatorCompatBase.ofArgb(target, property, values); } - return ObjectAnimatorCompatBase.ofArgb(target, property, values); } /** @@ -75,12 +83,14 @@ public static ObjectAnimator ofArgb(T target, Property property, * @param path The {@code Path} to animate values along. * @return An ObjectAnimator object that is set up to animate along {@code path}. */ - public static ObjectAnimator ofFloat(Object target, String xPropertyName, String yPropertyName, - Path path) { + @NonNull + public static ObjectAnimator ofFloat(@Nullable Object target, @NonNull String xPropertyName, + @NonNull String yPropertyName, @NonNull Path path) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return ObjectAnimatorCompatLollipop.ofFloat(target, xPropertyName, yPropertyName, path); + } else { + return ObjectAnimatorCompatBase.ofFloat(target, xPropertyName, yPropertyName, path); } - return ObjectAnimatorCompatBase.ofFloat(target, xPropertyName, yPropertyName, path); } /** @@ -95,12 +105,16 @@ public static ObjectAnimator ofFloat(Object target, String xPropertyName, String * @param path The {@code Path} to animate values along. * @return An ObjectAnimator object that is set up to animate along {@code path}. */ - public static ObjectAnimator ofFloat(T target, Property xProperty, - Property yProperty, Path path) { + @NonNull + public static ObjectAnimator ofFloat(@Nullable T target, + @NonNull Property xProperty, + @NonNull Property yProperty, + @NonNull Path path) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return ObjectAnimatorCompatLollipop.ofFloat(target, xProperty, yProperty, path); + } else { + return ObjectAnimatorCompatBase.ofFloat(target, xProperty, yProperty, path); } - return ObjectAnimatorCompatBase.ofFloat(target, xProperty, yProperty, path); } /** @@ -119,12 +133,14 @@ public static ObjectAnimator ofFloat(T target, Property xProperty, * @param path The {@code Path} to animate values along. * @return An ObjectAnimator object that is set up to animate along {@code path}. */ - public static ObjectAnimator ofInt(Object target, String xPropertyName, String yPropertyName, - Path path) { + @NonNull + public static ObjectAnimator ofInt(@Nullable Object target, @NonNull String xPropertyName, + @NonNull String yPropertyName, @NonNull Path path) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return ObjectAnimatorCompatLollipop.ofInt(target, xPropertyName, yPropertyName, path); + } else { + return ObjectAnimatorCompatBase.ofInt(target, xPropertyName, yPropertyName, path); } - return ObjectAnimatorCompatBase.ofInt(target, xPropertyName, yPropertyName, path); } /** @@ -139,11 +155,15 @@ public static ObjectAnimator ofInt(Object target, String xPropertyName, String y * @param path The {@code Path} to animate values along. * @return An ObjectAnimator object that is set up to animate along {@code path}. */ - public static ObjectAnimator ofInt(T target, Property xProperty, - Property yProperty, Path path) { + @NonNull + public static ObjectAnimator ofInt(@Nullable T target, + @NonNull Property xProperty, + @NonNull Property yProperty, + @NonNull Path path) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return ObjectAnimatorCompatLollipop.ofInt(target, xProperty, yProperty, path); + } else { + return ObjectAnimatorCompatBase.ofInt(target, xProperty, yProperty, path); } - return ObjectAnimatorCompatBase.ofInt(target, xProperty, yProperty, path); } } diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompatBase.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompatBase.java index 2166b00..a7652ba 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompatBase.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompatBase.java @@ -12,6 +12,8 @@ import android.graphics.PathMeasure; import android.util.Property; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.annotation.Size; class ObjectAnimatorCompatBase { @@ -21,21 +23,25 @@ class ObjectAnimatorCompatBase { private ObjectAnimatorCompatBase() {} - public static ObjectAnimator ofArgb(Object target, String propertyName, int... values) { + @NonNull + public static ObjectAnimator ofArgb(@Nullable Object target, @NonNull String propertyName, + int... values) { ObjectAnimator animator = ObjectAnimator.ofInt(target, propertyName, values); animator.setEvaluator(new ArgbEvaluator()); return animator; } - public static ObjectAnimator ofArgb(T target, Property property, - int... values) { + @NonNull + public static ObjectAnimator ofArgb(@Nullable T target, + @NonNull Property property, int... values) { ObjectAnimator animator = ObjectAnimator.ofInt(target, property, values); animator.setEvaluator(new ArgbEvaluator()); return animator; } - public static ObjectAnimator ofFloat(Object target, String xPropertyName, String yPropertyName, - Path path) { + @NonNull + public static ObjectAnimator ofFloat(@Nullable Object target, @NonNull String xPropertyName, + @NonNull String yPropertyName, @NonNull Path path) { float[] xValues = new float[NUM_POINTS]; float[] yValues = new float[NUM_POINTS]; @@ -47,8 +53,11 @@ public static ObjectAnimator ofFloat(Object target, String xPropertyName, String return ObjectAnimator.ofPropertyValuesHolder(target, xPvh, yPvh); } - public static ObjectAnimator ofFloat(T target, Property xProperty, - Property yProperty, Path path) { + @NonNull + public static ObjectAnimator ofFloat(@Nullable T target, + @NonNull Property xProperty, + @NonNull Property yProperty, + @NonNull Path path) { float[] xValues = new float[NUM_POINTS]; float[] yValues = new float[NUM_POINTS]; @@ -60,8 +69,9 @@ public static ObjectAnimator ofFloat(T target, Property xProperty, return ObjectAnimator.ofPropertyValuesHolder(target, xPvh, yPvh); } - public static ObjectAnimator ofInt(Object target, String xPropertyName, String yPropertyName, - Path path) { + @NonNull + public static ObjectAnimator ofInt(@Nullable Object target, @NonNull String xPropertyName, + @NonNull String yPropertyName, @NonNull Path path) { int[] xValues = new int[NUM_POINTS]; int[] yValues = new int[NUM_POINTS]; @@ -73,8 +83,11 @@ public static ObjectAnimator ofInt(Object target, String xPropertyName, String y return ObjectAnimator.ofPropertyValuesHolder(target, xPvh, yPvh); } - public static ObjectAnimator ofInt(T target, Property xProperty, - Property yProperty, Path path) { + @NonNull + public static ObjectAnimator ofInt(@Nullable T target, + @NonNull Property xProperty, + @NonNull Property yProperty, + @NonNull Path path) { int[] xValues = new int[NUM_POINTS]; int[] yValues = new int[NUM_POINTS]; @@ -86,8 +99,9 @@ public static ObjectAnimator ofInt(T target, Property xProperty, return ObjectAnimator.ofPropertyValuesHolder(target, xPvh, yPvh); } - private static void calculateXYValues(Path path, @Size(NUM_POINTS) float[] xValues, - @Size(NUM_POINTS) float[] yValues) { + private static void calculateXYValues(@NonNull Path path, + @NonNull @Size(NUM_POINTS) float[] xValues, + @NonNull @Size(NUM_POINTS) float[] yValues) { PathMeasure pathMeasure = new PathMeasure(path, false /* forceClosed */); float pathLength = pathMeasure.getLength(); @@ -101,8 +115,9 @@ private static void calculateXYValues(Path path, @Size(NUM_POINTS) float[] xValu } } - private static void calculateXYValues(Path path, @Size(NUM_POINTS) int[] xValues, - @Size(NUM_POINTS) int[] yValues) { + private static void calculateXYValues(@NonNull Path path, + @NonNull @Size(NUM_POINTS) int[] xValues, + @NonNull @Size(NUM_POINTS) int[] yValues) { PathMeasure pathMeasure = new PathMeasure(path, false /* forceClosed */); float pathLength = pathMeasure.getLength(); diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompatLollipop.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompatLollipop.java index e47f0f2..6a70b84 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompatLollipop.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ObjectAnimatorCompatLollipop.java @@ -11,37 +11,51 @@ import android.os.Build; import android.util.Property; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + @TargetApi(Build.VERSION_CODES.LOLLIPOP) class ObjectAnimatorCompatLollipop { private ObjectAnimatorCompatLollipop() {} - public static ObjectAnimator ofArgb(Object target, String propertyName, int... values) { + @NonNull + public static ObjectAnimator ofArgb(@Nullable Object target, @NonNull String propertyName, + int... values) { return ObjectAnimator.ofArgb(target, propertyName, values); } - public static ObjectAnimator ofArgb(T target, Property property, - int... values) { + @NonNull + public static ObjectAnimator ofArgb(@Nullable T target, + @NonNull Property property, int... values) { return ObjectAnimator.ofArgb(target, property, values); } - public static ObjectAnimator ofFloat(Object target, String xPropertyName, String yPropertyName, - Path path) { + @NonNull + public static ObjectAnimator ofFloat(@Nullable Object target, @NonNull String xPropertyName, + @NonNull String yPropertyName, @NonNull Path path) { return ObjectAnimator.ofFloat(target, xPropertyName, yPropertyName, path); } - public static ObjectAnimator ofFloat(T target, Property xProperty, - Property yProperty, Path path) { + @NonNull + public static ObjectAnimator ofFloat(@Nullable T target, + @NonNull Property xProperty, + @NonNull Property yProperty, + @NonNull Path path) { return ObjectAnimator.ofFloat(target, xProperty, yProperty, path); } - public static ObjectAnimator ofInt(Object target, String xPropertyName, String yPropertyName, - Path path) { + @NonNull + public static ObjectAnimator ofInt(@Nullable Object target, @NonNull String xPropertyName, + @NonNull String yPropertyName, @NonNull Path path) { return ObjectAnimator.ofInt(target, xPropertyName, yPropertyName, path); } - public static ObjectAnimator ofInt(T target, Property xProperty, - Property yProperty, Path path) { + @NonNull + public static ObjectAnimator ofInt(@Nullable T target, + @NonNull Property xProperty, + @NonNull Property yProperty, + @NonNull Path path) { return ObjectAnimator.ofInt(target, xProperty, yProperty, path); } } diff --git a/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ThemeUtils.java b/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ThemeUtils.java index 69ac36d..22d2a60 100644 --- a/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ThemeUtils.java +++ b/library/src/main/java/me/zhanghai/android/materialprogressbar/internal/ThemeUtils.java @@ -8,11 +8,17 @@ import android.content.Context; import android.content.res.TypedArray; +import androidx.annotation.AttrRes; +import androidx.annotation.ColorInt; +import androidx.annotation.NonNull; + public class ThemeUtils { private ThemeUtils() {} - public static int getColorFromAttrRes(int attrRes, int defaultValue, Context context) { + @ColorInt + public static int getColorFromAttrRes(@AttrRes int attrRes, @ColorInt int defaultValue, + @NonNull Context context) { TypedArray a = context.obtainStyledAttributes(new int[] { attrRes }); try { return a.getColor(0, defaultValue); @@ -21,7 +27,8 @@ public static int getColorFromAttrRes(int attrRes, int defaultValue, Context con } } - public static float getFloatFromAttrRes(int attrRes, float defaultValue, Context context) { + public static float getFloatFromAttrRes(@AttrRes int attrRes, float defaultValue, + @NonNull Context context) { TypedArray a = context.obtainStyledAttributes(new int[] { attrRes }); try { return a.getFloat(0, defaultValue);