2222import android .app .Application ;
2323import android .app .Application .ActivityLifecycleCallbacks ;
2424import android .content .Context ;
25- import android .content .res .Resources .Theme ;
2625import android .content .res .TypedArray ;
2726import android .os .Build ;
2827import android .os .Build .VERSION ;
2928import android .os .Build .VERSION_CODES ;
3029import android .os .Bundle ;
3130import android .view .ContextThemeWrapper ;
32- import android .view .View ;
33- import android .view .Window ;
3431import androidx .annotation .ChecksSdkIntAtLeast ;
3532import androidx .annotation .NonNull ;
3633import androidx .annotation .Nullable ;
4037import java .util .HashMap ;
4138import java .util .Map ;
4239
43- /**
44- * Utility for applying dynamic colors to application/activities.
45- */
40+ /** Utility for applying dynamic colors to application/activities. */
4641public class DynamicColors {
4742 private static final int [] DYNAMIC_COLOR_THEME_OVERLAY_ATTRIBUTE =
4843 new int [] { R .attr .dynamicColorThemeOverlay };
@@ -127,12 +122,11 @@ public static void applyToActivitiesIfAvailable(@NonNull Application application
127122 }
128123
129124 /**
130- * Applies dynamic colors to all activities with the given theme overlay by registering a
131- * {@link ActivityLifecycleCallbacks} to your application.
125+ * Applies dynamic colors to all activities with the given theme overlay by registering a {@link
126+ * ActivityLifecycleCallbacks} to your application.
132127 *
133128 * @see #applyToActivitiesIfAvailable(Application, int, Precondition) for more detailed info and
134- * examples.
135- *
129+ * examples.
136130 * @param application The target application.
137131 * @param theme The resource ID of the theme overlay that provides dynamic color definition.
138132 */
@@ -161,8 +155,9 @@ public static void applyToActivitiesIfAvailable(
161155 * Applies dynamic colors to all activities with the given theme overlay according to the given
162156 * precondition by registering a {@link ActivityLifecycleCallbacks} to your application.
163157 *
164- * A normal usage of this method should happen only once in {@link Application#onCreate()} or any
165- * methods that run before any of your activities are created. For example:
158+ * <p>A normal usage of this method should happen only once in {@link Application#onCreate()} or
159+ * any methods that run before any of your activities are created. For example:
160+ *
166161 * <pre>
167162 * public class YourApplication extends Application {
168163 * @Override
@@ -172,9 +167,10 @@ public static void applyToActivitiesIfAvailable(
172167 * }
173168 * }
174169 * </pre>
175- * This method will try to apply the given dynamic color theme overlay in every activity's
176- * {@link ActivityLifecycleCallbacks#onActivityPreCreated(Activity, Bundle)} callback. Therefore,
177- * if you are applying any other theme overlays after that, you will need to be careful about not
170+ *
171+ * This method will try to apply the given dynamic color theme overlay in every activity's {@link
172+ * ActivityLifecycleCallbacks#onActivityPreCreated(Activity, Bundle)} callback. Therefore, if you
173+ * are applying any other theme overlays after that, you will need to be careful about not
178174 * overriding the colors or you may lose the dynamic color support.
179175 *
180176 * @param application The target application.
@@ -261,7 +257,7 @@ private static void applyIfAvailable(
261257 theme = getDefaultThemeOverlay (activity );
262258 }
263259 if (theme != 0 && precondition .shouldApplyDynamicColors (activity , theme )) {
264- applyDynamicColorThemeOverlay (activity , theme );
260+ ThemeUtils . applyThemeOverlay (activity , theme );
265261 onAppliedCallback .onApplied (activity );
266262 }
267263 }
@@ -327,34 +323,6 @@ private static int getDefaultThemeOverlay(@NonNull Context context) {
327323 return theme ;
328324 }
329325
330- private static void applyDynamicColorThemeOverlay (Activity activity , @ StyleRes int theme ) {
331- // Use applyStyle() instead of setTheme() due to Force Dark issue.
332- activity .getTheme ().applyStyle (theme , /* force= */ true );
333-
334- // Make sure theme is applied to the Window decorView similar to Activity#setTheme, to ensure
335- // that the dynamic colors will be applied to things like ContextMenu using the DecorContext.
336- Theme windowDecorViewTheme = getWindowDecorViewTheme (activity );
337- if (windowDecorViewTheme != null ) {
338- windowDecorViewTheme .applyStyle (theme , /* force= */ true );
339- }
340- }
341-
342- @ Nullable
343- private static Theme getWindowDecorViewTheme (@ NonNull Activity activity ) {
344- Window window = activity .getWindow ();
345- if (window != null ) {
346- // Use peekDecorView() instead of getDecorView() to avoid locking the Window.
347- View decorView = window .peekDecorView ();
348- if (decorView != null ) {
349- Context context = decorView .getContext ();
350- if (context != null ) {
351- return context .getTheme ();
352- }
353- }
354- }
355- return null ;
356- }
357-
358326 /**
359327 * The interface that provides a precondition to decide if dynamic colors should be applied.
360328 */
0 commit comments