|
16 | 16 |
|
17 | 17 | package com.google.android.material.button; |
18 | 18 |
|
19 | | -import com.google.android.material.R; |
20 | | - |
21 | | -import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; |
22 | | - |
23 | 19 | import android.content.Context; |
24 | 20 | import android.content.res.ColorStateList; |
25 | 21 | import android.content.res.TypedArray; |
|
31 | 27 | import android.graphics.drawable.RippleDrawable; |
32 | 28 | import android.os.Build.VERSION; |
33 | 29 | import android.os.Build.VERSION_CODES; |
34 | | -import androidx.core.graphics.drawable.DrawableCompat; |
35 | | -import androidx.core.view.ViewCompat; |
| 30 | + |
36 | 31 | import androidx.annotation.Dimension; |
37 | 32 | import androidx.annotation.NonNull; |
38 | 33 | import androidx.annotation.Nullable; |
39 | 34 | 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; |
40 | 39 | import com.google.android.material.color.MaterialColors; |
41 | 40 | import com.google.android.material.internal.ViewUtils; |
42 | 41 | import com.google.android.material.resources.MaterialResources; |
|
46 | 45 | import com.google.android.material.shape.ShapeAppearanceModel; |
47 | 46 | import com.google.android.material.shape.Shapeable; |
48 | 47 |
|
| 48 | +import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; |
| 49 | + |
49 | 50 | /** @hide */ |
50 | 51 | @RestrictTo(LIBRARY_GROUP) |
51 | 52 | class MaterialButtonHelper { |
52 | 53 |
|
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; |
54 | 57 | private final MaterialButton materialButton; |
55 | 58 | @NonNull private ShapeAppearanceModel shapeAppearanceModel; |
56 | 59 |
|
@@ -218,7 +221,7 @@ private Drawable createBackground() { |
218 | 221 | ? MaterialColors.getColor(materialButton, R.attr.colorSurface) |
219 | 222 | : Color.TRANSPARENT); |
220 | 223 |
|
221 | | - if (IS_LOLLIPOP) { |
| 224 | + if (IS_MIN_LOLLIPOP) { |
222 | 225 | maskDrawable = new MaterialShapeDrawable(shapeAppearanceModel); |
223 | 226 | DrawableCompat.setTint(maskDrawable, Color.WHITE); |
224 | 227 | rippleDrawable = |
@@ -255,10 +258,10 @@ void setBackgroundColor(int color) { |
255 | 258 | void setRippleColor(@Nullable ColorStateList rippleColor) { |
256 | 259 | if (this.rippleColor != rippleColor) { |
257 | 260 | this.rippleColor = rippleColor; |
258 | | - if (IS_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawable) { |
| 261 | + if (IS_MIN_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawable) { |
259 | 262 | ((RippleDrawable) materialButton.getBackground()) |
260 | 263 | .setColor(RippleUtils.sanitizeRippleDrawableColor(rippleColor)); |
261 | | - } else if (!IS_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawableCompat) { |
| 264 | + } else if (!IS_MIN_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawableCompat) { |
262 | 265 | ((RippleDrawableCompat) materialButton.getBackground()).setTintList( |
263 | 266 | RippleUtils.sanitizeRippleDrawableColor(rippleColor)); |
264 | 267 | } |
@@ -326,7 +329,7 @@ int getCornerRadius() { |
326 | 329 | @Nullable |
327 | 330 | private MaterialShapeDrawable getMaterialShapeDrawable(boolean getSurfaceColorStrokeDrawable) { |
328 | 331 | if (rippleDrawable != null && rippleDrawable.getNumberOfLayers() > 0) { |
329 | | - if (IS_LOLLIPOP) { |
| 332 | + if (IS_MIN_LOLLIPOP) { |
330 | 333 | InsetDrawable insetDrawable = (InsetDrawable) rippleDrawable.getDrawable(0); |
331 | 334 | LayerDrawable layerDrawable = (LayerDrawable) insetDrawable.getDrawable(); |
332 | 335 | return (MaterialShapeDrawable) |
@@ -359,14 +362,20 @@ private MaterialShapeDrawable getSurfaceColorStrokeDrawable() { |
359 | 362 | } |
360 | 363 |
|
361 | 364 | 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 | + } |
370 | 379 | } |
371 | 380 | } |
372 | 381 |
|
|
0 commit comments