Home | History | Annotate | Download | only in utils
      1 /*
      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 
     34 #include <android/configuration.h>
     35 
     36 namespace android {
     37 
     38 /** ********************************************************************
     39  *  PNG Extensions
     40  *
     41  *  New private chunks that may be placed in PNG images.
     42  *
     43  *********************************************************************** */
     44 
     45 /**
     46  * This chunk specifies how to split an image into segments for
     47  * scaling.
     48  *
     49  * There are J horizontal and K vertical segments.  These segments divide
     50  * the image into J*K regions as follows (where J=4 and K=3):
     51  *
     52  *      F0   S0    F1     S1
     53  *   +-----+----+------+-------+
     54  * S2|  0  |  1 |  2   |   3   |
     55  *   +-----+----+------+-------+
     56  *   |     |    |      |       |
     57  *   |     |    |      |       |
     58  * F2|  4  |  5 |  6   |   7   |
     59  *   |     |    |      |       |
     60  *   |     |    |      |       |
     61  *   +-----+----+------+-------+
     62  * S3|  8  |  9 |  10  |   11  |
     63  *   +-----+----+------+-------+
     64  *
     65  * Each horizontal and vertical segment is considered to by either
     66  * stretchable (marked by the Sx labels) or fixed (marked by the Fy
     67  * labels), in the horizontal or vertical axis, respectively. In the
     68  * above example, the first is horizontal segment (F0) is fixed, the
     69  * next is stretchable and then they continue to alternate. Note that
     70  * the segment list for each axis can begin or end with a stretchable
     71  * or fixed segment.
     72  *
     73  * The relative sizes of the stretchy segments indicates the relative
     74  * amount of stretchiness of the regions bordered by the segments.  For
     75  * example, regions 3, 7 and 11 above will take up more horizontal space
     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
     80  * segment lengths.
     81  *
     82  * xDivs and yDivs point to arrays of horizontal and vertical pixel
     83  * indices.  The first pair of Divs (in either array) indicate the
     84  * starting and ending points of the first stretchable segment in that
     85  * axis. The next pair specifies the next stretchable segment, etc. So
     86  * in the above example xDiv[0] and xDiv[1] specify the horizontal
     87  * coordinates for the regions labeled 1, 5 and 9.  xDiv[2] and
     88  * xDiv[3] specify the coordinates for regions 3, 7 and 11. Note that
     89  * the leftmost slices always start at x=0 and the rightmost slices
     90  * always end at the end of the image. So, for example, the regions 0,
     91  * 4 and 8 (which are fixed along the X axis) start at x value 0 and
     92  * go to xDiv[0] and slices 2, 6 and 10 start at xDiv[1] and end at
     93  * xDiv[2].
     94  *
     95  * The array pointed to by the colors field lists contains hints for
     96  * each of the regions.  They are ordered according left-to-right and
     97  * top-to-bottom as indicated above. For each segment that is a solid
     98  * color the array entry will contain that color value; otherwise it
     99  * will contain NO_COLOR.  Segments that are completely transparent
    100  * will always have the value TRANSPARENT_COLOR.
    101  *
    102  * The PNG chunk type is "npTc".
    103  */
    104 struct Res_png_9patch
    105 {
    106     Res_png_9patch() : wasDeserialized(false), xDivs(NULL),
    107                        yDivs(NULL), colors(NULL) { }
    108 
    109     int8_t wasDeserialized;
    110     int8_t numXDivs;
    111     int8_t numYDivs;
    112     int8_t numColors;
    113 
    114     // These tell where the next section of a patch starts.
    115     // For example, the first patch includes the pixels from
    116     // 0 to xDivs[0]-1 and the second patch includes the pixels
    117     // from xDivs[0] to xDivs[1]-1.
    118     // Note: allocation/free of these pointers is left to the caller.
    119     int32_t* xDivs;
    120     int32_t* yDivs;
    121 
    122     int32_t paddingLeft, paddingRight;
    123     int32_t paddingTop, paddingBottom;
    124 
    125     enum {
    126         // The 9 patch segment is not a solid color.
    127         NO_COLOR = 0x00000001,
    128 
    129         // The 9 patch segment is completely transparent.
    130         TRANSPARENT_COLOR = 0x00000000
    131     };
    132     // Note: allocation/free of this pointer is left to the caller.
    133     uint32_t* colors;
    134 
    135     // Convert data from device representation to PNG file representation.
    136     void deviceToFile();
    137     // Convert data from PNG file representation to device representation.
    138     void fileToDevice();
    139     // Serialize/Marshall the patch data into a newly malloc-ed block
    140     void* serialize();
    141     // Serialize/Marshall the patch data
    142     void serialize(void* outData);
    143     // Deserialize/Unmarshall the patch data
    144     static Res_png_9patch* deserialize(const void* data);
    145     // Compute the size of the serialized data structure
    146     size_t serializedSize();
    147 };
    148 
    149 /** ********************************************************************
    150  *  Base Types
    151  *
    152  *  These are standard types that are shared between multiple specific
    153  *  resource types.
    154  *
    155  *********************************************************************** */
    156 
    157 /**
    158  * Header that appears at the front of every data chunk in a resource.
    159  */
    160 struct ResChunk_header
    161 {
    162     // Type identifier for this chunk.  The meaning of this value depends
    163     // on the containing chunk.
    164     uint16_t type;
    165 
    166     // Size of the chunk header (in bytes).  Adding this value to
    167     // the address of the chunk allows you to find its associated data
    168     // (if any).
    169     uint16_t headerSize;
    170 
    171     // Total size of this chunk (in bytes).  This is the chunkSize plus
    172     // the size of any data associated with the chunk.  Adding this value
    173     // to the chunk allows you to completely skip its contents (including
    174     // any child chunks).  If this value is the same as chunkSize, there is
    175     // no data associated with the chunk.
    176     uint32_t size;
    177 };
    178 
    179 enum {
    180     RES_NULL_TYPE               = 0x0000,
    181     RES_STRING_POOL_TYPE        = 0x0001,
    182     RES_TABLE_TYPE              = 0x0002,
    183     RES_XML_TYPE                = 0x0003,
    184 
    185     // Chunk types in RES_XML_TYPE
    186     RES_XML_FIRST_CHUNK_TYPE    = 0x0100,
    187     RES_XML_START_NAMESPACE_TYPE= 0x0100,
    188     RES_XML_END_NAMESPACE_TYPE  = 0x0101,
    189     RES_XML_START_ELEMENT_TYPE  = 0x0102,
    190     RES_XML_END_ELEMENT_TYPE    = 0x0103,
    191     RES_XML_CDATA_TYPE          = 0x0104,
    192     RES_XML_LAST_CHUNK_TYPE     = 0x017f,
    193     // This contains a uint32_t array mapping strings in the string
    194     // pool back to resource identifiers.  It is optional.
    195     RES_XML_RESOURCE_MAP_TYPE   = 0x0180,
    196 
    197     // Chunk types in RES_TABLE_TYPE
    198     RES_TABLE_PACKAGE_TYPE      = 0x0200,
    199     RES_TABLE_TYPE_TYPE         = 0x0201,
    200     RES_TABLE_TYPE_SPEC_TYPE    = 0x0202
    201 };
    202 
    203 /**
    204  * Macros for building/splitting resource identifiers.
    205  */
    206 #define Res_VALIDID(resid) (resid != 0)
    207 #define Res_CHECKID(resid) ((resid&0xFFFF0000) != 0)
    208 #define Res_MAKEID(package, type, entry) \
    209     (((package+1)<<24) | (((type+1)&0xFF)<<16) | (entry&0xFFFF))
    210 #define Res_GETPACKAGE(id) ((id>>24)-1)
    211 #define Res_GETTYPE(id) (((id>>16)&0xFF)-1)
    212 #define Res_GETENTRY(id) (id&0xFFFF)
    213 
    214 #define Res_INTERNALID(resid) ((resid&0xFFFF0000) != 0 && (resid&0xFF0000) == 0)
    215 #define Res_MAKEINTERNAL(entry) (0x01000000 | (entry&0xFFFF))
    216 #define Res_MAKEARRAY(entry) (0x02000000 | (entry&0xFFFF))
    217 
    218 #define Res_MAXPACKAGE 255
    219 
    220 /**
    221  * Representation of a value in a resource, supplying type
    222  * information.
    223  */
    224 struct Res_value
    225 {
    226     // Number of bytes in this structure.
    227     uint16_t size;
    228 
    229     // Always set to 0.
    230     uint8_t res0;
    231 
    232     // Type of the data value.
    233     enum {
    234         // Contains no data.
    235         TYPE_NULL = 0x00,
    236         // The 'data' holds a ResTable_ref, a reference to another resource
    237         // table entry.
    238         TYPE_REFERENCE = 0x01,
    239         // The 'data' holds an attribute resource identifier.
    240         TYPE_ATTRIBUTE = 0x02,
    241         // The 'data' holds an index into the containing resource table's
    242         // global value string pool.
    243         TYPE_STRING = 0x03,
    244         // The 'data' holds a single-precision floating point number.
    245         TYPE_FLOAT = 0x04,
    246         // The 'data' holds a complex number encoding a dimension value,
    247         // such as "100in".
    248         TYPE_DIMENSION = 0x05,
    249         // The 'data' holds a complex number encoding a fraction of a
    250         // container.
    251         TYPE_FRACTION = 0x06,
    252 
    253         // Beginning of integer flavors...
    254         TYPE_FIRST_INT = 0x10,
    255 
    256         // The 'data' is a raw integer value of the form n..n.
    257         TYPE_INT_DEC = 0x10,
    258         // The 'data' is a raw integer value of the form 0xn..n.
    259         TYPE_INT_HEX = 0x11,
    260         // The 'data' is either 0 or 1, for input "false" or "true" respectively.
    261         TYPE_INT_BOOLEAN = 0x12,
    262 
    263         // Beginning of color integer flavors...
    264         TYPE_FIRST_COLOR_INT = 0x1c,
    265 
    266         // The 'data' is a raw integer value of the form #aarrggbb.
    267         TYPE_INT_COLOR_ARGB8 = 0x1c,
    268         // The 'data' is a raw integer value of the form #rrggbb.
    269         TYPE_INT_COLOR_RGB8 = 0x1d,
    270         // The 'data' is a raw integer value of the form #argb.
    271         TYPE_INT_COLOR_ARGB4 = 0x1e,
    272         // The 'data' is a raw integer value of the form #rgb.
    273         TYPE_INT_COLOR_RGB4 = 0x1f,
    274 
    275         // ...end of integer flavors.
    276         TYPE_LAST_COLOR_INT = 0x1f,
    277 
    278         // ...end of integer flavors.
    279         TYPE_LAST_INT = 0x1f
    280     };
    281     uint8_t dataType;
    282 
    283     // Structure of complex data values (TYPE_UNIT and TYPE_FRACTION)
    284     enum {
    285         // Where the unit type information is.  This gives us 16 possible
    286         // types, as defined below.
    287         COMPLEX_UNIT_SHIFT = 0,
    288         COMPLEX_UNIT_MASK = 0xf,
    289 
    290         // TYPE_DIMENSION: Value is raw pixels.
    291         COMPLEX_UNIT_PX = 0,
    292         // TYPE_DIMENSION: Value is Device Independent Pixels.
    293         COMPLEX_UNIT_DIP = 1,
    294         // TYPE_DIMENSION: Value is a Scaled device independent Pixels.
    295         COMPLEX_UNIT_SP = 2,
    296         // TYPE_DIMENSION: Value is in points.
    297         COMPLEX_UNIT_PT = 3,
    298         // TYPE_DIMENSION: Value is in inches.
    299         COMPLEX_UNIT_IN = 4,
    300         // TYPE_DIMENSION: Value is in millimeters.
    301         COMPLEX_UNIT_MM = 5,
    302 
    303         // TYPE_FRACTION: A basic fraction of the overall size.
    304         COMPLEX_UNIT_FRACTION = 0,
    305         // TYPE_FRACTION: A fraction of the parent size.
    306         COMPLEX_UNIT_FRACTION_PARENT = 1,
    307 
    308         // Where the radix information is, telling where the decimal place
    309         // appears in the mantissa.  This give us 4 possible fixed point
    310         // representations as defined below.
    311         COMPLEX_RADIX_SHIFT = 4,
    312         COMPLEX_RADIX_MASK = 0x3,
    313 
    314         // The mantissa is an integral number -- i.e., 0xnnnnnn.0
    315         COMPLEX_RADIX_23p0 = 0,
    316         // The mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn
    317         COMPLEX_RADIX_16p7 = 1,
    318         // The mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn
    319         COMPLEX_RADIX_8p15 = 2,
    320         // The mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn
    321         COMPLEX_RADIX_0p23 = 3,
    322 
    323         // Where the actual value is.  This gives us 23 bits of
    324         // precision.  The top bit is the sign.
    325         COMPLEX_MANTISSA_SHIFT = 8,
    326         COMPLEX_MANTISSA_MASK = 0xffffff
    327     };
    328 
    329     // The data for this item, as interpreted according to dataType.
    330     uint32_t data;
    331 
    332     void copyFrom_dtoh(const Res_value& src);
    333 };
    334 
    335 /**
    336  *  This is a reference to a unique entry (a ResTable_entry structure)
    337  *  in a resource table.  The value is structured as: 0xpptteeee,
    338  *  where pp is the package index, tt is the type index in that
    339  *  package, and eeee is the entry index in that type.  The package
    340  *  and type values start at 1 for the first item, to help catch cases
    341  *  where they have not been supplied.
    342  */
    343 struct ResTable_ref
    344 {
    345     uint32_t ident;
    346 };
    347 
    348 /**
    349  * Reference to a string in a string pool.
    350  */
    351 struct ResStringPool_ref
    352 {
    353     // Index into the string pool table (uint32_t-offset from the indices
    354     // immediately after ResStringPool_header) at which to find the location
    355     // of the string data in the pool.
    356     uint32_t index;
    357 };
    358 
    359 /** ********************************************************************
    360  *  String Pool
    361  *
    362  *  A set of strings that can be references by others through a
    363  *  ResStringPool_ref.
    364  *
    365  *********************************************************************** */
    366 
    367 /**
    368  * Definition for a pool of strings.  The data of this chunk is an
    369  * array of uint32_t providing indices into the pool, relative to
    370  * stringsStart.  At stringsStart are all of the UTF-16 strings
    371  * concatenated together; each starts with a uint16_t of the string's
    372  * length and each ends with a 0x0000 terminator.  If a string is >
    373  * 32767 characters, the high bit of the length is set meaning to take
    374  * those 15 bits as a high word and it will be followed by another
    375  * uint16_t containing the low word.
    376  *
    377  * If styleCount is not zero, then immediately following the array of
    378  * uint32_t indices into the string table is another array of indices
    379  * into a style table starting at stylesStart.  Each entry in the
    380  * style table is an array of ResStringPool_span structures.
    381  */
    382 struct ResStringPool_header
    383 {
    384     struct ResChunk_header header;
    385 
    386     // Number of strings in this pool (number of uint32_t indices that follow
    387     // in the data).
    388     uint32_t stringCount;
    389 
    390     // Number of style span arrays in the pool (number of uint32_t indices
    391     // follow the string indices).
    392     uint32_t styleCount;
    393 
    394     // Flags.
    395     enum {
    396         // If set, the string index is sorted by the string values (based
    397         // on strcmp16()).
    398         SORTED_FLAG = 1<<0,
    399 
    400         // String pool is encoded in UTF-8
    401         UTF8_FLAG = 1<<8
    402     };
    403     uint32_t flags;
    404 
    405     // Index from header of the string data.
    406     uint32_t stringsStart;
    407 
    408     // Index from header of the style data.
    409     uint32_t stylesStart;
    410 };
    411 
    412 /**
    413  * This structure defines a span of style information associated with
    414  * a string in the pool.
    415  */
    416 struct ResStringPool_span
    417 {
    418     enum {
    419         END = 0xFFFFFFFF
    420     };
    421 
    422     // This is the name of the span -- that is, the name of the XML
    423     // tag that defined it.  The special value END (0xFFFFFFFF) indicates
    424     // the end of an array of spans.
    425     ResStringPool_ref name;
    426 
    427     // The range of characters in the string that this span applies to.
    428     uint32_t firstChar, lastChar;
    429 };
    430 
    431 /**
    432  * Convenience class for accessing data in a ResStringPool resource.
    433  */
    434 class ResStringPool
    435 {
    436 public:
    437     ResStringPool();
    438     ResStringPool(const void* data, size_t size, bool copyData=false);
    439     ~ResStringPool();
    440 
    441     status_t setTo(const void* data, size_t size, bool copyData=false);
    442 
    443     status_t getError() const;
    444 
    445     void uninit();
    446 
    447     inline const char16_t* stringAt(const ResStringPool_ref& ref, size_t* outLen) const {
    448         return stringAt(ref.index, outLen);
    449     }
    450     const char16_t* stringAt(size_t idx, size_t* outLen) const;
    451 
    452     const char* string8At(size_t idx, size_t* outLen) const;
    453 
    454     const ResStringPool_span* styleAt(const ResStringPool_ref& ref) const;
    455     const ResStringPool_span* styleAt(size_t idx) const;
    456 
    457     ssize_t indexOfString(const char16_t* str, size_t strLen) const;
    458 
    459     size_t size() const;
    460 
    461 #ifndef HAVE_ANDROID_OS
    462     bool isUTF8() const;
    463 #endif
    464 
    465 private:
    466     status_t                    mError;
    467     void*                       mOwnedData;
    468     const ResStringPool_header* mHeader;
    469     size_t                      mSize;
    470     mutable Mutex               mDecodeLock;
    471     const uint32_t*             mEntries;
    472     const uint32_t*             mEntryStyles;
    473     const void*                 mStrings;
    474     char16_t**                  mCache;
    475     uint32_t                    mStringPoolSize;    // number of uint16_t
    476     const uint32_t*             mStyles;
    477     uint32_t                    mStylePoolSize;    // number of uint32_t
    478 };
    479 
    480 /** ********************************************************************
    481  *  XML Tree
    482  *
    483  *  Binary representation of an XML document.  This is designed to
    484  *  express everything in an XML document, in a form that is much
    485  *  easier to parse on the device.
    486  *
    487  *********************************************************************** */
    488 
    489 /**
    490  * XML tree header.  This appears at the front of an XML tree,
    491  * describing its content.  It is followed by a flat array of
    492  * ResXMLTree_node structures; the hierarchy of the XML document
    493  * is described by the occurrance of RES_XML_START_ELEMENT_TYPE
    494  * and corresponding RES_XML_END_ELEMENT_TYPE nodes in the array.
    495  */
    496 struct ResXMLTree_header
    497 {
    498     struct ResChunk_header header;
    499 };
    500 
    501 /**
    502  * Basic XML tree node.  A single item in the XML document.  Extended info
    503  * about the node can be found after header.headerSize.
    504  */
    505 struct ResXMLTree_node
    506 {
    507     struct ResChunk_header header;
    508 
    509     // Line number in original source file at which this element appeared.
    510     uint32_t lineNumber;
    511 
    512     // Optional XML comment that was associated with this element; -1 if none.
    513     struct ResStringPool_ref comment;
    514 };
    515 
    516 /**
    517  * Extended XML tree node for CDATA tags -- includes the CDATA string.
    518  * Appears header.headerSize bytes after a ResXMLTree_node.
    519  */
    520 struct ResXMLTree_cdataExt
    521 {
    522     // The raw CDATA character data.
    523     struct ResStringPool_ref data;
    524 
    525     // The typed value of the character data if this is a CDATA node.
    526     struct Res_value typedData;
    527 };
    528 
    529 /**
    530  * Extended XML tree node for namespace start/end nodes.
    531  * Appears header.headerSize bytes after a ResXMLTree_node.
    532  */
    533 struct ResXMLTree_namespaceExt
    534 {
    535     // The prefix of the namespace.
    536     struct ResStringPool_ref prefix;
    537 
    538     // The URI of the namespace.
    539     struct ResStringPool_ref uri;
    540 };
    541 
    542 /**
    543  * Extended XML tree node for element start/end nodes.
    544  * Appears header.headerSize bytes after a ResXMLTree_node.
    545  */
    546 struct ResXMLTree_endElementExt
    547 {
    548     // String of the full namespace of this element.
    549     struct ResStringPool_ref ns;
    550 
    551     // String name of this node if it is an ELEMENT; the raw
    552     // character data if this is a CDATA node.
    553     struct ResStringPool_ref name;
    554 };
    555 
    556 /**
    557  * Extended XML tree node for start tags -- includes attribute
    558  * information.
    559  * Appears header.headerSize bytes after a ResXMLTree_node.
    560  */
    561 struct ResXMLTree_attrExt
    562 {
    563     // String of the full namespace of this element.
    564     struct ResStringPool_ref ns;
    565 
    566     // String name of this node if it is an ELEMENT; the raw
    567     // character data if this is a CDATA node.
    568     struct ResStringPool_ref name;
    569 
    570     // Byte offset from the start of this structure where the attributes start.
    571     uint16_t attributeStart;
    572 
    573     // Size of the ResXMLTree_attribute structures that follow.
    574     uint16_t attributeSize;
    575 
    576     // Number of attributes associated with an ELEMENT.  These are
    577     // available as an array of ResXMLTree_attribute structures
    578     // immediately following this node.
    579     uint16_t attributeCount;
    580 
    581     // Index (1-based) of the "id" attribute. 0 if none.
    582     uint16_t idIndex;
    583 
    584     // Index (1-based) of the "class" attribute. 0 if none.
    585     uint16_t classIndex;
    586 
    587     // Index (1-based) of the "style" attribute. 0 if none.
    588     uint16_t styleIndex;
    589 };
    590 
    591 struct ResXMLTree_attribute
    592 {
    593     // Namespace of this attribute.
    594     struct ResStringPool_ref ns;
    595 
    596     // Name of this attribute.
    597     struct ResStringPool_ref name;
    598 
    599     // The original raw string value of this attribute.
    600     struct ResStringPool_ref rawValue;
    601 
    602     // Processesd typed value of this attribute.
    603     struct Res_value typedValue;
    604 };
    605 
    606 class ResXMLTree;
    607 
    608 class ResXMLParser
    609 {
    610 public:
    611     ResXMLParser(const ResXMLTree& tree);
    612 
    613     enum event_code_t {
    614         BAD_DOCUMENT = -1,
    615         START_DOCUMENT = 0,
    616         END_DOCUMENT = 1,
    617 
    618         FIRST_CHUNK_CODE = RES_XML_FIRST_CHUNK_TYPE,
    619 
    620         START_NAMESPACE = RES_XML_START_NAMESPACE_TYPE,
    621         END_NAMESPACE = RES_XML_END_NAMESPACE_TYPE,
    622         START_TAG = RES_XML_START_ELEMENT_TYPE,
    623         END_TAG = RES_XML_END_ELEMENT_TYPE,
    624         TEXT = RES_XML_CDATA_TYPE
    625     };
    626 
    627     struct ResXMLPosition
    628     {
    629         event_code_t                eventCode;
    630         const ResXMLTree_node*      curNode;
    631         const void*                 curExt;
    632     };
    633 
    634     void restart();
    635 
    636     const ResStringPool& getStrings() const;
    637 
    638     event_code_t getEventType() const;
    639     // Note, unlike XmlPullParser, the first call to next() will return
    640     // START_TAG of the first element.
    641     event_code_t next();
    642 
    643     // These are available for all nodes:
    644     int32_t getCommentID() const;
    645     const uint16_t* getComment(size_t* outLen) const;
    646     uint32_t getLineNumber() const;
    647 
    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;
    652 
    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;
    658 
    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;
    664 
    665     // Remaining methods are for retrieving information about attributes
    666     // associated with a START_TAG:
    667 
    668     size_t getAttributeCount() const;
    669 
    670     // Returns -1 if no namespace, -2 if idx out of range.
    671     int32_t getAttributeNamespaceID(size_t idx) const;
    672     const uint16_t* getAttributeNamespace(size_t idx, size_t* outLen) const;
    673 
    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;
    677 
    678     int32_t getAttributeValueStringID(size_t idx) const;
    679     const uint16_t* getAttributeStringValue(size_t idx, size_t* outLen) const;
    680 
    681     int32_t getAttributeDataType(size_t idx) const;
    682     int32_t getAttributeData(size_t idx) const;
    683     ssize_t getAttributeValue(size_t idx, Res_value* outValue) const;
    684 
    685     ssize_t indexOfAttribute(const char* ns, const char* attr) const;
    686     ssize_t indexOfAttribute(const char16_t* ns, size_t nsLen,
    687                              const char16_t* attr, size_t attrLen) const;
    688 
    689     ssize_t indexOfID() const;
    690     ssize_t indexOfClass() const;
    691     ssize_t indexOfStyle() const;
    692 
    693     void getPosition(ResXMLPosition* pos) const;
    694     void setPosition(const ResXMLPosition& pos);
    695 
    696 private:
    697     friend class ResXMLTree;
    698 
    699     event_code_t nextNode();
    700 
    701     const ResXMLTree&           mTree;
    702     event_code_t                mEventCode;
    703     const ResXMLTree_node*      mCurNode;
    704     const void*                 mCurExt;
    705 };
    706 
    707 /**
    708  * Convenience class for accessing data in a ResXMLTree resource.
    709  */
    710 class ResXMLTree : public ResXMLParser
    711 {
    712 public:
    713     ResXMLTree();
    714     ResXMLTree(const void* data, size_t size, bool copyData=false);
    715     ~ResXMLTree();
    716 
    717     status_t setTo(const void* data, size_t size, bool copyData=false);
    718 
    719     status_t getError() const;
    720 
    721     void uninit();
    722 
    723 private:
    724     friend class ResXMLParser;
    725 
    726     status_t validateNode(const ResXMLTree_node* node) const;
    727 
    728     status_t                    mError;
    729     void*                       mOwnedData;
    730     const ResXMLTree_header*    mHeader;
    731     size_t                      mSize;
    732     const uint8_t*              mDataEnd;
    733     ResStringPool               mStrings;
    734     const uint32_t*             mResIds;
    735     size_t                      mNumResIds;
    736     const ResXMLTree_node*      mRootNode;
    737     const void*                 mRootExt;
    738     event_code_t                mRootCode;
    739 };
    740 
    741 /** ********************************************************************
    742  *  RESOURCE TABLE
    743  *
    744  *********************************************************************** */
    745 
    746 /**
    747  * Header for a resource table.  Its data contains a series of
    748  * additional chunks:
    749  *   * A ResStringPool_header containing all table values.
    750  *   * One or more ResTable_package chunks.
    751  *
    752  * Specific entries within a resource table can be uniquely identified
    753  * with a single integer as defined by the ResTable_ref structure.
    754  */
    755 struct ResTable_header
    756 {
    757     struct ResChunk_header header;
    758 
    759     // The number of ResTable_package structures.
    760     uint32_t packageCount;
    761 };
    762 
    763 /**
    764  * A collection of resource data types within a package.  Followed by
    765  * one or more ResTable_type and ResTable_typeSpec structures containing the
    766  * entry values for each resource type.
    767  */
    768 struct ResTable_package
    769 {
    770     struct ResChunk_header header;
    771 
    772     // If this is a base package, its ID.  Package IDs start
    773     // at 1 (corresponding to the value of the package bits in a
    774     // resource identifier).  0 means this is not a base package.
    775     uint32_t id;
    776 
    777     // Actual name of this package, \0-terminated.
    778     char16_t name[128];
    779 
    780     // Offset to a ResStringPool_header defining the resource
    781     // type symbol table.  If zero, this package is inheriting from
    782     // another base package (overriding specific values in it).
    783     uint32_t typeStrings;
    784 
    785     // Last index into typeStrings that is for public use by others.
    786     uint32_t lastPublicType;
    787 
    788     // Offset to a ResStringPool_header defining the resource
    789     // key symbol table.  If zero, this package is inheriting from
    790     // another base package (overriding specific values in it).
    791     uint32_t keyStrings;
    792 
    793     // Last index into keyStrings that is for public use by others.
    794     uint32_t lastPublicKey;
    795 };
    796 
    797 /**
    798  * Describes a particular resource configuration.
    799  */
    800 struct ResTable_config
    801 {
    802     // Number of bytes in this structure.
    803     uint32_t size;
    804 
    805     union {
    806         struct {
    807             // Mobile country code (from SIM).  0 means "any".
    808             uint16_t mcc;
    809             // Mobile network code (from SIM).  0 means "any".
    810             uint16_t mnc;
    811         };
    812         uint32_t imsi;
    813     };
    814 
    815     union {
    816         struct {
    817             // \0\0 means "any".  Otherwise, en, fr, etc.
    818             char language[2];
    819 
    820             // \0\0 means "any".  Otherwise, US, CA, etc.
    821             char country[2];
    822         };
    823         uint32_t locale;
    824     };
    825 
    826     enum {
    827         ORIENTATION_ANY  = ACONFIGURATION_ORIENTATION_ANY,
    828         ORIENTATION_PORT = ACONFIGURATION_ORIENTATION_PORT,
    829         ORIENTATION_LAND = ACONFIGURATION_ORIENTATION_LAND,
    830         ORIENTATION_SQUARE = ACONFIGURATION_ORIENTATION_SQUARE,
    831     };
    832 
    833     enum {
    834         TOUCHSCREEN_ANY  = ACONFIGURATION_TOUCHSCREEN_ANY,
    835         TOUCHSCREEN_NOTOUCH  = ACONFIGURATION_TOUCHSCREEN_NOTOUCH,
    836         TOUCHSCREEN_STYLUS  = ACONFIGURATION_TOUCHSCREEN_STYLUS,
    837         TOUCHSCREEN_FINGER  = ACONFIGURATION_TOUCHSCREEN_FINGER,
    838     };
    839 
    840     enum {
    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
    846     };
    847 
    848     union {
    849         struct {
    850             uint8_t orientation;
    851             uint8_t touchscreen;
    852             uint16_t density;
    853         };
    854         uint32_t screenType;
    855     };
    856 
    857     enum {
    858         KEYBOARD_ANY  = ACONFIGURATION_KEYBOARD_ANY,
    859         KEYBOARD_NOKEYS  = ACONFIGURATION_KEYBOARD_NOKEYS,
    860         KEYBOARD_QWERTY  = ACONFIGURATION_KEYBOARD_QWERTY,
    861         KEYBOARD_12KEY  = ACONFIGURATION_KEYBOARD_12KEY,
    862     };
    863 
    864     enum {
    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,
    870     };
    871 
    872     enum {
    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,
    878     };
    879 
    880     enum {
    881         MASK_NAVHIDDEN = 0x000c,
    882         SHIFT_NAVHIDDEN = 2,
    883         NAVHIDDEN_ANY = ACONFIGURATION_NAVHIDDEN_ANY << SHIFT_NAVHIDDEN,
    884         NAVHIDDEN_NO = ACONFIGURATION_NAVHIDDEN_NO << SHIFT_NAVHIDDEN,
    885         NAVHIDDEN_YES = ACONFIGURATION_NAVHIDDEN_YES << SHIFT_NAVHIDDEN,
    886     };
    887 
    888     union {
    889         struct {
    890             uint8_t keyboard;
    891             uint8_t navigation;
    892             uint8_t inputFlags;
    893             uint8_t inputPad0;
    894         };
    895         uint32_t input;
    896     };
    897 
    898     enum {
    899         SCREENWIDTH_ANY = 0
    900     };
    901 
    902     enum {
    903         SCREENHEIGHT_ANY = 0
    904     };
    905 
    906     union {
    907         struct {
    908             uint16_t screenWidth;
    909             uint16_t screenHeight;
    910         };
    911         uint32_t screenSize;
    912     };
    913 
    914     enum {
    915         SDKVERSION_ANY = 0
    916     };
    917 
    918     enum {
    919         MINORVERSION_ANY = 0
    920     };
    921 
    922     union {
    923         struct {
    924             uint16_t sdkVersion;
    925             // For now minorVersion must always be 0!!!  Its meaning
    926             // is currently undefined.
    927             uint16_t minorVersion;
    928         };
    929         uint32_t version;
    930     };
    931 
    932     enum {
    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,
    940 
    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,
    947     };
    948 
    949     enum {
    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,
    956 
    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,
    963     };
    964 
    965     union {
    966         struct {
    967             uint8_t screenLayout;
    968             uint8_t uiMode;
    969             uint8_t screenConfigPad1;
    970             uint8_t screenConfigPad2;
    971         };
    972         uint32_t screenConfig;
    973     };
    974 
    975     inline void copyFromDeviceNoSwap(const ResTable_config& o) {
    976         const size_t size = dtohl(o.size);
    977         if (size >= sizeof(ResTable_config)) {
    978             *this = o;
    979         } else {
    980             memcpy(this, &o, size);
    981             memset(((uint8_t*)this)+size, 0, sizeof(ResTable_config)-size);
    982         }
    983     }
    984 
    985     inline void copyFromDtoH(const ResTable_config& o) {
    986         copyFromDeviceNoSwap(o);
    987         size = sizeof(ResTable_config);
    988         mcc = dtohs(mcc);
    989         mnc = dtohs(mnc);
    990         density = dtohs(density);
    991         screenWidth = dtohs(screenWidth);
    992         screenHeight = dtohs(screenHeight);
    993         sdkVersion = dtohs(sdkVersion);
    994         minorVersion = dtohs(minorVersion);
    995     }
    996 
    997     inline void swapHtoD() {
    998         size = htodl(size);
    999         mcc = htods(mcc);
   1000         mnc = htods(mnc);
   1001         density = htods(density);
   1002         screenWidth = htods(screenWidth);
   1003         screenHeight = htods(screenHeight);
   1004         sdkVersion = htods(sdkVersion);
   1005         minorVersion = htods(minorVersion);
   1006     }
   1007 
   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);
   1024         return (int)diff;
   1025     }
   1026 
   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.
   1030     enum {
   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
   1044     };
   1045 
   1046     // Compare two configuration, returning CONFIG_* flags set for each value
   1047     // that is different.
   1048     inline int diff(const ResTable_config& o) const {
   1049         int diffs = 0;
   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;
   1064         return diffs;
   1065     }
   1066 
   1067     // Return true if 'this' is more specific than 'o'.
   1068     inline bool
   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) {
   1074             if (mcc != o.mcc) {
   1075                 if (!mcc) return false;
   1076                 if (!o.mcc) return true;
   1077             }
   1078 
   1079             if (mnc != o.mnc) {
   1080                 if (!mnc) return false;
   1081                 if (!o.mnc) return true;
   1082             }
   1083         }
   1084 
   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;
   1089             }
   1090 
   1091             if (country[0] != o.country[0]) {
   1092                 if (!country[0]) return false;
   1093                 if (!o.country[0]) return true;
   1094             }
   1095         }
   1096 
   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;
   1101             }
   1102             if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0) {
   1103                 if (!(screenLayout & MASK_SCREENLONG)) return false;
   1104                 if (!(o.screenLayout & MASK_SCREENLONG)) return true;
   1105             }
   1106         }
   1107 
   1108         if (orientation != o.orientation) {
   1109             if (!orientation) return false;
   1110             if (!o.orientation) return true;
   1111         }
   1112 
   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;
   1117             }
   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;
   1121             }
   1122         }
   1123 
   1124         // density is never 'more specific'
   1125         // as the default just equals 160
   1126 
   1127         if (touchscreen != o.touchscreen) {
   1128             if (!touchscreen) return false;
   1129             if (!o.touchscreen) return true;
   1130         }
   1131 
   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;
   1136             }
   1137 
   1138             if (((inputFlags^o.inputFlags) & MASK_NAVHIDDEN) != 0) {
   1139                 if (!(inputFlags & MASK_NAVHIDDEN)) return false;
   1140                 if (!(o.inputFlags & MASK_NAVHIDDEN)) return true;
   1141             }
   1142 
   1143             if (keyboard != o.keyboard) {
   1144                 if (!keyboard) return false;
   1145                 if (!o.keyboard) return true;
   1146             }
   1147 
   1148             if (navigation != o.navigation) {
   1149                 if (!navigation) return false;
   1150                 if (!o.navigation) return true;
   1151             }
   1152         }
   1153 
   1154         if (screenSize || o.screenSize) {
   1155             if (screenWidth != o.screenWidth) {
   1156                 if (!screenWidth) return false;
   1157                 if (!o.screenWidth) return true;
   1158             }
   1159 
   1160             if (screenHeight != o.screenHeight) {
   1161                 if (!screenHeight) return false;
   1162                 if (!o.screenHeight) return true;
   1163             }
   1164         }
   1165 
   1166         if (version || o.version) {
   1167             if (sdkVersion != o.sdkVersion) {
   1168                 if (!sdkVersion) return false;
   1169                 if (!o.sdkVersion) return true;
   1170             }
   1171 
   1172             if (minorVersion != o.minorVersion) {
   1173                 if (!minorVersion) return false;
   1174                 if (!o.minorVersion) return true;
   1175             }
   1176         }
   1177         return false;
   1178     }
   1179 
   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).
   1190     inline bool
   1191     isBetterThan(const ResTable_config& o,
   1192             const ResTable_config* requested) const {
   1193         if (requested) {
   1194             if (imsi || o.imsi) {
   1195                 if ((mcc != o.mcc) && requested->mcc) {
   1196                     return (mcc);
   1197                 }
   1198 
   1199                 if ((mnc != o.mnc) && requested->mnc) {
   1200                     return (mnc);
   1201                 }
   1202             }
   1203 
   1204             if (locale || o.locale) {
   1205                 if ((language[0] != o.language[0]) && requested->language[0]) {
   1206                     return (language[0]);
   1207                 }
   1208 
   1209                 if ((country[0] != o.country[0]) && requested->country[0]) {
   1210                     return (country[0]);
   1211                 }
   1212             }
   1213 
   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;
   1224                     int fixedOSL = oSL;
   1225                     if ((requested->screenLayout & MASK_SCREENSIZE) >= SCREENSIZE_NORMAL) {
   1226                         if (fixedMySL == 0) fixedMySL = SCREENSIZE_NORMAL;
   1227                         if (fixedOSL == 0) fixedOSL = SCREENSIZE_NORMAL;
   1228                     }
   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;
   1236                         return true;
   1237                     }
   1238                     return fixedMySL >= fixedOSL;
   1239                 }
   1240                 if (((screenLayout^o.screenLayout) & MASK_SCREENLONG) != 0
   1241                         && (requested->screenLayout & MASK_SCREENLONG)) {
   1242                     return (screenLayout & MASK_SCREENLONG);
   1243                 }
   1244             }
   1245 
   1246             if ((orientation != o.orientation) && requested->orientation) {
   1247                 return (orientation);
   1248             }
   1249 
   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);
   1254                 }
   1255                 if (((uiMode^o.uiMode) & MASK_UI_MODE_NIGHT) != 0
   1256                         && (requested->uiMode & MASK_UI_MODE_NIGHT)) {
   1257                     return (uiMode & MASK_UI_MODE_NIGHT);
   1258                 }
   1259             }
   1260 
   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;
   1271                     if (l > h) {
   1272                         int t = h;
   1273                         h = l;
   1274                         l = t;
   1275                         bImBigger = false;
   1276                     }
   1277 
   1278                     int reqValue = (requested->density?requested->density:160);
   1279                     if (reqValue >= h) {
   1280                         // requested value higher than both l and h, give h
   1281                         return bImBigger;
   1282                     }
   1283                     if (l >= reqValue) {
   1284                         // requested value lower than both l and h, give l
   1285                         return !bImBigger;
   1286                     }
   1287                     // saying that scaling down is 2x better than up
   1288                     if (((2 * l) - reqValue) * h > reqValue * reqValue) {
   1289                         return !bImBigger;
   1290                     } else {
   1291                         return bImBigger;
   1292                     }
   1293                 }
   1294 
   1295                 if ((touchscreen != o.touchscreen) && requested->touchscreen) {
   1296                     return (touchscreen);
   1297                 }
   1298             }
   1299 
   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) {
   1307 
   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;
   1315                     }
   1316                 }
   1317 
   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;
   1323                     if (reqNavHidden) {
   1324 
   1325                         if (!navHidden) return false;
   1326                         if (!oNavHidden) return true;
   1327                     }
   1328                 }
   1329 
   1330                 if ((keyboard != o.keyboard) && requested->keyboard) {
   1331                     return (keyboard);
   1332                 }
   1333 
   1334                 if ((navigation != o.navigation) && requested->navigation) {
   1335                     return (navigation);
   1336                 }
   1337             }
   1338 
   1339             if (screenSize || o.screenSize) {
   1340                 if ((screenWidth != o.screenWidth) && requested->screenWidth) {
   1341                     return (screenWidth);
   1342                 }
   1343 
   1344                 if ((screenHeight != o.screenHeight) &&
   1345                         requested->screenHeight) {
   1346                     return (screenHeight);
   1347                 }
   1348             }
   1349 
   1350             if (version || o.version) {
   1351                 if ((sdkVersion != o.sdkVersion) && requested->sdkVersion) {
   1352                     return (sdkVersion > o.sdkVersion);
   1353                 }
   1354 
   1355                 if ((minorVersion != o.minorVersion) &&
   1356                         requested->minorVersion) {
   1357                     return (minorVersion);
   1358                 }
   1359             }
   1360 
   1361             return false;
   1362         }
   1363         return isMoreSpecificThan(o);
   1364     }
   1365 
   1366     // Return true if 'this' can be considered a match for the parameters in
   1367     // 'settings'.
   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 {
   1373         if (imsi != 0) {
   1374             if ((settings.mcc != 0 && mcc != 0
   1375                  && mcc != settings.mcc) ||
   1376                 (settings.mcc == 0 && mcc != 0)) {
   1377                 return false;
   1378             }
   1379             if ((settings.mnc != 0 && mnc != 0
   1380                  && mnc != settings.mnc) ||
   1381                 (settings.mnc == 0 && mnc != 0)) {
   1382                 return false;
   1383             }
   1384         }
   1385         if (locale != 0) {
   1386             if (settings.language[0] != 0 && language[0] != 0
   1387                 && (language[0] != settings.language[0]
   1388                     || language[1] != settings.language[1])) {
   1389                 return false;
   1390             }
   1391             if (settings.country[0] != 0 && country[0] != 0
   1392                 && (country[0] != settings.country[0]
   1393                     || country[1] != settings.country[1])) {
   1394                 return false;
   1395             }
   1396         }
   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
   1401             // match.
   1402             if ((setScreenSize != 0 && screenSize != 0
   1403                     && screenSize > setScreenSize) ||
   1404                     (setScreenSize == 0 && screenSize != 0)) {
   1405                 return false;
   1406             }
   1407 
   1408             const int screenLong = screenLayout&MASK_SCREENLONG;
   1409             const int setScreenLong = settings.screenLayout&MASK_SCREENLONG;
   1410             if (setScreenLong != 0 && screenLong != 0
   1411                     && screenLong != setScreenLong) {
   1412                 return false;
   1413             }
   1414 
   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) {
   1419                 return false;
   1420             }
   1421 
   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) {
   1426                 return false;
   1427             }
   1428         }
   1429         if (screenType != 0) {
   1430             if (settings.orientation != 0 && orientation != 0
   1431                 && orientation != settings.orientation) {
   1432                 return false;
   1433             }
   1434             // density always matches - we can scale it.  See isBetterThan
   1435             if (settings.touchscreen != 0 && touchscreen != 0
   1436                 && touchscreen != settings.touchscreen) {
   1437                 return false;
   1438             }
   1439         }
   1440         if (input != 0) {
   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!");
   1451                     return false;
   1452                 }
   1453             }
   1454             const int navHidden = inputFlags&MASK_NAVHIDDEN;
   1455             const int setNavHidden = settings.inputFlags&MASK_NAVHIDDEN;
   1456             if (setNavHidden != 0 && navHidden != 0
   1457                 && navHidden != setNavHidden) {
   1458                 return false;
   1459             }
   1460             if (settings.keyboard != 0 && keyboard != 0
   1461                 && keyboard != settings.keyboard) {
   1462                 return false;
   1463             }
   1464             if (settings.navigation != 0 && navigation != 0
   1465                 && navigation != settings.navigation) {
   1466                 return false;
   1467             }
   1468         }
   1469         if (screenSize != 0) {
   1470             if (settings.screenWidth != 0 && screenWidth != 0
   1471                 && screenWidth != settings.screenWidth) {
   1472                 return false;
   1473             }
   1474             if (settings.screenHeight != 0 && screenHeight != 0
   1475                 && screenHeight != settings.screenHeight) {
   1476                 return false;
   1477             }
   1478         }
   1479         if (version != 0) {
   1480             if (settings.sdkVersion != 0 && sdkVersion != 0
   1481                 && sdkVersion > settings.sdkVersion) {
   1482                 return false;
   1483             }
   1484             if (settings.minorVersion != 0 && minorVersion != 0
   1485                 && minorVersion != settings.minorVersion) {
   1486                 return false;
   1487             }
   1488         }
   1489         return true;
   1490     }
   1491 
   1492     void getLocale(char str[6]) const {
   1493         memset(str, 0, 6);
   1494         if (language[0]) {
   1495             str[0] = language[0];
   1496             str[1] = language[1];
   1497             if (country[0]) {
   1498                 str[2] = '_';
   1499                 str[3] = country[0];
   1500                 str[4] = country[1];
   1501             }
   1502         }
   1503     }
   1504 
   1505     String8 toString() const {
   1506         char buf[200];
   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",
   1510                 mcc, mnc,
   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);
   1519     }
   1520 };
   1521 
   1522 /**
   1523  * A specification of the resources defined by a particular type.
   1524  *
   1525  * There should be one of these chunks for each resource type.
   1526  *
   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.
   1531  */
   1532 struct ResTable_typeSpec
   1533 {
   1534     struct ResChunk_header header;
   1535 
   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.
   1539     uint8_t id;
   1540 
   1541     // Must be 0.
   1542     uint8_t res0;
   1543     // Must be 0.
   1544     uint16_t res1;
   1545 
   1546     // Number of uint32_t entry configuration masks that follow.
   1547     uint32_t entryCount;
   1548 
   1549     enum {
   1550         // Additional flag indicating an entry is public.
   1551         SPEC_PUBLIC = 0x40000000
   1552     };
   1553 };
   1554 
   1555 /**
   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
   1561  * not defined.
   1562  *
   1563  * There may be multiple of these chunks for a particular resource type,
   1564  * supply different configuration variations for the resource values of
   1565  * that type.
   1566  *
   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.
   1569  */
   1570 struct ResTable_type
   1571 {
   1572     struct ResChunk_header header;
   1573 
   1574     enum {
   1575         NO_ENTRY = 0xFFFFFFFF
   1576     };
   1577 
   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.
   1581     uint8_t id;
   1582 
   1583     // Must be 0.
   1584     uint8_t res0;
   1585     // Must be 0.
   1586     uint16_t res1;
   1587 
   1588     // Number of uint32_t entry indices that follow.
   1589     uint32_t entryCount;
   1590 
   1591     // Offset from header where ResTable_entry data starts.
   1592     uint32_t entriesStart;
   1593 
   1594     // Configuration this collection of entries is designed for.
   1595     ResTable_config config;
   1596 };
   1597 
   1598 /**
   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.
   1605  */
   1606 struct ResTable_entry
   1607 {
   1608     // Number of bytes in this structure.
   1609     uint16_t size;
   1610 
   1611     enum {
   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
   1618     };
   1619     uint16_t flags;
   1620 
   1621     // Reference into ResTable_package::keyStrings identifying this entry.
   1622     struct ResStringPool_ref key;
   1623 };
   1624 
   1625 /**
   1626  * Extended form of a ResTable_entry for map entries, defining a parent map
   1627  * resource from which to inherit values.
   1628  */
   1629 struct ResTable_map_entry : public ResTable_entry
   1630 {
   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.
   1634     uint32_t count;
   1635 };
   1636 
   1637 /**
   1638  * A single name/value mapping that is part of a complex resource
   1639  * entry.
   1640  */
   1641 struct ResTable_map
   1642 {
   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.
   1647     ResTable_ref name;
   1648 
   1649     // Special values for 'name' when defining attribute resources.
   1650     enum {
   1651         // This entry holds the attribute's type code.
   1652         ATTR_TYPE = Res_MAKEINTERNAL(0),
   1653 
   1654         // For integral attributes, this is the minimum value it can hold.
   1655         ATTR_MIN = Res_MAKEINTERNAL(1),
   1656 
   1657         // For integral attributes, this is the maximum value it can hold.
   1658         ATTR_MAX = Res_MAKEINTERNAL(2),
   1659 
   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),
   1663 
   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)
   1671 
   1672     };
   1673 
   1674     // Bit mask of allowed types, for use with ATTR_TYPE.
   1675     enum {
   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,
   1681 
   1682         // Attribute holds a references to another resource.
   1683         TYPE_REFERENCE = 1<<0,
   1684 
   1685         // Attribute holds a generic string.
   1686         TYPE_STRING = 1<<1,
   1687 
   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,
   1691 
   1692         // Attribute holds a boolean integer.
   1693         TYPE_BOOLEAN = 1<<3,
   1694 
   1695         // Attribute holds a color value.
   1696         TYPE_COLOR = 1<<4,
   1697 
   1698         // Attribute holds a floating point value.
   1699         TYPE_FLOAT = 1<<5,
   1700 
   1701         // Attribute holds a dimension value, such as "20px".
   1702         TYPE_DIMENSION = 1<<6,
   1703 
   1704         // Attribute holds a fraction value, such as "20%".
   1705         TYPE_FRACTION = 1<<7,
   1706 
   1707         // Attribute holds an enumeration.  The enumeration values are
   1708         // supplied as additional entries in the map.
   1709         TYPE_ENUM = 1<<16,
   1710 
   1711         // Attribute holds a bitmaks of flags.  The flag bit values are
   1712         // supplied as additional entries in the map.
   1713         TYPE_FLAGS = 1<<17
   1714     };
   1715 
   1716     // Enum of localization modes, for use with ATTR_L10N.
   1717     enum {
   1718         L10N_NOT_REQUIRED = 0,
   1719         L10N_SUGGESTED    = 1
   1720     };
   1721 
   1722     // This mapping's value.
   1723     Res_value value;
   1724 };
   1725 
   1726 /**
   1727  * Convenience class for accessing data in a ResTable resource.
   1728  */
   1729 class ResTable
   1730 {
   1731 public:
   1732     ResTable();
   1733     ResTable(const void* data, size_t size, void* cookie,
   1734              bool copyData=false);
   1735     ~ResTable();
   1736 
   1737     status_t add(const void* data, size_t size, void* cookie,
   1738                  bool copyData=false);
   1739     status_t add(Asset* asset, void* cookie,
   1740                  bool copyData=false);
   1741     status_t add(ResTable* src);
   1742 
   1743     status_t getError() const;
   1744 
   1745     void uninit();
   1746 
   1747     struct resource_name
   1748     {
   1749         const char16_t* package;
   1750         size_t packageLen;
   1751         const char16_t* type;
   1752         size_t typeLen;
   1753         const char16_t* name;
   1754         size_t nameLen;
   1755     };
   1756 
   1757     bool getResourceName(uint32_t resID, resource_name* outName) const;
   1758 
   1759     /**
   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.
   1764      *
   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.
   1768      *
   1769      * @param resID The desired resoruce identifier.
   1770      * @param outValue Filled in with the resource data that was found.
   1771      *
   1772      * @return ssize_t Either a >= 0 table index or a negative error code.
   1773      */
   1774     ssize_t getResource(uint32_t resID, Res_value* outValue, bool mayBeBag=false,
   1775             uint32_t* outSpecFlags=NULL, ResTable_config* outConfig=NULL) const;
   1776 
   1777     inline ssize_t getResource(const ResTable_ref& res, Res_value* outValue,
   1778             uint32_t* outSpecFlags=NULL) const {
   1779         return getResource(res.ident, outValue, false, outSpecFlags, NULL);
   1780     }
   1781 
   1782     ssize_t resolveReference(Res_value* inOutValue,
   1783                              ssize_t blockIndex,
   1784                              uint32_t* outLastRef = NULL,
   1785                              uint32_t* inoutTypeSpecFlags = NULL,
   1786                              ResTable_config* outConfig = NULL) const;
   1787 
   1788     enum {
   1789         TMP_BUFFER_SIZE = 16
   1790     };
   1791     const char16_t* valueToString(const Res_value* value, size_t stringBlock,
   1792                                   char16_t tmpBuffer[TMP_BUFFER_SIZE],
   1793                                   size_t* outLen);
   1794 
   1795     struct bag_entry {
   1796         ssize_t stringBlock;
   1797         ResTable_map map;
   1798     };
   1799 
   1800     /**
   1801      * Retrieve the bag of a resource.  If the resoruce is found, returns the
   1802      * number of bags it contains and 'outBag' points to an array of their
   1803      * values.  If not found, a negative error code is returned.
   1804      *
   1805      * Note that this function -does- do reference traversal of the bag data.
   1806      *
   1807      * @param resID The desired resource identifier.
   1808      * @param outBag Filled inm with a pointer to the bag mappings.
   1809      *
   1810      * @return ssize_t Either a >= 0 bag count of negative error code.
   1811      */
   1812     ssize_t lockBag(uint32_t resID, const bag_entry** outBag) const;
   1813 
   1814     void unlockBag(const bag_entry* bag) const;
   1815 
   1816     void lock() const;
   1817 
   1818     ssize_t getBagLocked(uint32_t resID, const bag_entry** outBag,
   1819             uint32_t* outTypeSpecFlags=NULL) const;
   1820 
   1821     void unlock() const;
   1822 
   1823     class Theme {
   1824     public:
   1825         Theme(const ResTable& table);
   1826         ~Theme();
   1827 
   1828         inline const ResTable& getResTable() const { return mTable; }
   1829 
   1830         status_t applyStyle(uint32_t resID, bool force=false);
   1831         status_t setTo(const Theme& other);
   1832 
   1833         /**
   1834          * Retrieve a value in the theme.  If the theme defines this
   1835          * value, returns a value >= 0 indicating the table it is in
   1836          * (for use with getTableStringBlock() and getTableCookie) and
   1837          * fills in 'outValue'.  If not found, returns a negative error
   1838          * code.
   1839          *
   1840          * Note that this function does not do reference traversal.  If you want
   1841          * to follow references to other resources to get the "real" value to
   1842          * use, you need to call resolveReference() after this function.
   1843          *
   1844          * @param resID A resource identifier naming the desired theme
   1845          *              attribute.
   1846          * @param outValue Filled in with the theme value that was
   1847          *                 found.
   1848          *
   1849          * @return ssize_t Either a >= 0 table index or a negative error code.
   1850          */
   1851         ssize_t getAttribute(uint32_t resID, Res_value* outValue,
   1852                 uint32_t* outTypeSpecFlags = NULL) const;
   1853 
   1854         /**
   1855          * This is like ResTable::resolveReference(), but also takes
   1856          * care of resolving attribute references to the theme.
   1857          */
   1858         ssize_t resolveAttributeReference(Res_value* inOutValue,
   1859                 ssize_t blockIndex, uint32_t* outLastRef = NULL,
   1860                 uint32_t* inoutTypeSpecFlags = NULL,
   1861                 ResTable_config* inoutConfig = NULL) const;
   1862 
   1863         void dumpToLog() const;
   1864 
   1865     private:
   1866         Theme(const Theme&);
   1867         Theme& operator=(const Theme&);
   1868 
   1869         struct theme_entry {
   1870             ssize_t stringBlock;
   1871             uint32_t typeSpecFlags;
   1872             Res_value value;
   1873         };
   1874         struct type_info {
   1875             size_t numEntries;
   1876             theme_entry* entries;
   1877         };
   1878         struct package_info {
   1879             size_t numTypes;
   1880             type_info types[];
   1881         };
   1882 
   1883         void free_package(package_info* pi);
   1884         package_info* copy_package(package_info* pi);
   1885 
   1886         const ResTable& mTable;
   1887         package_info*   mPackages[Res_MAXPACKAGE];
   1888     };
   1889 
   1890     void setParameters(const ResTable_config* params);
   1891     void getParameters(ResTable_config* params) const;
   1892 
   1893     // Retrieve an identifier (which can be passed to getResource)
   1894     // for a given resource name.  The 'name' can be fully qualified
   1895     // (<package>:<type>.<basename>) or the package or type components
   1896     // can be dropped if default values are supplied here.
   1897     //
   1898     // Returns 0 if no such resource was found, else a valid resource ID.
   1899     uint32_t identifierForName(const char16_t* name, size_t nameLen,
   1900                                const char16_t* type = 0, size_t typeLen = 0,
   1901                                const char16_t* defPackage = 0,
   1902                                size_t defPackageLen = 0,
   1903                                uint32_t* outTypeSpecFlags = NULL) const;
   1904 
   1905     static bool expandResourceRef(const uint16_t* refStr, size_t refLen,
   1906                                   String16* outPackage,
   1907                                   String16* outType,
   1908                                   String16* outName,
   1909                                   const String16* defType = NULL,
   1910                                   const String16* defPackage = NULL,
   1911                                   const char** outErrorMsg = NULL);
   1912 
   1913     static bool stringToInt(const char16_t* s, size_t len, Res_value* outValue);
   1914     static bool stringToFloat(const char16_t* s, size_t len, Res_value* outValue);
   1915 
   1916     // Used with stringToValue.
   1917     class Accessor
   1918     {
   1919     public:
   1920         inline virtual ~Accessor() { }
   1921 
   1922         virtual uint32_t getCustomResource(const String16& package,
   1923                                            const String16& type,
   1924                                            const String16& name) const = 0;
   1925         virtual uint32_t getCustomResourceWithCreation(const String16& package,
   1926                                                        const String16& type,
   1927                                                        const String16& name,
   1928                                                        const bool createIfNeeded = false) = 0;
   1929         virtual uint32_t getRemappedPackage(uint32_t origPackage) const = 0;
   1930         virtual bool getAttributeType(uint32_t attrID, uint32_t* outType) = 0;
   1931         virtual bool getAttributeMin(uint32_t attrID, uint32_t* outMin) = 0;
   1932         virtual bool getAttributeMax(uint32_t attrID, uint32_t* outMax) = 0;
   1933         virtual bool getAttributeEnum(uint32_t attrID,
   1934                                       const char16_t* name, size_t nameLen,
   1935                                       Res_value* outValue) = 0;
   1936         virtual bool getAttributeFlags(uint32_t attrID,
   1937                                        const char16_t* name, size_t nameLen,
   1938                                        Res_value* outValue) = 0;
   1939         virtual uint32_t getAttributeL10N(uint32_t attrID) = 0;
   1940         virtual bool getLocalizationSetting() = 0;
   1941         virtual void reportError(void* accessorCookie, const char* fmt, ...) = 0;
   1942     };
   1943 
   1944     // Convert a string to a resource value.  Handles standard "@res",
   1945     // "#color", "123", and "0x1bd" types; performs escaping of strings.
   1946     // The resulting value is placed in 'outValue'; if it is a string type,
   1947     // 'outString' receives the string.  If 'attrID' is supplied, the value is
   1948     // type checked against this attribute and it is used to perform enum
   1949     // evaluation.  If 'acccessor' is supplied, it will be used to attempt to
   1950     // resolve resources that do not exist in this ResTable.  If 'attrType' is
   1951     // supplied, the value will be type checked for this format if 'attrID'
   1952     // is not supplied or found.
   1953     bool stringToValue(Res_value* outValue, String16* outString,
   1954                        const char16_t* s, size_t len,
   1955                        bool preserveSpaces, bool coerceType,
   1956                        uint32_t attrID = 0,
   1957                        const String16* defType = NULL,
   1958                        const String16* defPackage = NULL,
   1959                        Accessor* accessor = NULL,
   1960                        void* accessorCookie = NULL,
   1961                        uint32_t attrType = ResTable_map::TYPE_ANY,
   1962                        bool enforcePrivate = true) const;
   1963 
   1964     // Perform processing of escapes and quotes in a string.
   1965     static bool collectString(String16* outString,
   1966                               const char16_t* s, size_t len,
   1967                               bool preserveSpaces,
   1968                               const char** outErrorMsg = NULL,
   1969                               bool append = false);
   1970 
   1971     size_t getBasePackageCount() const;
   1972     const char16_t* getBasePackageName(size_t idx) const;
   1973     uint32_t getBasePackageId(size_t idx) const;
   1974 
   1975     size_t getTableCount() const;
   1976     const ResStringPool* getTableStringBlock(size_t index) const;
   1977     void* getTableCookie(size_t index) const;
   1978 
   1979     // Return the configurations (ResTable_config) that we know about
   1980     void getConfigurations(Vector<ResTable_config>* configs) const;
   1981 
   1982     void getLocales(Vector<String8>* locales) const;
   1983 
   1984 #ifndef HAVE_ANDROID_OS
   1985     void print(bool inclValues) const;
   1986 #endif
   1987 
   1988 private:
   1989     struct Header;
   1990     struct Type;
   1991     struct Package;
   1992     struct PackageGroup;
   1993     struct bag_set;
   1994 
   1995     status_t add(const void* data, size_t size, void* cookie,
   1996                  Asset* asset, bool copyData);
   1997 
   1998     ssize_t getResourcePackageIndex(uint32_t resID) const;
   1999     ssize_t getEntry(
   2000         const Package* package, int typeIndex, int entryIndex,
   2001         const ResTable_config* config,
   2002         const ResTable_type** outType, const ResTable_entry** outEntry,
   2003         const Type** outTypeClass) const;
   2004     status_t parsePackage(
   2005         const ResTable_package* const pkg, const Header* const header);
   2006 
   2007     void print_value(const Package* pkg, const Res_value& value) const;
   2008 
   2009     mutable Mutex               mLock;
   2010 
   2011     status_t                    mError;
   2012 
   2013     ResTable_config             mParams;
   2014 
   2015     // Array of all resource tables.
   2016     Vector<Header*>             mHeaders;
   2017 
   2018     // Array of packages in all resource tables.
   2019     Vector<PackageGroup*>       mPackageGroups;
   2020 
   2021     // Mapping from resource package IDs to indices into the internal
   2022     // package array.
   2023     uint8_t                     mPackageMap[256];
   2024 };
   2025 
   2026 }   // namespace android
   2027 
   2028 #endif // _LIBS_UTILS_RESOURCE_TYPES_H
   2029