diff --git a/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java b/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java index 985dd5a5f9590..6c775b906d1e9 100644 --- a/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java +++ b/tools/layoutlib/bridge/src/android/content/res/Resources_Delegate.java @@ -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 getResourceValue(Resources resources, int id, + private static Pair getResourceInfo(Resources resources, int id, boolean[] platformResFlag_out) { // first get the String related to this id in the framework Pair 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 getResourceValue(Resources resources, int id, + boolean[] platformResFlag_out) { + Pair 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 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 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 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 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 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 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); diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/util/DynamicIdMap.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/util/DynamicIdMap.java index 08a8faf0bbfb4..161bf4155a1ad 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/util/DynamicIdMap.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/util/DynamicIdMap.java @@ -27,8 +27,8 @@ import java.util.Map; public class DynamicIdMap { - private final Map, Integer> mDynamicIds = new HashMap, Integer>(); - private final SparseArray> mRevDynamicIds = new SparseArray>(); + private final Map, Integer> mDynamicIds = new HashMap<>(); + private final SparseArray> mRevDynamicIds = new SparseArray<>(); private int mDynamicSeed; public DynamicIdMap(int seed) { diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java index f32bb7667c7be..1a00cc942c42c 100644 --- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java +++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java @@ -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",