Skip to content

Commit dfd9bfb

Browse files
Material Design Teamdsn5ft
authored andcommitted
[M3][Color] Remove resources loader support for tonal surface update
PiperOrigin-RevId: 557500242 (cherry picked from commit 1a9d54f)
1 parent 7b8507f commit dfd9bfb

File tree

4 files changed

+8
-161
lines changed

4 files changed

+8
-161
lines changed

lib/java/com/google/android/material/color/ColorResourcesOverride.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import androidx.annotation.NonNull;
2525
import androidx.annotation.Nullable;
2626
import androidx.annotation.RestrictTo;
27-
import androidx.annotation.StyleRes;
2827
import java.util.Map;
2928

3029
/**
@@ -47,20 +46,6 @@ public interface ColorResourcesOverride {
4746
boolean applyIfPossible(
4847
@NonNull Context context, @NonNull Map<Integer, Integer> colorResourceIdsToColorValues);
4948

50-
/**
51-
* Overrides the color resources to the given context, returns {@code true} if new color values
52-
* have been applied.
53-
*
54-
* @param context The target context.
55-
* @param colorResourceIdsToColorValues The mapping from the color resources id to the updated
56-
* color value.
57-
* @param theme The resource ID of the theme overlay to be applied.
58-
*/
59-
boolean applyIfPossible(
60-
@NonNull Context context,
61-
@NonNull Map<Integer, Integer> colorResourceIdsToColorValues,
62-
@StyleRes int theme);
63-
6449
/**
6550
* Wraps the given Context with the theme overlay where color resources are updated at runtime. If
6651
* not possible, the original Context will be returned.
@@ -73,21 +58,6 @@ boolean applyIfPossible(
7358
Context wrapContextIfPossible(
7459
@NonNull Context context, @NonNull Map<Integer, Integer> colorResourceIdsToColorValues);
7560

76-
/**
77-
* Wraps the given Context with the theme overlay where color resources are updated at runtime. If
78-
* not possible, the original Context will be returned.
79-
*
80-
* @param context The target context.
81-
* @param colorResourceIdsToColorValues The mapping from the color resources id to the updated
82-
* color value.
83-
* @param theme The resource ID of the theme overlay to be applied.
84-
*/
85-
@NonNull
86-
Context wrapContextIfPossible(
87-
@NonNull Context context,
88-
@NonNull Map<Integer, Integer> colorResourceIdsToColorValues,
89-
@StyleRes int theme);
90-
9161
@Nullable
9262
static ColorResourcesOverride getInstance() {
9363
if (VERSION_CODES.R <= VERSION.SDK_INT && VERSION.SDK_INT <= VERSION_CODES.TIRAMISU) {

lib/java/com/google/android/material/color/DynamicColors.java

Lines changed: 5 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@
3232
import androidx.annotation.ChecksSdkIntAtLeast;
3333
import androidx.annotation.NonNull;
3434
import androidx.annotation.Nullable;
35-
import androidx.annotation.RequiresApi;
3635
import androidx.annotation.StyleRes;
37-
import androidx.core.content.ContextCompat;
3836
import androidx.core.os.BuildCompat;
3937
import com.google.android.material.color.utilities.Hct;
4038
import com.google.android.material.color.utilities.SchemeContent;
41-
import com.google.android.material.resources.MaterialAttributes;
4239
import java.lang.reflect.Method;
4340
import java.util.Collections;
4441
import java.util.HashMap;
@@ -50,24 +47,6 @@ public class DynamicColors {
5047
private static final int[] DYNAMIC_COLOR_THEME_OVERLAY_ATTRIBUTE =
5148
new int[] {R.attr.dynamicColorThemeOverlay};
5249

53-
@RequiresApi(api = VERSION_CODES.S)
54-
private static final int[] SYSTEM_NEUTRAL_PALETTE_RES_IDS =
55-
new int[] {
56-
android.R.color.system_neutral1_0,
57-
android.R.color.system_neutral1_10,
58-
android.R.color.system_neutral1_50,
59-
android.R.color.system_neutral1_100,
60-
android.R.color.system_neutral1_200,
61-
android.R.color.system_neutral1_300,
62-
android.R.color.system_neutral1_400,
63-
android.R.color.system_neutral1_500,
64-
android.R.color.system_neutral1_600,
65-
android.R.color.system_neutral1_700,
66-
android.R.color.system_neutral1_800,
67-
android.R.color.system_neutral1_900,
68-
android.R.color.system_neutral1_1000,
69-
};
70-
7150
private static final DeviceSupportCondition DEFAULT_DEVICE_SUPPORT_CONDITION =
7251
new DeviceSupportCondition() {
7352
@Override
@@ -137,7 +116,6 @@ public boolean isSupported() {
137116
}
138117

139118
private static final int USE_DEFAULT_THEME_OVERLAY = 0;
140-
private static final int UPDATED_NEUTRAL_PALETTE_CHROMA = 6;
141119

142120
private DynamicColors() {}
143121

@@ -305,18 +283,16 @@ public static void applyToActivityIfAvailable(
305283
return;
306284
}
307285
// Set default theme overlay as 0, as it's not used in content-based dynamic colors.
308-
int themeOverlayResourceId = 0;
286+
int theme = 0;
309287
// Only retrieves the theme overlay if we're applying just dynamic colors.
310288
if (dynamicColorsOptions.getContentBasedSeedColor() == null) {
311-
themeOverlayResourceId =
289+
theme =
312290
dynamicColorsOptions.getThemeOverlay() == USE_DEFAULT_THEME_OVERLAY
313291
? getDefaultThemeOverlay(activity)
314292
: dynamicColorsOptions.getThemeOverlay();
315293
}
316294

317-
if (dynamicColorsOptions
318-
.getPrecondition()
319-
.shouldApplyDynamicColors(activity, themeOverlayResourceId)) {
295+
if (dynamicColorsOptions.getPrecondition().shouldApplyDynamicColors(activity, theme)) {
320296
// Applies content-based dynamic colors if content-based source is provided.
321297
if (dynamicColorsOptions.getContentBasedSeedColor() != null) {
322298
SchemeContent scheme =
@@ -334,9 +310,8 @@ public static void applyToActivityIfAvailable(
334310
return;
335311
}
336312
}
337-
} else if (!maybeApplyThemeOverlayWithUpdatedNeutralChroma(
338-
activity, themeOverlayResourceId)) {
339-
ThemeUtils.applyThemeOverlay(activity, themeOverlayResourceId);
313+
} else {
314+
ThemeUtils.applyThemeOverlay(activity, theme);
340315
}
341316
// Applies client's callback after content-based dynamic colors or just dynamic colors has
342317
// been applied.
@@ -412,16 +387,6 @@ public static Context wrapContextIfAvailable(
412387
originalContext,
413388
MaterialColorUtilitiesHelper.createColorResourcesIdsToColorValues(scheme));
414389
}
415-
} else {
416-
if (shouldOverrideNeutralChroma(originalContext)) {
417-
ColorResourcesOverride resourcesOverride = ColorResourcesOverride.getInstance();
418-
if (resourcesOverride != null) {
419-
return resourcesOverride.wrapContextIfPossible(
420-
originalContext,
421-
createColorResourcesIdsToColorValuesWithUpdatedChroma(originalContext),
422-
theme);
423-
}
424-
}
425390
}
426391
return new ContextThemeWrapper(originalContext, theme);
427392
}
@@ -453,57 +418,6 @@ private static int getDefaultThemeOverlay(@NonNull Context context) {
453418
return theme;
454419
}
455420

456-
@RequiresApi(api = VERSION_CODES.S)
457-
private static Map<Integer, Integer> createColorResourcesIdsToColorValuesWithUpdatedChroma(
458-
Context context) {
459-
Map<Integer, Integer> colorResourcesIdsToColorValues = new HashMap<>();
460-
for (int neutralResId : SYSTEM_NEUTRAL_PALETTE_RES_IDS) {
461-
Hct colorHct = Hct.fromInt(ContextCompat.getColor(context, neutralResId));
462-
colorHct.setChroma(UPDATED_NEUTRAL_PALETTE_CHROMA);
463-
colorResourcesIdsToColorValues.put(neutralResId, colorHct.toInt());
464-
}
465-
return colorResourcesIdsToColorValues;
466-
}
467-
468-
/**
469-
* Applies the theme overlay to the context with an updated neutral palette with chroma 6, if
470-
* possible. See {@link #shouldOverrideNeutralChroma(Context)} for when the neutral palettes
471-
* should be updated.
472-
*
473-
* @return Whether the theme overlay is applied with updated neutral palettes successfully.
474-
*/
475-
private static boolean maybeApplyThemeOverlayWithUpdatedNeutralChroma(
476-
@NonNull Context context, int themeOverlayResourceId) {
477-
if (shouldOverrideNeutralChroma(context)) {
478-
ColorResourcesOverride resourcesOverride = ColorResourcesOverride.getInstance();
479-
if (resourcesOverride != null) {
480-
return resourcesOverride.applyIfPossible(
481-
context,
482-
createColorResourcesIdsToColorValuesWithUpdatedChroma(context),
483-
themeOverlayResourceId);
484-
}
485-
}
486-
return false;
487-
}
488-
489-
/**
490-
* Checks whether the neutral palette should be overridden with chroma 6.
491-
*
492-
* @return True, if Android version is S or T and preUDynamicNeutralChromaUpdateEnabled is true in
493-
* current context.
494-
*/
495-
@ChecksSdkIntAtLeast(api = VERSION_CODES.S)
496-
private static boolean shouldOverrideNeutralChroma(@NonNull Context context) {
497-
// TODO(b/272585197) Remove after tonal surface migration is complete.
498-
boolean shouldUpdateNeutralChroma =
499-
MaterialAttributes.resolveBoolean(
500-
context, R.attr.preUDynamicNeutralChromaUpdateEnabled, /* defaultValue= */ false);
501-
// Update neutral palette chroma from 4 to 6 for backward compatibility.
502-
return VERSION.SDK_INT < VERSION_CODES.UPSIDE_DOWN_CAKE
503-
&& VERSION.SDK_INT >= VERSION_CODES.S
504-
&& shouldUpdateNeutralChroma;
505-
}
506-
507421
/** The interface that provides a precondition to decide if dynamic colors should be applied. */
508422
public interface Precondition {
509423

lib/java/com/google/android/material/color/ResourcesLoaderColorResourcesOverride.java

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import android.view.ContextThemeWrapper;
2525
import androidx.annotation.NonNull;
2626
import androidx.annotation.RequiresApi;
27-
import androidx.annotation.StyleRes;
2827
import java.util.Map;
2928

3029
/**
@@ -47,24 +46,8 @@ private ResourcesLoaderColorResourcesOverride() {}
4746
@Override
4847
public boolean applyIfPossible(
4948
Context context, Map<Integer, Integer> colorResourceIdsToColorValues) {
50-
return applyIfPossible(
51-
context, colorResourceIdsToColorValues, R.style.ThemeOverlay_Material3_PersonalizedColors);
52-
}
53-
54-
/**
55-
* Overrides the color resources to the given context, returns {@code true} if new color values
56-
* have been applied.
57-
*
58-
* @param context The target context.
59-
* @param colorResourceIdsToColorValues The mapping from the color resources id to the updated
60-
* color value.
61-
* @param theme The resource ID of the theme overlay to be applied.
62-
*/
63-
@Override
64-
public boolean applyIfPossible(
65-
Context context, Map<Integer, Integer> colorResourceIdsToColorValues, @StyleRes int theme) {
6649
if (ResourcesLoaderUtils.addResourcesLoaderToContext(context, colorResourceIdsToColorValues)) {
67-
ThemeUtils.applyThemeOverlay(context, theme);
50+
ThemeUtils.applyThemeOverlay(context, R.style.ThemeOverlay_Material3_PersonalizedColors);
6851
return true;
6952
}
7053
return false;
@@ -82,24 +65,8 @@ public boolean applyIfPossible(
8265
@NonNull
8366
public Context wrapContextIfPossible(
8467
Context context, Map<Integer, Integer> colorResourceIdsToColorValues) {
85-
return wrapContextIfPossible(
86-
context, colorResourceIdsToColorValues, R.style.ThemeOverlay_Material3_PersonalizedColors);
87-
}
88-
89-
/**
90-
* Wraps the given Context with the theme overlay where color resources are updated at runtime. If
91-
* not possible, the original Context will be returned.
92-
*
93-
* @param context The target context.
94-
* @param colorResourceIdsToColorValues The mapping from the color resources id to the updated
95-
* color value.
96-
* @param theme The resource ID of the theme overlay to be applied.
97-
*/
98-
@Override
99-
@NonNull
100-
public Context wrapContextIfPossible(
101-
Context context, Map<Integer, Integer> colorResourceIdsToColorValues, @StyleRes int theme) {
102-
ContextThemeWrapper themeWrapper = new ContextThemeWrapper(context, theme);
68+
ContextThemeWrapper themeWrapper =
69+
new ContextThemeWrapper(context, R.style.ThemeOverlay_Material3_PersonalizedColors);
10370
// Because ContextThemeWrapper does not provide a new set of resources, override config to
10471
// retrieve the new set of resources and to keep the original context's resources intact.
10572
themeWrapper.applyOverrideConfiguration(new Configuration());

lib/java/com/google/android/material/color/res/values/attrs.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@
168168
<!-- The default dynamic color theme overlay to use when applying dynamic colors. -->
169169
<attr name="dynamicColorThemeOverlay" format="reference"/>
170170

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

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

0 commit comments

Comments
 (0)