2 * Copyright (C) 2005 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 // Definitions of resource data structures.
20 #ifndef _LIBS_UTILS_RESOURCE_TYPES_H
21 #define _LIBS_UTILS_RESOURCE_TYPES_H
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>
29 #include <utils/threads.h>
32 #include <sys/types.h>
34 #include <android/configuration.h>
38 /** ********************************************************************
41 * New private chunks that may be placed in PNG images.
43 *********************************************************************** */
46 * This chunk specifies how to split an image into segments for
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):
53 * +-----+----+------+-------+
55 * +-----+----+------+-------+
61 * +-----+----+------+-------+
62 * S3| 8 | 9 | 10 | 11 |
63 * +-----+----+------+-------+
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
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
76 * than regions 1, 5 and 9 since the horizontal segment associated with
77 * 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
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
92 * go to xDiv[0] and slices 2, 6 and 10 start at xDiv[1] and end at
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.
102 * The PNG chunk type is "npTc".
104 struct Res_png_9patch
106 Res_png_9patch() : wasDeserialized(false), xDivs(NULL),
107 yDivs(NULL), colors(NULL) { }
109 int8_t wasDeserialized;
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.
122 int32_t paddingLeft, paddingRight;
123 int32_t paddingTop, paddingBottom;
126 // The 9 patch segment is not a solid color.
127 NO_COLOR = 0x00000001,
129 // The 9 patch segment is completely transparent.
130 TRANSPARENT_COLOR = 0x00000000
132 // Note: allocation/free of this pointer is left to the caller.
135 // Convert data from device representation to PNG file representation.
137 // Convert data from PNG file representation to device representation.
139 // Serialize/Marshall the patch data into a newly malloc-ed block
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();
149 /** ********************************************************************
152 * These are standard types that are shared between multiple specific
155 *********************************************************************** */
158 * Header that appears at the front of every data chunk in a resource.
160 struct ResChunk_header
162 // Type identifier for this chunk. The meaning of this value depends
163 // on the containing chunk.
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
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.
180 RES_NULL_TYPE = 0x0000,
181 RES_STRING_POOL_TYPE = 0x0001,
182 RES_TABLE_TYPE = 0x0002,
183 RES_XML_TYPE = 0x0003,
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,
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
204 * Macros for building/splitting resource identifiers.
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)
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))
218 #define Res_MAXPACKAGE 255
221 * Representation of a value in a resource, supplying type
226 // Number of bytes in this structure.
232 // Type of the data value.
236 // The 'data' holds a ResTable_ref, a reference to another resource
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.
244 // The 'data' holds a single-precision floating point number.
246 // The 'data' holds a complex number encoding a dimension value,
248 TYPE_DIMENSION = 0x05,
249 // The 'data' holds a complex number encoding a fraction of a
251 TYPE_FRACTION = 0x06,
253 // Beginning of integer flavors...
254 TYPE_FIRST_INT = 0x10,
256 // The 'data' is a raw integer value of the form n..n.
258 // The 'data' is a raw integer value of the form 0xn..n.
260 // The 'data' is either 0 or 1, for input "false" or "true" respectively.
261 TYPE_INT_BOOLEAN = 0x12,
263 // Beginning of color integer flavors...
264 TYPE_FIRST_COLOR_INT = 0x1c,
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,
275 // ...end of integer flavors.
276 TYPE_LAST_COLOR_INT = 0x1f,
278 // ...end of integer flavors.
283 // Structure of complex data values (TYPE_UNIT and TYPE_FRACTION)
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,
290 // TYPE_DIMENSION: Value is raw pixels.
292 // TYPE_DIMENSION: Value is Device Independent Pixels.
293 COMPLEX_UNIT_DIP = 1,
294 // TYPE_DIMENSION: Value is a Scaled device independent Pixels.
296 // TYPE_DIMENSION: Value is in points.
298 // TYPE_DIMENSION: Value is in inches.
300 // TYPE_DIMENSION: Value is in millimeters.
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,
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,
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,
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
329 // The data for this item, as interpreted according to dataType.
332 void copyFrom_dtoh(const Res_value& src);
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.
349 * Reference to a string in a string pool.
351 struct ResStringPool_ref
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.
359 /** ********************************************************************
362 * A set of strings that can be references by others through a
365 *********************************************************************** */
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.
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.
382 struct ResStringPool_header
384 struct ResChunk_header header;
386 // Number of strings in this pool (number of uint32_t indices that follow
388 uint32_t stringCount;
390 // Number of style span arrays in the pool (number of uint32_t indices
391 // follow the string indices).
396 // If set, the string index is sorted by the string values (based
400 // String pool is encoded in UTF-8
405 // Index from header of the string data.
406 uint32_t stringsStart;
408 // Index from header of the style data.
409 uint32_t stylesStart;
413 * This structure defines a span of style information associated with
414 * a string in the pool.
416 struct ResStringPool_span
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;
427 // The range of characters in the string that this span applies to.
428 uint32_t firstChar, lastChar;
432 * Convenience class for accessing data in a ResStringPool resource.
438 ResStringPool(const void* data, size_t size, bool copyData=false);
441 status_t setTo(const void* data, size_t size, bool copyData=false);
443 status_t getError() const;
447 inline const char16_t* stringAt(const ResStringPool_ref& ref, size_t* outLen) const {
448 return stringAt(ref.index, outLen);
450 const char16_t* stringAt(size_t idx, size_t* outLen) const;
452 const char* string8At(size_t idx, size_t* outLen) const;
454 const ResStringPool_span* styleAt(const ResStringPool_ref& ref) const;
455 const ResStringPool_span* styleAt(size_t idx) const;
457 ssize_t indexOfString(const char16_t* str, size_t strLen) const;
461 #ifndef HAVE_ANDROID_OS
468 const ResStringPool_header* mHeader;
470 mutable Mutex mDecodeLock;
471 const uint32_t* mEntries;
472 const uint32_t* mEntryStyles;
473 const void* mStrings;
475 uint32_t mStringPoolSize; // number of uint16_t
476 const uint32_t* mStyles;
477 uint32_t mStylePoolSize; // number of uint32_t
480 /** ********************************************************************
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.
487 *********************************************************************** */
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.
496 struct ResXMLTree_header
498 struct ResChunk_header header;
502 * Basic XML tree node. A single item in the XML document. Extended info
503 * about the node can be found after header.headerSize.
505 struct ResXMLTree_node
507 struct ResChunk_header header;
509 // Line number in original source file at which this element appeared.
512 // Optional XML comment that was associated with this element; -1 if none.
513 struct ResStringPool_ref comment;
517 * Extended XML tree node for CDATA tags -- includes the CDATA string.
518 * Appears header.headerSize bytes after a ResXMLTree_node.
520 struct ResXMLTree_cdataExt
522 // The raw CDATA character data.
523 struct ResStringPool_ref data;
525 // The typed value of the character data if this is a CDATA node.
526 struct Res_value typedData;
530 * Extended XML tree node for namespace start/end nodes.
531 * Appears header.headerSize bytes after a ResXMLTree_node.
533 struct ResXMLTree_namespaceExt
535 // The prefix of the namespace.
536 struct ResStringPool_ref prefix;
538 // The URI of the namespace.
539 struct ResStringPool_ref uri;
543 * Extended XML tree node for element start/end nodes.
544 * Appears header.headerSize bytes after a ResXMLTree_node.
546 struct ResXMLTree_endElementExt
548 // String of the full namespace of this element.
549 struct ResStringPool_ref ns;
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;
557 * Extended XML tree node for start tags -- includes attribute
559 * Appears header.headerSize bytes after a ResXMLTree_node.
561 struct ResXMLTree_attrExt
563 // String of the full namespace of this element.
564 struct ResStringPool_ref ns;
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;
570 // Byte offset from the start of this structure where the attributes start.
571 uint16_t attributeStart;
573 // Size of the ResXMLTree_attribute structures that follow.
574 uint16_t attributeSize;
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;
581 // Index (1-based) of the "id" attribute. 0 if none.
584 // Index (1-based) of the "class" attribute. 0 if none.
587 // Index (1-based) of the "style" attribute. 0 if none.
591 struct ResXMLTree_attribute
593 // Namespace of this attribute.
594 struct ResStringPool_ref ns;
596 // Name of this attribute.
597 struct ResStringPool_ref name;
599 // The original raw string value of this attribute.
600 struct ResStringPool_ref rawValue;
602 // Processesd typed value of this attribute.
603 struct Res_value typedValue;
611 ResXMLParser(const ResXMLTree& tree);
618 FIRST_CHUNK_CODE = RES_XML_FIRST_CHUNK_TYPE,
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
627 struct ResXMLPosition
629 event_code_t eventCode;
630 const ResXMLTree_node* curNode;
636 const ResStringPool& getStrings() const;
638 event_code_t getEventType() const;
639 // Note, unlike XmlPullParser, the first call to next() will return
640 // START_TAG of the first element.
643 // These are available for all nodes:
644 int32_t getCommentID() const;
645 const uint16_t* getComment(size_t* outLen) const;
646 uint32_t getLineNumber() const;
648 // This is available for TEXT:
649 int32_t getTextID() const;
650 const uint16_t* getText(size_t* outLen) const;
651 ssize_t getTextValue(Res_value* outValue) const;
653 // These are available for START_NAMESPACE and END_NAMESPACE:
654 int32_t getNamespacePrefixID() const;
655 const uint16_t* getNamespacePrefix(size_t* outLen) const;
656 int32_t getNamespaceUriID() const;
657 const uint16_t* getNamespaceUri(size_t* outLen) const;
659 // These are available for START_TAG and END_TAG:
660 int32_t getElementNamespaceID() const;
661 const uint16_t* getElementNamespace(size_t* outLen) const;
662 int32_t getElementNameID() const;
663 const uint16_t* getElementName(size_t* outLen) const;
665 // Remaining methods are for retrieving information about attributes
666 // associated with a START_TAG:
668 size_t getAttributeCount() const;
670 // Returns -1 if no namespace, -2 if idx out of range.
671 int32_t getAttributeNamespaceID(size_t idx) const;
672 const uint16_t* getAttributeNamespace(size_t idx, size_t* outLen) const;
674 int32_t getAttributeNameID(size_t idx) const;
675 const uint16_t* getAttributeName(size_t idx, size_t* outLen) const;
676 uint32_t getAttributeNameResID(size_t idx) const;
678 int32_t getAttributeValueStringID(size_t idx) const;
679 const uint16_t* getAttributeStringValue(size_t idx, size_t* outLen) const;
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;
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;
689 ssize_t indexOfID() const;
690 ssize_t indexOfClass() const;
691 ssize_t indexOfStyle() const;
693 void getPosition(ResXMLPosition* pos) const;
694 void setPosition(const ResXMLPosition& pos);
697 friend class ResXMLTree;
699 event_code_t nextNode();
701 const ResXMLTree& mTree;
702 event_code_t mEventCode;
703 const ResXMLTree_node* mCurNode;
708 * Convenience class for accessing data in a ResXMLTree resource.
710 class ResXMLTree : public ResXMLParser
714 ResXMLTree(const void* data, size_t size, bool copyData=false);
717 status_t setTo(const void* data, size_t size, bool copyData=false);
719 status_t getError() const;
724 friend class ResXMLParser;
726 status_t validateNode(const ResXMLTree_node* node) const;
730 const ResXMLTree_header* mHeader;
732 const uint8_t* mDataEnd;
733 ResStringPool mStrings;
734 const uint32_t* mResIds;
736 const ResXMLTree_node* mRootNode;
737 const void* mRootExt;
738 event_code_t mRootCode;
741 /** ********************************************************************
744 *********************************************************************** */
747 * Header for a resource table. Its data contains a series of
749 * * A ResStringPool_header containing all table values.
750 * * One or more ResTable_package chunks.
752 * Specific entries within a resource table can be uniquely identified
753 * with a single integer as defined by the ResTable_ref structure.
755 struct ResTable_header
757 struct ResChunk_header header;
759 // The number of ResTable_package structures.
760 uint32_t packageCount;
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.
768 struct ResTable_package
770 struct ResChunk_header header;
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.
777 // Actual name of this package, \0-terminated.
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;
785 // Last index into typeStrings that is for public use by others.
786 uint32_t lastPublicType;
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).
793 // Last index into keyStrings that is for public use by others.
794 uint32_t lastPublicKey;
798 * Describes a particular resource configuration.
800 struct ResTable_config
802 // Number of bytes in this structure.
807 // Mobile country code (from SIM). 0 means "any".
809 // Mobile network code (from SIM). 0 means "any".
817 // \0\0 means "any". Otherwise, en, fr, etc.
820 // \0\0 means "any". Otherwise, US, CA, etc.
827 ORIENTATION_ANY = ACONFIGURATION_ORIENTATION_ANY,
828 ORIENTATION_PORT = ACONFIGURATION_ORIENTATION_PORT,
829 ORIENTATION_LAND = ACONFIGURATION_ORIENTATION_LAND,
830 ORIENTATION_SQUARE = ACONFIGURATION_ORIENTATION_SQUARE,
834 TOUCHSCREEN_ANY = ACONFIGURATION_TOUCHSCREEN_ANY,
835 TOUCHSCREEN_NOTOUCH = ACONFIGURATION_TOUCHSCREEN_NOTOUCH,
836 TOUCHSCREEN_STYLUS = ACONFIGURATION_TOUCHSCREEN_STYLUS,
837 TOUCHSCREEN_FINGER = ACONFIGURATION_TOUCHSCREEN_FINGER,
841 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
858 KEYBOARD_ANY = ACONFIGURATION_KEYBOARD_ANY,
859 KEYBOARD_NOKEYS = ACONFIGURATION_KEYBOARD_NOKEYS,
860 KEYBOARD_QWERTY = ACONFIGURATION_KEYBOARD_QWERTY,
861 KEYBOARD_12KEY = ACONFIGURATION_KEYBOARD_12KEY,
865 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,
873 MASK_KEYSHIDDEN = 0x0003,
874 KEYSHIDDEN_ANY = ACONFIGURATION_KEYSHIDDEN_ANY,
875 KEYSHIDDEN_NO = ACONFIGURATION_KEYSHIDDEN_NO,
876 KEYSHIDDEN_YES = ACONFIGURATION_KEYSHIDDEN_YES,
877 KEYSHIDDEN_SOFT = ACONFIGURATION_KEYSHIDDEN_SOFT,
881 MASK_NAVHIDDEN = 0x000c,
883 NAVHIDDEN_ANY = ACONFIGURATION_NAVHIDDEN_ANY << SHIFT_NAVHIDDEN,
884 NAVHIDDEN_NO = ACONFIGURATION_NAVHIDDEN_NO << SHIFT_NAVHIDDEN,
885 NAVHIDDEN_YES = ACONFIGURATION_NAVHIDDEN_YES << SHIFT_NAVHIDDEN,
908 uint16_t screenWidth;
909 uint16_t screenHeight;
925 // For now minorVersion must always be 0!!! Its meaning
926 // is currently undefined.
927 uint16_t minorVersion;
933 // screenLayout bits for screen size class.
934 MASK_SCREENSIZE = 0x0f,
935 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,
941 // screenLayout bits for wide/long screen variation.
942 MASK_SCREENLONG = 0x30,
943 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,
950 // uiMode bits for the mode type.
951 MASK_UI_MODE_TYPE = 0x0f,
952 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,
957 // uiMode bits for the night switch.
958 MASK_UI_MODE_NIGHT = 0x30,
959 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,
967 uint8_t screenLayout;
969 uint8_t screenConfigPad1;
970 uint8_t screenConfigPad2;
972 uint32_t screenConfig;
975 inline void copyFromDeviceNoSwap(const ResTable_config& o) {
976 const size_t size = dtohl(o.size);
977 if (size >= sizeof(ResTable_config)) {
980 memcpy(this, &o, size);
981 memset(((uint8_t*)this)+size, 0, sizeof(ResTable_config)-size);
985 inline void copyFromDtoH(const ResTable_config& o) {
986 copyFromDeviceNoSwap(o);
987 size = sizeof(ResTable_config);
990 density = dtohs(density);
991 screenWidth = dtohs(screenWidth);
992 screenHeight = dtohs(screenHeight);
993 sdkVersion = dtohs(sdkVersion);
994 minorVersion = dtohs(minorVersion);
997 inline void swapHtoD() {
1001 density = htods(density);
1002 screenWidth = htods(screenWidth);
1003 screenHeight = htods(screenHeight);
1004 sdkVersion = htods(sdkVersion);
1005 minorVersion = htods(minorVersion);
1008 inline int compare(const ResTable_config& o) const {
1009 int32_t diff = (int32_t)(imsi - o.imsi);
1010 if (diff != 0) return diff;
1011 diff = (int32_t)(locale - o.locale);
1012 if (diff != 0) return diff;
1013 diff = (int32_t)(screenType - o.screenType);
1014 if (diff != 0) return diff;
1015 diff = (int32_t)(input - o.input);
1016 if (diff != 0) return diff;
1017 diff = (int32_t)(screenSize - o.screenSize);
1018 if (diff != 0) return diff;
1019 diff = (int32_t)(version - o.version);
1020 if (diff != 0) return diff;
1021 diff = (int32_t)(screenLayout - o.screenLayout);
1022 if (diff != 0) return diff;
1023 diff = (int32_t)(uiMode - o.uiMode);
1027 // Flags indicating a set of config values. These flag constants must
1028 // match the corresponding ones in android.content.pm.ActivityInfo and
1029 // attrs_manifest.xml.
1031 CONFIG_MCC = ACONFIGURATION_MCC,
1032 CONFIG_MNC = ACONFIGURATION_MCC,
1033 CONFIG_LOCALE = ACONFIGURATION_LOCALE,
1034 CONFIG_TOUCHSCREEN = ACONFIGURATION_TOUCHSCREEN,
1035 CONFIG_KEYBOARD = ACONFIGURATION_KEYBOARD,
1036 CONFIG_KEYBOARD_HIDDEN = ACONFIGURATION_KEYBOARD_HIDDEN,
1037 CONFIG_NAVIGATION = ACONFIGURATION_NAVIGATION,
1038 CONFIG_ORIENTATION = ACONFIGURATION_ORIENTATION,
1039 CONFIG_DENSITY = ACONFIGURATION_DENSITY,
1040 CONFIG_SCREEN_SIZE = ACONFIGURATION_SCREEN_SIZE,
1041 CONFIG_VERSION = ACONFIGURATION_VERSION,
1042 CONFIG_SCREEN_LAYOUT = ACONFIGURATION_SCREEN_LAYOUT,
1043 CONFIG_UI_MODE = ACONFIGURATION_UI_MODE
1046 // Compare two configuration, returning CONFIG_* flags set for each value
1047 // that is different.
1048 inline int diff(const ResTable_config& o) const {
1050 if (mcc != o.mcc) diffs |= CONFIG_MCC;
1051 if (mnc != o.mnc) diffs |= CONFIG_MNC;
1052 if (locale != o.locale) diffs |= CONFIG_LOCALE;
1053 if (orientation != o.orientation) diffs |= CONFIG_ORIENTATION;
1054 if (density != o.density) diffs |= CONFIG_DENSITY;
1055 if (touchscreen != o.touchscreen) diffs |= CONFIG_TOUCHSCREEN;
1056 if (((inputFlags^o.inputFlags)&(MASK_KEYSHIDDEN|MASK_NAVHIDDEN)) != 0)
1057 diffs |= CONFIG_KEYBOARD_HIDDEN;
1058 if (keyboard != o.keyboard) diffs |= CONFIG_KEYBOARD;
1059 if (navigation != o.navigation) diffs |= CONFIG_NAVIGATION;
1060 if (screenSize != o.screenSize) diffs |= CONFIG_SCREEN_SIZE;
1061 if (version != o.version) diffs |= CONFIG_VERSION;
1062 if (screenLayout != o.screenLayout) diffs |= CONFIG_SCREEN_LAYOUT;
1063 if (uiMode != o.uiMode) diffs |= CONFIG_UI_MODE;
1067 // Return true if 'this' is more specific than 'o'.
1069 isMoreSpecificThan(const ResTable_config& o) const {
1070 // The order of the following tests defines the importance of one
1071 // configuration parameter over another. Those tests first are more
1072 // important, trumping any values in those following them.
1073 if (imsi || o.imsi) {
1075 if (!mcc) return false;
1076 if (!o.mcc) return true;
1080 if (!mnc) return false;
1081 if (!o.mnc) return true;
1085 if (locale || o.locale) {
1086 if (language[0] != o.language[0]) {
1087 if (!language[0]) return false;
1088 if (!o.language[0]) return true;
1091 if (country[0] != o.country[0]) {
1092 if (!country[0]) return false;
1093 if (!o.country[0]) return true;
1097 if (screenLayout || o.screenLayout) {
1098 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0) {
1099 if (!(screenLayout & MASK_SCREENSIZE)) return false;
1100 if (!(o.screenLayout & MASK_SCREENSIZE)) return true;
1102 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0) {
1103 if (!(screenLayout & MASK_SCREENLONG)) return false;
1104 if (!(o.screenLayout & MASK_SCREENLONG)) return true;
1108 if (orientation != o.orientation) {
1109 if (!orientation) return false;
1110 if (!o.orientation) return true;
1113 if (uiMode || o.uiMode) {
1114 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0) {
1115 if (!(uiMode & MASK_UI_MODE_TYPE)) return false;
1116 if (!(o.uiMode & MASK_UI_MODE_TYPE)) return true;
1118 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0) {
1119 if (!(uiMode & MASK_UI_MODE_NIGHT)) return false;
1120 if (!(o.uiMode & MASK_UI_MODE_NIGHT)) return true;
1124 // density is never 'more specific'
1125 // as the default just equals 160
1127 if (touchscreen != o.touchscreen) {
1128 if (!touchscreen) return false;
1129 if (!o.touchscreen) return true;
1132 if (input || o.input) {
1133 if (((inputFlags^o.inputFlags) & MASK_KEYSHIDDEN) != 0) {
1134 if (!(inputFlags & MASK_KEYSHIDDEN)) return false;
1135 if (!(o.inputFlags & MASK_KEYSHIDDEN)) return true;
1138 if (((inputFlags^o.inputFlags) & MASK_NAVHIDDEN) != 0) {
1139 if (!(inputFlags & MASK_NAVHIDDEN)) return false;
1140 if (!(o.inputFlags & MASK_NAVHIDDEN)) return true;
1143 if (keyboard != o.keyboard) {
1144 if (!keyboard) return false;
1145 if (!o.keyboard) return true;
1148 if (navigation != o.navigation) {
1149 if (!navigation) return false;
1150 if (!o.navigation) return true;
1154 if (screenSize || o.screenSize) {
1155 if (screenWidth != o.screenWidth) {
1156 if (!screenWidth) return false;
1157 if (!o.screenWidth) return true;
1160 if (screenHeight != o.screenHeight) {
1161 if (!screenHeight) return false;
1162 if (!o.screenHeight) return true;
1166 if (version || o.version) {
1167 if (sdkVersion != o.sdkVersion) {
1168 if (!sdkVersion) return false;
1169 if (!o.sdkVersion) return true;
1172 if (minorVersion != o.minorVersion) {
1173 if (!minorVersion) return false;
1174 if (!o.minorVersion) return true;
1180 // Return true if 'this' is a better match than 'o' for the 'requested'
1181 // configuration. This assumes that match() has already been used to
1182 // remove any configurations that don't match the requested configuration
1183 // at all; if they are not first filtered, non-matching results can be
1184 // considered better than matching ones.
1185 // The general rule per attribute: if the request cares about an attribute
1186 // (it normally does), if the two (this and o) are equal it's a tie. If
1187 // they are not equal then one must be generic because only generic and
1188 // '==requested' will pass the match() call. So if this is not generic,
1189 // it wins. If this IS generic, o wins (return false).
1191 isBetterThan(const ResTable_config& o,
1192 const ResTable_config* requested) const {
1194 if (imsi || o.imsi) {
1195 if ((mcc != o.mcc) && requested->mcc) {
1199 if ((mnc != o.mnc) && requested->mnc) {
1204 if (locale || o.locale) {
1205 if ((language[0] != o.language[0]) && requested->language[0]) {
1206 return (language[0]);
1209 if ((country[0] != o.country[0]) && requested->country[0]) {
1210 return (country[0]);
1214 if (screenLayout || o.screenLayout) {
1215 if (((screenLayout^o.screenLayout) & MASK_SCREENSIZE) != 0
1216 && (requested->screenLayout & MASK_SCREENSIZE)) {
1217 // A little backwards compatibility here: undefined is
1218 // considered equivalent to normal. But only if the
1219 // requested size is at least normal; otherwise, small
1220 // is better than the default.
1221 int mySL = (screenLayout & MASK_SCREENSIZE);
1222 int oSL = (o.screenLayout & MASK_SCREENSIZE);
1223 int fixedMySL = mySL;
1225 if ((requested->screenLayout & MASK_SCREENSIZE) >= SCREENSIZE_NORMAL) {
1226 if (fixedMySL == 0) fixedMySL = SCREENSIZE_NORMAL;
1227 if (fixedOSL == 0) fixedOSL = SCREENSIZE_NORMAL;
1229 // For screen size, the best match is the one that is
1230 // closest to the requested screen size, but not over
1231 // (the not over part is dealt with in match() below).
1232 if (fixedMySL == fixedOSL) {
1233 // If the two are the same, but 'this' is actually
1234 // undefined, then the other is really a better match.
1235 if (mySL == 0) return false;
1238 return fixedMySL >= fixedOSL;
1240 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0
1241 && (requested->screenLayout & MASK_SCREENLONG)) {
1242 return (screenLayout & MASK_SCREENLONG);
1246 if ((orientation != o.orientation) && requested->orientation) {
1247 return (orientation);
1250 if (uiMode || o.uiMode) {
1251 if (((uiMode^o.uiMode) & MASK_UI_MODE_TYPE) != 0
1252 && (requested->uiMode & MASK_UI_MODE_TYPE)) {
1253 return (uiMode & MASK_UI_MODE_TYPE);
1255 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0
1256 && (requested->uiMode & MASK_UI_MODE_NIGHT)) {
1257 return (uiMode & MASK_UI_MODE_NIGHT);
1261 if (screenType || o.screenType) {
1262 if (density != o.density) {
1263 // density is tough. Any density is potentially useful
1264 // because the system will scale it. Scaling down
1265 // is generally better than scaling up.
1266 // Default density counts as 160dpi (the system default)
1267 // TODO - remove 160 constants
1268 int h = (density?density:160);
1269 int l = (o.density?o.density:160);
1270 bool bImBigger = true;
1278 int reqValue = (requested->density?requested->density:160);
1279 if (reqValue >= h) {
1280 // requested value higher than both l and h, give h
1283 if (l >= reqValue) {
1284 // requested value lower than both l and h, give l
1287 // saying that scaling down is 2x better than up
1288 if (((2 * l) - reqValue) * h > reqValue * reqValue) {
1295 if ((touchscreen != o.touchscreen) && requested->touchscreen) {
1296 return (touchscreen);
1300 if (input || o.input) {
1301 const int keysHidden = inputFlags & MASK_KEYSHIDDEN;
1302 const int oKeysHidden = o.inputFlags & MASK_KEYSHIDDEN;
1303 if (keysHidden != oKeysHidden) {
1304 const int reqKeysHidden =
1305 requested->inputFlags & MASK_KEYSHIDDEN;
1306 if (reqKeysHidden) {
1308 if (!keysHidden) return false;
1309 if (!oKeysHidden) return true;
1310 // For compatibility, we count KEYSHIDDEN_NO as being
1311 // the same as KEYSHIDDEN_SOFT. Here we disambiguate
1312 // these by making an exact match more specific.
1313 if (reqKeysHidden == keysHidden) return true;
1314 if (reqKeysHidden == oKeysHidden) return false;
1318 const int navHidden = inputFlags & MASK_NAVHIDDEN;
1319 const int oNavHidden = o.inputFlags & MASK_NAVHIDDEN;
1320 if (navHidden != oNavHidden) {
1321 const int reqNavHidden =
1322 requested->inputFlags & MASK_NAVHIDDEN;
1325 if (!navHidden) return false;
1326 if (!oNavHidden) return true;
1330 if ((keyboard != o.keyboard) && requested->keyboard) {
1334 if ((navigation != o.navigation) && requested->navigation) {
1335 return (navigation);
1339 if (screenSize || o.screenSize) {
1340 if ((screenWidth != o.screenWidth) && requested->screenWidth) {
1341 return (screenWidth);
1344 if ((screenHeight != o.screenHeight) &&
1345 requested->screenHeight) {
1346 return (screenHeight);
1350 if (version || o.version) {
1351 if ((sdkVersion != o.sdkVersion) && requested->sdkVersion) {
1352 return (sdkVersion > o.sdkVersion);
1355 if ((minorVersion != o.minorVersion) &&
1356 requested->minorVersion) {
1357 return (minorVersion);
1363 return isMoreSpecificThan(o);
1366 // Return true if 'this' can be considered a match for the parameters in
1368 // Note this is asymetric. A default piece of data will match every request
1369 // but a request for the default should not match odd specifics
1370 // (ie, request with no mcc should not match a particular mcc's data)
1371 // settings is the requested settings
1372 inline bool match(const ResTable_config& settings) const {
1374 if ((settings.mcc != 0 && mcc != 0
1375 && mcc != settings.mcc) ||
1376 (settings.mcc == 0 && mcc != 0)) {
1379 if ((settings.mnc != 0 && mnc != 0
1380 && mnc != settings.mnc) ||
1381 (settings.mnc == 0 && mnc != 0)) {
1386 if (settings.language[0] != 0 && language[0] != 0
1387 && (language[0] != settings.language[0]
1388 || language[1] != settings.language[1])) {
1391 if (settings.country[0] != 0 && country[0] != 0
1392 && (country[0] != settings.country[0]
1393 || country[1] != settings.country[1])) {
1397 if (screenConfig != 0) {
1398 const int screenSize = screenLayout&MASK_SCREENSIZE;
1399 const int setScreenSize = settings.screenLayout&MASK_SCREENSIZE;
1400 // Any screen sizes for larger screens than the setting do not
1402 if ((setScreenSize != 0 && screenSize != 0
1403 && screenSize > setScreenSize) ||
1404 (setScreenSize == 0 && screenSize != 0)) {
1408 const int screenLong = screenLayout&MASK_SCREENLONG;
1409 const int setScreenLong = settings.screenLayout&MASK_SCREENLONG;
1410 if (setScreenLong != 0 && screenLong != 0
1411 && screenLong != setScreenLong) {
1415 const int uiModeType = uiMode&MASK_UI_MODE_TYPE;
1416 const int setUiModeType = settings.uiMode&MASK_UI_MODE_TYPE;
1417 if (setUiModeType != 0 && uiModeType != 0
1418 && uiModeType != setUiModeType) {
1422 const int uiModeNight = uiMode&MASK_UI_MODE_NIGHT;
1423 const int setUiModeNight = settings.uiMode&MASK_UI_MODE_NIGHT;
1424 if (setUiModeNight != 0 && uiModeNight != 0
1425 && uiModeNight != setUiModeNight) {
1429 if (screenType != 0) {
1430 if (settings.orientation != 0 && orientation != 0
1431 && orientation != settings.orientation) {
1434 // density always matches - we can scale it. See isBetterThan
1435 if (settings.touchscreen != 0 && touchscreen != 0
1436 && touchscreen != settings.touchscreen) {
1441 const int keysHidden = inputFlags&MASK_KEYSHIDDEN;
1442 const int setKeysHidden = settings.inputFlags&MASK_KEYSHIDDEN;
1443 if (setKeysHidden != 0 && keysHidden != 0
1444 && keysHidden != setKeysHidden) {
1445 // For compatibility, we count a request for KEYSHIDDEN_NO as also
1446 // matching the more recent KEYSHIDDEN_SOFT. Basically
1447 // KEYSHIDDEN_NO means there is some kind of keyboard available.
1448 //LOGI("Matching keysHidden: have=%d, config=%d\n", keysHidden, setKeysHidden);
1449 if (keysHidden != KEYSHIDDEN_NO || setKeysHidden != KEYSHIDDEN_SOFT) {
1450 //LOGI("No match!");
1454 const int navHidden = inputFlags&MASK_NAVHIDDEN;
1455 const int setNavHidden = settings.inputFlags&MASK_NAVHIDDEN;
1456 if (setNavHidden != 0 && navHidden != 0
1457 && navHidden != setNavHidden) {
1460 if (settings.keyboard != 0 && keyboard != 0
1461 && keyboard != settings.keyboard) {
1464 if (settings.navigation != 0 && navigation != 0
1465 && navigation != settings.navigation) {
1469 if (screenSize != 0) {
1470 if (settings.screenWidth != 0 && screenWidth != 0
1471 && screenWidth != settings.screenWidth) {
1474 if (settings.screenHeight != 0 && screenHeight != 0
1475 && screenHeight != settings.screenHeight) {
1480 if (settings.sdkVersion != 0 && sdkVersion != 0
1481 && sdkVersion > settings.sdkVersion) {
1484 if (settings.minorVersion != 0 && minorVersion != 0
1485 && minorVersion != settings.minorVersion) {
1492 void getLocale(char str[6]) const {
1495 str[0] = language[0];
1496 str[1] = language[1];
1499 str[3] = country[0];
1500 str[4] = country[1];
1505 String8 toString() const {
1507 sprintf(buf, "imsi=%d/%d lang=%c%c reg=%c%c orient=%d touch=%d dens=%d "
1508 "kbd=%d nav=%d input=%d scrnW=%d scrnH=%d sz=%d long=%d "
1509 "ui=%d night=%d vers=%d.%d",
1511 language[0] ? language[0] : '-', language[1] ? language[1] : '-',
1512 country[0] ? country[0] : '-', country[1] ? country[1] : '-',
1513 orientation, touchscreen, density, keyboard, navigation, inputFlags,
1514 screenWidth, screenHeight,
1515 screenLayout&MASK_SCREENSIZE, screenLayout&MASK_SCREENLONG,
1516 uiMode&MASK_UI_MODE_TYPE, uiMode&MASK_UI_MODE_NIGHT,
1517 sdkVersion, minorVersion);
1518 return String8(buf);
1523 * A specification of the resources defined by a particular type.
1525 * There should be one of these chunks for each resource type.
1527 * This structure is followed by an array of integers providing the set of
1528 * configuation change flags (ResTable_config::CONFIG_*) that have multiple
1529 * resources for that configuration. In addition, the high bit is set if that
1530 * resource has been made public.
1532 struct ResTable_typeSpec
1534 struct ResChunk_header header;
1536 // The type identifier this chunk is holding. Type IDs start
1537 // at 1 (corresponding to the value of the type bits in a
1538 // resource identifier). 0 is invalid.
1546 // Number of uint32_t entry configuration masks that follow.
1547 uint32_t entryCount;
1550 // Additional flag indicating an entry is public.
1551 SPEC_PUBLIC = 0x40000000
1556 * A collection of resource entries for a particular resource data
1557 * type. Followed by an array of uint32_t defining the resource
1558 * values, corresponding to the array of type strings in the
1559 * ResTable_package::typeStrings string block. Each of these hold an
1560 * index from entriesStart; a value of NO_ENTRY means that entry is
1563 * There may be multiple of these chunks for a particular resource type,
1564 * supply different configuration variations for the resource values of
1567 * It would be nice to have an additional ordered index of entries, so
1568 * we can do a binary search if trying to find a resource by string name.
1570 struct ResTable_type
1572 struct ResChunk_header header;
1575 NO_ENTRY = 0xFFFFFFFF
1578 // The type identifier this chunk is holding. Type IDs start
1579 // at 1 (corresponding to the value of the type bits in a
1580 // resource identifier). 0 is invalid.
1588 // Number of uint32_t entry indices that follow.
1589 uint32_t entryCount;
1591 // Offset from header where ResTable_entry data starts.
1592 uint32_t entriesStart;
1594 // Configuration this collection of entries is designed for.
1595 ResTable_config config;
1599 * This is the beginning of information about an entry in the resource
1600 * table. It holds the reference to the name of this entry, and is
1601 * immediately followed by one of:
1602 * * A Res_value structure, if FLAG_COMPLEX is -not- set.
1603 * * An array of ResTable_map structures, if FLAG_COMPLEX is set.
1604 * These supply a set of name/value mappings of data.
1606 struct ResTable_entry
1608 // Number of bytes in this structure.
1612 // If set, this is a complex entry, holding a set of name/value
1613 // mappings. It is followed by an array of ResTable_map structures.
1614 FLAG_COMPLEX = 0x0001,
1615 // If set, this resource has been declared public, so libraries
1616 // are allowed to reference it.
1617 FLAG_PUBLIC = 0x0002
1621 // Reference into ResTable_package::keyStrings identifying this entry.
1622 struct ResStringPool_ref key;
1626 * Extended form of a ResTable_entry for map entries, defining a parent map
1627 * resource from which to inherit values.
1629 struct ResTable_map_entry : public ResTable_entry
1631 // Resource identifier of the parent mapping, or 0 if there is none.
1632 ResTable_ref parent;
1633 // Number of name/value pairs that follow for FLAG_COMPLEX.
1638 * A single name/value mapping that is part of a complex resource
1643 // The resource identifier defining this mapping's name. For attribute
1644 // resources, 'name' can be one of the following special resource types
1645 // to supply meta-data about the attribute; for all other resource types
1646 // it must be an attribute resource.
1649 // Special values for 'name' when defining attribute resources.
1651 // This entry holds the attribute's type code.
1652 ATTR_TYPE = Res_MAKEINTERNAL(0),
1654 // For integral attributes, this is the minimum value it can hold.
1655 ATTR_MIN = Res_MAKEINTERNAL(1),
1657 // For integral attributes, this is the maximum value it can hold.
1658 ATTR_MAX = Res_MAKEINTERNAL(2),
1660 // Localization of this resource is can be encouraged or required with
1661 // an aapt flag if this is set
1662 ATTR_L10N = Res_MAKEINTERNAL(3),
1664 // for plural support, see android.content.res.PluralRules#attrForQuantity(int)
1665 ATTR_OTHER = Res_MAKEINTERNAL(4),
1666 ATTR_ZERO = Res_MAKEINTERNAL(5),
1667 ATTR_ONE = Res_MAKEINTERNAL(6),
1668 ATTR_TWO = Res_MAKEINTERNAL(7),
1669 ATTR_FEW = Res_MAKEINTERNAL(8),
1670 ATTR_MANY = Res_MAKEINTERNAL(9)
1674 // Bit mask of allowed types, for use with ATTR_TYPE.
1676 // No type has been defined for this attribute, use generic
1677 // type handling. The low 16 bits are for types that can be
1678 // handled generically; the upper 16 require additional information
1679 // in the bag so can not be handled generically for TYPE_ANY.
1680 TYPE_ANY = 0x0000FFFF,
1682 // Attribute holds a references to another resource.
1683 TYPE_REFERENCE = 1<<0,
1685 // Attribute holds a generic string.
1688 // Attribute holds an integer value. ATTR_MIN and ATTR_MIN can
1689 // optionally specify a constrained range of possible integer values.
1690 TYPE_INTEGER = 1<<2,
1692 // Attribute holds a boolean integer.
1693 TYPE_BOOLEAN = 1<<3,
1695 // Attribute holds a color value.
1698 // Attribute holds a floating point value.
1701 // Attribute holds a dimension value, such as "20px".
1702 TYPE_DIMENSION = 1<<6,
1704 // Attribute holds a fraction value, such as "20%".
1705 TYPE_FRACTION = 1<<7,
1707 // Attribute holds an enumeration. The enumeration values are
1708 // supplied as additional entries in the map.
1711 // Attribute holds a bitmaks of flags. The flag bit values are
1712 // supplied as additional entries in the map.
1716 // Enum of localization modes, for use with ATTR_L10N.
1718 L10N_NOT_REQUIRED = 0,
1722 // This mapping's value.
1727 * Convenience class for accessing data in a ResTable resource.
1733 ResTable(const void* data, size_t size, void* cookie,
1734 bool copyData=false);
1737 status_t add(const void* data, size_t size, void* cookie,
1738 bool copyData=false, const void* idmap = NULL);
1739 status_t add(Asset* asset, void* cookie,
1740 bool copyData=false, const void* idmap = NULL);
1741 status_t add(ResTable* src);
1743 status_t getError() const;
1747 struct resource_name
1749 const char16_t* package;
1751 const char16_t* type;
1753 const char16_t* name;
1757 bool getResourceName(uint32_t resID, resource_name* outName) const;
1760 * Retrieve the value of a resource. If the resource is found, returns a
1761 * value >= 0 indicating the table it is in (for use with
1762 * getTableStringBlock() and getTableCookie()) and fills in 'outValue'. If
1763 * not found, returns a negative error code.
1765 * Note that this function does not do reference traversal. If you want
1766 * to follow references to other resources to get the "real" value to
1767 * use, you need to call resolveReference() after this function.
1769 * @param resID The desired resoruce identifier.
1770 * @param outValue Filled in with the resource data that was found.
1772 * @return ssize_t Either a >= 0 table index or a negative error code.
1774 ssize_t getResource(uint32_t resID, Res_value* outValue, bool mayBeBag = false,
1775 uint16_t density = 0,
1776 uint32_t* outSpecFlags = NULL,
1777 ResTable_config* outConfig = NULL) const;
1779 inline ssize_t getResource(const ResTable_ref& res, Res_value* outValue,
1780 uint32_t* outSpecFlags=NULL) const {
1781 return getResource(res.ident, outValue, false, 0, outSpecFlags, NULL);
1784 ssize_t resolveReference(Res_value* inOutValue,
1786 uint32_t* outLastRef = NULL,
1787 uint32_t* inoutTypeSpecFlags = NULL,
1788 ResTable_config* outConfig = NULL) const;
1791 TMP_BUFFER_SIZE = 16
1793 const char16_t* valueToString(const Res_value* value, size_t stringBlock,
1794 char16_t tmpBuffer[TMP_BUFFER_SIZE],
1798 ssize_t stringBlock;
1803 * Retrieve the bag of a resource. If the resoruce is found, returns the
1804 * number of bags it contains and 'outBag' points to an array of their
1805 * values. If not found, a negative error code is returned.
1807 * Note that this function -does- do reference traversal of the bag data.
1809 * @param resID The desired resource identifier.
1810 * @param outBag Filled inm with a pointer to the bag mappings.
1812 * @return ssize_t Either a >= 0 bag count of negative error code.
1814 ssize_t lockBag(uint32_t resID, const bag_entry** outBag) const;
1816 void unlockBag(const bag_entry* bag) const;
1820 ssize_t getBagLocked(uint32_t resID, const bag_entry** outBag,
1821 uint32_t* outTypeSpecFlags=NULL) const;
1823 void unlock() const;
1827 Theme(const ResTable& table);
1830 inline const ResTable& getResTable() const { return mTable; }
1832 status_t applyStyle(uint32_t resID, bool force=false);
1833 status_t setTo(const Theme& other);
1836 * Retrieve a value in the theme. If the theme defines this
1837 * value, returns a value >= 0 indicating the table it is in
1838 * (for use with getTableStringBlock() and getTableCookie) and
1839 * fills in 'outValue'. If not found, returns a negative error
1842 * Note that this function does not do reference traversal. If you want
1843 * to follow references to other resources to get the "real" value to
1844 * use, you need to call resolveReference() after this function.
1846 * @param resID A resource identifier naming the desired theme
1848 * @param outValue Filled in with the theme value that was
1851 * @return ssize_t Either a >= 0 table index or a negative error code.
1853 ssize_t getAttribute(uint32_t resID, Res_value* outValue,
1854 uint32_t* outTypeSpecFlags = NULL) const;
1857 * This is like ResTable::resolveReference(), but also takes
1858 * care of resolving attribute references to the theme.
1860 ssize_t resolveAttributeReference(Res_value* inOutValue,
1861 ssize_t blockIndex, uint32_t* outLastRef = NULL,
1862 uint32_t* inoutTypeSpecFlags = NULL,
1863 ResTable_config* inoutConfig = NULL) const;
1865 void dumpToLog() const;
1868 Theme(const Theme&);
1869 Theme& operator=(const Theme&);
1871 struct theme_entry {
1872 ssize_t stringBlock;
1873 uint32_t typeSpecFlags;
1878 theme_entry* entries;
1880 struct package_info {
1885 void free_package(package_info* pi);
1886 package_info* copy_package(package_info* pi);
1888 const ResTable& mTable;
1889 package_info* mPackages[Res_MAXPACKAGE];
1892 void setParameters(const ResTable_config* params);
1893 void getParameters(ResTable_config* params) const;
1895 // Retrieve an identifier (which can be passed to getResource)
1896 // for a given resource name. The 'name' can be fully qualified
1897 // (<package>:<type>.<basename>) or the package or type components
1898 // can be dropped if default values are supplied here.
1900 // Returns 0 if no such resource was found, else a valid resource ID.
1901 uint32_t identifierForName(const char16_t* name, size_t nameLen,
1902 const char16_t* type = 0, size_t typeLen = 0,
1903 const char16_t* defPackage = 0,
1904 size_t defPackageLen = 0,
1905 uint32_t* outTypeSpecFlags = NULL) const;
1907 static bool expandResourceRef(const uint16_t* refStr, size_t refLen,
1908 String16* outPackage,
1911 const String16* defType = NULL,
1912 const String16* defPackage = NULL,
1913 const char** outErrorMsg = NULL);
1915 static bool stringToInt(const char16_t* s, size_t len, Res_value* outValue);
1916 static bool stringToFloat(const char16_t* s, size_t len, Res_value* outValue);
1918 // Used with stringToValue.
1922 inline virtual ~Accessor() { }
1924 virtual uint32_t getCustomResource(const String16& package,
1925 const String16& type,
1926 const String16& name) const = 0;
1927 virtual uint32_t getCustomResourceWithCreation(const String16& package,
1928 const String16& type,
1929 const String16& name,
1930 const bool createIfNeeded = false) = 0;
1931 virtual uint32_t getRemappedPackage(uint32_t origPackage) const = 0;
1932 virtual bool getAttributeType(uint32_t attrID, uint32_t* outType) = 0;
1933 virtual bool getAttributeMin(uint32_t attrID, uint32_t* outMin) = 0;
1934 virtual bool getAttributeMax(uint32_t attrID, uint32_t* outMax) = 0;
1935 virtual bool getAttributeEnum(uint32_t attrID,
1936 const char16_t* name, size_t nameLen,
1937 Res_value* outValue) = 0;
1938 virtual bool getAttributeFlags(uint32_t attrID,
1939 const char16_t* name, size_t nameLen,
1940 Res_value* outValue) = 0;
1941 virtual uint32_t getAttributeL10N(uint32_t attrID) = 0;
1942 virtual bool getLocalizationSetting() = 0;
1943 virtual void reportError(void* accessorCookie, const char* fmt, ...) = 0;
1946 // Convert a string to a resource value. Handles standard "@res",
1947 // "#color", "123", and "0x1bd" types; performs escaping of strings.
1948 // The resulting value is placed in 'outValue'; if it is a string type,
1949 // 'outString' receives the string. If 'attrID' is supplied, the value is
1950 // type checked against this attribute and it is used to perform enum
1951 // evaluation. If 'acccessor' is supplied, it will be used to attempt to
1952 // resolve resources that do not exist in this ResTable. If 'attrType' is
1953 // supplied, the value will be type checked for this format if 'attrID'
1954 // is not supplied or found.
1955 bool stringToValue(Res_value* outValue, String16* outString,
1956 const char16_t* s, size_t len,
1957 bool preserveSpaces, bool coerceType,
1958 uint32_t attrID = 0,
1959 const String16* defType = NULL,
1960 const String16* defPackage = NULL,
1961 Accessor* accessor = NULL,
1962 void* accessorCookie = NULL,
1963 uint32_t attrType = ResTable_map::TYPE_ANY,
1964 bool enforcePrivate = true) const;
1966 // Perform processing of escapes and quotes in a string.
1967 static bool collectString(String16* outString,
1968 const char16_t* s, size_t len,
1969 bool preserveSpaces,
1970 const char** outErrorMsg = NULL,
1971 bool append = false);
1973 size_t getBasePackageCount() const;
1974 const char16_t* getBasePackageName(size_t idx) const;
1975 uint32_t getBasePackageId(size_t idx) const;
1977 size_t getTableCount() const;
1978 const ResStringPool* getTableStringBlock(size_t index) const;
1979 void* getTableCookie(size_t index) const;
1981 // Return the configurations (ResTable_config) that we know about
1982 void getConfigurations(Vector<ResTable_config>* configs) const;
1984 void getLocales(Vector<String8>* locales) const;
1986 // Generate an idmap.
1988 // Return value: on success: NO_ERROR; caller is responsible for free-ing
1989 // outData (using free(3)). On failure, any status_t value other than
1990 // NO_ERROR; the caller should not free outData.
1991 status_t createIdmap(const ResTable& overlay, uint32_t originalCrc, uint32_t overlayCrc,
1992 void** outData, size_t* outSize) const;
1995 IDMAP_HEADER_SIZE_BYTES = 3 * sizeof(uint32_t),
1997 // Retrieve idmap meta-data.
1999 // This function only requires the idmap header (the first
2000 // IDMAP_HEADER_SIZE_BYTES) bytes of an idmap file.
2001 static bool getIdmapInfo(const void* idmap, size_t size,
2002 uint32_t* pOriginalCrc, uint32_t* pOverlayCrc);
2004 #ifndef HAVE_ANDROID_OS
2005 void print(bool inclValues) const;
2006 static String8 normalizeForOutput(const char* input);
2013 struct PackageGroup;
2016 status_t add(const void* data, size_t size, void* cookie,
2017 Asset* asset, bool copyData, const Asset* idmap);
2019 ssize_t getResourcePackageIndex(uint32_t resID) const;
2021 const Package* package, int typeIndex, int entryIndex,
2022 const ResTable_config* config,
2023 const ResTable_type** outType, const ResTable_entry** outEntry,
2024 const Type** outTypeClass) const;
2025 status_t parsePackage(
2026 const ResTable_package* const pkg, const Header* const header, uint32_t idmap_id);
2028 void print_value(const Package* pkg, const Res_value& value) const;
2030 mutable Mutex mLock;
2034 ResTable_config mParams;
2036 // Array of all resource tables.
2037 Vector<Header*> mHeaders;
2039 // Array of packages in all resource tables.
2040 Vector<PackageGroup*> mPackageGroups;
2042 // Mapping from resource package IDs to indices into the internal
2044 uint8_t mPackageMap[256];
2047 } // namespace android
2049 #endif // _LIBS_UTILS_RESOURCE_TYPES_H