diff --git a/lib/java/com/google/android/material/color/ColorResourcesOverride.java b/lib/java/com/google/android/material/color/ColorResourcesOverride.java index bb9ec1b3b07..536147d08f3 100644 --- a/lib/java/com/google/android/material/color/ColorResourcesOverride.java +++ b/lib/java/com/google/android/material/color/ColorResourcesOverride.java @@ -24,7 +24,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RestrictTo; -import androidx.annotation.StyleRes; import java.util.Map; /** @@ -47,20 +46,6 @@ public interface ColorResourcesOverride { boolean applyIfPossible( @NonNull Context context, @NonNull Map colorResourceIdsToColorValues); - /** - * Overrides the color resources to the given context, returns {@code true} if new color values - * have been applied. - * - * @param context The target context. - * @param colorResourceIdsToColorValues The mapping from the color resources id to the updated - * color value. - * @param theme The resource ID of the theme overlay to be applied. - */ - boolean applyIfPossible( - @NonNull Context context, - @NonNull Map colorResourceIdsToColorValues, - @StyleRes int theme); - /** * Wraps the given Context with the theme overlay where color resources are updated at runtime. If * not possible, the original Context will be returned. @@ -73,21 +58,6 @@ boolean applyIfPossible( Context wrapContextIfPossible( @NonNull Context context, @NonNull Map colorResourceIdsToColorValues); - /** - * Wraps the given Context with the theme overlay where color resources are updated at runtime. If - * not possible, the original Context will be returned. - * - * @param context The target context. - * @param colorResourceIdsToColorValues The mapping from the color resources id to the updated - * color value. - * @param theme The resource ID of the theme overlay to be applied. - */ - @NonNull - Context wrapContextIfPossible( - @NonNull Context context, - @NonNull Map colorResourceIdsToColorValues, - @StyleRes int theme); - @Nullable static ColorResourcesOverride getInstance() { if (VERSION_CODES.R <= VERSION.SDK_INT && VERSION.SDK_INT <= VERSION_CODES.TIRAMISU) { diff --git a/lib/java/com/google/android/material/color/DynamicColors.java b/lib/java/com/google/android/material/color/DynamicColors.java index 4a305a568fb..b937e0231f6 100644 --- a/lib/java/com/google/android/material/color/DynamicColors.java +++ b/lib/java/com/google/android/material/color/DynamicColors.java @@ -32,13 +32,10 @@ import androidx.annotation.ChecksSdkIntAtLeast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; import androidx.annotation.StyleRes; -import androidx.core.content.ContextCompat; import androidx.core.os.BuildCompat; import com.google.android.material.color.utilities.Hct; import com.google.android.material.color.utilities.SchemeContent; -import com.google.android.material.resources.MaterialAttributes; import java.lang.reflect.Method; import java.util.Collections; import java.util.HashMap; @@ -50,24 +47,6 @@ public class DynamicColors { private static final int[] DYNAMIC_COLOR_THEME_OVERLAY_ATTRIBUTE = new int[] {R.attr.dynamicColorThemeOverlay}; - @RequiresApi(api = VERSION_CODES.S) - private static final int[] SYSTEM_NEUTRAL_PALETTE_RES_IDS = - new int[] { - android.R.color.system_neutral1_0, - android.R.color.system_neutral1_10, - android.R.color.system_neutral1_50, - android.R.color.system_neutral1_100, - android.R.color.system_neutral1_200, - android.R.color.system_neutral1_300, - android.R.color.system_neutral1_400, - android.R.color.system_neutral1_500, - android.R.color.system_neutral1_600, - android.R.color.system_neutral1_700, - android.R.color.system_neutral1_800, - android.R.color.system_neutral1_900, - android.R.color.system_neutral1_1000, - }; - private static final DeviceSupportCondition DEFAULT_DEVICE_SUPPORT_CONDITION = new DeviceSupportCondition() { @Override @@ -137,7 +116,6 @@ public boolean isSupported() { } private static final int USE_DEFAULT_THEME_OVERLAY = 0; - private static final int UPDATED_NEUTRAL_PALETTE_CHROMA = 6; private DynamicColors() {} @@ -305,18 +283,16 @@ public static void applyToActivityIfAvailable( return; } // Set default theme overlay as 0, as it's not used in content-based dynamic colors. - int themeOverlayResourceId = 0; + int theme = 0; // Only retrieves the theme overlay if we're applying just dynamic colors. if (dynamicColorsOptions.getContentBasedSeedColor() == null) { - themeOverlayResourceId = + theme = dynamicColorsOptions.getThemeOverlay() == USE_DEFAULT_THEME_OVERLAY ? getDefaultThemeOverlay(activity) : dynamicColorsOptions.getThemeOverlay(); } - if (dynamicColorsOptions - .getPrecondition() - .shouldApplyDynamicColors(activity, themeOverlayResourceId)) { + if (dynamicColorsOptions.getPrecondition().shouldApplyDynamicColors(activity, theme)) { // Applies content-based dynamic colors if content-based source is provided. if (dynamicColorsOptions.getContentBasedSeedColor() != null) { SchemeContent scheme = @@ -334,9 +310,8 @@ public static void applyToActivityIfAvailable( return; } } - } else if (!maybeApplyThemeOverlayWithUpdatedNeutralChroma( - activity, themeOverlayResourceId)) { - ThemeUtils.applyThemeOverlay(activity, themeOverlayResourceId); + } else { + ThemeUtils.applyThemeOverlay(activity, theme); } // Applies client's callback after content-based dynamic colors or just dynamic colors has // been applied. @@ -412,16 +387,6 @@ public static Context wrapContextIfAvailable( originalContext, MaterialColorUtilitiesHelper.createColorResourcesIdsToColorValues(scheme)); } - } else { - if (shouldOverrideNeutralChroma(originalContext)) { - ColorResourcesOverride resourcesOverride = ColorResourcesOverride.getInstance(); - if (resourcesOverride != null) { - return resourcesOverride.wrapContextIfPossible( - originalContext, - createColorResourcesIdsToColorValuesWithUpdatedChroma(originalContext), - theme); - } - } } return new ContextThemeWrapper(originalContext, theme); } @@ -453,57 +418,6 @@ private static int getDefaultThemeOverlay(@NonNull Context context) { return theme; } - @RequiresApi(api = VERSION_CODES.S) - private static Map createColorResourcesIdsToColorValuesWithUpdatedChroma( - Context context) { - Map colorResourcesIdsToColorValues = new HashMap<>(); - for (int neutralResId : SYSTEM_NEUTRAL_PALETTE_RES_IDS) { - Hct colorHct = Hct.fromInt(ContextCompat.getColor(context, neutralResId)); - colorHct.setChroma(UPDATED_NEUTRAL_PALETTE_CHROMA); - colorResourcesIdsToColorValues.put(neutralResId, colorHct.toInt()); - } - return colorResourcesIdsToColorValues; - } - - /** - * Applies the theme overlay to the context with an updated neutral palette with chroma 6, if - * possible. See {@link #shouldOverrideNeutralChroma(Context)} for when the neutral palettes - * should be updated. - * - * @return Whether the theme overlay is applied with updated neutral palettes successfully. - */ - private static boolean maybeApplyThemeOverlayWithUpdatedNeutralChroma( - @NonNull Context context, int themeOverlayResourceId) { - if (shouldOverrideNeutralChroma(context)) { - ColorResourcesOverride resourcesOverride = ColorResourcesOverride.getInstance(); - if (resourcesOverride != null) { - return resourcesOverride.applyIfPossible( - context, - createColorResourcesIdsToColorValuesWithUpdatedChroma(context), - themeOverlayResourceId); - } - } - return false; - } - - /** - * Checks whether the neutral palette should be overridden with chroma 6. - * - * @return True, if Android version is S or T and preUDynamicNeutralChromaUpdateEnabled is true in - * current context. - */ - @ChecksSdkIntAtLeast(api = VERSION_CODES.S) - private static boolean shouldOverrideNeutralChroma(@NonNull Context context) { - // TODO(b/272585197) Remove after tonal surface migration is complete. - boolean shouldUpdateNeutralChroma = - MaterialAttributes.resolveBoolean( - context, R.attr.preUDynamicNeutralChromaUpdateEnabled, /* defaultValue= */ false); - // Update neutral palette chroma from 4 to 6 for backward compatibility. - return VERSION.SDK_INT < VERSION_CODES.UPSIDE_DOWN_CAKE - && VERSION.SDK_INT >= VERSION_CODES.S - && shouldUpdateNeutralChroma; - } - /** The interface that provides a precondition to decide if dynamic colors should be applied. */ public interface Precondition { diff --git a/lib/java/com/google/android/material/color/ResourcesLoaderColorResourcesOverride.java b/lib/java/com/google/android/material/color/ResourcesLoaderColorResourcesOverride.java index 71037c4e26c..cae7deee091 100644 --- a/lib/java/com/google/android/material/color/ResourcesLoaderColorResourcesOverride.java +++ b/lib/java/com/google/android/material/color/ResourcesLoaderColorResourcesOverride.java @@ -24,7 +24,6 @@ import android.view.ContextThemeWrapper; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; -import androidx.annotation.StyleRes; import java.util.Map; /** @@ -47,24 +46,8 @@ private ResourcesLoaderColorResourcesOverride() {} @Override public boolean applyIfPossible( Context context, Map colorResourceIdsToColorValues) { - return applyIfPossible( - context, colorResourceIdsToColorValues, R.style.ThemeOverlay_Material3_PersonalizedColors); - } - - /** - * Overrides the color resources to the given context, returns {@code true} if new color values - * have been applied. - * - * @param context The target context. - * @param colorResourceIdsToColorValues The mapping from the color resources id to the updated - * color value. - * @param theme The resource ID of the theme overlay to be applied. - */ - @Override - public boolean applyIfPossible( - Context context, Map colorResourceIdsToColorValues, @StyleRes int theme) { if (ResourcesLoaderUtils.addResourcesLoaderToContext(context, colorResourceIdsToColorValues)) { - ThemeUtils.applyThemeOverlay(context, theme); + ThemeUtils.applyThemeOverlay(context, R.style.ThemeOverlay_Material3_PersonalizedColors); return true; } return false; @@ -82,24 +65,8 @@ public boolean applyIfPossible( @NonNull public Context wrapContextIfPossible( Context context, Map colorResourceIdsToColorValues) { - return wrapContextIfPossible( - context, colorResourceIdsToColorValues, R.style.ThemeOverlay_Material3_PersonalizedColors); - } - - /** - * Wraps the given Context with the theme overlay where color resources are updated at runtime. If - * not possible, the original Context will be returned. - * - * @param context The target context. - * @param colorResourceIdsToColorValues The mapping from the color resources id to the updated - * color value. - * @param theme The resource ID of the theme overlay to be applied. - */ - @Override - @NonNull - public Context wrapContextIfPossible( - Context context, Map colorResourceIdsToColorValues, @StyleRes int theme) { - ContextThemeWrapper themeWrapper = new ContextThemeWrapper(context, theme); + ContextThemeWrapper themeWrapper = + new ContextThemeWrapper(context, R.style.ThemeOverlay_Material3_PersonalizedColors); // Because ContextThemeWrapper does not provide a new set of resources, override config to // retrieve the new set of resources and to keep the original context's resources intact. themeWrapper.applyOverrideConfiguration(new Configuration()); diff --git a/lib/java/com/google/android/material/color/res/values/attrs.xml b/lib/java/com/google/android/material/color/res/values/attrs.xml index 60ea4ebc1ac..327d3e92b8d 100644 --- a/lib/java/com/google/android/material/color/res/values/attrs.xml +++ b/lib/java/com/google/android/material/color/res/values/attrs.xml @@ -168,10 +168,6 @@ - - - -