diff options
author | Deepanshu Gupta <deepanshu@google.com> | 2015-08-26 20:19:23 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-08-26 20:19:23 +0000 |
commit | f2bf5470a3ec1f5eb8584e0d7c1a8eb3c08d8d20 (patch) | |
tree | a67e9ad8aeac15ceee957dba0d85d20692f5b898 /tools | |
parent | 7ec7805a50893be6242a41de01299cb1de44b461 (diff) | |
parent | 91e456d235256c385a7da1e45f098386bc68e86a (diff) | |
download | frameworks_base-f2bf5470a3ec1f5eb8584e0d7c1a8eb3c08d8d20.zip frameworks_base-f2bf5470a3ec1f5eb8584e0d7c1a8eb3c08d8d20.tar.gz frameworks_base-f2bf5470a3ec1f5eb8584e0d7c1a8eb3c08d8d20.tar.bz2 |
am 91e456d2: am e698bb27: Fix BridgeTypedArray.getType() [DO NOT MERGE]
* commit '91e456d235256c385a7da1e45f098386bc68e86a':
Fix BridgeTypedArray.getType() [DO NOT MERGE]
Diffstat (limited to 'tools')
-rw-r--r-- | tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java | 121 |
1 files changed, 119 insertions, 2 deletions
diff --git a/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java b/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java index 56936eb..d54e3d7 100644 --- a/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java +++ b/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java @@ -45,7 +45,24 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Map; -import static com.android.ide.common.rendering.api.RenderResources.*; +import static android.util.TypedValue.TYPE_ATTRIBUTE; +import static android.util.TypedValue.TYPE_DIMENSION; +import static android.util.TypedValue.TYPE_FLOAT; +import static android.util.TypedValue.TYPE_INT_BOOLEAN; +import static android.util.TypedValue.TYPE_INT_COLOR_ARGB4; +import static android.util.TypedValue.TYPE_INT_COLOR_ARGB8; +import static android.util.TypedValue.TYPE_INT_COLOR_RGB4; +import static android.util.TypedValue.TYPE_INT_COLOR_RGB8; +import static android.util.TypedValue.TYPE_INT_DEC; +import static android.util.TypedValue.TYPE_INT_HEX; +import static android.util.TypedValue.TYPE_NULL; +import static android.util.TypedValue.TYPE_REFERENCE; +import static android.util.TypedValue.TYPE_STRING; +import static com.android.SdkConstants.PREFIX_RESOURCE_REF; +import static com.android.SdkConstants.PREFIX_THEME_REF; +import static com.android.ide.common.rendering.api.RenderResources.REFERENCE_EMPTY; +import static com.android.ide.common.rendering.api.RenderResources.REFERENCE_NULL; +import static com.android.ide.common.rendering.api.RenderResources.REFERENCE_UNDEFINED; /** * Custom implementation of TypedArray to handle non compiled resources. @@ -223,7 +240,7 @@ public final class BridgeTypedArray extends TypedArray { String s = getString(index); try { if (s != null) { - return XmlUtils.convertValueToInt(s, defValue); + return convertValueToInt(s, defValue); } } catch (NumberFormatException e) { Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, @@ -762,6 +779,60 @@ public final class BridgeTypedArray extends TypedArray { return s != null && ResourceHelper.parseFloatAttribute(mNames[index], s, outValue, false); } + @Override + @SuppressWarnings("ResultOfMethodCallIgnored") + public int getType(int index) { + String value = getString(index); + if (value == null) { + return TYPE_NULL; + } + if (value.startsWith(PREFIX_RESOURCE_REF)) { + return TYPE_REFERENCE; + } + if (value.startsWith(PREFIX_THEME_REF)) { + return TYPE_ATTRIBUTE; + } + try { + // Don't care about the value. Only called to check if an exception is thrown. + convertValueToInt(value, 0); + if (value.startsWith("0x") || value.startsWith("0X")) { + return TYPE_INT_HEX; + } + // is it a color? + if (value.startsWith("#")) { + int length = value.length() - 1; + if (length == 3) { // rgb + return TYPE_INT_COLOR_RGB4; + } + if (length == 4) { // argb + return TYPE_INT_COLOR_ARGB4; + } + if (length == 6) { // rrggbb + return TYPE_INT_COLOR_RGB8; + } + if (length == 8) { // aarrggbb + return TYPE_INT_COLOR_ARGB8; + } + } + if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) { + return TYPE_INT_BOOLEAN; + } + return TYPE_INT_DEC; + } catch (NumberFormatException ignored) { + try { + Float.parseFloat(value); + return TYPE_FLOAT; + } catch (NumberFormatException ignore) { + } + // Might be a dimension. + if (ResourceHelper.parseFloatAttribute(null, value, new TypedValue(), false)) { + return TYPE_DIMENSION; + } + } + // TODO: handle fractions. + return TYPE_STRING; + } + /** * Determines whether there is an attribute at <var>index</var>. * @@ -871,6 +942,52 @@ public final class BridgeTypedArray extends TypedArray { return null; } + /** + * Copied from {@link XmlUtils#convertValueToInt(CharSequence, int)}, but adapted to account + * for aapt, and the fact that host Java VM's Integer.parseInt("XXXXXXXX", 16) cannot handle + * "XXXXXXXX" > 80000000. + */ + private static int convertValueToInt(@Nullable String charSeq, int defValue) { + if (null == charSeq) + return defValue; + + int sign = 1; + int index = 0; + int len = charSeq.length(); + int base = 10; + + if ('-' == charSeq.charAt(0)) { + sign = -1; + index++; + } + + if ('0' == charSeq.charAt(index)) { + // Quick check for a zero by itself + if (index == (len - 1)) + return 0; + + char c = charSeq.charAt(index + 1); + + if ('x' == c || 'X' == c) { + index += 2; + base = 16; + } else { + index++; + // Leave the base as 10. aapt removes the preceding zero, and thus when framework + // sees the value, it only gets the decimal value. + } + } else if ('#' == charSeq.charAt(index)) { + return ResourceHelper.getColor(charSeq) * sign; + } else if ("true".equals(charSeq) || "TRUE".equals(charSeq)) { + return -1; + } else if ("false".equals(charSeq) || "FALSE".equals(charSeq)) { + return 0; + } + + // Use Long, since we want to handle hex ints > 80000000. + return ((int)Long.parseLong(charSeq.substring(index), base)) * sign; + } + static TypedArray obtain(Resources res, int len) { return res instanceof BridgeResources ? new BridgeTypedArray(((BridgeResources) res), null, len, true) : null; |