Resources_Delegate.getColor should access statelists
When accessing a color resource through Resources.getColor, if the
resource resolves to a color value, then the method returns that color.
But if it resolves to a color state list, it returns the default color
for that state list. Resources_Delegate.getColor should do the same
instead of failing for state lists.
Bug: N/A
Test: layoutlib tests
Change-Id: I9bcf12d16e47bac3b36d9e036ed6fb2f615d70e8
(cherry picked from commit c0c5c7e92ecbde4027df54ec4b6b38101f0b11ae)
Merged-In: I9bcf12d16e47bac3b36d9e036ed6fb2f615d70e8
diff --git a/bridge/src/android/content/res/Resources_Delegate.java b/bridge/src/android/content/res/Resources_Delegate.java
index f453aec..7aa02f9 100644
--- a/bridge/src/android/content/res/Resources_Delegate.java
+++ b/bridge/src/android/content/res/Resources_Delegate.java
@@ -220,26 +220,19 @@
try {
return ResourceHelper.getColor(resourceValue.getValue());
} catch (NumberFormatException e) {
- // Check if the value passed is a file. If it is, mostly likely, user is referencing
- // a color state list from a place where they should reference only a pure color.
- AssetRepository repository = getAssetRepository(resources);
- String message;
- if (repository.isFileResource(resourceValue.getValue())) {
- String resource = (resourceValue.isFramework() ? "@android:" : "@") + "color/"
- + resourceValue.getName();
- message = "Hexadecimal color expected, found Color State List for " + resource;
- } else {
- message = e.getMessage();
+ ColorStateList stateList = ResourceHelper.getColorStateList(resourceValue,
+ getContext(resources), theme);
+ if (stateList != null) {
+ return stateList.getDefaultColor();
}
- Bridge.getLog().error(ILayoutLog.TAG_RESOURCES_FORMAT, message, e, null, null);
+ Bridge.getLog().error(ILayoutLog.TAG_RESOURCES_FORMAT, resourceValue.getName() +
+ " is neither a color value nor a color state list", null, null);
return 0;
}
}
- // Suppress possible NPE. getColorStateList will never return null, it will instead
- // throw an exception, but intelliJ can't figure that out
- //noinspection ConstantConditions
- return getColorStateList(resources, id, theme).getDefaultColor();
+ throwException(resources, id);
+ return 0;
}
@LayoutlibDelegate