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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "androidfw/AttributeFinder.h"
#include "androidfw/AttributeResolution.h"
#include "androidfw/ResourceTypes.h"
#include <android/log.h>
#include <cstdint>
constexpr bool kDebugStyles = false;
namespace android {
enum {
STYLE_NUM_ENTRIES = 6,
STYLE_TYPE = 0,
STYLE_DATA = 1,
STYLE_ASSET_COOKIE = 2,
STYLE_RESOURCE_ID = 3,
STYLE_CHANGING_CONFIGURATIONS = 4,
STYLE_DENSITY = 5
};
class XmlAttributeFinder : public BackTrackingAttributeFinder<XmlAttributeFinder, size_t> {
public:
explicit XmlAttributeFinder(const ResXMLParser* parser) :
BackTrackingAttributeFinder(0, parser != NULL ? parser->getAttributeCount() : 0),
mParser(parser) {
}
inline uint32_t getAttribute(size_t index) const {
return mParser->getAttributeNameResID(index);
}
private:
const ResXMLParser* mParser;
};
class BagAttributeFinder :
public BackTrackingAttributeFinder<BagAttributeFinder, const ResTable::bag_entry*> {
public:
BagAttributeFinder(const ResTable::bag_entry* start, const ResTable::bag_entry* end) :
BackTrackingAttributeFinder(start, end) {}
inline uint32_t getAttribute(const ResTable::bag_entry* entry) const {
return entry->map.name.ident;
}
};
bool resolveAttrs(ResTable::Theme* theme,
uint32_t defStyleAttr,
uint32_t defStyleRes,
uint32_t* srcValues, size_t srcValuesLength,
uint32_t* attrs, size_t attrsLength,
uint32_t* outValues,
uint32_t* outIndices) {
if (kDebugStyles) {
ALOGI("APPLY STYLE: theme=0x%p defStyleAttr=0x%x "
"defStyleRes=0x%x", theme, defStyleAttr, defStyleRes);
}
const ResTable& res = theme->getResTable();
ResTable_config config;
Res_value value;
int indicesIdx = 0;
// Load default style from attribute, if specified...
uint32_t defStyleBagTypeSetFlags = 0;
if (defStyleAttr != 0) {
Res_value value;
if (theme->getAttribute(defStyleAttr, &value, &defStyleBagTypeSetFlags) >= 0) {
if (value.dataType == Res_value::TYPE_REFERENCE) {
defStyleRes = value.data;
}
}
}
// Now lock down the resource object and start pulling stuff from it.
res.lock();
// Retrieve the default style bag, if requested.
const ResTable::bag_entry* defStyleStart = NULL;
uint32_t defStyleTypeSetFlags = 0;
ssize_t bagOff = defStyleRes != 0
? res.getBagLocked(defStyleRes, &defStyleStart, &defStyleTypeSetFlags) : -1;
defStyleTypeSetFlags |= defStyleBagTypeSetFlags;
const ResTable::bag_entry* const defStyleEnd = defStyleStart + (bagOff >= 0 ? bagOff : 0);
BagAttributeFinder defStyleAttrFinder(defStyleStart, defStyleEnd);
// Now iterate through all of the attributes that the client has requested,
// filling in each with whatever data we can find.
ssize_t block = 0;
uint32_t typeSetFlags;
for (size_t ii=0; ii<attrsLength; ii++) {
const uint32_t curIdent = attrs[ii];
if (kDebugStyles) {
ALOGI("RETRIEVING ATTR 0x%08x...", curIdent);
}
// Try to find a value for this attribute... we prioritize values
// coming from, first XML attributes, then XML style, then default
// style, and finally the theme.
value.dataType = Res_value::TYPE_NULL;
value.data = Res_value::DATA_NULL_UNDEFINED;
typeSetFlags = 0;
config.density = 0;
// Retrieve the current input value if available.
if (srcValuesLength > 0 && srcValues[ii] != 0) {
block = -1;
value.dataType = Res_value::TYPE_ATTRIBUTE;
value.data = srcValues[ii];
if (kDebugStyles) {
ALOGI("-> From values: type=0x%x, data=0x%08x", value.dataType, value.data);
}
}
if (value.dataType == Res_value::TYPE_NULL) {
const ResTable::bag_entry* const defStyleEntry = defStyleAttrFinder.find(curIdent);
if (defStyleEntry != defStyleEnd) {
block = defStyleEntry->stringBlock;
typeSetFlags = defStyleTypeSetFlags;
value = defStyleEntry->map.value;
if (kDebugStyles) {
ALOGI("-> From def style: type=0x%x, data=0x%08x", value.dataType, value.data);
}
}
}
uint32_t resid = 0;
if (value.dataType != Res_value::TYPE_NULL) {
// Take care of resolving the found resource to its final value.
ssize_t newBlock = theme->resolveAttributeReference(&value, block,
&resid, &typeSetFlags, &config);
if (newBlock >= 0) block = newBlock;
if (kDebugStyles) {
ALOGI("-> Resolved attr: type=0x%x, data=0x%08x", value.dataType, value.data);
}
} else {
// If we still don't have a value for this attribute, try to find
// it in the theme!
ssize_t newBlock = theme->getAttribute(curIdent, &value, &typeSetFlags);
if (newBlock >= 0) {
if (kDebugStyles) {
ALOGI("-> From theme: type=0x%x, data=0x%08x", value.dataType, value.data);
}
newBlock = res.resolveReference(&value, block, &resid,
&typeSetFlags, &config);
if (newBlock >= 0) block = newBlock;
if (kDebugStyles) {
ALOGI("-> Resolved theme: type=0x%x, data=0x%08x", value.dataType, value.data);
}
}
}
// Deal with the special @null value -- it turns back to TYPE_NULL.
if (value.dataType == Res_value::TYPE_REFERENCE && value.data == 0) {
if (kDebugStyles) {
ALOGI("-> Setting to @null!");
}
value.dataType = Res_value::TYPE_NULL;
value.data = Res_value::DATA_NULL_UNDEFINED;
block = -1;
}
if (kDebugStyles) {
ALOGI("Attribute 0x%08x: type=0x%x, data=0x%08x", curIdent, value.dataType,
value.data);
}
// Write the final value back to Java.
outValues[STYLE_TYPE] = value.dataType;
outValues[STYLE_DATA] = value.data;
outValues[STYLE_ASSET_COOKIE] = block != -1
? static_cast<uint32_t>(res.getTableCookie(block)) : static_cast<uint32_t>(-1);
outValues[STYLE_RESOURCE_ID] = resid;
outValues[STYLE_CHANGING_CONFIGURATIONS] = typeSetFlags;
outValues[STYLE_DENSITY] = config.density;
if (outIndices != NULL && value.dataType != Res_value::TYPE_NULL) {
indicesIdx++;
outIndices[indicesIdx] = ii;
}
outValues += STYLE_NUM_ENTRIES;
}
res.unlock();
if (outIndices != NULL) {
outIndices[0] = indicesIdx;
}
return true;
}
bool applyStyle(ResTable::Theme* theme, ResXMLParser* xmlParser,
uint32_t defStyleAttr,
uint32_t defStyleRes,
uint32_t* attrs, size_t attrsLength,
uint32_t* outValues,
uint32_t* outIndices) {
if (kDebugStyles) {
ALOGI("APPLY STYLE: theme=0x%p defStyleAttr=0x%x defStyleRes=0x%x xml=0x%p",
theme, defStyleAttr, defStyleRes, xmlParser);
}
const ResTable& res = theme->getResTable();
ResTable_config config;
Res_value value;
int indicesIdx = 0;
// Load default style from attribute, if specified...
uint32_t defStyleBagTypeSetFlags = 0;
if (defStyleAttr != 0) {
Res_value value;
if (theme->getAttribute(defStyleAttr, &value, &defStyleBagTypeSetFlags) >= 0) {
if (value.dataType == Res_value::TYPE_REFERENCE) {
defStyleRes = value.data;
}
}
}
// Retrieve the style class associated with the current XML tag.
int style = 0;
uint32_t styleBagTypeSetFlags = 0;
if (xmlParser != NULL) {
ssize_t idx = xmlParser->indexOfStyle();
if (idx >= 0 && xmlParser->getAttributeValue(idx, &value) >= 0) {
if (value.dataType == value.TYPE_ATTRIBUTE) {
if (theme->getAttribute(value.data, &value, &styleBagTypeSetFlags) < 0) {
value.dataType = Res_value::TYPE_NULL;
}
}
if (value.dataType == value.TYPE_REFERENCE) {
style = value.data;
}
}
}
// Now lock down the resource object and start pulling stuff from it.
res.lock();
// Retrieve the default style bag, if requested.
const ResTable::bag_entry* defStyleAttrStart = NULL;
uint32_t defStyleTypeSetFlags = 0;
ssize_t bagOff = defStyleRes != 0
? res.getBagLocked(defStyleRes, &defStyleAttrStart, &defStyleTypeSetFlags) : -1;
defStyleTypeSetFlags |= defStyleBagTypeSetFlags;
const ResTable::bag_entry* const defStyleAttrEnd = defStyleAttrStart + (bagOff >= 0 ? bagOff : 0);
BagAttributeFinder defStyleAttrFinder(defStyleAttrStart, defStyleAttrEnd);
// Retrieve the style class bag, if requested.
const ResTable::bag_entry* styleAttrStart = NULL;
uint32_t styleTypeSetFlags = 0;
bagOff = style != 0 ? res.getBagLocked(style, &styleAttrStart, &styleTypeSetFlags) : -1;
styleTypeSetFlags |= styleBagTypeSetFlags;
const ResTable::bag_entry* const styleAttrEnd = styleAttrStart + (bagOff >= 0 ? bagOff : 0);
BagAttributeFinder styleAttrFinder(styleAttrStart, styleAttrEnd);
// Retrieve the XML attributes, if requested.
static const ssize_t kXmlBlock = 0x10000000;
XmlAttributeFinder xmlAttrFinder(xmlParser);
const size_t xmlAttrEnd = xmlParser != NULL ? xmlParser->getAttributeCount() : 0;
// Now iterate through all of the attributes that the client has requested,
// filling in each with whatever data we can find.
ssize_t block = 0;
uint32_t typeSetFlags;
for (size_t ii = 0; ii < attrsLength; ii++) {
const uint32_t curIdent = attrs[ii];
if (kDebugStyles) {
ALOGI("RETRIEVING ATTR 0x%08x...", curIdent);
}
// Try to find a value for this attribute... we prioritize values
// coming from, first XML attributes, then XML style, then default
// style, and finally the theme.
value.dataType = Res_value::TYPE_NULL;
value.data = Res_value::DATA_NULL_UNDEFINED;
typeSetFlags = 0;
config.density = 0;
// Walk through the xml attributes looking for the requested attribute.
const size_t xmlAttrIdx = xmlAttrFinder.find(curIdent);
if (xmlAttrIdx != xmlAttrEnd) {
// We found the attribute we were looking for.
block = kXmlBlock;
xmlParser->getAttributeValue(xmlAttrIdx, &value);
if (kDebugStyles) {
ALOGI("-> From XML: type=0x%x, data=0x%08x", value.dataType, value.data);
}
}
if (value.dataType == Res_value::TYPE_NULL) {
// Walk through the style class values looking for the requested attribute.
const ResTable::bag_entry* const styleAttrEntry = styleAttrFinder.find(curIdent);
if (styleAttrEntry != styleAttrEnd) {
// We found the attribute we were looking for.
block = styleAttrEntry->stringBlock;
typeSetFlags = styleTypeSetFlags;
value = styleAttrEntry->map.value;
if (kDebugStyles) {
ALOGI("-> From style: type=0x%x, data=0x%08x", value.dataType, value.data);
}
}
}
if (value.dataType == Res_value::TYPE_NULL) {
// Walk through the default style values looking for the requested attribute.
const ResTable::bag_entry* const defStyleAttrEntry = defStyleAttrFinder.find(curIdent);
if (defStyleAttrEntry != defStyleAttrEnd) {
// We found the attribute we were looking for.
block = defStyleAttrEntry->stringBlock;
typeSetFlags = styleTypeSetFlags;
value = defStyleAttrEntry->map.value;
if (kDebugStyles) {
ALOGI("-> From def style: type=0x%x, data=0x%08x", value.dataType, value.data);
}
}
}
uint32_t resid = 0;
if (value.dataType != Res_value::TYPE_NULL) {
// Take care of resolving the found resource to its final value.
ssize_t newBlock = theme->resolveAttributeReference(&value, block,
&resid, &typeSetFlags, &config);
if (newBlock >= 0) {
block = newBlock;
}
if (kDebugStyles) {
ALOGI("-> Resolved attr: type=0x%x, data=0x%08x", value.dataType, value.data);
}
} else {
// If we still don't have a value for this attribute, try to find
// it in the theme!
ssize_t newBlock = theme->getAttribute(curIdent, &value, &typeSetFlags);
if (newBlock >= 0) {
if (kDebugStyles) {
ALOGI("-> From theme: type=0x%x, data=0x%08x", value.dataType, value.data);
}
newBlock = res.resolveReference(&value, block, &resid,
&typeSetFlags, &config);
if (newBlock >= 0) {
block = newBlock;
}
if (kDebugStyles) {
ALOGI("-> Resolved theme: type=0x%x, data=0x%08x", value.dataType, value.data);
}
}
}
// Deal with the special @null value -- it turns back to TYPE_NULL.
if (value.dataType == Res_value::TYPE_REFERENCE && value.data == 0) {
if (kDebugStyles) {
ALOGI("-> Setting to @null!");
}
value.dataType = Res_value::TYPE_NULL;
value.data = Res_value::DATA_NULL_UNDEFINED;
block = kXmlBlock;
}
if (kDebugStyles) {
ALOGI("Attribute 0x%08x: type=0x%x, data=0x%08x", curIdent, value.dataType, value.data);
}
// Write the final value back to Java.
outValues[STYLE_TYPE] = value.dataType;
outValues[STYLE_DATA] = value.data;
outValues[STYLE_ASSET_COOKIE] = block != kXmlBlock ?
static_cast<uint32_t>(res.getTableCookie(block)) : static_cast<uint32_t>(-1);
outValues[STYLE_RESOURCE_ID] = resid;
outValues[STYLE_CHANGING_CONFIGURATIONS] = typeSetFlags;
outValues[STYLE_DENSITY] = config.density;
if (outIndices != NULL && value.dataType != Res_value::TYPE_NULL) {
indicesIdx++;
outIndices[indicesIdx] = ii;
}
outValues += STYLE_NUM_ENTRIES;
}
res.unlock();
if (outIndices != NULL) {
outIndices[0] = indicesIdx;
}
return true;
}
bool retrieveAttributes(const ResTable* res, ResXMLParser* xmlParser,
uint32_t* attrs, size_t attrsLength,
uint32_t* outValues,
uint32_t* outIndices) {
ResTable_config config;
Res_value value;
int indicesIdx = 0;
// Now lock down the resource object and start pulling stuff from it.
res->lock();
// Retrieve the XML attributes, if requested.
const size_t NX = xmlParser->getAttributeCount();
size_t ix=0;
uint32_t curXmlAttr = xmlParser->getAttributeNameResID(ix);
static const ssize_t kXmlBlock = 0x10000000;
// Now iterate through all of the attributes that the client has requested,
// filling in each with whatever data we can find.
ssize_t block = 0;
uint32_t typeSetFlags;
for (size_t ii=0; ii<attrsLength; ii++) {
const uint32_t curIdent = attrs[ii];
// Try to find a value for this attribute...
value.dataType = Res_value::TYPE_NULL;
value.data = Res_value::DATA_NULL_UNDEFINED;
typeSetFlags = 0;
config.density = 0;
// Skip through XML attributes until the end or the next possible match.
while (ix < NX && curIdent > curXmlAttr) {
ix++;
curXmlAttr = xmlParser->getAttributeNameResID(ix);
}
// Retrieve the current XML attribute if it matches, and step to next.
if (ix < NX && curIdent == curXmlAttr) {
block = kXmlBlock;
xmlParser->getAttributeValue(ix, &value);
ix++;
curXmlAttr = xmlParser->getAttributeNameResID(ix);
}
//printf("Attribute 0x%08x: type=0x%x, data=0x%08x\n", curIdent, value.dataType, value.data);
uint32_t resid = 0;
if (value.dataType != Res_value::TYPE_NULL) {
// Take care of resolving the found resource to its final value.
//printf("Resolving attribute reference\n");
ssize_t newBlock = res->resolveReference(&value, block, &resid,
&typeSetFlags, &config);
if (newBlock >= 0) block = newBlock;
}
// Deal with the special @null value -- it turns back to TYPE_NULL.
if (value.dataType == Res_value::TYPE_REFERENCE && value.data == 0) {
value.dataType = Res_value::TYPE_NULL;
value.data = Res_value::DATA_NULL_UNDEFINED;
}
//printf("Attribute 0x%08x: final type=0x%x, data=0x%08x\n", curIdent, value.dataType, value.data);
// Write the final value back to Java.
outValues[STYLE_TYPE] = value.dataType;
outValues[STYLE_DATA] = value.data;
outValues[STYLE_ASSET_COOKIE] = block != kXmlBlock
? static_cast<uint32_t>(res->getTableCookie(block)) : static_cast<uint32_t>(-1);
outValues[STYLE_RESOURCE_ID] = resid;
outValues[STYLE_CHANGING_CONFIGURATIONS] = typeSetFlags;
outValues[STYLE_DENSITY] = config.density;
if (outIndices != NULL && value.dataType != Res_value::TYPE_NULL) {
indicesIdx++;
outIndices[indicesIdx] = ii;
}
outValues += STYLE_NUM_ENTRIES;
}
res->unlock();
if (outIndices != NULL) {
outIndices[0] = indicesIdx;
}
return true;
}
} // namespace android
|