blob: 173412e4155a03fcf4052d43aeed5c079291d880 [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
23#include <utils/Asset.h>
24#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
447 inline const char16_t* stringAt(const ResStringPool_ref& ref, size_t* outLen) const {
448 return stringAt(ref.index, outLen);
449 }
450 const char16_t* stringAt(size_t idx, size_t* outLen) const;
451
Kenny Root780d2a12010-02-22 22:36:26 -0800452 const char* string8At(size_t idx, size_t* outLen) const;
453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 const ResStringPool_span* styleAt(const ResStringPool_ref& ref) const;
455 const ResStringPool_span* styleAt(size_t idx) const;
456
457 ssize_t indexOfString(const char16_t* str, size_t strLen) const;
458
459 size_t size() const;
460
Kenny Rootbb79f642009-12-10 14:20:15 -0800461#ifndef HAVE_ANDROID_OS
462 bool isUTF8() const;
463#endif
464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465private:
466 status_t mError;
467 void* mOwnedData;
468 const ResStringPool_header* mHeader;
469 size_t mSize;
Kenny Root19138462009-12-04 09:38:48 -0800470 mutable Mutex mDecodeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 const uint32_t* mEntries;
472 const uint32_t* mEntryStyles;
Kenny Root19138462009-12-04 09:38:48 -0800473 const void* mStrings;
474 char16_t** mCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 uint32_t mStringPoolSize; // number of uint16_t
476 const uint32_t* mStyles;
477 uint32_t mStylePoolSize; // number of uint32_t
478};
479
480/** ********************************************************************
481 * XML Tree
482 *
483 * Binary representation of an XML document. This is designed to
484 * express everything in an XML document, in a form that is much
485 * easier to parse on the device.
486 *
487 *********************************************************************** */
488
489/**
490 * XML tree header. This appears at the front of an XML tree,
491 * describing its content. It is followed by a flat array of
492 * ResXMLTree_node structures; the hierarchy of the XML document
493 * is described by the occurrance of RES_XML_START_ELEMENT_TYPE
494 * and corresponding RES_XML_END_ELEMENT_TYPE nodes in the array.
495 */
496struct ResXMLTree_header
497{
498 struct ResChunk_header header;
499};
500
501/**
502 * Basic XML tree node. A single item in the XML document. Extended info
503 * about the node can be found after header.headerSize.
504 */
505struct ResXMLTree_node
506{
507 struct ResChunk_header header;
508
509 // Line number in original source file at which this element appeared.
510 uint32_t lineNumber;
511
512 // Optional XML comment that was associated with this element; -1 if none.
513 struct ResStringPool_ref comment;
514};
515
516/**
517 * Extended XML tree node for CDATA tags -- includes the CDATA string.
518 * Appears header.headerSize bytes after a ResXMLTree_node.
519 */
520struct ResXMLTree_cdataExt
521{
522 // The raw CDATA character data.
523 struct ResStringPool_ref data;
524
525 // The typed value of the character data if this is a CDATA node.
526 struct Res_value typedData;
527};
528
529/**
530 * Extended XML tree node for namespace start/end nodes.
531 * Appears header.headerSize bytes after a ResXMLTree_node.
532 */
533struct ResXMLTree_namespaceExt
534{
535 // The prefix of the namespace.
536 struct ResStringPool_ref prefix;
537
538 // The URI of the namespace.
539 struct ResStringPool_ref uri;
540};
541
542/**
543 * Extended XML tree node for element start/end nodes.
544 * Appears header.headerSize bytes after a ResXMLTree_node.
545 */
546struct ResXMLTree_endElementExt
547{
548 // String of the full namespace of this element.
549 struct ResStringPool_ref ns;
550
551 // String name of this node if it is an ELEMENT; the raw
552 // character data if this is a CDATA node.
553 struct ResStringPool_ref name;
554};
555
556/**
557 * Extended XML tree node for start tags -- includes attribute
558 * information.
559 * Appears header.headerSize bytes after a ResXMLTree_node.
560 */
561struct ResXMLTree_attrExt
562{
563 // String of the full namespace of this element.
564 struct ResStringPool_ref ns;
565
566 // String name of this node if it is an ELEMENT; the raw
567 // character data if this is a CDATA node.
568 struct ResStringPool_ref name;
569
570 // Byte offset from the start of this structure where the attributes start.
571 uint16_t attributeStart;
572
573 // Size of the ResXMLTree_attribute structures that follow.
574 uint16_t attributeSize;
575
576 // Number of attributes associated with an ELEMENT. These are
577 // available as an array of ResXMLTree_attribute structures
578 // immediately following this node.
579 uint16_t attributeCount;
580
581 // Index (1-based) of the "id" attribute. 0 if none.
582 uint16_t idIndex;
583
584 // Index (1-based) of the "class" attribute. 0 if none.
585 uint16_t classIndex;
586
587 // Index (1-based) of the "style" attribute. 0 if none.
588 uint16_t styleIndex;
589};
590
591struct ResXMLTree_attribute
592{
593 // Namespace of this attribute.
594 struct ResStringPool_ref ns;
595
596 // Name of this attribute.
597 struct ResStringPool_ref name;
598
599 // The original raw string value of this attribute.
600 struct ResStringPool_ref rawValue;
601
602 // Processesd typed value of this attribute.
603 struct Res_value typedValue;
604};
605
606class ResXMLTree;
607
608class ResXMLParser
609{
610public:
611 ResXMLParser(const ResXMLTree& tree);
612
613 enum event_code_t {
614 BAD_DOCUMENT = -1,
615 START_DOCUMENT = 0,
616 END_DOCUMENT = 1,
617
618 FIRST_CHUNK_CODE = RES_XML_FIRST_CHUNK_TYPE,
619
620 START_NAMESPACE = RES_XML_START_NAMESPACE_TYPE,
621 END_NAMESPACE = RES_XML_END_NAMESPACE_TYPE,
622 START_TAG = RES_XML_START_ELEMENT_TYPE,
623 END_TAG = RES_XML_END_ELEMENT_TYPE,
624 TEXT = RES_XML_CDATA_TYPE
625 };
626
627 struct ResXMLPosition
628 {
629 event_code_t eventCode;
630 const ResXMLTree_node* curNode;
631 const void* curExt;
632 };
633
634 void restart();
635
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800636 const ResStringPool& getStrings() const;
637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 event_code_t getEventType() const;
639 // Note, unlike XmlPullParser, the first call to next() will return
640 // START_TAG of the first element.
641 event_code_t next();
642
643 // These are available for all nodes:
Mathias Agopian5f910972009-06-22 02:35:32 -0700644 int32_t getCommentID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 const uint16_t* getComment(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700646 uint32_t getLineNumber() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647
648 // This is available for TEXT:
Mathias Agopian5f910972009-06-22 02:35:32 -0700649 int32_t getTextID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 const uint16_t* getText(size_t* outLen) const;
651 ssize_t getTextValue(Res_value* outValue) const;
652
653 // These are available for START_NAMESPACE and END_NAMESPACE:
Mathias Agopian5f910972009-06-22 02:35:32 -0700654 int32_t getNamespacePrefixID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 const uint16_t* getNamespacePrefix(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700656 int32_t getNamespaceUriID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 const uint16_t* getNamespaceUri(size_t* outLen) const;
658
659 // These are available for START_TAG and END_TAG:
Mathias Agopian5f910972009-06-22 02:35:32 -0700660 int32_t getElementNamespaceID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 const uint16_t* getElementNamespace(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700662 int32_t getElementNameID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 const uint16_t* getElementName(size_t* outLen) const;
664
665 // Remaining methods are for retrieving information about attributes
666 // associated with a START_TAG:
667
668 size_t getAttributeCount() const;
669
670 // Returns -1 if no namespace, -2 if idx out of range.
Mathias Agopian5f910972009-06-22 02:35:32 -0700671 int32_t getAttributeNamespaceID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 const uint16_t* getAttributeNamespace(size_t idx, size_t* outLen) const;
673
Mathias Agopian5f910972009-06-22 02:35:32 -0700674 int32_t getAttributeNameID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 const uint16_t* getAttributeName(size_t idx, size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700676 uint32_t getAttributeNameResID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677
Mathias Agopian5f910972009-06-22 02:35:32 -0700678 int32_t getAttributeValueStringID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 const uint16_t* getAttributeStringValue(size_t idx, size_t* outLen) const;
680
681 int32_t getAttributeDataType(size_t idx) const;
682 int32_t getAttributeData(size_t idx) const;
683 ssize_t getAttributeValue(size_t idx, Res_value* outValue) const;
684
685 ssize_t indexOfAttribute(const char* ns, const char* attr) const;
686 ssize_t indexOfAttribute(const char16_t* ns, size_t nsLen,
687 const char16_t* attr, size_t attrLen) const;
688
689 ssize_t indexOfID() const;
690 ssize_t indexOfClass() const;
691 ssize_t indexOfStyle() const;
692
693 void getPosition(ResXMLPosition* pos) const;
694 void setPosition(const ResXMLPosition& pos);
695
696private:
697 friend class ResXMLTree;
698
699 event_code_t nextNode();
700
701 const ResXMLTree& mTree;
702 event_code_t mEventCode;
703 const ResXMLTree_node* mCurNode;
704 const void* mCurExt;
705};
706
707/**
708 * Convenience class for accessing data in a ResXMLTree resource.
709 */
710class ResXMLTree : public ResXMLParser
711{
712public:
713 ResXMLTree();
714 ResXMLTree(const void* data, size_t size, bool copyData=false);
715 ~ResXMLTree();
716
717 status_t setTo(const void* data, size_t size, bool copyData=false);
718
719 status_t getError() const;
720
721 void uninit();
722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723private:
724 friend class ResXMLParser;
725
726 status_t validateNode(const ResXMLTree_node* node) const;
727
728 status_t mError;
729 void* mOwnedData;
730 const ResXMLTree_header* mHeader;
731 size_t mSize;
732 const uint8_t* mDataEnd;
733 ResStringPool mStrings;
734 const uint32_t* mResIds;
735 size_t mNumResIds;
736 const ResXMLTree_node* mRootNode;
737 const void* mRootExt;
738 event_code_t mRootCode;
739};
740
741/** ********************************************************************
742 * RESOURCE TABLE
743 *
744 *********************************************************************** */
745
746/**
747 * Header for a resource table. Its data contains a series of
748 * additional chunks:
749 * * A ResStringPool_header containing all table values.
750 * * One or more ResTable_package chunks.
751 *
752 * Specific entries within a resource table can be uniquely identified
753 * with a single integer as defined by the ResTable_ref structure.
754 */
755struct ResTable_header
756{
757 struct ResChunk_header header;
758
759 // The number of ResTable_package structures.
760 uint32_t packageCount;
761};
762
763/**
764 * A collection of resource data types within a package. Followed by
765 * one or more ResTable_type and ResTable_typeSpec structures containing the
766 * entry values for each resource type.
767 */
768struct ResTable_package
769{
770 struct ResChunk_header header;
771
772 // If this is a base package, its ID. Package IDs start
773 // at 1 (corresponding to the value of the package bits in a
774 // resource identifier). 0 means this is not a base package.
775 uint32_t id;
776
777 // Actual name of this package, \0-terminated.
778 char16_t name[128];
779
780 // Offset to a ResStringPool_header defining the resource
781 // type symbol table. If zero, this package is inheriting from
782 // another base package (overriding specific values in it).
783 uint32_t typeStrings;
784
785 // Last index into typeStrings that is for public use by others.
786 uint32_t lastPublicType;
787
788 // Offset to a ResStringPool_header defining the resource
789 // key symbol table. If zero, this package is inheriting from
790 // another base package (overriding specific values in it).
791 uint32_t keyStrings;
792
793 // Last index into keyStrings that is for public use by others.
794 uint32_t lastPublicKey;
795};
796
797/**
798 * Describes a particular resource configuration.
799 */
800struct ResTable_config
801{
802 // Number of bytes in this structure.
803 uint32_t size;
804
805 union {
806 struct {
807 // Mobile country code (from SIM). 0 means "any".
808 uint16_t mcc;
809 // Mobile network code (from SIM). 0 means "any".
810 uint16_t mnc;
811 };
812 uint32_t imsi;
813 };
814
815 union {
816 struct {
817 // \0\0 means "any". Otherwise, en, fr, etc.
818 char language[2];
819
820 // \0\0 means "any". Otherwise, US, CA, etc.
821 char country[2];
822 };
823 uint32_t locale;
824 };
825
826 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700827 ORIENTATION_ANY = ACONFIGURATION_ORIENTATION_ANY,
828 ORIENTATION_PORT = ACONFIGURATION_ORIENTATION_PORT,
829 ORIENTATION_LAND = ACONFIGURATION_ORIENTATION_LAND,
830 ORIENTATION_SQUARE = ACONFIGURATION_ORIENTATION_SQUARE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 };
832
833 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700834 TOUCHSCREEN_ANY = ACONFIGURATION_TOUCHSCREEN_ANY,
835 TOUCHSCREEN_NOTOUCH = ACONFIGURATION_TOUCHSCREEN_NOTOUCH,
836 TOUCHSCREEN_STYLUS = ACONFIGURATION_TOUCHSCREEN_STYLUS,
837 TOUCHSCREEN_FINGER = ACONFIGURATION_TOUCHSCREEN_FINGER,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 };
839
840 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700841 DENSITY_DEFAULT = ACONFIGURATION_DENSITY_DEFAULT,
842 DENSITY_LOW = ACONFIGURATION_DENSITY_LOW,
843 DENSITY_MEDIUM = ACONFIGURATION_DENSITY_MEDIUM,
844 DENSITY_HIGH = ACONFIGURATION_DENSITY_HIGH,
845 DENSITY_NONE = ACONFIGURATION_DENSITY_NONE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 };
847
848 union {
849 struct {
850 uint8_t orientation;
851 uint8_t touchscreen;
852 uint16_t density;
853 };
854 uint32_t screenType;
855 };
856
857 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700858 KEYBOARD_ANY = ACONFIGURATION_KEYBOARD_ANY,
859 KEYBOARD_NOKEYS = ACONFIGURATION_KEYBOARD_NOKEYS,
860 KEYBOARD_QWERTY = ACONFIGURATION_KEYBOARD_QWERTY,
861 KEYBOARD_12KEY = ACONFIGURATION_KEYBOARD_12KEY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 };
863
864 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700865 NAVIGATION_ANY = ACONFIGURATION_NAVIGATION_ANY,
866 NAVIGATION_NONAV = ACONFIGURATION_NAVIGATION_NONAV,
867 NAVIGATION_DPAD = ACONFIGURATION_NAVIGATION_DPAD,
868 NAVIGATION_TRACKBALL = ACONFIGURATION_NAVIGATION_TRACKBALL,
869 NAVIGATION_WHEEL = ACONFIGURATION_NAVIGATION_WHEEL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 };
871
872 enum {
873 MASK_KEYSHIDDEN = 0x0003,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700874 KEYSHIDDEN_ANY = ACONFIGURATION_KEYSHIDDEN_ANY,
875 KEYSHIDDEN_NO = ACONFIGURATION_KEYSHIDDEN_NO,
876 KEYSHIDDEN_YES = ACONFIGURATION_KEYSHIDDEN_YES,
877 KEYSHIDDEN_SOFT = ACONFIGURATION_KEYSHIDDEN_SOFT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 };
879
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700880 enum {
881 MASK_NAVHIDDEN = 0x000c,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700882 SHIFT_NAVHIDDEN = 2,
883 NAVHIDDEN_ANY = ACONFIGURATION_NAVHIDDEN_ANY << SHIFT_NAVHIDDEN,
884 NAVHIDDEN_NO = ACONFIGURATION_NAVHIDDEN_NO << SHIFT_NAVHIDDEN,
885 NAVHIDDEN_YES = ACONFIGURATION_NAVHIDDEN_YES << SHIFT_NAVHIDDEN,
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700886 };
887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 union {
889 struct {
890 uint8_t keyboard;
891 uint8_t navigation;
892 uint8_t inputFlags;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700893 uint8_t inputPad0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 };
895 uint32_t input;
896 };
897
898 enum {
899 SCREENWIDTH_ANY = 0
900 };
901
902 enum {
903 SCREENHEIGHT_ANY = 0
904 };
905
906 union {
907 struct {
908 uint16_t screenWidth;
909 uint16_t screenHeight;
910 };
911 uint32_t screenSize;
912 };
913
914 enum {
915 SDKVERSION_ANY = 0
916 };
917
918 enum {
919 MINORVERSION_ANY = 0
920 };
921
922 union {
923 struct {
924 uint16_t sdkVersion;
925 // For now minorVersion must always be 0!!! Its meaning
926 // is currently undefined.
927 uint16_t minorVersion;
928 };
929 uint32_t version;
930 };
931
Dianne Hackborn723738c2009-06-25 19:48:04 -0700932 enum {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700933 // screenLayout bits for screen size class.
934 MASK_SCREENSIZE = 0x0f,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700935 SCREENSIZE_ANY = ACONFIGURATION_SCREENSIZE_ANY,
936 SCREENSIZE_SMALL = ACONFIGURATION_SCREENSIZE_SMALL,
937 SCREENSIZE_NORMAL = ACONFIGURATION_SCREENSIZE_NORMAL,
938 SCREENSIZE_LARGE = ACONFIGURATION_SCREENSIZE_LARGE,
939 SCREENSIZE_XLARGE = ACONFIGURATION_SCREENSIZE_XLARGE,
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700940
941 // screenLayout bits for wide/long screen variation.
942 MASK_SCREENLONG = 0x30,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700943 SHIFT_SCREENLONG = 4,
944 SCREENLONG_ANY = ACONFIGURATION_SCREENLONG_ANY << SHIFT_SCREENLONG,
945 SCREENLONG_NO = ACONFIGURATION_SCREENLONG_NO << SHIFT_SCREENLONG,
946 SCREENLONG_YES = ACONFIGURATION_SCREENLONG_YES << SHIFT_SCREENLONG,
Dianne Hackborn723738c2009-06-25 19:48:04 -0700947 };
948
Tobias Haamel27b28b32010-02-09 23:09:17 +0100949 enum {
950 // uiMode bits for the mode type.
951 MASK_UI_MODE_TYPE = 0x0f,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700952 UI_MODE_TYPE_ANY = ACONFIGURATION_UI_MODE_TYPE_ANY,
953 UI_MODE_TYPE_NORMAL = ACONFIGURATION_UI_MODE_TYPE_NORMAL,
954 UI_MODE_TYPE_DESK = ACONFIGURATION_UI_MODE_TYPE_DESK,
955 UI_MODE_TYPE_CAR = ACONFIGURATION_UI_MODE_TYPE_CAR,
Tobias Haamel27b28b32010-02-09 23:09:17 +0100956
957 // uiMode bits for the night switch.
958 MASK_UI_MODE_NIGHT = 0x30,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700959 SHIFT_UI_MODE_NIGHT = 4,
960 UI_MODE_NIGHT_ANY = ACONFIGURATION_UI_MODE_NIGHT_ANY << SHIFT_UI_MODE_NIGHT,
961 UI_MODE_NIGHT_NO = ACONFIGURATION_UI_MODE_NIGHT_NO << SHIFT_UI_MODE_NIGHT,
962 UI_MODE_NIGHT_YES = ACONFIGURATION_UI_MODE_NIGHT_YES << SHIFT_UI_MODE_NIGHT,
Tobias Haamel27b28b32010-02-09 23:09:17 +0100963 };
964
Dianne Hackborn723738c2009-06-25 19:48:04 -0700965 union {
966 struct {
967 uint8_t screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100968 uint8_t uiMode;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700969 uint8_t screenConfigPad1;
970 uint8_t screenConfigPad2;
971 };
972 uint32_t screenConfig;
973 };
974
Dianne Hackborn3fc982f2011-03-30 16:20:26 -0700975 union {
976 struct {
977 uint16_t screenWidthDp;
978 uint16_t screenHeightDp;
979 };
980 uint32_t screenSizeDp;
981 };
982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 inline void copyFromDeviceNoSwap(const ResTable_config& o) {
984 const size_t size = dtohl(o.size);
985 if (size >= sizeof(ResTable_config)) {
986 *this = o;
987 } else {
988 memcpy(this, &o, size);
989 memset(((uint8_t*)this)+size, 0, sizeof(ResTable_config)-size);
990 }
991 }
992
993 inline void copyFromDtoH(const ResTable_config& o) {
994 copyFromDeviceNoSwap(o);
995 size = sizeof(ResTable_config);
996 mcc = dtohs(mcc);
997 mnc = dtohs(mnc);
998 density = dtohs(density);
999 screenWidth = dtohs(screenWidth);
1000 screenHeight = dtohs(screenHeight);
1001 sdkVersion = dtohs(sdkVersion);
1002 minorVersion = dtohs(minorVersion);
Dianne Hackborn3fc982f2011-03-30 16:20:26 -07001003 screenWidthDp = dtohs(screenWidthDp);
1004 screenHeightDp = dtohs(screenHeightDp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 }
1006
1007 inline void swapHtoD() {
1008 size = htodl(size);
1009 mcc = htods(mcc);
1010 mnc = htods(mnc);
1011 density = htods(density);
1012 screenWidth = htods(screenWidth);
1013 screenHeight = htods(screenHeight);
1014 sdkVersion = htods(sdkVersion);
1015 minorVersion = htods(minorVersion);
Dianne Hackborn3fc982f2011-03-30 16:20:26 -07001016 screenWidthDp = htods(screenWidthDp);
1017 screenHeightDp = htods(screenHeightDp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
1019
1020 inline int compare(const ResTable_config& o) const {
1021 int32_t diff = (int32_t)(imsi - o.imsi);
1022 if (diff != 0) return diff;
1023 diff = (int32_t)(locale - o.locale);
1024 if (diff != 0) return diff;
1025 diff = (int32_t)(screenType - o.screenType);
1026 if (diff != 0) return diff;
1027 diff = (int32_t)(input - o.input);
1028 if (diff != 0) return diff;
1029 diff = (int32_t)(screenSize - o.screenSize);
1030 if (diff != 0) return diff;
1031 diff = (int32_t)(version - o.version);
Dianne Hackborn723738c2009-06-25 19:48:04 -07001032 if (diff != 0) return diff;
1033 diff = (int32_t)(screenLayout - o.screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +01001034 if (diff != 0) return diff;
1035 diff = (int32_t)(uiMode - o.uiMode);
Dianne Hackborn3fc982f2011-03-30 16:20:26 -07001036 if (diff != 0) return diff;
1037 diff = (int32_t)(screenSizeDp - o.screenSizeDp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 return (int)diff;
1039 }
1040
1041 // Flags indicating a set of config values. These flag constants must
1042 // match the corresponding ones in android.content.pm.ActivityInfo and
1043 // attrs_manifest.xml.
1044 enum {
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001045 CONFIG_MCC = ACONFIGURATION_MCC,
1046 CONFIG_MNC = ACONFIGURATION_MCC,
1047 CONFIG_LOCALE = ACONFIGURATION_LOCALE,
1048 CONFIG_TOUCHSCREEN = ACONFIGURATION_TOUCHSCREEN,
1049 CONFIG_KEYBOARD = ACONFIGURATION_KEYBOARD,
1050 CONFIG_KEYBOARD_HIDDEN = ACONFIGURATION_KEYBOARD_HIDDEN,
1051 CONFIG_NAVIGATION = ACONFIGURATION_NAVIGATION,
1052 CONFIG_ORIENTATION = ACONFIGURATION_ORIENTATION,
1053 CONFIG_DENSITY = ACONFIGURATION_DENSITY,
1054 CONFIG_SCREEN_SIZE = ACONFIGURATION_SCREEN_SIZE,
1055 CONFIG_VERSION = ACONFIGURATION_VERSION,
1056 CONFIG_SCREEN_LAYOUT = ACONFIGURATION_SCREEN_LAYOUT,
1057 CONFIG_UI_MODE = ACONFIGURATION_UI_MODE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 };
1059
1060 // Compare two configuration, returning CONFIG_* flags set for each value
1061 // that is different.
1062 inline int diff(const ResTable_config& o) const {
1063 int diffs = 0;
1064 if (mcc != o.mcc) diffs |= CONFIG_MCC;
1065 if (mnc != o.mnc) diffs |= CONFIG_MNC;
1066 if (locale != o.locale) diffs |= CONFIG_LOCALE;
1067 if (orientation != o.orientation) diffs |= CONFIG_ORIENTATION;
1068 if (density != o.density) diffs |= CONFIG_DENSITY;
1069 if (touchscreen != o.touchscreen) diffs |= CONFIG_TOUCHSCREEN;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07001070 if (((inputFlags^o.inputFlags)&(MASK_KEYSHIDDEN|MASK_NAVHIDDEN)) != 0)
1071 diffs |= CONFIG_KEYBOARD_HIDDEN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 if (keyboard != o.keyboard) diffs |= CONFIG_KEYBOARD;
1073 if (navigation != o.navigation) diffs |= CONFIG_NAVIGATION;
1074 if (screenSize != o.screenSize) diffs |= CONFIG_SCREEN_SIZE;
1075 if (version != o.version) diffs |= CONFIG_VERSION;
Dianne Hackborn723738c2009-06-25 19:48:04 -07001076 if (screenLayout != o.screenLayout) diffs |= CONFIG_SCREEN_LAYOUT;
Tobias Haamel27b28b32010-02-09 23:09:17 +01001077 if (uiMode != o.uiMode) diffs |= CONFIG_UI_MODE;
Dianne Hackborn3fc982f2011-03-30 16:20:26 -07001078 if (screenSizeDp != o.screenSizeDp) diffs |= CONFIG_SCREEN_SIZE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 return diffs;
1080 }
1081
Robert Greenwalt96e20402009-04-22 14:35:11 -07001082 // Return true if 'this' is more specific than 'o'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 inline bool
Robert Greenwalt96e20402009-04-22 14:35:11 -07001084 isMoreSpecificThan(const ResTable_config& o) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 // The order of the following tests defines the importance of one
1086 // configuration parameter over another. Those tests first are more
1087 // important, trumping any values in those following them.
Robert Greenwalt96e20402009-04-22 14:35:11 -07001088 if (imsi || o.imsi) {
1089 if (mcc != o.mcc) {
1090 if (!mcc) return false;
1091 if (!o.mcc) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001093
1094 if (mnc != o.mnc) {
1095 if (!mnc) return false;
1096 if (!o.mnc) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
1098 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001099
1100 if (locale || o.locale) {
1101 if (language[0] != o.language[0]) {
1102 if (!language[0]) return false;
1103 if (!o.language[0]) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001105
1106 if (country[0] != o.country[0]) {
1107 if (!country[0]) return false;
1108 if (!o.country[0]) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
1110 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001111
Dianne Hackbornef05e072010-03-01 17:43:39 -08001112 if (screenLayout || o.screenLayout) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001113 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0) {
1114 if (!(screenLayout & MASK_SCREENSIZE)) return false;
1115 if (!(o.screenLayout & MASK_SCREENSIZE)) return true;
1116 }
1117 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0) {
1118 if (!(screenLayout & MASK_SCREENLONG)) return false;
1119 if (!(o.screenLayout & MASK_SCREENLONG)) return true;
1120 }
1121 }
1122
Dianne Hackborn3fc982f2011-03-30 16:20:26 -07001123 if (screenSizeDp || o.screenSizeDp) {
1124 if (screenWidthDp != o.screenWidthDp) {
1125 if (!screenWidthDp) return false;
1126 if (!o.screenWidthDp) return true;
1127 }
1128
1129 if (screenHeightDp != o.screenHeightDp) {
1130 if (!screenHeightDp) return false;
1131 if (!o.screenHeightDp) return true;
1132 }
1133 }
1134
Tobias Haamel27b28b32010-02-09 23:09:17 +01001135 if (orientation != o.orientation) {
1136 if (!orientation) return false;
1137 if (!o.orientation) return true;
1138 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001139
Dianne Hackbornef05e072010-03-01 17:43:39 -08001140 if (uiMode || o.uiMode) {
Tobias Haamel27b28b32010-02-09 23:09:17 +01001141 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0) {
1142 if (!(uiMode & MASK_UI_MODE_TYPE)) return false;
1143 if (!(o.uiMode & MASK_UI_MODE_TYPE)) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
Tobias Haamel27b28b32010-02-09 23:09:17 +01001145 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0) {
1146 if (!(uiMode & MASK_UI_MODE_NIGHT)) return false;
1147 if (!(o.uiMode & MASK_UI_MODE_NIGHT)) return true;
1148 }
1149 }
1150
1151 // density is never 'more specific'
1152 // as the default just equals 160
1153
1154 if (touchscreen != o.touchscreen) {
1155 if (!touchscreen) return false;
1156 if (!o.touchscreen) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001158
1159 if (input || o.input) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001160 if (((inputFlags^o.inputFlags) & MASK_KEYSHIDDEN) != 0) {
Robert Greenwalt96e20402009-04-22 14:35:11 -07001161 if (!(inputFlags & MASK_KEYSHIDDEN)) return false;
1162 if (!(o.inputFlags & MASK_KEYSHIDDEN)) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001164
Dianne Hackborn93e462b2009-09-15 22:50:40 -07001165 if (((inputFlags^o.inputFlags) & MASK_NAVHIDDEN) != 0) {
1166 if (!(inputFlags & MASK_NAVHIDDEN)) return false;
1167 if (!(o.inputFlags & MASK_NAVHIDDEN)) return true;
1168 }
1169
Robert Greenwalt96e20402009-04-22 14:35:11 -07001170 if (keyboard != o.keyboard) {
1171 if (!keyboard) return false;
1172 if (!o.keyboard) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001174
1175 if (navigation != o.navigation) {
1176 if (!navigation) return false;
1177 if (!o.navigation) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
1179 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001180
1181 if (screenSize || o.screenSize) {
1182 if (screenWidth != o.screenWidth) {
1183 if (!screenWidth) return false;
1184 if (!o.screenWidth) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001186
1187 if (screenHeight != o.screenHeight) {
1188 if (!screenHeight) return false;
1189 if (!o.screenHeight) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
1191 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001192
1193 if (version || o.version) {
1194 if (sdkVersion != o.sdkVersion) {
1195 if (!sdkVersion) return false;
1196 if (!o.sdkVersion) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001198
1199 if (minorVersion != o.minorVersion) {
1200 if (!minorVersion) return false;
1201 if (!o.minorVersion) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203 }
1204 return false;
1205 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001206
1207 // Return true if 'this' is a better match than 'o' for the 'requested'
1208 // configuration. This assumes that match() has already been used to
1209 // remove any configurations that don't match the requested configuration
1210 // at all; if they are not first filtered, non-matching results can be
1211 // considered better than matching ones.
1212 // The general rule per attribute: if the request cares about an attribute
1213 // (it normally does), if the two (this and o) are equal it's a tie. If
1214 // they are not equal then one must be generic because only generic and
1215 // '==requested' will pass the match() call. So if this is not generic,
1216 // it wins. If this IS generic, o wins (return false).
1217 inline bool
1218 isBetterThan(const ResTable_config& o,
1219 const ResTable_config* requested) const {
1220 if (requested) {
1221 if (imsi || o.imsi) {
1222 if ((mcc != o.mcc) && requested->mcc) {
1223 return (mcc);
1224 }
1225
1226 if ((mnc != o.mnc) && requested->mnc) {
1227 return (mnc);
1228 }
1229 }
1230
1231 if (locale || o.locale) {
1232 if ((language[0] != o.language[0]) && requested->language[0]) {
1233 return (language[0]);
1234 }
1235
1236 if ((country[0] != o.country[0]) && requested->country[0]) {
1237 return (country[0]);
1238 }
1239 }
1240
Dianne Hackbornef05e072010-03-01 17:43:39 -08001241 if (screenLayout || o.screenLayout) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001242 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0
1243 && (requested->screenLayout & MASK_SCREENSIZE)) {
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001244 // A little backwards compatibility here: undefined is
1245 // considered equivalent to normal. But only if the
1246 // requested size is at least normal; otherwise, small
1247 // is better than the default.
1248 int mySL = (screenLayout & MASK_SCREENSIZE);
1249 int oSL = (o.screenLayout & MASK_SCREENSIZE);
1250 int fixedMySL = mySL;
1251 int fixedOSL = oSL;
1252 if ((requested->screenLayout & MASK_SCREENSIZE) >= SCREENSIZE_NORMAL) {
1253 if (fixedMySL == 0) fixedMySL = SCREENSIZE_NORMAL;
1254 if (fixedOSL == 0) fixedOSL = SCREENSIZE_NORMAL;
1255 }
1256 // For screen size, the best match is the one that is
1257 // closest to the requested screen size, but not over
1258 // (the not over part is dealt with in match() below).
1259 if (fixedMySL == fixedOSL) {
1260 // If the two are the same, but 'this' is actually
1261 // undefined, then the other is really a better match.
1262 if (mySL == 0) return false;
1263 return true;
1264 }
1265 return fixedMySL >= fixedOSL;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001266 }
1267 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0
1268 && (requested->screenLayout & MASK_SCREENLONG)) {
1269 return (screenLayout & MASK_SCREENLONG);
1270 }
1271 }
1272
Dianne Hackborn3fc982f2011-03-30 16:20:26 -07001273 if (screenSizeDp || o.screenSizeDp) {
1274 // Better is based on the sum of the difference between both
1275 // width and height from the requested dimensions. We are
1276 // assuming the invalid configs (with smaller dimens) have
1277 // already been filtered. Note that if a particular dimension
1278 // is unspecified, we will end up with a large value (the
1279 // difference between 0 and the requested dimension), which is
1280 // good since we will prefer a config that has specified a
1281 // dimension value.
1282 int myDelta = 0, otherDelta = 0;
1283 if (requested->screenWidthDp) {
1284 myDelta += requested->screenWidthDp - screenWidthDp;
1285 otherDelta += requested->screenWidthDp - o.screenWidthDp;
1286 }
1287 if (requested->screenHeightDp) {
1288 myDelta += requested->screenHeightDp - screenHeightDp;
1289 otherDelta += requested->screenHeightDp - o.screenHeightDp;
1290 }
1291 //LOGI("Comparing this %dx%d to other %dx%d in %dx%d: myDelta=%d otherDelta=%d",
1292 // screenWidthDp, screenHeightDp, o.screenWidthDp, o.screenHeightDp,
1293 // requested->screenWidthDp, requested->screenHeightDp, myDelta, otherDelta);
1294 return (myDelta <= otherDelta);
1295 }
1296
Tobias Haamel27b28b32010-02-09 23:09:17 +01001297 if ((orientation != o.orientation) && requested->orientation) {
1298 return (orientation);
1299 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001300
Dianne Hackbornef05e072010-03-01 17:43:39 -08001301 if (uiMode || o.uiMode) {
Tobias Haamel27b28b32010-02-09 23:09:17 +01001302 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0
1303 && (requested->uiMode & MASK_UI_MODE_TYPE)) {
1304 return (uiMode & MASK_UI_MODE_TYPE);
1305 }
1306 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0
1307 && (requested->uiMode & MASK_UI_MODE_NIGHT)) {
1308 return (uiMode & MASK_UI_MODE_NIGHT);
1309 }
1310 }
1311
1312 if (screenType || o.screenType) {
Robert Greenwalt96e20402009-04-22 14:35:11 -07001313 if (density != o.density) {
1314 // density is tough. Any density is potentially useful
1315 // because the system will scale it. Scaling down
1316 // is generally better than scaling up.
1317 // Default density counts as 160dpi (the system default)
1318 // TODO - remove 160 constants
1319 int h = (density?density:160);
1320 int l = (o.density?o.density:160);
1321 bool bImBigger = true;
1322 if (l > h) {
1323 int t = h;
1324 h = l;
1325 l = t;
1326 bImBigger = false;
1327 }
1328
1329 int reqValue = (requested->density?requested->density:160);
1330 if (reqValue >= h) {
1331 // requested value higher than both l and h, give h
1332 return bImBigger;
1333 }
1334 if (l >= reqValue) {
1335 // requested value lower than both l and h, give l
1336 return !bImBigger;
1337 }
1338 // saying that scaling down is 2x better than up
1339 if (((2 * l) - reqValue) * h > reqValue * reqValue) {
1340 return !bImBigger;
1341 } else {
1342 return bImBigger;
1343 }
1344 }
1345
1346 if ((touchscreen != o.touchscreen) && requested->touchscreen) {
1347 return (touchscreen);
1348 }
1349 }
1350
1351 if (input || o.input) {
1352 const int keysHidden = inputFlags & MASK_KEYSHIDDEN;
1353 const int oKeysHidden = o.inputFlags & MASK_KEYSHIDDEN;
1354 if (keysHidden != oKeysHidden) {
1355 const int reqKeysHidden =
1356 requested->inputFlags & MASK_KEYSHIDDEN;
1357 if (reqKeysHidden) {
1358
1359 if (!keysHidden) return false;
1360 if (!oKeysHidden) return true;
1361 // For compatibility, we count KEYSHIDDEN_NO as being
1362 // the same as KEYSHIDDEN_SOFT. Here we disambiguate
1363 // these by making an exact match more specific.
1364 if (reqKeysHidden == keysHidden) return true;
1365 if (reqKeysHidden == oKeysHidden) return false;
1366 }
1367 }
1368
Dianne Hackborn93e462b2009-09-15 22:50:40 -07001369 const int navHidden = inputFlags & MASK_NAVHIDDEN;
1370 const int oNavHidden = o.inputFlags & MASK_NAVHIDDEN;
1371 if (navHidden != oNavHidden) {
1372 const int reqNavHidden =
1373 requested->inputFlags & MASK_NAVHIDDEN;
1374 if (reqNavHidden) {
1375
1376 if (!navHidden) return false;
1377 if (!oNavHidden) return true;
1378 }
1379 }
1380
Robert Greenwalt96e20402009-04-22 14:35:11 -07001381 if ((keyboard != o.keyboard) && requested->keyboard) {
1382 return (keyboard);
1383 }
1384
1385 if ((navigation != o.navigation) && requested->navigation) {
1386 return (navigation);
1387 }
1388 }
1389
1390 if (screenSize || o.screenSize) {
1391 if ((screenWidth != o.screenWidth) && requested->screenWidth) {
1392 return (screenWidth);
1393 }
1394
1395 if ((screenHeight != o.screenHeight) &&
1396 requested->screenHeight) {
1397 return (screenHeight);
1398 }
1399 }
1400
1401 if (version || o.version) {
1402 if ((sdkVersion != o.sdkVersion) && requested->sdkVersion) {
Dianne Hackborn55339952009-11-01 21:16:59 -08001403 return (sdkVersion > o.sdkVersion);
Robert Greenwalt96e20402009-04-22 14:35:11 -07001404 }
1405
1406 if ((minorVersion != o.minorVersion) &&
1407 requested->minorVersion) {
1408 return (minorVersion);
1409 }
1410 }
1411
1412 return false;
1413 }
1414 return isMoreSpecificThan(o);
1415 }
1416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 // Return true if 'this' can be considered a match for the parameters in
1418 // 'settings'.
1419 // Note this is asymetric. A default piece of data will match every request
1420 // but a request for the default should not match odd specifics
1421 // (ie, request with no mcc should not match a particular mcc's data)
1422 // settings is the requested settings
1423 inline bool match(const ResTable_config& settings) const {
1424 if (imsi != 0) {
1425 if ((settings.mcc != 0 && mcc != 0
1426 && mcc != settings.mcc) ||
1427 (settings.mcc == 0 && mcc != 0)) {
1428 return false;
1429 }
1430 if ((settings.mnc != 0 && mnc != 0
1431 && mnc != settings.mnc) ||
1432 (settings.mnc == 0 && mnc != 0)) {
1433 return false;
1434 }
1435 }
1436 if (locale != 0) {
1437 if (settings.language[0] != 0 && language[0] != 0
1438 && (language[0] != settings.language[0]
1439 || language[1] != settings.language[1])) {
1440 return false;
1441 }
1442 if (settings.country[0] != 0 && country[0] != 0
1443 && (country[0] != settings.country[0]
1444 || country[1] != settings.country[1])) {
1445 return false;
1446 }
1447 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001448 if (screenConfig != 0) {
1449 const int screenSize = screenLayout&MASK_SCREENSIZE;
1450 const int setScreenSize = settings.screenLayout&MASK_SCREENSIZE;
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001451 // Any screen sizes for larger screens than the setting do not
1452 // match.
1453 if ((setScreenSize != 0 && screenSize != 0
1454 && screenSize > setScreenSize) ||
1455 (setScreenSize == 0 && screenSize != 0)) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001456 return false;
1457 }
1458
1459 const int screenLong = screenLayout&MASK_SCREENLONG;
1460 const int setScreenLong = settings.screenLayout&MASK_SCREENLONG;
1461 if (setScreenLong != 0 && screenLong != 0
1462 && screenLong != setScreenLong) {
1463 return false;
1464 }
Tobias Haamel27b28b32010-02-09 23:09:17 +01001465
1466 const int uiModeType = uiMode&MASK_UI_MODE_TYPE;
1467 const int setUiModeType = settings.uiMode&MASK_UI_MODE_TYPE;
1468 if (setUiModeType != 0 && uiModeType != 0
1469 && uiModeType != setUiModeType) {
1470 return false;
1471 }
1472
1473 const int uiModeNight = uiMode&MASK_UI_MODE_NIGHT;
1474 const int setUiModeNight = settings.uiMode&MASK_UI_MODE_NIGHT;
1475 if (setUiModeNight != 0 && uiModeNight != 0
1476 && uiModeNight != setUiModeNight) {
1477 return false;
1478 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001479 }
Dianne Hackborn3fc982f2011-03-30 16:20:26 -07001480 if (screenSizeDp != 0) {
1481 if (settings.screenWidthDp != 0 && screenWidthDp != 0
1482 && screenWidthDp > settings.screenWidthDp) {
1483 //LOGI("Filtering out width %d in requested %d", screenWidthDp, settings.screenWidthDp);
1484 return false;
1485 }
1486 if (settings.screenHeightDp != 0 && screenHeightDp != 0
1487 && screenHeightDp > settings.screenHeightDp) {
1488 //LOGI("Filtering out height %d in requested %d", screenHeightDp, settings.screenHeightDp);
1489 return false;
1490 }
1491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 if (screenType != 0) {
1493 if (settings.orientation != 0 && orientation != 0
1494 && orientation != settings.orientation) {
1495 return false;
1496 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001497 // density always matches - we can scale it. See isBetterThan
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 if (settings.touchscreen != 0 && touchscreen != 0
1499 && touchscreen != settings.touchscreen) {
1500 return false;
1501 }
1502 }
1503 if (input != 0) {
1504 const int keysHidden = inputFlags&MASK_KEYSHIDDEN;
1505 const int setKeysHidden = settings.inputFlags&MASK_KEYSHIDDEN;
1506 if (setKeysHidden != 0 && keysHidden != 0
1507 && keysHidden != setKeysHidden) {
1508 // For compatibility, we count a request for KEYSHIDDEN_NO as also
1509 // matching the more recent KEYSHIDDEN_SOFT. Basically
1510 // KEYSHIDDEN_NO means there is some kind of keyboard available.
1511 //LOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden);
1512 if (keysHidden != KEYSHIDDEN_NO || setKeysHidden != KEYSHIDDEN_SOFT) {
1513 //LOGI("No match!");
1514 return false;
1515 }
1516 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07001517 const int navHidden = inputFlags&MASK_NAVHIDDEN;
1518 const int setNavHidden = settings.inputFlags&MASK_NAVHIDDEN;
1519 if (setNavHidden != 0 && navHidden != 0
1520 && navHidden != setNavHidden) {
1521 return false;
1522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 if (settings.keyboard != 0 && keyboard != 0
1524 && keyboard != settings.keyboard) {
1525 return false;
1526 }
1527 if (settings.navigation != 0 && navigation != 0
1528 && navigation != settings.navigation) {
1529 return false;
1530 }
1531 }
1532 if (screenSize != 0) {
1533 if (settings.screenWidth != 0 && screenWidth != 0
1534 && screenWidth != settings.screenWidth) {
1535 return false;
1536 }
1537 if (settings.screenHeight != 0 && screenHeight != 0
1538 && screenHeight != settings.screenHeight) {
1539 return false;
1540 }
1541 }
1542 if (version != 0) {
1543 if (settings.sdkVersion != 0 && sdkVersion != 0
Dianne Hackborn55339952009-11-01 21:16:59 -08001544 && sdkVersion > settings.sdkVersion) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 return false;
1546 }
1547 if (settings.minorVersion != 0 && minorVersion != 0
1548 && minorVersion != settings.minorVersion) {
1549 return false;
1550 }
1551 }
1552 return true;
1553 }
1554
1555 void getLocale(char str[6]) const {
1556 memset(str, 0, 6);
1557 if (language[0]) {
1558 str[0] = language[0];
1559 str[1] = language[1];
1560 if (country[0]) {
1561 str[2] = '_';
1562 str[3] = country[0];
1563 str[4] = country[1];
1564 }
1565 }
1566 }
1567
1568 String8 toString() const {
1569 char buf[200];
Dianne Hackborn723738c2009-06-25 19:48:04 -07001570 sprintf(buf, "imsi=%d/%d lang=%c%c reg=%c%c orient=%d touch=%d dens=%d "
Dianne Hackborn3fc982f2011-03-30 16:20:26 -07001571 "kbd=%d nav=%d input=%d ssz=%dx%d %ddp x %ddp sz=%d long=%d "
Tobias Haamel27b28b32010-02-09 23:09:17 +01001572 "ui=%d night=%d vers=%d.%d",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 mcc, mnc,
1574 language[0] ? language[0] : '-', language[1] ? language[1] : '-',
1575 country[0] ? country[0] : '-', country[1] ? country[1] : '-',
1576 orientation, touchscreen, density, keyboard, navigation, inputFlags,
Dianne Hackborn3fc982f2011-03-30 16:20:26 -07001577 screenWidth, screenHeight, screenWidthDp, screenHeightDp,
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001578 screenLayout&MASK_SCREENSIZE, screenLayout&MASK_SCREENLONG,
Tobias Haamel27b28b32010-02-09 23:09:17 +01001579 uiMode&MASK_UI_MODE_TYPE, uiMode&MASK_UI_MODE_NIGHT,
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001580 sdkVersion, minorVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 return String8(buf);
1582 }
1583};
1584
1585/**
1586 * A specification of the resources defined by a particular type.
1587 *
1588 * There should be one of these chunks for each resource type.
1589 *
1590 * This structure is followed by an array of integers providing the set of
1591 * configuation change flags (ResTable_config::CONFIG_*) that have multiple
1592 * resources for that configuration. In addition, the high bit is set if that
1593 * resource has been made public.
1594 */
1595struct ResTable_typeSpec
1596{
1597 struct ResChunk_header header;
1598
1599 // The type identifier this chunk is holding. Type IDs start
1600 // at 1 (corresponding to the value of the type bits in a
1601 // resource identifier). 0 is invalid.
1602 uint8_t id;
1603
1604 // Must be 0.
1605 uint8_t res0;
1606 // Must be 0.
1607 uint16_t res1;
1608
1609 // Number of uint32_t entry configuration masks that follow.
1610 uint32_t entryCount;
1611
1612 enum {
1613 // Additional flag indicating an entry is public.
1614 SPEC_PUBLIC = 0x40000000
1615 };
1616};
1617
1618/**
1619 * A collection of resource entries for a particular resource data
1620 * type. Followed by an array of uint32_t defining the resource
1621 * values, corresponding to the array of type strings in the
1622 * ResTable_package::typeStrings string block. Each of these hold an
1623 * index from entriesStart; a value of NO_ENTRY means that entry is
1624 * not defined.
1625 *
1626 * There may be multiple of these chunks for a particular resource type,
1627 * supply different configuration variations for the resource values of
1628 * that type.
1629 *
1630 * It would be nice to have an additional ordered index of entries, so
1631 * we can do a binary search if trying to find a resource by string name.
1632 */
1633struct ResTable_type
1634{
1635 struct ResChunk_header header;
1636
1637 enum {
1638 NO_ENTRY = 0xFFFFFFFF
1639 };
1640
1641 // The type identifier this chunk is holding. Type IDs start
1642 // at 1 (corresponding to the value of the type bits in a
1643 // resource identifier). 0 is invalid.
1644 uint8_t id;
1645
1646 // Must be 0.
1647 uint8_t res0;
1648 // Must be 0.
1649 uint16_t res1;
1650
1651 // Number of uint32_t entry indices that follow.
1652 uint32_t entryCount;
1653
1654 // Offset from header where ResTable_entry data starts.
1655 uint32_t entriesStart;
1656
1657 // Configuration this collection of entries is designed for.
1658 ResTable_config config;
1659};
1660
1661/**
1662 * This is the beginning of information about an entry in the resource
1663 * table. It holds the reference to the name of this entry, and is
1664 * immediately followed by one of:
Dianne Hackbornde7faf62009-06-30 13:27:30 -07001665 * * A Res_value structure, if FLAG_COMPLEX is -not- set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 * * An array of ResTable_map structures, if FLAG_COMPLEX is set.
1667 * These supply a set of name/value mappings of data.
1668 */
1669struct ResTable_entry
1670{
1671 // Number of bytes in this structure.
1672 uint16_t size;
1673
1674 enum {
1675 // If set, this is a complex entry, holding a set of name/value
1676 // mappings. It is followed by an array of ResTable_map structures.
1677 FLAG_COMPLEX = 0x0001,
1678 // If set, this resource has been declared public, so libraries
1679 // are allowed to reference it.
1680 FLAG_PUBLIC = 0x0002
1681 };
1682 uint16_t flags;
1683
1684 // Reference into ResTable_package::keyStrings identifying this entry.
1685 struct ResStringPool_ref key;
1686};
1687
1688/**
1689 * Extended form of a ResTable_entry for map entries, defining a parent map
1690 * resource from which to inherit values.
1691 */
1692struct ResTable_map_entry : public ResTable_entry
1693{
1694 // Resource identifier of the parent mapping, or 0 if there is none.
1695 ResTable_ref parent;
1696 // Number of name/value pairs that follow for FLAG_COMPLEX.
1697 uint32_t count;
1698};
1699
1700/**
1701 * A single name/value mapping that is part of a complex resource
1702 * entry.
1703 */
1704struct ResTable_map
1705{
1706 // The resource identifier defining this mapping's name. For attribute
1707 // resources, 'name' can be one of the following special resource types
1708 // to supply meta-data about the attribute; for all other resource types
1709 // it must be an attribute resource.
1710 ResTable_ref name;
1711
1712 // Special values for 'name' when defining attribute resources.
1713 enum {
1714 // This entry holds the attribute's type code.
1715 ATTR_TYPE = Res_MAKEINTERNAL(0),
1716
1717 // For integral attributes, this is the minimum value it can hold.
1718 ATTR_MIN = Res_MAKEINTERNAL(1),
1719
1720 // For integral attributes, this is the maximum value it can hold.
1721 ATTR_MAX = Res_MAKEINTERNAL(2),
1722
1723 // Localization of this resource is can be encouraged or required with
1724 // an aapt flag if this is set
1725 ATTR_L10N = Res_MAKEINTERNAL(3),
1726
1727 // for plural support, see android.content.res.PluralRules#attrForQuantity(int)
1728 ATTR_OTHER = Res_MAKEINTERNAL(4),
1729 ATTR_ZERO = Res_MAKEINTERNAL(5),
1730 ATTR_ONE = Res_MAKEINTERNAL(6),
1731 ATTR_TWO = Res_MAKEINTERNAL(7),
1732 ATTR_FEW = Res_MAKEINTERNAL(8),
1733 ATTR_MANY = Res_MAKEINTERNAL(9)
1734
1735 };
1736
1737 // Bit mask of allowed types, for use with ATTR_TYPE.
1738 enum {
1739 // No type has been defined for this attribute, use generic
1740 // type handling. The low 16 bits are for types that can be
1741 // handled generically; the upper 16 require additional information
1742 // in the bag so can not be handled generically for TYPE_ANY.
1743 TYPE_ANY = 0x0000FFFF,
1744
1745 // Attribute holds a references to another resource.
1746 TYPE_REFERENCE = 1<<0,
1747
1748 // Attribute holds a generic string.
1749 TYPE_STRING = 1<<1,
1750
1751 // Attribute holds an integer value. ATTR_MIN and ATTR_MIN can
1752 // optionally specify a constrained range of possible integer values.
1753 TYPE_INTEGER = 1<<2,
1754
1755 // Attribute holds a boolean integer.
1756 TYPE_BOOLEAN = 1<<3,
1757
1758 // Attribute holds a color value.
1759 TYPE_COLOR = 1<<4,
1760
1761 // Attribute holds a floating point value.
1762 TYPE_FLOAT = 1<<5,
1763
1764 // Attribute holds a dimension value, such as "20px".
1765 TYPE_DIMENSION = 1<<6,
1766
1767 // Attribute holds a fraction value, such as "20%".
1768 TYPE_FRACTION = 1<<7,
1769
1770 // Attribute holds an enumeration. The enumeration values are
1771 // supplied as additional entries in the map.
1772 TYPE_ENUM = 1<<16,
1773
1774 // Attribute holds a bitmaks of flags. The flag bit values are
1775 // supplied as additional entries in the map.
1776 TYPE_FLAGS = 1<<17
1777 };
1778
1779 // Enum of localization modes, for use with ATTR_L10N.
1780 enum {
1781 L10N_NOT_REQUIRED = 0,
1782 L10N_SUGGESTED = 1
1783 };
1784
1785 // This mapping's value.
1786 Res_value value;
1787};
1788
1789/**
1790 * Convenience class for accessing data in a ResTable resource.
1791 */
1792class ResTable
1793{
1794public:
1795 ResTable();
1796 ResTable(const void* data, size_t size, void* cookie,
1797 bool copyData=false);
1798 ~ResTable();
1799
1800 status_t add(const void* data, size_t size, void* cookie,
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001801 bool copyData=false, const void* idmap = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 status_t add(Asset* asset, void* cookie,
Mårten Kongstad57f4b772011-03-17 14:13:41 +01001803 bool copyData=false, const void* idmap = NULL);
Dianne Hackborn78c40512009-07-06 11:07:40 -07001804 status_t add(ResTable* src);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805
1806 status_t getError() const;
1807
1808 void uninit();
1809
1810 struct resource_name
1811 {
1812 const char16_t* package;
1813 size_t packageLen;
1814 const char16_t* type;
1815 size_t typeLen;
1816 const char16_t* name;
1817 size_t nameLen;
1818 };
1819
1820 bool getResourceName(uint32_t resID, resource_name* outName) const;
1821
1822 /**
1823 * Retrieve the value of a resource. If the resource is found, returns a
1824 * value >= 0 indicating the table it is in (for use with
1825 * getTableStringBlock() and getTableCookie()) and fills in 'outValue'. If
1826 * not found, returns a negative error code.
1827 *
1828 * Note that this function does not do reference traversal. If you want
1829 * to follow references to other resources to get the "real" value to
1830 * use, you need to call resolveReference() after this function.
1831 *
1832 * @param resID The desired resoruce identifier.
1833 * @param outValue Filled in with the resource data that was found.
1834 *
1835 * @return ssize_t Either a >= 0 table index or a negative error code.
1836 */
Kenny Root55fc8502010-10-28 14:47:01 -07001837 ssize_t getResource(uint32_t resID, Res_value* outValue, bool mayBeBag = false,
1838 uint16_t density = 0,
1839 uint32_t* outSpecFlags = NULL,
1840 ResTable_config* outConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841
1842 inline ssize_t getResource(const ResTable_ref& res, Res_value* outValue,
1843 uint32_t* outSpecFlags=NULL) const {
Kenny Root55fc8502010-10-28 14:47:01 -07001844 return getResource(res.ident, outValue, false, 0, outSpecFlags, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 }
1846
1847 ssize_t resolveReference(Res_value* inOutValue,
1848 ssize_t blockIndex,
1849 uint32_t* outLastRef = NULL,
Dianne Hackborn0d221012009-07-29 15:41:19 -07001850 uint32_t* inoutTypeSpecFlags = NULL,
1851 ResTable_config* outConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852
1853 enum {
1854 TMP_BUFFER_SIZE = 16
1855 };
1856 const char16_t* valueToString(const Res_value* value, size_t stringBlock,
1857 char16_t tmpBuffer[TMP_BUFFER_SIZE],
1858 size_t* outLen);
1859
1860 struct bag_entry {
1861 ssize_t stringBlock;
1862 ResTable_map map;
1863 };
1864
1865 /**
1866 * Retrieve the bag of a resource. If the resoruce is found, returns the
1867 * number of bags it contains and 'outBag' points to an array of their
1868 * values. If not found, a negative error code is returned.
1869 *
1870 * Note that this function -does- do reference traversal of the bag data.
1871 *
1872 * @param resID The desired resource identifier.
1873 * @param outBag Filled inm with a pointer to the bag mappings.
1874 *
1875 * @return ssize_t Either a >= 0 bag count of negative error code.
1876 */
1877 ssize_t lockBag(uint32_t resID, const bag_entry** outBag) const;
1878
1879 void unlockBag(const bag_entry* bag) const;
1880
1881 void lock() const;
1882
1883 ssize_t getBagLocked(uint32_t resID, const bag_entry** outBag,
1884 uint32_t* outTypeSpecFlags=NULL) const;
1885
1886 void unlock() const;
1887
1888 class Theme {
1889 public:
1890 Theme(const ResTable& table);
1891 ~Theme();
1892
1893 inline const ResTable& getResTable() const { return mTable; }
1894
1895 status_t applyStyle(uint32_t resID, bool force=false);
1896 status_t setTo(const Theme& other);
1897
1898 /**
1899 * Retrieve a value in the theme. If the theme defines this
1900 * value, returns a value >= 0 indicating the table it is in
1901 * (for use with getTableStringBlock() and getTableCookie) and
1902 * fills in 'outValue'. If not found, returns a negative error
1903 * code.
1904 *
1905 * Note that this function does not do reference traversal. If you want
1906 * to follow references to other resources to get the "real" value to
1907 * use, you need to call resolveReference() after this function.
1908 *
1909 * @param resID A resource identifier naming the desired theme
1910 * attribute.
1911 * @param outValue Filled in with the theme value that was
1912 * found.
1913 *
1914 * @return ssize_t Either a >= 0 table index or a negative error code.
1915 */
1916 ssize_t getAttribute(uint32_t resID, Res_value* outValue,
1917 uint32_t* outTypeSpecFlags = NULL) const;
1918
1919 /**
1920 * This is like ResTable::resolveReference(), but also takes
1921 * care of resolving attribute references to the theme.
1922 */
1923 ssize_t resolveAttributeReference(Res_value* inOutValue,
1924 ssize_t blockIndex, uint32_t* outLastRef = NULL,
Dianne Hackborn0d221012009-07-29 15:41:19 -07001925 uint32_t* inoutTypeSpecFlags = NULL,
1926 ResTable_config* inoutConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927
1928 void dumpToLog() const;
1929
1930 private:
1931 Theme(const Theme&);
1932 Theme& operator=(const Theme&);
1933
1934 struct theme_entry {
1935 ssize_t stringBlock;
1936 uint32_t typeSpecFlags;
1937 Res_value value;
1938 };
1939 struct type_info {
1940 size_t numEntries;
1941 theme_entry* entries;
1942 };
1943 struct package_info {
1944 size_t numTypes;
1945 type_info types[];
1946 };
1947
1948 void free_package(package_info* pi);
1949 package_info* copy_package(package_info* pi);
1950
1951 const ResTable& mTable;
1952 package_info* mPackages[Res_MAXPACKAGE];
1953 };
1954
1955 void setParameters(const ResTable_config* params);
1956 void getParameters(ResTable_config* params) const;
1957
1958 // Retrieve an identifier (which can be passed to getResource)
1959 // for a given resource name. The 'name' can be fully qualified
1960 // (<package>:<type>.<basename>) or the package or type components
1961 // can be dropped if default values are supplied here.
1962 //
1963 // Returns 0 if no such resource was found, else a valid resource ID.
1964 uint32_t identifierForName(const char16_t* name, size_t nameLen,
1965 const char16_t* type = 0, size_t typeLen = 0,
1966 const char16_t* defPackage = 0,
1967 size_t defPackageLen = 0,
1968 uint32_t* outTypeSpecFlags = NULL) const;
1969
1970 static bool expandResourceRef(const uint16_t* refStr, size_t refLen,
1971 String16* outPackage,
1972 String16* outType,
1973 String16* outName,
1974 const String16* defType = NULL,
1975 const String16* defPackage = NULL,
1976 const char** outErrorMsg = NULL);
1977
1978 static bool stringToInt(const char16_t* s, size_t len, Res_value* outValue);
1979 static bool stringToFloat(const char16_t* s, size_t len, Res_value* outValue);
1980
1981 // Used with stringToValue.
1982 class Accessor
1983 {
1984 public:
1985 inline virtual ~Accessor() { }
1986
1987 virtual uint32_t getCustomResource(const String16& package,
1988 const String16& type,
1989 const String16& name) const = 0;
1990 virtual uint32_t getCustomResourceWithCreation(const String16& package,
1991 const String16& type,
1992 const String16& name,
1993 const bool createIfNeeded = false) = 0;
1994 virtual uint32_t getRemappedPackage(uint32_t origPackage) const = 0;
1995 virtual bool getAttributeType(uint32_t attrID, uint32_t* outType) = 0;
1996 virtual bool getAttributeMin(uint32_t attrID, uint32_t* outMin) = 0;
1997 virtual bool getAttributeMax(uint32_t attrID, uint32_t* outMax) = 0;
1998 virtual bool getAttributeEnum(uint32_t attrID,
1999 const char16_t* name, size_t nameLen,
2000 Res_value* outValue) = 0;
2001 virtual bool getAttributeFlags(uint32_t attrID,
2002 const char16_t* name, size_t nameLen,
2003 Res_value* outValue) = 0;
2004 virtual uint32_t getAttributeL10N(uint32_t attrID) = 0;
2005 virtual bool getLocalizationSetting() = 0;
2006 virtual void reportError(void* accessorCookie, const char* fmt, ...) = 0;
2007 };
2008
2009 // Convert a string to a resource value. Handles standard "@res",
2010 // "#color", "123", and "0x1bd" types; performs escaping of strings.
2011 // The resulting value is placed in 'outValue'; if it is a string type,
2012 // 'outString' receives the string. If 'attrID' is supplied, the value is
2013 // type checked against this attribute and it is used to perform enum
2014 // evaluation. If 'acccessor' is supplied, it will be used to attempt to
2015 // resolve resources that do not exist in this ResTable. If 'attrType' is
2016 // supplied, the value will be type checked for this format if 'attrID'
2017 // is not supplied or found.
2018 bool stringToValue(Res_value* outValue, String16* outString,
2019 const char16_t* s, size_t len,
2020 bool preserveSpaces, bool coerceType,
2021 uint32_t attrID = 0,
2022 const String16* defType = NULL,
2023 const String16* defPackage = NULL,
2024 Accessor* accessor = NULL,
2025 void* accessorCookie = NULL,
2026 uint32_t attrType = ResTable_map::TYPE_ANY,
2027 bool enforcePrivate = true) const;
2028
2029 // Perform processing of escapes and quotes in a string.
2030 static bool collectString(String16* outString,
2031 const char16_t* s, size_t len,
2032 bool preserveSpaces,
2033 const char** outErrorMsg = NULL,
2034 bool append = false);
2035
2036 size_t getBasePackageCount() const;
2037 const char16_t* getBasePackageName(size_t idx) const;
2038 uint32_t getBasePackageId(size_t idx) const;
2039
2040 size_t getTableCount() const;
2041 const ResStringPool* getTableStringBlock(size_t index) const;
2042 void* getTableCookie(size_t index) const;
2043
2044 // Return the configurations (ResTable_config) that we know about
2045 void getConfigurations(Vector<ResTable_config>* configs) const;
2046
2047 void getLocales(Vector<String8>* locales) const;
2048
Mårten Kongstad57f4b772011-03-17 14:13:41 +01002049 // Generate an idmap.
2050 //
2051 // Return value: on success: NO_ERROR; caller is responsible for free-ing
2052 // outData (using free(3)). On failure, any status_t value other than
2053 // NO_ERROR; the caller should not free outData.
2054 status_t createIdmap(const ResTable& overlay, uint32_t originalCrc, uint32_t overlayCrc,
2055 void** outData, size_t* outSize) const;
2056
2057 enum {
2058 IDMAP_HEADER_SIZE_BYTES = 3 * sizeof(uint32_t),
2059 };
2060 // Retrieve idmap meta-data.
2061 //
2062 // This function only requires the idmap header (the first
2063 // IDMAP_HEADER_SIZE_BYTES) bytes of an idmap file.
2064 static bool getIdmapInfo(const void* idmap, size_t size,
2065 uint32_t* pOriginalCrc, uint32_t* pOverlayCrc);
2066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067#ifndef HAVE_ANDROID_OS
Dianne Hackborne17086b2009-06-19 15:13:28 -07002068 void print(bool inclValues) const;
Shachar Shemesh9872bf42010-12-20 17:38:33 +02002069 static String8 normalizeForOutput(const char* input);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070#endif
2071
2072private:
2073 struct Header;
2074 struct Type;
2075 struct Package;
2076 struct PackageGroup;
2077 struct bag_set;
2078
2079 status_t add(const void* data, size_t size, void* cookie,
Mårten Kongstad57f4b772011-03-17 14:13:41 +01002080 Asset* asset, bool copyData, const Asset* idmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081
2082 ssize_t getResourcePackageIndex(uint32_t resID) const;
2083 ssize_t getEntry(
2084 const Package* package, int typeIndex, int entryIndex,
2085 const ResTable_config* config,
2086 const ResTable_type** outType, const ResTable_entry** outEntry,
2087 const Type** outTypeClass) const;
2088 status_t parsePackage(
Mårten Kongstad57f4b772011-03-17 14:13:41 +01002089 const ResTable_package* const pkg, const Header* const header, uint32_t idmap_id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090
Dianne Hackbornde7faf62009-06-30 13:27:30 -07002091 void print_value(const Package* pkg, const Res_value& value) const;
2092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 mutable Mutex mLock;
2094
2095 status_t mError;
2096
2097 ResTable_config mParams;
2098
2099 // Array of all resource tables.
2100 Vector<Header*> mHeaders;
2101
2102 // Array of packages in all resource tables.
2103 Vector<PackageGroup*> mPackageGroups;
2104
2105 // Mapping from resource package IDs to indices into the internal
2106 // package array.
2107 uint8_t mPackageMap[256];
2108};
2109
2110} // namespace android
2111
2112#endif // _LIBS_UTILS_RESOURCE_TYPES_H