diff options
7 files changed, 170 insertions, 80 deletions
diff --git a/ide_common/src/com/android/ide/common/rendering/LayoutLibrary.java b/ide_common/src/com/android/ide/common/rendering/LayoutLibrary.java index 0d6e5e3..240c02f 100644 --- a/ide_common/src/com/android/ide/common/rendering/LayoutLibrary.java +++ b/ide_common/src/com/android/ide/common/rendering/LayoutLibrary.java @@ -283,8 +283,8 @@ public class LayoutLibrary { * @see Bridge#init(String, Map) */ public boolean init(Map<String, String> platformProperties, - File fontLocation, Map<String, - Map<String, Integer>> enumValueMap, + File fontLocation, + Map<String, Map<String, Integer>> enumValueMap, LayoutLog log) { if (mBridge != null) { return mBridge.init(platformProperties, fontLocation, enumValueMap, log); diff --git a/ide_common/src/com/android/ide/common/resources/MultiResourceFile.java b/ide_common/src/com/android/ide/common/resources/MultiResourceFile.java index c6bfeff..6d8ca0a 100644 --- a/ide_common/src/com/android/ide/common/resources/MultiResourceFile.java +++ b/ide_common/src/com/android/ide/common/resources/MultiResourceFile.java @@ -137,10 +137,11 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou /** * Adds a resource item to the list - * @param resType The type of the resource * @param value The value of the resource. */ - public void addResourceValue(ResourceType resType, ResourceValue value) { + public void addResourceValue(ResourceValue value) { + ResourceType resType = value.getResourceType(); + Map<String, ResourceValue> list = mResourceItems.get(resType); // if the list does not exist, create it. diff --git a/ide_common/src/com/android/ide/common/resources/ValueResourceParser.java b/ide_common/src/com/android/ide/common/resources/ValueResourceParser.java index 6bd085e..3a5a6a3 100644 --- a/ide_common/src/com/android/ide/common/resources/ValueResourceParser.java +++ b/ide_common/src/com/android/ide/common/resources/ValueResourceParser.java @@ -16,6 +16,7 @@ package com.android.ide.common.resources; +import com.android.ide.common.rendering.api.AttrResourceValue; import com.android.ide.common.rendering.api.DeclareStyleableResourceValue; import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.StyleResourceValue; @@ -42,7 +43,7 @@ public final class ValueResourceParser extends DefaultHandler { private final static int DEFAULT_NS_PREFIX_LEN = DEFAULT_NS_PREFIX.length(); public interface IValueResourceRepository { - void addResourceValue(ResourceType type, ResourceValue value); + void addResourceValue(ResourceValue value); } private boolean inResources = false; @@ -50,7 +51,7 @@ public final class ValueResourceParser extends DefaultHandler { private ResourceValue mCurrentValue = null; private StyleResourceValue mCurrentStyle = null; private DeclareStyleableResourceValue mCurrentDeclareStyleable = null; - private String mCurrentAttribute = null; + private AttrResourceValue mCurrentAttr; private IValueResourceRepository mRepository; private final boolean mIsFramework; @@ -71,9 +72,12 @@ public final class ValueResourceParser extends DefaultHandler { mCurrentValue = null; mCurrentStyle = null; mCurrentDeclareStyleable = null; + mCurrentAttr = null; } else if (mDepth == 3) { mCurrentValue = null; - mCurrentAttribute = null; + if (mCurrentDeclareStyleable != null) { + mCurrentAttr = null; + } } mDepth--; @@ -90,40 +94,32 @@ public final class ValueResourceParser extends DefaultHandler { inResources = true; } } else if (mDepth == 2 && inResources == true) { - String typeValue; - - // if the node is <item>, we get the type from the attribute "type" - if (NODE_ITEM.equals(qName)) { - typeValue = attributes.getValue(ATTR_TYPE); - } else { - // the type is the name of the node. - typeValue = qName; - } - - ResourceType type = ResourceType.getEnum(typeValue); + ResourceType type = getType(qName, attributes); if (type != null) { - if (type != ResourceType.ATTR) { - // get the resource name - String name = attributes.getValue(ATTR_NAME); - if (name != null) { - switch (type) { - case STYLE: - String parent = attributes.getValue(ATTR_PARENT); - mCurrentStyle = new StyleResourceValue(type, name, parent, - mIsFramework); - mRepository.addResourceValue(type, mCurrentStyle); - break; - case DECLARE_STYLEABLE: - mCurrentDeclareStyleable = new DeclareStyleableResourceValue( - type, name, mIsFramework); - mRepository.addResourceValue(type, mCurrentDeclareStyleable); - break; - default: - mCurrentValue = new ResourceValue(type, name, mIsFramework); - mRepository.addResourceValue(type, mCurrentValue); - break; - } + // get the resource name + String name = attributes.getValue(ATTR_NAME); + if (name != null) { + switch (type) { + case STYLE: + String parent = attributes.getValue(ATTR_PARENT); + mCurrentStyle = new StyleResourceValue(type, name, parent, + mIsFramework); + mRepository.addResourceValue(mCurrentStyle); + break; + case DECLARE_STYLEABLE: + mCurrentDeclareStyleable = new DeclareStyleableResourceValue( + type, name, mIsFramework); + mRepository.addResourceValue(mCurrentDeclareStyleable); + break; + case ATTR: + mCurrentAttr = new AttrResourceValue(type, name, mIsFramework); + mRepository.addResourceValue(mCurrentAttr); + break; + default: + mCurrentValue = new ResourceValue(type, name, mIsFramework); + mRepository.addResourceValue(mCurrentValue); + break; } } } @@ -141,10 +137,23 @@ public final class ValueResourceParser extends DefaultHandler { mCurrentValue = new ResourceValue(null, name, mIsFramework); mCurrentStyle.addValue(mCurrentValue); } else if (mCurrentDeclareStyleable != null) { - mCurrentAttribute = name; + mCurrentAttr = new AttrResourceValue(ResourceType.ATTR, name, mIsFramework); + mCurrentDeclareStyleable.addValue(mCurrentAttr); + } else if (mCurrentAttr != null) { + // get the enum/flag value + String value = attributes.getValue(ATTR_VALUE); + + try { + // Integer.decode/parseInt can't deal with hex value > 0x7FFFFFFF so we + // use Long.decode instead. + mCurrentAttr.addValue(name, (int)(long)Long.decode(value)); + } catch (NumberFormatException e) { + // pass, we'll just ignore this value + } + } } - } else if (mDepth == 4 && mCurrentDeclareStyleable != null) { + } else if (mDepth == 4 && mCurrentAttr != null) { // get the enum/flag name String name = attributes.getValue(ATTR_NAME); String value = attributes.getValue(ATTR_VALUE); @@ -152,8 +161,7 @@ public final class ValueResourceParser extends DefaultHandler { try { // Integer.decode/parseInt can't deal with hex value > 0x7FFFFFFF so we // use Long.decode instead. - mCurrentDeclareStyleable.addValue(mCurrentAttribute, - name, (int)(long)Long.decode(value)); + mCurrentAttr.addValue(name, (int)(long)Long.decode(value)); } catch (NumberFormatException e) { // pass, we'll just ignore this value } @@ -163,6 +171,22 @@ public final class ValueResourceParser extends DefaultHandler { } } + private ResourceType getType(String qName, Attributes attributes) { + String typeValue; + + // if the node is <item>, we get the type from the attribute "type" + if (NODE_ITEM.equals(qName)) { + typeValue = attributes.getValue(ATTR_TYPE); + } else { + // the type is the name of the node. + typeValue = qName; + } + + ResourceType type = ResourceType.getEnum(typeValue); + return type; + } + + @Override public void characters(char[] ch, int start, int length) throws SAXException { if (mCurrentValue != null) { diff --git a/layoutlib_api/sample/src/com/example/android/render/RenderServiceFactory.java b/layoutlib_api/sample/src/com/example/android/render/RenderServiceFactory.java index dffd4ec..0633b7d 100644 --- a/layoutlib_api/sample/src/com/example/android/render/RenderServiceFactory.java +++ b/layoutlib_api/sample/src/com/example/android/render/RenderServiceFactory.java @@ -18,11 +18,11 @@ package com.example.android.render; import com.android.ide.common.log.ILogger; import com.android.ide.common.rendering.LayoutLibrary; +import com.android.ide.common.rendering.api.AttrResourceValue; import com.android.ide.common.rendering.api.DeclareStyleableResourceValue; import com.android.ide.common.rendering.api.IProjectCallback; import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.resources.FrameworkResources; -import com.android.ide.common.resources.ResourceItem; import com.android.ide.common.resources.ResourceRepository; import com.android.ide.common.resources.ResourceResolver; import com.android.ide.common.resources.configuration.DensityQualifier; @@ -59,7 +59,6 @@ import com.android.sdklib.internal.project.ProjectProperties; import java.io.File; import java.io.IOException; import java.util.HashMap; -import java.util.List; import java.util.Map; /** @@ -275,22 +274,40 @@ public class RenderServiceFactory { // load the framework resources mResources = loadResources(resFolder, log); - // need to get the styleable info to find the enum/flag map. - List<ResourceItem> items = mResources.getResourceItemsOfType( - ResourceType.DECLARE_STYLEABLE); - + // get all the attr values. HashMap<String, Map<String, Integer>> enumMap = new HashMap<String, Map<String, Integer>>(); - // standard default config. FolderConfiguration config = new FolderConfiguration(); - for (ResourceItem item : items) { - ResourceValue value = item.getResourceValue( - ResourceType.DECLARE_STYLEABLE, config, true /*isFramework*/); + Map<ResourceType, Map<String, ResourceValue>> res = + mResources.getConfiguredResources(config); + + // get the ATTR values + Map<String, ResourceValue> attrItems = res.get(ResourceType.ATTR); + for (ResourceValue value : attrItems.values()) { + if (value instanceof AttrResourceValue) { + AttrResourceValue attr = (AttrResourceValue) value; + Map<String, Integer> values = attr.getAttributeValues(); + if (values != null) { + enumMap.put(attr.getName(), values); + } + } + } + + // get the declare-styleable values + Map<String, ResourceValue> styleableItems = res.get(ResourceType.DECLARE_STYLEABLE); + + // get the attr from the styleable + for (ResourceValue value : styleableItems.values()) { if (value instanceof DeclareStyleableResourceValue) { - DeclareStyleableResourceValue styleable = (DeclareStyleableResourceValue) value; - Map<String, Map<String, Integer>> map = styleable.getAllAttributes(); - if (map != null) { - enumMap.putAll(map); + DeclareStyleableResourceValue dsrc = (DeclareStyleableResourceValue) value; + Map<String, AttrResourceValue> attrs = dsrc.getAllAttributes(); + if (attrs != null && attrs.size() > 0) { + for (AttrResourceValue attr : attrs.values()) { + Map<String, Integer> values = attr.getAttributeValues(); + if (values != null) { + enumMap.put(attr.getName(), values); + } + } } } } diff --git a/layoutlib_api/sample/testproject/res/layout/main.xml b/layoutlib_api/sample/testproject/res/layout/main.xml index b79cddb..c1d0043 100644 --- a/layoutlib_api/sample/testproject/res/layout/main.xml +++ b/layoutlib_api/sample/testproject/res/layout/main.xml @@ -5,9 +5,10 @@ android:layout_height="fill_parent" > <TextView - android:layout_width="fill_parent" - android:layout_height="wrap_content" android:text="Hello World, Main" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="right" /> </LinearLayout> diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/AttrResourceValue.java b/layoutlib_api/src/com/android/ide/common/rendering/api/AttrResourceValue.java new file mode 100644 index 0000000..530e3d5 --- /dev/null +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/AttrResourceValue.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.ide.common.rendering.api; + +import com.android.resources.ResourceType; + +import java.util.HashMap; +import java.util.Map; + +/** + * A Resource value representing an attr resource. + * + * {@link #getValue()} will return null, instead use {@link #getAttributeValues()} to + * get the enum/flag value associated with an attribute defined in the declare-styleable. + * + */ +public class AttrResourceValue extends ResourceValue { + + private Map<String, Integer> mValueMap; + + + public AttrResourceValue(ResourceType type, String name, boolean isFramework) { + super(type, name, isFramework); + } + + /** + * Return the enum/flag integer values. + * + * @return the map of (name, integer) values. Can be null. + */ + public Map<String, Integer> getAttributeValues() { + return mValueMap; + } + + public void addValue(String name, Integer value) { + if (mValueMap == null) { + mValueMap = new HashMap<String, Integer>(); + } + + mValueMap.put(name, value); + } +} diff --git a/layoutlib_api/src/com/android/ide/common/rendering/api/DeclareStyleableResourceValue.java b/layoutlib_api/src/com/android/ide/common/rendering/api/DeclareStyleableResourceValue.java index 0699766..45679b2 100644 --- a/layoutlib_api/src/com/android/ide/common/rendering/api/DeclareStyleableResourceValue.java +++ b/layoutlib_api/src/com/android/ide/common/rendering/api/DeclareStyleableResourceValue.java @@ -30,11 +30,10 @@ import java.util.Map; */ public class DeclareStyleableResourceValue extends ResourceValue { - private Map<String, Map<String, Integer>> mEnumMap; + private Map<String, AttrResourceValue> mAttrMap; public DeclareStyleableResourceValue(ResourceType type, String name, boolean isFramework) { super(type, name, isFramework); - } /** @@ -43,33 +42,25 @@ public class DeclareStyleableResourceValue extends ResourceValue { * @return the map of (name, integer) values. */ public Map<String, Integer> getAttributeValues(String name) { - if (mEnumMap != null) { - return mEnumMap.get(name); + if (mAttrMap != null) { + AttrResourceValue attr = mAttrMap.get(name); + if (attr != null) { + return attr.getAttributeValues(); + } } return null; } - public Map<String, Map<String, Integer>> getAllAttributes() { - return mEnumMap; + public Map<String, AttrResourceValue> getAllAttributes() { + return mAttrMap; } - public void addValue(String attribute, String name, Integer value) { - Map<String, Integer> map; - - if (mEnumMap == null) { - mEnumMap = new HashMap<String, Map<String,Integer>>(); - - map = new HashMap<String, Integer>(); - mEnumMap.put(attribute, map); - } else { - map = mEnumMap.get(attribute); - if (map == null) { - map = new HashMap<String, Integer>(); - mEnumMap.put(attribute, map); - } + public void addValue(AttrResourceValue attr) { + if (mAttrMap == null) { + mAttrMap = new HashMap<String, AttrResourceValue>(); } - map.put(name, value); + mAttrMap.put(attr.getName(), attr); } } |