diff options
Diffstat (limited to 'bridge/src/android/content/res/BridgeTypedArray.java')
-rw-r--r-- | bridge/src/android/content/res/BridgeTypedArray.java | 245 |
1 files changed, 131 insertions, 114 deletions
diff --git a/bridge/src/android/content/res/BridgeTypedArray.java b/bridge/src/android/content/res/BridgeTypedArray.java index 5023dcb2e8..eb603f0ebe 100644 --- a/bridge/src/android/content/res/BridgeTypedArray.java +++ b/bridge/src/android/content/res/BridgeTypedArray.java @@ -18,7 +18,7 @@ package android.content.res; import com.android.ide.common.rendering.api.ArrayResourceValue; import com.android.ide.common.rendering.api.AttrResourceValue; -import com.android.ide.common.rendering.api.LayoutLog; +import com.android.ide.common.rendering.api.ILayoutLog; import com.android.ide.common.rendering.api.RenderResources; import com.android.ide.common.rendering.api.ResourceNamespace; import com.android.ide.common.rendering.api.ResourceNamespace.Resolver; @@ -26,6 +26,7 @@ import com.android.ide.common.rendering.api.ResourceReference; import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.StyleResourceValue; import com.android.ide.common.rendering.api.TextResourceValue; +import com.android.ide.common.resources.ValueXmlHelper; import com.android.internal.util.XmlUtils; import com.android.layoutlib.bridge.Bridge; import com.android.layoutlib.bridge.android.BridgeContext; @@ -73,6 +74,8 @@ import static com.android.ide.common.rendering.api.RenderResources.REFERENCE_UND * Custom implementation of TypedArray to handle non compiled resources. */ public final class BridgeTypedArray extends TypedArray { + private static final String MATCH_PARENT_INT_STRING = String.valueOf(LayoutParams.MATCH_PARENT); + private static final String WRAP_CONTENT_INT_STRING = String.valueOf(LayoutParams.WRAP_CONTENT); private final Resources mBridgeResources; private final BridgeContext mContext; @@ -208,7 +211,9 @@ public final class BridgeTypedArray extends TypedArray { ResourceValue resourceValue = mResourceData[index]; String value = resourceValue.getValue(); if (resourceValue instanceof TextResourceValue) { - String rawValue = resourceValue.getRawXmlValue(); + String rawValue = + ValueXmlHelper.unescapeResourceString(resourceValue.getRawXmlValue(), + true, false); if (rawValue != null && !rawValue.equals(value)) { return Html.fromHtml(rawValue, FROM_HTML_MODE_COMPACT); } @@ -265,7 +270,7 @@ public final class BridgeTypedArray extends TypedArray { try { return convertValueToInt(s, defValue); } catch (NumberFormatException e) { - Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, + Bridge.getLog().warning(ILayoutLog.TAG_RESOURCES_FORMAT, String.format("\"%1$s\" in attribute \"%2$s\" is not a valid integer", s, mNames[index]), null, null); @@ -288,7 +293,7 @@ public final class BridgeTypedArray extends TypedArray { return Float.parseFloat(s); } } catch (NumberFormatException e) { - Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, + Bridge.getLog().warning(ILayoutLog.TAG_RESOURCES_FORMAT, String.format("\"%1$s\" in attribute \"%2$s\" cannot be converted to float.", s, mNames[index]), null, null); @@ -382,13 +387,11 @@ public final class BridgeTypedArray extends TypedArray { return defValue; } // Check if the value is a magic constant that doesn't require a unit. - try { - int i = Integer.parseInt(s); - if (i == LayoutParams.MATCH_PARENT || i == LayoutParams.WRAP_CONTENT) { - return i; - } - } catch (NumberFormatException ignored) { - // pass + if (MATCH_PARENT_INT_STRING.equals(s)) { + return LayoutParams.MATCH_PARENT; + } + if (WRAP_CONTENT_INT_STRING.equals(s)) { + return LayoutParams.WRAP_CONTENT; } if (ResourceHelper.parseFloatAttribute(mNames[index], s, mValue, true)) { @@ -440,21 +443,33 @@ public final class BridgeTypedArray extends TypedArray { */ @Override public int getDimensionPixelSize(int index, int defValue) { - try { - return getDimension(index, null); - } catch (RuntimeException e) { - String s = getString(index); + String s = getString(index); + if (s == null) { + return defValue; + } - if (s != null) { - // looks like we were unable to resolve the dimension value - Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, - String.format("\"%1$s\" in attribute \"%2$s\" is not a valid format.", - s, mNames[index]), - null, null); - } + if (MATCH_PARENT_INT_STRING.equals(s)) { + return LayoutParams.MATCH_PARENT; + } + if (WRAP_CONTENT_INT_STRING.equals(s)) { + return LayoutParams.WRAP_CONTENT; + } - return defValue; + if (ResourceHelper.parseFloatAttribute(mNames[index], s, mValue, true)) { + float f = mValue.getDimension(mBridgeResources.getDisplayMetrics()); + + final int res = (int) (f + 0.5f); + if (res != 0) return res; + if (f == 0) return 0; + if (f > 0) return 1; } + + // looks like we were unable to resolve the dimension value + Bridge.getLog().warning(ILayoutLog.TAG_RESOURCES_FORMAT, + String.format("\"%1$s\" in attribute \"%2$s\" is not a valid format.", s, mNames[index]), + null, null); + + return defValue; } /** @@ -471,21 +486,34 @@ public final class BridgeTypedArray extends TypedArray { */ @Override public int getLayoutDimension(int index, String name) { - try { - // this will throw an exception if not found. - return getDimension(index, name); - } catch (RuntimeException e) { - - if (LayoutInflater_Delegate.sIsInInclude) { - throw new RuntimeException("Layout Dimension '" + name + "' not found."); + String s = getString(index); + if (s != null) { + // Check if the value is a magic constant that doesn't require a unit. + if (MATCH_PARENT_INT_STRING.equals(s)) { + return LayoutParams.MATCH_PARENT; + } + if (WRAP_CONTENT_INT_STRING.equals(s)) { + return LayoutParams.WRAP_CONTENT; } - Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, - "You must supply a " + name + " attribute.", - null, null); + if (ResourceHelper.parseFloatAttribute(mNames[index], s, mValue, true)) { + float f = mValue.getDimension(mBridgeResources.getDisplayMetrics()); - return 0; + final int res = (int) (f + 0.5f); + if (res != 0) return res; + if (f == 0) return 0; + if (f > 0) return 1; + } + } + + if (LayoutInflater_Delegate.sIsInInclude) { + throw new RuntimeException("Layout Dimension '" + name + "' not found."); } + + Bridge.getLog().warning(ILayoutLog.TAG_RESOURCES_FORMAT, + "You must supply a " + name + " attribute.", null, null); + + return 0; } @Override @@ -493,36 +521,6 @@ public final class BridgeTypedArray extends TypedArray { return getDimensionPixelSize(index, defValue); } - /** @param name attribute name, used for error reporting. */ - private int getDimension(int index, @Nullable String name) { - String s = getString(index); - if (s == null) { - if (name != null) { - throw new RuntimeException("Attribute '" + name + "' not found"); - } - throw new RuntimeException(); - } - // Check if the value is a magic constant that doesn't require a unit. - try { - int i = Integer.parseInt(s); - if (i == LayoutParams.MATCH_PARENT || i == LayoutParams.WRAP_CONTENT) { - return i; - } - } catch (NumberFormatException ignored) { - // pass - } - if (ResourceHelper.parseFloatAttribute(mNames[index], s, mValue, true)) { - float f = mValue.getDimension(mBridgeResources.getDisplayMetrics()); - - final int res = (int)(f+0.5f); - if (res != 0) return res; - if (f == 0) return 0; - if (f > 0) return 1; - } - - throw new RuntimeException(); - } - /** * Retrieve a fractional unit attribute at <var>index</var>. * @@ -550,7 +548,7 @@ public final class BridgeTypedArray extends TypedArray { } // looks like we were unable to resolve the fraction value - Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, + Bridge.getLog().warning(ILayoutLog.TAG_RESOURCES_FORMAT, String.format( "\"%1$s\" in attribute \"%2$s\" cannot be converted to a fraction.", value, mNames[index]), @@ -809,57 +807,9 @@ public final class BridgeTypedArray extends TypedArray { } @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; + return getType(value); } /** @@ -1031,6 +981,73 @@ public final class BridgeTypedArray extends TypedArray { return ((int)Long.parseLong(charSeq.substring(index), base)) * sign; } + protected static int getType(@Nullable String value) { + if (value == null) { + return TYPE_NULL; + } + if (value.startsWith(PREFIX_RESOURCE_REF)) { + return TYPE_REFERENCE; + } + if (value.startsWith(PREFIX_THEME_REF)) { + return TYPE_ATTRIBUTE; + } + if (value.equals("true") || value.equals("false")) { + return TYPE_INT_BOOLEAN; + } + if (value.startsWith("0x") || value.startsWith("0X")) { + try { + // Check if it is a hex value. + Long.parseLong(value.substring(2), 16); + return TYPE_INT_HEX; + } catch (NumberFormatException e) { + return TYPE_STRING; + } + } + if (value.startsWith("#")) { + try { + // Check if it is a color. + ResourceHelper.getColor(value); + 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; + } + } catch (NumberFormatException e) { + return TYPE_STRING; + } + } + if (!Character.isDigit(value.charAt(value.length() - 1))) { + // Check if it is a dimension. + if (ResourceHelper.parseFloatAttribute(null, value, new TypedValue(), false)) { + return TYPE_DIMENSION; + } else { + return TYPE_STRING; + } + } + try { + // Check if it is an int. + convertValueToInt(value, 0); + return TYPE_INT_DEC; + } catch (NumberFormatException ignored) { + try { + // Check if it is a float. + Float.parseFloat(value); + return TYPE_FLOAT; + } catch (NumberFormatException ignore) { + } + } + // TODO: handle fractions. + return TYPE_STRING; + } + static TypedArray obtain(Resources res, int len) { return new BridgeTypedArray(res, null, len); } |