From 08e2a4b56f23ce02f17300438f76ccc1f663f183 Mon Sep 17 00:00:00 2001 From: Xavier Ducrohet Date: Wed, 12 Aug 2009 21:47:11 -0700 Subject: Fix handling of reference XML file in layout files This allows layout that references animation to properly load in the layout editor in ADT --- .../bridge/src/android/os/ServiceManager.java | 2 + .../view/accessibility/AccessibilityManager.java | 2 + .../android/layoutlib/bridge/BridgeResources.java | 124 +++++++++++++-------- 3 files changed, 82 insertions(+), 46 deletions(-) (limited to 'tools/layoutlib') diff --git a/tools/layoutlib/bridge/src/android/os/ServiceManager.java b/tools/layoutlib/bridge/src/android/os/ServiceManager.java index 423abaf..6a68ee2 100644 --- a/tools/layoutlib/bridge/src/android/os/ServiceManager.java +++ b/tools/layoutlib/bridge/src/android/os/ServiceManager.java @@ -53,6 +53,8 @@ public final class ServiceManager { * Return a list of all currently running services. */ public static String[] listServices() throws RemoteException { + // actual implementation returns null sometimes, so it's ok + // to return null instead of an empty list. return null; } diff --git a/tools/layoutlib/bridge/src/android/view/accessibility/AccessibilityManager.java b/tools/layoutlib/bridge/src/android/view/accessibility/AccessibilityManager.java index 22d8448..251c053 100644 --- a/tools/layoutlib/bridge/src/android/view/accessibility/AccessibilityManager.java +++ b/tools/layoutlib/bridge/src/android/view/accessibility/AccessibilityManager.java @@ -87,6 +87,8 @@ public final class AccessibilityManager { * @return An unmodifiable list with {@link ServiceInfo}s. */ public List getAccessibilityServiceList() { + // normal implementation does this in some case, so let's do the same + // (unmodifiableList wrapped around null). List services = null; return Collections.unmodifiableList(services); } diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeResources.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeResources.java index 0bcc7fd..8a040e4 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeResources.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeResources.java @@ -43,14 +43,14 @@ import java.io.FileReader; import java.io.InputStream; /** - * + * */ public final class BridgeResources extends Resources { private BridgeContext mContext; private IProjectCallback mProjectCallback; private boolean[] mPlatformResourceFlag = new boolean[1]; - + /** * This initializes the static field {@link Resources#mSystem} which is used * by methods who get global resources using {@link Resources#getSystem()}. @@ -59,7 +59,7 @@ public final class BridgeResources extends Resources { *

* {@link Bridge} calls this method after setting up a new bridge. */ - /*package*/ static Resources initSystem(BridgeContext context, + /*package*/ static Resources initSystem(BridgeContext context, AssetManager assets, DisplayMetrics metrics, Configuration config, @@ -73,7 +73,7 @@ public final class BridgeResources extends Resources { } return Resources.mSystem; } - + /** * Clears the static {@link Resources#mSystem} to make sure we don't leave objects * around that would prevent us from unloading the library. @@ -92,15 +92,15 @@ public final class BridgeResources extends Resources { mContext = context; mProjectCallback = projectCallback; } - + public BridgeTypedArray newTypeArray(int numEntries, boolean platformFile) { return new BridgeTypedArray(this, mContext, numEntries, platformFile); } - + private IResourceValue getResourceValue(int id, boolean[] platformResFlag_out) { // first get the String related to this id in the framework String[] resourceInfo = Bridge.resolveResourceValue(id); - + if (resourceInfo != null) { platformResFlag_out[0] = true; return mContext.getFrameworkResource(resourceInfo[1], resourceInfo[0]); @@ -109,7 +109,7 @@ public final class BridgeResources extends Resources { // didn't find a match in the framework? look in the project. if (mProjectCallback != null) { resourceInfo = mProjectCallback.resolveResourceValue(id); - + if (resourceInfo != null) { platformResFlag_out[0] = false; return mContext.getProjectResource(resourceInfo[1], resourceInfo[0]); @@ -118,26 +118,26 @@ public final class BridgeResources extends Resources { return null; } - + @Override public Drawable getDrawable(int id) throws NotFoundException { IResourceValue value = getResourceValue(id, mPlatformResourceFlag); - + if (value != null) { return ResourceHelper.getDrawable(value.getValue(), mContext, value.isFramework()); } - + // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return null; } - + @Override public int getColor(int id) throws NotFoundException { IResourceValue value = getResourceValue(id, mPlatformResourceFlag); - + if (value != null) { try { return ResourceHelper.getColor(value.getValue()); @@ -145,18 +145,18 @@ public final class BridgeResources extends Resources { return 0; } } - + // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return 0; } - + @Override public ColorStateList getColorStateList(int id) throws NotFoundException { IResourceValue value = getResourceValue(id, mPlatformResourceFlag); - + if (value != null) { try { int color = ResourceHelper.getColor(value.getValue()); @@ -165,33 +165,33 @@ public final class BridgeResources extends Resources { return null; } } - + // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return null; } - + @Override public CharSequence getText(int id) throws NotFoundException { IResourceValue value = getResourceValue(id, mPlatformResourceFlag); - + if (value != null) { return value.getValue(); } - + // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return null; } - + @Override public XmlResourceParser getLayout(int id) throws NotFoundException { IResourceValue value = getResourceValue(id, mPlatformResourceFlag); - + if (value != null) { File xml = new File(value.getValue()); if (xml.isFile()) { @@ -201,7 +201,7 @@ public final class BridgeResources extends Resources { KXmlParser parser = new KXmlParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileReader(xml)); - + return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); } catch (XmlPullParserException e) { mContext.getLogger().error(e); @@ -215,22 +215,22 @@ public final class BridgeResources extends Resources { // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return null; } - + @Override public TypedArray obtainAttributes(AttributeSet set, int[] attrs) { return mContext.obtainStyledAttributes(set, attrs); } - + @Override public TypedArray obtainTypedArray(int id) throws NotFoundException { throw new UnsupportedOperationException(); } - - + + @Override public float getDimension(int id) throws NotFoundException { IResourceValue value = getResourceValue(id, mPlatformResourceFlag); @@ -244,7 +244,7 @@ public final class BridgeResources extends Resources { } else if (v.equals(BridgeConstants.WRAP_CONTENT)) { return LayoutParams.WRAP_CONTENT; } - + if (ResourceHelper.stringToFloat(v, mTmpValue) && mTmpValue.type == TypedValue.TYPE_DIMENSION) { return mTmpValue.getDimension(mMetrics); @@ -254,7 +254,7 @@ public final class BridgeResources extends Resources { // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return 0; } @@ -276,7 +276,7 @@ public final class BridgeResources extends Resources { // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return 0; } @@ -298,7 +298,7 @@ public final class BridgeResources extends Resources { // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return 0; } @@ -306,7 +306,7 @@ public final class BridgeResources extends Resources { @Override public int getInteger(int id) throws NotFoundException { IResourceValue value = getResourceValue(id, mPlatformResourceFlag); - + if (value != null && value.getValue() != null) { String v = value.getValue(); int radix = 10; @@ -320,10 +320,10 @@ public final class BridgeResources extends Resources { // return exception below } } - + // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return 0; } @@ -348,12 +348,12 @@ public final class BridgeResources extends Resources { String s = getString(id); if (s != null) { return String.format(s, formatArgs); - + } // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return null; } @@ -361,14 +361,14 @@ public final class BridgeResources extends Resources { @Override public String getString(int id) throws NotFoundException { IResourceValue value = getResourceValue(id, mPlatformResourceFlag); - + if (value != null && value.getValue() != null) { return value.getValue(); } // id was not found or not resolved. Throw a NotFoundException. throwException(id); - + // this is not used since the method above always throws return null; } @@ -385,6 +385,11 @@ public final class BridgeResources extends Resources { if (ResourceHelper.stringToFloat(v, outValue)) { return; } + + // else it's a string + outValue.type = TypedValue.TYPE_STRING; + outValue.string = v; + return; } } @@ -413,7 +418,7 @@ public final class BridgeResources extends Resources { KXmlParser parser = new KXmlParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileReader(f)); - + return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); } catch (XmlPullParserException e) { NotFoundException newE = new NotFoundException(); @@ -436,6 +441,33 @@ public final class BridgeResources extends Resources { } @Override + public XmlResourceParser loadXmlResourceParser(String file, int id, + int assetCookie, String type) throws NotFoundException { + // even though we know the XML file to load directly, we still need to resolve the + // id so that we can know if it's a platform or project resource. + // (mPlatformResouceFlag will get the result and will be used later). + getResourceValue(id, mPlatformResourceFlag); + + File f = new File(file); + try { + KXmlParser parser = new KXmlParser(); + parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); + parser.setInput(new FileReader(f)); + + return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); + } catch (XmlPullParserException e) { + NotFoundException newE = new NotFoundException(); + newE.initCause(e); + throw newE; + } catch (FileNotFoundException e) { + NotFoundException newE = new NotFoundException(); + newE.initCause(e); + throw newE; + } + } + + + @Override public InputStream openRawResource(int id) throws NotFoundException { IResourceValue value = getResourceValue(id, mPlatformResourceFlag); @@ -482,7 +514,7 @@ public final class BridgeResources extends Resources { if (resourceInfo == null && mProjectCallback != null) { resourceInfo = mProjectCallback.resolveResourceValue(id); } - + String message = null; if (resourceInfo != null) { message = String.format( @@ -492,7 +524,7 @@ public final class BridgeResources extends Resources { message = String.format( "Could not resolve resource value: 0x%1$X.", id); } - + throw new NotFoundException(message); } } -- cgit v1.1