Skip to content

Commit 8363cde

Browse files
committed
[Color] Deprecate previous apply...() APIs and add overload for activity apply method with no options
PiperOrigin-RevId: 435344858
1 parent e644a72 commit 8363cde

File tree

2 files changed

+57
-39
lines changed

2 files changed

+57
-39
lines changed

docs/theming/Color.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ calling the following method in your activities’ `onCreate()` method (or befor
221221
you inflate anything from it):
222222

223223
```
224-
DynamicColors.applyIfAvailable(this);
224+
DynamicColors.applyToActivityIfAvailable(this);
225225
```
226226

227227
If the app is running on Android S+, dynamic colors will be applied to the

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

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
/** Utility for applying dynamic colors to application/activities. */
4141
public class DynamicColors {
4242
private static final int[] DYNAMIC_COLOR_THEME_OVERLAY_ATTRIBUTE =
43-
new int[] { R.attr.dynamicColorThemeOverlay };
43+
new int[] {R.attr.dynamicColorThemeOverlay};
4444

4545
private static final DeviceSupportCondition DEFAULT_DEVICE_SUPPORT_CONDITION =
4646
new DeviceSupportCondition() {
@@ -115,8 +115,8 @@ private DynamicColors() {}
115115
* attribute {@code dynamicColorThemeOverlay} by registering a {@link ActivityLifecycleCallbacks}
116116
* to your application.
117117
*
118-
* @see #applyToActivitiesIfAvailable(Application, int, Precondition) for more detailed info and
119-
* examples.
118+
* @see #applyToActivitiesIfAvailable(Application, DynamicColorsOptions) for more detailed info
119+
* and examples.
120120
* @param application The target application.
121121
*/
122122
public static void applyToActivitiesIfAvailable(@NonNull Application application) {
@@ -127,11 +127,12 @@ public static void applyToActivitiesIfAvailable(@NonNull Application application
127127
* Applies dynamic colors to all activities with the given theme overlay by registering a {@link
128128
* ActivityLifecycleCallbacks} to your application.
129129
*
130-
* @see #applyToActivitiesIfAvailable(Application, int, Precondition) for more detailed info and
131-
* examples.
132130
* @param application The target application.
133131
* @param theme The resource ID of the theme overlay that provides dynamic color definition.
132+
* @deprecated Use {@link #applyToActivitiesIfAvailable(Application, DynamicColorsOptions)}
133+
* instead.
134134
*/
135+
@Deprecated
135136
public static void applyToActivitiesIfAvailable(
136137
@NonNull Application application, @StyleRes int theme) {
137138
applyToActivitiesIfAvailable(
@@ -143,11 +144,12 @@ public static void applyToActivitiesIfAvailable(
143144
* attribute {@code dynamicColorThemeOverlay} according to the given precondition by registering a
144145
* {@link ActivityLifecycleCallbacks} to your application.
145146
*
146-
* @see #applyToActivitiesIfAvailable(Application, int, Precondition) for more detailed info and
147-
* examples.
148147
* @param application The target application.
149148
* @param precondition The precondition to decide if dynamic colors should be applied.
149+
* @deprecated Use {@link #applyToActivitiesIfAvailable(Application, DynamicColorsOptions)}
150+
* instead.
150151
*/
152+
@Deprecated
151153
public static void applyToActivitiesIfAvailable(
152154
@NonNull Application application, @NonNull Precondition precondition) {
153155
applyToActivitiesIfAvailable(
@@ -158,6 +160,27 @@ public static void applyToActivitiesIfAvailable(
158160
* Applies dynamic colors to all activities with the given theme overlay according to the given
159161
* precondition by registering a {@link ActivityLifecycleCallbacks} to your application.
160162
*
163+
* @param application The target application.
164+
* @param theme The resource ID of the theme overlay that provides dynamic color definition.
165+
* @param precondition The precondition to decide if dynamic colors should be applied.
166+
* @deprecated Use {@link #applyToActivitiesIfAvailable(Application, DynamicColorsOptions)}
167+
* instead.
168+
*/
169+
@Deprecated
170+
public static void applyToActivitiesIfAvailable(
171+
@NonNull Application application, @StyleRes int theme, @NonNull Precondition precondition) {
172+
applyToActivitiesIfAvailable(
173+
application,
174+
new DynamicColorsOptions.Builder()
175+
.setThemeOverlay(theme)
176+
.setPrecondition(precondition)
177+
.build());
178+
}
179+
180+
/**
181+
* Applies dynamic colors to all activities based on the provided {@link DynamicColorsOptions}, by
182+
* registering a {@link ActivityLifecycleCallbacks} to your application.
183+
*
161184
* <p>A normal usage of this method should happen only once in {@link Application#onCreate()} or
162185
* any methods that run before any of your activities are created. For example:
163186
*
@@ -177,23 +200,6 @@ public static void applyToActivitiesIfAvailable(
177200
* overriding the colors or you may lose the dynamic color support.
178201
*
179202
* @param application The target application.
180-
* @param theme The resource ID of the theme overlay that provides dynamic color definition.
181-
* @param precondition The precondition to decide if dynamic colors should be applied.
182-
*/
183-
public static void applyToActivitiesIfAvailable(
184-
@NonNull Application application, @StyleRes int theme, @NonNull Precondition precondition) {
185-
applyToActivitiesIfAvailable(
186-
application,
187-
new DynamicColorsOptions.Builder()
188-
.setThemeOverlay(theme)
189-
.setPrecondition(precondition)
190-
.build());
191-
}
192-
193-
/**
194-
* Applies dynamic colors to the given application with {@link DynamicColorsOptions} provided.
195-
*
196-
* @param application The target application.
197203
* @param dynamicColorsOptions The dynamic colors options object that specifies the theme resource
198204
* ID, precondition to decide if dynamic colors should be applied and the callback function
199205
* for after dynamic colors have been applied.
@@ -209,17 +215,21 @@ public static void applyToActivitiesIfAvailable(
209215
* attribute {@code dynamicColorThemeOverlay}.
210216
*
211217
* @param activity The target activity.
218+
* @deprecated Use {@link #applyToActivityIfAvailable(Activity)} instead.
212219
*/
220+
@Deprecated
213221
public static void applyIfAvailable(@NonNull Activity activity) {
214-
applyToActivityIfAvailable(activity, new DynamicColorsOptions.Builder().build());
222+
applyToActivityIfAvailable(activity);
215223
}
216224

217225
/**
218226
* Applies dynamic colors to the given activity with the given theme overlay.
219227
*
220228
* @param activity The target activity.
221229
* @param theme The resource ID of the theme overlay that provides dynamic color definition.
230+
* @deprecated Use {@link #applyToActivityIfAvailable(Activity, DynamicColorsOptions)} instead.
222231
*/
232+
@Deprecated
223233
public static void applyIfAvailable(@NonNull Activity activity, @StyleRes int theme) {
224234
applyToActivityIfAvailable(
225235
activity, new DynamicColorsOptions.Builder().setThemeOverlay(theme).build());
@@ -231,13 +241,25 @@ public static void applyIfAvailable(@NonNull Activity activity, @StyleRes int th
231241
*
232242
* @param activity The target activity.
233243
* @param precondition The precondition to decide if dynamic colors should be applied.
244+
* @deprecated Use {@link #applyToActivityIfAvailable(Activity, DynamicColorsOptions)} instead.
234245
*/
246+
@Deprecated
235247
public static void applyIfAvailable(
236248
@NonNull Activity activity, @NonNull Precondition precondition) {
237249
applyToActivityIfAvailable(
238250
activity, new DynamicColorsOptions.Builder().setPrecondition(precondition).build());
239251
}
240252

253+
/**
254+
* Applies dynamic colors to the given activity.
255+
*
256+
* @param activity The target activity.
257+
* @see #applyToActivityIfAvailable(Activity, DynamicColorsOptions)
258+
*/
259+
public static void applyToActivityIfAvailable(@NonNull Activity activity) {
260+
applyToActivityIfAvailable(activity, new DynamicColorsOptions.Builder().build());
261+
}
262+
241263
/**
242264
* Applies dynamic colors to the given activity with {@link DynamicColorsOptions} provided.
243265
*
@@ -291,7 +313,7 @@ public static Context wrapContextIfAvailable(@NonNull Context originalContext) {
291313
* Wraps the given context with the given theme overlay. The returned context can be used to
292314
* create views with dynamic color support.
293315
*
294-
* If dynamic color support is not available, the original context will be returned.
316+
* <p>If dynamic color support is not available, the original context will be returned.
295317
*
296318
* @param originalContext The original context.
297319
* @param theme The resource ID of the theme overlay that provides dynamic color definition.
@@ -308,9 +330,7 @@ public static Context wrapContextIfAvailable(
308330
return theme == 0 ? originalContext : new ContextThemeWrapper(originalContext, theme);
309331
}
310332

311-
/**
312-
* Returns {@code true} if dynamic colors are available on the current SDK level.
313-
*/
333+
/** Returns {@code true} if dynamic colors are available on the current SDK level. */
314334
@SuppressLint("DefaultLocale")
315335
@ChecksSdkIntAtLeast(api = VERSION_CODES.S)
316336
public static boolean isDynamicColorAvailable() {
@@ -333,14 +353,12 @@ private static int getDefaultThemeOverlay(@NonNull Context context) {
333353
return theme;
334354
}
335355

336-
/**
337-
* The interface that provides a precondition to decide if dynamic colors should be applied.
338-
*/
356+
/** The interface that provides a precondition to decide if dynamic colors should be applied. */
339357
public interface Precondition {
340358

341359
/**
342-
* Return {@code true} if dynamic colors should be applied on the given activity with the
343-
* given theme overlay.
360+
* Return {@code true} if dynamic colors should be applied on the given activity with the given
361+
* theme overlay.
344362
*/
345363
boolean shouldApplyDynamicColors(@NonNull Activity activity, @StyleRes int theme);
346364
}
@@ -361,8 +379,8 @@ private static class DynamicColorsActivityLifecycleCallbacks
361379
}
362380

363381
@Override
364-
public void onActivityPreCreated(@NonNull Activity activity,
365-
@Nullable Bundle savedInstanceState) {
382+
public void onActivityPreCreated(
383+
@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
366384
applyToActivityIfAvailable(
367385
activity,
368386
dynamicColorsOptions.getThemeOverlay(),
@@ -371,8 +389,8 @@ public void onActivityPreCreated(@NonNull Activity activity,
371389
}
372390

373391
@Override
374-
public void onActivityCreated(@NonNull Activity activity,
375-
@Nullable Bundle savedInstanceState) {}
392+
public void onActivityCreated(
393+
@NonNull Activity activity, @Nullable Bundle savedInstanceState) {}
376394

377395
@Override
378396
public void onActivityStarted(@NonNull Activity activity) {}

0 commit comments

Comments
 (0)