-
Notifications
You must be signed in to change notification settings - Fork 1
/
AndroidResourceProviderImpl.java
361 lines (331 loc) · 12.1 KB
/
AndroidResourceProviderImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package software.coley.androidres;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.google.common.io.ByteStreams;
import com.google.devrel.gmscore.tools.apk.arsc.*;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2LongMap;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import software.coley.android.xml.AndroidResourceProvider;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* Android resource information.
* <ul>
* <li>{@link #getAndroidBase()} - Common Android resource information</li>
* <li>{@link #fromArsc(BinaryResourceFile)} - Android resource information from an ARSC file</li>
* </ul>
*
* @author Matt Coley
*/
public class AndroidResourceProviderImpl implements AndroidResourceProvider {
private static final XmlMapper MAPPER = new XmlMapper();
private static final AndroidResourceProviderImpl ANDROID_BASE;
private final Int2ObjectMap<String> resIdToName;
private final Object2IntMap<String> resNameToId;
private final Map<String, String> attrToFormat;
private final Map<String, Object2LongMap<String>> attrToEnum;
private final Map<String, Object2LongMap<String>> attrToFlags;
private final Map<String, BinaryResourceValue> attrToSimpleResource;
private final Map<String, Map<Integer, BinaryResourceValue>> attrToComplexResource;
private final Map<String, Set<String>> formatToAttrs;
private AndroidResourceProviderImpl(@Nonnull Int2ObjectMap<String> resIdToName,
@Nonnull Object2IntMap<String> resNameToId,
@Nonnull Map<String, String> attrToFormat,
@Nonnull Map<String, Object2LongMap<String>> attrToEnum,
@Nonnull Map<String, Object2LongMap<String>> attrToFlags,
@Nonnull Map<String, BinaryResourceValue> attrToSimpleResource,
@Nonnull Map<String, Map<Integer, BinaryResourceValue>> attrToComplexResource,
@Nonnull Map<String, Set<String>> formatToAttrs) {
this.resIdToName = resIdToName;
this.resNameToId = resNameToId;
this.attrToFormat = attrToFormat;
this.attrToEnum = attrToEnum;
this.attrToFlags = attrToFlags;
this.attrToSimpleResource = attrToSimpleResource;
this.attrToComplexResource = attrToComplexResource;
this.formatToAttrs = formatToAttrs;
}
@Nonnull
public static AndroidResourceProviderImpl fromArsc(@Nonnull BinaryResourceFile chunkModel) {
List<Chunk> chunks = chunkModel.getChunks();
// Maps to collect data into.
Int2ObjectMap<String> resIdToName = new Int2ObjectOpenHashMap<>();
Object2IntMap<String> resNameToId = new Object2IntOpenHashMap<>();
Map<String, String> attrToFormat = new TreeMap<>();
Map<String, Object2LongMap<String>> attrToEnum = new TreeMap<>();
Map<String, Object2LongMap<String>> attrToFlags = new TreeMap<>();
Map<String, BinaryResourceValue> attrToSimpleResource = new TreeMap<>();
Map<String, Map<Integer, BinaryResourceValue>> attrToComplexResource = new TreeMap<>();
Map<String, Set<String>> formatToAttrs = new TreeMap<>();
// Parse the chunks in the ARSC, extracting all entries
for (Chunk chunk : chunks) {
if (chunk instanceof ResourceTableChunk) {
ResourceTableChunk resourceTableChunk = (ResourceTableChunk) chunk;
for (PackageChunk packageChunk : resourceTableChunk.getPackages()) {
int packageId = packageChunk.getId();
for (TypeChunk typeChunk : packageChunk.getTypeChunks()) {
int typeId = typeChunk.getId();
String typePrefix = typeChunk.getTypeName();
typeChunk.getEntries().forEach((entryId, entry) -> {
int entryResId = packageId << 24 | typeId << 16 | entryId;
String key = entry.key();
String mapKey = typePrefix + "/" + key;
resIdToName.put(entryResId, mapKey);
resNameToId.put(mapKey, entryResId);
if (entry.isComplex()) {
Map<Integer, BinaryResourceValue> values = entry.values();
attrToComplexResource.put(mapKey, values);
} else {
BinaryResourceValue value = entry.value();
attrToSimpleResource.put(mapKey, value);
}
});
}
}
}
}
return new AndroidResourceProviderImpl(resIdToName, resNameToId, attrToFormat, attrToEnum, attrToFlags,
attrToSimpleResource, attrToComplexResource, formatToAttrs);
}
/**
* @return Instance of core Android resources.
*/
@Nonnull
public static AndroidResourceProviderImpl getAndroidBase() {
return ANDROID_BASE;
}
/**
* @param attrName
* Local attribute name, without the {@code attr/} prefix.
*
* @return Resource ID.
*/
public int getAttrResId(@Nonnull String attrName) {
return getResId("attr/" + attrName);
}
/**
* @param resName
* Resource name.
*
* @return Resource ID.
*/
public int getResId(@Nonnull String resName) {
return resNameToId.getOrDefault(resName, -1);
}
@Override
public boolean hasResName(int resId) {
return resIdToName.get(resId) != null;
}
@Override
@Nullable
public String getResName(int resId) {
return resIdToName.get(resId);
}
/**
* @param resId
* Resource ID.
*
* @return {@code true} when the resource is a known enum.
*/
public boolean isResEnum(int resId) {
String resName = getResName(resId);
if (resName == null)
return false;
return hasResEnum(resName);
}
@Override
public boolean hasResEnum(@Nonnull String resName) {
return attrToEnum.containsKey(resName);
}
@Override
@Nullable
public String getResEnumName(@Nonnull String resName, long value) {
Object2LongMap<String> values = attrToEnum.get(resName);
if (values == null)
return null;
return values.object2LongEntrySet().stream()
.filter(e -> e.getLongValue() == value)
.findFirst()
.map(Map.Entry::getKey)
.orElse(null);
}
/**
* @param resName
* Resource name.
*
* @return {@code true} when the resource is a known value.
*/
public boolean isSimpleResValue(@Nonnull String resName) {
return attrToSimpleResource.containsKey(resName);
}
/**
* @param resName
* Resource name.
*
* @return Resource value for the associated value if known, otherwise {@code null}.
*/
@Nullable
public BinaryResourceValue getSimpleResValue(@Nonnull String resName) {
return attrToSimpleResource.get(resName);
}
/**
* @param resName
* Resource name.
*
* @return {@code true} when the resource is a known value.
*/
public boolean isComplexResValue(@Nonnull String resName) {
return attrToComplexResource.containsKey(resName);
}
/**
* @param resName
* Resource name.
*
* @return Resource value for the associated value if known, otherwise {@code null}.
*/
@Nullable
public Map<Integer, BinaryResourceValue> getComplexResValue(@Nonnull String resName) {
return attrToComplexResource.get(resName);
}
/**
* @param attrName
* Attribute name.
*
* @return {@code true} when the attribute has an associated format.
*/
public boolean attributeHasFormat(@Nonnull String attrName) {
return attrToFormat.containsKey(attrName);
}
/**
* @param attrName
* Attribute name.
*
* @return Format identifier for the associated value if known, otherwise {@code null}.
*/
@Nullable
public String getAttributeFormat(@Nonnull String attrName) {
return attrToFormat.get(attrName);
}
/**
* @param resId
* Resource ID.
*
* @return {@code true} when the resource is a known flag.
*/
public boolean isResFlag(int resId) {
String resName = getResName(resId);
if (resName == null)
return false;
return hasResEnum(resName);
}
@Override
public boolean hasResFlag(@Nonnull String resName) {
return attrToFlags.containsKey(resName);
}
@Override
@Nonnull
public String getResFlagNames(@Nonnull String resName, long mask) {
Object2LongMap<String> values = attrToFlags.get(resName);
if (values == null)
return "";
return values.object2LongEntrySet().stream()
.filter(e -> (mask & e.getLongValue()) != 0L)
.sorted(Comparator.comparingLong(Object2LongMap.Entry::getLongValue))
.map(Map.Entry::getKey)
.collect(Collectors.joining("|"));
}
static {
try {
// Parse res-map entries (hex=name)
String resMapText = new String(ByteStreams.toByteArray(AndroidResourceProviderImpl.class.getResourceAsStream("/android/res-map.txt")));
String[] resMapLines = resMapText.split("[\n\r]+");
Int2ObjectMap<String> resIdToName = new Int2ObjectOpenHashMap<>(resMapLines.length);
Object2IntMap<String> resNameToId = new Object2IntOpenHashMap<>(resMapLines.length);
for (String line : resMapLines) {
String[] kv = line.split("=");
int key = Integer.parseInt(kv[0], 16);
String name = kv[1];
resIdToName.put(key, name);
resNameToId.put(name, key);
}
// Parse the attribute manifest
Map<String, Set<String>> formatToAttrs = new TreeMap<>();
Map<String, String> attrToFormat = new TreeMap<>();
Map<String, Object2LongMap<String>> attrToEnum = new TreeMap<>();
Map<String, Object2LongMap<String>> attrToFlags = new TreeMap<>();
JsonNode tree = MAPPER.readTree(AndroidResourceProviderImpl.class.getResourceAsStream("/android/attrs_manifest.xml"));
for (JsonNode child : tree)
visit(formatToAttrs, attrToFormat, attrToEnum, attrToFlags, child);
tree = MAPPER.readTree(AndroidResourceProviderImpl.class.getResourceAsStream("/android/attrs.xml"));
for (JsonNode child : tree)
visit(formatToAttrs, attrToFormat, attrToEnum, attrToFlags, child);
ANDROID_BASE = new AndroidResourceProviderImpl(resIdToName, resNameToId,
attrToFormat, attrToEnum, attrToFlags, Collections.emptyMap(), Collections.emptyMap(), formatToAttrs);
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
private static void visit(@Nonnull Map<String, Set<String>> formatToAttrs,
@Nonnull Map<String, String> attrToFormat,
@Nonnull Map<String, Object2LongMap<String>> attrToEnum,
@Nonnull Map<String, Object2LongMap<String>> attrToFlags,
@Nonnull JsonNode node) {
JsonNode nameNode = node.get("name");
if (nameNode != null && node instanceof ObjectNode) {
ObjectNode childObject = (ObjectNode) node;
String name = nameNode.asText();
// Handle different kinds of entries
JsonNode formatNode = childObject.get("format");
if (formatNode != null) {
String format = formatNode.asText();
attrToFormat.put(name, format);
formatToAttrs.computeIfAbsent(format, f -> new TreeSet<>()).add(name);
return;
} else {
JsonNode flagNode = childObject.get("flag");
if (flagNode instanceof ArrayNode) {
ArrayNode flagArray = (ArrayNode) flagNode;
Object2LongMap<String> nameToValue = new Object2LongOpenHashMap<>();
for (JsonNode flagEntry : flagArray) {
String flagName = flagEntry.get("name").asText();
String flagValueText = flagEntry.get("value").asText();
if (flagValueText.startsWith("0x"))
flagValueText = flagValueText.substring(2);
long flagValue = Long.parseLong(flagValueText, 16);
nameToValue.put(flagName, flagValue);
}
attrToFlags.put(name, nameToValue);
return;
} else {
JsonNode enumNode = node.get("enum");
if (enumNode instanceof ArrayNode) {
ArrayNode enumArray = (ArrayNode) enumNode;
Object2LongMap<String> nameToValue = new Object2LongOpenHashMap<>();
for (JsonNode flagEntry : enumArray) {
String enumName = flagEntry.get("name").asText();
String enumValueText = flagEntry.get("value").asText();
if (enumValueText.startsWith("0x"))
enumValueText = enumValueText.substring(2);
long enumValue = Long.parseLong(enumValueText);
nameToValue.put(enumName, enumValue);
}
attrToEnum.put(name, nameToValue);
return;
}
}
}
}
// Visit the children if this node is not a child and isn't one of the types we expect.
for (JsonNode child : node)
visit(formatToAttrs, attrToFormat, attrToEnum, attrToFlags, child);
}
}