From 6f714ecc4fa0cb7c1f33e04e30ef06bcb905eca7 Mon Sep 17 00:00:00 2001 From: cketcham Date: Fri, 27 Jul 2018 07:20:19 -0700 Subject: [PATCH] Update ThemeEnforcement context to read only the android:theme attribute from the style PiperOrigin-RevId: 206313825 --- .../material/internal/ThemeEnforcement.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/java/com/google/android/material/internal/ThemeEnforcement.java b/lib/java/com/google/android/material/internal/ThemeEnforcement.java index b78fe1bb3..3990fc942 100644 --- a/lib/java/com/google/android/material/internal/ThemeEnforcement.java +++ b/lib/java/com/google/android/material/internal/ThemeEnforcement.java @@ -231,10 +231,6 @@ public final class ThemeEnforcement { * defStyleAttr to use a theme overlay if one has been defined. */ public static Context createThemedContext(Context context, int defStyleAttr) { - if (context instanceof ContextThemeWrapper) { - return context; - } - TypedArray a = context.getTheme().obtainStyledAttributes(new int[] {defStyleAttr}); int style = a.getResourceId(0 /* index */, 0 /* defaultVal */); a.recycle(); @@ -244,6 +240,16 @@ public final class ThemeEnforcement { return context; } - return new ContextThemeWrapper(context, style); + a = context.obtainStyledAttributes(style, new int[] {android.R.attr.theme}); + int themeId = a.getResourceId(0 /* index */, 0 /* defaultVal */); + a.recycle(); + + if (themeId != 0 && (!(context instanceof ContextThemeWrapper) + || ((ContextThemeWrapper) context).getThemeResId() != themeId)) { + // If the context isn't a ContextThemeWrapper, or it is but does not have + // the same theme as we need, wrap it in a new wrapper + context = new ContextThemeWrapper(context, themeId); + } + return context; } }