OSDN Git Service

Support getResource*Name() methods for Resources
authorDeepanshu Gupta <deepanshu@google.com>
Sat, 19 Mar 2016 02:39:58 +0000 (19:39 -0700)
committerDeepanshu Gupta <deepanshu@google.com>
Thu, 24 Mar 2016 19:02:17 +0000 (12:02 -0700)
The following methods are now supported:
getResourceEntryName
getResourcePackageName
getResourceTypeName
getResourceName

Bug: http://b.android.com/181872
Change-Id: If06b5e0687ab6f8cb20ab77f339870dd08a84ce1

tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/util/DynamicIdMap.java
tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java

index 985dd5a..6c775b9 100644 (file)
@@ -21,6 +21,7 @@ import com.android.ide.common.rendering.api.ArrayResourceValue;
 import com.android.ide.common.rendering.api.DensityBasedResourceValue;
 import com.android.ide.common.rendering.api.LayoutLog;
 import com.android.ide.common.rendering.api.LayoutlibCallback;
+import com.android.ide.common.rendering.api.RenderResources;
 import com.android.ide.common.rendering.api.ResourceValue;
 import com.android.layoutlib.bridge.Bridge;
 import com.android.layoutlib.bridge.BridgeConstants;
@@ -84,7 +85,7 @@ public class Resources_Delegate {
         return new BridgeTypedArray(resources, resources.mContext, numEntries, platformFile);
     }
 
-    private static Pair<String, ResourceValue> getResourceValue(Resources resources, int id,
+    private static Pair<ResourceType, String> getResourceInfo(Resources resources, int id,
             boolean[] platformResFlag_out) {
         // first get the String related to this id in the framework
         Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(id);
@@ -97,11 +98,7 @@ public class Resources_Delegate {
 
         if (resourceInfo != null) {
             platformResFlag_out[0] = true;
-            String attributeName = resourceInfo.getSecond();
-
-            return Pair.of(attributeName,
-                    resources.mContext.getRenderResources().getFrameworkResource(
-                            resourceInfo.getFirst(), attributeName));
+            return resourceInfo;
         }
 
         // didn't find a match in the framework? look in the project.
@@ -110,13 +107,24 @@ public class Resources_Delegate {
 
             if (resourceInfo != null) {
                 platformResFlag_out[0] = false;
-                String attributeName = resourceInfo.getSecond();
-
-                return Pair.of(attributeName,
-                        resources.mContext.getRenderResources().getProjectResource(
-                                resourceInfo.getFirst(), attributeName));
+                return resourceInfo;
             }
         }
+        return null;
+    }
+
+    private static Pair<String, ResourceValue> getResourceValue(Resources resources, int id,
+            boolean[] platformResFlag_out) {
+        Pair<ResourceType, String> resourceInfo =
+                getResourceInfo(resources, id, platformResFlag_out);
+
+        if (resourceInfo != null) {
+            String attributeName = resourceInfo.getSecond();
+            RenderResources renderResources = resources.mContext.getRenderResources();
+            return Pair.of(attributeName, platformResFlag_out[0] ?
+                    renderResources.getFrameworkResource(resourceInfo.getFirst(), attributeName) :
+                    renderResources.getProjectResource(resourceInfo.getFirst(), attributeName));
+        }
 
         return null;
     }
@@ -626,17 +634,57 @@ public class Resources_Delegate {
 
     @LayoutlibDelegate
     static String getResourceEntryName(Resources resources, int resid) throws NotFoundException {
-        throw new UnsupportedOperationException();
+        Pair<ResourceType, String> resourceInfo = getResourceInfo(resources, resid, new boolean[1]);
+        if (resourceInfo != null) {
+            return resourceInfo.getSecond();
+        }
+        throwException(resid, null);
+        return null;
+
     }
 
     @LayoutlibDelegate
     static String getResourceName(Resources resources, int resid) throws NotFoundException {
-        throw new UnsupportedOperationException();
+        boolean[] platformOut = new boolean[1];
+        Pair<ResourceType, String> resourceInfo = getResourceInfo(resources, resid, platformOut);
+        String namespace;
+        if (resourceInfo != null) {
+            if (platformOut[0]) {
+                namespace = SdkConstants.ANDROID_NS_NAME;
+            } else {
+                namespace = resources.mContext.getPackageName();
+                namespace = namespace == null ? SdkConstants.APP_PREFIX : namespace;
+            }
+            return namespace + ':' + resourceInfo.getFirst().getName() + '/' +
+                    resourceInfo.getSecond();
+        }
+        throwException(resid, null);
+        return null;
+    }
+
+    @LayoutlibDelegate
+    static String getResourcePackageName(Resources resources, int resid) throws NotFoundException {
+        boolean[] platformOut = new boolean[1];
+        Pair<ResourceType, String> resourceInfo = getResourceInfo(resources, resid, platformOut);
+        if (resourceInfo != null) {
+            if (platformOut[0]) {
+                return SdkConstants.ANDROID_NS_NAME;
+            }
+            String packageName = resources.mContext.getPackageName();
+            return packageName == null ? SdkConstants.APP_PREFIX : packageName;
+        }
+        throwException(resid, null);
+        return null;
     }
 
     @LayoutlibDelegate
     static String getResourceTypeName(Resources resources, int resid) throws NotFoundException {
-        throw new UnsupportedOperationException();
+        Pair<ResourceType, String> resourceInfo = getResourceInfo(resources, resid, new boolean[1]);
+        if (resourceInfo != null) {
+            return resourceInfo.getFirst().getName();
+        }
+        throwException(resid, null);
+        return null;
     }
 
     @LayoutlibDelegate
@@ -849,22 +897,17 @@ public class Resources_Delegate {
      * @throws NotFoundException
      */
     private static void throwException(Resources resources, int id) throws NotFoundException {
-        // first get the String related to this id in the framework
-        Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(id);
-
-        // if the name is unknown in the framework, get it from the custom view loader.
-        if (resourceInfo == null && resources.mLayoutlibCallback != null) {
-            resourceInfo = resources.mLayoutlibCallback.resolveResourceId(id);
-        }
+        throwException(id, getResourceInfo(resources, id, new boolean[1]));
+    }
 
+    private static void throwException(int id, @Nullable Pair<ResourceType, String> resourceInfo) {
         String message;
         if (resourceInfo != null) {
             message = String.format(
                     "Could not find %1$s resource matching value 0x%2$X (resolved name: %3$s) in current configuration.",
                     resourceInfo.getFirst(), id, resourceInfo.getSecond());
         } else {
-            message = String.format(
-                    "Could not resolve resource value: 0x%1$X.", id);
+            message = String.format("Could not resolve resource value: 0x%1$X.", id);
         }
 
         throw new NotFoundException(message);
index 08a8faf..161bf41 100644 (file)
@@ -27,8 +27,8 @@ import java.util.Map;
 
 public class DynamicIdMap {
 
-    private final Map<Pair<ResourceType, String>, Integer> mDynamicIds = new HashMap<Pair<ResourceType, String>, Integer>();
-    private final SparseArray<Pair<ResourceType, String>> mRevDynamicIds = new SparseArray<Pair<ResourceType, String>>();
+    private final Map<Pair<ResourceType, String>, Integer> mDynamicIds = new HashMap<>();
+    private final SparseArray<Pair<ResourceType, String>> mRevDynamicIds = new SparseArray<>();
     private int mDynamicSeed;
 
     public DynamicIdMap(int seed) {
index f32bb76..1a00cc9 100644 (file)
@@ -168,6 +168,7 @@ public final class CreateInfo implements ICreateInfo {
         "android.content.res.Resources#getLayout",
         "android.content.res.Resources#getResourceEntryName",
         "android.content.res.Resources#getResourceName",
+        "android.content.res.Resources#getResourcePackageName",
         "android.content.res.Resources#getResourceTypeName",
         "android.content.res.Resources#getString",
         "android.content.res.Resources#getStringArray",