Loading tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java +23 −19 Original line number Original line Diff line number Diff line Loading @@ -25,11 +25,11 @@ import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.StyleResourceValue; import com.android.ide.common.rendering.api.StyleResourceValue; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.impl.ParserFactory; import com.android.layoutlib.bridge.impl.Stack; import com.android.layoutlib.bridge.impl.Stack; import com.android.resources.ResourceType; import com.android.resources.ResourceType; import com.android.util.Pair; import com.android.util.Pair; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException; Loading Loading @@ -206,6 +206,9 @@ public final class BridgeContext extends Activity { * @param parser the parser to add. * @param parser the parser to add. */ */ public void pushParser(BridgeXmlBlockParser parser) { public void pushParser(BridgeXmlBlockParser parser) { if (ParserFactory.LOG_PARSER) { System.out.println("PUSH " + parser.getParser().toString()); } mParserStack.push(parser); mParserStack.push(parser); } } Loading @@ -213,7 +216,10 @@ public final class BridgeContext extends Activity { * Removes the parser at the top of the stack * Removes the parser at the top of the stack */ */ public void popParser() { public void popParser() { mParserStack.pop(); BridgeXmlBlockParser parser = mParserStack.pop(); if (ParserFactory.LOG_PARSER) { System.out.println("POPD " + parser.getParser().toString()); } } } /** /** Loading Loading @@ -346,9 +352,7 @@ public final class BridgeContext extends Activity { // we need to create a pull parser around the layout XML file, and then // we need to create a pull parser around the layout XML file, and then // give that to our XmlBlockParser // give that to our XmlBlockParser try { try { KXmlParser parser = new KXmlParser(); XmlPullParser parser = ParserFactory.create(xml); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(xml), "UTF-8"); //$NON-NLS-1$); // set the resource ref to have correct view cookies // set the resource ref to have correct view cookies mBridgeInflater.setResourceReference(resource); mBridgeInflater.setResourceReference(resource); Loading Loading @@ -687,25 +691,25 @@ public final class BridgeContext extends Activity { */ */ private BridgeTypedArray createStyleBasedTypedArray(StyleResourceValue style, int[] attrs) private BridgeTypedArray createStyleBasedTypedArray(StyleResourceValue style, int[] attrs) throws Resources.NotFoundException { throws Resources.NotFoundException { AtomicBoolean frameworkAttributes = new AtomicBoolean(); AtomicReference<String> attrName = new AtomicReference<String>(); TreeMap<Integer, String> styleNameMap = searchAttrs(attrs, frameworkAttributes, attrName); BridgeTypedArray ta = ((BridgeResources) mSystemResources).newTypeArray(attrs.length, BridgeTypedArray ta = ((BridgeResources) mSystemResources).newTypeArray(attrs.length, style.isFramework(), frameworkAttributes.get(), attrName.get()); false, true, null); // loop through all the values in the style map, and init the TypedArray with // the style we got from the dynamic id for (Entry<Integer, String> styleAttribute : styleNameMap.entrySet()) { int index = styleAttribute.getKey().intValue(); String name = styleAttribute.getValue(); // for each attribute, get its name so that we can search it in the style for (int i = 0 ; i < attrs.length ; i++) { // get the value from the style, or its parent styles. Pair<ResourceType, String> resolvedResource = Bridge.resolveResourceId(attrs[i]); ResourceValue resValue = mRenderResources.findItemInStyle(style, name); if (resolvedResource != null) { String attrName = resolvedResource.getSecond(); // look for the value in the given style ResourceValue resValue = mRenderResources.findItemInStyle(style, attrName); if (resValue != null) { // resolve it to make sure there are no references left. // resolve it to make sure there are no references left. ta.bridgeSetValue(index, name, mRenderResources.resolveResValue(resValue)); ta.bridgeSetValue(i, attrName, mRenderResources.resolveResValue(resValue)); resValue = mRenderResources.resolveResValue(resValue); } } } } ta.sealArray(); ta.sealArray(); Loading tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeInflater.java +2 −5 Original line number Original line Diff line number Diff line Loading @@ -22,10 +22,10 @@ import com.android.ide.common.rendering.api.MergeCookie; import com.android.ide.common.rendering.api.ResourceReference; import com.android.ide.common.rendering.api.ResourceReference; import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.ResourceValue; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.impl.ParserFactory; import com.android.resources.ResourceType; import com.android.resources.ResourceType; import com.android.util.Pair; import com.android.util.Pair; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser; import android.content.Context; import android.content.Context; Loading @@ -36,7 +36,6 @@ import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup; import java.io.File; import java.io.File; import java.io.FileInputStream; /** /** * Custom implementation of {@link LayoutInflater} to handle custom views. * Custom implementation of {@link LayoutInflater} to handle custom views. Loading Loading @@ -175,9 +174,7 @@ public final class BridgeInflater extends LayoutInflater { File f = new File(value.getValue()); File f = new File(value.getValue()); if (f.isFile()) { if (f.isFile()) { try { try { KXmlParser parser = new KXmlParser(); XmlPullParser parser = ParserFactory.create(f); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(f), "UTF-8"); //$NON-NLS-1$ BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser( BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser( parser, bridgeContext, false); parser, bridgeContext, false); Loading tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java +56 −53 Original line number Original line Diff line number Diff line Loading @@ -21,12 +21,12 @@ import com.android.ide.common.rendering.api.LayoutLog; import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.ResourceValue; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.impl.ParserFactory; import com.android.layoutlib.bridge.impl.ResourceHelper; import com.android.layoutlib.bridge.impl.ResourceHelper; import com.android.ninepatch.NinePatch; import com.android.ninepatch.NinePatch; import com.android.resources.ResourceType; import com.android.resources.ResourceType; import com.android.util.Pair; import com.android.util.Pair; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException; Loading Loading @@ -131,14 +131,16 @@ public final class BridgeResources extends Resources { platformStyleable, styleableName); platformStyleable, styleableName); } } private ResourceValue getResourceValue(int id, boolean[] platformResFlag_out) { private Pair<String, ResourceValue> getResourceValue(int id, boolean[] platformResFlag_out) { // first get the String related to this id in the framework // first get the String related to this id in the framework Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(id); Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(id); if (resourceInfo != null) { if (resourceInfo != null) { platformResFlag_out[0] = true; platformResFlag_out[0] = true; return mContext.getRenderResources().getFrameworkResource( String attributeName = resourceInfo.getSecond(); resourceInfo.getFirst(), resourceInfo.getSecond()); return Pair.of(attributeName, mContext.getRenderResources().getFrameworkResource( resourceInfo.getFirst(), attributeName)); } } // didn't find a match in the framework? look in the project. // didn't find a match in the framework? look in the project. Loading @@ -147,8 +149,10 @@ public final class BridgeResources extends Resources { if (resourceInfo != null) { if (resourceInfo != null) { platformResFlag_out[0] = false; platformResFlag_out[0] = false; return mContext.getRenderResources().getProjectResource( String attributeName = resourceInfo.getSecond(); resourceInfo.getFirst(), resourceInfo.getSecond()); return Pair.of(attributeName, mContext.getRenderResources().getProjectResource( resourceInfo.getFirst(), attributeName)); } } } } Loading @@ -157,10 +161,10 @@ public final class BridgeResources extends Resources { @Override @Override public Drawable getDrawable(int id) throws NotFoundException { public Drawable getDrawable(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { return ResourceHelper.getDrawable(value, mContext); return ResourceHelper.getDrawable(value.getSecond(), mContext); } } // id was not found or not resolved. Throw a NotFoundException. // id was not found or not resolved. Throw a NotFoundException. Loading @@ -172,11 +176,11 @@ public final class BridgeResources extends Resources { @Override @Override public int getColor(int id) throws NotFoundException { public int getColor(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { try { try { return ResourceHelper.getColor(value.getValue()); return ResourceHelper.getColor(value.getSecond().getValue()); } catch (NumberFormatException e) { } catch (NumberFormatException e) { Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, e.getMessage(), e, Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, e.getMessage(), e, null /*data*/); null /*data*/); Loading @@ -193,10 +197,11 @@ public final class BridgeResources extends Resources { @Override @Override public ColorStateList getColorStateList(int id) throws NotFoundException { public ColorStateList getColorStateList(int id) throws NotFoundException { ResourceValue resValue = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> resValue = getResourceValue(id, mPlatformResourceFlag); if (resValue != null) { if (resValue != null) { ColorStateList stateList = ResourceHelper.getColorStateList(resValue, mContext); ColorStateList stateList = ResourceHelper.getColorStateList(resValue.getSecond(), mContext); if (stateList != null) { if (stateList != null) { return stateList; return stateList; } } Loading @@ -211,10 +216,10 @@ public final class BridgeResources extends Resources { @Override @Override public CharSequence getText(int id) throws NotFoundException { public CharSequence getText(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { return value.getValue(); return value.getSecond().getValue(); } } // id was not found or not resolved. Throw a NotFoundException. // id was not found or not resolved. Throw a NotFoundException. Loading @@ -226,9 +231,10 @@ public final class BridgeResources extends Resources { @Override @Override public XmlResourceParser getLayout(int id) throws NotFoundException { public XmlResourceParser getLayout(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> v = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (v != null) { ResourceValue value = v.getSecond(); XmlPullParser parser = null; XmlPullParser parser = null; try { try { Loading @@ -243,9 +249,7 @@ public final class BridgeResources extends Resources { if (xml.isFile()) { if (xml.isFile()) { // we need to create a pull parser around the layout XML file, and then // we need to create a pull parser around the layout XML file, and then // give that to our XmlBlockParser // give that to our XmlBlockParser parser = new KXmlParser(); parser = ParserFactory.create(xml); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(xml), "UTF-8"); //$NON-NLS-1$); } } } } Loading @@ -271,9 +275,10 @@ public final class BridgeResources extends Resources { @Override @Override public XmlResourceParser getAnimation(int id) throws NotFoundException { public XmlResourceParser getAnimation(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> v = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (v != null) { ResourceValue value = v.getSecond(); XmlPullParser parser = null; XmlPullParser parser = null; try { try { Loading @@ -281,9 +286,7 @@ public final class BridgeResources extends Resources { if (xml.isFile()) { if (xml.isFile()) { // we need to create a pull parser around the layout XML file, and then // we need to create a pull parser around the layout XML file, and then // give that to our XmlBlockParser // give that to our XmlBlockParser parser = new KXmlParser(); parser = ParserFactory.create(xml); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(xml), "UTF-8"); //$NON-NLS-1$); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); } } Loading Loading @@ -317,10 +320,10 @@ public final class BridgeResources extends Resources { @Override @Override public float getDimension(int id) throws NotFoundException { public float getDimension(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String v = value.getValue(); String v = value.getSecond().getValue(); if (v != null) { if (v != null) { if (v.equals(BridgeConstants.MATCH_PARENT) || if (v.equals(BridgeConstants.MATCH_PARENT) || Loading @@ -330,7 +333,8 @@ public final class BridgeResources extends Resources { return LayoutParams.WRAP_CONTENT; return LayoutParams.WRAP_CONTENT; } } if (ResourceHelper.stringToFloat(v, mTmpValue) && if (ResourceHelper.parseFloatAttribute( value.getFirst(), v, mTmpValue, true /*requireUnit*/) && mTmpValue.type == TypedValue.TYPE_DIMENSION) { mTmpValue.type == TypedValue.TYPE_DIMENSION) { return mTmpValue.getDimension(mMetrics); return mTmpValue.getDimension(mMetrics); } } Loading @@ -346,13 +350,14 @@ public final class BridgeResources extends Resources { @Override @Override public int getDimensionPixelOffset(int id) throws NotFoundException { public int getDimensionPixelOffset(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String v = value.getValue(); String v = value.getSecond().getValue(); if (v != null) { if (v != null) { if (ResourceHelper.stringToFloat(v, mTmpValue) && if (ResourceHelper.parseFloatAttribute( value.getFirst(), v, mTmpValue, true /*requireUnit*/) && mTmpValue.type == TypedValue.TYPE_DIMENSION) { mTmpValue.type == TypedValue.TYPE_DIMENSION) { return TypedValue.complexToDimensionPixelOffset(mTmpValue.data, mMetrics); return TypedValue.complexToDimensionPixelOffset(mTmpValue.data, mMetrics); } } Loading @@ -368,13 +373,14 @@ public final class BridgeResources extends Resources { @Override @Override public int getDimensionPixelSize(int id) throws NotFoundException { public int getDimensionPixelSize(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String v = value.getValue(); String v = value.getSecond().getValue(); if (v != null) { if (v != null) { if (ResourceHelper.stringToFloat(v, mTmpValue) && if (ResourceHelper.parseFloatAttribute( value.getFirst(), v, mTmpValue, true /*requireUnit*/) && mTmpValue.type == TypedValue.TYPE_DIMENSION) { mTmpValue.type == TypedValue.TYPE_DIMENSION) { return TypedValue.complexToDimensionPixelSize(mTmpValue.data, mMetrics); return TypedValue.complexToDimensionPixelSize(mTmpValue.data, mMetrics); } } Loading @@ -390,10 +396,10 @@ public final class BridgeResources extends Resources { @Override @Override public int getInteger(int id) throws NotFoundException { public int getInteger(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null && value.getValue() != null) { if (value != null && value.getSecond().getValue() != null) { String v = value.getValue(); String v = value.getSecond().getValue(); int radix = 10; int radix = 10; if (v.startsWith("0x")) { if (v.startsWith("0x")) { v = v.substring(2); v = v.substring(2); Loading Loading @@ -445,10 +451,10 @@ public final class BridgeResources extends Resources { @Override @Override public String getString(int id) throws NotFoundException { public String getString(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null && value.getValue() != null) { if (value != null && value.getSecond().getValue() != null) { return value.getValue(); return value.getSecond().getValue(); } } // id was not found or not resolved. Throw a NotFoundException. // id was not found or not resolved. Throw a NotFoundException. Loading @@ -461,13 +467,14 @@ public final class BridgeResources extends Resources { @Override @Override public void getValue(int id, TypedValue outValue, boolean resolveRefs) public void getValue(int id, TypedValue outValue, boolean resolveRefs) throws NotFoundException { throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String v = value.getValue(); String v = value.getSecond().getValue(); if (v != null) { if (v != null) { if (ResourceHelper.stringToFloat(v, outValue)) { if (ResourceHelper.parseFloatAttribute(value.getFirst(), v, outValue, false /*requireUnit*/)) { return; return; } } Loading @@ -490,19 +497,17 @@ public final class BridgeResources extends Resources { @Override @Override public XmlResourceParser getXml(int id) throws NotFoundException { public XmlResourceParser getXml(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String v = value.getValue(); String v = value.getSecond().getValue(); if (v != null) { if (v != null) { // check this is a file // check this is a file File f = new File(value.getValue()); File f = new File(v); if (f.isFile()) { if (f.isFile()) { try { try { KXmlParser parser = new KXmlParser(); XmlPullParser parser = ParserFactory.create(f); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(f), "UTF-8"); //$NON-NLS-1$); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); } catch (XmlPullParserException e) { } catch (XmlPullParserException e) { Loading Loading @@ -535,9 +540,7 @@ public final class BridgeResources extends Resources { File f = new File(file); File f = new File(file); try { try { KXmlParser parser = new KXmlParser(); XmlPullParser parser = ParserFactory.create(f); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(f), "UTF-8"); //$NON-NLS-1$); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); } catch (XmlPullParserException e) { } catch (XmlPullParserException e) { Loading @@ -554,10 +557,10 @@ public final class BridgeResources extends Resources { @Override @Override public InputStream openRawResource(int id) throws NotFoundException { public InputStream openRawResource(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String path = value.getValue(); String path = value.getSecond().getValue(); if (path != null) { if (path != null) { // check this is a file // check this is a file Loading tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeTypedArray.java +11 −31 Original line number Original line Diff line number Diff line Loading @@ -24,10 +24,10 @@ import com.android.ide.common.rendering.api.StyleResourceValue; import com.android.internal.util.XmlUtils; import com.android.internal.util.XmlUtils; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.impl.ParserFactory; import com.android.layoutlib.bridge.impl.ResourceHelper; import com.android.layoutlib.bridge.impl.ResourceHelper; import com.android.resources.ResourceType; import com.android.resources.ResourceType; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException; Loading @@ -41,7 +41,6 @@ import android.view.LayoutInflater_Delegate; import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.LayoutParams; import java.io.File; import java.io.File; import java.io.FileInputStream; import java.util.Arrays; import java.util.Arrays; import java.util.Map; import java.util.Map; Loading Loading @@ -211,7 +210,7 @@ public final class BridgeTypedArray extends TypedArray { Map<String, Integer> map = null; Map<String, Integer> map = null; if (mPlatformStyleable) { if (mPlatformStyleable) { map = Bridge.getEnumValues(mNames[index]); map = Bridge.getEnumValues(mNames[index]); } else { } else if (mStyleableName != null) { // get the styleable matching the resolved name // get the styleable matching the resolved name RenderResources res = mContext.getRenderResources(); RenderResources res = mContext.getRenderResources(); ResourceValue styleable = res.getProjectResource(ResourceType.DECLARE_STYLEABLE, ResourceValue styleable = res.getProjectResource(ResourceType.DECLARE_STYLEABLE, Loading Loading @@ -331,9 +330,7 @@ public final class BridgeTypedArray extends TypedArray { File f = new File(value); File f = new File(value); if (f.isFile()) { if (f.isFile()) { try { try { KXmlParser parser = new KXmlParser(); XmlPullParser parser = ParserFactory.create(f); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(f), "UTF-8"); //$NON-NLS-1$); BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser( BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser( parser, mContext, resValue.isFramework()); parser, mContext, resValue.isFramework()); Loading Loading @@ -377,26 +374,7 @@ public final class BridgeTypedArray extends TypedArray { */ */ @Override @Override public int getInteger(int index, int defValue) { public int getInteger(int index, int defValue) { if (mResourceData[index] == null) { return getInt(index, defValue); return defValue; } String s = mResourceData[index].getValue(); if (s != null) { try { return Integer.parseInt(s); } catch (NumberFormatException e) { Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, String.format( "\"%s\" in attribute \"%2$s\" cannont be converted to an integer.", s, mNames[index]), null /*data*/); // The default value is returned below. } } return defValue; } } /** /** Loading Loading @@ -434,7 +412,7 @@ public final class BridgeTypedArray extends TypedArray { return defValue; return defValue; } } if (ResourceHelper.stringToFloat(s, mValue)) { if (ResourceHelper.parseFloatAttribute(mNames[index], s, mValue, true /*requireUnit*/)) { return mValue.getDimension(mBridgeResources.mMetrics); return mValue.getDimension(mBridgeResources.mMetrics); } } Loading Loading @@ -561,7 +539,7 @@ public final class BridgeTypedArray extends TypedArray { throw new RuntimeException(); throw new RuntimeException(); } } if (ResourceHelper.stringToFloat(s, mValue)) { if (ResourceHelper.parseFloatAttribute(mNames[index], s, mValue, true /*requireUnit*/)) { float f = mValue.getDimension(mBridgeResources.mMetrics); float f = mValue.getDimension(mBridgeResources.mMetrics); final int res = (int)(f+0.5f); final int res = (int)(f+0.5f); Loading Loading @@ -599,14 +577,15 @@ public final class BridgeTypedArray extends TypedArray { return defValue; return defValue; } } if (ResourceHelper.stringToFloat(value, mValue)) { if (ResourceHelper.parseFloatAttribute(mNames[index], value, mValue, false /*requireUnit*/)) { return mValue.getFraction(base, pbase); return mValue.getFraction(base, pbase); } } // looks like we were unable to resolve the fraction value // looks like we were unable to resolve the fraction value Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, String.format( String.format( "\"%1$s\" in attribute \"%2$s\" cannont be converted to a fraction.", "\"%1$s\" in attribute \"%2$s\" cannot be converted to a fraction.", value, mNames[index]), null /*data*/); value, mNames[index]), null /*data*/); return defValue; return defValue; Loading Loading @@ -803,7 +782,8 @@ public final class BridgeTypedArray extends TypedArray { String s = mResourceData[index].getValue(); String s = mResourceData[index].getValue(); return ResourceHelper.stringToFloat(s, outValue); return ResourceHelper.parseFloatAttribute(mNames[index], s, outValue, false /*requireUnit*/); } } /** /** Loading tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeXmlBlockParser.java +54 −0 Original line number Original line Diff line number Diff line Loading @@ -18,6 +18,7 @@ package com.android.layoutlib.bridge.android; import com.android.ide.common.rendering.api.ILayoutPullParser; import com.android.ide.common.rendering.api.ILayoutPullParser; import com.android.layoutlib.bridge.impl.ParserFactory; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException; Loading Loading @@ -54,6 +55,10 @@ public class BridgeXmlBlockParser implements XmlResourceParser { * @param platformFile Indicates whether the the file is a platform file or not. * @param platformFile Indicates whether the the file is a platform file or not. */ */ public BridgeXmlBlockParser(XmlPullParser parser, BridgeContext context, boolean platformFile) { public BridgeXmlBlockParser(XmlPullParser parser, BridgeContext context, boolean platformFile) { if (ParserFactory.LOG_PARSER) { System.out.println("CRTE " + parser.toString()); } mParser = parser; mParser = parser; mContext = context; mContext = context; mPlatformFile = platformFile; mPlatformFile = platformFile; Loading @@ -65,6 +70,10 @@ public class BridgeXmlBlockParser implements XmlResourceParser { } } } } public XmlPullParser getParser() { return mParser; } public boolean isPlatformFile() { public boolean isPlatformFile() { return mPlatformFile; return mPlatformFile; } } Loading Loading @@ -247,18 +256,63 @@ public class BridgeXmlBlockParser implements XmlResourceParser { public int next() throws XmlPullParserException, IOException { public int next() throws XmlPullParserException, IOException { if (!mStarted) { if (!mStarted) { mStarted = true; mStarted = true; if (ParserFactory.LOG_PARSER) { System.out.println("STRT " + mParser.toString()); } return START_DOCUMENT; return START_DOCUMENT; } } int ev = mParser.next(); int ev = mParser.next(); if (ParserFactory.LOG_PARSER) { System.out.println("NEXT " + mParser.toString() + " " + eventTypeToString(mEventType) + " -> " + eventTypeToString(ev)); } if (ev == END_TAG && mParser.getDepth() == 1) { if (ev == END_TAG && mParser.getDepth() == 1) { // done with parser remove it from the context stack. // done with parser remove it from the context stack. ensurePopped(); ensurePopped(); if (ParserFactory.LOG_PARSER) { System.out.println(""); } } } mEventType = ev; mEventType = ev; return ev; return ev; } } public static String eventTypeToString(int eventType) { switch (eventType) { case START_DOCUMENT: return "START_DOC"; case END_DOCUMENT: return "END_DOC"; case START_TAG: return "START_TAG"; case END_TAG: return "END_TAG"; case TEXT: return "TEXT"; case CDSECT: return "CDSECT"; case ENTITY_REF: return "ENTITY_REF"; case IGNORABLE_WHITESPACE: return "IGNORABLE_WHITESPACE"; case PROCESSING_INSTRUCTION: return "PROCESSING_INSTRUCTION"; case COMMENT: return "COMMENT"; case DOCDECL: return "DOCDECL"; } return "????"; } public void require(int type, String namespace, String name) public void require(int type, String namespace, String name) throws XmlPullParserException { throws XmlPullParserException { if (type != getEventType() if (type != getEventType() Loading Loading
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java +23 −19 Original line number Original line Diff line number Diff line Loading @@ -25,11 +25,11 @@ import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.StyleResourceValue; import com.android.ide.common.rendering.api.StyleResourceValue; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.impl.ParserFactory; import com.android.layoutlib.bridge.impl.Stack; import com.android.layoutlib.bridge.impl.Stack; import com.android.resources.ResourceType; import com.android.resources.ResourceType; import com.android.util.Pair; import com.android.util.Pair; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException; Loading Loading @@ -206,6 +206,9 @@ public final class BridgeContext extends Activity { * @param parser the parser to add. * @param parser the parser to add. */ */ public void pushParser(BridgeXmlBlockParser parser) { public void pushParser(BridgeXmlBlockParser parser) { if (ParserFactory.LOG_PARSER) { System.out.println("PUSH " + parser.getParser().toString()); } mParserStack.push(parser); mParserStack.push(parser); } } Loading @@ -213,7 +216,10 @@ public final class BridgeContext extends Activity { * Removes the parser at the top of the stack * Removes the parser at the top of the stack */ */ public void popParser() { public void popParser() { mParserStack.pop(); BridgeXmlBlockParser parser = mParserStack.pop(); if (ParserFactory.LOG_PARSER) { System.out.println("POPD " + parser.getParser().toString()); } } } /** /** Loading Loading @@ -346,9 +352,7 @@ public final class BridgeContext extends Activity { // we need to create a pull parser around the layout XML file, and then // we need to create a pull parser around the layout XML file, and then // give that to our XmlBlockParser // give that to our XmlBlockParser try { try { KXmlParser parser = new KXmlParser(); XmlPullParser parser = ParserFactory.create(xml); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(xml), "UTF-8"); //$NON-NLS-1$); // set the resource ref to have correct view cookies // set the resource ref to have correct view cookies mBridgeInflater.setResourceReference(resource); mBridgeInflater.setResourceReference(resource); Loading Loading @@ -687,25 +691,25 @@ public final class BridgeContext extends Activity { */ */ private BridgeTypedArray createStyleBasedTypedArray(StyleResourceValue style, int[] attrs) private BridgeTypedArray createStyleBasedTypedArray(StyleResourceValue style, int[] attrs) throws Resources.NotFoundException { throws Resources.NotFoundException { AtomicBoolean frameworkAttributes = new AtomicBoolean(); AtomicReference<String> attrName = new AtomicReference<String>(); TreeMap<Integer, String> styleNameMap = searchAttrs(attrs, frameworkAttributes, attrName); BridgeTypedArray ta = ((BridgeResources) mSystemResources).newTypeArray(attrs.length, BridgeTypedArray ta = ((BridgeResources) mSystemResources).newTypeArray(attrs.length, style.isFramework(), frameworkAttributes.get(), attrName.get()); false, true, null); // loop through all the values in the style map, and init the TypedArray with // the style we got from the dynamic id for (Entry<Integer, String> styleAttribute : styleNameMap.entrySet()) { int index = styleAttribute.getKey().intValue(); String name = styleAttribute.getValue(); // for each attribute, get its name so that we can search it in the style for (int i = 0 ; i < attrs.length ; i++) { // get the value from the style, or its parent styles. Pair<ResourceType, String> resolvedResource = Bridge.resolveResourceId(attrs[i]); ResourceValue resValue = mRenderResources.findItemInStyle(style, name); if (resolvedResource != null) { String attrName = resolvedResource.getSecond(); // look for the value in the given style ResourceValue resValue = mRenderResources.findItemInStyle(style, attrName); if (resValue != null) { // resolve it to make sure there are no references left. // resolve it to make sure there are no references left. ta.bridgeSetValue(index, name, mRenderResources.resolveResValue(resValue)); ta.bridgeSetValue(i, attrName, mRenderResources.resolveResValue(resValue)); resValue = mRenderResources.resolveResValue(resValue); } } } } ta.sealArray(); ta.sealArray(); Loading
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeInflater.java +2 −5 Original line number Original line Diff line number Diff line Loading @@ -22,10 +22,10 @@ import com.android.ide.common.rendering.api.MergeCookie; import com.android.ide.common.rendering.api.ResourceReference; import com.android.ide.common.rendering.api.ResourceReference; import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.ResourceValue; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.impl.ParserFactory; import com.android.resources.ResourceType; import com.android.resources.ResourceType; import com.android.util.Pair; import com.android.util.Pair; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser; import android.content.Context; import android.content.Context; Loading @@ -36,7 +36,6 @@ import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup; import java.io.File; import java.io.File; import java.io.FileInputStream; /** /** * Custom implementation of {@link LayoutInflater} to handle custom views. * Custom implementation of {@link LayoutInflater} to handle custom views. Loading Loading @@ -175,9 +174,7 @@ public final class BridgeInflater extends LayoutInflater { File f = new File(value.getValue()); File f = new File(value.getValue()); if (f.isFile()) { if (f.isFile()) { try { try { KXmlParser parser = new KXmlParser(); XmlPullParser parser = ParserFactory.create(f); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(f), "UTF-8"); //$NON-NLS-1$ BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser( BridgeXmlBlockParser bridgeParser = new BridgeXmlBlockParser( parser, bridgeContext, false); parser, bridgeContext, false); Loading
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeResources.java +56 −53 Original line number Original line Diff line number Diff line Loading @@ -21,12 +21,12 @@ import com.android.ide.common.rendering.api.LayoutLog; import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.ResourceValue; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.impl.ParserFactory; import com.android.layoutlib.bridge.impl.ResourceHelper; import com.android.layoutlib.bridge.impl.ResourceHelper; import com.android.ninepatch.NinePatch; import com.android.ninepatch.NinePatch; import com.android.resources.ResourceType; import com.android.resources.ResourceType; import com.android.util.Pair; import com.android.util.Pair; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException; Loading Loading @@ -131,14 +131,16 @@ public final class BridgeResources extends Resources { platformStyleable, styleableName); platformStyleable, styleableName); } } private ResourceValue getResourceValue(int id, boolean[] platformResFlag_out) { private Pair<String, ResourceValue> getResourceValue(int id, boolean[] platformResFlag_out) { // first get the String related to this id in the framework // first get the String related to this id in the framework Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(id); Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(id); if (resourceInfo != null) { if (resourceInfo != null) { platformResFlag_out[0] = true; platformResFlag_out[0] = true; return mContext.getRenderResources().getFrameworkResource( String attributeName = resourceInfo.getSecond(); resourceInfo.getFirst(), resourceInfo.getSecond()); return Pair.of(attributeName, mContext.getRenderResources().getFrameworkResource( resourceInfo.getFirst(), attributeName)); } } // didn't find a match in the framework? look in the project. // didn't find a match in the framework? look in the project. Loading @@ -147,8 +149,10 @@ public final class BridgeResources extends Resources { if (resourceInfo != null) { if (resourceInfo != null) { platformResFlag_out[0] = false; platformResFlag_out[0] = false; return mContext.getRenderResources().getProjectResource( String attributeName = resourceInfo.getSecond(); resourceInfo.getFirst(), resourceInfo.getSecond()); return Pair.of(attributeName, mContext.getRenderResources().getProjectResource( resourceInfo.getFirst(), attributeName)); } } } } Loading @@ -157,10 +161,10 @@ public final class BridgeResources extends Resources { @Override @Override public Drawable getDrawable(int id) throws NotFoundException { public Drawable getDrawable(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { return ResourceHelper.getDrawable(value, mContext); return ResourceHelper.getDrawable(value.getSecond(), mContext); } } // id was not found or not resolved. Throw a NotFoundException. // id was not found or not resolved. Throw a NotFoundException. Loading @@ -172,11 +176,11 @@ public final class BridgeResources extends Resources { @Override @Override public int getColor(int id) throws NotFoundException { public int getColor(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { try { try { return ResourceHelper.getColor(value.getValue()); return ResourceHelper.getColor(value.getSecond().getValue()); } catch (NumberFormatException e) { } catch (NumberFormatException e) { Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, e.getMessage(), e, Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, e.getMessage(), e, null /*data*/); null /*data*/); Loading @@ -193,10 +197,11 @@ public final class BridgeResources extends Resources { @Override @Override public ColorStateList getColorStateList(int id) throws NotFoundException { public ColorStateList getColorStateList(int id) throws NotFoundException { ResourceValue resValue = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> resValue = getResourceValue(id, mPlatformResourceFlag); if (resValue != null) { if (resValue != null) { ColorStateList stateList = ResourceHelper.getColorStateList(resValue, mContext); ColorStateList stateList = ResourceHelper.getColorStateList(resValue.getSecond(), mContext); if (stateList != null) { if (stateList != null) { return stateList; return stateList; } } Loading @@ -211,10 +216,10 @@ public final class BridgeResources extends Resources { @Override @Override public CharSequence getText(int id) throws NotFoundException { public CharSequence getText(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { return value.getValue(); return value.getSecond().getValue(); } } // id was not found or not resolved. Throw a NotFoundException. // id was not found or not resolved. Throw a NotFoundException. Loading @@ -226,9 +231,10 @@ public final class BridgeResources extends Resources { @Override @Override public XmlResourceParser getLayout(int id) throws NotFoundException { public XmlResourceParser getLayout(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> v = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (v != null) { ResourceValue value = v.getSecond(); XmlPullParser parser = null; XmlPullParser parser = null; try { try { Loading @@ -243,9 +249,7 @@ public final class BridgeResources extends Resources { if (xml.isFile()) { if (xml.isFile()) { // we need to create a pull parser around the layout XML file, and then // we need to create a pull parser around the layout XML file, and then // give that to our XmlBlockParser // give that to our XmlBlockParser parser = new KXmlParser(); parser = ParserFactory.create(xml); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(xml), "UTF-8"); //$NON-NLS-1$); } } } } Loading @@ -271,9 +275,10 @@ public final class BridgeResources extends Resources { @Override @Override public XmlResourceParser getAnimation(int id) throws NotFoundException { public XmlResourceParser getAnimation(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> v = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (v != null) { ResourceValue value = v.getSecond(); XmlPullParser parser = null; XmlPullParser parser = null; try { try { Loading @@ -281,9 +286,7 @@ public final class BridgeResources extends Resources { if (xml.isFile()) { if (xml.isFile()) { // we need to create a pull parser around the layout XML file, and then // we need to create a pull parser around the layout XML file, and then // give that to our XmlBlockParser // give that to our XmlBlockParser parser = new KXmlParser(); parser = ParserFactory.create(xml); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(xml), "UTF-8"); //$NON-NLS-1$); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); } } Loading Loading @@ -317,10 +320,10 @@ public final class BridgeResources extends Resources { @Override @Override public float getDimension(int id) throws NotFoundException { public float getDimension(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String v = value.getValue(); String v = value.getSecond().getValue(); if (v != null) { if (v != null) { if (v.equals(BridgeConstants.MATCH_PARENT) || if (v.equals(BridgeConstants.MATCH_PARENT) || Loading @@ -330,7 +333,8 @@ public final class BridgeResources extends Resources { return LayoutParams.WRAP_CONTENT; return LayoutParams.WRAP_CONTENT; } } if (ResourceHelper.stringToFloat(v, mTmpValue) && if (ResourceHelper.parseFloatAttribute( value.getFirst(), v, mTmpValue, true /*requireUnit*/) && mTmpValue.type == TypedValue.TYPE_DIMENSION) { mTmpValue.type == TypedValue.TYPE_DIMENSION) { return mTmpValue.getDimension(mMetrics); return mTmpValue.getDimension(mMetrics); } } Loading @@ -346,13 +350,14 @@ public final class BridgeResources extends Resources { @Override @Override public int getDimensionPixelOffset(int id) throws NotFoundException { public int getDimensionPixelOffset(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String v = value.getValue(); String v = value.getSecond().getValue(); if (v != null) { if (v != null) { if (ResourceHelper.stringToFloat(v, mTmpValue) && if (ResourceHelper.parseFloatAttribute( value.getFirst(), v, mTmpValue, true /*requireUnit*/) && mTmpValue.type == TypedValue.TYPE_DIMENSION) { mTmpValue.type == TypedValue.TYPE_DIMENSION) { return TypedValue.complexToDimensionPixelOffset(mTmpValue.data, mMetrics); return TypedValue.complexToDimensionPixelOffset(mTmpValue.data, mMetrics); } } Loading @@ -368,13 +373,14 @@ public final class BridgeResources extends Resources { @Override @Override public int getDimensionPixelSize(int id) throws NotFoundException { public int getDimensionPixelSize(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String v = value.getValue(); String v = value.getSecond().getValue(); if (v != null) { if (v != null) { if (ResourceHelper.stringToFloat(v, mTmpValue) && if (ResourceHelper.parseFloatAttribute( value.getFirst(), v, mTmpValue, true /*requireUnit*/) && mTmpValue.type == TypedValue.TYPE_DIMENSION) { mTmpValue.type == TypedValue.TYPE_DIMENSION) { return TypedValue.complexToDimensionPixelSize(mTmpValue.data, mMetrics); return TypedValue.complexToDimensionPixelSize(mTmpValue.data, mMetrics); } } Loading @@ -390,10 +396,10 @@ public final class BridgeResources extends Resources { @Override @Override public int getInteger(int id) throws NotFoundException { public int getInteger(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null && value.getValue() != null) { if (value != null && value.getSecond().getValue() != null) { String v = value.getValue(); String v = value.getSecond().getValue(); int radix = 10; int radix = 10; if (v.startsWith("0x")) { if (v.startsWith("0x")) { v = v.substring(2); v = v.substring(2); Loading Loading @@ -445,10 +451,10 @@ public final class BridgeResources extends Resources { @Override @Override public String getString(int id) throws NotFoundException { public String getString(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null && value.getValue() != null) { if (value != null && value.getSecond().getValue() != null) { return value.getValue(); return value.getSecond().getValue(); } } // id was not found or not resolved. Throw a NotFoundException. // id was not found or not resolved. Throw a NotFoundException. Loading @@ -461,13 +467,14 @@ public final class BridgeResources extends Resources { @Override @Override public void getValue(int id, TypedValue outValue, boolean resolveRefs) public void getValue(int id, TypedValue outValue, boolean resolveRefs) throws NotFoundException { throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String v = value.getValue(); String v = value.getSecond().getValue(); if (v != null) { if (v != null) { if (ResourceHelper.stringToFloat(v, outValue)) { if (ResourceHelper.parseFloatAttribute(value.getFirst(), v, outValue, false /*requireUnit*/)) { return; return; } } Loading @@ -490,19 +497,17 @@ public final class BridgeResources extends Resources { @Override @Override public XmlResourceParser getXml(int id) throws NotFoundException { public XmlResourceParser getXml(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String v = value.getValue(); String v = value.getSecond().getValue(); if (v != null) { if (v != null) { // check this is a file // check this is a file File f = new File(value.getValue()); File f = new File(v); if (f.isFile()) { if (f.isFile()) { try { try { KXmlParser parser = new KXmlParser(); XmlPullParser parser = ParserFactory.create(f); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(f), "UTF-8"); //$NON-NLS-1$); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); } catch (XmlPullParserException e) { } catch (XmlPullParserException e) { Loading Loading @@ -535,9 +540,7 @@ public final class BridgeResources extends Resources { File f = new File(file); File f = new File(file); try { try { KXmlParser parser = new KXmlParser(); XmlPullParser parser = ParserFactory.create(f); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(f), "UTF-8"); //$NON-NLS-1$); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]); } catch (XmlPullParserException e) { } catch (XmlPullParserException e) { Loading @@ -554,10 +557,10 @@ public final class BridgeResources extends Resources { @Override @Override public InputStream openRawResource(int id) throws NotFoundException { public InputStream openRawResource(int id) throws NotFoundException { ResourceValue value = getResourceValue(id, mPlatformResourceFlag); Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag); if (value != null) { if (value != null) { String path = value.getValue(); String path = value.getSecond().getValue(); if (path != null) { if (path != null) { // check this is a file // check this is a file Loading
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeTypedArray.java +11 −31 Original line number Original line Diff line number Diff line Loading @@ -24,10 +24,10 @@ import com.android.ide.common.rendering.api.StyleResourceValue; import com.android.internal.util.XmlUtils; import com.android.internal.util.XmlUtils; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.BridgeConstants; import com.android.layoutlib.bridge.impl.ParserFactory; import com.android.layoutlib.bridge.impl.ResourceHelper; import com.android.layoutlib.bridge.impl.ResourceHelper; import com.android.resources.ResourceType; import com.android.resources.ResourceType; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException; Loading @@ -41,7 +41,6 @@ import android.view.LayoutInflater_Delegate; import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup.LayoutParams; import java.io.File; import java.io.File; import java.io.FileInputStream; import java.util.Arrays; import java.util.Arrays; import java.util.Map; import java.util.Map; Loading Loading @@ -211,7 +210,7 @@ public final class BridgeTypedArray extends TypedArray { Map<String, Integer> map = null; Map<String, Integer> map = null; if (mPlatformStyleable) { if (mPlatformStyleable) { map = Bridge.getEnumValues(mNames[index]); map = Bridge.getEnumValues(mNames[index]); } else { } else if (mStyleableName != null) { // get the styleable matching the resolved name // get the styleable matching the resolved name RenderResources res = mContext.getRenderResources(); RenderResources res = mContext.getRenderResources(); ResourceValue styleable = res.getProjectResource(ResourceType.DECLARE_STYLEABLE, ResourceValue styleable = res.getProjectResource(ResourceType.DECLARE_STYLEABLE, Loading Loading @@ -331,9 +330,7 @@ public final class BridgeTypedArray extends TypedArray { File f = new File(value); File f = new File(value); if (f.isFile()) { if (f.isFile()) { try { try { KXmlParser parser = new KXmlParser(); XmlPullParser parser = ParserFactory.create(f); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(new FileInputStream(f), "UTF-8"); //$NON-NLS-1$); BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser( BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser( parser, mContext, resValue.isFramework()); parser, mContext, resValue.isFramework()); Loading Loading @@ -377,26 +374,7 @@ public final class BridgeTypedArray extends TypedArray { */ */ @Override @Override public int getInteger(int index, int defValue) { public int getInteger(int index, int defValue) { if (mResourceData[index] == null) { return getInt(index, defValue); return defValue; } String s = mResourceData[index].getValue(); if (s != null) { try { return Integer.parseInt(s); } catch (NumberFormatException e) { Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, String.format( "\"%s\" in attribute \"%2$s\" cannont be converted to an integer.", s, mNames[index]), null /*data*/); // The default value is returned below. } } return defValue; } } /** /** Loading Loading @@ -434,7 +412,7 @@ public final class BridgeTypedArray extends TypedArray { return defValue; return defValue; } } if (ResourceHelper.stringToFloat(s, mValue)) { if (ResourceHelper.parseFloatAttribute(mNames[index], s, mValue, true /*requireUnit*/)) { return mValue.getDimension(mBridgeResources.mMetrics); return mValue.getDimension(mBridgeResources.mMetrics); } } Loading Loading @@ -561,7 +539,7 @@ public final class BridgeTypedArray extends TypedArray { throw new RuntimeException(); throw new RuntimeException(); } } if (ResourceHelper.stringToFloat(s, mValue)) { if (ResourceHelper.parseFloatAttribute(mNames[index], s, mValue, true /*requireUnit*/)) { float f = mValue.getDimension(mBridgeResources.mMetrics); float f = mValue.getDimension(mBridgeResources.mMetrics); final int res = (int)(f+0.5f); final int res = (int)(f+0.5f); Loading Loading @@ -599,14 +577,15 @@ public final class BridgeTypedArray extends TypedArray { return defValue; return defValue; } } if (ResourceHelper.stringToFloat(value, mValue)) { if (ResourceHelper.parseFloatAttribute(mNames[index], value, mValue, false /*requireUnit*/)) { return mValue.getFraction(base, pbase); return mValue.getFraction(base, pbase); } } // looks like we were unable to resolve the fraction value // looks like we were unable to resolve the fraction value Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, String.format( String.format( "\"%1$s\" in attribute \"%2$s\" cannont be converted to a fraction.", "\"%1$s\" in attribute \"%2$s\" cannot be converted to a fraction.", value, mNames[index]), null /*data*/); value, mNames[index]), null /*data*/); return defValue; return defValue; Loading Loading @@ -803,7 +782,8 @@ public final class BridgeTypedArray extends TypedArray { String s = mResourceData[index].getValue(); String s = mResourceData[index].getValue(); return ResourceHelper.stringToFloat(s, outValue); return ResourceHelper.parseFloatAttribute(mNames[index], s, outValue, false /*requireUnit*/); } } /** /** Loading
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeXmlBlockParser.java +54 −0 Original line number Original line Diff line number Diff line Loading @@ -18,6 +18,7 @@ package com.android.layoutlib.bridge.android; import com.android.ide.common.rendering.api.ILayoutPullParser; import com.android.ide.common.rendering.api.ILayoutPullParser; import com.android.layoutlib.bridge.impl.ParserFactory; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException; Loading Loading @@ -54,6 +55,10 @@ public class BridgeXmlBlockParser implements XmlResourceParser { * @param platformFile Indicates whether the the file is a platform file or not. * @param platformFile Indicates whether the the file is a platform file or not. */ */ public BridgeXmlBlockParser(XmlPullParser parser, BridgeContext context, boolean platformFile) { public BridgeXmlBlockParser(XmlPullParser parser, BridgeContext context, boolean platformFile) { if (ParserFactory.LOG_PARSER) { System.out.println("CRTE " + parser.toString()); } mParser = parser; mParser = parser; mContext = context; mContext = context; mPlatformFile = platformFile; mPlatformFile = platformFile; Loading @@ -65,6 +70,10 @@ public class BridgeXmlBlockParser implements XmlResourceParser { } } } } public XmlPullParser getParser() { return mParser; } public boolean isPlatformFile() { public boolean isPlatformFile() { return mPlatformFile; return mPlatformFile; } } Loading Loading @@ -247,18 +256,63 @@ public class BridgeXmlBlockParser implements XmlResourceParser { public int next() throws XmlPullParserException, IOException { public int next() throws XmlPullParserException, IOException { if (!mStarted) { if (!mStarted) { mStarted = true; mStarted = true; if (ParserFactory.LOG_PARSER) { System.out.println("STRT " + mParser.toString()); } return START_DOCUMENT; return START_DOCUMENT; } } int ev = mParser.next(); int ev = mParser.next(); if (ParserFactory.LOG_PARSER) { System.out.println("NEXT " + mParser.toString() + " " + eventTypeToString(mEventType) + " -> " + eventTypeToString(ev)); } if (ev == END_TAG && mParser.getDepth() == 1) { if (ev == END_TAG && mParser.getDepth() == 1) { // done with parser remove it from the context stack. // done with parser remove it from the context stack. ensurePopped(); ensurePopped(); if (ParserFactory.LOG_PARSER) { System.out.println(""); } } } mEventType = ev; mEventType = ev; return ev; return ev; } } public static String eventTypeToString(int eventType) { switch (eventType) { case START_DOCUMENT: return "START_DOC"; case END_DOCUMENT: return "END_DOC"; case START_TAG: return "START_TAG"; case END_TAG: return "END_TAG"; case TEXT: return "TEXT"; case CDSECT: return "CDSECT"; case ENTITY_REF: return "ENTITY_REF"; case IGNORABLE_WHITESPACE: return "IGNORABLE_WHITESPACE"; case PROCESSING_INSTRUCTION: return "PROCESSING_INSTRUCTION"; case COMMENT: return "COMMENT"; case DOCDECL: return "DOCDECL"; } return "????"; } public void require(int type, String namespace, String name) public void require(int type, String namespace, String name) throws XmlPullParserException { throws XmlPullParserException { if (type != getEventType() if (type != getEventType() Loading