diff options
author | Xavier Ducrohet <xav@android.com> | 2012-02-21 14:26:32 -0800 |
---|---|---|
committer | Xavier Ducrohet <xav@android.com> | 2012-02-21 14:33:37 -0800 |
commit | c3f979f6fecb8b46e9315fddb2a1f344bf1f1447 (patch) | |
tree | 2558c8a34c9bf516a3c669629c378c2516a20074 /tools | |
parent | 2d559b5311be1cd909e5a445afe75a840688b822 (diff) | |
download | frameworks_base-c3f979f6fecb8b46e9315fddb2a1f344bf1f1447.zip frameworks_base-c3f979f6fecb8b46e9315fddb2a1f344bf1f1447.tar.gz frameworks_base-c3f979f6fecb8b46e9315fddb2a1f344bf1f1447.tar.bz2 |
Make sure resource references are resolved.
XmlPullAttribute can query for attributes and return them in a given
format. We need to make sure they are first resolved before
trying to convert them to int/float/boolean/...
Change-Id: I2aaced022a0382e501978c396e49d6191d53bdc8
Diffstat (limited to 'tools')
-rw-r--r-- | tools/layoutlib/bridge/src/android/util/BridgeXmlPullAttributes.java | 191 | ||||
-rw-r--r-- | tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java | 4 |
2 files changed, 176 insertions, 19 deletions
diff --git a/tools/layoutlib/bridge/src/android/util/BridgeXmlPullAttributes.java b/tools/layoutlib/bridge/src/android/util/BridgeXmlPullAttributes.java index 0a3cdc6..6ac5b02 100644 --- a/tools/layoutlib/bridge/src/android/util/BridgeXmlPullAttributes.java +++ b/tools/layoutlib/bridge/src/android/util/BridgeXmlPullAttributes.java @@ -18,6 +18,7 @@ package android.util; import com.android.ide.common.rendering.api.RenderResources; import com.android.ide.common.rendering.api.ResourceValue; +import com.android.internal.util.XmlUtils; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.android.BridgeContext; @@ -25,9 +26,6 @@ import com.android.resources.ResourceType; import org.xmlpull.v1.XmlPullParser; -import android.util.AttributeSet; -import android.util.XmlPullAttributes; - /** * A correct implementation of the {@link AttributeSet} interface on top of a XmlPullParser */ @@ -80,21 +78,40 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes { return 0; } - /* - * (non-Javadoc) - * @see android.util.XmlPullAttributes#getAttributeResourceValue(int, int) - */ @Override - public int getAttributeResourceValue(int index, int defaultValue) { - String value = getAttributeValue(index); + public int getAttributeListValue(String namespace, String attribute, + String[] options, int defaultValue) { + String value = getAttributeValue(namespace, attribute); + if (value != null) { + ResourceValue r = getResourceValue(value); - return resolveResourceValue(value, defaultValue); + if (r != null) { + value = r.getValue(); + } + + return XmlUtils.convertValueToList(value, options, defaultValue); + } + + return defaultValue; + } + + @Override + public boolean getAttributeBooleanValue(String namespace, String attribute, + boolean defaultValue) { + String value = getAttributeValue(namespace, attribute); + if (value != null) { + ResourceValue r = getResourceValue(value); + + if (r != null) { + value = r.getValue(); + } + + return XmlUtils.convertValueToBoolean(value, defaultValue); + } + + return defaultValue; } - /* - * (non-Javadoc) - * @see android.util.XmlPullAttributes#getAttributeResourceValue(java.lang.String, java.lang.String, int) - */ @Override public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) { String value = getAttributeValue(namespace, attribute); @@ -102,12 +119,151 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes { return resolveResourceValue(value, defaultValue); } - private int resolveResourceValue(String value, int defaultValue) { + @Override + public int getAttributeIntValue(String namespace, String attribute, + int defaultValue) { + String value = getAttributeValue(namespace, attribute); + if (value != null) { + ResourceValue r = getResourceValue(value); + + if (r != null) { + value = r.getValue(); + } + + return XmlUtils.convertValueToInt(value, defaultValue); + } + + return defaultValue; + } + + @Override + public int getAttributeUnsignedIntValue(String namespace, String attribute, + int defaultValue) { + String value = getAttributeValue(namespace, attribute); + if (value != null) { + ResourceValue r = getResourceValue(value); + + if (r != null) { + value = r.getValue(); + } + + return XmlUtils.convertValueToUnsignedInt(value, defaultValue); + } + + return defaultValue; + } + + @Override + public float getAttributeFloatValue(String namespace, String attribute, + float defaultValue) { + String s = getAttributeValue(namespace, attribute); + if (s != null) { + ResourceValue r = getResourceValue(s); + + if (r != null) { + s = r.getValue(); + } + + return Float.parseFloat(s); + } + + return defaultValue; + } + + @Override + public int getAttributeListValue(int index, + String[] options, int defaultValue) { + return XmlUtils.convertValueToList( + getAttributeValue(index), options, defaultValue); + } + + @Override + public boolean getAttributeBooleanValue(int index, boolean defaultValue) { + String value = getAttributeValue(index); + if (value != null) { + ResourceValue r = getResourceValue(value); + + if (r != null) { + value = r.getValue(); + } + + return XmlUtils.convertValueToBoolean(value, defaultValue); + } + + return defaultValue; + } + + @Override + public int getAttributeResourceValue(int index, int defaultValue) { + String value = getAttributeValue(index); + + return resolveResourceValue(value, defaultValue); + } + + @Override + public int getAttributeIntValue(int index, int defaultValue) { + String value = getAttributeValue(index); + if (value != null) { + ResourceValue r = getResourceValue(value); + + if (r != null) { + value = r.getValue(); + } + + return XmlUtils.convertValueToInt(value, defaultValue); + } + + return defaultValue; + } + + @Override + public int getAttributeUnsignedIntValue(int index, int defaultValue) { + String value = getAttributeValue(index); + if (value != null) { + ResourceValue r = getResourceValue(value); + + if (r != null) { + value = r.getValue(); + } + + return XmlUtils.convertValueToUnsignedInt(value, defaultValue); + } + + return defaultValue; + } + + @Override + public float getAttributeFloatValue(int index, float defaultValue) { + String s = getAttributeValue(index); + if (s != null) { + ResourceValue r = getResourceValue(s); + + if (r != null) { + s = r.getValue(); + } + + return Float.parseFloat(s); + } + + return defaultValue; + } + + // -- private helper methods + + /** + * Returns a resolved {@link ResourceValue} from a given value. + */ + private ResourceValue getResourceValue(String value) { // now look for this particular value RenderResources resources = mContext.getRenderResources(); - ResourceValue resource = resources.resolveResValue( - resources.findResValue(value, mPlatformFile)); + return resources.resolveResValue(resources.findResValue(value, mPlatformFile)); + } + /** + * Resolves and return a value to its associated integer. + */ + private int resolveResourceValue(String value, int defaultValue) { + ResourceValue resource = getResourceValue(value); if (resource != null) { Integer id = null; if (mPlatformFile || resource.isFramework()) { @@ -124,5 +280,4 @@ public class BridgeXmlPullAttributes extends XmlPullAttributes { return defaultValue; } - } diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java index 33bf7bc..ff88209 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java @@ -24,8 +24,8 @@ import com.android.ide.common.rendering.api.DrawableParams; import com.android.ide.common.rendering.api.LayoutLog; import com.android.ide.common.rendering.api.RenderSession; import com.android.ide.common.rendering.api.Result; -import com.android.ide.common.rendering.api.SessionParams; import com.android.ide.common.rendering.api.Result.Status; +import com.android.ide.common.rendering.api.SessionParams; import com.android.layoutlib.bridge.impl.FontLoader; import com.android.layoutlib.bridge.impl.RenderDrawable; import com.android.layoutlib.bridge.impl.RenderSessionImpl; @@ -242,6 +242,8 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { if (fontLoader != null) { Typeface_Delegate.init(fontLoader); } else { + log.error(LayoutLog.TAG_BROKEN, + "Failed create FontLoader in layout lib.", null); return false; } |