diff options
4 files changed, 36 insertions, 29 deletions
diff --git a/tools/layoutlib/bridge/src/android/preference/BridgePreferenceInflater.java b/tools/layoutlib/bridge/src/android/preference/BridgePreferenceInflater.java index c2122f41644d..4f00b5da08a8 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 37a0adc2ee6a..49ee6426acae 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 @@ import java.util.Map; */ 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 @@ public class Preference_Delegate { 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 5c613823a2b0..3496d844c9d4 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 @@ -93,8 +93,15 @@ import java.util.Map; */ 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; @@ -191,6 +198,14 @@ public final class BridgeContext extends Context { 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 c8e8ba76b987..daf82fc0af4e 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 @@ public class RenderSessionImpl extends RenderAction<SessionParams> { mSystemViewInfoList = visitAllChildren(mViewRoot, 0, params.getExtendedViewInfoMode(), false); - // clear the preferences cookie map. - Preference_Delegate.clearCookiesMap(); - // success! return SUCCESS.createResult(); } catch (Throwable e) { |