Debian Codegraph @

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/data/value/ResValueFactory.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.data.value;
18
19 import android.util.TypedValue;
20 import brut.androlib.AndrolibException;
21 import brut.androlib.res.data.ResPackage;
22 import brut.androlib.res.data.ResTypeSpec;
23 import brut.util.Duo;
24
25 public class ResValueFactory {
26 private final ResPackage mPackage;
27
28 public ResValueFactory(ResPackage package_) {
29 this.mPackage = package_;
30 }
31
32 public ResScalarValue factory(int type, int value, String rawValue) throws AndrolibException {
33 switch (type) {
34 case TypedValue.TYPE_NULL:
35 if (value == TypedValue.DATA_NULL_UNDEFINED) { // Special case $empty as explicitly defined empty value
36 return new ResStringValue(null, value);
37 } else if (value == TypedValue.DATA_NULL_EMPTY) {
38 return new ResEmptyValue(value, rawValue, type);
39 }
40 return new ResReferenceValue(mPackage, 0, null);
41 case TypedValue.TYPE_REFERENCE:
42 return newReference(value, null);
43 case TypedValue.TYPE_ATTRIBUTE:
44 case TypedValue.TYPE_DYNAMIC_ATTRIBUTE:
45 return newReference(value, rawValue, true);
46 case TypedValue.TYPE_STRING:
47 return new ResStringValue(rawValue, value);
48 case TypedValue.TYPE_FLOAT:
49 return new ResFloatValue(Float.intBitsToFloat(value), value, rawValue);
50 case TypedValue.TYPE_DIMENSION:
51 return new ResDimenValue(value, rawValue);
52 case TypedValue.TYPE_FRACTION:
53 return new ResFractionValue(value, rawValue);
54 case TypedValue.TYPE_INT_BOOLEAN:
55 return new ResBoolValue(value != 0, value, rawValue);
56 case TypedValue.TYPE_DYNAMIC_REFERENCE:
57 return newReference(value, rawValue);
58 }
59
60 if (type >= TypedValue.TYPE_FIRST_COLOR_INT && type <= TypedValue.TYPE_LAST_COLOR_INT) {
61 return new ResColorValue(value, rawValue);
62 }
63 if (type >= TypedValue.TYPE_FIRST_INT && type <= TypedValue.TYPE_LAST_INT) {
64 return new ResIntValue(value, rawValue, type);
65 }
66
67 throw new AndrolibException("Invalid value type: " + type);
68 }
69
70 public ResIntBasedValue factory(String value, int rawValue) {
71 if (value == null) {
72 return new ResFileValue("", rawValue);
73 }
74 if (value.startsWith("res/")) {
75 return new ResFileValue(value, rawValue);
76 }
77 if (value.startsWith("r/") || value.startsWith("R/")) { //AndroResGuard
78 return new ResFileValue(value, rawValue);
79 }
80 return new ResStringValue(value, rawValue);
81 }
82
83 public ResBagValue bagFactory(int parent, Duo<Integer, ResScalarValue>[] items, ResTypeSpec resTypeSpec) throws AndrolibException {
84 ResReferenceValue parentVal = newReference(parent, null);
85
86 if (items.length == 0) {
87 return new ResBagValue(parentVal);
88 }
89 int key = items[0].m1;
90 if (key == ResAttr.BAG_KEY_ATTR_TYPE) {
91 return ResAttr.factory(parentVal, items, this, mPackage);
92 }
93
94 String resTypeName = resTypeSpec.getName();
95
96 // Android O Preview added an unknown enum for c. This is hardcoded as 0 for now.
97 if (ResTypeSpec.RES_TYPE_NAME_ARRAY.equals(resTypeName)
98 || key == ResArrayValue.BAG_KEY_ARRAY_START || key == 0) {
99 return new ResArrayValue(parentVal, items);
100 }
101
102 if (ResTypeSpec.RES_TYPE_NAME_PLURALS.equals(resTypeName) ||
103 (key >= ResPluralsValue.BAG_KEY_PLURALS_START && key <= ResPluralsValue.BAG_KEY_PLURALS_END)) {
104 return new ResPluralsValue(parentVal, items);
105 }
106
107 if (ResTypeSpec.RES_TYPE_NAME_ATTR.equals(resTypeName)) {
108 return new ResAttr(parentVal, 0, null, null, null);
109 }
110
111 if (resTypeName.startsWith(ResTypeSpec.RES_TYPE_NAME_STYLES)) {
112 return new ResStyleValue(parentVal, items, this);
113 }
114
115 throw new AndrolibException("unsupported res type name for bags. Found: " + resTypeName);
116 }
117
118 public ResReferenceValue newReference(int resID, String rawValue) {
119 return newReference(resID, rawValue, false);
120 }
121
122 public ResReferenceValue newReference(int resID, String rawValue, boolean theme) {
123 return new ResReferenceValue(mPackage, resID, rawValue, theme);
124 }
125 }