From d60fb3fc2276f71f182fd281b2aa79cfa6131402 Mon Sep 17 00:00:00 2001 From: connieshi Date: Mon, 29 Jul 2019 17:14:55 -0400 Subject: [PATCH] Workaround chip compositing bug that dropped stated_pressed and state_focused. PiperOrigin-RevId: 260577585 --- .../android/material/chip/ChipDrawable.java | 38 ++--- .../material/color/MaterialColors.java | 121 -------------- .../material/color/AndroidManifest.xml | 26 --- .../material/color/MaterialColorsTest.java | 148 ------------------ .../color/res/color/test_background_state.xml | 24 --- ...test_background_state_unsupported_case.xml | 24 --- .../color/res/color/test_surface_state.xml | 22 --- 7 files changed, 10 insertions(+), 393 deletions(-) delete mode 100644 lib/javatests/com/google/android/material/color/AndroidManifest.xml delete mode 100644 lib/javatests/com/google/android/material/color/MaterialColorsTest.java delete mode 100644 lib/javatests/com/google/android/material/color/res/color/test_background_state.xml delete mode 100644 lib/javatests/com/google/android/material/color/res/color/test_background_state_unsupported_case.xml delete mode 100644 lib/javatests/com/google/android/material/color/res/color/test_surface_state.xml diff --git a/lib/java/com/google/android/material/chip/ChipDrawable.java b/lib/java/com/google/android/material/chip/ChipDrawable.java index 5d8ac3ba3..e11f36ae2 100644 --- a/lib/java/com/google/android/material/chip/ChipDrawable.java +++ b/lib/java/com/google/android/material/chip/ChipDrawable.java @@ -162,18 +162,7 @@ public class ChipDrawable extends MaterialShapeDrawable private static final boolean DEBUG = false; private static final int[] DEFAULT_STATE = new int[] {android.R.attr.state_enabled}; private static final String NAMESPACE_APP = "http://schemas.android.com/apk/res-auto"; - private static final int[][] states = - new int[][] { - new int[] { - android.R.attr.state_enabled, android.R.attr.state_selected, - }, - new int[] { - android.R.attr.state_enabled, android.R.attr.state_checked, - }, - new int[] {android.R.attr.state_enabled, android.R.attr.state_checkable}, - new int[] {android.R.attr.state_enabled}, - new int[] {} // default - }; + private static final ShapeDrawable closeIconRippleMask = new ShapeDrawable(new OvalShape()); // Visuals @@ -254,6 +243,7 @@ public class ChipDrawable extends MaterialShapeDrawable @ColorInt private int currentChipSurfaceColor; @ColorInt private int currentChipBackgroundColor; + @ColorInt private int currentCompositeSurfaceBackgroundColor; @ColorInt private int currentChipStrokeColor; @ColorInt private int currentCompatRippleColor; @ColorInt private int currentTextColor; @@ -996,6 +986,14 @@ public class ChipDrawable extends MaterialShapeDrawable invalidate = true; } + int newCompositeSurfaceBackgroundColor = + MaterialColors.layer(newChipSurfaceColor, newChipBackgroundColor); + if (currentCompositeSurfaceBackgroundColor != newCompositeSurfaceBackgroundColor) { + currentCompositeSurfaceBackgroundColor = newCompositeSurfaceBackgroundColor; + setFillColor(ColorStateList.valueOf(currentCompositeSurfaceBackgroundColor)); + invalidate = true; + } + int newChipStrokeColor = chipStrokeColor != null ? chipStrokeColor.getColorForState(chipState, currentChipStrokeColor) @@ -1294,11 +1292,6 @@ public class ChipDrawable extends MaterialShapeDrawable private void setChipSurfaceColor(@Nullable ColorStateList chipSurfaceColor) { if (this.chipSurfaceColor != chipSurfaceColor) { this.chipSurfaceColor = chipSurfaceColor; - if (isShapeThemingEnabled) { - if (chipSurfaceColor != null && chipBackgroundColor != null) { - setFillColor(compositeSurfaceBackgroundColor(chipBackgroundColor, chipSurfaceColor)); - } - } onStateChange(getState()); } } @@ -1356,21 +1349,10 @@ public class ChipDrawable extends MaterialShapeDrawable public void setChipBackgroundColor(@Nullable ColorStateList chipBackgroundColor) { if (this.chipBackgroundColor != chipBackgroundColor) { this.chipBackgroundColor = chipBackgroundColor; - if (isShapeThemingEnabled) { - if (chipSurfaceColor != null && chipBackgroundColor != null) { - setFillColor(compositeSurfaceBackgroundColor(chipBackgroundColor, chipSurfaceColor)); - } - } onStateChange(getState()); } } - private ColorStateList compositeSurfaceBackgroundColor( - @NonNull ColorStateList backgroundColor, @NonNull ColorStateList surfaceColor) { - return MaterialColors.layer( - surfaceColor, currentChipSurfaceColor, backgroundColor, currentChipBackgroundColor, states); - } - /** * Returns this chip's minimum height. * diff --git a/lib/java/com/google/android/material/color/MaterialColors.java b/lib/java/com/google/android/material/color/MaterialColors.java index de73a59c8..2bc1df63b 100644 --- a/lib/java/com/google/android/material/color/MaterialColors.java +++ b/lib/java/com/google/android/material/color/MaterialColors.java @@ -18,21 +18,15 @@ package com.google.android.material.color; import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; import android.content.Context; -import android.content.res.ColorStateList; import android.graphics.Color; import androidx.annotation.AttrRes; import androidx.annotation.ColorInt; import androidx.annotation.FloatRange; -import androidx.annotation.Nullable; import androidx.annotation.RestrictTo; import com.google.android.material.resources.MaterialAttributes; import androidx.core.graphics.ColorUtils; -import android.util.StateSet; import android.util.TypedValue; import android.view.View; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; /** * A utility class for common color variants used in Material themes. @@ -144,119 +138,4 @@ public class MaterialColors { public static int layer(@ColorInt int backgroundColor, @ColorInt int overlayColor) { return ColorUtils.compositeColors(overlayColor, backgroundColor); } - - /** - * Calculates a color state list that represents the layering of the {@code overlayColor} on top - * of the {@code backgroundColor} for the given set of {@code states}. CAUTION: More specific - * states that have the same color value as a more generic state may be dropped, see example - * below: - *

states: - *

-   *   {selected, enabled},
-   *   {checked, enabled},
-   *   {enabled},
-   *   default
-   * 
- *

Overlay CSL: - *

-   *   ""      TRANSPARENT
-   * 
- *

Scenario 1 - *

Background CSL: - *

-   *   checked RED
-   *   ""      GREEN
-   * 
- * - *

Current result: - *

-   *   enabled, checked RED
-   *   enabled          GREEN
-   * 
- * - *

Color for state {enabled, checked, selected} --> returns RED # correct - * - *

Result if iterating top down through CSL to composite each state color: - *

-   *   enabled, selected GREEN
-   *   enabled, checked  RED
-   *   enabled           GREEN
-   * 
- * - *

Color for state {enabled, checked, selected} --> returns GREEN #incorrect - * - *

Scenario 2 - * Background CSL: - *

-   *   selected GREEN
-   *   checked  RED
-   *   ""       GREEN
-   * 
- *

Current result: - *

-   *   enabled, checked RED
-   *   enabled          GREEN
-   * 
- *

Color for state {enabled, checked, selected} --> returns RED # incorrect - * - *

Result if iterating top down through CSL to composite each state color: - *

-   *   enabled, selected GREEN
-   *   enabled, checked RED
-   *   enabled          GREEN
-   * 
- *

Color for state {enabled, checked, selected} --> returns GREEN # correct - */ - public static ColorStateList layer( - ColorStateList backgroundColor, - @ColorInt int defaultBackgroundColor, - ColorStateList overlayColor, - @ColorInt int defaultOverlayColor, - int[][] states) { - List uniqueColors = new ArrayList<>(); - List uniqueStateSet = new ArrayList<>(); - - // Iterates bottom to top, from least to most specific states. - for (int i = states.length - 1; i >= 0; i--) { - int[] curState = states[i]; - int layeredStateColor = - MaterialColors.layer( - backgroundColor.getColorForState(curState, defaultBackgroundColor), - overlayColor.getColorForState(curState, defaultOverlayColor)); - - if (shouldAddColorForState(uniqueColors, layeredStateColor, uniqueStateSet, curState)) { - // Add to the front of the list in original CSL order. - uniqueColors.add(0, layeredStateColor); - uniqueStateSet.add(0, curState); - } - } - - // Convert lists to arrays. - int numStates = uniqueColors.size(); - int[] colors = new int[numStates]; - int[][] colorStates = new int[numStates][]; - for (int i = 0; i < numStates; i++) { - colors[i] = uniqueColors.get(i); - colorStates[i] = uniqueStateSet.get(i); - } - return new ColorStateList(colorStates, colors); - } - - /** - * Returns whether the specified @{code color} should be added to a ColorStateList for the - * specified {@code state} or if the existing color set and state set already cover it. - */ - private static boolean shouldAddColorForState( - List colorSet, @ColorInt int color, List stateSet, @Nullable int[] state) { - new HashSet(colorSet); - if (!colorSet.contains(color)) { - return true; - } - for (int[] stateSetItem : stateSet) { - if (StateSet.stateSetMatches(stateSetItem, state)) { - return (colorSet.get(stateSet.indexOf(stateSetItem)) != color); - } - } - return true; - } } diff --git a/lib/javatests/com/google/android/material/color/AndroidManifest.xml b/lib/javatests/com/google/android/material/color/AndroidManifest.xml deleted file mode 100644 index 922707e4b..000000000 --- a/lib/javatests/com/google/android/material/color/AndroidManifest.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - diff --git a/lib/javatests/com/google/android/material/color/MaterialColorsTest.java b/lib/javatests/com/google/android/material/color/MaterialColorsTest.java deleted file mode 100644 index bac5c5b0a..000000000 --- a/lib/javatests/com/google/android/material/color/MaterialColorsTest.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * 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. - */ - -package com.google.android.material.color; - -import com.google.android.material.R; - -import android.content.Context; -import android.content.res.ColorStateList; -import androidx.annotation.ColorInt; -import androidx.appcompat.content.res.AppCompatResources; -import androidx.test.core.app.ApplicationProvider; -import java.util.ArrayList; -import java.util.List; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.internal.DoNotInstrument; - -/** Tests for {@link com.google.android.material.color.MaterialColors}. */ -@RunWith(RobolectricTestRunner.class) -@DoNotInstrument -public class MaterialColorsTest { - - private static final int[][] states = - new int[][] { - new int[] { - android.R.attr.state_enabled, android.R.attr.state_selected, - }, - new int[] { - android.R.attr.state_enabled, android.R.attr.state_checked, - }, - new int[] {android.R.attr.state_enabled}, - new int[] {}, - }; - - private static final int[] state_empty = {}; - private static final int[] state_enabled = {android.R.attr.state_enabled}; - - private final Context context = ApplicationProvider.getApplicationContext(); - - @Before - public void themeApplicationContext() { - context.setTheme(R.style.Theme_MaterialComponents_Light); - } - - @Test - public void testLayerColorStateLists() { - ColorStateList overlayColor = - AppCompatResources.getColorStateList(context, R.color.test_background_state); - ColorStateList backgroundColor = - AppCompatResources.getColorStateList(context, R.color.test_surface_state); - ColorStateList compositeColor = - MaterialColors.layer(backgroundColor, -1, overlayColor, 0, states); - - List testStatesList = generateTestStatesList(); - for (int[][] testStates : testStatesList) { - for (int[] state : testStates) { - compareLayeredColorsForStates(compositeColor, backgroundColor, overlayColor, state, true); - } - } - } - - @Test - public void testLayerColorStateLists_repeatedColors_unsupported() { - ColorStateList overlayColor = - AppCompatResources.getColorStateList( - context, R.color.test_background_state_unsupported_case); - ColorStateList backgroundColor = - AppCompatResources.getColorStateList(context, R.color.test_surface_state); - ColorStateList compositeColor = - MaterialColors.layer(backgroundColor, -1, overlayColor, 0, states); - - int[] unsupportedTestState = - new int[] { - android.R.attr.state_enabled, android.R.attr.state_selected, android.R.attr.state_checked, - }; - compareLayeredColorsForStates( - compositeColor, backgroundColor, overlayColor, unsupportedTestState, false); - } - - private static List generateTestStatesList() { - List testStatesList = new ArrayList<>(); - int[][] testStates1 = - new int[][] { - new int[] { - android.R.attr.state_enabled, - android.R.attr.state_selected, - android.R.attr.state_checked, - }, - state_enabled, - state_empty, - }; - testStatesList.add(testStates1); - int[][] testState2 = { - {android.R.attr.state_enabled, android.R.attr.state_selected}, state_enabled, state_empty - }; - testStatesList.add(testState2); - int[][] testState3 = { - {android.R.attr.state_checked, android.R.attr.state_enabled}, state_enabled, state_empty - }; - testStatesList.add(testState3); - int[][] testState4 = { - {android.R.attr.state_pressed, android.R.attr.state_checked, android.R.attr.state_enabled}, - state_enabled, - state_empty - }; - testStatesList.add(testState4); - return testStatesList; - } - - private static void compareLayeredColorsForStates( - ColorStateList compositeColor, - ColorStateList backgroundColor, - ColorStateList overlayColor, - int[] state, - boolean expectedResult) { - int composite = compositeColor.getColorForState(state, -1); - int background = backgroundColor.getColorForState(state, -1); - int overlay = overlayColor.getColorForState(state, -1); - int expectedColor = MaterialColors.layer(background, overlay); - assertColorsAreEqual(expectedColor, composite, expectedResult); - } - - private static void assertColorsAreEqual( - @ColorInt int expectedColor, @ColorInt int color, boolean expectedResult) { - if (expectedResult) { - Assert.assertEquals(expectedColor, color); - } else { - Assert.assertNotEquals(expectedColor, color); - } - } -} diff --git a/lib/javatests/com/google/android/material/color/res/color/test_background_state.xml b/lib/javatests/com/google/android/material/color/res/color/test_background_state.xml deleted file mode 100644 index c887c9c3d..000000000 --- a/lib/javatests/com/google/android/material/color/res/color/test_background_state.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - diff --git a/lib/javatests/com/google/android/material/color/res/color/test_background_state_unsupported_case.xml b/lib/javatests/com/google/android/material/color/res/color/test_background_state_unsupported_case.xml deleted file mode 100644 index 4eea745cd..000000000 --- a/lib/javatests/com/google/android/material/color/res/color/test_background_state_unsupported_case.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - diff --git a/lib/javatests/com/google/android/material/color/res/color/test_surface_state.xml b/lib/javatests/com/google/android/material/color/res/color/test_surface_state.xml deleted file mode 100644 index 928fd87ea..000000000 --- a/lib/javatests/com/google/android/material/color/res/color/test_surface_state.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - -