am d20a518c: Merge "Make Preference-cookie map non static." into lmp-dev

* commit 'd20a518c82e5e33d33e3dc5d74bf3c17d807f7d7':
  Make Preference-cookie map non static.
diff --git a/tools/layoutlib/bridge/src/android/preference/BridgePreferenceInflater.java b/tools/layoutlib/bridge/src/android/preference/BridgePreferenceInflater.java
index c2122f4..4f00b5d 100644
--- a/tools/layoutlib/bridge/src/android/preference/BridgePreferenceInflater.java
+++ b/tools/layoutlib/bridge/src/android/preference/BridgePreferenceInflater.java
@@ -16,35 +16,36 @@
 
 package android.preference;
 
+import com.android.layoutlib.bridge.android.BridgeContext;
 import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
 
 import android.content.Context;
 import android.util.AttributeSet;
 
-import java.util.Map;
-
 public class BridgePreferenceInflater extends PreferenceInflater {
 
-    private final Map<Preference, Object> mViewCookieMap;
-
-    public BridgePreferenceInflater(Context context, PreferenceManager preferenceManager,
-            Map<Preference, Object> viewCookieMap) {
+    public BridgePreferenceInflater(Context context, PreferenceManager preferenceManager) {
         super(context, preferenceManager);
-        mViewCookieMap = viewCookieMap;
     }
 
     @Override
     protected Preference onCreateItem(String name, AttributeSet attrs)
             throws ClassNotFoundException {
-        Object viewKey;
+        Object viewKey = null;
+        BridgeContext bc = null;
+
+        Context context = getContext();
+        if (context instanceof BridgeContext) {
+            bc = (BridgeContext) context;
+        }
         if (attrs instanceof BridgeXmlBlockParser) {
             viewKey = ((BridgeXmlBlockParser) attrs).getViewCookie();
-        } else {
-            viewKey = null;
         }
+
         Preference preference = super.onCreateItem(name, attrs);
-        if (viewKey != null) {
-            mViewCookieMap.put(preference, viewKey);
+
+        if (viewKey != null && bc != null) {
+            bc.addCookie(preference, viewKey);
         }
         return preference;
     }
diff --git a/tools/layoutlib/bridge/src/android/preference/Preference_Delegate.java b/tools/layoutlib/bridge/src/android/preference/Preference_Delegate.java
index 37a0adc..49ee642 100644
--- a/tools/layoutlib/bridge/src/android/preference/Preference_Delegate.java
+++ b/tools/layoutlib/bridge/src/android/preference/Preference_Delegate.java
@@ -40,29 +40,27 @@
  */
 public class Preference_Delegate {
 
-    private static final Map<Preference, Object> sViewCookies = new HashMap<Preference, Object>();
-
     @LayoutlibDelegate
     /*package*/ static View getView(Preference pref, View convertView, ViewGroup parent) {
         Context context = pref.getContext();
         BridgeContext bc = context instanceof BridgeContext ? ((BridgeContext) context) : null;
         convertView = pref.getView_Original(convertView, parent);
-        Object cookie = sViewCookies.get(pref);
-        if (bc != null && cookie != null) {
-            bc.addViewKey(convertView, cookie);
+        if (bc != null) {
+            Object cookie = bc.getCookie(pref);
+            if (cookie != null) {
+                bc.addViewKey(convertView, cookie);
+            }
         }
         return convertView;
     }
 
     /**
-     * Inflates the parser and returns the ListView containing the Preferences. The caller must call
-     * {@link #clearCookiesMap()} when the rendering is complete.
+     * Inflates the parser and returns the ListView containing the Preferences.
      */
     public static View inflatePreference(Context context, XmlPullParser parser, ViewGroup root) {
-        assert sViewCookies.isEmpty();
         PreferenceManager pm = new PreferenceManager(context);
         PreferenceScreen ps = pm.getPreferenceScreen();
-        PreferenceInflater inflater = new BridgePreferenceInflater(context, pm, sViewCookies);
+        PreferenceInflater inflater = new BridgePreferenceInflater(context, pm);
         ps = (PreferenceScreen) inflater.inflate(parser, ps, true);
         ListView preferenceView = createContainerView(context, root);
         ps.bind(preferenceView);
@@ -82,8 +80,4 @@
 
         return (ListView) root.findViewById(android.R.id.list);
     }
-
-    public static void clearCookiesMap() {
-        sViewCookies.clear();
-    }
 }
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 edc3c38..c41ebb9 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
@@ -92,8 +92,15 @@
  */
 public final class BridgeContext extends Context {
 
-    private Resources mSystemResources;
+    /** The map adds cookies to each view so that IDE can link xml tags to views. */
     private final HashMap<View, Object> mViewKeyMap = new HashMap<View, Object>();
+    /**
+     * In some cases, when inflating an xml, some objects are created. Then later, the objects are
+     * converted to views. This map stores the mapping from objects to cookies which can then be
+     * used to populate the mViewKeyMap.
+     */
+    private final HashMap<Object, Object> mViewKeyHelpMap = new HashMap<Object, Object>();
+    private Resources mSystemResources;
     private final Object mProjectKey;
     private final DisplayMetrics mMetrics;
     private final RenderResources mRenderResources;
@@ -190,6 +197,14 @@
         return mViewKeyMap.get(view);
     }
 
+    public void addCookie(Object o, Object cookie) {
+        mViewKeyHelpMap.put(o, cookie);
+    }
+
+    public Object getCookie(Object o) {
+        return mViewKeyHelpMap.get(o);
+    }
+
     public Object getProjectKey() {
         return mProjectKey;
     }
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
index c8e8ba7..daf82fc 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
@@ -599,9 +599,6 @@
             mSystemViewInfoList = visitAllChildren(mViewRoot, 0, params.getExtendedViewInfoMode(),
                     false);
 
-            // clear the preferences cookie map.
-            Preference_Delegate.clearCookiesMap();
-
             // success!
             return SUCCESS.createResult();
         } catch (Throwable e) {