Merge "Make Preference-cookie map non static." into lmp-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
d20a518c82
@@ -16,35 +16,36 @@
|
|||||||
|
|
||||||
package android.preference;
|
package android.preference;
|
||||||
|
|
||||||
|
import com.android.layoutlib.bridge.android.BridgeContext;
|
||||||
import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
|
import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class BridgePreferenceInflater extends PreferenceInflater {
|
public class BridgePreferenceInflater extends PreferenceInflater {
|
||||||
|
|
||||||
private final Map<Preference, Object> mViewCookieMap;
|
public BridgePreferenceInflater(Context context, PreferenceManager preferenceManager) {
|
||||||
|
|
||||||
public BridgePreferenceInflater(Context context, PreferenceManager preferenceManager,
|
|
||||||
Map<Preference, Object> viewCookieMap) {
|
|
||||||
super(context, preferenceManager);
|
super(context, preferenceManager);
|
||||||
mViewCookieMap = viewCookieMap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Preference onCreateItem(String name, AttributeSet attrs)
|
protected Preference onCreateItem(String name, AttributeSet attrs)
|
||||||
throws ClassNotFoundException {
|
throws ClassNotFoundException {
|
||||||
Object viewKey;
|
Object viewKey = null;
|
||||||
|
BridgeContext bc = null;
|
||||||
|
|
||||||
|
Context context = getContext();
|
||||||
|
if (context instanceof BridgeContext) {
|
||||||
|
bc = (BridgeContext) context;
|
||||||
|
}
|
||||||
if (attrs instanceof BridgeXmlBlockParser) {
|
if (attrs instanceof BridgeXmlBlockParser) {
|
||||||
viewKey = ((BridgeXmlBlockParser) attrs).getViewCookie();
|
viewKey = ((BridgeXmlBlockParser) attrs).getViewCookie();
|
||||||
} else {
|
|
||||||
viewKey = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Preference preference = super.onCreateItem(name, attrs);
|
Preference preference = super.onCreateItem(name, attrs);
|
||||||
if (viewKey != null) {
|
|
||||||
mViewCookieMap.put(preference, viewKey);
|
if (viewKey != null && bc != null) {
|
||||||
|
bc.addCookie(preference, viewKey);
|
||||||
}
|
}
|
||||||
return preference;
|
return preference;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,29 +40,27 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public class Preference_Delegate {
|
public class Preference_Delegate {
|
||||||
|
|
||||||
private static final Map<Preference, Object> sViewCookies = new HashMap<Preference, Object>();
|
|
||||||
|
|
||||||
@LayoutlibDelegate
|
@LayoutlibDelegate
|
||||||
/*package*/ static View getView(Preference pref, View convertView, ViewGroup parent) {
|
/*package*/ static View getView(Preference pref, View convertView, ViewGroup parent) {
|
||||||
Context context = pref.getContext();
|
Context context = pref.getContext();
|
||||||
BridgeContext bc = context instanceof BridgeContext ? ((BridgeContext) context) : null;
|
BridgeContext bc = context instanceof BridgeContext ? ((BridgeContext) context) : null;
|
||||||
convertView = pref.getView_Original(convertView, parent);
|
convertView = pref.getView_Original(convertView, parent);
|
||||||
Object cookie = sViewCookies.get(pref);
|
if (bc != null) {
|
||||||
if (bc != null && cookie != null) {
|
Object cookie = bc.getCookie(pref);
|
||||||
|
if (cookie != null) {
|
||||||
bc.addViewKey(convertView, cookie);
|
bc.addViewKey(convertView, cookie);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return convertView;
|
return convertView;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inflates the parser and returns the ListView containing the Preferences. The caller must call
|
* Inflates the parser and returns the ListView containing the Preferences.
|
||||||
* {@link #clearCookiesMap()} when the rendering is complete.
|
|
||||||
*/
|
*/
|
||||||
public static View inflatePreference(Context context, XmlPullParser parser, ViewGroup root) {
|
public static View inflatePreference(Context context, XmlPullParser parser, ViewGroup root) {
|
||||||
assert sViewCookies.isEmpty();
|
|
||||||
PreferenceManager pm = new PreferenceManager(context);
|
PreferenceManager pm = new PreferenceManager(context);
|
||||||
PreferenceScreen ps = pm.getPreferenceScreen();
|
PreferenceScreen ps = pm.getPreferenceScreen();
|
||||||
PreferenceInflater inflater = new BridgePreferenceInflater(context, pm, sViewCookies);
|
PreferenceInflater inflater = new BridgePreferenceInflater(context, pm);
|
||||||
ps = (PreferenceScreen) inflater.inflate(parser, ps, true);
|
ps = (PreferenceScreen) inflater.inflate(parser, ps, true);
|
||||||
ListView preferenceView = createContainerView(context, root);
|
ListView preferenceView = createContainerView(context, root);
|
||||||
ps.bind(preferenceView);
|
ps.bind(preferenceView);
|
||||||
@@ -82,8 +80,4 @@ public class Preference_Delegate {
|
|||||||
|
|
||||||
return (ListView) root.findViewById(android.R.id.list);
|
return (ListView) root.findViewById(android.R.id.list);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void clearCookiesMap() {
|
|
||||||
sViewCookies.clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,8 +92,15 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public final class BridgeContext extends Context {
|
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>();
|
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 Object mProjectKey;
|
||||||
private final DisplayMetrics mMetrics;
|
private final DisplayMetrics mMetrics;
|
||||||
private final RenderResources mRenderResources;
|
private final RenderResources mRenderResources;
|
||||||
@@ -190,6 +197,14 @@ public final class BridgeContext extends Context {
|
|||||||
return mViewKeyMap.get(view);
|
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() {
|
public Object getProjectKey() {
|
||||||
return mProjectKey;
|
return mProjectKey;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -599,9 +599,6 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
|
|||||||
mSystemViewInfoList = visitAllChildren(mViewRoot, 0, params.getExtendedViewInfoMode(),
|
mSystemViewInfoList = visitAllChildren(mViewRoot, 0, params.getExtendedViewInfoMode(),
|
||||||
false);
|
false);
|
||||||
|
|
||||||
// clear the preferences cookie map.
|
|
||||||
Preference_Delegate.clearCookiesMap();
|
|
||||||
|
|
||||||
// success!
|
// success!
|
||||||
return SUCCESS.createResult();
|
return SUCCESS.createResult();
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user