diff --git a/apktool-lib/src/main/java/brut/androlib/res/data/value/ResStringValue.java b/apktool-lib/src/main/java/brut/androlib/res/data/value/ResStringValue.java index bd585302..c356e70d 100644 --- a/apktool-lib/src/main/java/brut/androlib/res/data/value/ResStringValue.java +++ b/apktool-lib/src/main/java/brut/androlib/res/data/value/ResStringValue.java @@ -53,15 +53,8 @@ public class ResStringValue extends ResScalarValue { @Override protected void serializeExtraXmlAttrs(XmlSerializer serializer, ResResource res) throws IOException { - int pos = 0; - int count = 0; - while((pos = mRawValue.indexOf('%', pos)) != -1) { - if (mRawValue.charAt(pos + 1) != '%') { - if (++count >= 2) { - serializer.attribute(null, "formatted", "false"); - return; - } - } + if (ResXmlEncoders.hasMultipleNonPositionalSubstitutions(mRawValue)) { + serializer.attribute(null, "formatted", "false"); } } } diff --git a/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlEncoders.java b/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlEncoders.java index 81e2aa06..731588e7 100644 --- a/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlEncoders.java +++ b/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlEncoders.java @@ -133,6 +133,38 @@ public final class ResXmlEncoders { return out.toString(); } + public static boolean hasMultipleNonPositionalSubstitutions(String str) { + int pos = 0; + int count = 0; + while((pos = str.indexOf('%', pos)) != -1) { + if (str.charAt(pos + 1) != '%') { + if (++count >= 2) { + return true; + } + } + pos += 2; + } + + return false; + } + + public static String enumerateNonPositionalSubstitutions(String str) { + StringBuilder out = new StringBuilder(str); + int pos = 0; + int count = 0; + int offset = 0; + while((pos = str.indexOf('%', pos)) != -1) { + if (str.charAt(pos + 1) != '%') { + count++; + out.insert(pos + offset + 1, String.valueOf(count) + "$"); + offset += 2; + } + pos += 2; + } + + return out.toString(); + } + private static boolean isPrintableChar(char c) { Character.UnicodeBlock block = Character.UnicodeBlock.of(c); return !Character.isISOControl(c)