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