Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

package com.google.android.material.button;

import com.google.android.material.R;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
Expand All @@ -31,12 +27,15 @@
import android.graphics.drawable.RippleDrawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;

import androidx.annotation.Dimension;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;

import com.google.android.material.R;
import com.google.android.material.color.MaterialColors;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.resources.MaterialResources;
Expand All @@ -46,11 +45,15 @@
import com.google.android.material.shape.ShapeAppearanceModel;
import com.google.android.material.shape.Shapeable;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

/** @hide */
@RestrictTo(LIBRARY_GROUP)
class MaterialButtonHelper {

private static final boolean IS_LOLLIPOP = VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP;
private static final boolean IS_MIN_LOLLIPOP = VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IS_LOLLIPOP_OR_ABOVE?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, wasn't "IS_MIN" and old used convention in Android when gating min API logic? I'm pretty sure that I saw some time ago code using this prefix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess Android uses atLeastXXXX in many places, which also works. : )

private static final boolean IS_LOLLIPOP = VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP &&
VERSION.SDK_INT <= VERSION_CODES.LOLLIPOP_MR1;
private final MaterialButton materialButton;
@NonNull private ShapeAppearanceModel shapeAppearanceModel;

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

if (IS_LOLLIPOP) {
if (IS_MIN_LOLLIPOP) {
maskDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
DrawableCompat.setTint(maskDrawable, Color.WHITE);
rippleDrawable =
Expand Down Expand Up @@ -255,10 +258,10 @@ void setBackgroundColor(int color) {
void setRippleColor(@Nullable ColorStateList rippleColor) {
if (this.rippleColor != rippleColor) {
this.rippleColor = rippleColor;
if (IS_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawable) {
if (IS_MIN_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawable) {
((RippleDrawable) materialButton.getBackground())
.setColor(RippleUtils.sanitizeRippleDrawableColor(rippleColor));
} else if (!IS_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawableCompat) {
} else if (!IS_MIN_LOLLIPOP && materialButton.getBackground() instanceof RippleDrawableCompat) {
((RippleDrawableCompat) materialButton.getBackground()).setTintList(
RippleUtils.sanitizeRippleDrawableColor(rippleColor));
}
Expand Down Expand Up @@ -326,7 +329,7 @@ int getCornerRadius() {
@Nullable
private MaterialShapeDrawable getMaterialShapeDrawable(boolean getSurfaceColorStrokeDrawable) {
if (rippleDrawable != null && rippleDrawable.getNumberOfLayers() > 0) {
if (IS_LOLLIPOP) {
if (IS_MIN_LOLLIPOP) {
InsetDrawable insetDrawable = (InsetDrawable) rippleDrawable.getDrawable(0);
LayerDrawable layerDrawable = (LayerDrawable) insetDrawable.getDrawable();
return (MaterialShapeDrawable)
Expand Down Expand Up @@ -359,14 +362,20 @@ private MaterialShapeDrawable getSurfaceColorStrokeDrawable() {
}

private void updateButtonShape(@NonNull ShapeAppearanceModel shapeAppearanceModel) {
if (getMaterialShapeDrawable() != null) {
getMaterialShapeDrawable().setShapeAppearanceModel(shapeAppearanceModel);
}
if (getSurfaceColorStrokeDrawable() != null) {
getSurfaceColorStrokeDrawable().setShapeAppearanceModel(shapeAppearanceModel);
}
if (getMaskDrawable() != null) {
getMaskDrawable().setShapeAppearanceModel(shapeAppearanceModel);
if (IS_LOLLIPOP) {
// There seems to be a bug to drawables that is affecting Lollipop, since invalidation is not
// changing an existing drawable shape. This is a fallback
updateBackground();
} else {
if (getMaterialShapeDrawable() != null) {
getMaterialShapeDrawable().setShapeAppearanceModel(shapeAppearanceModel);
}
if (getSurfaceColorStrokeDrawable() != null) {
getSurfaceColorStrokeDrawable().setShapeAppearanceModel(shapeAppearanceModel);
}
if (getMaskDrawable() != null) {
getMaskDrawable().setShapeAppearanceModel(shapeAppearanceModel);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.core.graphics.drawable.DrawableCompat;

import android.os.Build;
import android.view.View.MeasureSpec;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
Expand Down Expand Up @@ -64,9 +66,23 @@ public void testSetShapeAppearanceModel_setCornerRadius() {

ShapeAppearanceModel newShapeAppearanceModel = materialButton.getShapeAppearanceModel();

assertThat(shapeAppearanceModel).isSameInstanceAs(newShapeAppearanceModel);
assertThatCornerSizesMatch(shapeAppearanceModel, newShapeAppearanceModel);
}

@Test
@Config(sdk = Build.VERSION_CODES.LOLLIPOP)
public void testShapeRippleDrawableInLollipop() {
MaterialButton materialButton = new MaterialButton(context);
ShapeAppearanceModel shapeAppearanceModel = materialButton.getShapeAppearanceModel();

materialButton.setCornerRadius((int) LARGE_CORNER_SIZE);
ShapeAppearanceModel newShapeAppearanceModel = materialButton.getShapeAppearanceModel();
assertThat(shapeAppearanceModel).isNotSameInstanceAs(newShapeAppearanceModel);
assertThat(shapeAppearanceModel).isNotEqualTo(newShapeAppearanceModel);
}


@Test
public void testSetShapeAppearanceModel() {
MaterialButton materialButton = new MaterialButton(context);
Expand Down