aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Tsai <chartsai@google.com>2017-05-18 15:38:22 +0100
committerCharlie Tsai <chartsai@google.com>2017-05-18 15:38:22 +0100
commitbc876628ee6b96164b91908ff62fcaefe930f403 (patch)
tree5e703ccf9d9fb135c9db04d42f91e80a011cc0ce
parent63d675501e665c311f43a4777bea68c50e15e4ed (diff)
downloadlayoutlib-bc876628ee6b96164b91908ff62fcaefe930f403.tar.gz
Support reference type for BridgeTypedArray.getValue()
Test: N/A (indirectly tested by rendering tests) Change-Id: I7f90b080afe10f41ce695276f37dc8e298132dcd
-rw-r--r--bridge/src/android/content/res/BridgeTypedArray.java12
-rw-r--r--bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java56
2 files changed, 45 insertions, 23 deletions
diff --git a/bridge/src/android/content/res/BridgeTypedArray.java b/bridge/src/android/content/res/BridgeTypedArray.java
index b5996afd79..69242ac170 100644
--- a/bridge/src/android/content/res/BridgeTypedArray.java
+++ b/bridge/src/android/content/res/BridgeTypedArray.java
@@ -70,6 +70,7 @@ public final class BridgeTypedArray extends TypedArray {
private final BridgeContext mContext;
private final boolean mPlatformFile;
+ private final int[] mResourceId;
private final ResourceValue[] mResourceData;
private final String[] mNames;
private final boolean[] mIsFramework;
@@ -85,6 +86,7 @@ public final class BridgeTypedArray extends TypedArray {
mBridgeResources = resources;
mContext = context;
mPlatformFile = platformFile;
+ mResourceId = new int[len];
mResourceData = new ResourceValue[len];
mNames = new String[len];
mIsFramework = new boolean[len];
@@ -95,9 +97,12 @@ public final class BridgeTypedArray extends TypedArray {
* @param index the index of the value in the TypedArray
* @param name the name of the attribute
* @param isFramework whether the attribute is in the android namespace.
+ * @param resourceId the reference id of this resource
* @param value the value of the attribute
*/
- public void bridgeSetValue(int index, String name, boolean isFramework, ResourceValue value) {
+ public void bridgeSetValue(int index, String name, boolean isFramework, int resourceId,
+ ResourceValue value) {
+ mResourceId[index] = resourceId;
mResourceData[index] = value;
mNames[index] = name;
mIsFramework[index] = isFramework;
@@ -105,7 +110,7 @@ public final class BridgeTypedArray extends TypedArray {
/**
* Seals the array after all calls to
- * {@link #bridgeSetValue(int, String, boolean, ResourceValue)} have been done.
+ * {@link #bridgeSetValue(int, String, boolean, int, ResourceValue)} have been done.
* <p/>This allows to compute the list of non default values, permitting
* {@link #getIndexCount()} to return the proper value.
*/
@@ -788,6 +793,9 @@ public final class BridgeTypedArray extends TypedArray {
case TYPE_STRING:
outValue.string = getString(index);
return true;
+ case TYPE_REFERENCE:
+ outValue.resourceId = mResourceId[index];
+ return true;
default:
// For back-compatibility, parse as float.
String s = getString(index);
diff --git a/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java b/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
index 8bd924ea9c..004972d16e 100644
--- a/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
+++ b/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
@@ -750,7 +750,7 @@ public class BridgeContext extends Context {
return null;
}
- List<Pair<String, Boolean>> attributeList = searchAttrs(attrs);
+ List<AttributeHolder> attributeList = searchAttrs(attrs);
BridgeTypedArray ta =
Resources_Delegate.newTypeArray(mSystemResources, attrs.length, isPlatformFile);
@@ -866,14 +866,14 @@ public class BridgeContext extends Context {
if (attributeList != null) {
for (int index = 0 ; index < attributeList.size() ; index++) {
- Pair<String, Boolean> attribute = attributeList.get(index);
+ AttributeHolder attributeHolder = attributeList.get(index);
- if (attribute == null) {
+ if (attributeHolder == null) {
continue;
}
- String attrName = attribute.getFirst();
- boolean frameworkAttr = attribute.getSecond();
+ String attrName = attributeHolder.name;
+ boolean frameworkAttr = attributeHolder.isFramework;
String value = null;
if (set != null) {
value = set.getAttributeValue(
@@ -965,11 +965,12 @@ public class BridgeContext extends Context {
}
}
- ta.bridgeSetValue(index, attrName, frameworkAttr, defaultValue);
+ ta.bridgeSetValue(index, attrName, frameworkAttr, attributeHolder.resourceId,
+ defaultValue);
} else {
// there is a value in the XML, but we need to resolve it in case it's
// referencing another resource or a theme value.
- ta.bridgeSetValue(index, attrName, frameworkAttr,
+ ta.bridgeSetValue(index, attrName, frameworkAttr, attributeHolder.resourceId,
mRenderResources.resolveValue(null, attrName, value, isPlatformFile));
}
}
@@ -1013,7 +1014,7 @@ public class BridgeContext extends Context {
*/
private Pair<BridgeTypedArray, PropertiesMap> createStyleBasedTypedArray(
@Nullable StyleResourceValue style, int[] attrs) throws Resources.NotFoundException {
- List<Pair<String, Boolean>> attributes = searchAttrs(attrs);
+ List<AttributeHolder> attributes = searchAttrs(attrs);
BridgeTypedArray ta =
Resources_Delegate.newTypeArray(mSystemResources, attrs.length, false);
@@ -1021,13 +1022,13 @@ public class BridgeContext extends Context {
PropertiesMap defaultPropMap = new PropertiesMap();
// for each attribute, get its name so that we can search it in the style
for (int i = 0; i < attrs.length; i++) {
- Pair<String, Boolean> attribute = attributes.get(i);
+ AttributeHolder attrHolder = attributes.get(i);
- if (attribute != null) {
+ if (attrHolder != null) {
// look for the value in the given style
ResourceValue resValue;
- String attrName = attribute.getFirst();
- boolean frameworkAttr = attribute.getSecond();
+ String attrName = attrHolder.name;
+ boolean frameworkAttr = attrHolder.isFramework;
if (style != null) {
resValue = mRenderResources.findItemInStyle(style, attrName, frameworkAttr);
} else {
@@ -1039,7 +1040,8 @@ public class BridgeContext extends Context {
String preResolve = resValue.getValue();
// resolve it to make sure there are no references left.
resValue = mRenderResources.resolveResValue(resValue);
- ta.bridgeSetValue(i, attrName, frameworkAttr, resValue);
+ ta.bridgeSetValue(i, attrName, frameworkAttr, attrHolder.resourceId,
+ resValue);
defaultPropMap.put(
frameworkAttr ? SdkConstants.ANDROID_PREFIX + attrName : attrName,
new Property(preResolve, resValue.getValue()));
@@ -1053,28 +1055,28 @@ public class BridgeContext extends Context {
}
/**
- * The input int[] attrs is a list of attributes. The returns a list of information about
+ * The input int[] attributeIds is a list of attributes. The returns a list of information about
* each attributes. The information is (name, isFramework)
* <p/>
*
- * @param attrs An attribute array reference given to obtainStyledAttributes.
+ * @param attributeIds An attribute array reference given to obtainStyledAttributes.
* @return List of attribute information.
*/
- private List<Pair<String, Boolean>> searchAttrs(int[] attrs) {
- List<Pair<String, Boolean>> results = new ArrayList<>(attrs.length);
+ private List<AttributeHolder> searchAttrs(int[] attributeIds) {
+ List<AttributeHolder> results = new ArrayList<>(attributeIds.length);
// for each attribute, get its name so that we can search it in the style
- for (int attr : attrs) {
- Pair<ResourceType, String> resolvedResource = Bridge.resolveResourceId(attr);
+ for (int id : attributeIds) {
+ Pair<ResourceType, String> resolvedResource = Bridge.resolveResourceId(id);
boolean isFramework = false;
if (resolvedResource != null) {
isFramework = true;
} else {
- resolvedResource = mLayoutlibCallback.resolveResourceId(attr);
+ resolvedResource = mLayoutlibCallback.resolveResourceId(id);
}
if (resolvedResource != null) {
- results.add(Pair.of(resolvedResource.getSecond(), isFramework));
+ results.add(new AttributeHolder(id, resolvedResource.getSecond(), isFramework));
} else {
results.add(null);
}
@@ -2008,6 +2010,18 @@ public class BridgeContext extends Context {
return false;
}
+ private class AttributeHolder {
+ private int resourceId;
+ private String name;
+ private boolean isFramework;
+
+ private AttributeHolder(int resourceId, String name, boolean isFramework) {
+ this.resourceId = resourceId;
+ this.name = name;
+ this.isFramework = isFramework;
+ }
+ }
+
/**
* The cached value depends on
* <ol>