From eb26f5cecd5366b29b3cb746a7edaf3769f40480 Mon Sep 17 00:00:00 2001 From: Deepanshu Gupta Date: Wed, 26 Aug 2015 11:54:50 -0700 Subject: LayoutLib: Fix typedArray caching. The typedArray depends on the current context (more specifically, the themes set on the context). The fact was ignored in the caching of the typedArray and caused the android:theme attribute to be ignored in certain cases. Change-Id: If095580919474f12c0eb4e1f8fb7f076cf3c4ed0 --- .../layoutlib/bridge/android/BridgeContext.java | 46 +++++++++++++--------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'tools') diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java index 7fff0e0..9555e9e 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java @@ -36,8 +36,8 @@ import com.android.util.Pair; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; -import android.annotation.Nullable; import android.annotation.NonNull; +import android.annotation.Nullable; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.ContentResolver; @@ -136,8 +136,9 @@ public final class BridgeContext extends Context { private Map mStyleToDynamicIdMap; private int mDynamicIdGenerator = 0x02030000; // Base id for R.style in custom namespace - // cache for TypedArray generated from IStyleResourceValue object - private Map> mTypedArrayCache; + // cache for TypedArray generated from StyleResourceValue object + private Map, Map>> + mTypedArrayCache; private BridgeInflater mBridgeInflater; private BridgeContentResolver mContentResolver; @@ -588,31 +589,38 @@ public final class BridgeContext extends Context { } } + // The map is from + // attrs (int[]) -> context's current themes (List) -> resid (int) -> typed array. if (mTypedArrayCache == null) { - mTypedArrayCache = new HashMap>(); - - Map map = new HashMap(); - mTypedArrayCache.put(attrs, map); - - BridgeTypedArray ta = createStyleBasedTypedArray(style, attrs); - map.put(resid, ta); - - return ta; + mTypedArrayCache = new IdentityHashMap, Map>>(); } // get the 2nd map - Map map = mTypedArrayCache.get(attrs); - if (map == null) { - map = new HashMap(); - mTypedArrayCache.put(attrs, map); + Map, Map> map2 = + mTypedArrayCache.get(attrs); + if (map2 == null) { + map2 = new HashMap, Map>(); + mTypedArrayCache.put(attrs, map2); + } + + // get the 3rd map + List currentThemes = mRenderResources.getAllThemes(); + Map map3 = map2.get(currentThemes); + if (map3 == null) { + map3 = new HashMap(); + // Create a copy of the list before adding it to the map. This allows reusing the + // existing list. + currentThemes = new ArrayList(currentThemes); + map2.put(currentThemes, map3); } - // get the array from the 2nd map - BridgeTypedArray ta = map.get(resid); + // get the array from the 3rd map + BridgeTypedArray ta = map3.get(resid); if (ta == null) { ta = createStyleBasedTypedArray(style, attrs); - map.put(resid, ta); + map3.put(resid, ta); } return ta; -- cgit v1.1