3232import androidx .annotation .ChecksSdkIntAtLeast ;
3333import androidx .annotation .NonNull ;
3434import androidx .annotation .Nullable ;
35- import androidx .annotation .RequiresApi ;
3635import androidx .annotation .StyleRes ;
37- import androidx .core .content .ContextCompat ;
3836import androidx .core .os .BuildCompat ;
3937import com .google .android .material .color .utilities .Hct ;
4038import com .google .android .material .color .utilities .SchemeContent ;
41- import com .google .android .material .resources .MaterialAttributes ;
4239import java .lang .reflect .Method ;
4340import java .util .Collections ;
4441import 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
0 commit comments