From 33e4f841b213fb306091971dd1d1025c8da897a7 Mon Sep 17 00:00:00 2001 From: conradchen Date: Tue, 21 Feb 2023 11:18:12 -0800 Subject: [PATCH] [Catalog][Menu] Fixes context menu are not themed on S On S, it seems like the framework has a different behavior of applying themes/styles on the decor view theme. Catalog's theme overlay applying logic didn't take care of the implication of the decor view theme, somehow this causes the Material themes are not applied to decor views at all, when a theme overlay is applied. We've solved the similar issue with the dynamic color implementation. Applies the same fix on catalog to fix this issue. Resolves https://github.com/material-components/material-components-android/issues/2682 PiperOrigin-RevId: 511254476 --- .../catalog/preferences/ThemeOverlayUtils.java | 4 +++- .../android/material/color/ThemeUtils.java | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/catalog/java/io/material/catalog/preferences/ThemeOverlayUtils.java b/catalog/java/io/material/catalog/preferences/ThemeOverlayUtils.java index 075828879c6..e0ded024e60 100644 --- a/catalog/java/io/material/catalog/preferences/ThemeOverlayUtils.java +++ b/catalog/java/io/material/catalog/preferences/ThemeOverlayUtils.java @@ -20,6 +20,7 @@ import android.util.SparseIntArray; import androidx.annotation.IdRes; import androidx.annotation.StyleRes; +import com.google.android.material.color.ThemeUtils; /** Utils for theme themeOverlays. */ public class ThemeOverlayUtils { @@ -51,9 +52,10 @@ public static int getThemeOverlay(@IdRes int id) { return themeOverlays.get(id); } + @SuppressWarnings("RestrictTo") public static void applyThemeOverlays(Activity activity) { for (int i = 0; i < themeOverlays.size(); ++i) { - activity.setTheme(themeOverlays.valueAt(i)); + ThemeUtils.applyThemeOverlay(activity, themeOverlays.valueAt(i)); } } } diff --git a/lib/java/com/google/android/material/color/ThemeUtils.java b/lib/java/com/google/android/material/color/ThemeUtils.java index f0073f4fbb4..10be154bfb8 100644 --- a/lib/java/com/google/android/material/color/ThemeUtils.java +++ b/lib/java/com/google/android/material/color/ThemeUtils.java @@ -16,6 +16,8 @@ package com.google.android.material.color; +import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP; + import android.app.Activity; import android.content.Context; import android.content.res.Resources.Theme; @@ -23,14 +25,22 @@ import android.view.Window; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.RestrictTo; import androidx.annotation.StyleRes; -/** Utility methods for theme. */ -final class ThemeUtils { +// TODO(b/269781013): move this class to internal folder, which involves resolving cyclic dependency +// between color and internal folders +/** + * Utility methods for theme. + * + * @hide + */ +@RestrictTo(LIBRARY_GROUP) +public final class ThemeUtils { private ThemeUtils() {} - static void applyThemeOverlay(@NonNull Context context, @StyleRes int theme) { + public static void applyThemeOverlay(@NonNull Context context, @StyleRes int theme) { // Use applyStyle() instead of setTheme() due to Force Dark issue. context.getTheme().applyStyle(theme, /* force= */ true);