blob: 9c70fafb4ca0e211b5408fa5bb87ada6f06a0d7b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//
18// Definitions of resource data structures.
19//
20#ifndef _LIBS_UTILS_RESOURCE_TYPES_H
21#define _LIBS_UTILS_RESOURCE_TYPES_H
22
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080023#include <androidfw/Asset.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/ByteOrder.h>
25#include <utils/Errors.h>
26#include <utils/String16.h>
27#include <utils/Vector.h>
Adam Lesinskide898ff2014-01-29 18:20:45 -080028#include <utils/KeyedVector.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
30#include <utils/threads.h>
31
32#include <stdint.h>
33#include <sys/types.h>
34
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -070035#include <android/configuration.h>
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037namespace android {
38
Adam Lesinski4bf58102014-11-03 11:21:19 -080039/**
40 * In C++11, char16_t is defined as *at least* 16 bits. We do a lot of
41 * casting on raw data and expect char16_t to be exactly 16 bits.
42 */
43#if __cplusplus >= 201103L
44struct __assertChar16Size {
45 static_assert(sizeof(char16_t) == sizeof(uint16_t), "char16_t is not 16 bits");
46 static_assert(alignof(char16_t) == alignof(uint16_t), "char16_t is not 16-bit aligned");
47};
48#endif
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050/** ********************************************************************
51 * PNG Extensions
52 *
53 * New private chunks that may be placed in PNG images.
54 *
55 *********************************************************************** */
56
57/**
58 * This chunk specifies how to split an image into segments for
59 * scaling.
60 *
61 * There are J horizontal and K vertical segments. These segments divide
62 * the image into J*K regions as follows (where J=4 and K=3):
63 *
64 * F0 S0 F1 S1
65 * +-----+----+------+-------+
66 * S2| 0 | 1 | 2 | 3 |
67 * +-----+----+------+-------+
68 * | | | | |
69 * | | | | |
70 * F2| 4 | 5 | 6 | 7 |
71 * | | | | |
72 * | | | | |
73 * +-----+----+------+-------+
74 * S3| 8 | 9 | 10 | 11 |
75 * +-----+----+------+-------+
76 *
77 * Each horizontal and vertical segment is considered to by either
78 * stretchable (marked by the Sx labels) or fixed (marked by the Fy
79 * labels), in the horizontal or vertical axis, respectively. In the
80 * above example, the first is horizontal segment (F0) is fixed, the
81 * next is stretchable and then they continue to alternate. Note that
82 * the segment list for each axis can begin or end with a stretchable
83 * or fixed segment.
84 *
85 * The relative sizes of the stretchy segments indicates the relative
86 * amount of stretchiness of the regions bordered by the segments. For
87 * example, regions 3, 7 and 11 above will take up more horizontal space
Mathias Agopian5f910972009-06-22 02:35:32 -070088 * than regions 1, 5 and 9 since the horizontal segment associated with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * the first set of regions is larger than the other set of regions. The
90 * ratios of the amount of horizontal (or vertical) space taken by any
91 * two stretchable slices is exactly the ratio of their corresponding
92 * segment lengths.
93 *
Narayan Kamath6381dd42014-03-03 17:12:03 +000094 * xDivs and yDivs are arrays of horizontal and vertical pixel
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * indices. The first pair of Divs (in either array) indicate the
96 * starting and ending points of the first stretchable segment in that
97 * axis. The next pair specifies the next stretchable segment, etc. So
98 * in the above example xDiv[0] and xDiv[1] specify the horizontal
99 * coordinates for the regions labeled 1, 5 and 9. xDiv[2] and
100 * xDiv[3] specify the coordinates for regions 3, 7 and 11. Note that
101 * the leftmost slices always start at x=0 and the rightmost slices
102 * always end at the end of the image. So, for example, the regions 0,
103 * 4 and 8 (which are fixed along the X axis) start at x value 0 and
Mathias Agopian5f910972009-06-22 02:35:32 -0700104 * go to xDiv[0] and slices 2, 6 and 10 start at xDiv[1] and end at
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * xDiv[2].
106 *
Narayan Kamath6381dd42014-03-03 17:12:03 +0000107 * The colors array contains hints for each of the regions. They are
108 * ordered according left-to-right and top-to-bottom as indicated above.
109 * For each segment that is a solid color the array entry will contain
110 * that color value; otherwise it will contain NO_COLOR. Segments that
111 * are completely transparent will always have the value TRANSPARENT_COLOR.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 *
113 * The PNG chunk type is "npTc".
114 */
115struct Res_png_9patch
116{
Narayan Kamath6381dd42014-03-03 17:12:03 +0000117 Res_png_9patch() : wasDeserialized(false), xDivsOffset(0),
118 yDivsOffset(0), colorsOffset(0) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120 int8_t wasDeserialized;
121 int8_t numXDivs;
122 int8_t numYDivs;
123 int8_t numColors;
124
Narayan Kamath6381dd42014-03-03 17:12:03 +0000125 // The offset (from the start of this structure) to the xDivs & yDivs
126 // array for this 9patch. To get a pointer to this array, call
127 // getXDivs or getYDivs. Note that the serialized form for 9patches places
128 // the xDivs, yDivs and colors arrays immediately after the location
129 // of the Res_png_9patch struct.
130 uint32_t xDivsOffset;
131 uint32_t yDivsOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 int32_t paddingLeft, paddingRight;
134 int32_t paddingTop, paddingBottom;
135
136 enum {
137 // The 9 patch segment is not a solid color.
138 NO_COLOR = 0x00000001,
139
140 // The 9 patch segment is completely transparent.
141 TRANSPARENT_COLOR = 0x00000000
142 };
Narayan Kamath6381dd42014-03-03 17:12:03 +0000143
144 // The offset (from the start of this structure) to the colors array
145 // for this 9patch.
146 uint32_t colorsOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
148 // Convert data from device representation to PNG file representation.
149 void deviceToFile();
150 // Convert data from PNG file representation to device representation.
151 void fileToDevice();
Narayan Kamath6381dd42014-03-03 17:12:03 +0000152
153 // Serialize/Marshall the patch data into a newly malloc-ed block.
154 static void* serialize(const Res_png_9patch& patchHeader, const int32_t* xDivs,
155 const int32_t* yDivs, const uint32_t* colors);
156 // Serialize/Marshall the patch data into |outData|.
157 static void serialize(const Res_png_9patch& patchHeader, const int32_t* xDivs,
158 const int32_t* yDivs, const uint32_t* colors, void* outData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 // Deserialize/Unmarshall the patch data
Narayan Kamath6381dd42014-03-03 17:12:03 +0000160 static Res_png_9patch* deserialize(void* data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 // Compute the size of the serialized data structure
Narayan Kamath6381dd42014-03-03 17:12:03 +0000162 size_t serializedSize() const;
163
164 // These tell where the next section of a patch starts.
165 // For example, the first patch includes the pixels from
166 // 0 to xDivs[0]-1 and the second patch includes the pixels
167 // from xDivs[0] to xDivs[1]-1.
168 inline int32_t* getXDivs() const {
169 return reinterpret_cast<int32_t*>(reinterpret_cast<uintptr_t>(this) + xDivsOffset);
170 }
171 inline int32_t* getYDivs() const {
172 return reinterpret_cast<int32_t*>(reinterpret_cast<uintptr_t>(this) + yDivsOffset);
173 }
174 inline uint32_t* getColors() const {
175 return reinterpret_cast<uint32_t*>(reinterpret_cast<uintptr_t>(this) + colorsOffset);
176 }
177
178} __attribute__((packed));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
180/** ********************************************************************
181 * Base Types
182 *
183 * These are standard types that are shared between multiple specific
184 * resource types.
185 *
186 *********************************************************************** */
187
188/**
189 * Header that appears at the front of every data chunk in a resource.
190 */
191struct ResChunk_header
192{
193 // Type identifier for this chunk. The meaning of this value depends
194 // on the containing chunk.
195 uint16_t type;
196
197 // Size of the chunk header (in bytes). Adding this value to
198 // the address of the chunk allows you to find its associated data
199 // (if any).
200 uint16_t headerSize;
201
202 // Total size of this chunk (in bytes). This is the chunkSize plus
203 // the size of any data associated with the chunk. Adding this value
204 // to the chunk allows you to completely skip its contents (including
205 // any child chunks). If this value is the same as chunkSize, there is
206 // no data associated with the chunk.
207 uint32_t size;
208};
209
210enum {
211 RES_NULL_TYPE = 0x0000,
212 RES_STRING_POOL_TYPE = 0x0001,
213 RES_TABLE_TYPE = 0x0002,
214 RES_XML_TYPE = 0x0003,
215
216 // Chunk types in RES_XML_TYPE
217 RES_XML_FIRST_CHUNK_TYPE = 0x0100,
218 RES_XML_START_NAMESPACE_TYPE= 0x0100,
219 RES_XML_END_NAMESPACE_TYPE = 0x0101,
220 RES_XML_START_ELEMENT_TYPE = 0x0102,
221 RES_XML_END_ELEMENT_TYPE = 0x0103,
222 RES_XML_CDATA_TYPE = 0x0104,
223 RES_XML_LAST_CHUNK_TYPE = 0x017f,
224 // This contains a uint32_t array mapping strings in the string
225 // pool back to resource identifiers. It is optional.
226 RES_XML_RESOURCE_MAP_TYPE = 0x0180,
227
228 // Chunk types in RES_TABLE_TYPE
229 RES_TABLE_PACKAGE_TYPE = 0x0200,
230 RES_TABLE_TYPE_TYPE = 0x0201,
Adam Lesinskide898ff2014-01-29 18:20:45 -0800231 RES_TABLE_TYPE_SPEC_TYPE = 0x0202,
232 RES_TABLE_LIBRARY_TYPE = 0x0203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233};
234
235/**
236 * Macros for building/splitting resource identifiers.
237 */
238#define Res_VALIDID(resid) (resid != 0)
239#define Res_CHECKID(resid) ((resid&0xFFFF0000) != 0)
240#define Res_MAKEID(package, type, entry) \
241 (((package+1)<<24) | (((type+1)&0xFF)<<16) | (entry&0xFFFF))
242#define Res_GETPACKAGE(id) ((id>>24)-1)
243#define Res_GETTYPE(id) (((id>>16)&0xFF)-1)
244#define Res_GETENTRY(id) (id&0xFFFF)
245
246#define Res_INTERNALID(resid) ((resid&0xFFFF0000) != 0 && (resid&0xFF0000) == 0)
247#define Res_MAKEINTERNAL(entry) (0x01000000 | (entry&0xFFFF))
248#define Res_MAKEARRAY(entry) (0x02000000 | (entry&0xFFFF))
249
250#define Res_MAXPACKAGE 255
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700251#define Res_MAXTYPE 255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253/**
254 * Representation of a value in a resource, supplying type
255 * information.
256 */
257struct Res_value
258{
259 // Number of bytes in this structure.
260 uint16_t size;
261
262 // Always set to 0.
263 uint8_t res0;
264
265 // Type of the data value.
266 enum {
267 // Contains no data.
268 TYPE_NULL = 0x00,
269 // The 'data' holds a ResTable_ref, a reference to another resource
270 // table entry.
271 TYPE_REFERENCE = 0x01,
272 // The 'data' holds an attribute resource identifier.
273 TYPE_ATTRIBUTE = 0x02,
274 // The 'data' holds an index into the containing resource table's
275 // global value string pool.
276 TYPE_STRING = 0x03,
277 // The 'data' holds a single-precision floating point number.
278 TYPE_FLOAT = 0x04,
279 // The 'data' holds a complex number encoding a dimension value,
280 // such as "100in".
281 TYPE_DIMENSION = 0x05,
282 // The 'data' holds a complex number encoding a fraction of a
283 // container.
284 TYPE_FRACTION = 0x06,
Adam Lesinskide898ff2014-01-29 18:20:45 -0800285 // The 'data' holds a dynamic ResTable_ref, which needs to be
286 // resolved before it can be used like a TYPE_REFERENCE.
287 TYPE_DYNAMIC_REFERENCE = 0x07,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288
289 // Beginning of integer flavors...
290 TYPE_FIRST_INT = 0x10,
291
292 // The 'data' is a raw integer value of the form n..n.
293 TYPE_INT_DEC = 0x10,
294 // The 'data' is a raw integer value of the form 0xn..n.
295 TYPE_INT_HEX = 0x11,
296 // The 'data' is either 0 or 1, for input "false" or "true" respectively.
297 TYPE_INT_BOOLEAN = 0x12,
298
299 // Beginning of color integer flavors...
300 TYPE_FIRST_COLOR_INT = 0x1c,
301
302 // The 'data' is a raw integer value of the form #aarrggbb.
303 TYPE_INT_COLOR_ARGB8 = 0x1c,
304 // The 'data' is a raw integer value of the form #rrggbb.
305 TYPE_INT_COLOR_RGB8 = 0x1d,
306 // The 'data' is a raw integer value of the form #argb.
307 TYPE_INT_COLOR_ARGB4 = 0x1e,
308 // The 'data' is a raw integer value of the form #rgb.
309 TYPE_INT_COLOR_RGB4 = 0x1f,
310
311 // ...end of integer flavors.
312 TYPE_LAST_COLOR_INT = 0x1f,
313
314 // ...end of integer flavors.
315 TYPE_LAST_INT = 0x1f
316 };
317 uint8_t dataType;
318
319 // Structure of complex data values (TYPE_UNIT and TYPE_FRACTION)
320 enum {
321 // Where the unit type information is. This gives us 16 possible
322 // types, as defined below.
323 COMPLEX_UNIT_SHIFT = 0,
324 COMPLEX_UNIT_MASK = 0xf,
325
326 // TYPE_DIMENSION: Value is raw pixels.
327 COMPLEX_UNIT_PX = 0,
328 // TYPE_DIMENSION: Value is Device Independent Pixels.
329 COMPLEX_UNIT_DIP = 1,
330 // TYPE_DIMENSION: Value is a Scaled device independent Pixels.
331 COMPLEX_UNIT_SP = 2,
332 // TYPE_DIMENSION: Value is in points.
333 COMPLEX_UNIT_PT = 3,
334 // TYPE_DIMENSION: Value is in inches.
335 COMPLEX_UNIT_IN = 4,
336 // TYPE_DIMENSION: Value is in millimeters.
337 COMPLEX_UNIT_MM = 5,
338
339 // TYPE_FRACTION: A basic fraction of the overall size.
340 COMPLEX_UNIT_FRACTION = 0,
341 // TYPE_FRACTION: A fraction of the parent size.
342 COMPLEX_UNIT_FRACTION_PARENT = 1,
343
344 // Where the radix information is, telling where the decimal place
345 // appears in the mantissa. This give us 4 possible fixed point
346 // representations as defined below.
347 COMPLEX_RADIX_SHIFT = 4,
348 COMPLEX_RADIX_MASK = 0x3,
349
350 // The mantissa is an integral number -- i.e., 0xnnnnnn.0
351 COMPLEX_RADIX_23p0 = 0,
352 // The mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn
353 COMPLEX_RADIX_16p7 = 1,
354 // The mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn
355 COMPLEX_RADIX_8p15 = 2,
356 // The mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn
357 COMPLEX_RADIX_0p23 = 3,
358
359 // Where the actual value is. This gives us 23 bits of
360 // precision. The top bit is the sign.
361 COMPLEX_MANTISSA_SHIFT = 8,
362 COMPLEX_MANTISSA_MASK = 0xffffff
363 };
364
365 // The data for this item, as interpreted according to dataType.
366 uint32_t data;
367
368 void copyFrom_dtoh(const Res_value& src);
369};
370
371/**
372 * This is a reference to a unique entry (a ResTable_entry structure)
373 * in a resource table. The value is structured as: 0xpptteeee,
374 * where pp is the package index, tt is the type index in that
375 * package, and eeee is the entry index in that type. The package
376 * and type values start at 1 for the first item, to help catch cases
377 * where they have not been supplied.
378 */
379struct ResTable_ref
380{
381 uint32_t ident;
382};
383
384/**
385 * Reference to a string in a string pool.
386 */
387struct ResStringPool_ref
388{
389 // Index into the string pool table (uint32_t-offset from the indices
390 // immediately after ResStringPool_header) at which to find the location
391 // of the string data in the pool.
392 uint32_t index;
393};
394
395/** ********************************************************************
396 * String Pool
397 *
398 * A set of strings that can be references by others through a
399 * ResStringPool_ref.
400 *
401 *********************************************************************** */
402
403/**
404 * Definition for a pool of strings. The data of this chunk is an
405 * array of uint32_t providing indices into the pool, relative to
406 * stringsStart. At stringsStart are all of the UTF-16 strings
407 * concatenated together; each starts with a uint16_t of the string's
408 * length and each ends with a 0x0000 terminator. If a string is >
409 * 32767 characters, the high bit of the length is set meaning to take
410 * those 15 bits as a high word and it will be followed by another
411 * uint16_t containing the low word.
412 *
413 * If styleCount is not zero, then immediately following the array of
414 * uint32_t indices into the string table is another array of indices
415 * into a style table starting at stylesStart. Each entry in the
416 * style table is an array of ResStringPool_span structures.
417 */
418struct ResStringPool_header
419{
420 struct ResChunk_header header;
421
422 // Number of strings in this pool (number of uint32_t indices that follow
423 // in the data).
424 uint32_t stringCount;
425
426 // Number of style span arrays in the pool (number of uint32_t indices
427 // follow the string indices).
428 uint32_t styleCount;
429
430 // Flags.
431 enum {
432 // If set, the string index is sorted by the string values (based
433 // on strcmp16()).
Kenny Root19138462009-12-04 09:38:48 -0800434 SORTED_FLAG = 1<<0,
435
436 // String pool is encoded in UTF-8
437 UTF8_FLAG = 1<<8
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 };
439 uint32_t flags;
440
441 // Index from header of the string data.
442 uint32_t stringsStart;
443
444 // Index from header of the style data.
445 uint32_t stylesStart;
446};
447
448/**
449 * This structure defines a span of style information associated with
450 * a string in the pool.
451 */
452struct ResStringPool_span
453{
454 enum {
455 END = 0xFFFFFFFF
456 };
457
458 // This is the name of the span -- that is, the name of the XML
459 // tag that defined it. The special value END (0xFFFFFFFF) indicates
460 // the end of an array of spans.
461 ResStringPool_ref name;
462
463 // The range of characters in the string that this span applies to.
464 uint32_t firstChar, lastChar;
465};
466
467/**
468 * Convenience class for accessing data in a ResStringPool resource.
469 */
470class ResStringPool
471{
472public:
473 ResStringPool();
474 ResStringPool(const void* data, size_t size, bool copyData=false);
475 ~ResStringPool();
476
Adam Lesinskide898ff2014-01-29 18:20:45 -0800477 void setToEmpty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 status_t setTo(const void* data, size_t size, bool copyData=false);
479
480 status_t getError() const;
481
482 void uninit();
483
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800484 // Return string entry as UTF16; if the pool is UTF8, the string will
485 // be converted before returning.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 inline const char16_t* stringAt(const ResStringPool_ref& ref, size_t* outLen) const {
487 return stringAt(ref.index, outLen);
488 }
489 const char16_t* stringAt(size_t idx, size_t* outLen) const;
490
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800491 // Note: returns null if the string pool is not UTF8.
Kenny Root780d2a12010-02-22 22:36:26 -0800492 const char* string8At(size_t idx, size_t* outLen) const;
493
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800494 // Return string whether the pool is UTF8 or UTF16. Does not allow you
495 // to distinguish null.
496 const String8 string8ObjectAt(size_t idx) const;
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 const ResStringPool_span* styleAt(const ResStringPool_ref& ref) const;
499 const ResStringPool_span* styleAt(size_t idx) const;
500
501 ssize_t indexOfString(const char16_t* str, size_t strLen) const;
502
503 size_t size() const;
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800504 size_t styleCount() const;
505 size_t bytes() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800507 bool isSorted() const;
Kenny Rootbb79f642009-12-10 14:20:15 -0800508 bool isUTF8() const;
Kenny Rootbb79f642009-12-10 14:20:15 -0800509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510private:
511 status_t mError;
512 void* mOwnedData;
513 const ResStringPool_header* mHeader;
514 size_t mSize;
Kenny Root19138462009-12-04 09:38:48 -0800515 mutable Mutex mDecodeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 const uint32_t* mEntries;
517 const uint32_t* mEntryStyles;
Kenny Root19138462009-12-04 09:38:48 -0800518 const void* mStrings;
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700519 char16_t mutable** mCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 uint32_t mStringPoolSize; // number of uint16_t
521 const uint32_t* mStyles;
522 uint32_t mStylePoolSize; // number of uint32_t
523};
524
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700525/**
526 * Wrapper class that allows the caller to retrieve a string from
527 * a string pool without knowing which string pool to look.
528 */
529class StringPoolRef {
530public:
531 StringPoolRef();
532 StringPoolRef(const ResStringPool* pool, uint32_t index);
533
534 const char* string8(size_t* outLen) const;
535 const char16_t* string16(size_t* outLen) const;
536
537private:
538 const ResStringPool* mPool;
539 uint32_t mIndex;
540};
541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542/** ********************************************************************
543 * XML Tree
544 *
545 * Binary representation of an XML document. This is designed to
546 * express everything in an XML document, in a form that is much
547 * easier to parse on the device.
548 *
549 *********************************************************************** */
550
551/**
552 * XML tree header. This appears at the front of an XML tree,
553 * describing its content. It is followed by a flat array of
554 * ResXMLTree_node structures; the hierarchy of the XML document
555 * is described by the occurrance of RES_XML_START_ELEMENT_TYPE
556 * and corresponding RES_XML_END_ELEMENT_TYPE nodes in the array.
557 */
558struct ResXMLTree_header
559{
560 struct ResChunk_header header;
561};
562
563/**
564 * Basic XML tree node. A single item in the XML document. Extended info
565 * about the node can be found after header.headerSize.
566 */
567struct ResXMLTree_node
568{
569 struct ResChunk_header header;
570
571 // Line number in original source file at which this element appeared.
572 uint32_t lineNumber;
573
574 // Optional XML comment that was associated with this element; -1 if none.
575 struct ResStringPool_ref comment;
576};
577
578/**
579 * Extended XML tree node for CDATA tags -- includes the CDATA string.
580 * Appears header.headerSize bytes after a ResXMLTree_node.
581 */
582struct ResXMLTree_cdataExt
583{
584 // The raw CDATA character data.
585 struct ResStringPool_ref data;
586
587 // The typed value of the character data if this is a CDATA node.
588 struct Res_value typedData;
589};
590
591/**
592 * Extended XML tree node for namespace start/end nodes.
593 * Appears header.headerSize bytes after a ResXMLTree_node.
594 */
595struct ResXMLTree_namespaceExt
596{
597 // The prefix of the namespace.
598 struct ResStringPool_ref prefix;
599
600 // The URI of the namespace.
601 struct ResStringPool_ref uri;
602};
603
604/**
605 * Extended XML tree node for element start/end nodes.
606 * Appears header.headerSize bytes after a ResXMLTree_node.
607 */
608struct ResXMLTree_endElementExt
609{
610 // String of the full namespace of this element.
611 struct ResStringPool_ref ns;
612
613 // String name of this node if it is an ELEMENT; the raw
614 // character data if this is a CDATA node.
615 struct ResStringPool_ref name;
616};
617
618/**
619 * Extended XML tree node for start tags -- includes attribute
620 * information.
621 * Appears header.headerSize bytes after a ResXMLTree_node.
622 */
623struct ResXMLTree_attrExt
624{
625 // String of the full namespace of this element.
626 struct ResStringPool_ref ns;
627
628 // String name of this node if it is an ELEMENT; the raw
629 // character data if this is a CDATA node.
630 struct ResStringPool_ref name;
631
632 // Byte offset from the start of this structure where the attributes start.
633 uint16_t attributeStart;
634
635 // Size of the ResXMLTree_attribute structures that follow.
636 uint16_t attributeSize;
637
638 // Number of attributes associated with an ELEMENT. These are
639 // available as an array of ResXMLTree_attribute structures
640 // immediately following this node.
641 uint16_t attributeCount;
642
643 // Index (1-based) of the "id" attribute. 0 if none.
644 uint16_t idIndex;
645
646 // Index (1-based) of the "class" attribute. 0 if none.
647 uint16_t classIndex;
648
649 // Index (1-based) of the "style" attribute. 0 if none.
650 uint16_t styleIndex;
651};
652
653struct ResXMLTree_attribute
654{
655 // Namespace of this attribute.
656 struct ResStringPool_ref ns;
657
658 // Name of this attribute.
659 struct ResStringPool_ref name;
660
661 // The original raw string value of this attribute.
662 struct ResStringPool_ref rawValue;
663
664 // Processesd typed value of this attribute.
665 struct Res_value typedValue;
666};
667
668class ResXMLTree;
669
670class ResXMLParser
671{
672public:
673 ResXMLParser(const ResXMLTree& tree);
674
675 enum event_code_t {
676 BAD_DOCUMENT = -1,
677 START_DOCUMENT = 0,
678 END_DOCUMENT = 1,
679
680 FIRST_CHUNK_CODE = RES_XML_FIRST_CHUNK_TYPE,
681
682 START_NAMESPACE = RES_XML_START_NAMESPACE_TYPE,
683 END_NAMESPACE = RES_XML_END_NAMESPACE_TYPE,
684 START_TAG = RES_XML_START_ELEMENT_TYPE,
685 END_TAG = RES_XML_END_ELEMENT_TYPE,
686 TEXT = RES_XML_CDATA_TYPE
687 };
688
689 struct ResXMLPosition
690 {
691 event_code_t eventCode;
692 const ResXMLTree_node* curNode;
693 const void* curExt;
694 };
695
696 void restart();
697
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800698 const ResStringPool& getStrings() const;
699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 event_code_t getEventType() const;
701 // Note, unlike XmlPullParser, the first call to next() will return
702 // START_TAG of the first element.
703 event_code_t next();
704
705 // These are available for all nodes:
Mathias Agopian5f910972009-06-22 02:35:32 -0700706 int32_t getCommentID() const;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800707 const char16_t* getComment(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700708 uint32_t getLineNumber() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709
710 // This is available for TEXT:
Mathias Agopian5f910972009-06-22 02:35:32 -0700711 int32_t getTextID() const;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800712 const char16_t* getText(size_t* outLen) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 ssize_t getTextValue(Res_value* outValue) const;
714
715 // These are available for START_NAMESPACE and END_NAMESPACE:
Mathias Agopian5f910972009-06-22 02:35:32 -0700716 int32_t getNamespacePrefixID() const;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800717 const char16_t* getNamespacePrefix(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700718 int32_t getNamespaceUriID() const;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800719 const char16_t* getNamespaceUri(size_t* outLen) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720
721 // These are available for START_TAG and END_TAG:
Mathias Agopian5f910972009-06-22 02:35:32 -0700722 int32_t getElementNamespaceID() const;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800723 const char16_t* getElementNamespace(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700724 int32_t getElementNameID() const;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800725 const char16_t* getElementName(size_t* outLen) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
727 // Remaining methods are for retrieving information about attributes
728 // associated with a START_TAG:
729
730 size_t getAttributeCount() const;
731
732 // Returns -1 if no namespace, -2 if idx out of range.
Mathias Agopian5f910972009-06-22 02:35:32 -0700733 int32_t getAttributeNamespaceID(size_t idx) const;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800734 const char16_t* getAttributeNamespace(size_t idx, size_t* outLen) const;
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700735
Mathias Agopian5f910972009-06-22 02:35:32 -0700736 int32_t getAttributeNameID(size_t idx) const;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800737 const char16_t* getAttributeName(size_t idx, size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700738 uint32_t getAttributeNameResID(size_t idx) const;
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700739
740 // These will work only if the underlying string pool is UTF-8.
741 const char* getAttributeNamespace8(size_t idx, size_t* outLen) const;
742 const char* getAttributeName8(size_t idx, size_t* outLen) const;
743
Mathias Agopian5f910972009-06-22 02:35:32 -0700744 int32_t getAttributeValueStringID(size_t idx) const;
Adam Lesinski4bf58102014-11-03 11:21:19 -0800745 const char16_t* getAttributeStringValue(size_t idx, size_t* outLen) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746
747 int32_t getAttributeDataType(size_t idx) const;
748 int32_t getAttributeData(size_t idx) const;
749 ssize_t getAttributeValue(size_t idx, Res_value* outValue) const;
750
751 ssize_t indexOfAttribute(const char* ns, const char* attr) const;
752 ssize_t indexOfAttribute(const char16_t* ns, size_t nsLen,
753 const char16_t* attr, size_t attrLen) const;
754
755 ssize_t indexOfID() const;
756 ssize_t indexOfClass() const;
757 ssize_t indexOfStyle() const;
758
759 void getPosition(ResXMLPosition* pos) const;
760 void setPosition(const ResXMLPosition& pos);
761
762private:
763 friend class ResXMLTree;
764
765 event_code_t nextNode();
766
767 const ResXMLTree& mTree;
768 event_code_t mEventCode;
769 const ResXMLTree_node* mCurNode;
770 const void* mCurExt;
771};
772
Adam Lesinskide898ff2014-01-29 18:20:45 -0800773class DynamicRefTable;
774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775/**
776 * Convenience class for accessing data in a ResXMLTree resource.
777 */
778class ResXMLTree : public ResXMLParser
779{
780public:
Adam Lesinskide898ff2014-01-29 18:20:45 -0800781 ResXMLTree(const DynamicRefTable* dynamicRefTable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 ResXMLTree();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 ~ResXMLTree();
784
785 status_t setTo(const void* data, size_t size, bool copyData=false);
786
787 status_t getError() const;
788
789 void uninit();
790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791private:
792 friend class ResXMLParser;
793
794 status_t validateNode(const ResXMLTree_node* node) const;
795
Adam Lesinskide898ff2014-01-29 18:20:45 -0800796 const DynamicRefTable* const mDynamicRefTable;
797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 status_t mError;
799 void* mOwnedData;
800 const ResXMLTree_header* mHeader;
801 size_t mSize;
802 const uint8_t* mDataEnd;
803 ResStringPool mStrings;
804 const uint32_t* mResIds;
805 size_t mNumResIds;
806 const ResXMLTree_node* mRootNode;
807 const void* mRootExt;
808 event_code_t mRootCode;
809};
810
811/** ********************************************************************
812 * RESOURCE TABLE
813 *
814 *********************************************************************** */
815
816/**
817 * Header for a resource table. Its data contains a series of
818 * additional chunks:
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800819 * * A ResStringPool_header containing all table values. This string pool
820 * contains all of the string values in the entire resource table (not
821 * the names of entries or type identifiers however).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 * * One or more ResTable_package chunks.
823 *
824 * Specific entries within a resource table can be uniquely identified
825 * with a single integer as defined by the ResTable_ref structure.
826 */
827struct ResTable_header
828{
829 struct ResChunk_header header;
830
831 // The number of ResTable_package structures.
832 uint32_t packageCount;
833};
834
835/**
836 * A collection of resource data types within a package. Followed by
837 * one or more ResTable_type and ResTable_typeSpec structures containing the
838 * entry values for each resource type.
839 */
840struct ResTable_package
841{
842 struct ResChunk_header header;
843
844 // If this is a base package, its ID. Package IDs start
845 // at 1 (corresponding to the value of the package bits in a
846 // resource identifier). 0 means this is not a base package.
847 uint32_t id;
848
849 // Actual name of this package, \0-terminated.
Adam Lesinski4bf58102014-11-03 11:21:19 -0800850 uint16_t name[128];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851
852 // Offset to a ResStringPool_header defining the resource
853 // type symbol table. If zero, this package is inheriting from
854 // another base package (overriding specific values in it).
855 uint32_t typeStrings;
856
857 // Last index into typeStrings that is for public use by others.
858 uint32_t lastPublicType;
859
860 // Offset to a ResStringPool_header defining the resource
861 // key symbol table. If zero, this package is inheriting from
862 // another base package (overriding specific values in it).
863 uint32_t keyStrings;
864
865 // Last index into keyStrings that is for public use by others.
866 uint32_t lastPublicKey;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700867
868 uint32_t typeIdOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869};
870
Narayan Kamath48620f12014-01-20 13:57:11 +0000871// The most specific locale can consist of:
872//
873// - a 3 char language code
874// - a 3 char region code prefixed by a 'r'
875// - a 4 char script code prefixed by a 's'
876// - a 8 char variant code prefixed by a 'v'
877//
878// each separated by a single char separator, which sums up to a total of 24
879// chars, (25 include the string terminator) rounded up to 28 to be 4 byte
880// aligned.
881#define RESTABLE_MAX_LOCALE_LEN 28
882
883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884/**
885 * Describes a particular resource configuration.
886 */
887struct ResTable_config
888{
889 // Number of bytes in this structure.
890 uint32_t size;
891
892 union {
893 struct {
894 // Mobile country code (from SIM). 0 means "any".
895 uint16_t mcc;
896 // Mobile network code (from SIM). 0 means "any".
897 uint16_t mnc;
898 };
899 uint32_t imsi;
900 };
901
902 union {
903 struct {
Narayan Kamath48620f12014-01-20 13:57:11 +0000904 // This field can take three different forms:
905 // - \0\0 means "any".
906 //
907 // - Two 7 bit ascii values interpreted as ISO-639-1 language
908 // codes ('fr', 'en' etc. etc.). The high bit for both bytes is
909 // zero.
910 //
911 // - A single 16 bit little endian packed value representing an
912 // ISO-639-2 3 letter language code. This will be of the form:
913 //
914 // {1, t, t, t, t, t, s, s, s, s, s, f, f, f, f, f}
915 //
916 // bit[0, 4] = first letter of the language code
917 // bit[5, 9] = second letter of the language code
918 // bit[10, 14] = third letter of the language code.
919 // bit[15] = 1 always
920 //
921 // For backwards compatibility, languages that have unambiguous
922 // two letter codes are represented in that format.
923 //
924 // The layout is always bigendian irrespective of the runtime
925 // architecture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 char language[2];
927
Narayan Kamath48620f12014-01-20 13:57:11 +0000928 // This field can take three different forms:
929 // - \0\0 means "any".
930 //
931 // - Two 7 bit ascii values interpreted as 2 letter region
932 // codes ('US', 'GB' etc.). The high bit for both bytes is zero.
933 //
934 // - An UN M.49 3 digit region code. For simplicity, these are packed
935 // in the same manner as the language codes, though we should need
936 // only 10 bits to represent them, instead of the 15.
937 //
938 // The layout is always bigendian irrespective of the runtime
939 // architecture.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 char country[2];
941 };
942 uint32_t locale;
943 };
944
945 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700946 ORIENTATION_ANY = ACONFIGURATION_ORIENTATION_ANY,
947 ORIENTATION_PORT = ACONFIGURATION_ORIENTATION_PORT,
948 ORIENTATION_LAND = ACONFIGURATION_ORIENTATION_LAND,
949 ORIENTATION_SQUARE = ACONFIGURATION_ORIENTATION_SQUARE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 };
951
952 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700953 TOUCHSCREEN_ANY = ACONFIGURATION_TOUCHSCREEN_ANY,
954 TOUCHSCREEN_NOTOUCH = ACONFIGURATION_TOUCHSCREEN_NOTOUCH,
955 TOUCHSCREEN_STYLUS = ACONFIGURATION_TOUCHSCREEN_STYLUS,
956 TOUCHSCREEN_FINGER = ACONFIGURATION_TOUCHSCREEN_FINGER,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 };
958
959 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700960 DENSITY_DEFAULT = ACONFIGURATION_DENSITY_DEFAULT,
961 DENSITY_LOW = ACONFIGURATION_DENSITY_LOW,
962 DENSITY_MEDIUM = ACONFIGURATION_DENSITY_MEDIUM,
Dianne Hackbornb96cbbd2011-05-27 13:40:26 -0700963 DENSITY_TV = ACONFIGURATION_DENSITY_TV,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700964 DENSITY_HIGH = ACONFIGURATION_DENSITY_HIGH,
Dianne Hackbornd96e3df2012-01-25 15:12:23 -0800965 DENSITY_XHIGH = ACONFIGURATION_DENSITY_XHIGH,
966 DENSITY_XXHIGH = ACONFIGURATION_DENSITY_XXHIGH,
Dianne Hackborn56a23012013-02-12 15:41:49 -0800967 DENSITY_XXXHIGH = ACONFIGURATION_DENSITY_XXXHIGH,
Adam Lesinski31245b42014-08-22 19:10:56 -0700968 DENSITY_ANY = ACONFIGURATION_DENSITY_ANY,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700969 DENSITY_NONE = ACONFIGURATION_DENSITY_NONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 };
971
972 union {
973 struct {
974 uint8_t orientation;
975 uint8_t touchscreen;
976 uint16_t density;
977 };
978 uint32_t screenType;
979 };
980
981 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700982 KEYBOARD_ANY = ACONFIGURATION_KEYBOARD_ANY,
983 KEYBOARD_NOKEYS = ACONFIGURATION_KEYBOARD_NOKEYS,
984 KEYBOARD_QWERTY = ACONFIGURATION_KEYBOARD_QWERTY,
985 KEYBOARD_12KEY = ACONFIGURATION_KEYBOARD_12KEY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 };
987
988 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700989 NAVIGATION_ANY = ACONFIGURATION_NAVIGATION_ANY,
990 NAVIGATION_NONAV = ACONFIGURATION_NAVIGATION_NONAV,
991 NAVIGATION_DPAD = ACONFIGURATION_NAVIGATION_DPAD,
992 NAVIGATION_TRACKBALL = ACONFIGURATION_NAVIGATION_TRACKBALL,
993 NAVIGATION_WHEEL = ACONFIGURATION_NAVIGATION_WHEEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 };
995
996 enum {
997 MASK_KEYSHIDDEN = 0x0003,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700998 KEYSHIDDEN_ANY = ACONFIGURATION_KEYSHIDDEN_ANY,
999 KEYSHIDDEN_NO = ACONFIGURATION_KEYSHIDDEN_NO,
1000 KEYSHIDDEN_YES = ACONFIGURATION_KEYSHIDDEN_YES,
1001 KEYSHIDDEN_SOFT = ACONFIGURATION_KEYSHIDDEN_SOFT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 };
1003
Dianne Hackborn93e462b2009-09-15 22:50:40 -07001004 enum {
1005 MASK_NAVHIDDEN = 0x000c,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001006 SHIFT_NAVHIDDEN = 2,
1007 NAVHIDDEN_ANY = ACONFIGURATION_NAVHIDDEN_ANY << SHIFT_NAVHIDDEN,
1008 NAVHIDDEN_NO = ACONFIGURATION_NAVHIDDEN_NO << SHIFT_NAVHIDDEN,
1009 NAVHIDDEN_YES = ACONFIGURATION_NAVHIDDEN_YES << SHIFT_NAVHIDDEN,
Dianne Hackborn93e462b2009-09-15 22:50:40 -07001010 };
1011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 union {
1013 struct {
1014 uint8_t keyboard;
1015 uint8_t navigation;
1016 uint8_t inputFlags;
Dianne Hackborn723738c2009-06-25 19:48:04 -07001017 uint8_t inputPad0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 };
1019 uint32_t input;
1020 };
1021
1022 enum {
1023 SCREENWIDTH_ANY = 0
1024 };
1025
1026 enum {
1027 SCREENHEIGHT_ANY = 0
1028 };
1029
1030 union {
1031 struct {
1032 uint16_t screenWidth;
1033 uint16_t screenHeight;
1034 };
1035 uint32_t screenSize;
1036 };
1037
1038 enum {
1039 SDKVERSION_ANY = 0
1040 };
1041
Narayan Kamath48620f12014-01-20 13:57:11 +00001042 enum {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 MINORVERSION_ANY = 0
1044 };
1045
1046 union {
1047 struct {
1048 uint16_t sdkVersion;
1049 // For now minorVersion must always be 0!!! Its meaning
1050 // is currently undefined.
1051 uint16_t minorVersion;
1052 };
1053 uint32_t version;
1054 };
1055
Dianne Hackborn723738c2009-06-25 19:48:04 -07001056 enum {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001057 // screenLayout bits for screen size class.
1058 MASK_SCREENSIZE = 0x0f,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001059 SCREENSIZE_ANY = ACONFIGURATION_SCREENSIZE_ANY,
1060 SCREENSIZE_SMALL = ACONFIGURATION_SCREENSIZE_SMALL,
1061 SCREENSIZE_NORMAL = ACONFIGURATION_SCREENSIZE_NORMAL,
1062 SCREENSIZE_LARGE = ACONFIGURATION_SCREENSIZE_LARGE,
1063 SCREENSIZE_XLARGE = ACONFIGURATION_SCREENSIZE_XLARGE,
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001064
1065 // screenLayout bits for wide/long screen variation.
1066 MASK_SCREENLONG = 0x30,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001067 SHIFT_SCREENLONG = 4,
1068 SCREENLONG_ANY = ACONFIGURATION_SCREENLONG_ANY << SHIFT_SCREENLONG,
1069 SCREENLONG_NO = ACONFIGURATION_SCREENLONG_NO << SHIFT_SCREENLONG,
1070 SCREENLONG_YES = ACONFIGURATION_SCREENLONG_YES << SHIFT_SCREENLONG,
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07001071
1072 // screenLayout bits for layout direction.
1073 MASK_LAYOUTDIR = 0xC0,
1074 SHIFT_LAYOUTDIR = 6,
1075 LAYOUTDIR_ANY = ACONFIGURATION_LAYOUTDIR_ANY << SHIFT_LAYOUTDIR,
1076 LAYOUTDIR_LTR = ACONFIGURATION_LAYOUTDIR_LTR << SHIFT_LAYOUTDIR,
1077 LAYOUTDIR_RTL = ACONFIGURATION_LAYOUTDIR_RTL << SHIFT_LAYOUTDIR,
Dianne Hackborn723738c2009-06-25 19:48:04 -07001078 };
1079
Tobias Haamel27b28b32010-02-09 23:09:17 +01001080 enum {
1081 // uiMode bits for the mode type.
1082 MASK_UI_MODE_TYPE = 0x0f,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001083 UI_MODE_TYPE_ANY = ACONFIGURATION_UI_MODE_TYPE_ANY,
1084 UI_MODE_TYPE_NORMAL = ACONFIGURATION_UI_MODE_TYPE_NORMAL,
1085 UI_MODE_TYPE_DESK = ACONFIGURATION_UI_MODE_TYPE_DESK,
1086 UI_MODE_TYPE_CAR = ACONFIGURATION_UI_MODE_TYPE_CAR,
Dianne Hackborne360bb62011-05-20 16:11:04 -07001087 UI_MODE_TYPE_TELEVISION = ACONFIGURATION_UI_MODE_TYPE_TELEVISION,
Joe Onorato44fcb832011-12-14 20:59:30 -08001088 UI_MODE_TYPE_APPLIANCE = ACONFIGURATION_UI_MODE_TYPE_APPLIANCE,
John Spurlock6c191292014-04-03 16:37:27 -04001089 UI_MODE_TYPE_WATCH = ACONFIGURATION_UI_MODE_TYPE_WATCH,
Tobias Haamel27b28b32010-02-09 23:09:17 +01001090
1091 // uiMode bits for the night switch.
1092 MASK_UI_MODE_NIGHT = 0x30,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001093 SHIFT_UI_MODE_NIGHT = 4,
1094 UI_MODE_NIGHT_ANY = ACONFIGURATION_UI_MODE_NIGHT_ANY << SHIFT_UI_MODE_NIGHT,
1095 UI_MODE_NIGHT_NO = ACONFIGURATION_UI_MODE_NIGHT_NO << SHIFT_UI_MODE_NIGHT,
1096 UI_MODE_NIGHT_YES = ACONFIGURATION_UI_MODE_NIGHT_YES << SHIFT_UI_MODE_NIGHT,
Tobias Haamel27b28b32010-02-09 23:09:17 +01001097 };
1098
Dianne Hackborn723738c2009-06-25 19:48:04 -07001099 union {
1100 struct {
1101 uint8_t screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +01001102 uint8_t uiMode;
Dianne Hackborn69cb8752011-05-19 18:13:32 -07001103 uint16_t smallestScreenWidthDp;
Dianne Hackborn723738c2009-06-25 19:48:04 -07001104 };
1105 uint32_t screenConfig;
1106 };
1107
Dianne Hackbornebff8f92011-05-12 18:07:47 -07001108 union {
1109 struct {
1110 uint16_t screenWidthDp;
1111 uint16_t screenHeightDp;
1112 };
1113 uint32_t screenSizeDp;
1114 };
1115
Narayan Kamath48620f12014-01-20 13:57:11 +00001116 // The ISO-15924 short name for the script corresponding to this
1117 // configuration. (eg. Hant, Latn, etc.). Interpreted in conjunction with
Narayan Kamath788fa412014-01-21 15:32:36 +00001118 // the locale field.
Narayan Kamath48620f12014-01-20 13:57:11 +00001119 char localeScript[4];
1120
1121 // A single BCP-47 variant subtag. Will vary in length between 5 and 8
1122 // chars. Interpreted in conjunction with the locale field.
1123 char localeVariant[8];
1124
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001125 void copyFromDeviceNoSwap(const ResTable_config& o);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001127 void copyFromDtoH(const ResTable_config& o);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001129 void swapHtoD();
1130
1131 int compare(const ResTable_config& o) const;
1132 int compareLogical(const ResTable_config& o) const;
1133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 // Flags indicating a set of config values. These flag constants must
1135 // match the corresponding ones in android.content.pm.ActivityInfo and
1136 // attrs_manifest.xml.
1137 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001138 CONFIG_MCC = ACONFIGURATION_MCC,
Mårten Kongstad4482e7c2013-11-26 14:48:22 +01001139 CONFIG_MNC = ACONFIGURATION_MNC,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001140 CONFIG_LOCALE = ACONFIGURATION_LOCALE,
1141 CONFIG_TOUCHSCREEN = ACONFIGURATION_TOUCHSCREEN,
1142 CONFIG_KEYBOARD = ACONFIGURATION_KEYBOARD,
1143 CONFIG_KEYBOARD_HIDDEN = ACONFIGURATION_KEYBOARD_HIDDEN,
1144 CONFIG_NAVIGATION = ACONFIGURATION_NAVIGATION,
1145 CONFIG_ORIENTATION = ACONFIGURATION_ORIENTATION,
1146 CONFIG_DENSITY = ACONFIGURATION_DENSITY,
1147 CONFIG_SCREEN_SIZE = ACONFIGURATION_SCREEN_SIZE,
Dianne Hackborn69cb8752011-05-19 18:13:32 -07001148 CONFIG_SMALLEST_SCREEN_SIZE = ACONFIGURATION_SMALLEST_SCREEN_SIZE,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001149 CONFIG_VERSION = ACONFIGURATION_VERSION,
1150 CONFIG_SCREEN_LAYOUT = ACONFIGURATION_SCREEN_LAYOUT,
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07001151 CONFIG_UI_MODE = ACONFIGURATION_UI_MODE,
1152 CONFIG_LAYOUTDIR = ACONFIGURATION_LAYOUTDIR,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 };
1154
1155 // Compare two configuration, returning CONFIG_* flags set for each value
1156 // that is different.
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001157 int diff(const ResTable_config& o) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158
Robert Greenwalt96e20402009-04-22 14:35:11 -07001159 // Return true if 'this' is more specific than 'o'.
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001160 bool isMoreSpecificThan(const ResTable_config& o) const;
Robert Greenwalt96e20402009-04-22 14:35:11 -07001161
1162 // Return true if 'this' is a better match than 'o' for the 'requested'
1163 // configuration. This assumes that match() has already been used to
1164 // remove any configurations that don't match the requested configuration
1165 // at all; if they are not first filtered, non-matching results can be
1166 // considered better than matching ones.
1167 // The general rule per attribute: if the request cares about an attribute
1168 // (it normally does), if the two (this and o) are equal it's a tie. If
1169 // they are not equal then one must be generic because only generic and
1170 // '==requested' will pass the match() call. So if this is not generic,
1171 // it wins. If this IS generic, o wins (return false).
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001172 bool isBetterThan(const ResTable_config& o, const ResTable_config* requested) const;
Robert Greenwalt96e20402009-04-22 14:35:11 -07001173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 // Return true if 'this' can be considered a match for the parameters in
1175 // 'settings'.
1176 // Note this is asymetric. A default piece of data will match every request
1177 // but a request for the default should not match odd specifics
1178 // (ie, request with no mcc should not match a particular mcc's data)
1179 // settings is the requested settings
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001180 bool match(const ResTable_config& settings) const;
Tobias Haamel27b28b32010-02-09 23:09:17 +01001181
Narayan Kamath48620f12014-01-20 13:57:11 +00001182 // Get the string representation of the locale component of this
Narayan Kamath788fa412014-01-21 15:32:36 +00001183 // Config. The maximum size of this representation will be
1184 // |RESTABLE_MAX_LOCALE_LEN| (including a terminating '\0').
Narayan Kamath48620f12014-01-20 13:57:11 +00001185 //
Narayan Kamath788fa412014-01-21 15:32:36 +00001186 // Example: en-US, en-Latn-US, en-POSIX.
1187 void getBcp47Locale(char* out) const;
1188
1189 // Sets the values of language, region, script and variant to the
1190 // well formed BCP-47 locale contained in |in|. The input locale is
1191 // assumed to be valid and no validation is performed.
1192 void setBcp47Locale(const char* in);
1193
1194 inline void clearLocale() {
1195 locale = 0;
1196 memset(localeScript, 0, sizeof(localeScript));
1197 memset(localeVariant, 0, sizeof(localeVariant));
1198 }
1199
Narayan Kamath48620f12014-01-20 13:57:11 +00001200 // Get the 2 or 3 letter language code of this configuration. Trailing
1201 // bytes are set to '\0'.
1202 size_t unpackLanguage(char language[4]) const;
1203 // Get the 2 or 3 letter language code of this configuration. Trailing
1204 // bytes are set to '\0'.
1205 size_t unpackRegion(char region[4]) const;
1206
Narayan Kamath788fa412014-01-21 15:32:36 +00001207 // Sets the language code of this configuration to the first three
1208 // chars at |language|.
1209 //
1210 // If |language| is a 2 letter code, the trailing byte must be '\0' or
1211 // the BCP-47 separator '-'.
1212 void packLanguage(const char* language);
1213 // Sets the region code of this configuration to the first three bytes
1214 // at |region|. If |region| is a 2 letter code, the trailing byte must be '\0'
1215 // or the BCP-47 separator '-'.
1216 void packRegion(const char* region);
Narayan Kamath48620f12014-01-20 13:57:11 +00001217
1218 // Returns a positive integer if this config is more specific than |o|
1219 // with respect to their locales, a negative integer if |o| is more specific
1220 // and 0 if they're equally specific.
1221 int isLocaleMoreSpecificThan(const ResTable_config &o) const;
Tobias Haamel27b28b32010-02-09 23:09:17 +01001222
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001223 String8 toString() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224};
1225
1226/**
1227 * A specification of the resources defined by a particular type.
1228 *
1229 * There should be one of these chunks for each resource type.
1230 *
1231 * This structure is followed by an array of integers providing the set of
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07001232 * configuration change flags (ResTable_config::CONFIG_*) that have multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 * resources for that configuration. In addition, the high bit is set if that
1234 * resource has been made public.
1235 */
1236struct ResTable_typeSpec
1237{
1238 struct ResChunk_header header;
1239
1240 // The type identifier this chunk is holding. Type IDs start
1241 // at 1 (corresponding to the value of the type bits in a
1242 // resource identifier). 0 is invalid.
1243 uint8_t id;
1244
1245 // Must be 0.
1246 uint8_t res0;
1247 // Must be 0.
1248 uint16_t res1;
1249
1250 // Number of uint32_t entry configuration masks that follow.
1251 uint32_t entryCount;
1252
1253 enum {
1254 // Additional flag indicating an entry is public.
1255 SPEC_PUBLIC = 0x40000000
1256 };
1257};
1258
1259/**
1260 * A collection of resource entries for a particular resource data
1261 * type. Followed by an array of uint32_t defining the resource
1262 * values, corresponding to the array of type strings in the
1263 * ResTable_package::typeStrings string block. Each of these hold an
1264 * index from entriesStart; a value of NO_ENTRY means that entry is
1265 * not defined.
1266 *
1267 * There may be multiple of these chunks for a particular resource type,
1268 * supply different configuration variations for the resource values of
1269 * that type.
1270 *
1271 * It would be nice to have an additional ordered index of entries, so
1272 * we can do a binary search if trying to find a resource by string name.
1273 */
1274struct ResTable_type
1275{
1276 struct ResChunk_header header;
1277
1278 enum {
1279 NO_ENTRY = 0xFFFFFFFF
1280 };
1281
1282 // The type identifier this chunk is holding. Type IDs start
1283 // at 1 (corresponding to the value of the type bits in a
1284 // resource identifier). 0 is invalid.
1285 uint8_t id;
1286
1287 // Must be 0.
1288 uint8_t res0;
1289 // Must be 0.
1290 uint16_t res1;
1291
1292 // Number of uint32_t entry indices that follow.
1293 uint32_t entryCount;
1294
1295 // Offset from header where ResTable_entry data starts.
1296 uint32_t entriesStart;
1297
1298 // Configuration this collection of entries is designed for.
1299 ResTable_config config;
1300};
1301
1302/**
1303 * This is the beginning of information about an entry in the resource
1304 * table. It holds the reference to the name of this entry, and is
1305 * immediately followed by one of:
Dianne Hackbornde7faf62009-06-30 13:27:30 -07001306 * * A Res_value structure, if FLAG_COMPLEX is -not- set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 * * An array of ResTable_map structures, if FLAG_COMPLEX is set.
1308 * These supply a set of name/value mappings of data.
1309 */
1310struct ResTable_entry
1311{
1312 // Number of bytes in this structure.
1313 uint16_t size;
1314
1315 enum {
1316 // If set, this is a complex entry, holding a set of name/value
1317 // mappings. It is followed by an array of ResTable_map structures.
1318 FLAG_COMPLEX = 0x0001,
1319 // If set, this resource has been declared public, so libraries
1320 // are allowed to reference it.
1321 FLAG_PUBLIC = 0x0002
1322 };
1323 uint16_t flags;
1324
1325 // Reference into ResTable_package::keyStrings identifying this entry.
1326 struct ResStringPool_ref key;
1327};
1328
1329/**
1330 * Extended form of a ResTable_entry for map entries, defining a parent map
1331 * resource from which to inherit values.
1332 */
1333struct ResTable_map_entry : public ResTable_entry
1334{
1335 // Resource identifier of the parent mapping, or 0 if there is none.
Adam Lesinskide898ff2014-01-29 18:20:45 -08001336 // This is always treated as a TYPE_DYNAMIC_REFERENCE.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 ResTable_ref parent;
1338 // Number of name/value pairs that follow for FLAG_COMPLEX.
1339 uint32_t count;
1340};
1341
1342/**
1343 * A single name/value mapping that is part of a complex resource
1344 * entry.
1345 */
1346struct ResTable_map
1347{
1348 // The resource identifier defining this mapping's name. For attribute
1349 // resources, 'name' can be one of the following special resource types
1350 // to supply meta-data about the attribute; for all other resource types
1351 // it must be an attribute resource.
1352 ResTable_ref name;
1353
1354 // Special values for 'name' when defining attribute resources.
1355 enum {
1356 // This entry holds the attribute's type code.
1357 ATTR_TYPE = Res_MAKEINTERNAL(0),
1358
1359 // For integral attributes, this is the minimum value it can hold.
1360 ATTR_MIN = Res_MAKEINTERNAL(1),
1361
1362 // For integral attributes, this is the maximum value it can hold.
1363 ATTR_MAX = Res_MAKEINTERNAL(2),
1364
1365 // Localization of this resource is can be encouraged or required with
1366 // an aapt flag if this is set
1367 ATTR_L10N = Res_MAKEINTERNAL(3),
1368
1369 // for plural support, see android.content.res.PluralRules#attrForQuantity(int)
1370 ATTR_OTHER = Res_MAKEINTERNAL(4),
1371 ATTR_ZERO = Res_MAKEINTERNAL(5),
1372 ATTR_ONE = Res_MAKEINTERNAL(6),
1373 ATTR_TWO = Res_MAKEINTERNAL(7),
1374 ATTR_FEW = Res_MAKEINTERNAL(8),
1375 ATTR_MANY = Res_MAKEINTERNAL(9)
1376
1377 };
1378
1379 // Bit mask of allowed types, for use with ATTR_TYPE.
1380 enum {
1381 // No type has been defined for this attribute, use generic
1382 // type handling. The low 16 bits are for types that can be
1383 // handled generically; the upper 16 require additional information
1384 // in the bag so can not be handled generically for TYPE_ANY.
1385 TYPE_ANY = 0x0000FFFF,
1386
1387 // Attribute holds a references to another resource.
1388 TYPE_REFERENCE = 1<<0,
1389
1390 // Attribute holds a generic string.
1391 TYPE_STRING = 1<<1,
1392
1393 // Attribute holds an integer value. ATTR_MIN and ATTR_MIN can
1394 // optionally specify a constrained range of possible integer values.
1395 TYPE_INTEGER = 1<<2,
1396
1397 // Attribute holds a boolean integer.
1398 TYPE_BOOLEAN = 1<<3,
1399
1400 // Attribute holds a color value.
1401 TYPE_COLOR = 1<<4,
1402
1403 // Attribute holds a floating point value.
1404 TYPE_FLOAT = 1<<5,
1405
1406 // Attribute holds a dimension value, such as "20px".
1407 TYPE_DIMENSION = 1<<6,
1408
1409 // Attribute holds a fraction value, such as "20%".
1410 TYPE_FRACTION = 1<<7,
1411
1412 // Attribute holds an enumeration. The enumeration values are
1413 // supplied as additional entries in the map.
1414 TYPE_ENUM = 1<<16,
1415
1416 // Attribute holds a bitmaks of flags. The flag bit values are
1417 // supplied as additional entries in the map.
1418 TYPE_FLAGS = 1<<17
1419 };
1420
1421 // Enum of localization modes, for use with ATTR_L10N.
1422 enum {
1423 L10N_NOT_REQUIRED = 0,
1424 L10N_SUGGESTED = 1
1425 };
1426
1427 // This mapping's value.
1428 Res_value value;
1429};
1430
1431/**
Adam Lesinskide898ff2014-01-29 18:20:45 -08001432 * A package-id to package name mapping for any shared libraries used
1433 * in this resource table. The package-id's encoded in this resource
1434 * table may be different than the id's assigned at runtime. We must
1435 * be able to translate the package-id's based on the package name.
1436 */
1437struct ResTable_lib_header
1438{
1439 struct ResChunk_header header;
1440
1441 // The number of shared libraries linked in this resource table.
1442 uint32_t count;
1443};
1444
1445/**
1446 * A shared library package-id to package name entry.
1447 */
1448struct ResTable_lib_entry
1449{
1450 // The package-id this shared library was assigned at build time.
1451 // We use a uint32 to keep the structure aligned on a uint32 boundary.
1452 uint32_t packageId;
1453
1454 // The package name of the shared library. \0 terminated.
Adam Lesinski4bf58102014-11-03 11:21:19 -08001455 uint16_t packageName[128];
Adam Lesinskide898ff2014-01-29 18:20:45 -08001456};
1457
1458/**
1459 * Holds the shared library ID table. Shared libraries are assigned package IDs at
1460 * build time, but they may be loaded in a different order, so we need to maintain
1461 * a mapping of build-time package ID to run-time assigned package ID.
1462 *
1463 * Dynamic references are not currently supported in overlays. Only the base package
1464 * may have dynamic references.
1465 */
1466class DynamicRefTable
1467{
1468public:
1469 DynamicRefTable(uint8_t packageId);
1470
1471 // Loads an unmapped reference table from the package.
1472 status_t load(const ResTable_lib_header* const header);
1473
Adam Lesinski6022deb2014-08-20 14:59:19 -07001474 // Adds mappings from the other DynamicRefTable
1475 status_t addMappings(const DynamicRefTable& other);
1476
Adam Lesinskide898ff2014-01-29 18:20:45 -08001477 // Creates a mapping from build-time package ID to run-time package ID for
1478 // the given package.
1479 status_t addMapping(const String16& packageName, uint8_t packageId);
1480
1481 // Performs the actual conversion of build-time resource ID to run-time
1482 // resource ID.
1483 inline status_t lookupResourceId(uint32_t* resId) const;
1484 inline status_t lookupResourceValue(Res_value* value) const;
1485
1486 inline const KeyedVector<String16, uint8_t>& entries() const {
1487 return mEntries;
1488 }
1489
1490private:
1491 const uint8_t mAssignedPackageId;
1492 uint8_t mLookupTable[256];
1493 KeyedVector<String16, uint8_t> mEntries;
1494};
1495
1496/**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 * Convenience class for accessing data in a ResTable resource.
1498 */
1499class ResTable
1500{
1501public:
1502 ResTable();
Narayan Kamath7c4887f2014-01-27 17:32:37 +00001503 ResTable(const void* data, size_t size, const int32_t cookie,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 bool copyData=false);
1505 ~ResTable();
1506
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001507 status_t add(const void* data, size_t size, const int32_t cookie=-1, bool copyData=false);
1508 status_t add(const void* data, size_t size, const void* idmapData, size_t idmapDataSize,
1509 const int32_t cookie=-1, bool copyData=false);
1510
1511 status_t add(Asset* asset, const int32_t cookie=-1, bool copyData=false);
1512 status_t add(Asset* asset, Asset* idmapAsset, const int32_t cookie=-1, bool copyData=false);
1513
Dianne Hackborn78c40512009-07-06 11:07:40 -07001514 status_t add(ResTable* src);
Adam Lesinskide898ff2014-01-29 18:20:45 -08001515 status_t addEmpty(const int32_t cookie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516
1517 status_t getError() const;
1518
1519 void uninit();
1520
1521 struct resource_name
1522 {
1523 const char16_t* package;
1524 size_t packageLen;
1525 const char16_t* type;
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001526 const char* type8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 size_t typeLen;
1528 const char16_t* name;
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001529 const char* name8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 size_t nameLen;
1531 };
1532
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001533 bool getResourceName(uint32_t resID, bool allowUtf8, resource_name* outName) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534
Adam Lesinski82a2dd82014-09-17 18:34:15 -07001535 bool getResourceFlags(uint32_t resID, uint32_t* outFlags) const;
1536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 /**
1538 * Retrieve the value of a resource. If the resource is found, returns a
1539 * value >= 0 indicating the table it is in (for use with
1540 * getTableStringBlock() and getTableCookie()) and fills in 'outValue'. If
1541 * not found, returns a negative error code.
1542 *
1543 * Note that this function does not do reference traversal. If you want
1544 * to follow references to other resources to get the "real" value to
1545 * use, you need to call resolveReference() after this function.
1546 *
1547 * @param resID The desired resoruce identifier.
1548 * @param outValue Filled in with the resource data that was found.
1549 *
1550 * @return ssize_t Either a >= 0 table index or a negative error code.
1551 */
Kenny Root55fc8502010-10-28 14:47:01 -07001552 ssize_t getResource(uint32_t resID, Res_value* outValue, bool mayBeBag = false,
1553 uint16_t density = 0,
1554 uint32_t* outSpecFlags = NULL,
1555 ResTable_config* outConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556
1557 inline ssize_t getResource(const ResTable_ref& res, Res_value* outValue,
1558 uint32_t* outSpecFlags=NULL) const {
Kenny Root55fc8502010-10-28 14:47:01 -07001559 return getResource(res.ident, outValue, false, 0, outSpecFlags, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
1561
1562 ssize_t resolveReference(Res_value* inOutValue,
1563 ssize_t blockIndex,
1564 uint32_t* outLastRef = NULL,
Dianne Hackborn0d221012009-07-29 15:41:19 -07001565 uint32_t* inoutTypeSpecFlags = NULL,
1566 ResTable_config* outConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567
1568 enum {
1569 TMP_BUFFER_SIZE = 16
1570 };
1571 const char16_t* valueToString(const Res_value* value, size_t stringBlock,
1572 char16_t tmpBuffer[TMP_BUFFER_SIZE],
Adam Lesinskiad2d07d2014-08-27 16:21:08 -07001573 size_t* outLen) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574
1575 struct bag_entry {
1576 ssize_t stringBlock;
1577 ResTable_map map;
1578 };
1579
1580 /**
1581 * Retrieve the bag of a resource. If the resoruce is found, returns the
1582 * number of bags it contains and 'outBag' points to an array of their
1583 * values. If not found, a negative error code is returned.
1584 *
1585 * Note that this function -does- do reference traversal of the bag data.
1586 *
1587 * @param resID The desired resource identifier.
1588 * @param outBag Filled inm with a pointer to the bag mappings.
1589 *
1590 * @return ssize_t Either a >= 0 bag count of negative error code.
1591 */
1592 ssize_t lockBag(uint32_t resID, const bag_entry** outBag) const;
1593
1594 void unlockBag(const bag_entry* bag) const;
1595
1596 void lock() const;
1597
1598 ssize_t getBagLocked(uint32_t resID, const bag_entry** outBag,
1599 uint32_t* outTypeSpecFlags=NULL) const;
1600
1601 void unlock() const;
1602
1603 class Theme {
1604 public:
1605 Theme(const ResTable& table);
1606 ~Theme();
1607
1608 inline const ResTable& getResTable() const { return mTable; }
1609
1610 status_t applyStyle(uint32_t resID, bool force=false);
1611 status_t setTo(const Theme& other);
1612
1613 /**
1614 * Retrieve a value in the theme. If the theme defines this
1615 * value, returns a value >= 0 indicating the table it is in
1616 * (for use with getTableStringBlock() and getTableCookie) and
1617 * fills in 'outValue'. If not found, returns a negative error
1618 * code.
1619 *
1620 * Note that this function does not do reference traversal. If you want
1621 * to follow references to other resources to get the "real" value to
1622 * use, you need to call resolveReference() after this function.
1623 *
1624 * @param resID A resource identifier naming the desired theme
1625 * attribute.
1626 * @param outValue Filled in with the theme value that was
1627 * found.
1628 *
1629 * @return ssize_t Either a >= 0 table index or a negative error code.
1630 */
1631 ssize_t getAttribute(uint32_t resID, Res_value* outValue,
1632 uint32_t* outTypeSpecFlags = NULL) const;
1633
1634 /**
1635 * This is like ResTable::resolveReference(), but also takes
1636 * care of resolving attribute references to the theme.
1637 */
1638 ssize_t resolveAttributeReference(Res_value* inOutValue,
1639 ssize_t blockIndex, uint32_t* outLastRef = NULL,
Dianne Hackborn0d221012009-07-29 15:41:19 -07001640 uint32_t* inoutTypeSpecFlags = NULL,
1641 ResTable_config* inoutConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642
1643 void dumpToLog() const;
1644
1645 private:
1646 Theme(const Theme&);
1647 Theme& operator=(const Theme&);
1648
1649 struct theme_entry {
1650 ssize_t stringBlock;
1651 uint32_t typeSpecFlags;
1652 Res_value value;
1653 };
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 struct type_info {
1656 size_t numEntries;
1657 theme_entry* entries;
1658 };
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 struct package_info {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001661 type_info types[Res_MAXTYPE + 1];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 };
1663
1664 void free_package(package_info* pi);
1665 package_info* copy_package(package_info* pi);
1666
1667 const ResTable& mTable;
1668 package_info* mPackages[Res_MAXPACKAGE];
1669 };
1670
1671 void setParameters(const ResTable_config* params);
1672 void getParameters(ResTable_config* params) const;
1673
1674 // Retrieve an identifier (which can be passed to getResource)
1675 // for a given resource name. The 'name' can be fully qualified
1676 // (<package>:<type>.<basename>) or the package or type components
1677 // can be dropped if default values are supplied here.
1678 //
1679 // Returns 0 if no such resource was found, else a valid resource ID.
1680 uint32_t identifierForName(const char16_t* name, size_t nameLen,
1681 const char16_t* type = 0, size_t typeLen = 0,
1682 const char16_t* defPackage = 0,
1683 size_t defPackageLen = 0,
1684 uint32_t* outTypeSpecFlags = NULL) const;
1685
Adam Lesinski4bf58102014-11-03 11:21:19 -08001686 static bool expandResourceRef(const char16_t* refStr, size_t refLen,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 String16* outPackage,
1688 String16* outType,
1689 String16* outName,
1690 const String16* defType = NULL,
1691 const String16* defPackage = NULL,
Dianne Hackborn426431a2011-06-09 11:29:08 -07001692 const char** outErrorMsg = NULL,
1693 bool* outPublicOnly = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694
1695 static bool stringToInt(const char16_t* s, size_t len, Res_value* outValue);
1696 static bool stringToFloat(const char16_t* s, size_t len, Res_value* outValue);
1697
1698 // Used with stringToValue.
1699 class Accessor
1700 {
1701 public:
1702 inline virtual ~Accessor() { }
1703
Adam Lesinski833f3cc2014-06-18 15:06:01 -07001704 virtual const String16& getAssetsPackage() const = 0;
1705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 virtual uint32_t getCustomResource(const String16& package,
1707 const String16& type,
1708 const String16& name) const = 0;
1709 virtual uint32_t getCustomResourceWithCreation(const String16& package,
1710 const String16& type,
1711 const String16& name,
1712 const bool createIfNeeded = false) = 0;
1713 virtual uint32_t getRemappedPackage(uint32_t origPackage) const = 0;
1714 virtual bool getAttributeType(uint32_t attrID, uint32_t* outType) = 0;
1715 virtual bool getAttributeMin(uint32_t attrID, uint32_t* outMin) = 0;
1716 virtual bool getAttributeMax(uint32_t attrID, uint32_t* outMax) = 0;
1717 virtual bool getAttributeEnum(uint32_t attrID,
1718 const char16_t* name, size_t nameLen,
1719 Res_value* outValue) = 0;
1720 virtual bool getAttributeFlags(uint32_t attrID,
1721 const char16_t* name, size_t nameLen,
1722 Res_value* outValue) = 0;
1723 virtual uint32_t getAttributeL10N(uint32_t attrID) = 0;
1724 virtual bool getLocalizationSetting() = 0;
1725 virtual void reportError(void* accessorCookie, const char* fmt, ...) = 0;
1726 };
1727
1728 // Convert a string to a resource value. Handles standard "@res",
1729 // "#color", "123", and "0x1bd" types; performs escaping of strings.
1730 // The resulting value is placed in 'outValue'; if it is a string type,
1731 // 'outString' receives the string. If 'attrID' is supplied, the value is
1732 // type checked against this attribute and it is used to perform enum
1733 // evaluation. If 'acccessor' is supplied, it will be used to attempt to
1734 // resolve resources that do not exist in this ResTable. If 'attrType' is
1735 // supplied, the value will be type checked for this format if 'attrID'
1736 // is not supplied or found.
1737 bool stringToValue(Res_value* outValue, String16* outString,
1738 const char16_t* s, size_t len,
1739 bool preserveSpaces, bool coerceType,
1740 uint32_t attrID = 0,
1741 const String16* defType = NULL,
1742 const String16* defPackage = NULL,
1743 Accessor* accessor = NULL,
1744 void* accessorCookie = NULL,
1745 uint32_t attrType = ResTable_map::TYPE_ANY,
1746 bool enforcePrivate = true) const;
1747
1748 // Perform processing of escapes and quotes in a string.
1749 static bool collectString(String16* outString,
1750 const char16_t* s, size_t len,
1751 bool preserveSpaces,
1752 const char** outErrorMsg = NULL,
1753 bool append = false);
1754
1755 size_t getBasePackageCount() const;
Adam Lesinskide898ff2014-01-29 18:20:45 -08001756 const String16 getBasePackageName(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 uint32_t getBasePackageId(size_t idx) const;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001758 uint32_t getLastTypeIdForPackage(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001760 // Return the number of resource tables that the object contains.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 size_t getTableCount() const;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001762 // Return the values string pool for the resource table at the given
1763 // index. This string pool contains all of the strings for values
1764 // contained in the resource table -- that is the item values themselves,
1765 // but not the names their entries or types.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 const ResStringPool* getTableStringBlock(size_t index) const;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001767 // Return unique cookie identifier for the given resource table.
Narayan Kamath7c4887f2014-01-27 17:32:37 +00001768 int32_t getTableCookie(size_t index) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769
Adam Lesinskide898ff2014-01-29 18:20:45 -08001770 const DynamicRefTable* getDynamicRefTableForCookie(int32_t cookie) const;
1771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 // Return the configurations (ResTable_config) that we know about
1773 void getConfigurations(Vector<ResTable_config>* configs) const;
1774
1775 void getLocales(Vector<String8>* locales) const;
1776
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001777 // Generate an idmap.
1778 //
1779 // Return value: on success: NO_ERROR; caller is responsible for free-ing
1780 // outData (using free(3)). On failure, any status_t value other than
1781 // NO_ERROR; the caller should not free outData.
Mårten Kongstad65a05fd2014-01-31 14:01:52 +01001782 status_t createIdmap(const ResTable& overlay,
1783 uint32_t targetCrc, uint32_t overlayCrc,
1784 const char* targetPath, const char* overlayPath,
Dianne Hackborn4385d372014-02-11 13:56:21 -08001785 void** outData, size_t* outSize) const;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +01001786
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001787 enum {
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001788 IDMAP_HEADER_SIZE_BYTES = 4 * sizeof(uint32_t) + 2 * 256,
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001789 };
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001790
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001791 // Retrieve idmap meta-data.
1792 //
1793 // This function only requires the idmap header (the first
1794 // IDMAP_HEADER_SIZE_BYTES) bytes of an idmap file.
1795 static bool getIdmapInfo(const void* idmap, size_t size,
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001796 uint32_t* pVersion,
Mårten Kongstad65a05fd2014-01-31 14:01:52 +01001797 uint32_t* pTargetCrc, uint32_t* pOverlayCrc,
Mårten Kongstad48d22322014-01-31 14:43:27 +01001798 String8* pTargetPath, String8* pOverlayPath);
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001799
Dianne Hackborne17086b2009-06-19 15:13:28 -07001800 void print(bool inclValues) const;
Shachar Shemesh9872bf42010-12-20 17:38:33 +02001801 static String8 normalizeForOutput(const char* input);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802
1803private:
1804 struct Header;
1805 struct Type;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001806 struct Entry;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 struct Package;
1808 struct PackageGroup;
1809 struct bag_set;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001810 typedef Vector<Type*> TypeList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001812 status_t addInternal(const void* data, size_t size, const void* idmapData, size_t idmapDataSize,
1813 const int32_t cookie, bool copyData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814
1815 ssize_t getResourcePackageIndex(uint32_t resID) const;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001816
1817 status_t getEntry(
1818 const PackageGroup* packageGroup, int typeIndex, int entryIndex,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 const ResTable_config* config,
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001820 Entry* outEntry) const;
1821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 status_t parsePackage(
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -07001823 const ResTable_package* const pkg, const Header* const header);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824
Dianne Hackbornde7faf62009-06-30 13:27:30 -07001825 void print_value(const Package* pkg, const Res_value& value) const;
1826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 mutable Mutex mLock;
1828
1829 status_t mError;
1830
1831 ResTable_config mParams;
1832
1833 // Array of all resource tables.
1834 Vector<Header*> mHeaders;
1835
1836 // Array of packages in all resource tables.
1837 Vector<PackageGroup*> mPackageGroups;
1838
1839 // Mapping from resource package IDs to indices into the internal
1840 // package array.
1841 uint8_t mPackageMap[256];
Adam Lesinskide898ff2014-01-29 18:20:45 -08001842
1843 uint8_t mNextPackageId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844};
1845
1846} // namespace android
1847
1848#endif // _LIBS_UTILS_RESOURCE_TYPES_H