From 753517700dc6ee95b9945a6c8857aa8c633cde2a Mon Sep 17 00:00:00 2001 From: Deepanshu Gupta Date: Fri, 28 Aug 2015 11:53:46 -0700 Subject: [PATCH] Fix android:theme parsing for custom views. Change-Id: Iba83ba1d2e4a96461cc298a759e32e4e51e311a0 --- .../src/android/view/BridgeInflater.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tools/layoutlib/bridge/src/android/view/BridgeInflater.java b/tools/layoutlib/bridge/src/android/view/BridgeInflater.java index 1e33e3ab2090c..f1726ebae4cb9 100644 --- a/tools/layoutlib/bridge/src/android/view/BridgeInflater.java +++ b/tools/layoutlib/bridge/src/android/view/BridgeInflater.java @@ -36,6 +36,7 @@ import org.xmlpull.v1.XmlPullParser; import android.annotation.NonNull; import android.content.Context; +import android.content.res.TypedArray; import android.util.AttributeSet; import java.io.File; @@ -54,6 +55,9 @@ public final class BridgeInflater extends LayoutInflater { private ResourceReference mResourceReference; private Map mOpenDrawerLayouts; + // Keep in sync with the same value in LayoutInflater. + private static final int[] ATTRS_THEME = new int[] {com.android.internal.R.attr.theme }; + /** * List of class prefixes which are tried first by default. *

@@ -135,11 +139,23 @@ public final class BridgeInflater extends LayoutInflater { @Override public View createViewFromTag(View parent, String name, Context context, AttributeSet attrs, - boolean ignoreThemeAttrs) { + boolean ignoreThemeAttr) { View view; try { - view = super.createViewFromTag(parent, name, context, attrs, ignoreThemeAttrs); + view = super.createViewFromTag(parent, name, context, attrs, ignoreThemeAttr); } catch (InflateException e) { + // Creation of ContextThemeWrapper code is same as in the super method. + // Apply a theme wrapper, if allowed and one is specified. + if (!ignoreThemeAttr) { + final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME); + final int themeResId = ta.getResourceId(0, 0); + if (themeResId != 0) { + context = new ContextThemeWrapper(context, themeResId); + } + ta.recycle(); + } + final Object lastContext = mConstructorArgs[0]; + mConstructorArgs[0] = context; // try to load the class from using the custom view loader try { view = loadCustomView(name, attrs); @@ -153,6 +169,8 @@ public final class BridgeInflater extends LayoutInflater { exception.initCause(e); } throw exception; + } finally { + mConstructorArgs[0] = lastContext; } }