From 04067f33c80b03eec74eee0e42c55ee40ce24f6a Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Tue, 25 Aug 2015 14:07:02 -0400 Subject: [PATCH] Clean up ContextThemeWrapper Fix trailing white space, use of final, comments. Change-Id: Iacc034acd46a651c35c32f2efbc0fe5fb58e1209 --- .../android/view/ContextThemeWrapper.java | 80 +++++++++++++------ 1 file changed, 54 insertions(+), 26 deletions(-) diff --git a/core/java/android/view/ContextThemeWrapper.java b/core/java/android/view/ContextThemeWrapper.java index 9611edb734887..ea0873d6fb1be 100644 --- a/core/java/android/view/ContextThemeWrapper.java +++ b/core/java/android/view/ContextThemeWrapper.java @@ -24,8 +24,8 @@ import android.content.res.Configuration; import android.content.res.Resources; /** - * A ContextWrapper that allows you to modify the theme from what is in the - * wrapped context. + * A context wrapper that allows you to modify or replace the theme of the + * wrapped context. */ public class ContextThemeWrapper extends ContextWrapper { private int mThemeResource; @@ -34,15 +34,42 @@ public class ContextThemeWrapper extends ContextWrapper { private Configuration mOverrideConfiguration; private Resources mResources; + /** + * Creates a new context wrapper with no theme and no base context. + *

+ * Note: A base context must be attached + * using {@link #attachBaseContext(Context)} before calling any other + * method on the newly constructed context wrapper. + */ public ContextThemeWrapper() { super(null); } + /** + * Creates a new context wrapper with the specified theme. + *

+ * The specified theme will be applied on top of the base context's theme. + * Any attributes not explicitly defined in the theme identified by + * themeResId will retain their original values. + * + * @param base the base context + * @param themeResId the resource ID of the theme to be applied on top of + * the base context's theme + */ public ContextThemeWrapper(Context base, @StyleRes int themeResId) { super(base); mThemeResource = themeResId; } + /** + * Creates a new context wrapper with the specified theme. + *

+ * Unlike {@link #ContextThemeWrapper(Context, int)}, the theme passed to + * this constructor will completely replace the base context's theme. + * + * @param base the base context + * @param theme the theme against which resources should be inflated + */ public ContextThemeWrapper(Context base, Resources.Theme theme) { super(base); mTheme = theme; @@ -82,19 +109,18 @@ public class ContextThemeWrapper extends ContextWrapper { @Override public Resources getResources() { - if (mResources != null) { - return mResources; - } - if (mOverrideConfiguration == null) { - mResources = super.getResources(); - return mResources; - } else { - Context resc = createConfigurationContext(mOverrideConfiguration); - mResources = resc.getResources(); - return mResources; + if (mResources == null) { + if (mOverrideConfiguration == null) { + mResources = super.getResources(); + } else { + final Context resContext = createConfigurationContext(mOverrideConfiguration); + mResources = resContext.getResources(); + } } + + return mResources; } - + @Override public void setTheme(int resid) { if (mThemeResource != resid) { @@ -102,14 +128,15 @@ public class ContextThemeWrapper extends ContextWrapper { initializeTheme(); } } - + /** @hide */ @Override public int getThemeResId() { return mThemeResource; } - @Override public Resources.Theme getTheme() { + @Override + public Resources.Theme getTheme() { if (mTheme != null) { return mTheme; } @@ -121,7 +148,8 @@ public class ContextThemeWrapper extends ContextWrapper { return mTheme; } - @Override public Object getSystemService(String name) { + @Override + public Object getSystemService(String name) { if (LAYOUT_INFLATER_SERVICE.equals(name)) { if (mInflater == null) { mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this); @@ -130,27 +158,27 @@ public class ContextThemeWrapper extends ContextWrapper { } return getBaseContext().getSystemService(name); } - + /** * Called by {@link #setTheme} and {@link #getTheme} to apply a theme - * resource to the current Theme object. Can override to change the - * default (simple) behavior. This method will not be called in multiple + * resource to the current Theme object. May be overridden to change the + * default (simple) behavior. This method will not be called in multiple * threads simultaneously. * - * @param theme The Theme object being modified. - * @param resid The theme style resource being applied to theme. - * @param first Set to true if this is the first time a style is being - * applied to theme. + * @param theme the theme being modified + * @param resId the style resource being applied to theme + * @param first {@code true} if this is the first time a style is being + * applied to theme */ - protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) { - theme.applyStyle(resid, true); + protected void onApplyThemeResource(Resources.Theme theme, int resId, boolean first) { + theme.applyStyle(resId, true); } private void initializeTheme() { final boolean first = mTheme == null; if (first) { mTheme = getResources().newTheme(); - Resources.Theme theme = getBaseContext().getTheme(); + final Resources.Theme theme = getBaseContext().getTheme(); if (theme != null) { mTheme.setTo(theme); }