LayoutLib: fix some issue with resource resolution.
TypedArray.getResourceId() is not only about id/foo,
it can be any type of resources.
Change-Id: Ia5e147bc078bb349a3fa500a04c596ae44ea34ce
diff --git a/bridge/src/com/android/layoutlib/bridge/BridgeConstants.java b/bridge/src/com/android/layoutlib/bridge/BridgeConstants.java
index 791e53b..596cbfc 100644
--- a/bridge/src/com/android/layoutlib/bridge/BridgeConstants.java
+++ b/bridge/src/com/android/layoutlib/bridge/BridgeConstants.java
@@ -48,6 +48,8 @@
public final static String PREFIX_ANDROID = "android:";
+ public final static String RES_ANIM = "anim";
+ public final static String RES_ANIMATOR = "animator";
public final static String RES_STYLE = "style";
public final static String RES_ATTR = "attr";
public final static String RES_DIMEN = "dimen";
diff --git a/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java b/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
index b0eea4c..fb79473 100644
--- a/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
+++ b/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java
@@ -493,12 +493,12 @@
/**
* Resolves the value of a resource, if the value references a theme or resource value.
* <p/>
- * This method ensures that it returns a {@link IResourceValue} object that does not
+ * This method ensures that it returns a {@link ResourceValue} object that does not
* reference another resource.
* If the resource cannot be resolved, it returns <code>null</code>.
* <p/>
* If a value that does not need to be resolved is given, the method will return a new
- * instance of IResourceValue that contains the input value.
+ * instance of {@link ResourceValue} that contains the input value.
*
* @param type the type of the resource
* @param name the name of the attribute containing this value.
@@ -510,11 +510,11 @@
return null;
}
- // get the IResourceValue referenced by this value
+ // get the ResourceValue referenced by this value
ResourceValue resValue = findResValue(value, false /*forceFrameworkOnly*/);
// if resValue is null, but value is not null, this means it was not a reference.
- // we return the name/value wrapper in a IResourceValue. the isFramework flag doesn't
+ // we return the name/value wrapper in a ResourceValue. the isFramework flag doesn't
// matter.
if (resValue == null) {
return new ResourceValue(type, name, value, false /*isFramework*/);
@@ -525,9 +525,9 @@
}
/**
- * Returns the {@link IResourceValue} referenced by the value of <var>value</var>.
+ * Returns the {@link ResourceValue} referenced by the value of <var>value</var>.
* <p/>
- * This method ensures that it returns a {@link IResourceValue} object that does not
+ * This method ensures that it returns a {@link ResourceValue} object that does not
* reference another resource.
* If the resource cannot be resolved, it returns <code>null</code>.
* <p/>
@@ -535,7 +535,7 @@
* value.
*
* @param value the value containing the reference to resolve.
- * @return a {@link IResourceValue} object or <code>null</code>
+ * @return a {@link ResourceValue} object or <code>null</code>
*/
public ResourceValue resolveResValue(ResourceValue value) {
if (value == null) {
@@ -547,7 +547,7 @@
return value;
}
- // else attempt to find another IResourceValue referenced by this one.
+ // else attempt to find another ResourceValue referenced by this one.
ResourceValue resolvedValue = findResValue(value.getValue(), value.isFramework());
// if the value did not reference anything, then we simply return the input value
@@ -560,7 +560,7 @@
}
/**
- * Searches for, and returns a {@link IResourceValue} by its reference.
+ * Searches for, and returns a {@link ResourceValue} by its reference.
* <p/>
* The reference format can be:
* <pre>@resType/resName</pre>
@@ -577,7 +577,7 @@
* @param reference the resource reference to search for.
* @param forceFrameworkOnly if true all references are considered to be toward framework
* resource even if the reference does not include the android: prefix.
- * @return a {@link IResourceValue} or <code>null</code>.
+ * @return a {@link ResourceValue} or <code>null</code>.
*/
ResourceValue findResValue(String reference, boolean forceFrameworkOnly) {
if (reference == null) {
@@ -670,7 +670,7 @@
}
/**
- * Searches for, and returns a {@link IResourceValue} by its name, and type.
+ * Searches for, and returns a {@link ResourceValue} by its name, and type.
* @param resType the type of the resource
* @param resName the name of the resource
* @param frameworkOnly if <code>true</code>, the method does not search in the
@@ -746,11 +746,11 @@
}
/**
- * Returns the {@link IResourceValue} matching a given name in a given style. If the
+ * Returns the {@link ResourceValue} matching a given name in a given style. If the
* item is not directly available in the style, the method looks in its parent style.
* @param style the style to search in
* @param itemName the name of the item to search for.
- * @return the {@link IResourceValue} object or <code>null</code>
+ * @return the {@link ResourceValue} object or <code>null</code>
*/
public ResourceValue findItemInStyle(StyleResourceValue style, String itemName) {
ResourceValue item = style.findValue(itemName);
@@ -878,8 +878,8 @@
return null;
}
- int getFrameworkIdValue(String idName, int defValue) {
- Integer value = Bridge.getResourceValue(BridgeConstants.RES_ID, idName);
+ int getFrameworkResourceValue(String resType, String resName, int defValue) {
+ Integer value = Bridge.getResourceValue(resType, resName);
if (value != null) {
return value.intValue();
}
@@ -887,9 +887,9 @@
return defValue;
}
- int getProjectIdValue(String idName, int defValue) {
+ int getProjectResourceValue(String resType, String resName, int defValue) {
if (mProjectCallback != null) {
- Integer value = mProjectCallback.getResourceValue(BridgeConstants.RES_ID, idName);
+ Integer value = mProjectCallback.getResourceValue(resType, resName);
if (value != null) {
return value.intValue();
}
diff --git a/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java b/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java
index 2ea5281..215c5f93 100644
--- a/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java
+++ b/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java
@@ -294,7 +294,6 @@
return null;
}
-
@Override
public TypedArray obtainAttributes(AttributeSet set, int[] attrs) {
return mContext.obtainStyledAttributes(set, attrs);
diff --git a/bridge/src/com/android/layoutlib/bridge/android/BridgeTypedArray.java b/bridge/src/com/android/layoutlib/bridge/android/BridgeTypedArray.java
index 4fb280d..d36846b 100644
--- a/bridge/src/com/android/layoutlib/bridge/android/BridgeTypedArray.java
+++ b/bridge/src/com/android/layoutlib/bridge/android/BridgeTypedArray.java
@@ -577,19 +577,21 @@
return mContext.getDynamicIdByStyle((StyleResourceValue)resValue);
}
- // if the attribute was a reference to an id, and not a declaration of an id (@+id), then
- // the xml attribute value was "resolved" which leads us to a ResourceValue with
- // getType() returning "id" and getName() returning the id name
+ // if the attribute was a reference to a resource, and not a declaration of an id (@+id),
+ // then the xml attribute value was "resolved" which leads us to a ResourceValue with a
+ // valid getType() and getName() returning a resource name.
// (and getValue() returning null!). We need to handle this!
- if (resValue.getType() != null && resValue.getType().equals(BridgeConstants.RES_ID)) {
+ if (resValue.getType() != null && resValue.getType().startsWith("@+") == false) {
// if this is a framework id
if (mPlatformFile || resValue.isFramework()) {
// look for idName in the android R classes
- return mContext.getFrameworkIdValue(resValue.getName(), defValue);
+ return mContext.getFrameworkResourceValue(
+ resValue.getType(), resValue.getName(), defValue);
}
// look for idName in the project R class.
- return mContext.getProjectIdValue(resValue.getName(), defValue);
+ return mContext.getProjectResourceValue(
+ resValue.getType(), resValue.getName(), defValue);
}
// else, try to get the value, and resolve it somehow.
@@ -626,11 +628,11 @@
// if this is a framework id
if (mPlatformFile || value.startsWith("@android") || value.startsWith("@+android")) {
// look for idName in the android R classes
- return mContext.getFrameworkIdValue(idName, defValue);
+ return mContext.getFrameworkResourceValue(BridgeConstants.RES_ID, idName, defValue);
}
// look for idName in the project R class.
- return mContext.getProjectIdValue(idName, defValue);
+ return mContext.getProjectResourceValue(BridgeConstants.RES_ID, idName, defValue);
}
// not a direct id valid reference? resolve it
diff --git a/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java b/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
index 55a5bc0..d2fef5e 100644
--- a/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
+++ b/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java
@@ -505,14 +505,18 @@
ResourceValue animationResource = null;
int animationId = 0;
if (isFrameworkAnimation) {
- animationResource = mContext.getFrameworkResource("anim", animationName);
+ animationResource = mContext.getFrameworkResource(BridgeConstants.RES_ANIM,
+ animationName);
if (animationResource != null) {
- animationId = Bridge.getResourceValue("anim", animationName);
+ animationId = Bridge.getResourceValue(BridgeConstants.RES_ANIM,
+ animationName);
}
} else {
- animationResource = mContext.getProjectResource("anim", animationName);
+ animationResource = mContext.getProjectResource(BridgeConstants.RES_ANIM,
+ animationName);
if (animationResource != null) {
- animationId = mContext.getProjectCallback().getResourceValue("anim", animationName);
+ animationId = mContext.getProjectCallback().getResourceValue(
+ BridgeConstants.RES_ANIM, animationName);
}
}