Merge "LayoutLib: update with Pair API." into honeycomb

This commit is contained in:
Xavier Ducrohet
2011-01-28 17:21:57 -08:00
committed by Android (Google) Code Review
10 changed files with 81 additions and 82 deletions

View File

@@ -8,6 +8,6 @@
<classpathentry kind="var" path="ANDROID_PLAT_SRC/prebuilt/common/kxml2/kxml2-2.3.0.jar" sourcepath="/ANDROID_PLAT_SRC/dalvik/libcore/xml/src/main/java"/>
<classpathentry kind="var" path="ANDROID_PLAT_SRC/out/host/common/obj/JAVA_LIBRARIES/temp_layoutlib_intermediates/javalib.jar" sourcepath="/ANDROID_PLAT_SRC/frameworks/base"/>
<classpathentry kind="var" path="ANDROID_PLAT_SRC/prebuilt/common/ninepatch/ninepatch-prebuilt.jar"/>
<classpathentry kind="var" path="ANDROID_PLAT_SRC/prebuilt/common/resources/resources-prebuilt.jar"/>
<classpathentry kind="var" path="ANDROID_PLAT_SRC/prebuilt/common/common/common-prebuilt.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -21,7 +21,7 @@ LOCAL_SRC_FILES := $(call all-java-files-under,src)
LOCAL_JAVA_LIBRARIES := \
kxml2-2.3.0 \
layoutlib_api-prebuilt \
resources-prebuilt
common-prebuilt
LOCAL_STATIC_JAVA_LIBRARIES := \
temp_layoutlib \

View File

@@ -31,6 +31,7 @@ import com.android.ninepatch.NinePatchChunk;
import com.android.resources.ResourceType;
import com.android.tools.layoutlib.create.MethodAdapter;
import com.android.tools.layoutlib.create.OverrideMethod;
import com.android.util.Pair;
import android.graphics.Bitmap;
import android.graphics.Typeface;
@@ -42,6 +43,7 @@ import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
@@ -69,9 +71,11 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
private final static ReentrantLock sLock = new ReentrantLock();
/**
* Maps from id to resource name/type. This is for android.R only.
* Maps from id to resource type/name. This is for android.R only.
*/
private final static Map<Integer, String[]> sRMap = new HashMap<Integer, String[]>();
private final static Map<Integer, Pair<ResourceType, String>> sRMap =
new HashMap<Integer, Pair<ResourceType, String>>();
/**
* Same as sRMap except for int[] instead of int resources. This is for android.R only.
*/
@@ -80,8 +84,8 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
* Reverse map compared to sRMap, resource type -> (resource name -> id).
* This is for android.R only.
*/
private final static Map<String, Map<String, Integer>> sRFullMap =
new HashMap<String, Map<String,Integer>>();
private final static Map<ResourceType, Map<String, Integer>> sRFullMap =
new EnumMap<ResourceType, Map<String,Integer>>(ResourceType.class);
private final static Map<Object, Map<String, SoftReference<Bitmap>>> sProjectBitmapCache =
new HashMap<Object, Map<String, SoftReference<Bitmap>>>();
@@ -131,7 +135,7 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
}
}
/** Instance of IntArrayWrapper to be reused in {@link #resolveResourceValue(int[])}. */
/** Instance of IntArrayWrapper to be reused in {@link #resolveResourceId(int[])}. */
private final static IntArray sIntArrayWrapper = new IntArray();
/**
@@ -237,28 +241,30 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
Class<?> r = com.android.internal.R.class;
for (Class<?> inner : r.getDeclaredClasses()) {
String resType = inner.getSimpleName();
String resTypeName = inner.getSimpleName();
ResourceType resType = ResourceType.getEnum(resTypeName);
if (resType != null) {
Map<String, Integer> fullMap = new HashMap<String, Integer>();
sRFullMap.put(resType, fullMap);
Map<String, Integer> fullMap = new HashMap<String, Integer>();
sRFullMap.put(resType, fullMap);
for (Field f : inner.getDeclaredFields()) {
// only process static final fields. Since the final attribute may have
// been altered by layoutlib_create, we only check static
int modifiers = f.getModifiers();
if (Modifier.isStatic(modifiers)) {
Class<?> type = f.getType();
if (type.isArray() && type.getComponentType() == int.class) {
// if the object is an int[] we put it in sRArrayMap using an IntArray
// wrapper that properly implements equals and hashcode for the array
// objects, as required by the map contract.
sRArrayMap.put(new IntArray((int[]) f.get(null)), f.getName());
} else if (type == int.class) {
Integer value = (Integer) f.get(null);
sRMap.put(value, new String[] { f.getName(), resType });
fullMap.put(f.getName(), value);
} else {
assert false;
for (Field f : inner.getDeclaredFields()) {
// only process static final fields. Since the final attribute may have
// been altered by layoutlib_create, we only check static
int modifiers = f.getModifiers();
if (Modifier.isStatic(modifiers)) {
Class<?> type = f.getType();
if (type.isArray() && type.getComponentType() == int.class) {
// if the object is an int[] we put it in sRArrayMap using an IntArray
// wrapper that properly implements equals and hashcode for the array
// objects, as required by the map contract.
sRArrayMap.put(new IntArray((int[]) f.get(null)), f.getName());
} else if (type == int.class) {
Integer value = (Integer) f.get(null);
sRMap.put(value, Pair.of(resType, f.getName()));
fullMap.put(f.getName(), value);
} else {
assert false;
}
}
}
}
@@ -389,10 +395,10 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
/**
* Returns details of a framework resource from its integer value.
* @param value the integer value
* @return an array of 2 strings containing the resource name and type, or null if the id
* does not match any resource.
* @return a Pair containing the resource type and name, or null if the id
* does not match any resource.
*/
public static String[] resolveResourceValue(int value) {
public static Pair<ResourceType, String> resolveResourceId(int value) {
return sRMap.get(value);
}
@@ -400,7 +406,7 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
* Returns the name of a framework resource whose value is an int array.
* @param array
*/
public static String resolveResourceValue(int[] array) {
public static String resolveResourceId(int[] array) {
sIntArrayWrapper.set(array);
return sRArrayMap.get(sIntArrayWrapper);
}
@@ -411,9 +417,8 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge {
* @param name the name of the resource.
* @return an {@link Integer} containing the resource id, or null if no resource were found.
*/
public static Integer getResourceValue(ResourceType type, String name) {
String typeString = type.getName();
Map<String, Integer> map = sRFullMap.get(typeString);
public static Integer getResourceId(ResourceType type, String name) {
Map<String, Integer> map = sRFullMap.get(type);
if (map != null) {
return map.get(name);
}

View File

@@ -25,6 +25,7 @@ import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.BridgeConstants;
import com.android.layoutlib.bridge.impl.Stack;
import com.android.resources.ResourceType;
import com.android.util.Pair;
import android.app.Activity;
import android.app.Fragment;
@@ -518,14 +519,14 @@ public final class BridgeContext extends Activity {
*/
private TreeMap<Integer,String> searchAttrs(int[] attrs, boolean[] outFrameworkFlag) {
// get the name of the array from the framework resources
String arrayName = Bridge.resolveResourceValue(attrs);
String arrayName = Bridge.resolveResourceId(attrs);
if (arrayName != null) {
// if we found it, get the name of each of the int in the array.
TreeMap<Integer,String> attributes = new TreeMap<Integer, String>();
for (int i = 0 ; i < attrs.length ; i++) {
String[] info = Bridge.resolveResourceValue(attrs[i]);
Pair<ResourceType, String> info = Bridge.resolveResourceId(attrs[i]);
if (info != null) {
attributes.put(i, info[0]);
attributes.put(i, info.getSecond());
} else {
// FIXME Not sure what we should be doing here...
attributes.put(i, null);
@@ -541,13 +542,13 @@ public final class BridgeContext extends Activity {
// if the name was not found in the framework resources, look in the project
// resources
arrayName = mProjectCallback.resolveResourceValue(attrs);
arrayName = mProjectCallback.resolveResourceId(attrs);
if (arrayName != null) {
TreeMap<Integer,String> attributes = new TreeMap<Integer, String>();
for (int i = 0 ; i < attrs.length ; i++) {
String[] info = mProjectCallback.resolveResourceValue(attrs[i]);
Pair<ResourceType, String> info = mProjectCallback.resolveResourceId(attrs[i]);
if (info != null) {
attributes.put(i, info[0]);
attributes.put(i, info.getSecond());
} else {
// FIXME Not sure what we should be doing here...
attributes.put(i, null);
@@ -572,14 +573,14 @@ public final class BridgeContext extends Activity {
* if nothing is found.
*/
public String searchAttr(int attr) {
String[] info = Bridge.resolveResourceValue(attr);
Pair<ResourceType, String> info = Bridge.resolveResourceId(attr);
if (info != null) {
return info[0];
return info.getSecond();
}
info = mProjectCallback.resolveResourceValue(attr);
info = mProjectCallback.resolveResourceId(attr);
if (info != null) {
return info[0];
return info.getSecond();
}
return null;
@@ -616,7 +617,7 @@ public final class BridgeContext extends Activity {
}
int getFrameworkResourceValue(ResourceType resType, String resName, int defValue) {
Integer value = Bridge.getResourceValue(resType, resName);
Integer value = Bridge.getResourceId(resType, resName);
if (value != null) {
return value.intValue();
}
@@ -626,7 +627,7 @@ public final class BridgeContext extends Activity {
int getProjectResourceValue(ResourceType resType, String resName, int defValue) {
if (mProjectCallback != null) {
Integer value = mProjectCallback.getResourceValue(resType, resName);
Integer value = mProjectCallback.getResourceId(resType, resName);
if (value != null) {
return value.intValue();
}

View File

@@ -22,6 +22,7 @@ import com.android.ide.common.rendering.api.MergeCookie;
import com.android.ide.common.rendering.api.ResourceValue;
import com.android.layoutlib.bridge.Bridge;
import com.android.resources.ResourceType;
import com.android.util.Pair;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
@@ -155,16 +156,16 @@ public final class BridgeInflater extends LayoutInflater {
ResourceValue value = null;
String[] layoutInfo = Bridge.resolveResourceValue(resource);
Pair<ResourceType, String> layoutInfo = Bridge.resolveResourceId(resource);
if (layoutInfo != null) {
value = bridgeContext.getRenderResources().getFrameworkResource(
ResourceType.LAYOUT, layoutInfo[0]);
ResourceType.LAYOUT, layoutInfo.getSecond());
} else {
layoutInfo = mProjectCallback.resolveResourceValue(resource);
layoutInfo = mProjectCallback.resolveResourceId(resource);
if (layoutInfo != null) {
value = bridgeContext.getRenderResources().getProjectResource(
ResourceType.LAYOUT, layoutInfo[0]);
ResourceType.LAYOUT, layoutInfo.getSecond());
}
}

View File

@@ -23,6 +23,7 @@ import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.BridgeConstants;
import com.android.layoutlib.bridge.impl.ResourceHelper;
import com.android.resources.ResourceType;
import com.android.util.Pair;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
@@ -101,32 +102,22 @@ public final class BridgeResources extends Resources {
private ResourceValue getResourceValue(int id, boolean[] platformResFlag_out) {
// first get the String related to this id in the framework
String[] resourceInfo = Bridge.resolveResourceValue(id);
Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(id);
if (resourceInfo != null) {
ResourceType resType = ResourceType.getEnum(resourceInfo[1]);
if (resType == null) {
return null;
}
platformResFlag_out[0] = true;
return mContext.getRenderResources().getFrameworkResource(
resType, resourceInfo[0]);
resourceInfo.getFirst(), resourceInfo.getSecond());
}
// didn't find a match in the framework? look in the project.
if (mProjectCallback != null) {
resourceInfo = mProjectCallback.resolveResourceValue(id);
resourceInfo = mProjectCallback.resolveResourceId(id);
if (resourceInfo != null) {
ResourceType resType = ResourceType.getEnum(resourceInfo[1]);
if (resType == null) {
return null;
}
platformResFlag_out[0] = false;
return mContext.getRenderResources().getProjectResource(
resType, resourceInfo[0]);
resourceInfo.getFirst(), resourceInfo.getSecond());
}
}
@@ -625,18 +616,18 @@ public final class BridgeResources extends Resources {
*/
private void throwException(int id) throws NotFoundException {
// first get the String related to this id in the framework
String[] resourceInfo = Bridge.resolveResourceValue(id);
Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(id);
// if the name is unknown in the framework, get it from the custom view loader.
if (resourceInfo == null && mProjectCallback != null) {
resourceInfo = mProjectCallback.resolveResourceValue(id);
resourceInfo = mProjectCallback.resolveResourceId(id);
}
String message = null;
if (resourceInfo != null) {
message = String.format(
"Could not find %1$s resource matching value 0x%2$X (resolved name: %3$s) in current configuration.",
resourceInfo[1], id, resourceInfo[0]);
resourceInfo.getFirst(), id, resourceInfo.getSecond());
} else {
message = String.format(
"Could not resolve resource value: 0x%1$X.", id);

View File

@@ -589,7 +589,7 @@ public final class BridgeTypedArray extends TypedArray {
// 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.getResourceType() != null && resValue.getType().startsWith("@+") == false) {
if (resValue.getResourceType() != null) {
// if this is a framework id
if (mPlatformFile || resValue.isFramework()) {
// look for idName in the android R classes
@@ -647,10 +647,10 @@ public final class BridgeTypedArray extends TypedArray {
Integer idValue = null;
if (resValue.isFramework()) {
idValue = Bridge.getResourceValue(resValue.getResourceType(),
idValue = Bridge.getResourceId(resValue.getResourceType(),
resValue.getName());
} else {
idValue = mContext.getProjectCallback().getResourceValue(
idValue = mContext.getProjectCallback().getResourceId(
resValue.getResourceType(), resValue.getName());
}

View File

@@ -59,7 +59,7 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {
String ns = mParser.getAttributeNamespace(index);
if (BridgeConstants.NS_RESOURCES.equals(ns)) {
Integer v = Bridge.getResourceValue(ResourceType.ATTR, name);
Integer v = Bridge.getResourceId(ResourceType.ATTR, name);
if (v != null) {
return v.intValue();
}
@@ -70,8 +70,7 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {
// this is not an attribute in the android namespace, we query the customviewloader, if
// the namespaces match.
if (mContext.getProjectCallback().getNamespace().equals(ns)) {
Integer v = mContext.getProjectCallback().getResourceValue(ResourceType.ATTR,
name);
Integer v = mContext.getProjectCallback().getResourceId(ResourceType.ATTR, name);
if (v != null) {
return v.intValue();
}
@@ -111,9 +110,9 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes {
if (resource != null) {
Integer id = null;
if (mPlatformFile || resource.isFramework()) {
id = Bridge.getResourceValue(resource.getResourceType(), resource.getName());
id = Bridge.getResourceId(resource.getResourceType(), resource.getName());
} else {
id = mContext.getProjectCallback().getResourceValue(
id = mContext.getProjectCallback().getResourceId(
resource.getResourceType(), resource.getName());
}

View File

@@ -50,6 +50,7 @@ import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
import com.android.resources.Density;
import com.android.resources.ResourceType;
import com.android.resources.ScreenSize;
import com.android.util.Pair;
import android.animation.Animator;
import android.animation.AnimatorInflater;
@@ -569,13 +570,13 @@ public class RenderSessionImpl extends FrameworkResourceIdProvider {
animationResource = mContext.getRenderResources().getFrameworkResource(
ResourceType.ANIMATOR, animationName);
if (animationResource != null) {
animationId = Bridge.getResourceValue(ResourceType.ANIMATOR, animationName);
animationId = Bridge.getResourceId(ResourceType.ANIMATOR, animationName);
}
} else {
animationResource = mContext.getRenderResources().getProjectResource(
ResourceType.ANIMATOR, animationName);
if (animationResource != null) {
animationId = mContext.getProjectCallback().getResourceValue(
animationId = mContext.getProjectCallback().getResourceId(
ResourceType.ANIMATOR, animationName);
}
}
@@ -1227,10 +1228,10 @@ public class RenderSessionImpl extends FrameworkResourceIdProvider {
View child = content.getChildAt(i);
String tabSpec = String.format("tab_spec%d", i+1);
int id = child.getId();
String[] resource = projectCallback.resolveResourceValue(id);
Pair<ResourceType, String> resource = projectCallback.resolveResourceId(id);
String name;
if (resource != null) {
name = resource[0]; // 0 is resource name, 1 is resource type.
name = resource.getSecond();
} else {
name = String.format("Tab %d", i+1); // default name if id is unresolved.
}
@@ -1310,6 +1311,6 @@ public class RenderSessionImpl extends FrameworkResourceIdProvider {
@Override
public Integer getId(ResourceType resType, String resName) {
return Bridge.getResourceValue(resType, resName);
return Bridge.getResourceId(resType, resName);
}
}

View File

@@ -197,7 +197,8 @@ public final class ResourceHelper {
} catch (Exception e) {
// this is an error and not warning since the file existence is checked before
// attempting to parse it.
Bridge.getLog().error(null, "Failed to parse file " + value, e, null /*data*/);
Bridge.getLog().error(null, "Failed to parse file " + stringValue,
e, null /*data*/);
}
} else {
Bridge.getLog().error(LayoutLog.TAG_BROKEN,