From c56c90d3e0df0311dde644858a89cf69107e7c28 Mon Sep 17 00:00:00 2001 From: REAndroid Date: Sat, 18 Mar 2023 08:50:21 -0400 Subject: [PATCH] separate FLOAT and DIMENSION encoding #15 --- .../reandroid/arsc/decoder/ValueDecoder.java | 78 +++++++++++++------ 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/reandroid/arsc/decoder/ValueDecoder.java b/src/main/java/com/reandroid/arsc/decoder/ValueDecoder.java index c08c3fe..c90c870 100755 --- a/src/main/java/com/reandroid/arsc/decoder/ValueDecoder.java +++ b/src/main/java/com/reandroid/arsc/decoder/ValueDecoder.java @@ -213,35 +213,51 @@ import java.util.regex.Pattern; if(value==null){ return null; } - Matcher matcher=PATTERN_DIMEN.matcher(value); + EncodeResult result = encodeFloat(value); + if(result == null){ + result = encodeDimensionOrFraction(value); + } + return result; + } + public static EncodeResult encodeFloat(String txt){ + Float value = parseFloat(txt); + if(value==null){ + return null; + } + return new EncodeResult(ValueType.FLOAT, + Float.floatToIntBits(value)); + } + public static Float parseFloat(String txt){ + if(txt==null || txt.indexOf('.')<0){ + return null; + } + try{ + return Float.parseFloat(txt); + }catch (NumberFormatException ignored){ + return null; + } + } + public static EncodeResult encodeDimensionOrFraction(String value){ + if(value==null){ + return null; + } + Matcher matcher = PATTERN_DIMEN.matcher(value); if(!matcher.find()){ return null; } - String sign = matcher.group(1); - String number = matcher.group(2); - String unit = matcher.group(3); + String number = matcher.group(1); + String unit = matcher.group(4); float fraction = Float.parseFloat(number); - if("-".equals(sign)){ - fraction=-fraction; - } - return encodeDimensionOrFloat(fraction, unit); + return encodeDimensionOrFraction(fraction, unit); } - private static EncodeResult encodeDimensionOrFloat(float val, String unit){ - if(unit==null||"".equals(unit)){ - return new EncodeResult(ValueType.FLOAT, - Float.floatToIntBits(val)); - } + // TODO: This method should be revised + private static EncodeResult encodeDimensionOrFraction(float val, String unit){ ValueType valueType = ValueType.DIMENSION; - int index=0; - if("%".equals(unit)||"%p".equals(unit)){ + if(unit.charAt(0) == '%'){ val=val/100.0f; - if("%p".equals(unit)){ - index=1; - } valueType = ValueType.FRACTION; - }else { - index=ValueDecoder.getDimensionIndex(unit); } + int index = ValueDecoder.getDimensionIndex(unit); int result = 0; int shift = 0; if(val!=0.0f){ @@ -736,14 +752,27 @@ import java.util.regex.Pattern; } return DIMENSION_UNIT_STRS[index]; } - static int getDimensionIndex(String unit){ + private static int getDimensionIndex(String unit){ + if("%".equals(unit)){ + return 0; + } + if("%p".equals(unit)){ + return 1; + } + if("dip".equals(unit)){ + unit = "dp"; + } String[] dims=DIMENSION_UNIT_STRS; for(int i=0;i