blob: cd657e8311f178307bb9ae2ed569305714b407e8 [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
34namespace android {
35
36/** ********************************************************************
37 * PNG Extensions
38 *
39 * New private chunks that may be placed in PNG images.
40 *
41 *********************************************************************** */
42
43/**
44 * This chunk specifies how to split an image into segments for
45 * scaling.
46 *
47 * There are J horizontal and K vertical segments. These segments divide
48 * the image into J*K regions as follows (where J=4 and K=3):
49 *
50 * F0 S0 F1 S1
51 * +-----+----+------+-------+
52 * S2| 0 | 1 | 2 | 3 |
53 * +-----+----+------+-------+
54 * | | | | |
55 * | | | | |
56 * F2| 4 | 5 | 6 | 7 |
57 * | | | | |
58 * | | | | |
59 * +-----+----+------+-------+
60 * S3| 8 | 9 | 10 | 11 |
61 * +-----+----+------+-------+
62 *
63 * Each horizontal and vertical segment is considered to by either
64 * stretchable (marked by the Sx labels) or fixed (marked by the Fy
65 * labels), in the horizontal or vertical axis, respectively. In the
66 * above example, the first is horizontal segment (F0) is fixed, the
67 * next is stretchable and then they continue to alternate. Note that
68 * the segment list for each axis can begin or end with a stretchable
69 * or fixed segment.
70 *
71 * The relative sizes of the stretchy segments indicates the relative
72 * amount of stretchiness of the regions bordered by the segments. For
73 * example, regions 3, 7 and 11 above will take up more horizontal space
Mathias Agopian5f910972009-06-22 02:35:32 -070074 * than regions 1, 5 and 9 since the horizontal segment associated with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * the first set of regions is larger than the other set of regions. The
76 * ratios of the amount of horizontal (or vertical) space taken by any
77 * two stretchable slices is exactly the ratio of their corresponding
78 * segment lengths.
79 *
80 * xDivs and yDivs point to arrays of horizontal and vertical pixel
81 * indices. The first pair of Divs (in either array) indicate the
82 * starting and ending points of the first stretchable segment in that
83 * axis. The next pair specifies the next stretchable segment, etc. So
84 * in the above example xDiv[0] and xDiv[1] specify the horizontal
85 * coordinates for the regions labeled 1, 5 and 9. xDiv[2] and
86 * xDiv[3] specify the coordinates for regions 3, 7 and 11. Note that
87 * the leftmost slices always start at x=0 and the rightmost slices
88 * always end at the end of the image. So, for example, the regions 0,
89 * 4 and 8 (which are fixed along the X axis) start at x value 0 and
Mathias Agopian5f910972009-06-22 02:35:32 -070090 * 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 -080091 * xDiv[2].
92 *
93 * The array pointed to by the colors field lists contains hints for
94 * each of the regions. They are ordered according left-to-right and
95 * top-to-bottom as indicated above. For each segment that is a solid
96 * color the array entry will contain that color value; otherwise it
97 * will contain NO_COLOR. Segments that are completely transparent
98 * will always have the value TRANSPARENT_COLOR.
99 *
100 * The PNG chunk type is "npTc".
101 */
102struct Res_png_9patch
103{
104 Res_png_9patch() : wasDeserialized(false), xDivs(NULL),
105 yDivs(NULL), colors(NULL) { }
106
107 int8_t wasDeserialized;
108 int8_t numXDivs;
109 int8_t numYDivs;
110 int8_t numColors;
111
112 // These tell where the next section of a patch starts.
113 // For example, the first patch includes the pixels from
114 // 0 to xDivs[0]-1 and the second patch includes the pixels
115 // from xDivs[0] to xDivs[1]-1.
116 // Note: allocation/free of these pointers is left to the caller.
117 int32_t* xDivs;
118 int32_t* yDivs;
119
120 int32_t paddingLeft, paddingRight;
121 int32_t paddingTop, paddingBottom;
122
123 enum {
124 // The 9 patch segment is not a solid color.
125 NO_COLOR = 0x00000001,
126
127 // The 9 patch segment is completely transparent.
128 TRANSPARENT_COLOR = 0x00000000
129 };
130 // Note: allocation/free of this pointer is left to the caller.
131 uint32_t* colors;
132
133 // Convert data from device representation to PNG file representation.
134 void deviceToFile();
135 // Convert data from PNG file representation to device representation.
136 void fileToDevice();
137 // Serialize/Marshall the patch data into a newly malloc-ed block
138 void* serialize();
139 // Serialize/Marshall the patch data
140 void serialize(void* outData);
141 // Deserialize/Unmarshall the patch data
142 static Res_png_9patch* deserialize(const void* data);
143 // Compute the size of the serialized data structure
144 size_t serializedSize();
145};
146
147/** ********************************************************************
148 * Base Types
149 *
150 * These are standard types that are shared between multiple specific
151 * resource types.
152 *
153 *********************************************************************** */
154
155/**
156 * Header that appears at the front of every data chunk in a resource.
157 */
158struct ResChunk_header
159{
160 // Type identifier for this chunk. The meaning of this value depends
161 // on the containing chunk.
162 uint16_t type;
163
164 // Size of the chunk header (in bytes). Adding this value to
165 // the address of the chunk allows you to find its associated data
166 // (if any).
167 uint16_t headerSize;
168
169 // Total size of this chunk (in bytes). This is the chunkSize plus
170 // the size of any data associated with the chunk. Adding this value
171 // to the chunk allows you to completely skip its contents (including
172 // any child chunks). If this value is the same as chunkSize, there is
173 // no data associated with the chunk.
174 uint32_t size;
175};
176
177enum {
178 RES_NULL_TYPE = 0x0000,
179 RES_STRING_POOL_TYPE = 0x0001,
180 RES_TABLE_TYPE = 0x0002,
181 RES_XML_TYPE = 0x0003,
182
183 // Chunk types in RES_XML_TYPE
184 RES_XML_FIRST_CHUNK_TYPE = 0x0100,
185 RES_XML_START_NAMESPACE_TYPE= 0x0100,
186 RES_XML_END_NAMESPACE_TYPE = 0x0101,
187 RES_XML_START_ELEMENT_TYPE = 0x0102,
188 RES_XML_END_ELEMENT_TYPE = 0x0103,
189 RES_XML_CDATA_TYPE = 0x0104,
190 RES_XML_LAST_CHUNK_TYPE = 0x017f,
191 // This contains a uint32_t array mapping strings in the string
192 // pool back to resource identifiers. It is optional.
193 RES_XML_RESOURCE_MAP_TYPE = 0x0180,
194
195 // Chunk types in RES_TABLE_TYPE
196 RES_TABLE_PACKAGE_TYPE = 0x0200,
197 RES_TABLE_TYPE_TYPE = 0x0201,
198 RES_TABLE_TYPE_SPEC_TYPE = 0x0202
199};
200
201/**
202 * Macros for building/splitting resource identifiers.
203 */
204#define Res_VALIDID(resid) (resid != 0)
205#define Res_CHECKID(resid) ((resid&0xFFFF0000) != 0)
206#define Res_MAKEID(package, type, entry) \
207 (((package+1)<<24) | (((type+1)&0xFF)<<16) | (entry&0xFFFF))
208#define Res_GETPACKAGE(id) ((id>>24)-1)
209#define Res_GETTYPE(id) (((id>>16)&0xFF)-1)
210#define Res_GETENTRY(id) (id&0xFFFF)
211
212#define Res_INTERNALID(resid) ((resid&0xFFFF0000) != 0 && (resid&0xFF0000) == 0)
213#define Res_MAKEINTERNAL(entry) (0x01000000 | (entry&0xFFFF))
214#define Res_MAKEARRAY(entry) (0x02000000 | (entry&0xFFFF))
215
216#define Res_MAXPACKAGE 255
217
218/**
219 * Representation of a value in a resource, supplying type
220 * information.
221 */
222struct Res_value
223{
224 // Number of bytes in this structure.
225 uint16_t size;
226
227 // Always set to 0.
228 uint8_t res0;
229
230 // Type of the data value.
231 enum {
232 // Contains no data.
233 TYPE_NULL = 0x00,
234 // The 'data' holds a ResTable_ref, a reference to another resource
235 // table entry.
236 TYPE_REFERENCE = 0x01,
237 // The 'data' holds an attribute resource identifier.
238 TYPE_ATTRIBUTE = 0x02,
239 // The 'data' holds an index into the containing resource table's
240 // global value string pool.
241 TYPE_STRING = 0x03,
242 // The 'data' holds a single-precision floating point number.
243 TYPE_FLOAT = 0x04,
244 // The 'data' holds a complex number encoding a dimension value,
245 // such as "100in".
246 TYPE_DIMENSION = 0x05,
247 // The 'data' holds a complex number encoding a fraction of a
248 // container.
249 TYPE_FRACTION = 0x06,
250
251 // Beginning of integer flavors...
252 TYPE_FIRST_INT = 0x10,
253
254 // The 'data' is a raw integer value of the form n..n.
255 TYPE_INT_DEC = 0x10,
256 // The 'data' is a raw integer value of the form 0xn..n.
257 TYPE_INT_HEX = 0x11,
258 // The 'data' is either 0 or 1, for input "false" or "true" respectively.
259 TYPE_INT_BOOLEAN = 0x12,
260
261 // Beginning of color integer flavors...
262 TYPE_FIRST_COLOR_INT = 0x1c,
263
264 // The 'data' is a raw integer value of the form #aarrggbb.
265 TYPE_INT_COLOR_ARGB8 = 0x1c,
266 // The 'data' is a raw integer value of the form #rrggbb.
267 TYPE_INT_COLOR_RGB8 = 0x1d,
268 // The 'data' is a raw integer value of the form #argb.
269 TYPE_INT_COLOR_ARGB4 = 0x1e,
270 // The 'data' is a raw integer value of the form #rgb.
271 TYPE_INT_COLOR_RGB4 = 0x1f,
272
273 // ...end of integer flavors.
274 TYPE_LAST_COLOR_INT = 0x1f,
275
276 // ...end of integer flavors.
277 TYPE_LAST_INT = 0x1f
278 };
279 uint8_t dataType;
280
281 // Structure of complex data values (TYPE_UNIT and TYPE_FRACTION)
282 enum {
283 // Where the unit type information is. This gives us 16 possible
284 // types, as defined below.
285 COMPLEX_UNIT_SHIFT = 0,
286 COMPLEX_UNIT_MASK = 0xf,
287
288 // TYPE_DIMENSION: Value is raw pixels.
289 COMPLEX_UNIT_PX = 0,
290 // TYPE_DIMENSION: Value is Device Independent Pixels.
291 COMPLEX_UNIT_DIP = 1,
292 // TYPE_DIMENSION: Value is a Scaled device independent Pixels.
293 COMPLEX_UNIT_SP = 2,
294 // TYPE_DIMENSION: Value is in points.
295 COMPLEX_UNIT_PT = 3,
296 // TYPE_DIMENSION: Value is in inches.
297 COMPLEX_UNIT_IN = 4,
298 // TYPE_DIMENSION: Value is in millimeters.
299 COMPLEX_UNIT_MM = 5,
300
301 // TYPE_FRACTION: A basic fraction of the overall size.
302 COMPLEX_UNIT_FRACTION = 0,
303 // TYPE_FRACTION: A fraction of the parent size.
304 COMPLEX_UNIT_FRACTION_PARENT = 1,
305
306 // Where the radix information is, telling where the decimal place
307 // appears in the mantissa. This give us 4 possible fixed point
308 // representations as defined below.
309 COMPLEX_RADIX_SHIFT = 4,
310 COMPLEX_RADIX_MASK = 0x3,
311
312 // The mantissa is an integral number -- i.e., 0xnnnnnn.0
313 COMPLEX_RADIX_23p0 = 0,
314 // The mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn
315 COMPLEX_RADIX_16p7 = 1,
316 // The mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn
317 COMPLEX_RADIX_8p15 = 2,
318 // The mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn
319 COMPLEX_RADIX_0p23 = 3,
320
321 // Where the actual value is. This gives us 23 bits of
322 // precision. The top bit is the sign.
323 COMPLEX_MANTISSA_SHIFT = 8,
324 COMPLEX_MANTISSA_MASK = 0xffffff
325 };
326
327 // The data for this item, as interpreted according to dataType.
328 uint32_t data;
329
330 void copyFrom_dtoh(const Res_value& src);
331};
332
333/**
334 * This is a reference to a unique entry (a ResTable_entry structure)
335 * in a resource table. The value is structured as: 0xpptteeee,
336 * where pp is the package index, tt is the type index in that
337 * package, and eeee is the entry index in that type. The package
338 * and type values start at 1 for the first item, to help catch cases
339 * where they have not been supplied.
340 */
341struct ResTable_ref
342{
343 uint32_t ident;
344};
345
346/**
347 * Reference to a string in a string pool.
348 */
349struct ResStringPool_ref
350{
351 // Index into the string pool table (uint32_t-offset from the indices
352 // immediately after ResStringPool_header) at which to find the location
353 // of the string data in the pool.
354 uint32_t index;
355};
356
357/** ********************************************************************
358 * String Pool
359 *
360 * A set of strings that can be references by others through a
361 * ResStringPool_ref.
362 *
363 *********************************************************************** */
364
365/**
366 * Definition for a pool of strings. The data of this chunk is an
367 * array of uint32_t providing indices into the pool, relative to
368 * stringsStart. At stringsStart are all of the UTF-16 strings
369 * concatenated together; each starts with a uint16_t of the string's
370 * length and each ends with a 0x0000 terminator. If a string is >
371 * 32767 characters, the high bit of the length is set meaning to take
372 * those 15 bits as a high word and it will be followed by another
373 * uint16_t containing the low word.
374 *
375 * If styleCount is not zero, then immediately following the array of
376 * uint32_t indices into the string table is another array of indices
377 * into a style table starting at stylesStart. Each entry in the
378 * style table is an array of ResStringPool_span structures.
379 */
380struct ResStringPool_header
381{
382 struct ResChunk_header header;
383
384 // Number of strings in this pool (number of uint32_t indices that follow
385 // in the data).
386 uint32_t stringCount;
387
388 // Number of style span arrays in the pool (number of uint32_t indices
389 // follow the string indices).
390 uint32_t styleCount;
391
392 // Flags.
393 enum {
394 // If set, the string index is sorted by the string values (based
395 // on strcmp16()).
Kenny Root19138462009-12-04 09:38:48 -0800396 SORTED_FLAG = 1<<0,
397
398 // String pool is encoded in UTF-8
399 UTF8_FLAG = 1<<8
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 };
401 uint32_t flags;
402
403 // Index from header of the string data.
404 uint32_t stringsStart;
405
406 // Index from header of the style data.
407 uint32_t stylesStart;
408};
409
410/**
411 * This structure defines a span of style information associated with
412 * a string in the pool.
413 */
414struct ResStringPool_span
415{
416 enum {
417 END = 0xFFFFFFFF
418 };
419
420 // This is the name of the span -- that is, the name of the XML
421 // tag that defined it. The special value END (0xFFFFFFFF) indicates
422 // the end of an array of spans.
423 ResStringPool_ref name;
424
425 // The range of characters in the string that this span applies to.
426 uint32_t firstChar, lastChar;
427};
428
429/**
430 * Convenience class for accessing data in a ResStringPool resource.
431 */
432class ResStringPool
433{
434public:
435 ResStringPool();
436 ResStringPool(const void* data, size_t size, bool copyData=false);
437 ~ResStringPool();
438
439 status_t setTo(const void* data, size_t size, bool copyData=false);
440
441 status_t getError() const;
442
443 void uninit();
444
445 inline const char16_t* stringAt(const ResStringPool_ref& ref, size_t* outLen) const {
446 return stringAt(ref.index, outLen);
447 }
448 const char16_t* stringAt(size_t idx, size_t* outLen) const;
449
Kenny Root780d2a12010-02-22 22:36:26 -0800450 const char* string8At(size_t idx, size_t* outLen) const;
451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 const ResStringPool_span* styleAt(const ResStringPool_ref& ref) const;
453 const ResStringPool_span* styleAt(size_t idx) const;
454
455 ssize_t indexOfString(const char16_t* str, size_t strLen) const;
456
457 size_t size() const;
458
Kenny Rootbb79f642009-12-10 14:20:15 -0800459#ifndef HAVE_ANDROID_OS
460 bool isUTF8() const;
461#endif
462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463private:
464 status_t mError;
465 void* mOwnedData;
466 const ResStringPool_header* mHeader;
467 size_t mSize;
Kenny Root19138462009-12-04 09:38:48 -0800468 mutable Mutex mDecodeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 const uint32_t* mEntries;
470 const uint32_t* mEntryStyles;
Kenny Root19138462009-12-04 09:38:48 -0800471 const void* mStrings;
472 char16_t** mCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 uint32_t mStringPoolSize; // number of uint16_t
474 const uint32_t* mStyles;
475 uint32_t mStylePoolSize; // number of uint32_t
476};
477
478/** ********************************************************************
479 * XML Tree
480 *
481 * Binary representation of an XML document. This is designed to
482 * express everything in an XML document, in a form that is much
483 * easier to parse on the device.
484 *
485 *********************************************************************** */
486
487/**
488 * XML tree header. This appears at the front of an XML tree,
489 * describing its content. It is followed by a flat array of
490 * ResXMLTree_node structures; the hierarchy of the XML document
491 * is described by the occurrance of RES_XML_START_ELEMENT_TYPE
492 * and corresponding RES_XML_END_ELEMENT_TYPE nodes in the array.
493 */
494struct ResXMLTree_header
495{
496 struct ResChunk_header header;
497};
498
499/**
500 * Basic XML tree node. A single item in the XML document. Extended info
501 * about the node can be found after header.headerSize.
502 */
503struct ResXMLTree_node
504{
505 struct ResChunk_header header;
506
507 // Line number in original source file at which this element appeared.
508 uint32_t lineNumber;
509
510 // Optional XML comment that was associated with this element; -1 if none.
511 struct ResStringPool_ref comment;
512};
513
514/**
515 * Extended XML tree node for CDATA tags -- includes the CDATA string.
516 * Appears header.headerSize bytes after a ResXMLTree_node.
517 */
518struct ResXMLTree_cdataExt
519{
520 // The raw CDATA character data.
521 struct ResStringPool_ref data;
522
523 // The typed value of the character data if this is a CDATA node.
524 struct Res_value typedData;
525};
526
527/**
528 * Extended XML tree node for namespace start/end nodes.
529 * Appears header.headerSize bytes after a ResXMLTree_node.
530 */
531struct ResXMLTree_namespaceExt
532{
533 // The prefix of the namespace.
534 struct ResStringPool_ref prefix;
535
536 // The URI of the namespace.
537 struct ResStringPool_ref uri;
538};
539
540/**
541 * Extended XML tree node for element start/end nodes.
542 * Appears header.headerSize bytes after a ResXMLTree_node.
543 */
544struct ResXMLTree_endElementExt
545{
546 // String of the full namespace of this element.
547 struct ResStringPool_ref ns;
548
549 // String name of this node if it is an ELEMENT; the raw
550 // character data if this is a CDATA node.
551 struct ResStringPool_ref name;
552};
553
554/**
555 * Extended XML tree node for start tags -- includes attribute
556 * information.
557 * Appears header.headerSize bytes after a ResXMLTree_node.
558 */
559struct ResXMLTree_attrExt
560{
561 // String of the full namespace of this element.
562 struct ResStringPool_ref ns;
563
564 // String name of this node if it is an ELEMENT; the raw
565 // character data if this is a CDATA node.
566 struct ResStringPool_ref name;
567
568 // Byte offset from the start of this structure where the attributes start.
569 uint16_t attributeStart;
570
571 // Size of the ResXMLTree_attribute structures that follow.
572 uint16_t attributeSize;
573
574 // Number of attributes associated with an ELEMENT. These are
575 // available as an array of ResXMLTree_attribute structures
576 // immediately following this node.
577 uint16_t attributeCount;
578
579 // Index (1-based) of the "id" attribute. 0 if none.
580 uint16_t idIndex;
581
582 // Index (1-based) of the "class" attribute. 0 if none.
583 uint16_t classIndex;
584
585 // Index (1-based) of the "style" attribute. 0 if none.
586 uint16_t styleIndex;
587};
588
589struct ResXMLTree_attribute
590{
591 // Namespace of this attribute.
592 struct ResStringPool_ref ns;
593
594 // Name of this attribute.
595 struct ResStringPool_ref name;
596
597 // The original raw string value of this attribute.
598 struct ResStringPool_ref rawValue;
599
600 // Processesd typed value of this attribute.
601 struct Res_value typedValue;
602};
603
604class ResXMLTree;
605
606class ResXMLParser
607{
608public:
609 ResXMLParser(const ResXMLTree& tree);
610
611 enum event_code_t {
612 BAD_DOCUMENT = -1,
613 START_DOCUMENT = 0,
614 END_DOCUMENT = 1,
615
616 FIRST_CHUNK_CODE = RES_XML_FIRST_CHUNK_TYPE,
617
618 START_NAMESPACE = RES_XML_START_NAMESPACE_TYPE,
619 END_NAMESPACE = RES_XML_END_NAMESPACE_TYPE,
620 START_TAG = RES_XML_START_ELEMENT_TYPE,
621 END_TAG = RES_XML_END_ELEMENT_TYPE,
622 TEXT = RES_XML_CDATA_TYPE
623 };
624
625 struct ResXMLPosition
626 {
627 event_code_t eventCode;
628 const ResXMLTree_node* curNode;
629 const void* curExt;
630 };
631
632 void restart();
633
634 event_code_t getEventType() const;
635 // Note, unlike XmlPullParser, the first call to next() will return
636 // START_TAG of the first element.
637 event_code_t next();
638
639 // These are available for all nodes:
Mathias Agopian5f910972009-06-22 02:35:32 -0700640 int32_t getCommentID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 const uint16_t* getComment(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700642 uint32_t getLineNumber() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643
644 // This is available for TEXT:
Mathias Agopian5f910972009-06-22 02:35:32 -0700645 int32_t getTextID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 const uint16_t* getText(size_t* outLen) const;
647 ssize_t getTextValue(Res_value* outValue) const;
648
649 // These are available for START_NAMESPACE and END_NAMESPACE:
Mathias Agopian5f910972009-06-22 02:35:32 -0700650 int32_t getNamespacePrefixID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 const uint16_t* getNamespacePrefix(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700652 int32_t getNamespaceUriID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 const uint16_t* getNamespaceUri(size_t* outLen) const;
654
655 // These are available for START_TAG and END_TAG:
Mathias Agopian5f910972009-06-22 02:35:32 -0700656 int32_t getElementNamespaceID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 const uint16_t* getElementNamespace(size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700658 int32_t getElementNameID() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 const uint16_t* getElementName(size_t* outLen) const;
660
661 // Remaining methods are for retrieving information about attributes
662 // associated with a START_TAG:
663
664 size_t getAttributeCount() const;
665
666 // Returns -1 if no namespace, -2 if idx out of range.
Mathias Agopian5f910972009-06-22 02:35:32 -0700667 int32_t getAttributeNamespaceID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 const uint16_t* getAttributeNamespace(size_t idx, size_t* outLen) const;
669
Mathias Agopian5f910972009-06-22 02:35:32 -0700670 int32_t getAttributeNameID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 const uint16_t* getAttributeName(size_t idx, size_t* outLen) const;
Mathias Agopian5f910972009-06-22 02:35:32 -0700672 uint32_t getAttributeNameResID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673
Mathias Agopian5f910972009-06-22 02:35:32 -0700674 int32_t getAttributeValueStringID(size_t idx) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 const uint16_t* getAttributeStringValue(size_t idx, size_t* outLen) const;
676
677 int32_t getAttributeDataType(size_t idx) const;
678 int32_t getAttributeData(size_t idx) const;
679 ssize_t getAttributeValue(size_t idx, Res_value* outValue) const;
680
681 ssize_t indexOfAttribute(const char* ns, const char* attr) const;
682 ssize_t indexOfAttribute(const char16_t* ns, size_t nsLen,
683 const char16_t* attr, size_t attrLen) const;
684
685 ssize_t indexOfID() const;
686 ssize_t indexOfClass() const;
687 ssize_t indexOfStyle() const;
688
689 void getPosition(ResXMLPosition* pos) const;
690 void setPosition(const ResXMLPosition& pos);
691
692private:
693 friend class ResXMLTree;
694
695 event_code_t nextNode();
696
697 const ResXMLTree& mTree;
698 event_code_t mEventCode;
699 const ResXMLTree_node* mCurNode;
700 const void* mCurExt;
701};
702
703/**
704 * Convenience class for accessing data in a ResXMLTree resource.
705 */
706class ResXMLTree : public ResXMLParser
707{
708public:
709 ResXMLTree();
710 ResXMLTree(const void* data, size_t size, bool copyData=false);
711 ~ResXMLTree();
712
713 status_t setTo(const void* data, size_t size, bool copyData=false);
714
715 status_t getError() const;
716
717 void uninit();
718
719 const ResStringPool& getStrings() const;
720
721private:
722 friend class ResXMLParser;
723
724 status_t validateNode(const ResXMLTree_node* node) const;
725
726 status_t mError;
727 void* mOwnedData;
728 const ResXMLTree_header* mHeader;
729 size_t mSize;
730 const uint8_t* mDataEnd;
731 ResStringPool mStrings;
732 const uint32_t* mResIds;
733 size_t mNumResIds;
734 const ResXMLTree_node* mRootNode;
735 const void* mRootExt;
736 event_code_t mRootCode;
737};
738
739/** ********************************************************************
740 * RESOURCE TABLE
741 *
742 *********************************************************************** */
743
744/**
745 * Header for a resource table. Its data contains a series of
746 * additional chunks:
747 * * A ResStringPool_header containing all table values.
748 * * One or more ResTable_package chunks.
749 *
750 * Specific entries within a resource table can be uniquely identified
751 * with a single integer as defined by the ResTable_ref structure.
752 */
753struct ResTable_header
754{
755 struct ResChunk_header header;
756
757 // The number of ResTable_package structures.
758 uint32_t packageCount;
759};
760
761/**
762 * A collection of resource data types within a package. Followed by
763 * one or more ResTable_type and ResTable_typeSpec structures containing the
764 * entry values for each resource type.
765 */
766struct ResTable_package
767{
768 struct ResChunk_header header;
769
770 // If this is a base package, its ID. Package IDs start
771 // at 1 (corresponding to the value of the package bits in a
772 // resource identifier). 0 means this is not a base package.
773 uint32_t id;
774
775 // Actual name of this package, \0-terminated.
776 char16_t name[128];
777
778 // Offset to a ResStringPool_header defining the resource
779 // type symbol table. If zero, this package is inheriting from
780 // another base package (overriding specific values in it).
781 uint32_t typeStrings;
782
783 // Last index into typeStrings that is for public use by others.
784 uint32_t lastPublicType;
785
786 // Offset to a ResStringPool_header defining the resource
787 // key symbol table. If zero, this package is inheriting from
788 // another base package (overriding specific values in it).
789 uint32_t keyStrings;
790
791 // Last index into keyStrings that is for public use by others.
792 uint32_t lastPublicKey;
793};
794
795/**
796 * Describes a particular resource configuration.
797 */
798struct ResTable_config
799{
800 // Number of bytes in this structure.
801 uint32_t size;
802
803 union {
804 struct {
805 // Mobile country code (from SIM). 0 means "any".
806 uint16_t mcc;
807 // Mobile network code (from SIM). 0 means "any".
808 uint16_t mnc;
809 };
810 uint32_t imsi;
811 };
812
813 union {
814 struct {
815 // \0\0 means "any". Otherwise, en, fr, etc.
816 char language[2];
817
818 // \0\0 means "any". Otherwise, US, CA, etc.
819 char country[2];
820 };
821 uint32_t locale;
822 };
823
824 enum {
825 ORIENTATION_ANY = 0x0000,
826 ORIENTATION_PORT = 0x0001,
827 ORIENTATION_LAND = 0x0002,
The Android Open Source Project10592532009-03-18 17:39:46 -0700828 ORIENTATION_SQUARE = 0x0003,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 };
830
831 enum {
832 TOUCHSCREEN_ANY = 0x0000,
833 TOUCHSCREEN_NOTOUCH = 0x0001,
834 TOUCHSCREEN_STYLUS = 0x0002,
835 TOUCHSCREEN_FINGER = 0x0003,
836 };
837
838 enum {
Dianne Hackborna53b8282009-07-17 11:13:48 -0700839 DENSITY_DEFAULT = 0,
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700840 DENSITY_LOW = 120,
841 DENSITY_MEDIUM = 160,
842 DENSITY_HIGH = 240,
Dianne Hackborna53b8282009-07-17 11:13:48 -0700843 DENSITY_NONE = 0xffff
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 };
845
846 union {
847 struct {
848 uint8_t orientation;
849 uint8_t touchscreen;
850 uint16_t density;
851 };
852 uint32_t screenType;
853 };
854
855 enum {
856 KEYBOARD_ANY = 0x0000,
857 KEYBOARD_NOKEYS = 0x0001,
858 KEYBOARD_QWERTY = 0x0002,
859 KEYBOARD_12KEY = 0x0003,
860 };
861
862 enum {
863 NAVIGATION_ANY = 0x0000,
864 NAVIGATION_NONAV = 0x0001,
865 NAVIGATION_DPAD = 0x0002,
866 NAVIGATION_TRACKBALL = 0x0003,
867 NAVIGATION_WHEEL = 0x0004,
868 };
869
870 enum {
871 MASK_KEYSHIDDEN = 0x0003,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 KEYSHIDDEN_ANY = 0x0000,
873 KEYSHIDDEN_NO = 0x0001,
874 KEYSHIDDEN_YES = 0x0002,
875 KEYSHIDDEN_SOFT = 0x0003,
876 };
877
Dianne Hackborn93e462b2009-09-15 22:50:40 -0700878 enum {
879 MASK_NAVHIDDEN = 0x000c,
880 NAVHIDDEN_ANY = 0x0000,
881 NAVHIDDEN_NO = 0x0004,
882 NAVHIDDEN_YES = 0x0008,
883 };
884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 union {
886 struct {
887 uint8_t keyboard;
888 uint8_t navigation;
889 uint8_t inputFlags;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700890 uint8_t inputPad0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 };
892 uint32_t input;
893 };
894
895 enum {
896 SCREENWIDTH_ANY = 0
897 };
898
899 enum {
900 SCREENHEIGHT_ANY = 0
901 };
902
903 union {
904 struct {
905 uint16_t screenWidth;
906 uint16_t screenHeight;
907 };
908 uint32_t screenSize;
909 };
910
911 enum {
912 SDKVERSION_ANY = 0
913 };
914
915 enum {
916 MINORVERSION_ANY = 0
917 };
918
919 union {
920 struct {
921 uint16_t sdkVersion;
922 // For now minorVersion must always be 0!!! Its meaning
923 // is currently undefined.
924 uint16_t minorVersion;
925 };
926 uint32_t version;
927 };
928
Dianne Hackborn723738c2009-06-25 19:48:04 -0700929 enum {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700930 // screenLayout bits for screen size class.
931 MASK_SCREENSIZE = 0x0f,
932 SCREENSIZE_ANY = 0x00,
933 SCREENSIZE_SMALL = 0x01,
934 SCREENSIZE_NORMAL = 0x02,
935 SCREENSIZE_LARGE = 0x03,
936
937 // screenLayout bits for wide/long screen variation.
938 MASK_SCREENLONG = 0x30,
939 SCREENLONG_ANY = 0x00,
940 SCREENLONG_NO = 0x10,
941 SCREENLONG_YES = 0x20,
Dianne Hackborn723738c2009-06-25 19:48:04 -0700942 };
943
Tobias Haamel27b28b32010-02-09 23:09:17 +0100944 enum {
945 // uiMode bits for the mode type.
946 MASK_UI_MODE_TYPE = 0x0f,
947 UI_MODE_TYPE_NORMAL = 0x00,
948 UI_MODE_TYPE_CAR = 0x01,
949
950 // uiMode bits for the night switch.
951 MASK_UI_MODE_NIGHT = 0x30,
952 UI_MODE_NIGHT_ANY = 0x00,
953 UI_MODE_NIGHT_NO = 0x10,
954 UI_MODE_NIGHT_YES = 0x20,
955 };
956
Dianne Hackborn723738c2009-06-25 19:48:04 -0700957 union {
958 struct {
959 uint8_t screenLayout;
Tobias Haamel27b28b32010-02-09 23:09:17 +0100960 uint8_t uiMode;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700961 uint8_t screenConfigPad1;
962 uint8_t screenConfigPad2;
963 };
964 uint32_t screenConfig;
965 };
966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 inline void copyFromDeviceNoSwap(const ResTable_config& o) {
968 const size_t size = dtohl(o.size);
969 if (size >= sizeof(ResTable_config)) {
970 *this = o;
971 } else {
972 memcpy(this, &o, size);
973 memset(((uint8_t*)this)+size, 0, sizeof(ResTable_config)-size);
974 }
975 }
976
977 inline void copyFromDtoH(const ResTable_config& o) {
978 copyFromDeviceNoSwap(o);
979 size = sizeof(ResTable_config);
980 mcc = dtohs(mcc);
981 mnc = dtohs(mnc);
982 density = dtohs(density);
983 screenWidth = dtohs(screenWidth);
984 screenHeight = dtohs(screenHeight);
985 sdkVersion = dtohs(sdkVersion);
986 minorVersion = dtohs(minorVersion);
987 }
988
989 inline void swapHtoD() {
990 size = htodl(size);
991 mcc = htods(mcc);
992 mnc = htods(mnc);
993 density = htods(density);
994 screenWidth = htods(screenWidth);
995 screenHeight = htods(screenHeight);
996 sdkVersion = htods(sdkVersion);
997 minorVersion = htods(minorVersion);
998 }
999
1000 inline int compare(const ResTable_config& o) const {
1001 int32_t diff = (int32_t)(imsi - o.imsi);
1002 if (diff != 0) return diff;
1003 diff = (int32_t)(locale - o.locale);
1004 if (diff != 0) return diff;
1005 diff = (int32_t)(screenType - o.screenType);
1006 if (diff != 0) return diff;
1007 diff = (int32_t)(input - o.input);
1008 if (diff != 0) return diff;
1009 diff = (int32_t)(screenSize - o.screenSize);
1010 if (diff != 0) return diff;
1011 diff = (int32_t)(version - o.version);
Dianne Hackborn723738c2009-06-25 19:48:04 -07001012 if (diff != 0) return diff;
1013 diff = (int32_t)(screenLayout - o.screenLayout);
Tobias Haamel27b28b32010-02-09 23:09:17 +01001014 if (diff != 0) return diff;
1015 diff = (int32_t)(uiMode - o.uiMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 return (int)diff;
1017 }
1018
1019 // Flags indicating a set of config values. These flag constants must
1020 // match the corresponding ones in android.content.pm.ActivityInfo and
1021 // attrs_manifest.xml.
1022 enum {
1023 CONFIG_MCC = 0x0001,
1024 CONFIG_MNC = 0x0002,
1025 CONFIG_LOCALE = 0x0004,
1026 CONFIG_TOUCHSCREEN = 0x0008,
1027 CONFIG_KEYBOARD = 0x0010,
1028 CONFIG_KEYBOARD_HIDDEN = 0x0020,
1029 CONFIG_NAVIGATION = 0x0040,
1030 CONFIG_ORIENTATION = 0x0080,
1031 CONFIG_DENSITY = 0x0100,
1032 CONFIG_SCREEN_SIZE = 0x0200,
Dianne Hackborn723738c2009-06-25 19:48:04 -07001033 CONFIG_VERSION = 0x0400,
Tobias Haamel27b28b32010-02-09 23:09:17 +01001034 CONFIG_SCREEN_LAYOUT = 0x0800,
1035 CONFIG_UI_MODE = 0x1000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 };
1037
1038 // Compare two configuration, returning CONFIG_* flags set for each value
1039 // that is different.
1040 inline int diff(const ResTable_config& o) const {
1041 int diffs = 0;
1042 if (mcc != o.mcc) diffs |= CONFIG_MCC;
1043 if (mnc != o.mnc) diffs |= CONFIG_MNC;
1044 if (locale != o.locale) diffs |= CONFIG_LOCALE;
1045 if (orientation != o.orientation) diffs |= CONFIG_ORIENTATION;
1046 if (density != o.density) diffs |= CONFIG_DENSITY;
1047 if (touchscreen != o.touchscreen) diffs |= CONFIG_TOUCHSCREEN;
Dianne Hackborn93e462b2009-09-15 22:50:40 -07001048 if (((inputFlags^o.inputFlags)&(MASK_KEYSHIDDEN|MASK_NAVHIDDEN)) != 0)
1049 diffs |= CONFIG_KEYBOARD_HIDDEN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 if (keyboard != o.keyboard) diffs |= CONFIG_KEYBOARD;
1051 if (navigation != o.navigation) diffs |= CONFIG_NAVIGATION;
1052 if (screenSize != o.screenSize) diffs |= CONFIG_SCREEN_SIZE;
1053 if (version != o.version) diffs |= CONFIG_VERSION;
Dianne Hackborn723738c2009-06-25 19:48:04 -07001054 if (screenLayout != o.screenLayout) diffs |= CONFIG_SCREEN_LAYOUT;
Tobias Haamel27b28b32010-02-09 23:09:17 +01001055 if (uiMode != o.uiMode) diffs |= CONFIG_UI_MODE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 return diffs;
1057 }
1058
Robert Greenwalt96e20402009-04-22 14:35:11 -07001059 // Return true if 'this' is more specific than 'o'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 inline bool
Robert Greenwalt96e20402009-04-22 14:35:11 -07001061 isMoreSpecificThan(const ResTable_config& o) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 // The order of the following tests defines the importance of one
1063 // configuration parameter over another. Those tests first are more
1064 // important, trumping any values in those following them.
Robert Greenwalt96e20402009-04-22 14:35:11 -07001065 if (imsi || o.imsi) {
1066 if (mcc != o.mcc) {
1067 if (!mcc) return false;
1068 if (!o.mcc) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001070
1071 if (mnc != o.mnc) {
1072 if (!mnc) return false;
1073 if (!o.mnc) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
1075 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001076
1077 if (locale || o.locale) {
1078 if (language[0] != o.language[0]) {
1079 if (!language[0]) return false;
1080 if (!o.language[0]) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001082
1083 if (country[0] != o.country[0]) {
1084 if (!country[0]) return false;
1085 if (!o.country[0]) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
1087 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001088
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001089 if (screenConfig || o.screenConfig) {
1090 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0) {
1091 if (!(screenLayout & MASK_SCREENSIZE)) return false;
1092 if (!(o.screenLayout & MASK_SCREENSIZE)) return true;
1093 }
1094 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0) {
1095 if (!(screenLayout & MASK_SCREENLONG)) return false;
1096 if (!(o.screenLayout & MASK_SCREENLONG)) return true;
1097 }
1098 }
1099
Tobias Haamel27b28b32010-02-09 23:09:17 +01001100 if (orientation != o.orientation) {
1101 if (!orientation) return false;
1102 if (!o.orientation) return true;
1103 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001104
Tobias Haamel27b28b32010-02-09 23:09:17 +01001105 if (screenConfig || o.screenConfig) {
1106 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0) {
1107 if (!(uiMode & MASK_UI_MODE_TYPE)) return false;
1108 if (!(o.uiMode & MASK_UI_MODE_TYPE)) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
Tobias Haamel27b28b32010-02-09 23:09:17 +01001110 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0) {
1111 if (!(uiMode & MASK_UI_MODE_NIGHT)) return false;
1112 if (!(o.uiMode & MASK_UI_MODE_NIGHT)) return true;
1113 }
1114 }
1115
1116 // density is never 'more specific'
1117 // as the default just equals 160
1118
1119 if (touchscreen != o.touchscreen) {
1120 if (!touchscreen) return false;
1121 if (!o.touchscreen) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001123
1124 if (input || o.input) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001125 if (((inputFlags^o.inputFlags) & MASK_KEYSHIDDEN) != 0) {
Robert Greenwalt96e20402009-04-22 14:35:11 -07001126 if (!(inputFlags & MASK_KEYSHIDDEN)) return false;
1127 if (!(o.inputFlags & MASK_KEYSHIDDEN)) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001129
Dianne Hackborn93e462b2009-09-15 22:50:40 -07001130 if (((inputFlags^o.inputFlags) & MASK_NAVHIDDEN) != 0) {
1131 if (!(inputFlags & MASK_NAVHIDDEN)) return false;
1132 if (!(o.inputFlags & MASK_NAVHIDDEN)) return true;
1133 }
1134
Robert Greenwalt96e20402009-04-22 14:35:11 -07001135 if (keyboard != o.keyboard) {
1136 if (!keyboard) return false;
1137 if (!o.keyboard) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001139
1140 if (navigation != o.navigation) {
1141 if (!navigation) return false;
1142 if (!o.navigation) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
1144 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001145
1146 if (screenSize || o.screenSize) {
1147 if (screenWidth != o.screenWidth) {
1148 if (!screenWidth) return false;
1149 if (!o.screenWidth) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001151
1152 if (screenHeight != o.screenHeight) {
1153 if (!screenHeight) return false;
1154 if (!o.screenHeight) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 }
1156 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001157
1158 if (version || o.version) {
1159 if (sdkVersion != o.sdkVersion) {
1160 if (!sdkVersion) return false;
1161 if (!o.sdkVersion) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001163
1164 if (minorVersion != o.minorVersion) {
1165 if (!minorVersion) return false;
1166 if (!o.minorVersion) return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 }
1168 }
1169 return false;
1170 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001171
1172 // Return true if 'this' is a better match than 'o' for the 'requested'
1173 // configuration. This assumes that match() has already been used to
1174 // remove any configurations that don't match the requested configuration
1175 // at all; if they are not first filtered, non-matching results can be
1176 // considered better than matching ones.
1177 // The general rule per attribute: if the request cares about an attribute
1178 // (it normally does), if the two (this and o) are equal it's a tie. If
1179 // they are not equal then one must be generic because only generic and
1180 // '==requested' will pass the match() call. So if this is not generic,
1181 // it wins. If this IS generic, o wins (return false).
1182 inline bool
1183 isBetterThan(const ResTable_config& o,
1184 const ResTable_config* requested) const {
1185 if (requested) {
1186 if (imsi || o.imsi) {
1187 if ((mcc != o.mcc) && requested->mcc) {
1188 return (mcc);
1189 }
1190
1191 if ((mnc != o.mnc) && requested->mnc) {
1192 return (mnc);
1193 }
1194 }
1195
1196 if (locale || o.locale) {
1197 if ((language[0] != o.language[0]) && requested->language[0]) {
1198 return (language[0]);
1199 }
1200
1201 if ((country[0] != o.country[0]) && requested->country[0]) {
1202 return (country[0]);
1203 }
1204 }
1205
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001206 if (screenConfig || o.screenConfig) {
1207 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0
1208 && (requested->screenLayout & MASK_SCREENSIZE)) {
1209 return (screenLayout & MASK_SCREENSIZE);
1210 }
1211 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0
1212 && (requested->screenLayout & MASK_SCREENLONG)) {
1213 return (screenLayout & MASK_SCREENLONG);
1214 }
1215 }
1216
Tobias Haamel27b28b32010-02-09 23:09:17 +01001217 if ((orientation != o.orientation) && requested->orientation) {
1218 return (orientation);
1219 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001220
Tobias Haamel27b28b32010-02-09 23:09:17 +01001221 if (screenConfig || o.screenConfig) {
1222 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0
1223 && (requested->uiMode & MASK_UI_MODE_TYPE)) {
1224 return (uiMode & MASK_UI_MODE_TYPE);
1225 }
1226 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0
1227 && (requested->uiMode & MASK_UI_MODE_NIGHT)) {
1228 return (uiMode & MASK_UI_MODE_NIGHT);
1229 }
1230 }
1231
1232 if (screenType || o.screenType) {
Robert Greenwalt96e20402009-04-22 14:35:11 -07001233 if (density != o.density) {
1234 // density is tough. Any density is potentially useful
1235 // because the system will scale it. Scaling down
1236 // is generally better than scaling up.
1237 // Default density counts as 160dpi (the system default)
1238 // TODO - remove 160 constants
1239 int h = (density?density:160);
1240 int l = (o.density?o.density:160);
1241 bool bImBigger = true;
1242 if (l > h) {
1243 int t = h;
1244 h = l;
1245 l = t;
1246 bImBigger = false;
1247 }
1248
1249 int reqValue = (requested->density?requested->density:160);
1250 if (reqValue >= h) {
1251 // requested value higher than both l and h, give h
1252 return bImBigger;
1253 }
1254 if (l >= reqValue) {
1255 // requested value lower than both l and h, give l
1256 return !bImBigger;
1257 }
1258 // saying that scaling down is 2x better than up
1259 if (((2 * l) - reqValue) * h > reqValue * reqValue) {
1260 return !bImBigger;
1261 } else {
1262 return bImBigger;
1263 }
1264 }
1265
1266 if ((touchscreen != o.touchscreen) && requested->touchscreen) {
1267 return (touchscreen);
1268 }
1269 }
1270
1271 if (input || o.input) {
1272 const int keysHidden = inputFlags & MASK_KEYSHIDDEN;
1273 const int oKeysHidden = o.inputFlags & MASK_KEYSHIDDEN;
1274 if (keysHidden != oKeysHidden) {
1275 const int reqKeysHidden =
1276 requested->inputFlags & MASK_KEYSHIDDEN;
1277 if (reqKeysHidden) {
1278
1279 if (!keysHidden) return false;
1280 if (!oKeysHidden) return true;
1281 // For compatibility, we count KEYSHIDDEN_NO as being
1282 // the same as KEYSHIDDEN_SOFT. Here we disambiguate
1283 // these by making an exact match more specific.
1284 if (reqKeysHidden == keysHidden) return true;
1285 if (reqKeysHidden == oKeysHidden) return false;
1286 }
1287 }
1288
Dianne Hackborn93e462b2009-09-15 22:50:40 -07001289 const int navHidden = inputFlags & MASK_NAVHIDDEN;
1290 const int oNavHidden = o.inputFlags & MASK_NAVHIDDEN;
1291 if (navHidden != oNavHidden) {
1292 const int reqNavHidden =
1293 requested->inputFlags & MASK_NAVHIDDEN;
1294 if (reqNavHidden) {
1295
1296 if (!navHidden) return false;
1297 if (!oNavHidden) return true;
1298 }
1299 }
1300
Robert Greenwalt96e20402009-04-22 14:35:11 -07001301 if ((keyboard != o.keyboard) && requested->keyboard) {
1302 return (keyboard);
1303 }
1304
1305 if ((navigation != o.navigation) && requested->navigation) {
1306 return (navigation);
1307 }
1308 }
1309
1310 if (screenSize || o.screenSize) {
1311 if ((screenWidth != o.screenWidth) && requested->screenWidth) {
1312 return (screenWidth);
1313 }
1314
1315 if ((screenHeight != o.screenHeight) &&
1316 requested->screenHeight) {
1317 return (screenHeight);
1318 }
1319 }
1320
1321 if (version || o.version) {
1322 if ((sdkVersion != o.sdkVersion) && requested->sdkVersion) {
Dianne Hackborn55339952009-11-01 21:16:59 -08001323 return (sdkVersion > o.sdkVersion);
Robert Greenwalt96e20402009-04-22 14:35:11 -07001324 }
1325
1326 if ((minorVersion != o.minorVersion) &&
1327 requested->minorVersion) {
1328 return (minorVersion);
1329 }
1330 }
1331
1332 return false;
1333 }
1334 return isMoreSpecificThan(o);
1335 }
1336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 // Return true if 'this' can be considered a match for the parameters in
1338 // 'settings'.
1339 // Note this is asymetric. A default piece of data will match every request
1340 // but a request for the default should not match odd specifics
1341 // (ie, request with no mcc should not match a particular mcc's data)
1342 // settings is the requested settings
1343 inline bool match(const ResTable_config& settings) const {
1344 if (imsi != 0) {
1345 if ((settings.mcc != 0 && mcc != 0
1346 && mcc != settings.mcc) ||
1347 (settings.mcc == 0 && mcc != 0)) {
1348 return false;
1349 }
1350 if ((settings.mnc != 0 && mnc != 0
1351 && mnc != settings.mnc) ||
1352 (settings.mnc == 0 && mnc != 0)) {
1353 return false;
1354 }
1355 }
1356 if (locale != 0) {
1357 if (settings.language[0] != 0 && language[0] != 0
1358 && (language[0] != settings.language[0]
1359 || language[1] != settings.language[1])) {
1360 return false;
1361 }
1362 if (settings.country[0] != 0 && country[0] != 0
1363 && (country[0] != settings.country[0]
1364 || country[1] != settings.country[1])) {
1365 return false;
1366 }
1367 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001368 if (screenConfig != 0) {
1369 const int screenSize = screenLayout&MASK_SCREENSIZE;
1370 const int setScreenSize = settings.screenLayout&MASK_SCREENSIZE;
1371 if (setScreenSize != 0 && screenSize != 0
1372 && screenSize != setScreenSize) {
1373 return false;
1374 }
1375
1376 const int screenLong = screenLayout&MASK_SCREENLONG;
1377 const int setScreenLong = settings.screenLayout&MASK_SCREENLONG;
1378 if (setScreenLong != 0 && screenLong != 0
1379 && screenLong != setScreenLong) {
1380 return false;
1381 }
Tobias Haamel27b28b32010-02-09 23:09:17 +01001382
1383 const int uiModeType = uiMode&MASK_UI_MODE_TYPE;
1384 const int setUiModeType = settings.uiMode&MASK_UI_MODE_TYPE;
1385 if (setUiModeType != 0 && uiModeType != 0
1386 && uiModeType != setUiModeType) {
1387 return false;
1388 }
1389
1390 const int uiModeNight = uiMode&MASK_UI_MODE_NIGHT;
1391 const int setUiModeNight = settings.uiMode&MASK_UI_MODE_NIGHT;
1392 if (setUiModeNight != 0 && uiModeNight != 0
1393 && uiModeNight != setUiModeNight) {
1394 return false;
1395 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 if (screenType != 0) {
1398 if (settings.orientation != 0 && orientation != 0
1399 && orientation != settings.orientation) {
1400 return false;
1401 }
Robert Greenwalt96e20402009-04-22 14:35:11 -07001402 // density always matches - we can scale it. See isBetterThan
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 if (settings.touchscreen != 0 && touchscreen != 0
1404 && touchscreen != settings.touchscreen) {
1405 return false;
1406 }
1407 }
1408 if (input != 0) {
1409 const int keysHidden = inputFlags&MASK_KEYSHIDDEN;
1410 const int setKeysHidden = settings.inputFlags&MASK_KEYSHIDDEN;
1411 if (setKeysHidden != 0 && keysHidden != 0
1412 && keysHidden != setKeysHidden) {
1413 // For compatibility, we count a request for KEYSHIDDEN_NO as also
1414 // matching the more recent KEYSHIDDEN_SOFT. Basically
1415 // KEYSHIDDEN_NO means there is some kind of keyboard available.
1416 //LOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden);
1417 if (keysHidden != KEYSHIDDEN_NO || setKeysHidden != KEYSHIDDEN_SOFT) {
1418 //LOGI("No match!");
1419 return false;
1420 }
1421 }
Dianne Hackborn93e462b2009-09-15 22:50:40 -07001422 const int navHidden = inputFlags&MASK_NAVHIDDEN;
1423 const int setNavHidden = settings.inputFlags&MASK_NAVHIDDEN;
1424 if (setNavHidden != 0 && navHidden != 0
1425 && navHidden != setNavHidden) {
1426 return false;
1427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 if (settings.keyboard != 0 && keyboard != 0
1429 && keyboard != settings.keyboard) {
1430 return false;
1431 }
1432 if (settings.navigation != 0 && navigation != 0
1433 && navigation != settings.navigation) {
1434 return false;
1435 }
1436 }
1437 if (screenSize != 0) {
1438 if (settings.screenWidth != 0 && screenWidth != 0
1439 && screenWidth != settings.screenWidth) {
1440 return false;
1441 }
1442 if (settings.screenHeight != 0 && screenHeight != 0
1443 && screenHeight != settings.screenHeight) {
1444 return false;
1445 }
1446 }
1447 if (version != 0) {
1448 if (settings.sdkVersion != 0 && sdkVersion != 0
Dianne Hackborn55339952009-11-01 21:16:59 -08001449 && sdkVersion > settings.sdkVersion) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 return false;
1451 }
1452 if (settings.minorVersion != 0 && minorVersion != 0
1453 && minorVersion != settings.minorVersion) {
1454 return false;
1455 }
1456 }
1457 return true;
1458 }
1459
1460 void getLocale(char str[6]) const {
1461 memset(str, 0, 6);
1462 if (language[0]) {
1463 str[0] = language[0];
1464 str[1] = language[1];
1465 if (country[0]) {
1466 str[2] = '_';
1467 str[3] = country[0];
1468 str[4] = country[1];
1469 }
1470 }
1471 }
1472
1473 String8 toString() const {
1474 char buf[200];
Dianne Hackborn723738c2009-06-25 19:48:04 -07001475 sprintf(buf, "imsi=%d/%d lang=%c%c reg=%c%c orient=%d touch=%d dens=%d "
Tobias Haamel27b28b32010-02-09 23:09:17 +01001476 "kbd=%d nav=%d input=%d scrnW=%d scrnH=%d sz=%d long=%d "
1477 "ui=%d night=%d vers=%d.%d",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 mcc, mnc,
1479 language[0] ? language[0] : '-', language[1] ? language[1] : '-',
1480 country[0] ? country[0] : '-', country[1] ? country[1] : '-',
1481 orientation, touchscreen, density, keyboard, navigation, inputFlags,
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001482 screenWidth, screenHeight,
1483 screenLayout&MASK_SCREENSIZE, screenLayout&MASK_SCREENLONG,
Tobias Haamel27b28b32010-02-09 23:09:17 +01001484 uiMode&MASK_UI_MODE_TYPE, uiMode&MASK_UI_MODE_NIGHT,
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001485 sdkVersion, minorVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 return String8(buf);
1487 }
1488};
1489
1490/**
1491 * A specification of the resources defined by a particular type.
1492 *
1493 * There should be one of these chunks for each resource type.
1494 *
1495 * This structure is followed by an array of integers providing the set of
1496 * configuation change flags (ResTable_config::CONFIG_*) that have multiple
1497 * resources for that configuration. In addition, the high bit is set if that
1498 * resource has been made public.
1499 */
1500struct ResTable_typeSpec
1501{
1502 struct ResChunk_header header;
1503
1504 // The type identifier this chunk is holding. Type IDs start
1505 // at 1 (corresponding to the value of the type bits in a
1506 // resource identifier). 0 is invalid.
1507 uint8_t id;
1508
1509 // Must be 0.
1510 uint8_t res0;
1511 // Must be 0.
1512 uint16_t res1;
1513
1514 // Number of uint32_t entry configuration masks that follow.
1515 uint32_t entryCount;
1516
1517 enum {
1518 // Additional flag indicating an entry is public.
1519 SPEC_PUBLIC = 0x40000000
1520 };
1521};
1522
1523/**
1524 * A collection of resource entries for a particular resource data
1525 * type. Followed by an array of uint32_t defining the resource
1526 * values, corresponding to the array of type strings in the
1527 * ResTable_package::typeStrings string block. Each of these hold an
1528 * index from entriesStart; a value of NO_ENTRY means that entry is
1529 * not defined.
1530 *
1531 * There may be multiple of these chunks for a particular resource type,
1532 * supply different configuration variations for the resource values of
1533 * that type.
1534 *
1535 * It would be nice to have an additional ordered index of entries, so
1536 * we can do a binary search if trying to find a resource by string name.
1537 */
1538struct ResTable_type
1539{
1540 struct ResChunk_header header;
1541
1542 enum {
1543 NO_ENTRY = 0xFFFFFFFF
1544 };
1545
1546 // The type identifier this chunk is holding. Type IDs start
1547 // at 1 (corresponding to the value of the type bits in a
1548 // resource identifier). 0 is invalid.
1549 uint8_t id;
1550
1551 // Must be 0.
1552 uint8_t res0;
1553 // Must be 0.
1554 uint16_t res1;
1555
1556 // Number of uint32_t entry indices that follow.
1557 uint32_t entryCount;
1558
1559 // Offset from header where ResTable_entry data starts.
1560 uint32_t entriesStart;
1561
1562 // Configuration this collection of entries is designed for.
1563 ResTable_config config;
1564};
1565
1566/**
1567 * This is the beginning of information about an entry in the resource
1568 * table. It holds the reference to the name of this entry, and is
1569 * immediately followed by one of:
Dianne Hackbornde7faf62009-06-30 13:27:30 -07001570 * * A Res_value structure, if FLAG_COMPLEX is -not- set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 * * An array of ResTable_map structures, if FLAG_COMPLEX is set.
1572 * These supply a set of name/value mappings of data.
1573 */
1574struct ResTable_entry
1575{
1576 // Number of bytes in this structure.
1577 uint16_t size;
1578
1579 enum {
1580 // If set, this is a complex entry, holding a set of name/value
1581 // mappings. It is followed by an array of ResTable_map structures.
1582 FLAG_COMPLEX = 0x0001,
1583 // If set, this resource has been declared public, so libraries
1584 // are allowed to reference it.
1585 FLAG_PUBLIC = 0x0002
1586 };
1587 uint16_t flags;
1588
1589 // Reference into ResTable_package::keyStrings identifying this entry.
1590 struct ResStringPool_ref key;
1591};
1592
1593/**
1594 * Extended form of a ResTable_entry for map entries, defining a parent map
1595 * resource from which to inherit values.
1596 */
1597struct ResTable_map_entry : public ResTable_entry
1598{
1599 // Resource identifier of the parent mapping, or 0 if there is none.
1600 ResTable_ref parent;
1601 // Number of name/value pairs that follow for FLAG_COMPLEX.
1602 uint32_t count;
1603};
1604
1605/**
1606 * A single name/value mapping that is part of a complex resource
1607 * entry.
1608 */
1609struct ResTable_map
1610{
1611 // The resource identifier defining this mapping's name. For attribute
1612 // resources, 'name' can be one of the following special resource types
1613 // to supply meta-data about the attribute; for all other resource types
1614 // it must be an attribute resource.
1615 ResTable_ref name;
1616
1617 // Special values for 'name' when defining attribute resources.
1618 enum {
1619 // This entry holds the attribute's type code.
1620 ATTR_TYPE = Res_MAKEINTERNAL(0),
1621
1622 // For integral attributes, this is the minimum value it can hold.
1623 ATTR_MIN = Res_MAKEINTERNAL(1),
1624
1625 // For integral attributes, this is the maximum value it can hold.
1626 ATTR_MAX = Res_MAKEINTERNAL(2),
1627
1628 // Localization of this resource is can be encouraged or required with
1629 // an aapt flag if this is set
1630 ATTR_L10N = Res_MAKEINTERNAL(3),
1631
1632 // for plural support, see android.content.res.PluralRules#attrForQuantity(int)
1633 ATTR_OTHER = Res_MAKEINTERNAL(4),
1634 ATTR_ZERO = Res_MAKEINTERNAL(5),
1635 ATTR_ONE = Res_MAKEINTERNAL(6),
1636 ATTR_TWO = Res_MAKEINTERNAL(7),
1637 ATTR_FEW = Res_MAKEINTERNAL(8),
1638 ATTR_MANY = Res_MAKEINTERNAL(9)
1639
1640 };
1641
1642 // Bit mask of allowed types, for use with ATTR_TYPE.
1643 enum {
1644 // No type has been defined for this attribute, use generic
1645 // type handling. The low 16 bits are for types that can be
1646 // handled generically; the upper 16 require additional information
1647 // in the bag so can not be handled generically for TYPE_ANY.
1648 TYPE_ANY = 0x0000FFFF,
1649
1650 // Attribute holds a references to another resource.
1651 TYPE_REFERENCE = 1<<0,
1652
1653 // Attribute holds a generic string.
1654 TYPE_STRING = 1<<1,
1655
1656 // Attribute holds an integer value. ATTR_MIN and ATTR_MIN can
1657 // optionally specify a constrained range of possible integer values.
1658 TYPE_INTEGER = 1<<2,
1659
1660 // Attribute holds a boolean integer.
1661 TYPE_BOOLEAN = 1<<3,
1662
1663 // Attribute holds a color value.
1664 TYPE_COLOR = 1<<4,
1665
1666 // Attribute holds a floating point value.
1667 TYPE_FLOAT = 1<<5,
1668
1669 // Attribute holds a dimension value, such as "20px".
1670 TYPE_DIMENSION = 1<<6,
1671
1672 // Attribute holds a fraction value, such as "20%".
1673 TYPE_FRACTION = 1<<7,
1674
1675 // Attribute holds an enumeration. The enumeration values are
1676 // supplied as additional entries in the map.
1677 TYPE_ENUM = 1<<16,
1678
1679 // Attribute holds a bitmaks of flags. The flag bit values are
1680 // supplied as additional entries in the map.
1681 TYPE_FLAGS = 1<<17
1682 };
1683
1684 // Enum of localization modes, for use with ATTR_L10N.
1685 enum {
1686 L10N_NOT_REQUIRED = 0,
1687 L10N_SUGGESTED = 1
1688 };
1689
1690 // This mapping's value.
1691 Res_value value;
1692};
1693
1694/**
1695 * Convenience class for accessing data in a ResTable resource.
1696 */
1697class ResTable
1698{
1699public:
1700 ResTable();
1701 ResTable(const void* data, size_t size, void* cookie,
1702 bool copyData=false);
1703 ~ResTable();
1704
1705 status_t add(const void* data, size_t size, void* cookie,
1706 bool copyData=false);
1707 status_t add(Asset* asset, void* cookie,
1708 bool copyData=false);
Dianne Hackborn78c40512009-07-06 11:07:40 -07001709 status_t add(ResTable* src);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710
1711 status_t getError() const;
1712
1713 void uninit();
1714
1715 struct resource_name
1716 {
1717 const char16_t* package;
1718 size_t packageLen;
1719 const char16_t* type;
1720 size_t typeLen;
1721 const char16_t* name;
1722 size_t nameLen;
1723 };
1724
1725 bool getResourceName(uint32_t resID, resource_name* outName) const;
1726
1727 /**
1728 * Retrieve the value of a resource. If the resource is found, returns a
1729 * value >= 0 indicating the table it is in (for use with
1730 * getTableStringBlock() and getTableCookie()) and fills in 'outValue'. If
1731 * not found, returns a negative error code.
1732 *
1733 * Note that this function does not do reference traversal. If you want
1734 * to follow references to other resources to get the "real" value to
1735 * use, you need to call resolveReference() after this function.
1736 *
1737 * @param resID The desired resoruce identifier.
1738 * @param outValue Filled in with the resource data that was found.
1739 *
1740 * @return ssize_t Either a >= 0 table index or a negative error code.
1741 */
1742 ssize_t getResource(uint32_t resID, Res_value* outValue, bool mayBeBag=false,
1743 uint32_t* outSpecFlags=NULL, ResTable_config* outConfig=NULL) const;
1744
1745 inline ssize_t getResource(const ResTable_ref& res, Res_value* outValue,
1746 uint32_t* outSpecFlags=NULL) const {
1747 return getResource(res.ident, outValue, false, outSpecFlags, NULL);
1748 }
1749
1750 ssize_t resolveReference(Res_value* inOutValue,
1751 ssize_t blockIndex,
1752 uint32_t* outLastRef = NULL,
Dianne Hackborn0d221012009-07-29 15:41:19 -07001753 uint32_t* inoutTypeSpecFlags = NULL,
1754 ResTable_config* outConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755
1756 enum {
1757 TMP_BUFFER_SIZE = 16
1758 };
1759 const char16_t* valueToString(const Res_value* value, size_t stringBlock,
1760 char16_t tmpBuffer[TMP_BUFFER_SIZE],
1761 size_t* outLen);
1762
1763 struct bag_entry {
1764 ssize_t stringBlock;
1765 ResTable_map map;
1766 };
1767
1768 /**
1769 * Retrieve the bag of a resource. If the resoruce is found, returns the
1770 * number of bags it contains and 'outBag' points to an array of their
1771 * values. If not found, a negative error code is returned.
1772 *
1773 * Note that this function -does- do reference traversal of the bag data.
1774 *
1775 * @param resID The desired resource identifier.
1776 * @param outBag Filled inm with a pointer to the bag mappings.
1777 *
1778 * @return ssize_t Either a >= 0 bag count of negative error code.
1779 */
1780 ssize_t lockBag(uint32_t resID, const bag_entry** outBag) const;
1781
1782 void unlockBag(const bag_entry* bag) const;
1783
1784 void lock() const;
1785
1786 ssize_t getBagLocked(uint32_t resID, const bag_entry** outBag,
1787 uint32_t* outTypeSpecFlags=NULL) const;
1788
1789 void unlock() const;
1790
1791 class Theme {
1792 public:
1793 Theme(const ResTable& table);
1794 ~Theme();
1795
1796 inline const ResTable& getResTable() const { return mTable; }
1797
1798 status_t applyStyle(uint32_t resID, bool force=false);
1799 status_t setTo(const Theme& other);
1800
1801 /**
1802 * Retrieve a value in the theme. If the theme defines this
1803 * value, returns a value >= 0 indicating the table it is in
1804 * (for use with getTableStringBlock() and getTableCookie) and
1805 * fills in 'outValue'. If not found, returns a negative error
1806 * code.
1807 *
1808 * Note that this function does not do reference traversal. If you want
1809 * to follow references to other resources to get the "real" value to
1810 * use, you need to call resolveReference() after this function.
1811 *
1812 * @param resID A resource identifier naming the desired theme
1813 * attribute.
1814 * @param outValue Filled in with the theme value that was
1815 * found.
1816 *
1817 * @return ssize_t Either a >= 0 table index or a negative error code.
1818 */
1819 ssize_t getAttribute(uint32_t resID, Res_value* outValue,
1820 uint32_t* outTypeSpecFlags = NULL) const;
1821
1822 /**
1823 * This is like ResTable::resolveReference(), but also takes
1824 * care of resolving attribute references to the theme.
1825 */
1826 ssize_t resolveAttributeReference(Res_value* inOutValue,
1827 ssize_t blockIndex, uint32_t* outLastRef = NULL,
Dianne Hackborn0d221012009-07-29 15:41:19 -07001828 uint32_t* inoutTypeSpecFlags = NULL,
1829 ResTable_config* inoutConfig = NULL) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830
1831 void dumpToLog() const;
1832
1833 private:
1834 Theme(const Theme&);
1835 Theme& operator=(const Theme&);
1836
1837 struct theme_entry {
1838 ssize_t stringBlock;
1839 uint32_t typeSpecFlags;
1840 Res_value value;
1841 };
1842 struct type_info {
1843 size_t numEntries;
1844 theme_entry* entries;
1845 };
1846 struct package_info {
1847 size_t numTypes;
1848 type_info types[];
1849 };
1850
1851 void free_package(package_info* pi);
1852 package_info* copy_package(package_info* pi);
1853
1854 const ResTable& mTable;
1855 package_info* mPackages[Res_MAXPACKAGE];
1856 };
1857
1858 void setParameters(const ResTable_config* params);
1859 void getParameters(ResTable_config* params) const;
1860
1861 // Retrieve an identifier (which can be passed to getResource)
1862 // for a given resource name. The 'name' can be fully qualified
1863 // (<package>:<type>.<basename>) or the package or type components
1864 // can be dropped if default values are supplied here.
1865 //
1866 // Returns 0 if no such resource was found, else a valid resource ID.
1867 uint32_t identifierForName(const char16_t* name, size_t nameLen,
1868 const char16_t* type = 0, size_t typeLen = 0,
1869 const char16_t* defPackage = 0,
1870 size_t defPackageLen = 0,
1871 uint32_t* outTypeSpecFlags = NULL) const;
1872
1873 static bool expandResourceRef(const uint16_t* refStr, size_t refLen,
1874 String16* outPackage,
1875 String16* outType,
1876 String16* outName,
1877 const String16* defType = NULL,
1878 const String16* defPackage = NULL,
1879 const char** outErrorMsg = NULL);
1880
1881 static bool stringToInt(const char16_t* s, size_t len, Res_value* outValue);
1882 static bool stringToFloat(const char16_t* s, size_t len, Res_value* outValue);
1883
1884 // Used with stringToValue.
1885 class Accessor
1886 {
1887 public:
1888 inline virtual ~Accessor() { }
1889
1890 virtual uint32_t getCustomResource(const String16& package,
1891 const String16& type,
1892 const String16& name) const = 0;
1893 virtual uint32_t getCustomResourceWithCreation(const String16& package,
1894 const String16& type,
1895 const String16& name,
1896 const bool createIfNeeded = false) = 0;
1897 virtual uint32_t getRemappedPackage(uint32_t origPackage) const = 0;
1898 virtual bool getAttributeType(uint32_t attrID, uint32_t* outType) = 0;
1899 virtual bool getAttributeMin(uint32_t attrID, uint32_t* outMin) = 0;
1900 virtual bool getAttributeMax(uint32_t attrID, uint32_t* outMax) = 0;
1901 virtual bool getAttributeEnum(uint32_t attrID,
1902 const char16_t* name, size_t nameLen,
1903 Res_value* outValue) = 0;
1904 virtual bool getAttributeFlags(uint32_t attrID,
1905 const char16_t* name, size_t nameLen,
1906 Res_value* outValue) = 0;
1907 virtual uint32_t getAttributeL10N(uint32_t attrID) = 0;
1908 virtual bool getLocalizationSetting() = 0;
1909 virtual void reportError(void* accessorCookie, const char* fmt, ...) = 0;
1910 };
1911
1912 // Convert a string to a resource value. Handles standard "@res",
1913 // "#color", "123", and "0x1bd" types; performs escaping of strings.
1914 // The resulting value is placed in 'outValue'; if it is a string type,
1915 // 'outString' receives the string. If 'attrID' is supplied, the value is
1916 // type checked against this attribute and it is used to perform enum
1917 // evaluation. If 'acccessor' is supplied, it will be used to attempt to
1918 // resolve resources that do not exist in this ResTable. If 'attrType' is
1919 // supplied, the value will be type checked for this format if 'attrID'
1920 // is not supplied or found.
1921 bool stringToValue(Res_value* outValue, String16* outString,
1922 const char16_t* s, size_t len,
1923 bool preserveSpaces, bool coerceType,
1924 uint32_t attrID = 0,
1925 const String16* defType = NULL,
1926 const String16* defPackage = NULL,
1927 Accessor* accessor = NULL,
1928 void* accessorCookie = NULL,
1929 uint32_t attrType = ResTable_map::TYPE_ANY,
1930 bool enforcePrivate = true) const;
1931
1932 // Perform processing of escapes and quotes in a string.
1933 static bool collectString(String16* outString,
1934 const char16_t* s, size_t len,
1935 bool preserveSpaces,
1936 const char** outErrorMsg = NULL,
1937 bool append = false);
1938
1939 size_t getBasePackageCount() const;
1940 const char16_t* getBasePackageName(size_t idx) const;
1941 uint32_t getBasePackageId(size_t idx) const;
1942
1943 size_t getTableCount() const;
1944 const ResStringPool* getTableStringBlock(size_t index) const;
1945 void* getTableCookie(size_t index) const;
1946
1947 // Return the configurations (ResTable_config) that we know about
1948 void getConfigurations(Vector<ResTable_config>* configs) const;
1949
1950 void getLocales(Vector<String8>* locales) const;
1951
1952#ifndef HAVE_ANDROID_OS
Dianne Hackborne17086b2009-06-19 15:13:28 -07001953 void print(bool inclValues) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954#endif
1955
1956private:
1957 struct Header;
1958 struct Type;
1959 struct Package;
1960 struct PackageGroup;
1961 struct bag_set;
1962
1963 status_t add(const void* data, size_t size, void* cookie,
1964 Asset* asset, bool copyData);
1965
1966 ssize_t getResourcePackageIndex(uint32_t resID) const;
1967 ssize_t getEntry(
1968 const Package* package, int typeIndex, int entryIndex,
1969 const ResTable_config* config,
1970 const ResTable_type** outType, const ResTable_entry** outEntry,
1971 const Type** outTypeClass) const;
1972 status_t parsePackage(
1973 const ResTable_package* const pkg, const Header* const header);
1974
Dianne Hackbornde7faf62009-06-30 13:27:30 -07001975 void print_value(const Package* pkg, const Res_value& value) const;
1976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 mutable Mutex mLock;
1978
1979 status_t mError;
1980
1981 ResTable_config mParams;
1982
1983 // Array of all resource tables.
1984 Vector<Header*> mHeaders;
1985
1986 // Array of packages in all resource tables.
1987 Vector<PackageGroup*> mPackageGroups;
1988
1989 // Mapping from resource package IDs to indices into the internal
1990 // package array.
1991 uint8_t mPackageMap[256];
1992};
1993
1994} // namespace android
1995
1996#endif // _LIBS_UTILS_RESOURCE_TYPES_H