Skip to content

Commit

Permalink
[Checkbox] Fix pre-21 issue where a child of the layer drawable (the …
Browse files Browse the repository at this point in the history
…button and/or the icon drawables) may not have its constant state set up properly.

PiperOrigin-RevId: 464128801
  • Loading branch information
leticiarossi authored and drchen committed Aug 4, 2022
1 parent ee19b01 commit 079371f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Expand Up @@ -541,10 +541,10 @@ public boolean isCenterIfNoTextEnabled() {

private void refreshButtonDrawable() {
buttonDrawable =
DrawableUtils.createTintableDrawableIfNeeded(
DrawableUtils.createTintableMutatedDrawableIfNeeded(
buttonDrawable, buttonTintList, CompoundButtonCompat.getButtonTintMode(this));
buttonIconDrawable =
DrawableUtils.createTintableDrawableIfNeeded(
DrawableUtils.createTintableMutatedDrawableIfNeeded(
buttonIconDrawable, buttonIconTintList, buttonIconTintMode);

setUpDefaultButtonDrawableAnimationIfNeeded();
Expand Down
32 changes: 32 additions & 0 deletions lib/java/com/google/android/material/internal/DrawableUtils.java
Expand Up @@ -41,9 +41,39 @@ public class DrawableUtils {

private DrawableUtils() {}

/**
* Wraps and mutates the passed in drawable so that it may be used for tinting if a tintList is
* present. Also applies the tintMode if present.
*/
@Nullable
public static Drawable createTintableDrawableIfNeeded(
@Nullable Drawable drawable, @Nullable ColorStateList tintList, @Nullable Mode tintMode) {
return createTintableMutatedDrawableIfNeeded(
drawable, tintList, tintMode, /* forceMutate= */ false);
}

/**
* Wraps and mutates the passed in drawable so that it may be used for tinting if a tintList is
* present. Also applies the tintMode if present. If there's not a tintList and the API level is <
* 21, it'll still mutate the drawable.
*
* <p>Use this method instead of the above if the passed in drawable will be a child of a {@link
* LayerDrawable} in APIs < 21, its tintList may be null, and it may be mutated, in order to
* prevent issue where the drawable may not have its constant state set up properly.
*/
@Nullable
public static Drawable createTintableMutatedDrawableIfNeeded(
@Nullable Drawable drawable, @Nullable ColorStateList tintList, @Nullable Mode tintMode) {
return createTintableMutatedDrawableIfNeeded(
drawable, tintList, tintMode, VERSION.SDK_INT < VERSION_CODES.LOLLIPOP);
}

@Nullable
private static Drawable createTintableMutatedDrawableIfNeeded(
@Nullable Drawable drawable,
@Nullable ColorStateList tintList,
@Nullable Mode tintMode,
boolean forceMutate) {
if (drawable == null) {
return null;
}
Expand All @@ -52,6 +82,8 @@ public static Drawable createTintableDrawableIfNeeded(
if (tintMode != null) {
DrawableCompat.setTintMode(drawable, tintMode);
}
} else if (forceMutate) {
drawable.mutate();
}
return drawable;
}
Expand Down

0 comments on commit 079371f

Please sign in to comment.