blob: 7779f7f2241bc765551573994c57ac76922dde67 [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>
28
29#include <utils/threads.h>
30
31#include <stdint.h>
32#include <sys/types.h>
33
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -070034#include <android/configuration.h>
35
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036namespace android {
37
38/** ********************************************************************
39 * PNG Extensions
40 *
41 * New private chunks that may be placed in PNG images.
42 *
43 *********************************************************************** */
44
45/**
46 * This chunk specifies how to split an image into segments for
47 * scaling.
48 *
49 * There are J horizontal and K vertical segments. These segments divide
50 * the image into J*K regions as follows (where J=4 and K=3):
51 *
52 * F0 S0 F1 S1
53 * +-----+----+------+-------+
54 * S2| 0 | 1 | 2 | 3 |
55 * +-----+----+------+-------+
56 * | | | | |
57 * | | | | |
58 * F2| 4 | 5 | 6 | 7 |
59 * | | | | |
60 * | | | | |
61 * +-----+----+------+-------+
62 * S3| 8 | 9 | 10 | 11 |
63 * +-----+----+------+-------+
64 *
65 * Each horizontal and vertical segment is considered to by either
66 * stretchable (marked by the Sx labels) or fixed (marked by the Fy
67 * labels), in the horizontal or vertical axis, respectively. In the
68 * above example, the first is horizontal segment (F0) is fixed, the
69 * next is stretchable and then they continue to alternate. Note that
70 * the segment list for each axis can begin or end with a stretchable
71 * or fixed segment.
72 *
73 * The relative sizes of the stretchy segments indicates the relative
74 * amount of stretchiness of the regions bordered by the segments. For
75 * example, regions 3, 7 and 11 above will take up more horizontal space
Mathias Agopian5f910972009-06-22 02:35:32 -070076 * than regions 1, 5 and 9 since the horizontal segment associated with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 * the first set of regions is larger than the other set of regions. The
78 * ratios of the amount of horizontal (or vertical) space taken by any
79 * two stretchable slices is exactly the ratio of their corresponding
80 * segment lengths.
81 *
82 * xDivs and yDivs point to arrays of horizontal and vertical pixel
83 * indices. The first pair of Divs (in either array) indicate the
84 * starting and ending points of the first stretchable segment in that
85 * axis. The next pair specifies the next stretchable segment, etc. So
86 * in the above example xDiv[0] and xDiv[1] specify the horizontal
87 * coordinates for the regions labeled 1, 5 and 9. xDiv[2] and
88 * xDiv[3] specify the coordinates for regions 3, 7 and 11. Note that
89 * the leftmost slices always start at x=0 and the rightmost slices
90 * always end at the end of the image. So, for example, the regions 0,
91 * 4 and 8 (which are fixed along the X axis) start at x value 0 and
Mathias Agopian5f910972009-06-22 02:35:32 -070092 * 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 -080093 * xDiv[2].
94 *
95 * The array pointed to by the colors field lists contains hints for
96 * each of the regions. They are ordered according left-to-right and
97 * top-to-bottom as indicated above. For each segment that is a solid
98 * color the array entry will contain that color value; otherwise it
99 * will contain NO_COLOR. Segments that are completely transparent
100 * will always have the value TRANSPARENT_COLOR.
101 *
102 * The PNG chunk type is "npTc".
103 */
104struct Res_png_9patch
105{
106 Res_png_9patch() : wasDeserialized(false), xDivs(NULL),
107 yDivs(NULL), colors(NULL) { }
108
109 int8_t wasDeserialized;
110 int8_t numXDivs;
111 int8_t numYDivs;
112 int8_t numColors;
113
114 // These tell where the next section of a patch starts.
115 // For example, the first patch includes the pixels from
116 // 0 to xDivs[0]-1 and the second patch includes the pixels
117 // from xDivs[0] to xDivs[1]-1.
118 // Note: allocation/free of these pointers is left to the caller.
119 int32_t* xDivs;
120 int32_t* yDivs;
121
122 int32_t paddingLeft, paddingRight;
123 int32_t paddingTop, paddingBottom;
124
125 enum {
126 // The 9 patch segment is not a solid color.
127 NO_COLOR = 0x00000001,
128
129 // The 9 patch segment is completely transparent.
130 TRANSPARENT_COLOR = 0x00000000
131 };
132 // Note: allocation/free of this pointer is left to the caller.
133 uint32_t* colors;
134
135 // Convert data from device representation to PNG file representation.
136 void deviceToFile();
137 // Convert data from PNG file representation to device representation.
138 void fileToDevice();
139 // Serialize/Marshall the patch data into a newly malloc-ed block
140 void* serialize();
141 // Serialize/Marshall the patch data
142 void serialize(void* outData);
143 // Deserialize/Unmarshall the patch data
144 static Res_png_9patch* deserialize(const void* data);
145 // Compute the size of the serialized data structure
146 size_t serializedSize();
147};
148
149/** ********************************************************************
150 * Base Types
151 *
152 * These are standard types that are shared between multiple specific
153 * resource types.
154 *
155 *********************************************************************** */
156
157/**
158 * Header that appears at the front of every data chunk in a resource.
159 */
160struct ResChunk_header
161{
162 // Type identifier for this chunk. The meaning of this value depends
163 // on the containing chunk.
164 uint16_t type;
165
166 // Size of the chunk header (in bytes). Adding this value to
167 // the address of the chunk allows you to find its associated data
168 // (if any).
169 uint16_t headerSize;
170
171 // Total size of this chunk (in bytes). This is the chunkSize plus
172 // the size of any data associated with the chunk. Adding this value
173 // to the chunk allows you to completely skip its contents (including
174 // any child chunks). If this value is the same as chunkSize, there is
175 // no data associated with the chunk.
176 uint32_t size;
177};
178
179enum {
180 RES_NULL_TYPE = 0x0000,
181 RES_STRING_POOL_TYPE = 0x0001,
182 RES_TABLE_TYPE = 0x0002,
183 RES_XML_TYPE = 0x0003,
184
185 // Chunk types in RES_XML_TYPE
186 RES_XML_FIRST_CHUNK_TYPE = 0x0100,
187 RES_XML_START_NAMESPACE_TYPE= 0x0100,
188 RES_XML_END_NAMESPACE_TYPE = 0x0101,
189 RES_XML_START_ELEMENT_TYPE = 0x0102,
190 RES_XML_END_ELEMENT_TYPE = 0x0103,
191 RES_XML_CDATA_TYPE = 0x0104,
192 RES_XML_LAST_CHUNK_TYPE = 0x017f,
193 // This contains a uint32_t array mapping strings in the string
194 // pool back to resource identifiers. It is optional.
195 RES_XML_RESOURCE_MAP_TYPE = 0x0180,
196
197 // Chunk types in RES_TABLE_TYPE
198 RES_TABLE_PACKAGE_TYPE = 0x0200,
199 RES_TABLE_TYPE_TYPE = 0x0201,
200 RES_TABLE_TYPE_SPEC_TYPE = 0x0202
201};
202
203/**
204 * Macros for building/splitting resource identifiers.
205 */
206#define Res_VALIDID(resid) (resid != 0)
207#define Res_CHECKID(resid) ((resid&0xFFFF0000) != 0)
208#define Res_MAKEID(package, type, entry) \
209 (((package+1)<<24) | (((type+1)&0xFF)<<16) | (entry&0xFFFF))
210#define Res_GETPACKAGE(id) ((id>>24)-1)
211#define Res_GETTYPE(id) (((id>>16)&0xFF)-1)
212#define Res_GETENTRY(id) (id&0xFFFF)
213
214#define Res_INTERNALID(resid) ((resid&0xFFFF0000) != 0 && (resid&0xFF0000) == 0)
215#define Res_MAKEINTERNAL(entry) (0x01000000 | (entry&0xFFFF))
216#define Res_MAKEARRAY(entry) (0x02000000 | (entry&0xFFFF))
217
218#define Res_MAXPACKAGE 255
219
220/**
221 * Representation of a value in a resource, supplying type
222 * information.
223 */
224struct Res_value
225{
226 // Number of bytes in this structure.
227 uint16_t size;
228
229 // Always set to 0.
230 uint8_t res0;
231
232 // Type of the data value.
233 enum {
234 // Contains no data.
235 TYPE_NULL = 0x00,
236 // The 'data' holds a ResTable_ref, a reference to another resource
237 // table entry.
238 TYPE_REFERENCE = 0x01,
239 // The 'data' holds an attribute resource identifier.
240 TYPE_ATTRIBUTE = 0x02,
241 // The 'data' holds an index into the containing resource table's
242 // global value string pool.
243 TYPE_STRING = 0x03,
244 // The 'data' holds a single-precision floating point number.
245 TYPE_FLOAT = 0x04,
246 // The 'data' holds a complex number encoding a dimension value,
247 // such as "100in".
248 TYPE_DIMENSION = 0x05,
249 // The 'data' holds a complex number encoding a fraction of a
250 // container.
251 TYPE_FRACTION = 0x06,
252
253 // Beginning of integer flavors...
254 TYPE_FIRST_INT = 0x10,
255
256 // The 'data' is a raw integer value of the form n..n.
257 TYPE_INT_DEC = 0x10,
258 // The 'data' is a raw integer value of the form 0xn..n.
259 TYPE_INT_HEX = 0x11,
260 // The 'data' is either 0 or 1, for input "false" or "true" respectively.
261 TYPE_INT_BOOLEAN = 0x12,
262
263 // Beginning of color integer flavors...
264 TYPE_FIRST_COLOR_INT = 0x1c,
265
266 // The 'data' is a raw integer value of the form #aarrggbb.
267 TYPE_INT_COLOR_ARGB8 = 0x1c,
268 // The 'data' is a raw integer value of the form #rrggbb.
269 TYPE_INT_COLOR_RGB8 = 0x1d,
270 // The 'data' is a raw integer value of the form #argb.
271 TYPE_INT_COLOR_ARGB4 = 0x1e,
272 // The 'data' is a raw integer value of the form #rgb.
273 TYPE_INT_COLOR_RGB4 = 0x1f,
274
275 // ...end of integer flavors.
276 TYPE_LAST_COLOR_INT = 0x1f,
277
278 // ...end of integer flavors.
279 TYPE_LAST_INT = 0x1f
280 };
281 uint8_t dataType;
282
283 // Structure of complex data values (TYPE_UNIT and TYPE_FRACTION)
284 enum {
285 // Where the unit type information is. This gives us 16 possible
286 // types, as defined below.
287 COMPLEX_UNIT_SHIFT = 0,
288 COMPLEX_UNIT_MASK = 0xf,
289
290 // TYPE_DIMENSION: Value is raw pixels.
291 COMPLEX_UNIT_PX = 0,
292 // TYPE_DIMENSION: Value is Device Independent Pixels.
293 COMPLEX_UNIT_DIP = 1,
294 // TYPE_DIMENSION: Value is a Scaled device independent Pixels.
295 COMPLEX_UNIT_SP = 2,
296 // TYPE_DIMENSION: Value is in points.
297 COMPLEX_UNIT_PT = 3,
298 // TYPE_DIMENSION: Value is in inches.
299 COMPLEX_UNIT_IN = 4,
300 // TYPE_DIMENSION: Value is in millimeters.
301 COMPLEX_UNIT_MM = 5,
302
303 // TYPE_FRACTION: A basic fraction of the overall size.
304 COMPLEX_UNIT_FRACTION = 0,
305 // TYPE_FRACTION: A fraction of the parent size.
306 COMPLEX_UNIT_FRACTION_PARENT = 1,
307
308 // Where the radix information is, telling where the decimal place
309 // appears in the mantissa. This give us 4 possible fixed point
310 // representations as defined below.
311 COMPLEX_RADIX_SHIFT = 4,
312 COMPLEX_RADIX_MASK = 0x3,
313
314 // The mantissa is an integral number -- i.e., 0xnnnnnn.0
315 COMPLEX_RADIX_23p0 = 0,
316 // The mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn
317 COMPLEX_RADIX_16p7 = 1,
318 // The mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn
319 COMPLEX_RADIX_8p15 = 2,
320 // The mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn
321 COMPLEX_RADIX_0p23 = 3,
322
323 // Where the actual value is. This gives us 23 bits of
324 // precision. The top bit is the sign.
325 COMPLEX_MANTISSA_SHIFT = 8,
326 COMPLEX_MANTISSA_MASK = 0xffffff
327 };
328
329 // The data for this item, as interpreted according to dataType.
330 uint32_t data;
331
332 void copyFrom_dtoh(const Res_value& src);
333};
334
335/**
336 * This is a reference to a unique entry (a ResTable_entry structure)
337 * in a resource table. The value is structured as: 0xpptteeee,
338 * where pp is the package index, tt is the type index in that
339 * package, and eeee is the entry index in that type. The package
340 * and type values start at 1 for the first item, to help catch cases
341 * where they have not been supplied.
342 */
343struct ResTable_ref
344{
345 uint32_t ident;
346};
347
348/**
349 * Reference to a string in a string pool.
350 */
351struct ResStringPool_ref
352{
353 // Index into the string pool table (uint32_t-offset from the indices
354 // immediately after ResStringPool_header) at which to find the location
355 // of the string data in the pool.
356 uint32_t index;
357};
358
359/** ********************************************************************
360 * String Pool
361 *
362 * A set of strings that can be references by others through a
363 * ResStringPool_ref.
364 *
365 *********************************************************************** */
366
367/**
368 * Definition for a pool of strings. The data of this chunk is an
369 * array of uint32_t providing indices into the pool, relative to
370 * stringsStart. At stringsStart are all of the UTF-16 strings
371 * concatenated together; each starts with a uint16_t of the string's
372 * length and each ends with a 0x0000 terminator. If a string is >
373 * 32767 characters, the high bit of the length is set meaning to take
374 * those 15 bits as a high word and it will be followed by another
375 * uint16_t containing the low word.
376 *
377 * If styleCount is not zero, then immediately following the array of
378 * uint32_t indices into the string table is another array of indices
379 * into a style table starting at stylesStart. Each entry in the
380 * style table is an array of ResStringPool_span structures.
381 */
382struct ResStringPool_header
383{
384 struct ResChunk_header header;
385
386 // Number of strings in this pool (number of uint32_t indices that follow
387 // in the data).
388 uint32_t stringCount;
389
390 // Number of style span arrays in the pool (number of uint32_t indices
391 // follow the string indices).
392 uint32_t styleCount;
393
394 // Flags.
395 enum {
396 // If set, the string index is sorted by the string values (based
397 // on strcmp16()).
Kenny Root19138462009-12-04 09:38:48 -0800398 SORTED_FLAG = 1<<0,
399
400 // String pool is encoded in UTF-8
401 UTF8_FLAG = 1<<8
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 };
403 uint32_t flags;
404
405 // Index from header of the string data.
406 uint32_t stringsStart;
407
408 // Index from header of the style data.
409 uint32_t stylesStart;
410};
411
412/**
413 * This structure defines a span of style information associated with
414 * a string in the pool.
415 */
416struct ResStringPool_span
417{
418 enum {
419 END = 0xFFFFFFFF
420 };
421
422 // This is the name of the span -- that is, the name of the XML
423 // tag that defined it. The special value END (0xFFFFFFFF) indicates
424 // the end of an array of spans.
425 ResStringPool_ref name;
426
427 // The range of characters in the string that this span applies to.
428 uint32_t firstChar, lastChar;
429};
430
431/**
432 * Convenience class for accessing data in a ResStringPool resource.
433 */
434class ResStringPool
435{
436public:
437 ResStringPool();
438 ResStringPool(const void* data, size_t size, bool copyData=false);
439 ~ResStringPool();
440
441 status_t setTo(const void* data, size_t size, bool copyData=false);
442
443 status_t getError() const;
444
445 void uninit();
446
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800447 // Return string entry as UTF16; if the pool is UTF8, the string will
448 // be converted before returning.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 inline const char16_t* stringAt(const ResStringPool_ref& ref, size_t* outLen) const {
450 return stringAt(ref.index, outLen);
451 }
452 const char16_t* stringAt(size_t idx, size_t* outLen) const;
453
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800454 // Note: returns null if the string pool is not UTF8.
Kenny Root780d2a12010-02-22 22:36:26 -0800455 const char* string8At(size_t idx, size_t* outLen) const;
456
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800457 // Return string whether the pool is UTF8 or UTF16. Does not allow you
458 // to distinguish null.
459 const String8 string8ObjectAt(size_t idx) const;
460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 const ResStringPool_span* styleAt(const ResStringPool_ref& ref) const;
462 const ResStringPool_span* styleAt(size_t idx) const;
463
464 ssize_t indexOfString(const char16_t* str, size_t strLen) const;
465
466 size_t size() const;
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800467 size_t styleCount() const;
468 size_t bytes() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800470 bool isSorted() const;
Kenny Rootbb79f642009-12-10 14:20:15 -0800471 bool isUTF8() const;
Kenny Rootbb79f642009-12-10 14:20:15 -0800472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473private:
474 status_t mError;
475 void* mOwnedData;
476 const ResStringPool_header* mHeader;
477 size_t mSize;
Kenny Root19138462009-12-04 09:38:48 -0800478 mutable Mutex mDecodeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 const uint32_t* mEntries;
480 const uint32_t* mEntryStyles;
Kenny Root19138462009-12-04 09:38:48 -0800481 const void* mStrings;
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700482 char16_t mutable** mCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 uint32_t mStringPoolSize; // number of uint16_t
484 const uint32_t* mStyles;
485 uint32_t mStylePoolSize; // number of uint32_t
486};
487
488/** ********************************************************************
489 * XML Tree
490 *
491 * Binary representation of an XML document. This is designed to
492 * express everything in an XML document, in a form that is much
493 * easier to parse on the device.
494 *
495 *********************************************************************** */
496
497/**
498 * XML tree header. This appears at the front of an XML tree,
499 * describing its content. It is followed by a flat array of
500 * ResXMLTree_node structures; the hierarchy of the XML document
501 * is described by the occurrance of RES_XML_START_ELEMENT_TYPE
502 * and corresponding RES_XML_END_ELEMENT_TYPE nodes in the array.
503 */
504struct ResXMLTree_header
505{
506 struct ResChunk_header header;
507};
508
509/**
510 * Basic XML tree node. A single item in the XML document. Extended info
511 * about the node can be found after header.headerSize.
512 */
513struct ResXMLTree_node
514{
515 struct ResChunk_header header;
516
517 // Line number in original source file at which this element appeared.
518 uint32_t lineNumber;
519
520 // Optional XML comment that was associated with this element; -1 if none.
521 struct ResStringPool_ref comment;
522};
523
524/**
525 * Extended XML tree node for CDATA tags -- includes the CDATA string.
526 * Appears header.headerSize bytes after a ResXMLTree_node.
527 */
528struct ResXMLTree_cdataExt
529{
530 // The raw CDATA character data.
531 struct ResStringPool_ref data;
532
533 // The typed value of the character data if this is a CDATA node.
534 struct Res_value typedData;
535};
536
537/**
538 * Extended XML tree node for namespace start/end nodes.
539 * Appears header.headerSize bytes after a ResXMLTree_node.
540 */
541struct ResXMLTree_namespaceExt
542{
543 // The prefix of the namespace.
544 struct ResStringPool_ref prefix;
545
546 // The URI of the namespace.
547 struct ResStringPool_ref uri;
548};
549
550/**
551 * Extended XML tree node for element start/end nodes.
552 * Appears header.headerSize bytes after a ResXMLTree_node.
553 */
554struct ResXMLTree_endElementExt
555{
556 // String of the full namespace of this element.
557 struct ResStringPool_ref ns;
558
559 // String name of this node if it is an ELEMENT; the raw
560 // character data if this is a CDATA node.
561 struct ResStringPool_ref name;
562};
563
564/**
565 * Extended XML tree node for start tags -- includes attribute
566 * information.
567 * Appears header.headerSize bytes after a ResXMLTree_node.
568 */
569struct ResXMLTree_attrExt
570{
571 // String of the full namespace of this element.
572 struct ResStringPool_ref ns;
573
574 // String name of this node if it is an ELEMENT; the raw
575 // character data if this is a CDATA node.
576 struct ResStringPool_ref name;
577
578 // Byte offset from the start of this structure where the attributes start.
579 uint16_t attributeStart;
580
581 // Size of the ResXMLTree_attribute structures that follow.
582 uint16_t attributeSize;
583
584 // Number of attributes associated with an ELEMENT. These are
585 // available as an array of ResXMLTree_attribute structures
586 // immediately following this node.
587 uint16_t attributeCount;
588
589 // Index (1-based) of the "id" attribute. 0 if none.
590 uint16_t idIndex;
591
592 // Index (1-based) of the "class" attribute. 0 if none.
593 uint16_t classIndex;
594
595 // Index (1-based) of the "style" attribute. 0 if none.
596 uint16_t styleIndex;
597};
598
599struct ResXMLTree_attribute
600{
601 // Namespace of this attribute.
602 struct ResStringPool_ref ns;
603
604 // Name of this attribute.
605 struct ResStringPool_ref name;
606
607 // The original raw string value of this attribute.
608 struct ResStringPool_ref rawValue;
609
610 // Processesd typed value of this attribute.
611 struct Res_value typedValue;
612};
613
614class ResXMLTree;
615
616class ResXMLParser
617{
618public:
619 ResXMLParser(const ResXMLTree& tree);
620
621 enum event_code_t {
622 BAD_DOCUMENT = -1,
623 START_DOCUMENT = 0,
624 END_DOCUMENT = 1,
625
626 FIRST_CHUNK_CODE = RES_XML_FIRST_CHUNK_TYPE,
627
628 START_NAMESPACE = RES_XML_START_NAMESPACE_TYPE,
629 END_NAMESPACE = RES_XML_END_NAMESPACE_TYPE,
630 START_TAG = RES_XML_START_ELEMENT_TYPE,
631 END_TAG = RES_XML_END_ELEMENT_TYPE,
632 TEXT = RES_XML_CDATA_TYPE
633 };
634
635 struct ResXMLPosition
636 {
637 event_code_t eventCode;
638 const ResXMLTree_node* curNode;
639 const void* curExt;
640 };
641
642 void restart();
643
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800644 const ResStringPool& getStrings() const;
645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 event_code_t getEventType() const;
647 // Note, unlike XmlPullParser, the first call to next() will return
648 // START_TAG of the first element.
649 event_code_t next();
650
651 // These are available for all nodes:
Mathias Agopian5f910972009-06-22 02:35:32 -0700652 int32_t getCommentID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 const uint16_t* getComment(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700654 uint32_t getLineNumber() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655
656 // This is available for TEXT:
Mathias Agopian5f910972009-06-22 02:35:32 -0700657 int32_t getTextID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 const uint16_t* getText(size_t* outLen) const;
659 ssize_t getTextValue(Res_value* outValue) const;
660
661 // These are available for START_NAMESPACE and END_NAMESPACE:
Mathias Agopian5f910972009-06-22 02:35:32 -0700662 int32_t getNamespacePrefixID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 const uint16_t* getNamespacePrefix(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700664 int32_t getNamespaceUriID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 const uint16_t* getNamespaceUri(size_t* outLen) const;
666
667 // These are available for START_TAG and END_TAG:
Mathias Agopian5f910972009-06-22 02:35:32 -0700668 int32_t getElementNamespaceID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 const uint16_t* getElementNamespace(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700670 int32_t getElementNameID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 const uint16_t* getElementName(size_t* outLen) const;
672
673 // Remaining methods are for retrieving information about attributes
674 // associated with a START_TAG:
675
676 size_t getAttributeCount() const;
677
678 // Returns -1 if no namespace, -2 if idx out of range.
Mathias Agopian5f910972009-06-22 02:35:32 -0700679 int32_t getAttributeNamespaceID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 const uint16_t* getAttributeNamespace(size_t idx, size_t* outLen) const;
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700681
Mathias Agopian5f910972009-06-22 02:35:32 -0700682 int32_t getAttributeNameID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 const uint16_t* getAttributeName(size_t idx, size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700684 uint32_t getAttributeNameResID(size_t idx) const;
Dianne Hackbornd45c68d2013-07-31 12:14:24 -0700685
686 // These will work only if the underlying string pool is UTF-8.
687 const char* getAttributeNamespace8(size_t idx, size_t* outLen) const;
688 const char* getAttributeName8(size_t idx, size_t* outLen) const;
689
Mathias Agopian5f910972009-06-22 02:35:32 -0700690 int32_t getAttributeValueStringID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 const uint16_t* getAttributeStringValue(size_t idx, size_t* outLen) const;
692
693 int32_t getAttributeDataType(size_t idx) const;
694 int32_t getAttributeData(size_t idx) const;
695 ssize_t getAttributeValue(size_t idx, Res_value* outValue) const;
696
697 ssize_t indexOfAttribute(const char* ns, const char* attr) const;
698 ssize_t indexOfAttribute(const char16_t* ns, size_t nsLen,
699 const char16_t* attr, size_t attrLen) const;
700
701 ssize_t indexOfID() const;
702 ssize_t indexOfClass() const;
703 ssize_t indexOfStyle() const;
704
705 void getPosition(ResXMLPosition* pos) const;
706 void setPosition(const ResXMLPosition& pos);
707
708private:
709 friend class ResXMLTree;
710
711 event_code_t nextNode();
712
713 const ResXMLTree& mTree;
714 event_code_t mEventCode;
715 const ResXMLTree_node* mCurNode;
716 const void* mCurExt;
717};
718
719/**
720 * Convenience class for accessing data in a ResXMLTree resource.
721 */
722class ResXMLTree : public ResXMLParser
723{
724public:
725 ResXMLTree();
726 ResXMLTree(const void* data, size_t size, bool copyData=false);
727 ~ResXMLTree();
728
729 status_t setTo(const void* data, size_t size, bool copyData=false);
730
731 status_t getError() const;
732
733 void uninit();
734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735private:
736 friend class ResXMLParser;
737
738 status_t validateNode(const ResXMLTree_node* node) const;
739
740 status_t mError;
741 void* mOwnedData;
742 const ResXMLTree_header* mHeader;
743 size_t mSize;
744 const uint8_t* mDataEnd;
745 ResStringPool mStrings;
746 const uint32_t* mResIds;
747 size_t mNumResIds;
748 const ResXMLTree_node* mRootNode;
749 const void* mRootExt;
750 event_code_t mRootCode;
751};
752
753/** ********************************************************************
754 * RESOURCE TABLE
755 *
756 *********************************************************************** */
757
758/**
759 * Header for a resource table. Its data contains a series of
760 * additional chunks:
Dianne Hackborn6c997a92012-01-31 11:27:43 -0800761 * * A ResStringPool_header containing all table values. This string pool
762 * contains all of the string values in the entire resource table (not
763 * the names of entries or type identifiers however).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 * * One or more ResTable_package chunks.
765 *
766 * Specific entries within a resource table can be uniquely identified
767 * with a single integer as defined by the ResTable_ref structure.
768 */
769struct ResTable_header
770{
771 struct ResChunk_header header;
772
773 // The number of ResTable_package structures.
774 uint32_t packageCount;
775};
776
777/**
778 * A collection of resource data types within a package. Followed by
779 * one or more ResTable_type and ResTable_typeSpec structures containing the
780 * entry values for each resource type.
781 */
782struct ResTable_package
783{
784 struct ResChunk_header header;
785
786 // If this is a base package, its ID. Package IDs start
787 // at 1 (corresponding to the value of the package bits in a
788 // resource identifier). 0 means this is not a base package.
789 uint32_t id;
790
791 // Actual name of this package, \0-terminated.
792 char16_t name[128];
793
794 // Offset to a ResStringPool_header defining the resource
795 // type symbol table. If zero, this package is inheriting from
796 // another base package (overriding specific values in it).
797 uint32_t typeStrings;
798
799 // Last index into typeStrings that is for public use by others.
800 uint32_t lastPublicType;
801
802 // Offset to a ResStringPool_header defining the resource
803 // key symbol table. If zero, this package is inheriting from
804 // another base package (overriding specific values in it).
805 uint32_t keyStrings;
806
807 // Last index into keyStrings that is for public use by others.
808 uint32_t lastPublicKey;
809};
810
811/**
812 * Describes a particular resource configuration.
813 */
814struct ResTable_config
815{
816 // Number of bytes in this structure.
817 uint32_t size;
818
819 union {
820 struct {
821 // Mobile country code (from SIM). 0 means "any".
822 uint16_t mcc;
823 // Mobile network code (from SIM). 0 means "any".
824 uint16_t mnc;
825 };
826 uint32_t imsi;
827 };
828
829 union {
830 struct {
831 // \0\0 means "any". Otherwise, en, fr, etc.
832 char language[2];
833
834 // \0\0 means "any". Otherwise, US, CA, etc.
835 char country[2];
836 };
837 uint32_t locale;
838 };
839
840 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700841 ORIENTATION_ANY = ACONFIGURATION_ORIENTATION_ANY,
842 ORIENTATION_PORT = ACONFIGURATION_ORIENTATION_PORT,
843 ORIENTATION_LAND = ACONFIGURATION_ORIENTATION_LAND,
844 ORIENTATION_SQUARE = ACONFIGURATION_ORIENTATION_SQUARE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 };
846
847 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700848 TOUCHSCREEN_ANY = ACONFIGURATION_TOUCHSCREEN_ANY,
849 TOUCHSCREEN_NOTOUCH = ACONFIGURATION_TOUCHSCREEN_NOTOUCH,
850 TOUCHSCREEN_STYLUS = ACONFIGURATION_TOUCHSCREEN_STYLUS,
851 TOUCHSCREEN_FINGER = ACONFIGURATION_TOUCHSCREEN_FINGER,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 };
853
854 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700855 DENSITY_DEFAULT = ACONFIGURATION_DENSITY_DEFAULT,
856 DENSITY_LOW = ACONFIGURATION_DENSITY_LOW,
857 DENSITY_MEDIUM = ACONFIGURATION_DENSITY_MEDIUM,
Dianne Hackbornb96cbbd2011-05-27 13:40:26 -0700858 DENSITY_TV = ACONFIGURATION_DENSITY_TV,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700859 DENSITY_HIGH = ACONFIGURATION_DENSITY_HIGH,
Dianne Hackbornd96e3df2012-01-25 15:12:23 -0800860 DENSITY_XHIGH = ACONFIGURATION_DENSITY_XHIGH,
861 DENSITY_XXHIGH = ACONFIGURATION_DENSITY_XXHIGH,
Dianne Hackborn56a23012013-02-12 15:41:49 -0800862 DENSITY_XXXHIGH = ACONFIGURATION_DENSITY_XXXHIGH,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700863 DENSITY_NONE = ACONFIGURATION_DENSITY_NONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 };
865
866 union {
867 struct {
868 uint8_t orientation;
869 uint8_t touchscreen;
870 uint16_t density;
871 };
872 uint32_t screenType;
873 };
874
875 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700876 KEYBOARD_ANY = ACONFIGURATION_KEYBOARD_ANY,
877 KEYBOARD_NOKEYS = ACONFIGURATION_KEYBOARD_NOKEYS,
878 KEYBOARD_QWERTY = ACONFIGURATION_KEYBOARD_QWERTY,
879 KEYBOARD_12KEY = ACONFIGURATION_KEYBOARD_12KEY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 };
881
882 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700883 NAVIGATION_ANY = ACONFIGURATION_NAVIGATION_ANY,
884 NAVIGATION_NONAV = ACONFIGURATION_NAVIGATION_NONAV,
885 NAVIGATION_DPAD = ACONFIGURATION_NAVIGATION_DPAD,
886 NAVIGATION_TRACKBALL = ACONFIGURATION_NAVIGATION_TRACKBALL,
887 NAVIGATION_WHEEL = ACONFIGURATION_NAVIGATION_WHEEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 };
889
890 enum {
891 MASK_KEYSHIDDEN = 0x0003,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700892 KEYSHIDDEN_ANY = ACONFIGURATION_KEYSHIDDEN_ANY,
893 KEYSHIDDEN_NO = ACONFIGURATION_KEYSHIDDEN_NO,
894 KEYSHIDDEN_YES = ACONFIGURATION_KEYSHIDDEN_YES,
895 KEYSHIDDEN_SOFT = ACONFIGURATION_KEYSHIDDEN_SOFT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 };
897
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700898 enum {
899 MASK_NAVHIDDEN = 0x000c,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700900 SHIFT_NAVHIDDEN = 2,
901 NAVHIDDEN_ANY = ACONFIGURATION_NAVHIDDEN_ANY << SHIFT_NAVHIDDEN,
902 NAVHIDDEN_NO = ACONFIGURATION_NAVHIDDEN_NO << SHIFT_NAVHIDDEN,
903 NAVHIDDEN_YES = ACONFIGURATION_NAVHIDDEN_YES << SHIFT_NAVHIDDEN,
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700904 };
905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 union {
907 struct {
908 uint8_t keyboard;
909 uint8_t navigation;
910 uint8_t inputFlags;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700911 uint8_t inputPad0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 };
913 uint32_t input;
914 };
915
916 enum {
917 SCREENWIDTH_ANY = 0
918 };
919
920 enum {
921 SCREENHEIGHT_ANY = 0
922 };
923
924 union {
925 struct {
926 uint16_t screenWidth;
927 uint16_t screenHeight;
928 };
929 uint32_t screenSize;
930 };
931
932 enum {
933 SDKVERSION_ANY = 0
934 };
935
936 enum {
937 MINORVERSION_ANY = 0
938 };
939
940 union {
941 struct {
942 uint16_t sdkVersion;
943 // For now minorVersion must always be 0!!! Its meaning
944 // is currently undefined.
945 uint16_t minorVersion;
946 };
947 uint32_t version;
948 };
949
Dianne Hackborn723738c2009-06-25 19:48:04 -0700950 enum {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700951 // screenLayout bits for screen size class.
952 MASK_SCREENSIZE = 0x0f,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700953 SCREENSIZE_ANY = ACONFIGURATION_SCREENSIZE_ANY,
954 SCREENSIZE_SMALL = ACONFIGURATION_SCREENSIZE_SMALL,
955 SCREENSIZE_NORMAL = ACONFIGURATION_SCREENSIZE_NORMAL,
956 SCREENSIZE_LARGE = ACONFIGURATION_SCREENSIZE_LARGE,
957 SCREENSIZE_XLARGE = ACONFIGURATION_SCREENSIZE_XLARGE,
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700958
959 // screenLayout bits for wide/long screen variation.
960 MASK_SCREENLONG = 0x30,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700961 SHIFT_SCREENLONG = 4,
962 SCREENLONG_ANY = ACONFIGURATION_SCREENLONG_ANY << SHIFT_SCREENLONG,
963 SCREENLONG_NO = ACONFIGURATION_SCREENLONG_NO << SHIFT_SCREENLONG,
964 SCREENLONG_YES = ACONFIGURATION_SCREENLONG_YES << SHIFT_SCREENLONG,
Fabrice Di Meglio5f797992012-06-15 20:16:41 -0700965
966 // screenLayout bits for layout direction.
967 MASK_LAYOUTDIR = 0xC0,
968 SHIFT_LAYOUTDIR = 6,
969 LAYOUTDIR_ANY = ACONFIGURATION_LAYOUTDIR_ANY << SHIFT_LAYOUTDIR,
970 LAYOUTDIR_LTR = ACONFIGURATION_LAYOUTDIR_LTR << SHIFT_LAYOUTDIR,
971 LAYOUTDIR_RTL = ACONFIGURATION_LAYOUTDIR_RTL << SHIFT_LAYOUTDIR,
Dianne Hackborn723738c2009-06-25 19:48:04 -0700972 };
973
Tobias Haamel27b28b32010-02-09 23:09:17 +0100974 enum {
975 // uiMode bits for the mode type.
976 MASK_UI_MODE_TYPE = 0x0f,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700977 UI_MODE_TYPE_ANY = ACONFIGURATION_UI_MODE_TYPE_ANY,
978 UI_MODE_TYPE_NORMAL = ACONFIGURATION_UI_MODE_TYPE_NORMAL,
979 UI_MODE_TYPE_DESK = ACONFIGURATION_UI_MODE_TYPE_DESK,
980 UI_MODE_TYPE_CAR = ACONFIGURATION_UI_MODE_TYPE_CAR,
Dianne Hackborne360bb62011-05-20 16:11:04 -0700981 UI_MODE_TYPE_TELEVISION = ACONFIGURATION_UI_MODE_TYPE_TELEVISION,
Joe Onorato44fcb832011-12-14 20:59:30 -0800982 UI_MODE_TYPE_APPLIANCE = ACONFIGURATION_UI_MODE_TYPE_APPLIANCE,
Tobias Haamel27b28b32010-02-09 23:09:17 +0100983
984 // uiMode bits for the night switch.
985 MASK_UI_MODE_NIGHT = 0x30,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700986 SHIFT_UI_MODE_NIGHT = 4,
987 UI_MODE_NIGHT_ANY = ACONFIGURATION_UI_MODE_NIGHT_ANY << SHIFT_UI_MODE_NIGHT,
988 UI_MODE_NIGHT_NO = ACONFIGURATION_UI_MODE_NIGHT_NO << SHIFT_UI_MODE_NIGHT,
989 UI_MODE_NIGHT_YES = ACONFIGURATION_UI_MODE_NIGHT_YES << SHIFT_UI_MODE_NIGHT,
Tobias Haamel27b28b32010-02-09 23:09:17 +0100990 };
991
Dianne Hackborn723738c2009-06-25 19:48:04 -0700992 union {
993 struct {
994 uint8_t screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100995 uint8_t uiMode;
Dianne Hackborn69cb8752011-05-19 18:13:32 -0700996 uint16_t smallestScreenWidthDp;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700997 };
998 uint32_t screenConfig;
999 };
1000
Dianne Hackbornebff8f92011-05-12 18:07:47 -07001001 union {
1002 struct {
1003 uint16_t screenWidthDp;
1004 uint16_t screenHeightDp;
1005 };
1006 uint32_t screenSizeDp;
1007 };
1008
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001009 void copyFromDeviceNoSwap(const ResTable_config& o);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001011 void copyFromDtoH(const ResTable_config& o);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001013 void swapHtoD();
1014
1015 int compare(const ResTable_config& o) const;
1016 int compareLogical(const ResTable_config& o) const;
1017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 // Flags indicating a set of config values. These flag constants must
1019 // match the corresponding ones in android.content.pm.ActivityInfo and
1020 // attrs_manifest.xml.
1021 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001022 CONFIG_MCC = ACONFIGURATION_MCC,
Mårten Kongstad4482e7c2013-11-26 14:48:22 +01001023 CONFIG_MNC = ACONFIGURATION_MNC,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001024 CONFIG_LOCALE = ACONFIGURATION_LOCALE,
1025 CONFIG_TOUCHSCREEN = ACONFIGURATION_TOUCHSCREEN,
1026 CONFIG_KEYBOARD = ACONFIGURATION_KEYBOARD,
1027 CONFIG_KEYBOARD_HIDDEN = ACONFIGURATION_KEYBOARD_HIDDEN,
1028 CONFIG_NAVIGATION = ACONFIGURATION_NAVIGATION,
1029 CONFIG_ORIENTATION = ACONFIGURATION_ORIENTATION,
1030 CONFIG_DENSITY = ACONFIGURATION_DENSITY,
1031 CONFIG_SCREEN_SIZE = ACONFIGURATION_SCREEN_SIZE,
Dianne Hackborn69cb8752011-05-19 18:13:32 -07001032 CONFIG_SMALLEST_SCREEN_SIZE = ACONFIGURATION_SMALLEST_SCREEN_SIZE,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001033 CONFIG_VERSION = ACONFIGURATION_VERSION,
1034 CONFIG_SCREEN_LAYOUT = ACONFIGURATION_SCREEN_LAYOUT,
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07001035 CONFIG_UI_MODE = ACONFIGURATION_UI_MODE,
1036 CONFIG_LAYOUTDIR = ACONFIGURATION_LAYOUTDIR,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 };
1038
1039 // Compare two configuration, returning CONFIG_* flags set for each value
1040 // that is different.
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001041 int diff(const ResTable_config& o) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042
Robert Greenwalt96e20402009-04-22 14:35:11 -07001043 // Return true if 'this' is more specific than 'o'.
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001044 bool isMoreSpecificThan(const ResTable_config& o) const;
Robert Greenwalt96e20402009-04-22 14:35:11 -07001045
1046 // Return true if 'this' is a better match than 'o' for the 'requested'
1047 // configuration. This assumes that match() has already been used to
1048 // remove any configurations that don't match the requested configuration
1049 // at all; if they are not first filtered, non-matching results can be
1050 // considered better than matching ones.
1051 // The general rule per attribute: if the request cares about an attribute
1052 // (it normally does), if the two (this and o) are equal it's a tie. If
1053 // they are not equal then one must be generic because only generic and
1054 // '==requested' will pass the match() call. So if this is not generic,
1055 // it wins. If this IS generic, o wins (return false).
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001056 bool isBetterThan(const ResTable_config& o, const ResTable_config* requested) const;
Robert Greenwalt96e20402009-04-22 14:35:11 -07001057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 // Return true if 'this' can be considered a match for the parameters in
1059 // 'settings'.
1060 // Note this is asymetric. A default piece of data will match every request
1061 // but a request for the default should not match odd specifics
1062 // (ie, request with no mcc should not match a particular mcc's data)
1063 // settings is the requested settings
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001064 bool match(const ResTable_config& settings) const;
Tobias Haamel27b28b32010-02-09 23:09:17 +01001065
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001066 void getLocale(char str[6]) const;
Tobias Haamel27b28b32010-02-09 23:09:17 +01001067
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001068 String8 toString() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069};
1070
1071/**
1072 * A specification of the resources defined by a particular type.
1073 *
1074 * There should be one of these chunks for each resource type.
1075 *
1076 * This structure is followed by an array of integers providing the set of
Fabrice Di Meglio5f797992012-06-15 20:16:41 -07001077 * configuration change flags (ResTable_config::CONFIG_*) that have multiple
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 * resources for that configuration. In addition, the high bit is set if that
1079 * resource has been made public.
1080 */
1081struct ResTable_typeSpec
1082{
1083 struct ResChunk_header header;
1084
1085 // The type identifier this chunk is holding. Type IDs start
1086 // at 1 (corresponding to the value of the type bits in a
1087 // resource identifier). 0 is invalid.
1088 uint8_t id;
1089
1090 // Must be 0.
1091 uint8_t res0;
1092 // Must be 0.
1093 uint16_t res1;
1094
1095 // Number of uint32_t entry configuration masks that follow.
1096 uint32_t entryCount;
1097
1098 enum {
1099 // Additional flag indicating an entry is public.
1100 SPEC_PUBLIC = 0x40000000
1101 };
1102};
1103
1104/**
1105 * A collection of resource entries for a particular resource data
1106 * type. Followed by an array of uint32_t defining the resource
1107 * values, corresponding to the array of type strings in the
1108 * ResTable_package::typeStrings string block. Each of these hold an
1109 * index from entriesStart; a value of NO_ENTRY means that entry is
1110 * not defined.
1111 *
1112 * There may be multiple of these chunks for a particular resource type,
1113 * supply different configuration variations for the resource values of
1114 * that type.
1115 *
1116 * It would be nice to have an additional ordered index of entries, so
1117 * we can do a binary search if trying to find a resource by string name.
1118 */
1119struct ResTable_type
1120{
1121 struct ResChunk_header header;
1122
1123 enum {
1124 NO_ENTRY = 0xFFFFFFFF
1125 };
1126
1127 // The type identifier this chunk is holding. Type IDs start
1128 // at 1 (corresponding to the value of the type bits in a
1129 // resource identifier). 0 is invalid.
1130 uint8_t id;
1131
1132 // Must be 0.
1133 uint8_t res0;
1134 // Must be 0.
1135 uint16_t res1;
1136
1137 // Number of uint32_t entry indices that follow.
1138 uint32_t entryCount;
1139
1140 // Offset from header where ResTable_entry data starts.
1141 uint32_t entriesStart;
1142
1143 // Configuration this collection of entries is designed for.
1144 ResTable_config config;
1145};
1146
1147/**
1148 * This is the beginning of information about an entry in the resource
1149 * table. It holds the reference to the name of this entry, and is
1150 * immediately followed by one of:
Dianne Hackbornde7faf62009-06-30 13:27:30 -07001151 * * A Res_value structure, if FLAG_COMPLEX is -not- set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 * * An array of ResTable_map structures, if FLAG_COMPLEX is set.
1153 * These supply a set of name/value mappings of data.
1154 */
1155struct ResTable_entry
1156{
1157 // Number of bytes in this structure.
1158 uint16_t size;
1159
1160 enum {
1161 // If set, this is a complex entry, holding a set of name/value
1162 // mappings. It is followed by an array of ResTable_map structures.
1163 FLAG_COMPLEX = 0x0001,
1164 // If set, this resource has been declared public, so libraries
1165 // are allowed to reference it.
1166 FLAG_PUBLIC = 0x0002
1167 };
1168 uint16_t flags;
1169
1170 // Reference into ResTable_package::keyStrings identifying this entry.
1171 struct ResStringPool_ref key;
1172};
1173
1174/**
1175 * Extended form of a ResTable_entry for map entries, defining a parent map
1176 * resource from which to inherit values.
1177 */
1178struct ResTable_map_entry : public ResTable_entry
1179{
1180 // Resource identifier of the parent mapping, or 0 if there is none.
1181 ResTable_ref parent;
1182 // Number of name/value pairs that follow for FLAG_COMPLEX.
1183 uint32_t count;
1184};
1185
1186/**
1187 * A single name/value mapping that is part of a complex resource
1188 * entry.
1189 */
1190struct ResTable_map
1191{
1192 // The resource identifier defining this mapping's name. For attribute
1193 // resources, 'name' can be one of the following special resource types
1194 // to supply meta-data about the attribute; for all other resource types
1195 // it must be an attribute resource.
1196 ResTable_ref name;
1197
1198 // Special values for 'name' when defining attribute resources.
1199 enum {
1200 // This entry holds the attribute's type code.
1201 ATTR_TYPE = Res_MAKEINTERNAL(0),
1202
1203 // For integral attributes, this is the minimum value it can hold.
1204 ATTR_MIN = Res_MAKEINTERNAL(1),
1205
1206 // For integral attributes, this is the maximum value it can hold.
1207 ATTR_MAX = Res_MAKEINTERNAL(2),
1208
1209 // Localization of this resource is can be encouraged or required with
1210 // an aapt flag if this is set
1211 ATTR_L10N = Res_MAKEINTERNAL(3),
1212
1213 // for plural support, see android.content.res.PluralRules#attrForQuantity(int)
1214 ATTR_OTHER = Res_MAKEINTERNAL(4),
1215 ATTR_ZERO = Res_MAKEINTERNAL(5),
1216 ATTR_ONE = Res_MAKEINTERNAL(6),
1217 ATTR_TWO = Res_MAKEINTERNAL(7),
1218 ATTR_FEW = Res_MAKEINTERNAL(8),
1219 ATTR_MANY = Res_MAKEINTERNAL(9)
1220
1221 };
1222
1223 // Bit mask of allowed types, for use with ATTR_TYPE.
1224 enum {
1225 // No type has been defined for this attribute, use generic
1226 // type handling. The low 16 bits are for types that can be
1227 // handled generically; the upper 16 require additional information
1228 // in the bag so can not be handled generically for TYPE_ANY.
1229 TYPE_ANY = 0x0000FFFF,
1230
1231 // Attribute holds a references to another resource.
1232 TYPE_REFERENCE = 1<<0,
1233
1234 // Attribute holds a generic string.
1235 TYPE_STRING = 1<<1,
1236
1237 // Attribute holds an integer value. ATTR_MIN and ATTR_MIN can
1238 // optionally specify a constrained range of possible integer values.
1239 TYPE_INTEGER = 1<<2,
1240
1241 // Attribute holds a boolean integer.
1242 TYPE_BOOLEAN = 1<<3,
1243
1244 // Attribute holds a color value.
1245 TYPE_COLOR = 1<<4,
1246
1247 // Attribute holds a floating point value.
1248 TYPE_FLOAT = 1<<5,
1249
1250 // Attribute holds a dimension value, such as "20px".
1251 TYPE_DIMENSION = 1<<6,
1252
1253 // Attribute holds a fraction value, such as "20%".
1254 TYPE_FRACTION = 1<<7,
1255
1256 // Attribute holds an enumeration. The enumeration values are
1257 // supplied as additional entries in the map.
1258 TYPE_ENUM = 1<<16,
1259
1260 // Attribute holds a bitmaks of flags. The flag bit values are
1261 // supplied as additional entries in the map.
1262 TYPE_FLAGS = 1<<17
1263 };
1264
1265 // Enum of localization modes, for use with ATTR_L10N.
1266 enum {
1267 L10N_NOT_REQUIRED = 0,
1268 L10N_SUGGESTED = 1
1269 };
1270
1271 // This mapping's value.
1272 Res_value value;
1273};
1274
1275/**
1276 * Convenience class for accessing data in a ResTable resource.
1277 */
1278class ResTable
1279{
1280public:
1281 ResTable();
Narayan Kamath7c4887f2014-01-27 17:32:37 +00001282 ResTable(const void* data, size_t size, const int32_t cookie,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 bool copyData=false);
1284 ~ResTable();
1285
Narayan Kamath7c4887f2014-01-27 17:32:37 +00001286 status_t add(Asset* asset, const int32_t cookie, bool copyData,
1287 const void* idmap);
1288 status_t add(const void *data, size_t size);
Dianne Hackborn78c40512009-07-06 11:07:40 -07001289 status_t add(ResTable* src);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290
1291 status_t getError() const;
1292
1293 void uninit();
1294
1295 struct resource_name
1296 {
1297 const char16_t* package;
1298 size_t packageLen;
1299 const char16_t* type;
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001300 const char* type8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 size_t typeLen;
1302 const char16_t* name;
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001303 const char* name8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 size_t nameLen;
1305 };
1306
Dianne Hackbornd45c68d2013-07-31 12:14:24 -07001307 bool getResourceName(uint32_t resID, bool allowUtf8, resource_name* outName) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308
1309 /**
1310 * Retrieve the value of a resource. If the resource is found, returns a
1311 * value >= 0 indicating the table it is in (for use with
1312 * getTableStringBlock() and getTableCookie()) and fills in 'outValue'. If
1313 * not found, returns a negative error code.
1314 *
1315 * Note that this function does not do reference traversal. If you want
1316 * to follow references to other resources to get the "real" value to
1317 * use, you need to call resolveReference() after this function.
1318 *
1319 * @param resID The desired resoruce identifier.
1320 * @param outValue Filled in with the resource data that was found.
1321 *
1322 * @return ssize_t Either a >= 0 table index or a negative error code.
1323 */
Kenny Root55fc8502010-10-28 14:47:01 -07001324 ssize_t getResource(uint32_t resID, Res_value* outValue, bool mayBeBag = false,
1325 uint16_t density = 0,
1326 uint32_t* outSpecFlags = NULL,
1327 ResTable_config* outConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328
1329 inline ssize_t getResource(const ResTable_ref& res, Res_value* outValue,
1330 uint32_t* outSpecFlags=NULL) const {
Kenny Root55fc8502010-10-28 14:47:01 -07001331 return getResource(res.ident, outValue, false, 0, outSpecFlags, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 }
1333
1334 ssize_t resolveReference(Res_value* inOutValue,
1335 ssize_t blockIndex,
1336 uint32_t* outLastRef = NULL,
Dianne Hackborn0d221012009-07-29 15:41:19 -07001337 uint32_t* inoutTypeSpecFlags = NULL,
1338 ResTable_config* outConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339
1340 enum {
1341 TMP_BUFFER_SIZE = 16
1342 };
1343 const char16_t* valueToString(const Res_value* value, size_t stringBlock,
1344 char16_t tmpBuffer[TMP_BUFFER_SIZE],
1345 size_t* outLen);
1346
1347 struct bag_entry {
1348 ssize_t stringBlock;
1349 ResTable_map map;
1350 };
1351
1352 /**
1353 * Retrieve the bag of a resource. If the resoruce is found, returns the
1354 * number of bags it contains and 'outBag' points to an array of their
1355 * values. If not found, a negative error code is returned.
1356 *
1357 * Note that this function -does- do reference traversal of the bag data.
1358 *
1359 * @param resID The desired resource identifier.
1360 * @param outBag Filled inm with a pointer to the bag mappings.
1361 *
1362 * @return ssize_t Either a >= 0 bag count of negative error code.
1363 */
1364 ssize_t lockBag(uint32_t resID, const bag_entry** outBag) const;
1365
1366 void unlockBag(const bag_entry* bag) const;
1367
1368 void lock() const;
1369
1370 ssize_t getBagLocked(uint32_t resID, const bag_entry** outBag,
1371 uint32_t* outTypeSpecFlags=NULL) const;
1372
1373 void unlock() const;
1374
1375 class Theme {
1376 public:
1377 Theme(const ResTable& table);
1378 ~Theme();
1379
1380 inline const ResTable& getResTable() const { return mTable; }
1381
1382 status_t applyStyle(uint32_t resID, bool force=false);
1383 status_t setTo(const Theme& other);
1384
1385 /**
1386 * Retrieve a value in the theme. If the theme defines this
1387 * value, returns a value >= 0 indicating the table it is in
1388 * (for use with getTableStringBlock() and getTableCookie) and
1389 * fills in 'outValue'. If not found, returns a negative error
1390 * code.
1391 *
1392 * Note that this function does not do reference traversal. If you want
1393 * to follow references to other resources to get the "real" value to
1394 * use, you need to call resolveReference() after this function.
1395 *
1396 * @param resID A resource identifier naming the desired theme
1397 * attribute.
1398 * @param outValue Filled in with the theme value that was
1399 * found.
1400 *
1401 * @return ssize_t Either a >= 0 table index or a negative error code.
1402 */
1403 ssize_t getAttribute(uint32_t resID, Res_value* outValue,
1404 uint32_t* outTypeSpecFlags = NULL) const;
1405
1406 /**
1407 * This is like ResTable::resolveReference(), but also takes
1408 * care of resolving attribute references to the theme.
1409 */
1410 ssize_t resolveAttributeReference(Res_value* inOutValue,
1411 ssize_t blockIndex, uint32_t* outLastRef = NULL,
Dianne Hackborn0d221012009-07-29 15:41:19 -07001412 uint32_t* inoutTypeSpecFlags = NULL,
1413 ResTable_config* inoutConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414
1415 void dumpToLog() const;
1416
1417 private:
1418 Theme(const Theme&);
1419 Theme& operator=(const Theme&);
1420
1421 struct theme_entry {
1422 ssize_t stringBlock;
1423 uint32_t typeSpecFlags;
1424 Res_value value;
1425 };
1426 struct type_info {
1427 size_t numEntries;
1428 theme_entry* entries;
1429 };
1430 struct package_info {
1431 size_t numTypes;
1432 type_info types[];
1433 };
1434
1435 void free_package(package_info* pi);
1436 package_info* copy_package(package_info* pi);
1437
1438 const ResTable& mTable;
1439 package_info* mPackages[Res_MAXPACKAGE];
1440 };
1441
1442 void setParameters(const ResTable_config* params);
1443 void getParameters(ResTable_config* params) const;
1444
1445 // Retrieve an identifier (which can be passed to getResource)
1446 // for a given resource name. The 'name' can be fully qualified
1447 // (<package>:<type>.<basename>) or the package or type components
1448 // can be dropped if default values are supplied here.
1449 //
1450 // Returns 0 if no such resource was found, else a valid resource ID.
1451 uint32_t identifierForName(const char16_t* name, size_t nameLen,
1452 const char16_t* type = 0, size_t typeLen = 0,
1453 const char16_t* defPackage = 0,
1454 size_t defPackageLen = 0,
1455 uint32_t* outTypeSpecFlags = NULL) const;
1456
1457 static bool expandResourceRef(const uint16_t* refStr, size_t refLen,
1458 String16* outPackage,
1459 String16* outType,
1460 String16* outName,
1461 const String16* defType = NULL,
1462 const String16* defPackage = NULL,
Dianne Hackborn426431a2011-06-09 11:29:08 -07001463 const char** outErrorMsg = NULL,
1464 bool* outPublicOnly = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465
1466 static bool stringToInt(const char16_t* s, size_t len, Res_value* outValue);
1467 static bool stringToFloat(const char16_t* s, size_t len, Res_value* outValue);
1468
1469 // Used with stringToValue.
1470 class Accessor
1471 {
1472 public:
1473 inline virtual ~Accessor() { }
1474
1475 virtual uint32_t getCustomResource(const String16& package,
1476 const String16& type,
1477 const String16& name) const = 0;
1478 virtual uint32_t getCustomResourceWithCreation(const String16& package,
1479 const String16& type,
1480 const String16& name,
1481 const bool createIfNeeded = false) = 0;
1482 virtual uint32_t getRemappedPackage(uint32_t origPackage) const = 0;
1483 virtual bool getAttributeType(uint32_t attrID, uint32_t* outType) = 0;
1484 virtual bool getAttributeMin(uint32_t attrID, uint32_t* outMin) = 0;
1485 virtual bool getAttributeMax(uint32_t attrID, uint32_t* outMax) = 0;
1486 virtual bool getAttributeEnum(uint32_t attrID,
1487 const char16_t* name, size_t nameLen,
1488 Res_value* outValue) = 0;
1489 virtual bool getAttributeFlags(uint32_t attrID,
1490 const char16_t* name, size_t nameLen,
1491 Res_value* outValue) = 0;
1492 virtual uint32_t getAttributeL10N(uint32_t attrID) = 0;
1493 virtual bool getLocalizationSetting() = 0;
1494 virtual void reportError(void* accessorCookie, const char* fmt, ...) = 0;
1495 };
1496
1497 // Convert a string to a resource value. Handles standard "@res",
1498 // "#color", "123", and "0x1bd" types; performs escaping of strings.
1499 // The resulting value is placed in 'outValue'; if it is a string type,
1500 // 'outString' receives the string. If 'attrID' is supplied, the value is
1501 // type checked against this attribute and it is used to perform enum
1502 // evaluation. If 'acccessor' is supplied, it will be used to attempt to
1503 // resolve resources that do not exist in this ResTable. If 'attrType' is
1504 // supplied, the value will be type checked for this format if 'attrID'
1505 // is not supplied or found.
1506 bool stringToValue(Res_value* outValue, String16* outString,
1507 const char16_t* s, size_t len,
1508 bool preserveSpaces, bool coerceType,
1509 uint32_t attrID = 0,
1510 const String16* defType = NULL,
1511 const String16* defPackage = NULL,
1512 Accessor* accessor = NULL,
1513 void* accessorCookie = NULL,
1514 uint32_t attrType = ResTable_map::TYPE_ANY,
1515 bool enforcePrivate = true) const;
1516
1517 // Perform processing of escapes and quotes in a string.
1518 static bool collectString(String16* outString,
1519 const char16_t* s, size_t len,
1520 bool preserveSpaces,
1521 const char** outErrorMsg = NULL,
1522 bool append = false);
1523
1524 size_t getBasePackageCount() const;
1525 const char16_t* getBasePackageName(size_t idx) const;
1526 uint32_t getBasePackageId(size_t idx) const;
1527
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001528 // Return the number of resource tables that the object contains.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 size_t getTableCount() const;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001530 // Return the values string pool for the resource table at the given
1531 // index. This string pool contains all of the strings for values
1532 // contained in the resource table -- that is the item values themselves,
1533 // but not the names their entries or types.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 const ResStringPool* getTableStringBlock(size_t index) const;
Dianne Hackborn6c997a92012-01-31 11:27:43 -08001535 // Return unique cookie identifier for the given resource table.
Narayan Kamath7c4887f2014-01-27 17:32:37 +00001536 int32_t getTableCookie(size_t index) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537
1538 // Return the configurations (ResTable_config) that we know about
1539 void getConfigurations(Vector<ResTable_config>* configs) const;
1540
1541 void getLocales(Vector<String8>* locales) const;
1542
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001543 // Generate an idmap.
1544 //
1545 // Return value: on success: NO_ERROR; caller is responsible for free-ing
1546 // outData (using free(3)). On failure, any status_t value other than
1547 // NO_ERROR; the caller should not free outData.
Mårten Kongstad65a05fd2014-01-31 14:01:52 +01001548 status_t createIdmap(const ResTable& overlay, uint32_t targetCrc, uint32_t overlayCrc,
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001549 void** outData, size_t* outSize) const;
1550
Mårten Kongstad65a05fd2014-01-31 14:01:52 +01001551 status_t createIdmap(const ResTable& overlay,
1552 uint32_t targetCrc, uint32_t overlayCrc,
1553 const char* targetPath, const char* overlayPath,
1554 void** outData, uint32_t* outSize) const
1555 {
1556 (void)targetPath;
1557 (void)overlayPath;
1558 return createIdmap(overlay, targetCrc, overlayCrc, outData, outSize);
1559 }
1560
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001561 enum {
1562 IDMAP_HEADER_SIZE_BYTES = 3 * sizeof(uint32_t),
1563 };
1564 // Retrieve idmap meta-data.
1565 //
1566 // This function only requires the idmap header (the first
1567 // IDMAP_HEADER_SIZE_BYTES) bytes of an idmap file.
1568 static bool getIdmapInfo(const void* idmap, size_t size,
Mårten Kongstad65a05fd2014-01-31 14:01:52 +01001569 uint32_t* pTargetCrc, uint32_t* pOverlayCrc);
1570
1571 static bool getIdmapInfo(const void* idmap, size_t size,
1572 uint32_t* pTargetCrc, uint32_t* pOverlayCrc,
1573 String8* pTargetPath, String8* pOverlayPath)
1574 {
1575 if (*pTargetPath)
1576 *pTargetPath = String8();
1577 if (*pOverlayPath)
1578 *pOverlayPath = String8();
1579 return getIdmapInfo(idmap, size, pTargetCrc, pOverlayCrc);
1580 }
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001581
Dianne Hackborne17086b2009-06-19 15:13:28 -07001582 void print(bool inclValues) const;
Shachar Shemesh9872bf42010-12-20 17:38:33 +02001583 static String8 normalizeForOutput(const char* input);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584
1585private:
1586 struct Header;
1587 struct Type;
1588 struct Package;
1589 struct PackageGroup;
1590 struct bag_set;
1591
Narayan Kamath7c4887f2014-01-27 17:32:37 +00001592 status_t addInternal(const void* data, size_t size, const int32_t cookie,
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001593 Asset* asset, bool copyData, const Asset* idmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594
1595 ssize_t getResourcePackageIndex(uint32_t resID) const;
1596 ssize_t getEntry(
1597 const Package* package, int typeIndex, int entryIndex,
1598 const ResTable_config* config,
1599 const ResTable_type** outType, const ResTable_entry** outEntry,
1600 const Type** outTypeClass) const;
1601 status_t parsePackage(
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001602 const ResTable_package* const pkg, const Header* const header, uint32_t idmap_id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603
Dianne Hackbornde7faf62009-06-30 13:27:30 -07001604 void print_value(const Package* pkg, const Res_value& value) const;
1605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 mutable Mutex mLock;
1607
1608 status_t mError;
1609
1610 ResTable_config mParams;
1611
1612 // Array of all resource tables.
1613 Vector<Header*> mHeaders;
1614
1615 // Array of packages in all resource tables.
1616 Vector<PackageGroup*> mPackageGroups;
1617
1618 // Mapping from resource package IDs to indices into the internal
1619 // package array.
1620 uint8_t mPackageMap[256];
1621};
1622
1623} // namespace android
1624
1625#endif // _LIBS_UTILS_RESOURCE_TYPES_H