Skip to content

Commit

Permalink
[M3][Color] Remove resources loader support for tonal surface update
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 557500242
(cherry picked from commit 1a9d54f)
  • Loading branch information
Material Design Team authored and dsn5ft committed Sep 7, 2023
1 parent 7b8507f commit dfd9bfb
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 161 deletions.
Expand Up @@ -24,7 +24,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.StyleRes;
import java.util.Map;

/**
Expand All @@ -47,20 +46,6 @@ public interface ColorResourcesOverride {
boolean applyIfPossible(
@NonNull Context context, @NonNull Map<Integer, Integer> 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<Integer, Integer> 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.
Expand All @@ -73,21 +58,6 @@ boolean applyIfPossible(
Context wrapContextIfPossible(
@NonNull Context context, @NonNull Map<Integer, Integer> 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<Integer, Integer> colorResourceIdsToColorValues,
@StyleRes int theme);

@Nullable
static ColorResourcesOverride getInstance() {
if (VERSION_CODES.R <= VERSION.SDK_INT && VERSION.SDK_INT <= VERSION_CODES.TIRAMISU) {
Expand Down
96 changes: 5 additions & 91 deletions lib/java/com/google/android/material/color/DynamicColors.java
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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() {}

Expand Down Expand Up @@ -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 =
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -453,57 +418,6 @@ private static int getDefaultThemeOverlay(@NonNull Context context) {
return theme;
}

@RequiresApi(api = VERSION_CODES.S)
private static Map<Integer, Integer> createColorResourcesIdsToColorValuesWithUpdatedChroma(
Context context) {
Map<Integer, Integer> 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 {

Expand Down
Expand Up @@ -24,7 +24,6 @@
import android.view.ContextThemeWrapper;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.StyleRes;
import java.util.Map;

/**
Expand All @@ -47,24 +46,8 @@ private ResourcesLoaderColorResourcesOverride() {}
@Override
public boolean applyIfPossible(
Context context, Map<Integer, Integer> 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<Integer, Integer> 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;
Expand All @@ -82,24 +65,8 @@ public boolean applyIfPossible(
@NonNull
public Context wrapContextIfPossible(
Context context, Map<Integer, Integer> 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<Integer, Integer> 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());
Expand Down
Expand Up @@ -168,10 +168,6 @@
<!-- The default dynamic color theme overlay to use when applying dynamic colors. -->
<attr name="dynamicColorThemeOverlay" format="reference"/>

<!-- Do not use. For internal migration only. -->
<!-- TODO(b/272585197) Remove after tonal surface migration is complete. -->
<attr name="preUDynamicNeutralChromaUpdateEnabled" format="boolean"/>

<!-- Deprecated. -->

<!-- A tonal variation of the primary color. -->
Expand Down

0 comments on commit dfd9bfb

Please sign in to comment.