Debian Codegraph @

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/AndroidManifestResourceParser.java

raw
1 /*
2 * Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
3 * Copyright (C) 2010 Connor Tumbleson <[email protected]>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package brut.androlib.res.decoder;
18
19 import android.util.TypedValue;
20
21 import java.util.regex.Pattern;
22
23 /**
24 * AXmlResourceParser specifically for parsing encoded AndroidManifest.xml.
25 */
26 public class AndroidManifestResourceParser extends AXmlResourceParser {
27
28 /**
29 * Pattern for matching numeric string meta-data values. aapt automatically infers the
30 * type for a manifest meta-data value based on the string in the unencoded XML. However,
31 * some apps intentionally coerce integers to be strings by prepending an escaped space.
32 * For details/discussion, see https://stackoverflow.com/questions/2154945/how-to-force-a-meta-data-value-to-type-string
33 * With aapt1, the escaped space is dropped when encoded. For aapt2, the escaped space is preserved.
34 */
35 private static final Pattern PATTERN_NUMERIC_STRING = Pattern.compile("\\s?\\d+");
36
37 @Override
38 public String getAttributeValue(int index) {
39 String value = super.getAttributeValue(index);
40 if (value == null) {
41 return "";
42 }
43
44 if (!isNumericStringMetadataAttributeValue(index, value)) {
45 return value;
46 }
47
48 // Patch the numeric string value by prefixing it with an escaped space.
49 // Otherwise, when the decoded app is rebuilt, aapt will incorrectly encode
50 // the value as an int or float (depending on aapt version), breaking the original
51 // app functionality.
52 return "\\ " + value.trim();
53 }
54
55 private boolean isNumericStringMetadataAttributeValue(int index, String value) {
56 return "meta-data".equalsIgnoreCase(super.getName())
57 && "value".equalsIgnoreCase(super.getAttributeName(index))
58 && super.getAttributeValueType(index) == TypedValue.TYPE_STRING
59 && PATTERN_NUMERIC_STRING.matcher(value).matches();
60 }
61 }