Merge "Support getResource*Name() methods for Resources" into nyc-dev

This commit is contained in:
Deepanshu Gupta
2016-03-24 19:04:20 +00:00
committed by Android (Google) Code Review
3 changed files with 69 additions and 25 deletions

View 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.DensityBasedResourceValue;
import com.android.ide.common.rendering.api.LayoutLog; import com.android.ide.common.rendering.api.LayoutLog;
import com.android.ide.common.rendering.api.LayoutlibCallback; 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.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;
@@ -84,7 +85,7 @@ public class Resources_Delegate {
return new BridgeTypedArray(resources, resources.mContext, numEntries, platformFile); 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) { 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);
@@ -97,11 +98,7 @@ public class Resources_Delegate {
if (resourceInfo != null) { if (resourceInfo != null) {
platformResFlag_out[0] = true; platformResFlag_out[0] = true;
String attributeName = resourceInfo.getSecond(); return resourceInfo;
return Pair.of(attributeName,
resources.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.
@@ -110,13 +107,24 @@ public class Resources_Delegate {
if (resourceInfo != null) { if (resourceInfo != null) {
platformResFlag_out[0] = false; platformResFlag_out[0] = false;
String attributeName = resourceInfo.getSecond(); return resourceInfo;
return Pair.of(attributeName,
resources.mContext.getRenderResources().getProjectResource(
resourceInfo.getFirst(), attributeName));
} }
} }
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; return null;
} }
@@ -626,17 +634,57 @@ public class Resources_Delegate {
@LayoutlibDelegate @LayoutlibDelegate
static String getResourceEntryName(Resources resources, int resid) throws NotFoundException { 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 @LayoutlibDelegate
static String getResourceName(Resources resources, int resid) throws NotFoundException { 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 @LayoutlibDelegate
static String getResourceTypeName(Resources resources, int resid) throws NotFoundException { 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 @LayoutlibDelegate
@@ -849,22 +897,17 @@ public class Resources_Delegate {
* @throws NotFoundException * @throws NotFoundException
*/ */
private static void throwException(Resources resources, int id) throws NotFoundException { private static void throwException(Resources resources, int id) throws NotFoundException {
// first get the String related to this id in the framework throwException(id, getResourceInfo(resources, id, new boolean[1]));
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);
} }
private static void throwException(int id, @Nullable Pair<ResourceType, String> resourceInfo) {
String message; String message;
if (resourceInfo != null) { if (resourceInfo != null) {
message = String.format( message = String.format(
"Could not find %1$s resource matching value 0x%2$X (resolved name: %3$s) in current configuration.", "Could not find %1$s resource matching value 0x%2$X (resolved name: %3$s) in current configuration.",
resourceInfo.getFirst(), id, resourceInfo.getSecond()); resourceInfo.getFirst(), id, resourceInfo.getSecond());
} else { } else {
message = String.format( message = String.format("Could not resolve resource value: 0x%1$X.", id);
"Could not resolve resource value: 0x%1$X.", id);
} }
throw new NotFoundException(message); throw new NotFoundException(message);

View File

@@ -27,8 +27,8 @@ import java.util.Map;
public class DynamicIdMap { public class DynamicIdMap {
private final Map<Pair<ResourceType, String>, Integer> mDynamicIds = new HashMap<Pair<ResourceType, String>, Integer>(); private final Map<Pair<ResourceType, String>, Integer> mDynamicIds = new HashMap<>();
private final SparseArray<Pair<ResourceType, String>> mRevDynamicIds = new SparseArray<Pair<ResourceType, String>>(); private final SparseArray<Pair<ResourceType, String>> mRevDynamicIds = new SparseArray<>();
private int mDynamicSeed; private int mDynamicSeed;
public DynamicIdMap(int seed) { public DynamicIdMap(int seed) {

View File

@@ -168,6 +168,7 @@ public final class CreateInfo implements ICreateInfo {
"android.content.res.Resources#getLayout", "android.content.res.Resources#getLayout",
"android.content.res.Resources#getResourceEntryName", "android.content.res.Resources#getResourceEntryName",
"android.content.res.Resources#getResourceName", "android.content.res.Resources#getResourceName",
"android.content.res.Resources#getResourcePackageName",
"android.content.res.Resources#getResourceTypeName", "android.content.res.Resources#getResourceTypeName",
"android.content.res.Resources#getString", "android.content.res.Resources#getString",
"android.content.res.Resources#getStringArray", "android.content.res.Resources#getStringArray",