diff options
author | Xavier Ducrohet <xav@android.com> | 2011-01-27 18:05:56 -0800 |
---|---|---|
committer | Xavier Ducrohet <xav@android.com> | 2011-01-28 12:42:05 -0800 |
commit | 3bd45f0b16f5ebfafd8080a0f17f71d85c9840ed (patch) | |
tree | 72a0de0eda8b085d7038a3987ef23d1a05000654 /ide_common/src/com/android/ide | |
parent | 40826f2c5a850a1c0b74ec6193181383175f20cf (diff) | |
download | sdk-3bd45f0b16f5ebfafd8080a0f17f71d85c9840ed.zip sdk-3bd45f0b16f5ebfafd8080a0f17f71d85c9840ed.tar.gz sdk-3bd45f0b16f5ebfafd8080a0f17f71d85c9840ed.tar.bz2 |
Change APIs using String instead of ResourceType.
Move ResourceType into resources.jar so that it's accessible
to layoutlib.jar
This is cleaner and allows us to us more efficient EnumMap objects.
Change-Id: If11cbc69ae3ca8bd6c96e6d0ef402570a07af16f
Diffstat (limited to 'ide_common/src/com/android/ide')
3 files changed, 68 insertions, 44 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 bd72632..6fe7f10 100644 --- a/ide_common/src/com/android/ide/common/rendering/LayoutLibrary.java +++ b/ide_common/src/com/android/ide/common/rendering/LayoutLibrary.java @@ -23,6 +23,7 @@ import com.android.ide.common.rendering.api.ILayoutPullParser; import com.android.ide.common.rendering.api.LayoutLog; import com.android.ide.common.rendering.api.Params; import com.android.ide.common.rendering.api.RenderSession; +import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.Result; import com.android.ide.common.rendering.api.ViewInfo; import com.android.ide.common.rendering.api.Params.RenderingMode; @@ -38,6 +39,7 @@ import com.android.layoutlib.api.IProjectCallback; import com.android.layoutlib.api.IResourceValue; import com.android.layoutlib.api.IXmlPullParser; import com.android.layoutlib.api.ILayoutResult.ILayoutViewInfo; +import com.android.resources.ResourceType; import java.io.File; import java.lang.reflect.Constructor; @@ -46,7 +48,9 @@ import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; +import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; /** * Class to use the Layout library. @@ -371,12 +375,11 @@ public class LayoutLibrary { // convert the map of ResourceValue into IResourceValue. Super ugly but works. - @SuppressWarnings("unchecked") - Map<String, Map<String, IResourceValue>> projectMap = - (Map<String, Map<String, IResourceValue>>)(Map) resources.getProjectResources(); - @SuppressWarnings("unchecked") - Map<String, Map<String, IResourceValue>> frameworkMap = - (Map<String, Map<String, IResourceValue>>)(Map) resources.getFrameworkResources(); + + Map<String, Map<String, IResourceValue>> projectMap = convertMap( + resources.getProjectResources()); + Map<String, Map<String, IResourceValue>> frameworkMap = convertMap( + resources.getFrameworkResources()); ILayoutResult result = null; @@ -434,6 +437,21 @@ public class LayoutLibrary { return convertToScene(result); } + @SuppressWarnings("unchecked") + private Map<String, Map<String, IResourceValue>> convertMap( + Map<ResourceType, Map<String, ResourceValue>> map) { + Map<String, Map<String, IResourceValue>> result = + new HashMap<String, Map<String, IResourceValue>>(); + + for (Entry<ResourceType, Map<String, ResourceValue>> entry : map.entrySet()) { + // ugly case but works. + result.put(entry.getKey().getName(), + (Map<String, IResourceValue>)(Map) entry.getValue()); + } + + return result; + } + /** * Converts a {@link ILayoutResult} to a {@link RenderSession}. */ diff --git a/ide_common/src/com/android/ide/common/resources/ResourceResolver.java b/ide_common/src/com/android/ide/common/resources/ResourceResolver.java index ddfe2bc..3948ac5 100644 --- a/ide_common/src/com/android/ide/common/resources/ResourceResolver.java +++ b/ide_common/src/com/android/ide/common/resources/ResourceResolver.java @@ -20,6 +20,7 @@ import com.android.ide.common.rendering.api.LayoutLog; import com.android.ide.common.rendering.api.RenderResources; import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.StyleResourceValue; +import com.android.resources.ResourceType; import java.util.Collection; import java.util.HashMap; @@ -27,7 +28,7 @@ import java.util.Map; public class ResourceResolver extends RenderResources { - private final static String REFERENCE_STYLE = RES_STYLE + "/"; + private final static String REFERENCE_STYLE = ResourceType.STYLE.getName() + "/"; private final static String PREFIX_ANDROID_RESOURCE_REF = "@android:"; private final static String PREFIX_RESOURCE_REF = "@"; private final static String PREFIX_ANDROID_THEME_REF = "?android:"; @@ -35,8 +36,8 @@ public class ResourceResolver extends RenderResources { private final static String PREFIX_ANDROID = "android:"; - private final Map<String, Map<String, ResourceValue>> mProjectResources; - private final Map<String, Map<String, ResourceValue>> mFrameworkResources; + private final Map<ResourceType, Map<String, ResourceValue>> mProjectResources; + private final Map<ResourceType, Map<String, ResourceValue>> mFrameworkResources; private final Map<StyleResourceValue, StyleResourceValue> mStyleInheritanceMap = new HashMap<StyleResourceValue, StyleResourceValue>(); @@ -49,8 +50,8 @@ public class ResourceResolver extends RenderResources { private boolean mIsProjectTheme; private ResourceResolver( - Map<String, Map<String, ResourceValue>> projectResources, - Map<String, Map<String, ResourceValue>> frameworkResources) { + Map<ResourceType, Map<String, ResourceValue>> projectResources, + Map<ResourceType, Map<String, ResourceValue>> frameworkResources) { mProjectResources = projectResources; mFrameworkResources = frameworkResources; } @@ -66,8 +67,8 @@ public class ResourceResolver extends RenderResources { * @return */ public static ResourceResolver create( - Map<String, Map<String, ResourceValue>> projectResources, - Map<String, Map<String, ResourceValue>> frameworkResources, + Map<ResourceType, Map<String, ResourceValue>> projectResources, + Map<ResourceType, Map<String, ResourceValue>> frameworkResources, String themeName, boolean isProjectTheme) { ResourceResolver resolver = new ResourceResolver( @@ -88,11 +89,11 @@ public class ResourceResolver extends RenderResources { return mIsProjectTheme; } - public Map<String, Map<String, ResourceValue>> getProjectResources() { + public Map<ResourceType, Map<String, ResourceValue>> getProjectResources() { return mProjectResources; } - public Map<String, Map<String, ResourceValue>> getFrameworkResources() { + public Map<ResourceType, Map<String, ResourceValue>> getFrameworkResources() { return mFrameworkResources; } @@ -118,12 +119,13 @@ public class ResourceResolver extends RenderResources { ResourceValue theme = null; if (frameworkTheme) { - Map<String, ResourceValue> frameworkStyleMap = mFrameworkResources.get(RES_STYLE); + Map<String, ResourceValue> frameworkStyleMap = mFrameworkResources.get( + ResourceType.STYLE); if (frameworkStyleMap != null) { theme = frameworkStyleMap.get(name); } } else { - Map<String, ResourceValue> projectStyleMap = mProjectResources.get(RES_STYLE); + Map<String, ResourceValue> projectStyleMap = mProjectResources.get(ResourceType.STYLE); if (projectStyleMap != null) { theme = projectStyleMap.get(name); } @@ -149,12 +151,12 @@ public class ResourceResolver extends RenderResources { } @Override - public ResourceValue getFrameworkResource(String resourceType, String resourceName) { + public ResourceValue getFrameworkResource(ResourceType resourceType, String resourceName) { return getResource(resourceType, resourceName, mFrameworkResources); } @Override - public ResourceValue getProjectResource(String resourceType, String resourceName) { + public ResourceValue getProjectResource(ResourceType resourceType, String resourceName) { return getResource(resourceType, resourceName, mProjectResources); } @@ -204,7 +206,7 @@ public class ResourceResolver extends RenderResources { if (segments.length == 2) { // there was a resType in the reference. If it's attr, we ignore it // else, we assert for now. - if (RES_ATTR.equals(segments[0])) { + if (ResourceType.ATTR.getName().equals(segments[0])) { referenceName = segments[1]; } else { // At this time, no support for ?type/name where type is not "attr" @@ -256,7 +258,14 @@ public class ResourceResolver extends RenderResources { segments[1] = segments[1].substring(PREFIX_ANDROID.length()); } - return findResValue(segments[0], segments[1], + ResourceType type = ResourceType.getEnum(segments[0]); + + // unknown type? + if (type == null) { + return null; + } + + return findResValue(type, segments[1], forceFrameworkOnly ? true :frameworkOnly); } @@ -265,7 +274,7 @@ public class ResourceResolver extends RenderResources { } @Override - public ResourceValue resolveValue(String type, String name, String value, + public ResourceValue resolveValue(ResourceType type, String name, String value, boolean isFrameworkValue) { if (value == null) { return null; @@ -317,7 +326,8 @@ public class ResourceResolver extends RenderResources { * @param frameworkOnly if <code>true</code>, the method does not search in the * project resources */ - private ResourceValue findResValue(String resType, String resName, boolean frameworkOnly) { + private ResourceValue findResValue(ResourceType resType, String resName, + boolean frameworkOnly) { // map of ResouceValue for the given type Map<String, ResourceValue> typeMap; @@ -343,7 +353,7 @@ public class ResourceResolver extends RenderResources { // if it was not found and the type is an id, it is possible that the ID was // generated dynamically when compiling the framework resources. // Look for it in the R map. - if (mFrameworkProvider != null && RES_ID.equals(resType)) { + if (mFrameworkProvider != null && resType == ResourceType.ID) { if (mFrameworkProvider.getId(resType, resName) != null) { return new ResourceValue(resType, resName, true); } @@ -351,11 +361,7 @@ public class ResourceResolver extends RenderResources { } // didn't find the resource anywhere. - // This is normal if the resource is an ID that is generated automatically. - // For other resources, we output a warning - if (mLogger != null && - "+id".equals(resType) == false && //$NON-NLS-1$ - "+android:id".equals(resType) == false) { //$NON-NLS-1$ + if (mLogger != null) { mLogger.warning(LayoutLog.TAG_RESOURCES_RESOLVE, "Couldn't resolve resource @" + (frameworkOnly ? "android:" : "") + resType + "/" + resName, @@ -364,8 +370,8 @@ public class ResourceResolver extends RenderResources { return null; } - private ResourceValue getResource(String resourceType, String resourceName, - Map<String, Map<String, ResourceValue>> resourceRepository) { + private ResourceValue getResource(ResourceType resourceType, String resourceName, + Map<ResourceType, Map<String, ResourceValue>> resourceRepository) { Map<String, ResourceValue> typeMap = resourceRepository.get(resourceType); if (typeMap != null) { ResourceValue item = typeMap.get(resourceName); @@ -388,8 +394,8 @@ public class ResourceResolver extends RenderResources { private void computeStyleMaps(String themeName, boolean isProjectTheme) { mThemeName = themeName; mIsProjectTheme = isProjectTheme; - Map<String, ResourceValue> projectStyleMap = mProjectResources.get(RES_STYLE); - Map<String, ResourceValue> frameworkStyleMap = mFrameworkResources.get(RES_STYLE); + Map<String, ResourceValue> projectStyleMap = mProjectResources.get(ResourceType.STYLE); + Map<String, ResourceValue> frameworkStyleMap = mFrameworkResources.get(ResourceType.STYLE); if (projectStyleMap != null && frameworkStyleMap != null) { // first, get the theme 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 2e4cf8e..cda6587 100644 --- a/ide_common/src/com/android/ide/common/resources/ValueResourceParser.java +++ b/ide_common/src/com/android/ide/common/resources/ValueResourceParser.java @@ -18,6 +18,7 @@ package com.android.ide.common.resources; import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.StyleResourceValue; +import com.android.resources.ResourceType; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -35,15 +36,11 @@ public final class ValueResourceParser extends DefaultHandler { private final static String ATTR_TYPE = "type"; private final static String ATTR_PARENT = "parent"; - // Resource type definition - private final static String RES_STYLE = "style"; - private final static String RES_ATTR = "attr"; - private final static String DEFAULT_NS_PREFIX = "android:"; private final static int DEFAULT_NS_PREFIX_LEN = DEFAULT_NS_PREFIX.length(); public interface IValueResourceRepository { - void addResourceValue(String resType, ResourceValue value); + void addResourceValue(ResourceType type, ResourceValue value); } private boolean inResources = false; @@ -87,24 +84,27 @@ public final class ValueResourceParser extends DefaultHandler { inResources = true; } } else if (mDepth == 2 && inResources == true) { - String type; + String typeValue; // if the node is <item>, we get the type from the attribute "type" if (NODE_ITEM.equals(qName)) { - type = attributes.getValue(ATTR_TYPE); + typeValue = attributes.getValue(ATTR_TYPE); } else { // the type is the name of the node. - type = qName; + typeValue = qName; } + ResourceType type = ResourceType.getEnum(typeValue); + if (type != null) { - if (RES_ATTR.equals(type) == false) { + if (type != ResourceType.ATTR) { // get the resource name String name = attributes.getValue(ATTR_NAME); if (name != null) { - if (RES_STYLE.equals(type)) { + if (type == ResourceType.STYLE) { String parent = attributes.getValue(ATTR_PARENT); - mCurrentStyle = new StyleResourceValue(type, name, parent, mIsFramework); + mCurrentStyle = new StyleResourceValue(type, name, parent, + mIsFramework); mRepository.addResourceValue(type, mCurrentStyle); } else { mCurrentValue = new ResourceValue(type, name, mIsFramework); |