Skip to content

Commit cd17642

Browse files
committed
Fallback logic for button shape update logic for lollipop
Fallback logic for button shape update logic for lollipop
1 parent 0e5c393 commit cd17642

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

lib/java/com/google/android/material/button/MaterialButtonHelper.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
package com.google.android.material.button;
1818

19-
import com.google.android.material.R;
20-
21-
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
22-
2319
import android.content.Context;
2420
import android.content.res.ColorStateList;
2521
import android.content.res.TypedArray;
@@ -31,12 +27,15 @@
3127
import android.graphics.drawable.RippleDrawable;
3228
import android.os.Build.VERSION;
3329
import android.os.Build.VERSION_CODES;
34-
import androidx.core.graphics.drawable.DrawableCompat;
35-
import androidx.core.view.ViewCompat;
30+
3631
import androidx.annotation.Dimension;
3732
import androidx.annotation.NonNull;
3833
import androidx.annotation.Nullable;
3934
import androidx.annotation.RestrictTo;
35+
import androidx.core.graphics.drawable.DrawableCompat;
36+
import androidx.core.view.ViewCompat;
37+
38+
import com.google.android.material.R;
4039
import com.google.android.material.color.MaterialColors;
4140
import com.google.android.material.internal.ViewUtils;
4241
import com.google.android.material.resources.MaterialResources;
@@ -46,11 +45,15 @@
4645
import com.google.android.material.shape.ShapeAppearanceModel;
4746
import com.google.android.material.shape.Shapeable;
4847

48+
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
49+
4950
/** @hide */
5051
@RestrictTo(LIBRARY_GROUP)
5152
class MaterialButtonHelper {
5253

53-
private static final boolean IS_LOLLIPOP = VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP;
54+
private static final boolean IS_MIN_LOLLIPOP = VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP;
55+
private static final boolean IS_LOLLIPOP = VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP &&
56+
VERSION.SDK_INT <= VERSION_CODES.LOLLIPOP_MR1;
5457
private final MaterialButton materialButton;
5558
@NonNull private ShapeAppearanceModel shapeAppearanceModel;
5659

@@ -218,7 +221,7 @@ private Drawable createBackground() {
218221
? MaterialColors.getColor(materialButton, R.attr.colorSurface)
219222
: Color.TRANSPARENT);
220223

221-
if (IS_LOLLIPOP) {
224+
if (IS_MIN_LOLLIPOP) {
222225
maskDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
223226
DrawableCompat.setTint(maskDrawable, Color.WHITE);
224227
rippleDrawable =
@@ -255,10 +258,10 @@ void setBackgroundColor(int color) {
255258
void setRippleColor(@Nullable ColorStateList rippleColor) {
256259
if (this.rippleColor != rippleColor) {
257260
this.rippleColor = rippleColor;
258-
if (IS_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawable) {
261+
if (IS_MIN_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawable) {
259262
((RippleDrawable) materialButton.getBackground())
260263
.setColor(RippleUtils.sanitizeRippleDrawableColor(rippleColor));
261-
} else if (!IS_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawableCompat) {
264+
} else if (!IS_MIN_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawableCompat) {
262265
((RippleDrawableCompat) materialButton.getBackground()).setTintList(
263266
RippleUtils.sanitizeRippleDrawableColor(rippleColor));
264267
}
@@ -326,7 +329,7 @@ int getCornerRadius() {
326329
@Nullable
327330
private MaterialShapeDrawable getMaterialShapeDrawable(boolean getSurfaceColorStrokeDrawable) {
328331
if (rippleDrawable != null && rippleDrawable.getNumberOfLayers() > 0) {
329-
if (IS_LOLLIPOP) {
332+
if (IS_MIN_LOLLIPOP) {
330333
InsetDrawable insetDrawable = (InsetDrawable) rippleDrawable.getDrawable(0);
331334
LayerDrawable layerDrawable = (LayerDrawable) insetDrawable.getDrawable();
332335
return (MaterialShapeDrawable)
@@ -359,14 +362,20 @@ private MaterialShapeDrawable getSurfaceColorStrokeDrawable() {
359362
}
360363

361364
private void updateButtonShape(@NonNull ShapeAppearanceModel shapeAppearanceModel) {
362-
if (getMaterialShapeDrawable() != null) {
363-
getMaterialShapeDrawable().setShapeAppearanceModel(shapeAppearanceModel);
364-
}
365-
if (getSurfaceColorStrokeDrawable() != null) {
366-
getSurfaceColorStrokeDrawable().setShapeAppearanceModel(shapeAppearanceModel);
367-
}
368-
if (getMaskDrawable() != null) {
369-
getMaskDrawable().setShapeAppearanceModel(shapeAppearanceModel);
365+
if (IS_LOLLIPOP) {
366+
// There seems to be a bug to drawables that is affecting Lollipop, since invalidation is not
367+
// changing an existing drawable shape. This is a fallback
368+
updateBackground();
369+
} else {
370+
if (getMaterialShapeDrawable() != null) {
371+
getMaterialShapeDrawable().setShapeAppearanceModel(shapeAppearanceModel);
372+
}
373+
if (getSurfaceColorStrokeDrawable() != null) {
374+
getSurfaceColorStrokeDrawable().setShapeAppearanceModel(shapeAppearanceModel);
375+
}
376+
if (getMaskDrawable() != null) {
377+
getMaskDrawable().setShapeAppearanceModel(shapeAppearanceModel);
378+
}
370379
}
371380
}
372381

lib/javatests/com/google/android/material/button/MaterialButtonTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import android.content.Context;
2323
import android.graphics.drawable.Drawable;
2424
import androidx.core.graphics.drawable.DrawableCompat;
25+
26+
import android.os.Build;
2527
import android.view.View.MeasureSpec;
2628
import androidx.annotation.Nullable;
2729
import androidx.core.content.ContextCompat;
@@ -64,9 +66,23 @@ public void testSetShapeAppearanceModel_setCornerRadius() {
6466

6567
ShapeAppearanceModel newShapeAppearanceModel = materialButton.getShapeAppearanceModel();
6668

69+
assertThat(shapeAppearanceModel).isSameInstanceAs(newShapeAppearanceModel);
6770
assertThatCornerSizesMatch(shapeAppearanceModel, newShapeAppearanceModel);
6871
}
6972

73+
@Test
74+
@Config(sdk = Build.VERSION_CODES.LOLLIPOP)
75+
public void testShapeRippleDrawableInLollipop() {
76+
MaterialButton materialButton = new MaterialButton(context);
77+
ShapeAppearanceModel shapeAppearanceModel = materialButton.getShapeAppearanceModel();
78+
79+
materialButton.setCornerRadius((int) LARGE_CORNER_SIZE);
80+
ShapeAppearanceModel newShapeAppearanceModel = materialButton.getShapeAppearanceModel();
81+
assertThat(shapeAppearanceModel).isNotSameInstanceAs(newShapeAppearanceModel);
82+
assertThat(shapeAppearanceModel).isNotEqualTo(newShapeAppearanceModel);
83+
}
84+
85+
7086
@Test
7187
public void testSetShapeAppearanceModel() {
7288
MaterialButton materialButton = new MaterialButton(context);

0 commit comments

Comments
 (0)