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