diff options
author | Ralston Da Silva <ralu@google.com> | 2019-03-18 15:48:53 -0700 |
---|---|---|
committer | Ralston Da Silva <ralu@google.com> | 2019-07-12 10:33:51 -0700 |
commit | a5ced893c6f9dc04d68114c4ff4fb58ac59a09c2 (patch) | |
tree | c020c075ba867aef60ad4102ee48f33b158ab2d9 | |
parent | c9d7d440660b4bd6659d3db9a9c166b7a4dbd2bb (diff) | |
download | support-a5ced893c6f9dc04d68114c4ff4fb58ac59a09c2.tar.gz |
SwipeRefreshLayout - Add attributes for colors, so that they can be added to the material styles
Bug: 120855684
Test: Ran androidx.swiperefreshlayout.widget.SwipeRefreshLayoutTest
Change-Id: I9cdbb1dd91664beffd47286d8576e8c3960d286a
7 files changed, 126 insertions, 34 deletions
diff --git a/jetifier/jetifier/migration.config b/jetifier/jetifier/migration.config index 98984f78ccd..6b276ed7ccc 100644 --- a/jetifier/jetifier/migration.config +++ b/jetifier/jetifier/migration.config @@ -614,6 +614,10 @@ "to": "ignore" }, { + "from": "androidx/swiperefreshlayout/R(.*)", + "to": "ignore" + }, + { "from": "androidx/swiperefreshlayout/widget/CircleImageView(.*)", "to": "ignore" }, diff --git a/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutTest.java b/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutTest.java index de81eb07e0c..caf7544790a 100644 --- a/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutTest.java +++ b/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutTest.java @@ -29,6 +29,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import android.view.ContextThemeWrapper; import android.view.View; import androidx.swiperefreshlayout.test.R; @@ -100,6 +101,28 @@ public class SwipeRefreshLayoutTest { } @Test + @SmallTest + public void testStyles_setBackgroundColor() { + mSwipeRefresh.setProgressBackgroundColorSchemeColor(0xFFFBFBFB); + assertEquals(mSwipeRefresh.mCircleView.getBackgroundColor(), 0xFFFBFBFB); + } + + @Test + @SmallTest + public void testStyles_defaultBackgroundColor() { + assertEquals(mSwipeRefresh.mCircleView.getBackgroundColor(), 0xFFFAFAFA); + } + + @Test + @SmallTest + public void testStyles_backgroundColor() { + ContextThemeWrapper newContext = new ContextThemeWrapper( + mSwipeRefresh.getContext(), R.style.TestActivityThemeWithBackground); + SwipeRefreshLayout swipeRefreshLayout = new SwipeRefreshLayout(newContext); + assertEquals(swipeRefreshLayout.mCircleView.getBackgroundColor(), 0xFF808080); + } + + @Test @LargeTest public void testSwipeDownToRefresh() throws Throwable { assertFalse(mSwipeRefresh.isRefreshing()); @@ -114,13 +137,16 @@ public class SwipeRefreshLayoutTest { float density = mSwipeRefresh.getResources().getDisplayMetrics().density; assertEquals((int) (SwipeRefreshLayout.CIRCLE_DIAMETER * density), mSwipeRefresh.getProgressCircleDiameter()); - onView(withId(R.id.swipe_refresh)).perform(SwipeRefreshLayoutActions.setSize(SwipeRefreshLayout.LARGE)); + onView(withId(R.id.swipe_refresh)).perform( + SwipeRefreshLayoutActions.setSize(SwipeRefreshLayout.LARGE)); assertEquals((int) (SwipeRefreshLayout.CIRCLE_DIAMETER_LARGE * density), mSwipeRefresh.getProgressCircleDiameter()); - onView(withId(R.id.swipe_refresh)).perform(SwipeRefreshLayoutActions.setSize(SwipeRefreshLayout.DEFAULT)); + onView(withId(R.id.swipe_refresh)).perform( + SwipeRefreshLayoutActions.setSize(SwipeRefreshLayout.DEFAULT)); assertEquals((int) (SwipeRefreshLayout.CIRCLE_DIAMETER * density), mSwipeRefresh.getProgressCircleDiameter()); - onView(withId(R.id.swipe_refresh)).perform(SwipeRefreshLayoutActions.setSize(SwipeRefreshLayout.DEFAULT)); + onView(withId(R.id.swipe_refresh)).perform( + SwipeRefreshLayoutActions.setSize(SwipeRefreshLayout.DEFAULT)); onView(withId(R.id.swipe_refresh)).perform(SwipeRefreshLayoutActions.setSize(INVALID_SIZE)); assertEquals((int) (SwipeRefreshLayout.CIRCLE_DIAMETER * density), mSwipeRefresh.getProgressCircleDiameter()); diff --git a/swiperefreshlayout/src/androidTest/res/values/styles.xml b/swiperefreshlayout/src/androidTest/res/values/styles.xml index 0e0cb3d901c..da27ddfe621 100644 --- a/swiperefreshlayout/src/androidTest/res/values/styles.xml +++ b/swiperefreshlayout/src/androidTest/res/values/styles.xml @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> -<!-- Copyright (C) 2015 The Android Open Source Project +<!-- Copyright (C) 2019 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -17,4 +17,8 @@ <style name="TestActivityTheme" parent="Theme.AppCompat.Light"> <item name="android:windowAnimationStyle">@null</item> </style> + + <style name="TestActivityThemeWithBackground" parent="TestActivityTheme"> + <item name="swipeRefreshLayoutProgressSpinnerBackgroundColor">#FF808080</item> + </style> </resources>
\ No newline at end of file diff --git a/swiperefreshlayout/src/main/java/androidx/swiperefreshlayout/widget/CircleImageView.java b/swiperefreshlayout/src/main/java/androidx/swiperefreshlayout/widget/CircleImageView.java index 9addccd9fb4..de9f4042681 100644 --- a/swiperefreshlayout/src/main/java/androidx/swiperefreshlayout/widget/CircleImageView.java +++ b/swiperefreshlayout/src/main/java/androidx/swiperefreshlayout/widget/CircleImageView.java @@ -16,7 +16,9 @@ package androidx.swiperefreshlayout.widget; +import android.annotation.SuppressLint; import android.content.Context; +import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; @@ -28,8 +30,8 @@ import android.view.View; import android.view.animation.Animation; import android.widget.ImageView; -import androidx.core.content.ContextCompat; import androidx.core.view.ViewCompat; +import androidx.swiperefreshlayout.R; /** * Private class created to work around issues with AnimationListeners being @@ -38,8 +40,10 @@ import androidx.core.view.ViewCompat; */ class CircleImageView extends ImageView { - private static final int KEY_SHADOW_COLOR = 0x1E000000; + private static final int DEFAULT_BACKGROUND_COLOR = 0xFFFAFAFA; private static final int FILL_SHADOW_COLOR = 0x3D000000; + private static final int KEY_SHADOW_COLOR = 0x1E000000; + // PX private static final float X_OFFSET = 0f; private static final float Y_OFFSET = 1.75f; @@ -47,23 +51,33 @@ class CircleImageView extends ImageView { private static final int SHADOW_ELEVATION = 4; private Animation.AnimationListener mListener; - int mShadowRadius; + private int mShadowRadius; + private int mBackgroundColor; - CircleImageView(Context context, int color) { + CircleImageView(Context context) { super(context); + final float density = getContext().getResources().getDisplayMetrics().density; final int shadowYOffset = (int) (density * Y_OFFSET); final int shadowXOffset = (int) (density * X_OFFSET); mShadowRadius = (int) (density * SHADOW_RADIUS); + // The style attribute is named SwipeRefreshLayout instead of CircleImageView because + // CircleImageView is not part of the public api. + @SuppressLint("CustomViewStyleable") + TypedArray colorArray = getContext().obtainStyledAttributes(R.styleable.SwipeRefreshLayout); + mBackgroundColor = colorArray.getColor( + R.styleable.SwipeRefreshLayout_swipeRefreshLayoutProgressSpinnerBackgroundColor, + DEFAULT_BACKGROUND_COLOR); + colorArray.recycle(); + ShapeDrawable circle; if (elevationSupported()) { circle = new ShapeDrawable(new OvalShape()); ViewCompat.setElevation(this, SHADOW_ELEVATION * density); } else { - OvalShape oval = new OvalShadow(mShadowRadius); - circle = new ShapeDrawable(oval); + circle = new ShapeDrawable(new OvalShadow(this, mShadowRadius)); setLayerType(View.LAYER_TYPE_SOFTWARE, circle.getPaint()); circle.getPaint().setShadowLayer(mShadowRadius, shadowXOffset, shadowYOffset, KEY_SHADOW_COLOR); @@ -71,7 +85,7 @@ class CircleImageView extends ImageView { // set padding so the inner image sits correctly within the shadow. setPadding(padding, padding, padding, padding); } - circle.getPaint().setColor(color); + circle.getPaint().setColor(mBackgroundColor); ViewCompat.setBackground(this, circle); } @@ -108,28 +122,26 @@ class CircleImageView extends ImageView { } } - /** - * Update the background color of the circle image view. - * - * @param colorRes Id of a color resource. - */ - public void setBackgroundColorRes(int colorRes) { - setBackgroundColor(ContextCompat.getColor(getContext(), colorRes)); - } - @Override public void setBackgroundColor(int color) { if (getBackground() instanceof ShapeDrawable) { ((ShapeDrawable) getBackground()).getPaint().setColor(color); + mBackgroundColor = color; } } - private class OvalShadow extends OvalShape { - private RadialGradient mRadialGradient; + public int getBackgroundColor() { + return mBackgroundColor; + } + + private static class OvalShadow extends OvalShape { private Paint mShadowPaint; + private int mShadowRadius; + private CircleImageView mCircleImageView; - OvalShadow(int shadowRadius) { + OvalShadow(CircleImageView circleImageView, int shadowRadius) { super(); + mCircleImageView = circleImageView; mShadowPaint = new Paint(); mShadowRadius = shadowRadius; updateRadialGradient((int) rect().width()); @@ -143,17 +155,20 @@ class CircleImageView extends ImageView { @Override public void draw(Canvas canvas, Paint paint) { - final int viewWidth = CircleImageView.this.getWidth(); - final int viewHeight = CircleImageView.this.getHeight(); - canvas.drawCircle(viewWidth / 2, viewHeight / 2, viewWidth / 2, mShadowPaint); - canvas.drawCircle(viewWidth / 2, viewHeight / 2, viewWidth / 2 - mShadowRadius, paint); + final int x = mCircleImageView.getWidth() / 2; + final int y = mCircleImageView.getHeight() / 2; + canvas.drawCircle(x, y, x, mShadowPaint); + canvas.drawCircle(x, y, x - mShadowRadius, paint); } private void updateRadialGradient(int diameter) { - mRadialGradient = new RadialGradient(diameter / 2, diameter / 2, - mShadowRadius, new int[] { FILL_SHADOW_COLOR, Color.TRANSPARENT }, - null, Shader.TileMode.CLAMP); - mShadowPaint.setShader(mRadialGradient); + mShadowPaint.setShader(new RadialGradient( + diameter / 2, + diameter / 2, + mShadowRadius, + new int[]{FILL_SHADOW_COLOR, Color.TRANSPARENT}, + null, + Shader.TileMode.CLAMP)); } } } diff --git a/swiperefreshlayout/src/main/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayout.java b/swiperefreshlayout/src/main/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayout.java index b104d3fe068..2d04256786a 100644 --- a/swiperefreshlayout/src/main/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayout.java +++ b/swiperefreshlayout/src/main/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayout.java @@ -109,8 +109,6 @@ public class SwipeRefreshLayout extends ViewGroup implements NestedScrollingPare private static final int ANIMATE_TO_START_DURATION = 200; - // Default background for the progress spinner - private static final int CIRCLE_BG_LIGHT = 0xFFFAFAFA; // Default offset in dips from the top of the view to where the progress spinner should stop private static final int DEFAULT_CIRCLE_TARGET = 64; @@ -456,7 +454,7 @@ public class SwipeRefreshLayout extends ViewGroup implements NestedScrollingPare } private void createProgressView() { - mCircleView = new CircleImageView(getContext(), CIRCLE_BG_LIGHT); + mCircleView = new CircleImageView(getContext()); mProgress = new CircularProgressDrawable(getContext()); mProgress.setStyle(CircularProgressDrawable.DEFAULT); mCircleView.setImageDrawable(mProgress); diff --git a/swiperefreshlayout/src/main/res-public/values/attrs.xml b/swiperefreshlayout/src/main/res-public/values/attrs.xml new file mode 100644 index 00000000000..1824c9b3847 --- /dev/null +++ b/swiperefreshlayout/src/main/res-public/values/attrs.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2019 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<!-- Definitions of attributes to be exposed as public --> +<resources> + <public type="attr" name="swipeRefreshLayoutProgressSpinnerBackgroundColor" format="color"/> +</resources>
\ No newline at end of file diff --git a/swiperefreshlayout/src/main/res/values/attrs.xml b/swiperefreshlayout/src/main/res/values/attrs.xml new file mode 100644 index 00000000000..d628dd3cfc5 --- /dev/null +++ b/swiperefreshlayout/src/main/res/values/attrs.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright 2019 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> + +<resources> + + <declare-styleable name="SwipeRefreshLayout"> + <!-- Background color for SwipeRefreshLayout progress spinner. --> + <attr name="swipeRefreshLayoutProgressSpinnerBackgroundColor" format="color" /> + </declare-styleable> + +</resources>
\ No newline at end of file |