Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2006 The Android Open Source Project |
| 3 | // |
| 4 | // Build resource files from raw assets. |
| 5 | // |
| 6 | |
| 7 | #include "ResourceTable.h" |
| 8 | |
Adam Lesinski | de7de47 | 2014-11-03 12:03:08 -0800 | [diff] [blame] | 9 | #include "AaptUtil.h" |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 10 | #include "XMLNode.h" |
| 11 | #include "ResourceFilter.h" |
| 12 | #include "ResourceIdCache.h" |
Adam Lesinski | dcdfe9f | 2014-11-06 12:54:36 -0800 | [diff] [blame] | 13 | #include "SdkConstants.h" |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 14 | |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 15 | #include <algorithm> |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 16 | #include <androidfw/ResourceTypes.h> |
| 17 | #include <utils/ByteOrder.h> |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 18 | #include <utils/TypeHelpers.h> |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 19 | #include <stdarg.h> |
| 20 | |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 21 | // SSIZE: mingw does not have signed size_t == ssize_t. |
| 22 | // STATUST: mingw does seem to redefine UNKNOWN_ERROR from our enum value, so a cast is necessary. |
Elliott Hughes | b12f241 | 2015-04-03 12:56:45 -0700 | [diff] [blame] | 23 | #if !defined(_WIN32) |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 24 | # define SSIZE(x) x |
| 25 | # define STATUST(x) x |
| 26 | #else |
| 27 | # define SSIZE(x) (signed size_t)x |
| 28 | # define STATUST(x) (status_t)x |
| 29 | #endif |
| 30 | |
| 31 | // Set to true for noisy debug output. |
| 32 | static const bool kIsDebug = false; |
| 33 | |
| 34 | #if PRINT_STRING_METRICS |
| 35 | static const bool kPrintStringMetrics = true; |
| 36 | #else |
| 37 | static const bool kPrintStringMetrics = false; |
| 38 | #endif |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 39 | |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 40 | static const char* kAttrPrivateType = "^attr-private"; |
| 41 | |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 42 | status_t compileXmlFile(const Bundle* bundle, |
| 43 | const sp<AaptAssets>& assets, |
| 44 | const String16& resourceName, |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 45 | const sp<AaptFile>& target, |
| 46 | ResourceTable* table, |
| 47 | int options) |
| 48 | { |
| 49 | sp<XMLNode> root = XMLNode::parse(target); |
| 50 | if (root == NULL) { |
| 51 | return UNKNOWN_ERROR; |
| 52 | } |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 53 | |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 54 | return compileXmlFile(bundle, assets, resourceName, root, target, table, options); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 57 | status_t compileXmlFile(const Bundle* bundle, |
| 58 | const sp<AaptAssets>& assets, |
| 59 | const String16& resourceName, |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 60 | const sp<AaptFile>& target, |
| 61 | const sp<AaptFile>& outTarget, |
| 62 | ResourceTable* table, |
| 63 | int options) |
| 64 | { |
| 65 | sp<XMLNode> root = XMLNode::parse(target); |
| 66 | if (root == NULL) { |
| 67 | return UNKNOWN_ERROR; |
| 68 | } |
| 69 | |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 70 | return compileXmlFile(bundle, assets, resourceName, root, outTarget, table, options); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 73 | status_t compileXmlFile(const Bundle* bundle, |
| 74 | const sp<AaptAssets>& assets, |
| 75 | const String16& resourceName, |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 76 | const sp<XMLNode>& root, |
| 77 | const sp<AaptFile>& target, |
| 78 | ResourceTable* table, |
| 79 | int options) |
| 80 | { |
| 81 | if ((options&XML_COMPILE_STRIP_WHITESPACE) != 0) { |
| 82 | root->removeWhitespace(true, NULL); |
| 83 | } else if ((options&XML_COMPILE_COMPACT_WHITESPACE) != 0) { |
| 84 | root->removeWhitespace(false, NULL); |
| 85 | } |
| 86 | |
| 87 | if ((options&XML_COMPILE_UTF8) != 0) { |
| 88 | root->setUTF8(true); |
| 89 | } |
| 90 | |
Adam Lesinski | 07dfd2d | 2015-10-28 15:44:27 -0700 | [diff] [blame] | 91 | if (table->processBundleFormat(bundle, resourceName, target, root) != NO_ERROR) { |
| 92 | return UNKNOWN_ERROR; |
| 93 | } |
Adam Lesinski | 5b9847c | 2015-11-30 21:07:44 +0000 | [diff] [blame] | 94 | |
Adam Lesinski | 07dfd2d | 2015-10-28 15:44:27 -0700 | [diff] [blame] | 95 | bool hasErrors = false; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 96 | if ((options&XML_COMPILE_ASSIGN_ATTRIBUTE_IDS) != 0) { |
| 97 | status_t err = root->assignResourceIds(assets, table); |
| 98 | if (err != NO_ERROR) { |
| 99 | hasErrors = true; |
| 100 | } |
| 101 | } |
| 102 | |
Adam Lesinski | 07dfd2d | 2015-10-28 15:44:27 -0700 | [diff] [blame] | 103 | if ((options&XML_COMPILE_PARSE_VALUES) != 0) { |
| 104 | status_t err = root->parseValues(assets, table); |
| 105 | if (err != NO_ERROR) { |
| 106 | hasErrors = true; |
| 107 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (hasErrors) { |
| 111 | return UNKNOWN_ERROR; |
| 112 | } |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 113 | |
| 114 | if (table->modifyForCompat(bundle, resourceName, target, root) != NO_ERROR) { |
| 115 | return UNKNOWN_ERROR; |
| 116 | } |
Andreas Gampe | 87332a7 | 2014-10-01 22:03:58 -0700 | [diff] [blame] | 117 | |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 118 | if (kIsDebug) { |
| 119 | printf("Input XML Resource:\n"); |
| 120 | root->print(); |
| 121 | } |
Adam Lesinski | 07dfd2d | 2015-10-28 15:44:27 -0700 | [diff] [blame] | 122 | status_t err = root->flatten(target, |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 123 | (options&XML_COMPILE_STRIP_COMMENTS) != 0, |
| 124 | (options&XML_COMPILE_STRIP_RAW_VALUES) != 0); |
| 125 | if (err != NO_ERROR) { |
| 126 | return err; |
| 127 | } |
| 128 | |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 129 | if (kIsDebug) { |
| 130 | printf("Output XML Resource:\n"); |
| 131 | ResXMLTree tree; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 132 | tree.setTo(target->getData(), target->getSize()); |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 133 | printXMLBlock(&tree); |
| 134 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 135 | |
| 136 | target->setCompressionMethod(ZipEntry::kCompressDeflated); |
| 137 | |
| 138 | return err; |
| 139 | } |
| 140 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 141 | struct flag_entry |
| 142 | { |
| 143 | const char16_t* name; |
| 144 | size_t nameLen; |
| 145 | uint32_t value; |
| 146 | const char* description; |
| 147 | }; |
| 148 | |
| 149 | static const char16_t referenceArray[] = |
| 150 | { 'r', 'e', 'f', 'e', 'r', 'e', 'n', 'c', 'e' }; |
| 151 | static const char16_t stringArray[] = |
| 152 | { 's', 't', 'r', 'i', 'n', 'g' }; |
| 153 | static const char16_t integerArray[] = |
| 154 | { 'i', 'n', 't', 'e', 'g', 'e', 'r' }; |
| 155 | static const char16_t booleanArray[] = |
| 156 | { 'b', 'o', 'o', 'l', 'e', 'a', 'n' }; |
| 157 | static const char16_t colorArray[] = |
| 158 | { 'c', 'o', 'l', 'o', 'r' }; |
| 159 | static const char16_t floatArray[] = |
| 160 | { 'f', 'l', 'o', 'a', 't' }; |
| 161 | static const char16_t dimensionArray[] = |
| 162 | { 'd', 'i', 'm', 'e', 'n', 's', 'i', 'o', 'n' }; |
| 163 | static const char16_t fractionArray[] = |
| 164 | { 'f', 'r', 'a', 'c', 't', 'i', 'o', 'n' }; |
| 165 | static const char16_t enumArray[] = |
| 166 | { 'e', 'n', 'u', 'm' }; |
| 167 | static const char16_t flagsArray[] = |
| 168 | { 'f', 'l', 'a', 'g', 's' }; |
| 169 | |
| 170 | static const flag_entry gFormatFlags[] = { |
| 171 | { referenceArray, sizeof(referenceArray)/2, ResTable_map::TYPE_REFERENCE, |
| 172 | "a reference to another resource, in the form \"<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>\"\n" |
| 173 | "or to a theme attribute in the form \"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\"."}, |
| 174 | { stringArray, sizeof(stringArray)/2, ResTable_map::TYPE_STRING, |
| 175 | "a string value, using '\\\\;' to escape characters such as '\\\\n' or '\\\\uxxxx' for a unicode character." }, |
| 176 | { integerArray, sizeof(integerArray)/2, ResTable_map::TYPE_INTEGER, |
| 177 | "an integer value, such as \"<code>100</code>\"." }, |
| 178 | { booleanArray, sizeof(booleanArray)/2, ResTable_map::TYPE_BOOLEAN, |
| 179 | "a boolean value, either \"<code>true</code>\" or \"<code>false</code>\"." }, |
| 180 | { colorArray, sizeof(colorArray)/2, ResTable_map::TYPE_COLOR, |
| 181 | "a color value, in the form of \"<code>#<i>rgb</i></code>\", \"<code>#<i>argb</i></code>\",\n" |
| 182 | "\"<code>#<i>rrggbb</i></code>\", or \"<code>#<i>aarrggbb</i></code>\"." }, |
| 183 | { floatArray, sizeof(floatArray)/2, ResTable_map::TYPE_FLOAT, |
| 184 | "a floating point value, such as \"<code>1.2</code>\"."}, |
| 185 | { dimensionArray, sizeof(dimensionArray)/2, ResTable_map::TYPE_DIMENSION, |
| 186 | "a dimension value, which is a floating point number appended with a unit such as \"<code>14.5sp</code>\".\n" |
| 187 | "Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size),\n" |
| 188 | "in (inches), mm (millimeters)." }, |
| 189 | { fractionArray, sizeof(fractionArray)/2, ResTable_map::TYPE_FRACTION, |
| 190 | "a fractional value, which is a floating point number appended with either % or %p, such as \"<code>14.5%</code>\".\n" |
| 191 | "The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to\n" |
| 192 | "some parent container." }, |
| 193 | { enumArray, sizeof(enumArray)/2, ResTable_map::TYPE_ENUM, NULL }, |
| 194 | { flagsArray, sizeof(flagsArray)/2, ResTable_map::TYPE_FLAGS, NULL }, |
| 195 | { NULL, 0, 0, NULL } |
| 196 | }; |
| 197 | |
| 198 | static const char16_t suggestedArray[] = { 's', 'u', 'g', 'g', 'e', 's', 't', 'e', 'd' }; |
| 199 | |
| 200 | static const flag_entry l10nRequiredFlags[] = { |
| 201 | { suggestedArray, sizeof(suggestedArray)/2, ResTable_map::L10N_SUGGESTED, NULL }, |
| 202 | { NULL, 0, 0, NULL } |
| 203 | }; |
| 204 | |
| 205 | static const char16_t nulStr[] = { 0 }; |
| 206 | |
| 207 | static uint32_t parse_flags(const char16_t* str, size_t len, |
| 208 | const flag_entry* flags, bool* outError = NULL) |
| 209 | { |
| 210 | while (len > 0 && isspace(*str)) { |
| 211 | str++; |
| 212 | len--; |
| 213 | } |
| 214 | while (len > 0 && isspace(str[len-1])) { |
| 215 | len--; |
| 216 | } |
| 217 | |
| 218 | const char16_t* const end = str + len; |
| 219 | uint32_t value = 0; |
| 220 | |
| 221 | while (str < end) { |
| 222 | const char16_t* div = str; |
| 223 | while (div < end && *div != '|') { |
| 224 | div++; |
| 225 | } |
| 226 | |
| 227 | const flag_entry* cur = flags; |
| 228 | while (cur->name) { |
| 229 | if (strzcmp16(cur->name, cur->nameLen, str, div-str) == 0) { |
| 230 | value |= cur->value; |
| 231 | break; |
| 232 | } |
| 233 | cur++; |
| 234 | } |
| 235 | |
| 236 | if (!cur->name) { |
| 237 | if (outError) *outError = true; |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | str = div < end ? div+1 : div; |
| 242 | } |
| 243 | |
| 244 | if (outError) *outError = false; |
| 245 | return value; |
| 246 | } |
| 247 | |
| 248 | static String16 mayOrMust(int type, int flags) |
| 249 | { |
| 250 | if ((type&(~flags)) == 0) { |
| 251 | return String16("<p>Must"); |
| 252 | } |
| 253 | |
| 254 | return String16("<p>May"); |
| 255 | } |
| 256 | |
| 257 | static void appendTypeInfo(ResourceTable* outTable, const String16& pkg, |
| 258 | const String16& typeName, const String16& ident, int type, |
| 259 | const flag_entry* flags) |
| 260 | { |
| 261 | bool hadType = false; |
| 262 | while (flags->name) { |
| 263 | if ((type&flags->value) != 0 && flags->description != NULL) { |
| 264 | String16 fullMsg(mayOrMust(type, flags->value)); |
| 265 | fullMsg.append(String16(" be ")); |
| 266 | fullMsg.append(String16(flags->description)); |
| 267 | outTable->appendTypeComment(pkg, typeName, ident, fullMsg); |
| 268 | hadType = true; |
| 269 | } |
| 270 | flags++; |
| 271 | } |
| 272 | if (hadType && (type&ResTable_map::TYPE_REFERENCE) == 0) { |
| 273 | outTable->appendTypeComment(pkg, typeName, ident, |
| 274 | String16("<p>This may also be a reference to a resource (in the form\n" |
| 275 | "\"<code>@[<i>package</i>:]<i>type</i>:<i>name</i></code>\") or\n" |
| 276 | "theme attribute (in the form\n" |
| 277 | "\"<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>\")\n" |
| 278 | "containing a value of this type.")); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | struct PendingAttribute |
| 283 | { |
| 284 | const String16 myPackage; |
| 285 | const SourcePos sourcePos; |
| 286 | const bool appendComment; |
| 287 | int32_t type; |
| 288 | String16 ident; |
| 289 | String16 comment; |
| 290 | bool hasErrors; |
| 291 | bool added; |
| 292 | |
| 293 | PendingAttribute(String16 _package, const sp<AaptFile>& in, |
| 294 | ResXMLTree& block, bool _appendComment) |
| 295 | : myPackage(_package) |
| 296 | , sourcePos(in->getPrintableSource(), block.getLineNumber()) |
| 297 | , appendComment(_appendComment) |
| 298 | , type(ResTable_map::TYPE_ANY) |
| 299 | , hasErrors(false) |
| 300 | , added(false) |
| 301 | { |
| 302 | } |
| 303 | |
| 304 | status_t createIfNeeded(ResourceTable* outTable) |
| 305 | { |
| 306 | if (added || hasErrors) { |
| 307 | return NO_ERROR; |
| 308 | } |
| 309 | added = true; |
| 310 | |
| 311 | String16 attr16("attr"); |
| 312 | |
| 313 | if (outTable->hasBagOrEntry(myPackage, attr16, ident)) { |
| 314 | sourcePos.error("Attribute \"%s\" has already been defined\n", |
| 315 | String8(ident).string()); |
| 316 | hasErrors = true; |
| 317 | return UNKNOWN_ERROR; |
| 318 | } |
| 319 | |
| 320 | char numberStr[16]; |
| 321 | sprintf(numberStr, "%d", type); |
| 322 | status_t err = outTable->addBag(sourcePos, myPackage, |
| 323 | attr16, ident, String16(""), |
| 324 | String16("^type"), |
| 325 | String16(numberStr), NULL, NULL); |
| 326 | if (err != NO_ERROR) { |
| 327 | hasErrors = true; |
| 328 | return err; |
| 329 | } |
| 330 | outTable->appendComment(myPackage, attr16, ident, comment, appendComment); |
| 331 | //printf("Attribute %s comment: %s\n", String8(ident).string(), |
| 332 | // String8(comment).string()); |
| 333 | return err; |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | static status_t compileAttribute(const sp<AaptFile>& in, |
| 338 | ResXMLTree& block, |
| 339 | const String16& myPackage, |
| 340 | ResourceTable* outTable, |
| 341 | String16* outIdent = NULL, |
| 342 | bool inStyleable = false) |
| 343 | { |
| 344 | PendingAttribute attr(myPackage, in, block, inStyleable); |
| 345 | |
| 346 | const String16 attr16("attr"); |
| 347 | const String16 id16("id"); |
| 348 | |
| 349 | // Attribute type constants. |
| 350 | const String16 enum16("enum"); |
| 351 | const String16 flag16("flag"); |
| 352 | |
| 353 | ResXMLTree::event_code_t code; |
| 354 | size_t len; |
| 355 | status_t err; |
| 356 | |
| 357 | ssize_t identIdx = block.indexOfAttribute(NULL, "name"); |
| 358 | if (identIdx >= 0) { |
| 359 | attr.ident = String16(block.getAttributeStringValue(identIdx, &len)); |
| 360 | if (outIdent) { |
| 361 | *outIdent = attr.ident; |
| 362 | } |
| 363 | } else { |
| 364 | attr.sourcePos.error("A 'name' attribute is required for <attr>\n"); |
| 365 | attr.hasErrors = true; |
| 366 | } |
| 367 | |
| 368 | attr.comment = String16( |
| 369 | block.getComment(&len) ? block.getComment(&len) : nulStr); |
| 370 | |
| 371 | ssize_t typeIdx = block.indexOfAttribute(NULL, "format"); |
| 372 | if (typeIdx >= 0) { |
| 373 | String16 typeStr = String16(block.getAttributeStringValue(typeIdx, &len)); |
| 374 | attr.type = parse_flags(typeStr.string(), typeStr.size(), gFormatFlags); |
| 375 | if (attr.type == 0) { |
| 376 | attr.sourcePos.error("Tag <attr> 'format' attribute value \"%s\" not valid\n", |
| 377 | String8(typeStr).string()); |
| 378 | attr.hasErrors = true; |
| 379 | } |
| 380 | attr.createIfNeeded(outTable); |
| 381 | } else if (!inStyleable) { |
| 382 | // Attribute definitions outside of styleables always define the |
| 383 | // attribute as a generic value. |
| 384 | attr.createIfNeeded(outTable); |
| 385 | } |
| 386 | |
| 387 | //printf("Attribute %s: type=0x%08x\n", String8(attr.ident).string(), attr.type); |
| 388 | |
| 389 | ssize_t minIdx = block.indexOfAttribute(NULL, "min"); |
| 390 | if (minIdx >= 0) { |
| 391 | String16 val = String16(block.getAttributeStringValue(minIdx, &len)); |
| 392 | if (!ResTable::stringToInt(val.string(), val.size(), NULL)) { |
| 393 | attr.sourcePos.error("Tag <attr> 'min' attribute must be a number, not \"%s\"\n", |
| 394 | String8(val).string()); |
| 395 | attr.hasErrors = true; |
| 396 | } |
| 397 | attr.createIfNeeded(outTable); |
| 398 | if (!attr.hasErrors) { |
| 399 | err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, |
| 400 | String16(""), String16("^min"), String16(val), NULL, NULL); |
| 401 | if (err != NO_ERROR) { |
| 402 | attr.hasErrors = true; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | ssize_t maxIdx = block.indexOfAttribute(NULL, "max"); |
| 408 | if (maxIdx >= 0) { |
| 409 | String16 val = String16(block.getAttributeStringValue(maxIdx, &len)); |
| 410 | if (!ResTable::stringToInt(val.string(), val.size(), NULL)) { |
| 411 | attr.sourcePos.error("Tag <attr> 'max' attribute must be a number, not \"%s\"\n", |
| 412 | String8(val).string()); |
| 413 | attr.hasErrors = true; |
| 414 | } |
| 415 | attr.createIfNeeded(outTable); |
| 416 | if (!attr.hasErrors) { |
| 417 | err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, |
| 418 | String16(""), String16("^max"), String16(val), NULL, NULL); |
| 419 | attr.hasErrors = true; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if ((minIdx >= 0 || maxIdx >= 0) && (attr.type&ResTable_map::TYPE_INTEGER) == 0) { |
| 424 | attr.sourcePos.error("Tag <attr> must have format=integer attribute if using max or min\n"); |
| 425 | attr.hasErrors = true; |
| 426 | } |
| 427 | |
| 428 | ssize_t l10nIdx = block.indexOfAttribute(NULL, "localization"); |
| 429 | if (l10nIdx >= 0) { |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 430 | const char16_t* str = block.getAttributeStringValue(l10nIdx, &len); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 431 | bool error; |
| 432 | uint32_t l10n_required = parse_flags(str, len, l10nRequiredFlags, &error); |
| 433 | if (error) { |
| 434 | attr.sourcePos.error("Tag <attr> 'localization' attribute value \"%s\" not valid\n", |
| 435 | String8(str).string()); |
| 436 | attr.hasErrors = true; |
| 437 | } |
| 438 | attr.createIfNeeded(outTable); |
| 439 | if (!attr.hasErrors) { |
| 440 | char buf[11]; |
| 441 | sprintf(buf, "%d", l10n_required); |
| 442 | err = outTable->addBag(attr.sourcePos, myPackage, attr16, attr.ident, |
| 443 | String16(""), String16("^l10n"), String16(buf), NULL, NULL); |
| 444 | if (err != NO_ERROR) { |
| 445 | attr.hasErrors = true; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | String16 enumOrFlagsComment; |
| 451 | |
| 452 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 453 | if (code == ResXMLTree::START_TAG) { |
| 454 | uint32_t localType = 0; |
| 455 | if (strcmp16(block.getElementName(&len), enum16.string()) == 0) { |
| 456 | localType = ResTable_map::TYPE_ENUM; |
| 457 | } else if (strcmp16(block.getElementName(&len), flag16.string()) == 0) { |
| 458 | localType = ResTable_map::TYPE_FLAGS; |
| 459 | } else { |
| 460 | SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| 461 | .error("Tag <%s> can not appear inside <attr>, only <enum> or <flag>\n", |
| 462 | String8(block.getElementName(&len)).string()); |
| 463 | return UNKNOWN_ERROR; |
| 464 | } |
| 465 | |
| 466 | attr.createIfNeeded(outTable); |
| 467 | |
| 468 | if (attr.type == ResTable_map::TYPE_ANY) { |
| 469 | // No type was explicitly stated, so supplying enum tags |
| 470 | // implicitly creates an enum or flag. |
| 471 | attr.type = 0; |
| 472 | } |
| 473 | |
| 474 | if ((attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) == 0) { |
| 475 | // Wasn't originally specified as an enum, so update its type. |
| 476 | attr.type |= localType; |
| 477 | if (!attr.hasErrors) { |
| 478 | char numberStr[16]; |
| 479 | sprintf(numberStr, "%d", attr.type); |
| 480 | err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), |
| 481 | myPackage, attr16, attr.ident, String16(""), |
| 482 | String16("^type"), String16(numberStr), NULL, NULL, true); |
| 483 | if (err != NO_ERROR) { |
| 484 | attr.hasErrors = true; |
| 485 | } |
| 486 | } |
| 487 | } else if ((uint32_t)(attr.type&(ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)) != localType) { |
| 488 | if (localType == ResTable_map::TYPE_ENUM) { |
| 489 | SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| 490 | .error("<enum> attribute can not be used inside a flags format\n"); |
| 491 | attr.hasErrors = true; |
| 492 | } else { |
| 493 | SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| 494 | .error("<flag> attribute can not be used inside a enum format\n"); |
| 495 | attr.hasErrors = true; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | String16 itemIdent; |
| 500 | ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name"); |
| 501 | if (itemIdentIdx >= 0) { |
| 502 | itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len)); |
| 503 | } else { |
| 504 | SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| 505 | .error("A 'name' attribute is required for <enum> or <flag>\n"); |
| 506 | attr.hasErrors = true; |
| 507 | } |
| 508 | |
| 509 | String16 value; |
| 510 | ssize_t valueIdx = block.indexOfAttribute(NULL, "value"); |
| 511 | if (valueIdx >= 0) { |
| 512 | value = String16(block.getAttributeStringValue(valueIdx, &len)); |
| 513 | } else { |
| 514 | SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| 515 | .error("A 'value' attribute is required for <enum> or <flag>\n"); |
| 516 | attr.hasErrors = true; |
| 517 | } |
| 518 | if (!attr.hasErrors && !ResTable::stringToInt(value.string(), value.size(), NULL)) { |
| 519 | SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| 520 | .error("Tag <enum> or <flag> 'value' attribute must be a number," |
| 521 | " not \"%s\"\n", |
| 522 | String8(value).string()); |
| 523 | attr.hasErrors = true; |
| 524 | } |
| 525 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 526 | if (!attr.hasErrors) { |
| 527 | if (enumOrFlagsComment.size() == 0) { |
| 528 | enumOrFlagsComment.append(mayOrMust(attr.type, |
| 529 | ResTable_map::TYPE_ENUM|ResTable_map::TYPE_FLAGS)); |
| 530 | enumOrFlagsComment.append((attr.type&ResTable_map::TYPE_ENUM) |
| 531 | ? String16(" be one of the following constant values.") |
| 532 | : String16(" be one or more (separated by '|') of the following constant values.")); |
| 533 | enumOrFlagsComment.append(String16("</p>\n<table>\n" |
| 534 | "<colgroup align=\"left\" />\n" |
| 535 | "<colgroup align=\"left\" />\n" |
| 536 | "<colgroup align=\"left\" />\n" |
| 537 | "<tr><th>Constant</th><th>Value</th><th>Description</th></tr>")); |
| 538 | } |
| 539 | |
| 540 | enumOrFlagsComment.append(String16("\n<tr><td><code>")); |
| 541 | enumOrFlagsComment.append(itemIdent); |
| 542 | enumOrFlagsComment.append(String16("</code></td><td>")); |
| 543 | enumOrFlagsComment.append(value); |
| 544 | enumOrFlagsComment.append(String16("</td><td>")); |
| 545 | if (block.getComment(&len)) { |
| 546 | enumOrFlagsComment.append(String16(block.getComment(&len))); |
| 547 | } |
| 548 | enumOrFlagsComment.append(String16("</td></tr>")); |
| 549 | |
| 550 | err = outTable->addBag(SourcePos(in->getPrintableSource(), block.getLineNumber()), |
| 551 | myPackage, |
| 552 | attr16, attr.ident, String16(""), |
| 553 | itemIdent, value, NULL, NULL, false, true); |
| 554 | if (err != NO_ERROR) { |
| 555 | attr.hasErrors = true; |
| 556 | } |
| 557 | } |
| 558 | } else if (code == ResXMLTree::END_TAG) { |
| 559 | if (strcmp16(block.getElementName(&len), attr16.string()) == 0) { |
| 560 | break; |
| 561 | } |
| 562 | if ((attr.type&ResTable_map::TYPE_ENUM) != 0) { |
| 563 | if (strcmp16(block.getElementName(&len), enum16.string()) != 0) { |
| 564 | SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| 565 | .error("Found tag </%s> where </enum> is expected\n", |
| 566 | String8(block.getElementName(&len)).string()); |
| 567 | return UNKNOWN_ERROR; |
| 568 | } |
| 569 | } else { |
| 570 | if (strcmp16(block.getElementName(&len), flag16.string()) != 0) { |
| 571 | SourcePos(in->getPrintableSource(), block.getLineNumber()) |
| 572 | .error("Found tag </%s> where </flag> is expected\n", |
| 573 | String8(block.getElementName(&len)).string()); |
| 574 | return UNKNOWN_ERROR; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if (!attr.hasErrors && attr.added) { |
| 581 | appendTypeInfo(outTable, myPackage, attr16, attr.ident, attr.type, gFormatFlags); |
| 582 | } |
| 583 | |
| 584 | if (!attr.hasErrors && enumOrFlagsComment.size() > 0) { |
| 585 | enumOrFlagsComment.append(String16("\n</table>")); |
| 586 | outTable->appendTypeComment(myPackage, attr16, attr.ident, enumOrFlagsComment); |
| 587 | } |
| 588 | |
| 589 | |
| 590 | return NO_ERROR; |
| 591 | } |
| 592 | |
| 593 | bool localeIsDefined(const ResTable_config& config) |
| 594 | { |
| 595 | return config.locale == 0; |
| 596 | } |
| 597 | |
| 598 | status_t parseAndAddBag(Bundle* bundle, |
| 599 | const sp<AaptFile>& in, |
| 600 | ResXMLTree* block, |
| 601 | const ResTable_config& config, |
| 602 | const String16& myPackage, |
| 603 | const String16& curType, |
| 604 | const String16& ident, |
| 605 | const String16& parentIdent, |
| 606 | const String16& itemIdent, |
| 607 | int32_t curFormat, |
| 608 | bool isFormatted, |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 609 | const String16& /* product */, |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 610 | PseudolocalizationMethod pseudolocalize, |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 611 | const bool overwrite, |
| 612 | ResourceTable* outTable) |
| 613 | { |
| 614 | status_t err; |
| 615 | const String16 item16("item"); |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 616 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 617 | String16 str; |
| 618 | Vector<StringPool::entry_style_span> spans; |
| 619 | err = parseStyledString(bundle, in->getPrintableSource().string(), |
| 620 | block, item16, &str, &spans, isFormatted, |
| 621 | pseudolocalize); |
| 622 | if (err != NO_ERROR) { |
| 623 | return err; |
| 624 | } |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 625 | |
| 626 | if (kIsDebug) { |
| 627 | printf("Adding resource bag entry l=%c%c c=%c%c orien=%d d=%d " |
| 628 | " pid=%s, bag=%s, id=%s: %s\n", |
| 629 | config.language[0], config.language[1], |
| 630 | config.country[0], config.country[1], |
| 631 | config.orientation, config.density, |
| 632 | String8(parentIdent).string(), |
| 633 | String8(ident).string(), |
| 634 | String8(itemIdent).string(), |
| 635 | String8(str).string()); |
| 636 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 637 | |
| 638 | err = outTable->addBag(SourcePos(in->getPrintableSource(), block->getLineNumber()), |
| 639 | myPackage, curType, ident, parentIdent, itemIdent, str, |
| 640 | &spans, &config, overwrite, false, curFormat); |
| 641 | return err; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Returns true if needle is one of the elements in the comma-separated list |
| 646 | * haystack, false otherwise. |
| 647 | */ |
| 648 | bool isInProductList(const String16& needle, const String16& haystack) { |
| 649 | const char16_t *needle2 = needle.string(); |
| 650 | const char16_t *haystack2 = haystack.string(); |
| 651 | size_t needlesize = needle.size(); |
| 652 | |
| 653 | while (*haystack2 != '\0') { |
| 654 | if (strncmp16(haystack2, needle2, needlesize) == 0) { |
| 655 | if (haystack2[needlesize] == '\0' || haystack2[needlesize] == ',') { |
| 656 | return true; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | while (*haystack2 != '\0' && *haystack2 != ',') { |
| 661 | haystack2++; |
| 662 | } |
| 663 | if (*haystack2 == ',') { |
| 664 | haystack2++; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | return false; |
| 669 | } |
| 670 | |
Adam Lesinski | 8ff15b4 | 2013-10-07 16:54:01 -0700 | [diff] [blame] | 671 | /* |
| 672 | * A simple container that holds a resource type and name. It is ordered first by type then |
| 673 | * by name. |
| 674 | */ |
| 675 | struct type_ident_pair_t { |
| 676 | String16 type; |
| 677 | String16 ident; |
| 678 | |
| 679 | type_ident_pair_t() { }; |
| 680 | type_ident_pair_t(const String16& t, const String16& i) : type(t), ident(i) { } |
| 681 | type_ident_pair_t(const type_ident_pair_t& o) : type(o.type), ident(o.ident) { } |
| 682 | inline bool operator < (const type_ident_pair_t& o) const { |
| 683 | int cmp = compare_type(type, o.type); |
| 684 | if (cmp < 0) { |
| 685 | return true; |
| 686 | } else if (cmp > 0) { |
| 687 | return false; |
| 688 | } else { |
| 689 | return strictly_order_type(ident, o.ident); |
| 690 | } |
| 691 | } |
| 692 | }; |
| 693 | |
| 694 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 695 | status_t parseAndAddEntry(Bundle* bundle, |
| 696 | const sp<AaptFile>& in, |
| 697 | ResXMLTree* block, |
| 698 | const ResTable_config& config, |
| 699 | const String16& myPackage, |
| 700 | const String16& curType, |
| 701 | const String16& ident, |
| 702 | const String16& curTag, |
| 703 | bool curIsStyled, |
| 704 | int32_t curFormat, |
| 705 | bool isFormatted, |
| 706 | const String16& product, |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 707 | PseudolocalizationMethod pseudolocalize, |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 708 | const bool overwrite, |
Adam Lesinski | 8ff15b4 | 2013-10-07 16:54:01 -0700 | [diff] [blame] | 709 | KeyedVector<type_ident_pair_t, bool>* skippedResourceNames, |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 710 | ResourceTable* outTable) |
| 711 | { |
| 712 | status_t err; |
| 713 | |
| 714 | String16 str; |
| 715 | Vector<StringPool::entry_style_span> spans; |
| 716 | err = parseStyledString(bundle, in->getPrintableSource().string(), block, |
| 717 | curTag, &str, curIsStyled ? &spans : NULL, |
| 718 | isFormatted, pseudolocalize); |
| 719 | |
| 720 | if (err < NO_ERROR) { |
| 721 | return err; |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * If a product type was specified on the command line |
| 726 | * and also in the string, and the two are not the same, |
| 727 | * return without adding the string. |
| 728 | */ |
| 729 | |
| 730 | const char *bundleProduct = bundle->getProduct(); |
| 731 | if (bundleProduct == NULL) { |
| 732 | bundleProduct = ""; |
| 733 | } |
| 734 | |
| 735 | if (product.size() != 0) { |
| 736 | /* |
| 737 | * If the command-line-specified product is empty, only "default" |
| 738 | * matches. Other variants are skipped. This is so generation |
| 739 | * of the R.java file when the product is not known is predictable. |
| 740 | */ |
| 741 | |
| 742 | if (bundleProduct[0] == '\0') { |
| 743 | if (strcmp16(String16("default").string(), product.string()) != 0) { |
Adam Lesinski | 8ff15b4 | 2013-10-07 16:54:01 -0700 | [diff] [blame] | 744 | /* |
| 745 | * This string has a product other than 'default'. Do not add it, |
| 746 | * but record it so that if we do not see the same string with |
| 747 | * product 'default' or no product, then report an error. |
| 748 | */ |
| 749 | skippedResourceNames->replaceValueFor( |
| 750 | type_ident_pair_t(curType, ident), true); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 751 | return NO_ERROR; |
| 752 | } |
| 753 | } else { |
| 754 | /* |
| 755 | * The command-line product is not empty. |
| 756 | * If the product for this string is on the command-line list, |
| 757 | * it matches. "default" also matches, but only if nothing |
| 758 | * else has matched already. |
| 759 | */ |
| 760 | |
| 761 | if (isInProductList(product, String16(bundleProduct))) { |
| 762 | ; |
| 763 | } else if (strcmp16(String16("default").string(), product.string()) == 0 && |
| 764 | !outTable->hasBagOrEntry(myPackage, curType, ident, config)) { |
| 765 | ; |
| 766 | } else { |
| 767 | return NO_ERROR; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 772 | if (kIsDebug) { |
| 773 | printf("Adding resource entry l=%c%c c=%c%c orien=%d d=%d id=%s: %s\n", |
| 774 | config.language[0], config.language[1], |
| 775 | config.country[0], config.country[1], |
| 776 | config.orientation, config.density, |
| 777 | String8(ident).string(), String8(str).string()); |
| 778 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 779 | |
| 780 | err = outTable->addEntry(SourcePos(in->getPrintableSource(), block->getLineNumber()), |
| 781 | myPackage, curType, ident, str, &spans, &config, |
| 782 | false, curFormat, overwrite); |
| 783 | |
| 784 | return err; |
| 785 | } |
| 786 | |
| 787 | status_t compileResourceFile(Bundle* bundle, |
| 788 | const sp<AaptAssets>& assets, |
| 789 | const sp<AaptFile>& in, |
| 790 | const ResTable_config& defParams, |
| 791 | const bool overwrite, |
| 792 | ResourceTable* outTable) |
| 793 | { |
| 794 | ResXMLTree block; |
| 795 | status_t err = parseXMLResource(in, &block, false, true); |
| 796 | if (err != NO_ERROR) { |
| 797 | return err; |
| 798 | } |
| 799 | |
| 800 | // Top-level tag. |
| 801 | const String16 resources16("resources"); |
| 802 | |
| 803 | // Identifier declaration tags. |
| 804 | const String16 declare_styleable16("declare-styleable"); |
| 805 | const String16 attr16("attr"); |
| 806 | |
| 807 | // Data creation organizational tags. |
| 808 | const String16 string16("string"); |
| 809 | const String16 drawable16("drawable"); |
| 810 | const String16 color16("color"); |
| 811 | const String16 bool16("bool"); |
| 812 | const String16 integer16("integer"); |
| 813 | const String16 dimen16("dimen"); |
| 814 | const String16 fraction16("fraction"); |
| 815 | const String16 style16("style"); |
| 816 | const String16 plurals16("plurals"); |
| 817 | const String16 array16("array"); |
| 818 | const String16 string_array16("string-array"); |
| 819 | const String16 integer_array16("integer-array"); |
| 820 | const String16 public16("public"); |
| 821 | const String16 public_padding16("public-padding"); |
| 822 | const String16 private_symbols16("private-symbols"); |
| 823 | const String16 java_symbol16("java-symbol"); |
| 824 | const String16 add_resource16("add-resource"); |
| 825 | const String16 skip16("skip"); |
| 826 | const String16 eat_comment16("eat-comment"); |
| 827 | |
| 828 | // Data creation tags. |
| 829 | const String16 bag16("bag"); |
| 830 | const String16 item16("item"); |
| 831 | |
| 832 | // Attribute type constants. |
| 833 | const String16 enum16("enum"); |
| 834 | |
| 835 | // plural values |
| 836 | const String16 other16("other"); |
| 837 | const String16 quantityOther16("^other"); |
| 838 | const String16 zero16("zero"); |
| 839 | const String16 quantityZero16("^zero"); |
| 840 | const String16 one16("one"); |
| 841 | const String16 quantityOne16("^one"); |
| 842 | const String16 two16("two"); |
| 843 | const String16 quantityTwo16("^two"); |
| 844 | const String16 few16("few"); |
| 845 | const String16 quantityFew16("^few"); |
| 846 | const String16 many16("many"); |
| 847 | const String16 quantityMany16("^many"); |
| 848 | |
| 849 | // useful attribute names and special values |
| 850 | const String16 name16("name"); |
| 851 | const String16 translatable16("translatable"); |
| 852 | const String16 formatted16("formatted"); |
| 853 | const String16 false16("false"); |
| 854 | |
| 855 | const String16 myPackage(assets->getPackage()); |
| 856 | |
| 857 | bool hasErrors = false; |
| 858 | |
| 859 | bool fileIsTranslatable = true; |
| 860 | if (strstr(in->getPrintableSource().string(), "donottranslate") != NULL) { |
| 861 | fileIsTranslatable = false; |
| 862 | } |
| 863 | |
| 864 | DefaultKeyedVector<String16, uint32_t> nextPublicId(0); |
| 865 | |
Adam Lesinski | 8ff15b4 | 2013-10-07 16:54:01 -0700 | [diff] [blame] | 866 | // Stores the resource names that were skipped. Typically this happens when |
| 867 | // AAPT is invoked without a product specified and a resource has no |
| 868 | // 'default' product attribute. |
| 869 | KeyedVector<type_ident_pair_t, bool> skippedResourceNames; |
| 870 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 871 | ResXMLTree::event_code_t code; |
| 872 | do { |
| 873 | code = block.next(); |
| 874 | } while (code == ResXMLTree::START_NAMESPACE); |
| 875 | |
| 876 | size_t len; |
| 877 | if (code != ResXMLTree::START_TAG) { |
| 878 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 879 | "No start tag found\n"); |
| 880 | return UNKNOWN_ERROR; |
| 881 | } |
| 882 | if (strcmp16(block.getElementName(&len), resources16.string()) != 0) { |
| 883 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 884 | "Invalid start tag %s\n", String8(block.getElementName(&len)).string()); |
| 885 | return UNKNOWN_ERROR; |
| 886 | } |
| 887 | |
| 888 | ResTable_config curParams(defParams); |
| 889 | |
| 890 | ResTable_config pseudoParams(curParams); |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 891 | pseudoParams.language[0] = 'e'; |
| 892 | pseudoParams.language[1] = 'n'; |
| 893 | pseudoParams.country[0] = 'X'; |
| 894 | pseudoParams.country[1] = 'A'; |
| 895 | |
| 896 | ResTable_config pseudoBidiParams(curParams); |
| 897 | pseudoBidiParams.language[0] = 'a'; |
| 898 | pseudoBidiParams.language[1] = 'r'; |
| 899 | pseudoBidiParams.country[0] = 'X'; |
| 900 | pseudoBidiParams.country[1] = 'B'; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 901 | |
Igor Viarheichyk | 47843df | 2014-05-01 17:04:39 -0700 | [diff] [blame] | 902 | // We should skip resources for pseudolocales if they were |
| 903 | // already added automatically. This is a fix for a transition period when |
| 904 | // manually pseudolocalized resources may be expected. |
| 905 | // TODO: remove this check after next SDK version release. |
| 906 | if ((bundle->getPseudolocalize() & PSEUDO_ACCENTED && |
| 907 | curParams.locale == pseudoParams.locale) || |
| 908 | (bundle->getPseudolocalize() & PSEUDO_BIDI && |
| 909 | curParams.locale == pseudoBidiParams.locale)) { |
| 910 | SourcePos(in->getPrintableSource(), 0).warning( |
| 911 | "Resource file %s is skipped as pseudolocalization" |
| 912 | " was done automatically.", |
| 913 | in->getPrintableSource().string()); |
| 914 | return NO_ERROR; |
| 915 | } |
| 916 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 917 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 918 | if (code == ResXMLTree::START_TAG) { |
| 919 | const String16* curTag = NULL; |
| 920 | String16 curType; |
Adrian Roos | 5892248 | 2015-06-01 17:59:41 -0700 | [diff] [blame] | 921 | String16 curName; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 922 | int32_t curFormat = ResTable_map::TYPE_ANY; |
| 923 | bool curIsBag = false; |
| 924 | bool curIsBagReplaceOnOverwrite = false; |
| 925 | bool curIsStyled = false; |
| 926 | bool curIsPseudolocalizable = false; |
| 927 | bool curIsFormatted = fileIsTranslatable; |
| 928 | bool localHasErrors = false; |
| 929 | |
| 930 | if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { |
| 931 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| 932 | && code != ResXMLTree::BAD_DOCUMENT) { |
| 933 | if (code == ResXMLTree::END_TAG) { |
| 934 | if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { |
| 935 | break; |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | continue; |
| 940 | |
| 941 | } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { |
| 942 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| 943 | && code != ResXMLTree::BAD_DOCUMENT) { |
| 944 | if (code == ResXMLTree::END_TAG) { |
| 945 | if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { |
| 946 | break; |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | continue; |
| 951 | |
| 952 | } else if (strcmp16(block.getElementName(&len), public16.string()) == 0) { |
| 953 | SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); |
| 954 | |
| 955 | String16 type; |
| 956 | ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); |
| 957 | if (typeIdx < 0) { |
| 958 | srcPos.error("A 'type' attribute is required for <public>\n"); |
| 959 | hasErrors = localHasErrors = true; |
| 960 | } |
| 961 | type = String16(block.getAttributeStringValue(typeIdx, &len)); |
| 962 | |
| 963 | String16 name; |
| 964 | ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); |
| 965 | if (nameIdx < 0) { |
| 966 | srcPos.error("A 'name' attribute is required for <public>\n"); |
| 967 | hasErrors = localHasErrors = true; |
| 968 | } |
| 969 | name = String16(block.getAttributeStringValue(nameIdx, &len)); |
| 970 | |
| 971 | uint32_t ident = 0; |
| 972 | ssize_t identIdx = block.indexOfAttribute(NULL, "id"); |
| 973 | if (identIdx >= 0) { |
| 974 | const char16_t* identStr = block.getAttributeStringValue(identIdx, &len); |
| 975 | Res_value identValue; |
| 976 | if (!ResTable::stringToInt(identStr, len, &identValue)) { |
| 977 | srcPos.error("Given 'id' attribute is not an integer: %s\n", |
| 978 | String8(block.getAttributeStringValue(identIdx, &len)).string()); |
| 979 | hasErrors = localHasErrors = true; |
| 980 | } else { |
| 981 | ident = identValue.data; |
| 982 | nextPublicId.replaceValueFor(type, ident+1); |
| 983 | } |
| 984 | } else if (nextPublicId.indexOfKey(type) < 0) { |
| 985 | srcPos.error("No 'id' attribute supplied <public>," |
| 986 | " and no previous id defined in this file.\n"); |
| 987 | hasErrors = localHasErrors = true; |
| 988 | } else if (!localHasErrors) { |
| 989 | ident = nextPublicId.valueFor(type); |
| 990 | nextPublicId.replaceValueFor(type, ident+1); |
| 991 | } |
| 992 | |
| 993 | if (!localHasErrors) { |
| 994 | err = outTable->addPublic(srcPos, myPackage, type, name, ident); |
| 995 | if (err < NO_ERROR) { |
| 996 | hasErrors = localHasErrors = true; |
| 997 | } |
| 998 | } |
| 999 | if (!localHasErrors) { |
| 1000 | sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); |
| 1001 | if (symbols != NULL) { |
| 1002 | symbols = symbols->addNestedSymbol(String8(type), srcPos); |
| 1003 | } |
| 1004 | if (symbols != NULL) { |
| 1005 | symbols->makeSymbolPublic(String8(name), srcPos); |
| 1006 | String16 comment( |
| 1007 | block.getComment(&len) ? block.getComment(&len) : nulStr); |
| 1008 | symbols->appendComment(String8(name), comment, srcPos); |
| 1009 | } else { |
| 1010 | srcPos.error("Unable to create symbols!\n"); |
| 1011 | hasErrors = localHasErrors = true; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 1016 | if (code == ResXMLTree::END_TAG) { |
| 1017 | if (strcmp16(block.getElementName(&len), public16.string()) == 0) { |
| 1018 | break; |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | continue; |
| 1023 | |
| 1024 | } else if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) { |
| 1025 | SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); |
| 1026 | |
| 1027 | String16 type; |
| 1028 | ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); |
| 1029 | if (typeIdx < 0) { |
| 1030 | srcPos.error("A 'type' attribute is required for <public-padding>\n"); |
| 1031 | hasErrors = localHasErrors = true; |
| 1032 | } |
| 1033 | type = String16(block.getAttributeStringValue(typeIdx, &len)); |
| 1034 | |
| 1035 | String16 name; |
| 1036 | ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); |
| 1037 | if (nameIdx < 0) { |
| 1038 | srcPos.error("A 'name' attribute is required for <public-padding>\n"); |
| 1039 | hasErrors = localHasErrors = true; |
| 1040 | } |
| 1041 | name = String16(block.getAttributeStringValue(nameIdx, &len)); |
| 1042 | |
| 1043 | uint32_t start = 0; |
| 1044 | ssize_t startIdx = block.indexOfAttribute(NULL, "start"); |
| 1045 | if (startIdx >= 0) { |
| 1046 | const char16_t* startStr = block.getAttributeStringValue(startIdx, &len); |
| 1047 | Res_value startValue; |
| 1048 | if (!ResTable::stringToInt(startStr, len, &startValue)) { |
| 1049 | srcPos.error("Given 'start' attribute is not an integer: %s\n", |
| 1050 | String8(block.getAttributeStringValue(startIdx, &len)).string()); |
| 1051 | hasErrors = localHasErrors = true; |
| 1052 | } else { |
| 1053 | start = startValue.data; |
| 1054 | } |
| 1055 | } else if (nextPublicId.indexOfKey(type) < 0) { |
| 1056 | srcPos.error("No 'start' attribute supplied <public-padding>," |
| 1057 | " and no previous id defined in this file.\n"); |
| 1058 | hasErrors = localHasErrors = true; |
| 1059 | } else if (!localHasErrors) { |
| 1060 | start = nextPublicId.valueFor(type); |
| 1061 | } |
| 1062 | |
| 1063 | uint32_t end = 0; |
| 1064 | ssize_t endIdx = block.indexOfAttribute(NULL, "end"); |
| 1065 | if (endIdx >= 0) { |
| 1066 | const char16_t* endStr = block.getAttributeStringValue(endIdx, &len); |
| 1067 | Res_value endValue; |
| 1068 | if (!ResTable::stringToInt(endStr, len, &endValue)) { |
| 1069 | srcPos.error("Given 'end' attribute is not an integer: %s\n", |
| 1070 | String8(block.getAttributeStringValue(endIdx, &len)).string()); |
| 1071 | hasErrors = localHasErrors = true; |
| 1072 | } else { |
| 1073 | end = endValue.data; |
| 1074 | } |
| 1075 | } else { |
| 1076 | srcPos.error("No 'end' attribute supplied <public-padding>\n"); |
| 1077 | hasErrors = localHasErrors = true; |
| 1078 | } |
| 1079 | |
| 1080 | if (end >= start) { |
| 1081 | nextPublicId.replaceValueFor(type, end+1); |
| 1082 | } else { |
| 1083 | srcPos.error("Padding start '%ul' is after end '%ul'\n", |
| 1084 | start, end); |
| 1085 | hasErrors = localHasErrors = true; |
| 1086 | } |
| 1087 | |
| 1088 | String16 comment( |
| 1089 | block.getComment(&len) ? block.getComment(&len) : nulStr); |
| 1090 | for (uint32_t curIdent=start; curIdent<=end; curIdent++) { |
| 1091 | if (localHasErrors) { |
| 1092 | break; |
| 1093 | } |
| 1094 | String16 curName(name); |
| 1095 | char buf[64]; |
| 1096 | sprintf(buf, "%d", (int)(end-curIdent+1)); |
| 1097 | curName.append(String16(buf)); |
| 1098 | |
| 1099 | err = outTable->addEntry(srcPos, myPackage, type, curName, |
| 1100 | String16("padding"), NULL, &curParams, false, |
| 1101 | ResTable_map::TYPE_STRING, overwrite); |
| 1102 | if (err < NO_ERROR) { |
| 1103 | hasErrors = localHasErrors = true; |
| 1104 | break; |
| 1105 | } |
| 1106 | err = outTable->addPublic(srcPos, myPackage, type, |
| 1107 | curName, curIdent); |
| 1108 | if (err < NO_ERROR) { |
| 1109 | hasErrors = localHasErrors = true; |
| 1110 | break; |
| 1111 | } |
| 1112 | sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); |
| 1113 | if (symbols != NULL) { |
| 1114 | symbols = symbols->addNestedSymbol(String8(type), srcPos); |
| 1115 | } |
| 1116 | if (symbols != NULL) { |
| 1117 | symbols->makeSymbolPublic(String8(curName), srcPos); |
| 1118 | symbols->appendComment(String8(curName), comment, srcPos); |
| 1119 | } else { |
| 1120 | srcPos.error("Unable to create symbols!\n"); |
| 1121 | hasErrors = localHasErrors = true; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 1126 | if (code == ResXMLTree::END_TAG) { |
| 1127 | if (strcmp16(block.getElementName(&len), public_padding16.string()) == 0) { |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | continue; |
| 1133 | |
| 1134 | } else if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) { |
| 1135 | String16 pkg; |
| 1136 | ssize_t pkgIdx = block.indexOfAttribute(NULL, "package"); |
| 1137 | if (pkgIdx < 0) { |
| 1138 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1139 | "A 'package' attribute is required for <private-symbols>\n"); |
| 1140 | hasErrors = localHasErrors = true; |
| 1141 | } |
| 1142 | pkg = String16(block.getAttributeStringValue(pkgIdx, &len)); |
| 1143 | if (!localHasErrors) { |
Adam Lesinski | 7871399 | 2015-12-07 14:02:15 -0800 | [diff] [blame] | 1144 | SourcePos(in->getPrintableSource(), block.getLineNumber()).warning( |
| 1145 | "<private-symbols> is deprecated. Use the command line flag " |
| 1146 | "--private-symbols instead.\n"); |
| 1147 | if (assets->havePrivateSymbols()) { |
| 1148 | SourcePos(in->getPrintableSource(), block.getLineNumber()).warning( |
| 1149 | "private symbol package already specified. Ignoring...\n"); |
| 1150 | } else { |
| 1151 | assets->setSymbolsPrivatePackage(String8(pkg)); |
| 1152 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 1156 | if (code == ResXMLTree::END_TAG) { |
| 1157 | if (strcmp16(block.getElementName(&len), private_symbols16.string()) == 0) { |
| 1158 | break; |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | continue; |
| 1163 | |
| 1164 | } else if (strcmp16(block.getElementName(&len), java_symbol16.string()) == 0) { |
| 1165 | SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); |
| 1166 | |
| 1167 | String16 type; |
| 1168 | ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); |
| 1169 | if (typeIdx < 0) { |
| 1170 | srcPos.error("A 'type' attribute is required for <public>\n"); |
| 1171 | hasErrors = localHasErrors = true; |
| 1172 | } |
| 1173 | type = String16(block.getAttributeStringValue(typeIdx, &len)); |
| 1174 | |
| 1175 | String16 name; |
| 1176 | ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); |
| 1177 | if (nameIdx < 0) { |
| 1178 | srcPos.error("A 'name' attribute is required for <public>\n"); |
| 1179 | hasErrors = localHasErrors = true; |
| 1180 | } |
| 1181 | name = String16(block.getAttributeStringValue(nameIdx, &len)); |
| 1182 | |
| 1183 | sp<AaptSymbols> symbols = assets->getJavaSymbolsFor(String8("R")); |
| 1184 | if (symbols != NULL) { |
| 1185 | symbols = symbols->addNestedSymbol(String8(type), srcPos); |
| 1186 | } |
| 1187 | if (symbols != NULL) { |
| 1188 | symbols->makeSymbolJavaSymbol(String8(name), srcPos); |
| 1189 | String16 comment( |
| 1190 | block.getComment(&len) ? block.getComment(&len) : nulStr); |
| 1191 | symbols->appendComment(String8(name), comment, srcPos); |
| 1192 | } else { |
| 1193 | srcPos.error("Unable to create symbols!\n"); |
| 1194 | hasErrors = localHasErrors = true; |
| 1195 | } |
| 1196 | |
| 1197 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 1198 | if (code == ResXMLTree::END_TAG) { |
| 1199 | if (strcmp16(block.getElementName(&len), java_symbol16.string()) == 0) { |
| 1200 | break; |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | continue; |
| 1205 | |
| 1206 | |
| 1207 | } else if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) { |
| 1208 | SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); |
| 1209 | |
| 1210 | String16 typeName; |
| 1211 | ssize_t typeIdx = block.indexOfAttribute(NULL, "type"); |
| 1212 | if (typeIdx < 0) { |
| 1213 | srcPos.error("A 'type' attribute is required for <add-resource>\n"); |
| 1214 | hasErrors = localHasErrors = true; |
| 1215 | } |
| 1216 | typeName = String16(block.getAttributeStringValue(typeIdx, &len)); |
| 1217 | |
| 1218 | String16 name; |
| 1219 | ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); |
| 1220 | if (nameIdx < 0) { |
| 1221 | srcPos.error("A 'name' attribute is required for <add-resource>\n"); |
| 1222 | hasErrors = localHasErrors = true; |
| 1223 | } |
| 1224 | name = String16(block.getAttributeStringValue(nameIdx, &len)); |
| 1225 | |
| 1226 | outTable->canAddEntry(srcPos, myPackage, typeName, name); |
| 1227 | |
| 1228 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 1229 | if (code == ResXMLTree::END_TAG) { |
| 1230 | if (strcmp16(block.getElementName(&len), add_resource16.string()) == 0) { |
| 1231 | break; |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | continue; |
| 1236 | |
| 1237 | } else if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) { |
| 1238 | SourcePos srcPos(in->getPrintableSource(), block.getLineNumber()); |
| 1239 | |
| 1240 | String16 ident; |
| 1241 | ssize_t identIdx = block.indexOfAttribute(NULL, "name"); |
| 1242 | if (identIdx < 0) { |
| 1243 | srcPos.error("A 'name' attribute is required for <declare-styleable>\n"); |
| 1244 | hasErrors = localHasErrors = true; |
| 1245 | } |
| 1246 | ident = String16(block.getAttributeStringValue(identIdx, &len)); |
| 1247 | |
| 1248 | sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); |
| 1249 | if (!localHasErrors) { |
| 1250 | if (symbols != NULL) { |
| 1251 | symbols = symbols->addNestedSymbol(String8("styleable"), srcPos); |
| 1252 | } |
| 1253 | sp<AaptSymbols> styleSymbols = symbols; |
| 1254 | if (symbols != NULL) { |
| 1255 | symbols = symbols->addNestedSymbol(String8(ident), srcPos); |
| 1256 | } |
| 1257 | if (symbols == NULL) { |
| 1258 | srcPos.error("Unable to create symbols!\n"); |
| 1259 | return UNKNOWN_ERROR; |
| 1260 | } |
| 1261 | |
| 1262 | String16 comment( |
| 1263 | block.getComment(&len) ? block.getComment(&len) : nulStr); |
| 1264 | styleSymbols->appendComment(String8(ident), comment, srcPos); |
| 1265 | } else { |
| 1266 | symbols = NULL; |
| 1267 | } |
| 1268 | |
| 1269 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 1270 | if (code == ResXMLTree::START_TAG) { |
| 1271 | if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { |
| 1272 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| 1273 | && code != ResXMLTree::BAD_DOCUMENT) { |
| 1274 | if (code == ResXMLTree::END_TAG) { |
| 1275 | if (strcmp16(block.getElementName(&len), skip16.string()) == 0) { |
| 1276 | break; |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | continue; |
| 1281 | } else if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { |
| 1282 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| 1283 | && code != ResXMLTree::BAD_DOCUMENT) { |
| 1284 | if (code == ResXMLTree::END_TAG) { |
| 1285 | if (strcmp16(block.getElementName(&len), eat_comment16.string()) == 0) { |
| 1286 | break; |
| 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | continue; |
| 1291 | } else if (strcmp16(block.getElementName(&len), attr16.string()) != 0) { |
| 1292 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1293 | "Tag <%s> can not appear inside <declare-styleable>, only <attr>\n", |
| 1294 | String8(block.getElementName(&len)).string()); |
| 1295 | return UNKNOWN_ERROR; |
| 1296 | } |
| 1297 | |
| 1298 | String16 comment( |
| 1299 | block.getComment(&len) ? block.getComment(&len) : nulStr); |
| 1300 | String16 itemIdent; |
| 1301 | err = compileAttribute(in, block, myPackage, outTable, &itemIdent, true); |
| 1302 | if (err != NO_ERROR) { |
| 1303 | hasErrors = localHasErrors = true; |
| 1304 | } |
| 1305 | |
| 1306 | if (symbols != NULL) { |
| 1307 | SourcePos srcPos(String8(in->getPrintableSource()), block.getLineNumber()); |
| 1308 | symbols->addSymbol(String8(itemIdent), 0, srcPos); |
| 1309 | symbols->appendComment(String8(itemIdent), comment, srcPos); |
| 1310 | //printf("Attribute %s comment: %s\n", String8(itemIdent).string(), |
| 1311 | // String8(comment).string()); |
| 1312 | } |
| 1313 | } else if (code == ResXMLTree::END_TAG) { |
| 1314 | if (strcmp16(block.getElementName(&len), declare_styleable16.string()) == 0) { |
| 1315 | break; |
| 1316 | } |
| 1317 | |
| 1318 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1319 | "Found tag </%s> where </attr> is expected\n", |
| 1320 | String8(block.getElementName(&len)).string()); |
| 1321 | return UNKNOWN_ERROR; |
| 1322 | } |
| 1323 | } |
| 1324 | continue; |
| 1325 | |
| 1326 | } else if (strcmp16(block.getElementName(&len), attr16.string()) == 0) { |
| 1327 | err = compileAttribute(in, block, myPackage, outTable, NULL); |
| 1328 | if (err != NO_ERROR) { |
| 1329 | hasErrors = true; |
| 1330 | } |
| 1331 | continue; |
| 1332 | |
| 1333 | } else if (strcmp16(block.getElementName(&len), item16.string()) == 0) { |
| 1334 | curTag = &item16; |
| 1335 | ssize_t attri = block.indexOfAttribute(NULL, "type"); |
| 1336 | if (attri >= 0) { |
| 1337 | curType = String16(block.getAttributeStringValue(attri, &len)); |
Adrian Roos | 5892248 | 2015-06-01 17:59:41 -0700 | [diff] [blame] | 1338 | ssize_t nameIdx = block.indexOfAttribute(NULL, "name"); |
| 1339 | if (nameIdx >= 0) { |
| 1340 | curName = String16(block.getAttributeStringValue(nameIdx, &len)); |
| 1341 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1342 | ssize_t formatIdx = block.indexOfAttribute(NULL, "format"); |
| 1343 | if (formatIdx >= 0) { |
| 1344 | String16 formatStr = String16(block.getAttributeStringValue( |
| 1345 | formatIdx, &len)); |
| 1346 | curFormat = parse_flags(formatStr.string(), formatStr.size(), |
| 1347 | gFormatFlags); |
| 1348 | if (curFormat == 0) { |
| 1349 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1350 | "Tag <item> 'format' attribute value \"%s\" not valid\n", |
| 1351 | String8(formatStr).string()); |
| 1352 | hasErrors = localHasErrors = true; |
| 1353 | } |
| 1354 | } |
| 1355 | } else { |
| 1356 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1357 | "A 'type' attribute is required for <item>\n"); |
| 1358 | hasErrors = localHasErrors = true; |
| 1359 | } |
| 1360 | curIsStyled = true; |
| 1361 | } else if (strcmp16(block.getElementName(&len), string16.string()) == 0) { |
| 1362 | // Note the existence and locale of every string we process |
Narayan Kamath | 91447d8 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 1363 | char rawLocale[RESTABLE_MAX_LOCALE_LEN]; |
| 1364 | curParams.getBcp47Locale(rawLocale); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1365 | String8 locale(rawLocale); |
| 1366 | String16 name; |
| 1367 | String16 translatable; |
| 1368 | String16 formatted; |
| 1369 | |
| 1370 | size_t n = block.getAttributeCount(); |
| 1371 | for (size_t i = 0; i < n; i++) { |
| 1372 | size_t length; |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1373 | const char16_t* attr = block.getAttributeName(i, &length); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1374 | if (strcmp16(attr, name16.string()) == 0) { |
| 1375 | name.setTo(block.getAttributeStringValue(i, &length)); |
| 1376 | } else if (strcmp16(attr, translatable16.string()) == 0) { |
| 1377 | translatable.setTo(block.getAttributeStringValue(i, &length)); |
| 1378 | } else if (strcmp16(attr, formatted16.string()) == 0) { |
| 1379 | formatted.setTo(block.getAttributeStringValue(i, &length)); |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | if (name.size() > 0) { |
Adrian Roos | 5892248 | 2015-06-01 17:59:41 -0700 | [diff] [blame] | 1384 | if (locale.size() == 0) { |
| 1385 | outTable->addDefaultLocalization(name); |
| 1386 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1387 | if (translatable == false16) { |
| 1388 | curIsFormatted = false; |
| 1389 | // Untranslatable strings must only exist in the default [empty] locale |
| 1390 | if (locale.size() > 0) { |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 1391 | SourcePos(in->getPrintableSource(), block.getLineNumber()).warning( |
| 1392 | "string '%s' marked untranslatable but exists in locale '%s'\n", |
| 1393 | String8(name).string(), |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1394 | locale.string()); |
| 1395 | // hasErrors = localHasErrors = true; |
| 1396 | } else { |
| 1397 | // Intentionally empty block: |
| 1398 | // |
| 1399 | // Don't add untranslatable strings to the localization table; that |
| 1400 | // way if we later see localizations of them, they'll be flagged as |
| 1401 | // having no default translation. |
| 1402 | } |
| 1403 | } else { |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 1404 | outTable->addLocalization( |
| 1405 | name, |
| 1406 | locale, |
| 1407 | SourcePos(in->getPrintableSource(), block.getLineNumber())); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | if (formatted == false16) { |
| 1411 | curIsFormatted = false; |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | curTag = &string16; |
| 1416 | curType = string16; |
| 1417 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING; |
| 1418 | curIsStyled = true; |
Igor Viarheichyk | 84410b0 | 2014-04-30 11:56:42 -0700 | [diff] [blame] | 1419 | curIsPseudolocalizable = fileIsTranslatable && (translatable != false16); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1420 | } else if (strcmp16(block.getElementName(&len), drawable16.string()) == 0) { |
| 1421 | curTag = &drawable16; |
| 1422 | curType = drawable16; |
| 1423 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR; |
| 1424 | } else if (strcmp16(block.getElementName(&len), color16.string()) == 0) { |
| 1425 | curTag = &color16; |
| 1426 | curType = color16; |
| 1427 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_COLOR; |
| 1428 | } else if (strcmp16(block.getElementName(&len), bool16.string()) == 0) { |
| 1429 | curTag = &bool16; |
| 1430 | curType = bool16; |
| 1431 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_BOOLEAN; |
| 1432 | } else if (strcmp16(block.getElementName(&len), integer16.string()) == 0) { |
| 1433 | curTag = &integer16; |
| 1434 | curType = integer16; |
| 1435 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER; |
| 1436 | } else if (strcmp16(block.getElementName(&len), dimen16.string()) == 0) { |
| 1437 | curTag = &dimen16; |
| 1438 | curType = dimen16; |
| 1439 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_DIMENSION; |
| 1440 | } else if (strcmp16(block.getElementName(&len), fraction16.string()) == 0) { |
| 1441 | curTag = &fraction16; |
| 1442 | curType = fraction16; |
| 1443 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_FRACTION; |
| 1444 | } else if (strcmp16(block.getElementName(&len), bag16.string()) == 0) { |
| 1445 | curTag = &bag16; |
| 1446 | curIsBag = true; |
| 1447 | ssize_t attri = block.indexOfAttribute(NULL, "type"); |
| 1448 | if (attri >= 0) { |
| 1449 | curType = String16(block.getAttributeStringValue(attri, &len)); |
| 1450 | } else { |
| 1451 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1452 | "A 'type' attribute is required for <bag>\n"); |
| 1453 | hasErrors = localHasErrors = true; |
| 1454 | } |
| 1455 | } else if (strcmp16(block.getElementName(&len), style16.string()) == 0) { |
| 1456 | curTag = &style16; |
| 1457 | curType = style16; |
| 1458 | curIsBag = true; |
| 1459 | } else if (strcmp16(block.getElementName(&len), plurals16.string()) == 0) { |
| 1460 | curTag = &plurals16; |
| 1461 | curType = plurals16; |
| 1462 | curIsBag = true; |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1463 | curIsPseudolocalizable = fileIsTranslatable; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1464 | } else if (strcmp16(block.getElementName(&len), array16.string()) == 0) { |
| 1465 | curTag = &array16; |
| 1466 | curType = array16; |
| 1467 | curIsBag = true; |
| 1468 | curIsBagReplaceOnOverwrite = true; |
| 1469 | ssize_t formatIdx = block.indexOfAttribute(NULL, "format"); |
| 1470 | if (formatIdx >= 0) { |
| 1471 | String16 formatStr = String16(block.getAttributeStringValue( |
| 1472 | formatIdx, &len)); |
| 1473 | curFormat = parse_flags(formatStr.string(), formatStr.size(), |
| 1474 | gFormatFlags); |
| 1475 | if (curFormat == 0) { |
| 1476 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1477 | "Tag <array> 'format' attribute value \"%s\" not valid\n", |
| 1478 | String8(formatStr).string()); |
| 1479 | hasErrors = localHasErrors = true; |
| 1480 | } |
| 1481 | } |
| 1482 | } else if (strcmp16(block.getElementName(&len), string_array16.string()) == 0) { |
| 1483 | // Check whether these strings need valid formats. |
| 1484 | // (simplified form of what string16 does above) |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1485 | bool isTranslatable = false; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1486 | size_t n = block.getAttributeCount(); |
Narayan Kamath | 9a9fa16 | 2013-12-18 13:27:30 +0000 | [diff] [blame] | 1487 | |
| 1488 | // Pseudolocalizable by default, unless this string array isn't |
| 1489 | // translatable. |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1490 | for (size_t i = 0; i < n; i++) { |
| 1491 | size_t length; |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1492 | const char16_t* attr = block.getAttributeName(i, &length); |
Narayan Kamath | 9a9fa16 | 2013-12-18 13:27:30 +0000 | [diff] [blame] | 1493 | if (strcmp16(attr, formatted16.string()) == 0) { |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1494 | const char16_t* value = block.getAttributeStringValue(i, &length); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1495 | if (strcmp16(value, false16.string()) == 0) { |
| 1496 | curIsFormatted = false; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1497 | } |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1498 | } else if (strcmp16(attr, translatable16.string()) == 0) { |
Dan Albert | f348c15 | 2014-09-08 18:28:00 -0700 | [diff] [blame] | 1499 | const char16_t* value = block.getAttributeStringValue(i, &length); |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1500 | if (strcmp16(value, false16.string()) == 0) { |
| 1501 | isTranslatable = false; |
| 1502 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | curTag = &string_array16; |
| 1507 | curType = array16; |
| 1508 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_STRING; |
| 1509 | curIsBag = true; |
| 1510 | curIsBagReplaceOnOverwrite = true; |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1511 | curIsPseudolocalizable = isTranslatable && fileIsTranslatable; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1512 | } else if (strcmp16(block.getElementName(&len), integer_array16.string()) == 0) { |
| 1513 | curTag = &integer_array16; |
| 1514 | curType = array16; |
| 1515 | curFormat = ResTable_map::TYPE_REFERENCE|ResTable_map::TYPE_INTEGER; |
| 1516 | curIsBag = true; |
| 1517 | curIsBagReplaceOnOverwrite = true; |
| 1518 | } else { |
| 1519 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1520 | "Found tag %s where item is expected\n", |
| 1521 | String8(block.getElementName(&len)).string()); |
| 1522 | return UNKNOWN_ERROR; |
| 1523 | } |
| 1524 | |
| 1525 | String16 ident; |
| 1526 | ssize_t identIdx = block.indexOfAttribute(NULL, "name"); |
| 1527 | if (identIdx >= 0) { |
| 1528 | ident = String16(block.getAttributeStringValue(identIdx, &len)); |
| 1529 | } else { |
| 1530 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1531 | "A 'name' attribute is required for <%s>\n", |
| 1532 | String8(*curTag).string()); |
| 1533 | hasErrors = localHasErrors = true; |
| 1534 | } |
| 1535 | |
| 1536 | String16 product; |
| 1537 | identIdx = block.indexOfAttribute(NULL, "product"); |
| 1538 | if (identIdx >= 0) { |
| 1539 | product = String16(block.getAttributeStringValue(identIdx, &len)); |
| 1540 | } |
| 1541 | |
| 1542 | String16 comment(block.getComment(&len) ? block.getComment(&len) : nulStr); |
| 1543 | |
| 1544 | if (curIsBag) { |
| 1545 | // Figure out the parent of this bag... |
| 1546 | String16 parentIdent; |
| 1547 | ssize_t parentIdentIdx = block.indexOfAttribute(NULL, "parent"); |
| 1548 | if (parentIdentIdx >= 0) { |
| 1549 | parentIdent = String16(block.getAttributeStringValue(parentIdentIdx, &len)); |
| 1550 | } else { |
| 1551 | ssize_t sep = ident.findLast('.'); |
| 1552 | if (sep >= 0) { |
| 1553 | parentIdent.setTo(ident, sep); |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | if (!localHasErrors) { |
| 1558 | err = outTable->startBag(SourcePos(in->getPrintableSource(), |
| 1559 | block.getLineNumber()), myPackage, curType, ident, |
| 1560 | parentIdent, &curParams, |
| 1561 | overwrite, curIsBagReplaceOnOverwrite); |
| 1562 | if (err != NO_ERROR) { |
| 1563 | hasErrors = localHasErrors = true; |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | ssize_t elmIndex = 0; |
| 1568 | char elmIndexStr[14]; |
| 1569 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| 1570 | && code != ResXMLTree::BAD_DOCUMENT) { |
| 1571 | |
| 1572 | if (code == ResXMLTree::START_TAG) { |
| 1573 | if (strcmp16(block.getElementName(&len), item16.string()) != 0) { |
| 1574 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1575 | "Tag <%s> can not appear inside <%s>, only <item>\n", |
| 1576 | String8(block.getElementName(&len)).string(), |
| 1577 | String8(*curTag).string()); |
| 1578 | return UNKNOWN_ERROR; |
| 1579 | } |
| 1580 | |
| 1581 | String16 itemIdent; |
| 1582 | if (curType == array16) { |
| 1583 | sprintf(elmIndexStr, "^index_%d", (int)elmIndex++); |
| 1584 | itemIdent = String16(elmIndexStr); |
| 1585 | } else if (curType == plurals16) { |
| 1586 | ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "quantity"); |
| 1587 | if (itemIdentIdx >= 0) { |
| 1588 | String16 quantity16(block.getAttributeStringValue(itemIdentIdx, &len)); |
| 1589 | if (quantity16 == other16) { |
| 1590 | itemIdent = quantityOther16; |
| 1591 | } |
| 1592 | else if (quantity16 == zero16) { |
| 1593 | itemIdent = quantityZero16; |
| 1594 | } |
| 1595 | else if (quantity16 == one16) { |
| 1596 | itemIdent = quantityOne16; |
| 1597 | } |
| 1598 | else if (quantity16 == two16) { |
| 1599 | itemIdent = quantityTwo16; |
| 1600 | } |
| 1601 | else if (quantity16 == few16) { |
| 1602 | itemIdent = quantityFew16; |
| 1603 | } |
| 1604 | else if (quantity16 == many16) { |
| 1605 | itemIdent = quantityMany16; |
| 1606 | } |
| 1607 | else { |
| 1608 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1609 | "Illegal 'quantity' attribute is <item> inside <plurals>\n"); |
| 1610 | hasErrors = localHasErrors = true; |
| 1611 | } |
| 1612 | } else { |
| 1613 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1614 | "A 'quantity' attribute is required for <item> inside <plurals>\n"); |
| 1615 | hasErrors = localHasErrors = true; |
| 1616 | } |
| 1617 | } else { |
| 1618 | ssize_t itemIdentIdx = block.indexOfAttribute(NULL, "name"); |
| 1619 | if (itemIdentIdx >= 0) { |
| 1620 | itemIdent = String16(block.getAttributeStringValue(itemIdentIdx, &len)); |
| 1621 | } else { |
| 1622 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1623 | "A 'name' attribute is required for <item>\n"); |
| 1624 | hasErrors = localHasErrors = true; |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | ResXMLParser::ResXMLPosition parserPosition; |
| 1629 | block.getPosition(&parserPosition); |
| 1630 | |
| 1631 | err = parseAndAddBag(bundle, in, &block, curParams, myPackage, curType, |
| 1632 | ident, parentIdent, itemIdent, curFormat, curIsFormatted, |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1633 | product, NO_PSEUDOLOCALIZATION, overwrite, outTable); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1634 | if (err == NO_ERROR) { |
| 1635 | if (curIsPseudolocalizable && localeIsDefined(curParams) |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1636 | && bundle->getPseudolocalize() > 0) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1637 | // pseudolocalize here |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1638 | if ((PSEUDO_ACCENTED & bundle->getPseudolocalize()) == |
| 1639 | PSEUDO_ACCENTED) { |
| 1640 | block.setPosition(parserPosition); |
| 1641 | err = parseAndAddBag(bundle, in, &block, pseudoParams, myPackage, |
| 1642 | curType, ident, parentIdent, itemIdent, curFormat, |
| 1643 | curIsFormatted, product, PSEUDO_ACCENTED, |
| 1644 | overwrite, outTable); |
| 1645 | } |
| 1646 | if ((PSEUDO_BIDI & bundle->getPseudolocalize()) == |
| 1647 | PSEUDO_BIDI) { |
| 1648 | block.setPosition(parserPosition); |
| 1649 | err = parseAndAddBag(bundle, in, &block, pseudoBidiParams, myPackage, |
| 1650 | curType, ident, parentIdent, itemIdent, curFormat, |
| 1651 | curIsFormatted, product, PSEUDO_BIDI, |
| 1652 | overwrite, outTable); |
| 1653 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1654 | } |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1655 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1656 | if (err != NO_ERROR) { |
| 1657 | hasErrors = localHasErrors = true; |
| 1658 | } |
| 1659 | } else if (code == ResXMLTree::END_TAG) { |
| 1660 | if (strcmp16(block.getElementName(&len), curTag->string()) != 0) { |
| 1661 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1662 | "Found tag </%s> where </%s> is expected\n", |
| 1663 | String8(block.getElementName(&len)).string(), |
| 1664 | String8(*curTag).string()); |
| 1665 | return UNKNOWN_ERROR; |
| 1666 | } |
| 1667 | break; |
| 1668 | } |
| 1669 | } |
| 1670 | } else { |
| 1671 | ResXMLParser::ResXMLPosition parserPosition; |
| 1672 | block.getPosition(&parserPosition); |
| 1673 | |
| 1674 | err = parseAndAddEntry(bundle, in, &block, curParams, myPackage, curType, ident, |
| 1675 | *curTag, curIsStyled, curFormat, curIsFormatted, |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1676 | product, NO_PSEUDOLOCALIZATION, overwrite, &skippedResourceNames, outTable); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1677 | |
| 1678 | if (err < NO_ERROR) { // Why err < NO_ERROR instead of err != NO_ERROR? |
| 1679 | hasErrors = localHasErrors = true; |
| 1680 | } |
| 1681 | else if (err == NO_ERROR) { |
Adrian Roos | 5892248 | 2015-06-01 17:59:41 -0700 | [diff] [blame] | 1682 | if (curType == string16 && !curParams.language[0] && !curParams.country[0]) { |
| 1683 | outTable->addDefaultLocalization(curName); |
| 1684 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1685 | if (curIsPseudolocalizable && localeIsDefined(curParams) |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1686 | && bundle->getPseudolocalize() > 0) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1687 | // pseudolocalize here |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 1688 | if ((PSEUDO_ACCENTED & bundle->getPseudolocalize()) == |
| 1689 | PSEUDO_ACCENTED) { |
| 1690 | block.setPosition(parserPosition); |
| 1691 | err = parseAndAddEntry(bundle, in, &block, pseudoParams, myPackage, curType, |
| 1692 | ident, *curTag, curIsStyled, curFormat, |
| 1693 | curIsFormatted, product, |
| 1694 | PSEUDO_ACCENTED, overwrite, &skippedResourceNames, outTable); |
| 1695 | } |
| 1696 | if ((PSEUDO_BIDI & bundle->getPseudolocalize()) == |
| 1697 | PSEUDO_BIDI) { |
| 1698 | block.setPosition(parserPosition); |
| 1699 | err = parseAndAddEntry(bundle, in, &block, pseudoBidiParams, |
| 1700 | myPackage, curType, ident, *curTag, curIsStyled, curFormat, |
| 1701 | curIsFormatted, product, |
| 1702 | PSEUDO_BIDI, overwrite, &skippedResourceNames, outTable); |
| 1703 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1704 | if (err != NO_ERROR) { |
| 1705 | hasErrors = localHasErrors = true; |
| 1706 | } |
| 1707 | } |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | #if 0 |
| 1712 | if (comment.size() > 0) { |
| 1713 | printf("Comment for @%s:%s/%s: %s\n", String8(myPackage).string(), |
| 1714 | String8(curType).string(), String8(ident).string(), |
| 1715 | String8(comment).string()); |
| 1716 | } |
| 1717 | #endif |
| 1718 | if (!localHasErrors) { |
| 1719 | outTable->appendComment(myPackage, curType, ident, comment, false); |
| 1720 | } |
| 1721 | } |
| 1722 | else if (code == ResXMLTree::END_TAG) { |
| 1723 | if (strcmp16(block.getElementName(&len), resources16.string()) != 0) { |
| 1724 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1725 | "Unexpected end tag %s\n", String8(block.getElementName(&len)).string()); |
| 1726 | return UNKNOWN_ERROR; |
| 1727 | } |
| 1728 | } |
| 1729 | else if (code == ResXMLTree::START_NAMESPACE || code == ResXMLTree::END_NAMESPACE) { |
| 1730 | } |
| 1731 | else if (code == ResXMLTree::TEXT) { |
| 1732 | if (isWhitespace(block.getText(&len))) { |
| 1733 | continue; |
| 1734 | } |
| 1735 | SourcePos(in->getPrintableSource(), block.getLineNumber()).error( |
| 1736 | "Found text \"%s\" where item tag is expected\n", |
| 1737 | String8(block.getText(&len)).string()); |
| 1738 | return UNKNOWN_ERROR; |
| 1739 | } |
| 1740 | } |
| 1741 | |
Adam Lesinski | 8ff15b4 | 2013-10-07 16:54:01 -0700 | [diff] [blame] | 1742 | // For every resource defined, there must be exist one variant with a product attribute |
| 1743 | // set to 'default' (or no product attribute at all). |
| 1744 | // We check to see that for every resource that was ignored because of a mismatched |
| 1745 | // product attribute, some product variant of that resource was processed. |
| 1746 | for (size_t i = 0; i < skippedResourceNames.size(); i++) { |
| 1747 | if (skippedResourceNames[i]) { |
| 1748 | const type_ident_pair_t& p = skippedResourceNames.keyAt(i); |
| 1749 | if (!outTable->hasBagOrEntry(myPackage, p.type, p.ident)) { |
| 1750 | const char* bundleProduct = |
| 1751 | (bundle->getProduct() == NULL) ? "" : bundle->getProduct(); |
| 1752 | fprintf(stderr, "In resource file %s: %s\n", |
| 1753 | in->getPrintableSource().string(), |
| 1754 | curParams.toString().string()); |
| 1755 | |
| 1756 | fprintf(stderr, "\t%s '%s' does not match product %s.\n" |
| 1757 | "\tYou may have forgotten to include a 'default' product variant" |
| 1758 | " of the resource.\n", |
| 1759 | String8(p.type).string(), String8(p.ident).string(), |
| 1760 | bundleProduct[0] == 0 ? "default" : bundleProduct); |
| 1761 | return UNKNOWN_ERROR; |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 1766 | return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1767 | } |
| 1768 | |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 1769 | ResourceTable::ResourceTable(Bundle* bundle, const String16& assetsPackage, ResourceTable::PackageType type) |
| 1770 | : mAssetsPackage(assetsPackage) |
| 1771 | , mPackageType(type) |
| 1772 | , mTypeIdOffset(0) |
| 1773 | , mNumLocal(0) |
| 1774 | , mBundle(bundle) |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1775 | { |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 1776 | ssize_t packageId = -1; |
| 1777 | switch (mPackageType) { |
| 1778 | case App: |
| 1779 | case AppFeature: |
| 1780 | packageId = 0x7f; |
| 1781 | break; |
| 1782 | |
| 1783 | case System: |
| 1784 | packageId = 0x01; |
| 1785 | break; |
| 1786 | |
| 1787 | case SharedLibrary: |
| 1788 | packageId = 0x00; |
| 1789 | break; |
| 1790 | |
| 1791 | default: |
| 1792 | assert(0); |
| 1793 | break; |
| 1794 | } |
| 1795 | sp<Package> package = new Package(mAssetsPackage, packageId); |
| 1796 | mPackages.add(assetsPackage, package); |
| 1797 | mOrderedPackages.add(package); |
| 1798 | |
| 1799 | // Every resource table always has one first entry, the bag attributes. |
| 1800 | const SourcePos unknown(String8("????"), 0); |
| 1801 | getType(mAssetsPackage, String16("attr"), unknown); |
| 1802 | } |
| 1803 | |
| 1804 | static uint32_t findLargestTypeIdForPackage(const ResTable& table, const String16& packageName) { |
| 1805 | const size_t basePackageCount = table.getBasePackageCount(); |
| 1806 | for (size_t i = 0; i < basePackageCount; i++) { |
| 1807 | if (packageName == table.getBasePackageName(i)) { |
| 1808 | return table.getLastTypeIdForPackage(i); |
| 1809 | } |
| 1810 | } |
| 1811 | return 0; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | status_t ResourceTable::addIncludedResources(Bundle* bundle, const sp<AaptAssets>& assets) |
| 1815 | { |
| 1816 | status_t err = assets->buildIncludedResources(bundle); |
| 1817 | if (err != NO_ERROR) { |
| 1818 | return err; |
| 1819 | } |
| 1820 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1821 | mAssets = assets; |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 1822 | mTypeIdOffset = findLargestTypeIdForPackage(assets->getIncludedResources(), mAssetsPackage); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1823 | |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 1824 | const String8& featureAfter = bundle->getFeatureAfterPackage(); |
| 1825 | if (!featureAfter.isEmpty()) { |
| 1826 | AssetManager featureAssetManager; |
| 1827 | if (!featureAssetManager.addAssetPath(featureAfter, NULL)) { |
| 1828 | fprintf(stderr, "ERROR: Feature package '%s' not found.\n", |
| 1829 | featureAfter.string()); |
| 1830 | return UNKNOWN_ERROR; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1831 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1832 | |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 1833 | const ResTable& featureTable = featureAssetManager.getResources(false); |
Dan Albert | 030f536 | 2015-03-04 13:54:20 -0800 | [diff] [blame] | 1834 | mTypeIdOffset = std::max(mTypeIdOffset, |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 1835 | findLargestTypeIdForPackage(featureTable, mAssetsPackage)); |
| 1836 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1837 | |
| 1838 | return NO_ERROR; |
| 1839 | } |
| 1840 | |
| 1841 | status_t ResourceTable::addPublic(const SourcePos& sourcePos, |
| 1842 | const String16& package, |
| 1843 | const String16& type, |
| 1844 | const String16& name, |
| 1845 | const uint32_t ident) |
| 1846 | { |
| 1847 | uint32_t rid = mAssets->getIncludedResources() |
| 1848 | .identifierForName(name.string(), name.size(), |
| 1849 | type.string(), type.size(), |
| 1850 | package.string(), package.size()); |
| 1851 | if (rid != 0) { |
| 1852 | sourcePos.error("Error declaring public resource %s/%s for included package %s\n", |
| 1853 | String8(type).string(), String8(name).string(), |
| 1854 | String8(package).string()); |
| 1855 | return UNKNOWN_ERROR; |
| 1856 | } |
| 1857 | |
| 1858 | sp<Type> t = getType(package, type, sourcePos); |
| 1859 | if (t == NULL) { |
| 1860 | return UNKNOWN_ERROR; |
| 1861 | } |
| 1862 | return t->addPublic(sourcePos, name, ident); |
| 1863 | } |
| 1864 | |
| 1865 | status_t ResourceTable::addEntry(const SourcePos& sourcePos, |
| 1866 | const String16& package, |
| 1867 | const String16& type, |
| 1868 | const String16& name, |
| 1869 | const String16& value, |
| 1870 | const Vector<StringPool::entry_style_span>* style, |
| 1871 | const ResTable_config* params, |
| 1872 | const bool doSetIndex, |
| 1873 | const int32_t format, |
| 1874 | const bool overwrite) |
| 1875 | { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1876 | uint32_t rid = mAssets->getIncludedResources() |
| 1877 | .identifierForName(name.string(), name.size(), |
| 1878 | type.string(), type.size(), |
| 1879 | package.string(), package.size()); |
| 1880 | if (rid != 0) { |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 1881 | sourcePos.error("Resource entry %s/%s is already defined in package %s.", |
| 1882 | String8(type).string(), String8(name).string(), String8(package).string()); |
| 1883 | return UNKNOWN_ERROR; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1884 | } |
| 1885 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1886 | sp<Entry> e = getEntry(package, type, name, sourcePos, overwrite, |
| 1887 | params, doSetIndex); |
| 1888 | if (e == NULL) { |
| 1889 | return UNKNOWN_ERROR; |
| 1890 | } |
| 1891 | status_t err = e->setItem(sourcePos, value, style, format, overwrite); |
| 1892 | if (err == NO_ERROR) { |
| 1893 | mNumLocal++; |
| 1894 | } |
| 1895 | return err; |
| 1896 | } |
| 1897 | |
| 1898 | status_t ResourceTable::startBag(const SourcePos& sourcePos, |
| 1899 | const String16& package, |
| 1900 | const String16& type, |
| 1901 | const String16& name, |
| 1902 | const String16& bagParent, |
| 1903 | const ResTable_config* params, |
| 1904 | bool overlay, |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 1905 | bool replace, bool /* isId */) |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1906 | { |
| 1907 | status_t result = NO_ERROR; |
| 1908 | |
| 1909 | // Check for adding entries in other packages... for now we do |
| 1910 | // nothing. We need to do the right thing here to support skinning. |
| 1911 | uint32_t rid = mAssets->getIncludedResources() |
| 1912 | .identifierForName(name.string(), name.size(), |
| 1913 | type.string(), type.size(), |
| 1914 | package.string(), package.size()); |
| 1915 | if (rid != 0) { |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 1916 | sourcePos.error("Resource entry %s/%s is already defined in package %s.", |
| 1917 | String8(type).string(), String8(name).string(), String8(package).string()); |
| 1918 | return UNKNOWN_ERROR; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1919 | } |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 1920 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1921 | if (overlay && !mBundle->getAutoAddOverlay() && !hasBagOrEntry(package, type, name)) { |
| 1922 | bool canAdd = false; |
| 1923 | sp<Package> p = mPackages.valueFor(package); |
| 1924 | if (p != NULL) { |
| 1925 | sp<Type> t = p->getTypes().valueFor(type); |
| 1926 | if (t != NULL) { |
| 1927 | if (t->getCanAddEntries().indexOf(name) >= 0) { |
| 1928 | canAdd = true; |
| 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | if (!canAdd) { |
| 1933 | sourcePos.error("Resource does not already exist in overlay at '%s'; use <add-resource> to add.\n", |
| 1934 | String8(name).string()); |
| 1935 | return UNKNOWN_ERROR; |
| 1936 | } |
| 1937 | } |
| 1938 | sp<Entry> e = getEntry(package, type, name, sourcePos, overlay, params); |
| 1939 | if (e == NULL) { |
| 1940 | return UNKNOWN_ERROR; |
| 1941 | } |
| 1942 | |
| 1943 | // If a parent is explicitly specified, set it. |
| 1944 | if (bagParent.size() > 0) { |
| 1945 | e->setParent(bagParent); |
| 1946 | } |
| 1947 | |
| 1948 | if ((result = e->makeItABag(sourcePos)) != NO_ERROR) { |
| 1949 | return result; |
| 1950 | } |
| 1951 | |
| 1952 | if (overlay && replace) { |
| 1953 | return e->emptyBag(sourcePos); |
| 1954 | } |
| 1955 | return result; |
| 1956 | } |
| 1957 | |
| 1958 | status_t ResourceTable::addBag(const SourcePos& sourcePos, |
| 1959 | const String16& package, |
| 1960 | const String16& type, |
| 1961 | const String16& name, |
| 1962 | const String16& bagParent, |
| 1963 | const String16& bagKey, |
| 1964 | const String16& value, |
| 1965 | const Vector<StringPool::entry_style_span>* style, |
| 1966 | const ResTable_config* params, |
| 1967 | bool replace, bool isId, const int32_t format) |
| 1968 | { |
| 1969 | // Check for adding entries in other packages... for now we do |
| 1970 | // nothing. We need to do the right thing here to support skinning. |
| 1971 | uint32_t rid = mAssets->getIncludedResources() |
| 1972 | .identifierForName(name.string(), name.size(), |
| 1973 | type.string(), type.size(), |
| 1974 | package.string(), package.size()); |
| 1975 | if (rid != 0) { |
| 1976 | return NO_ERROR; |
| 1977 | } |
| 1978 | |
| 1979 | #if 0 |
| 1980 | if (name == String16("left")) { |
| 1981 | printf("Adding bag left: file=%s, line=%d, type=%s\n", |
| 1982 | sourcePos.file.striing(), sourcePos.line, String8(type).string()); |
| 1983 | } |
| 1984 | #endif |
| 1985 | sp<Entry> e = getEntry(package, type, name, sourcePos, replace, params); |
| 1986 | if (e == NULL) { |
| 1987 | return UNKNOWN_ERROR; |
| 1988 | } |
| 1989 | |
| 1990 | // If a parent is explicitly specified, set it. |
| 1991 | if (bagParent.size() > 0) { |
| 1992 | e->setParent(bagParent); |
| 1993 | } |
| 1994 | |
| 1995 | const bool first = e->getBag().indexOfKey(bagKey) < 0; |
| 1996 | status_t err = e->addToBag(sourcePos, bagKey, value, style, replace, isId, format); |
| 1997 | if (err == NO_ERROR && first) { |
| 1998 | mNumLocal++; |
| 1999 | } |
| 2000 | return err; |
| 2001 | } |
| 2002 | |
| 2003 | bool ResourceTable::hasBagOrEntry(const String16& package, |
| 2004 | const String16& type, |
| 2005 | const String16& name) const |
| 2006 | { |
| 2007 | // First look for this in the included resources... |
| 2008 | uint32_t rid = mAssets->getIncludedResources() |
| 2009 | .identifierForName(name.string(), name.size(), |
| 2010 | type.string(), type.size(), |
| 2011 | package.string(), package.size()); |
| 2012 | if (rid != 0) { |
| 2013 | return true; |
| 2014 | } |
| 2015 | |
| 2016 | sp<Package> p = mPackages.valueFor(package); |
| 2017 | if (p != NULL) { |
| 2018 | sp<Type> t = p->getTypes().valueFor(type); |
| 2019 | if (t != NULL) { |
| 2020 | sp<ConfigList> c = t->getConfigs().valueFor(name); |
| 2021 | if (c != NULL) return true; |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | return false; |
| 2026 | } |
| 2027 | |
| 2028 | bool ResourceTable::hasBagOrEntry(const String16& package, |
| 2029 | const String16& type, |
| 2030 | const String16& name, |
| 2031 | const ResTable_config& config) const |
| 2032 | { |
| 2033 | // First look for this in the included resources... |
| 2034 | uint32_t rid = mAssets->getIncludedResources() |
| 2035 | .identifierForName(name.string(), name.size(), |
| 2036 | type.string(), type.size(), |
| 2037 | package.string(), package.size()); |
| 2038 | if (rid != 0) { |
| 2039 | return true; |
| 2040 | } |
| 2041 | |
| 2042 | sp<Package> p = mPackages.valueFor(package); |
| 2043 | if (p != NULL) { |
| 2044 | sp<Type> t = p->getTypes().valueFor(type); |
| 2045 | if (t != NULL) { |
| 2046 | sp<ConfigList> c = t->getConfigs().valueFor(name); |
| 2047 | if (c != NULL) { |
| 2048 | sp<Entry> e = c->getEntries().valueFor(config); |
| 2049 | if (e != NULL) { |
| 2050 | return true; |
| 2051 | } |
| 2052 | } |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | return false; |
| 2057 | } |
| 2058 | |
| 2059 | bool ResourceTable::hasBagOrEntry(const String16& ref, |
| 2060 | const String16* defType, |
| 2061 | const String16* defPackage) |
| 2062 | { |
| 2063 | String16 package, type, name; |
| 2064 | if (!ResTable::expandResourceRef(ref.string(), ref.size(), &package, &type, &name, |
| 2065 | defType, defPackage ? defPackage:&mAssetsPackage, NULL)) { |
| 2066 | return false; |
| 2067 | } |
| 2068 | return hasBagOrEntry(package, type, name); |
| 2069 | } |
| 2070 | |
| 2071 | bool ResourceTable::appendComment(const String16& package, |
| 2072 | const String16& type, |
| 2073 | const String16& name, |
| 2074 | const String16& comment, |
| 2075 | bool onlyIfEmpty) |
| 2076 | { |
| 2077 | if (comment.size() <= 0) { |
| 2078 | return true; |
| 2079 | } |
| 2080 | |
| 2081 | sp<Package> p = mPackages.valueFor(package); |
| 2082 | if (p != NULL) { |
| 2083 | sp<Type> t = p->getTypes().valueFor(type); |
| 2084 | if (t != NULL) { |
| 2085 | sp<ConfigList> c = t->getConfigs().valueFor(name); |
| 2086 | if (c != NULL) { |
| 2087 | c->appendComment(comment, onlyIfEmpty); |
| 2088 | return true; |
| 2089 | } |
| 2090 | } |
| 2091 | } |
| 2092 | return false; |
| 2093 | } |
| 2094 | |
| 2095 | bool ResourceTable::appendTypeComment(const String16& package, |
| 2096 | const String16& type, |
| 2097 | const String16& name, |
| 2098 | const String16& comment) |
| 2099 | { |
| 2100 | if (comment.size() <= 0) { |
| 2101 | return true; |
| 2102 | } |
| 2103 | |
| 2104 | sp<Package> p = mPackages.valueFor(package); |
| 2105 | if (p != NULL) { |
| 2106 | sp<Type> t = p->getTypes().valueFor(type); |
| 2107 | if (t != NULL) { |
| 2108 | sp<ConfigList> c = t->getConfigs().valueFor(name); |
| 2109 | if (c != NULL) { |
| 2110 | c->appendTypeComment(comment); |
| 2111 | return true; |
| 2112 | } |
| 2113 | } |
| 2114 | } |
| 2115 | return false; |
| 2116 | } |
| 2117 | |
| 2118 | void ResourceTable::canAddEntry(const SourcePos& pos, |
| 2119 | const String16& package, const String16& type, const String16& name) |
| 2120 | { |
| 2121 | sp<Type> t = getType(package, type, pos); |
| 2122 | if (t != NULL) { |
| 2123 | t->canAddEntry(name); |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | size_t ResourceTable::size() const { |
| 2128 | return mPackages.size(); |
| 2129 | } |
| 2130 | |
| 2131 | size_t ResourceTable::numLocalResources() const { |
| 2132 | return mNumLocal; |
| 2133 | } |
| 2134 | |
| 2135 | bool ResourceTable::hasResources() const { |
| 2136 | return mNumLocal > 0; |
| 2137 | } |
| 2138 | |
Adam Lesinski | 27f69f4 | 2014-08-21 13:19:12 -0700 | [diff] [blame] | 2139 | sp<AaptFile> ResourceTable::flatten(Bundle* bundle, const sp<const ResourceFilter>& filter, |
| 2140 | const bool isBase) |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2141 | { |
| 2142 | sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8()); |
Adam Lesinski | 27f69f4 | 2014-08-21 13:19:12 -0700 | [diff] [blame] | 2143 | status_t err = flatten(bundle, filter, data, isBase); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2144 | return err == NO_ERROR ? data : NULL; |
| 2145 | } |
| 2146 | |
| 2147 | inline uint32_t ResourceTable::getResId(const sp<Package>& p, |
| 2148 | const sp<Type>& t, |
| 2149 | uint32_t nameId) |
| 2150 | { |
| 2151 | return makeResId(p->getAssignedId(), t->getIndex(), nameId); |
| 2152 | } |
| 2153 | |
| 2154 | uint32_t ResourceTable::getResId(const String16& package, |
| 2155 | const String16& type, |
| 2156 | const String16& name, |
| 2157 | bool onlyPublic) const |
| 2158 | { |
| 2159 | uint32_t id = ResourceIdCache::lookup(package, type, name, onlyPublic); |
| 2160 | if (id != 0) return id; // cache hit |
| 2161 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2162 | // First look for this in the included resources... |
| 2163 | uint32_t specFlags = 0; |
| 2164 | uint32_t rid = mAssets->getIncludedResources() |
| 2165 | .identifierForName(name.string(), name.size(), |
| 2166 | type.string(), type.size(), |
| 2167 | package.string(), package.size(), |
| 2168 | &specFlags); |
| 2169 | if (rid != 0) { |
| 2170 | if (onlyPublic) { |
| 2171 | if ((specFlags & ResTable_typeSpec::SPEC_PUBLIC) == 0) { |
| 2172 | return 0; |
| 2173 | } |
| 2174 | } |
| 2175 | |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2176 | return ResourceIdCache::store(package, type, name, onlyPublic, rid); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2177 | } |
| 2178 | |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2179 | sp<Package> p = mPackages.valueFor(package); |
| 2180 | if (p == NULL) return 0; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2181 | sp<Type> t = p->getTypes().valueFor(type); |
| 2182 | if (t == NULL) return 0; |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 2183 | sp<ConfigList> c = t->getConfigs().valueFor(name); |
| 2184 | if (c == NULL) { |
| 2185 | if (type != String16("attr")) { |
| 2186 | return 0; |
| 2187 | } |
| 2188 | t = p->getTypes().valueFor(String16(kAttrPrivateType)); |
| 2189 | if (t == NULL) return 0; |
| 2190 | c = t->getConfigs().valueFor(name); |
| 2191 | if (c == NULL) return 0; |
| 2192 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2193 | int32_t ei = c->getEntryIndex(); |
| 2194 | if (ei < 0) return 0; |
| 2195 | |
| 2196 | return ResourceIdCache::store(package, type, name, onlyPublic, |
| 2197 | getResId(p, t, ei)); |
| 2198 | } |
| 2199 | |
| 2200 | uint32_t ResourceTable::getResId(const String16& ref, |
| 2201 | const String16* defType, |
| 2202 | const String16* defPackage, |
| 2203 | const char** outErrorMsg, |
| 2204 | bool onlyPublic) const |
| 2205 | { |
| 2206 | String16 package, type, name; |
| 2207 | bool refOnlyPublic = true; |
| 2208 | if (!ResTable::expandResourceRef( |
| 2209 | ref.string(), ref.size(), &package, &type, &name, |
| 2210 | defType, defPackage ? defPackage:&mAssetsPackage, |
| 2211 | outErrorMsg, &refOnlyPublic)) { |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 2212 | if (kIsDebug) { |
| 2213 | printf("Expanding resource: ref=%s\n", String8(ref).string()); |
| 2214 | printf("Expanding resource: defType=%s\n", |
| 2215 | defType ? String8(*defType).string() : "NULL"); |
| 2216 | printf("Expanding resource: defPackage=%s\n", |
| 2217 | defPackage ? String8(*defPackage).string() : "NULL"); |
| 2218 | printf("Expanding resource: ref=%s\n", String8(ref).string()); |
| 2219 | printf("Expanded resource: p=%s, t=%s, n=%s, res=0\n", |
| 2220 | String8(package).string(), String8(type).string(), |
| 2221 | String8(name).string()); |
| 2222 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2223 | return 0; |
| 2224 | } |
| 2225 | uint32_t res = getResId(package, type, name, onlyPublic && refOnlyPublic); |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 2226 | if (kIsDebug) { |
| 2227 | printf("Expanded resource: p=%s, t=%s, n=%s, res=%d\n", |
| 2228 | String8(package).string(), String8(type).string(), |
| 2229 | String8(name).string(), res); |
| 2230 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2231 | if (res == 0) { |
| 2232 | if (outErrorMsg) |
| 2233 | *outErrorMsg = "No resource found that matches the given name"; |
| 2234 | } |
| 2235 | return res; |
| 2236 | } |
| 2237 | |
| 2238 | bool ResourceTable::isValidResourceName(const String16& s) |
| 2239 | { |
| 2240 | const char16_t* p = s.string(); |
| 2241 | bool first = true; |
| 2242 | while (*p) { |
| 2243 | if ((*p >= 'a' && *p <= 'z') |
| 2244 | || (*p >= 'A' && *p <= 'Z') |
| 2245 | || *p == '_' |
| 2246 | || (!first && *p >= '0' && *p <= '9')) { |
| 2247 | first = false; |
| 2248 | p++; |
| 2249 | continue; |
| 2250 | } |
| 2251 | return false; |
| 2252 | } |
| 2253 | return true; |
| 2254 | } |
| 2255 | |
| 2256 | bool ResourceTable::stringToValue(Res_value* outValue, StringPool* pool, |
| 2257 | const String16& str, |
| 2258 | bool preserveSpaces, bool coerceType, |
| 2259 | uint32_t attrID, |
| 2260 | const Vector<StringPool::entry_style_span>* style, |
| 2261 | String16* outStr, void* accessorCookie, |
| 2262 | uint32_t attrType, const String8* configTypeName, |
| 2263 | const ConfigDescription* config) |
| 2264 | { |
| 2265 | String16 finalStr; |
| 2266 | |
| 2267 | bool res = true; |
| 2268 | if (style == NULL || style->size() == 0) { |
| 2269 | // Text is not styled so it can be any type... let's figure it out. |
| 2270 | res = mAssets->getIncludedResources() |
| 2271 | .stringToValue(outValue, &finalStr, str.string(), str.size(), preserveSpaces, |
| 2272 | coerceType, attrID, NULL, &mAssetsPackage, this, |
| 2273 | accessorCookie, attrType); |
| 2274 | } else { |
| 2275 | // Styled text can only be a string, and while collecting the style |
| 2276 | // information we have already processed that string! |
| 2277 | outValue->size = sizeof(Res_value); |
| 2278 | outValue->res0 = 0; |
| 2279 | outValue->dataType = outValue->TYPE_STRING; |
| 2280 | outValue->data = 0; |
| 2281 | finalStr = str; |
| 2282 | } |
| 2283 | |
| 2284 | if (!res) { |
| 2285 | return false; |
| 2286 | } |
| 2287 | |
| 2288 | if (outValue->dataType == outValue->TYPE_STRING) { |
| 2289 | // Should do better merging styles. |
| 2290 | if (pool) { |
| 2291 | String8 configStr; |
| 2292 | if (config != NULL) { |
| 2293 | configStr = config->toString(); |
| 2294 | } else { |
| 2295 | configStr = "(null)"; |
| 2296 | } |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 2297 | if (kIsDebug) { |
| 2298 | printf("Adding to pool string style #%zu config %s: %s\n", |
| 2299 | style != NULL ? style->size() : 0U, |
| 2300 | configStr.string(), String8(finalStr).string()); |
| 2301 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2302 | if (style != NULL && style->size() > 0) { |
| 2303 | outValue->data = pool->add(finalStr, *style, configTypeName, config); |
| 2304 | } else { |
| 2305 | outValue->data = pool->add(finalStr, true, configTypeName, config); |
| 2306 | } |
| 2307 | } else { |
| 2308 | // Caller will fill this in later. |
| 2309 | outValue->data = 0; |
| 2310 | } |
| 2311 | |
| 2312 | if (outStr) { |
| 2313 | *outStr = finalStr; |
| 2314 | } |
| 2315 | |
| 2316 | } |
| 2317 | |
| 2318 | return true; |
| 2319 | } |
| 2320 | |
| 2321 | uint32_t ResourceTable::getCustomResource( |
| 2322 | const String16& package, const String16& type, const String16& name) const |
| 2323 | { |
| 2324 | //printf("getCustomResource: %s %s %s\n", String8(package).string(), |
| 2325 | // String8(type).string(), String8(name).string()); |
| 2326 | sp<Package> p = mPackages.valueFor(package); |
| 2327 | if (p == NULL) return 0; |
| 2328 | sp<Type> t = p->getTypes().valueFor(type); |
| 2329 | if (t == NULL) return 0; |
| 2330 | sp<ConfigList> c = t->getConfigs().valueFor(name); |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 2331 | if (c == NULL) { |
| 2332 | if (type != String16("attr")) { |
| 2333 | return 0; |
| 2334 | } |
| 2335 | t = p->getTypes().valueFor(String16(kAttrPrivateType)); |
| 2336 | if (t == NULL) return 0; |
| 2337 | c = t->getConfigs().valueFor(name); |
| 2338 | if (c == NULL) return 0; |
| 2339 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2340 | int32_t ei = c->getEntryIndex(); |
| 2341 | if (ei < 0) return 0; |
| 2342 | return getResId(p, t, ei); |
| 2343 | } |
| 2344 | |
| 2345 | uint32_t ResourceTable::getCustomResourceWithCreation( |
| 2346 | const String16& package, const String16& type, const String16& name, |
| 2347 | const bool createIfNotFound) |
| 2348 | { |
| 2349 | uint32_t resId = getCustomResource(package, type, name); |
| 2350 | if (resId != 0 || !createIfNotFound) { |
| 2351 | return resId; |
| 2352 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2353 | |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 2354 | if (mAssetsPackage != package) { |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2355 | mCurrentXmlPos.error("creating resource for external package %s: %s/%s.", |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 2356 | String8(package).string(), String8(type).string(), String8(name).string()); |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2357 | if (package == String16("android")) { |
| 2358 | mCurrentXmlPos.printf("did you mean to use @+id instead of @+android:id?"); |
| 2359 | } |
| 2360 | return 0; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | String16 value("false"); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2364 | status_t status = addEntry(mCurrentXmlPos, package, type, name, value, NULL, NULL, true); |
| 2365 | if (status == NO_ERROR) { |
| 2366 | resId = getResId(package, type, name); |
| 2367 | return resId; |
| 2368 | } |
| 2369 | return 0; |
| 2370 | } |
| 2371 | |
| 2372 | uint32_t ResourceTable::getRemappedPackage(uint32_t origPackage) const |
| 2373 | { |
| 2374 | return origPackage; |
| 2375 | } |
| 2376 | |
| 2377 | bool ResourceTable::getAttributeType(uint32_t attrID, uint32_t* outType) |
| 2378 | { |
| 2379 | //printf("getAttributeType #%08x\n", attrID); |
| 2380 | Res_value value; |
| 2381 | if (getItemValue(attrID, ResTable_map::ATTR_TYPE, &value)) { |
| 2382 | //printf("getAttributeType #%08x (%s): #%08x\n", attrID, |
| 2383 | // String8(getEntry(attrID)->getName()).string(), value.data); |
| 2384 | *outType = value.data; |
| 2385 | return true; |
| 2386 | } |
| 2387 | return false; |
| 2388 | } |
| 2389 | |
| 2390 | bool ResourceTable::getAttributeMin(uint32_t attrID, uint32_t* outMin) |
| 2391 | { |
| 2392 | //printf("getAttributeMin #%08x\n", attrID); |
| 2393 | Res_value value; |
| 2394 | if (getItemValue(attrID, ResTable_map::ATTR_MIN, &value)) { |
| 2395 | *outMin = value.data; |
| 2396 | return true; |
| 2397 | } |
| 2398 | return false; |
| 2399 | } |
| 2400 | |
| 2401 | bool ResourceTable::getAttributeMax(uint32_t attrID, uint32_t* outMax) |
| 2402 | { |
| 2403 | //printf("getAttributeMax #%08x\n", attrID); |
| 2404 | Res_value value; |
| 2405 | if (getItemValue(attrID, ResTable_map::ATTR_MAX, &value)) { |
| 2406 | *outMax = value.data; |
| 2407 | return true; |
| 2408 | } |
| 2409 | return false; |
| 2410 | } |
| 2411 | |
| 2412 | uint32_t ResourceTable::getAttributeL10N(uint32_t attrID) |
| 2413 | { |
| 2414 | //printf("getAttributeL10N #%08x\n", attrID); |
| 2415 | Res_value value; |
| 2416 | if (getItemValue(attrID, ResTable_map::ATTR_L10N, &value)) { |
| 2417 | return value.data; |
| 2418 | } |
| 2419 | return ResTable_map::L10N_NOT_REQUIRED; |
| 2420 | } |
| 2421 | |
| 2422 | bool ResourceTable::getLocalizationSetting() |
| 2423 | { |
| 2424 | return mBundle->getRequireLocalization(); |
| 2425 | } |
| 2426 | |
| 2427 | void ResourceTable::reportError(void* accessorCookie, const char* fmt, ...) |
| 2428 | { |
| 2429 | if (accessorCookie != NULL && fmt != NULL) { |
| 2430 | AccessorCookie* ac = (AccessorCookie*)accessorCookie; |
| 2431 | int retval=0; |
| 2432 | char buf[1024]; |
| 2433 | va_list ap; |
| 2434 | va_start(ap, fmt); |
| 2435 | retval = vsnprintf(buf, sizeof(buf), fmt, ap); |
| 2436 | va_end(ap); |
| 2437 | ac->sourcePos.error("Error: %s (at '%s' with value '%s').\n", |
| 2438 | buf, ac->attr.string(), ac->value.string()); |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | bool ResourceTable::getAttributeKeys( |
| 2443 | uint32_t attrID, Vector<String16>* outKeys) |
| 2444 | { |
| 2445 | sp<const Entry> e = getEntry(attrID); |
| 2446 | if (e != NULL) { |
| 2447 | const size_t N = e->getBag().size(); |
| 2448 | for (size_t i=0; i<N; i++) { |
| 2449 | const String16& key = e->getBag().keyAt(i); |
| 2450 | if (key.size() > 0 && key.string()[0] != '^') { |
| 2451 | outKeys->add(key); |
| 2452 | } |
| 2453 | } |
| 2454 | return true; |
| 2455 | } |
| 2456 | return false; |
| 2457 | } |
| 2458 | |
| 2459 | bool ResourceTable::getAttributeEnum( |
| 2460 | uint32_t attrID, const char16_t* name, size_t nameLen, |
| 2461 | Res_value* outValue) |
| 2462 | { |
| 2463 | //printf("getAttributeEnum #%08x %s\n", attrID, String8(name, nameLen).string()); |
| 2464 | String16 nameStr(name, nameLen); |
| 2465 | sp<const Entry> e = getEntry(attrID); |
| 2466 | if (e != NULL) { |
| 2467 | const size_t N = e->getBag().size(); |
| 2468 | for (size_t i=0; i<N; i++) { |
| 2469 | //printf("Comparing %s to %s\n", String8(name, nameLen).string(), |
| 2470 | // String8(e->getBag().keyAt(i)).string()); |
| 2471 | if (e->getBag().keyAt(i) == nameStr) { |
| 2472 | return getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, outValue); |
| 2473 | } |
| 2474 | } |
| 2475 | } |
| 2476 | return false; |
| 2477 | } |
| 2478 | |
| 2479 | bool ResourceTable::getAttributeFlags( |
| 2480 | uint32_t attrID, const char16_t* name, size_t nameLen, |
| 2481 | Res_value* outValue) |
| 2482 | { |
| 2483 | outValue->dataType = Res_value::TYPE_INT_HEX; |
| 2484 | outValue->data = 0; |
| 2485 | |
| 2486 | //printf("getAttributeFlags #%08x %s\n", attrID, String8(name, nameLen).string()); |
| 2487 | String16 nameStr(name, nameLen); |
| 2488 | sp<const Entry> e = getEntry(attrID); |
| 2489 | if (e != NULL) { |
| 2490 | const size_t N = e->getBag().size(); |
| 2491 | |
| 2492 | const char16_t* end = name + nameLen; |
| 2493 | const char16_t* pos = name; |
| 2494 | while (pos < end) { |
| 2495 | const char16_t* start = pos; |
| 2496 | while (pos < end && *pos != '|') { |
| 2497 | pos++; |
| 2498 | } |
| 2499 | |
| 2500 | String16 nameStr(start, pos-start); |
| 2501 | size_t i; |
| 2502 | for (i=0; i<N; i++) { |
| 2503 | //printf("Comparing \"%s\" to \"%s\"\n", String8(nameStr).string(), |
| 2504 | // String8(e->getBag().keyAt(i)).string()); |
| 2505 | if (e->getBag().keyAt(i) == nameStr) { |
| 2506 | Res_value val; |
| 2507 | bool got = getItemValue(attrID, e->getBag().valueAt(i).bagKeyId, &val); |
| 2508 | if (!got) { |
| 2509 | return false; |
| 2510 | } |
| 2511 | //printf("Got value: 0x%08x\n", val.data); |
| 2512 | outValue->data |= val.data; |
| 2513 | break; |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | if (i >= N) { |
| 2518 | // Didn't find this flag identifier. |
| 2519 | return false; |
| 2520 | } |
| 2521 | pos++; |
| 2522 | } |
| 2523 | |
| 2524 | return true; |
| 2525 | } |
| 2526 | return false; |
| 2527 | } |
| 2528 | |
| 2529 | status_t ResourceTable::assignResourceIds() |
| 2530 | { |
| 2531 | const size_t N = mOrderedPackages.size(); |
| 2532 | size_t pi; |
| 2533 | status_t firstError = NO_ERROR; |
| 2534 | |
| 2535 | // First generate all bag attributes and assign indices. |
| 2536 | for (pi=0; pi<N; pi++) { |
| 2537 | sp<Package> p = mOrderedPackages.itemAt(pi); |
| 2538 | if (p == NULL || p->getTypes().size() == 0) { |
| 2539 | // Empty, skip! |
| 2540 | continue; |
| 2541 | } |
| 2542 | |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 2543 | if (mPackageType == System) { |
| 2544 | p->movePrivateAttrs(); |
| 2545 | } |
| 2546 | |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2547 | // This has no sense for packages being built as AppFeature (aka with a non-zero offset). |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2548 | status_t err = p->applyPublicTypeOrder(); |
| 2549 | if (err != NO_ERROR && firstError == NO_ERROR) { |
| 2550 | firstError = err; |
| 2551 | } |
| 2552 | |
| 2553 | // Generate attributes... |
| 2554 | const size_t N = p->getOrderedTypes().size(); |
| 2555 | size_t ti; |
| 2556 | for (ti=0; ti<N; ti++) { |
| 2557 | sp<Type> t = p->getOrderedTypes().itemAt(ti); |
| 2558 | if (t == NULL) { |
| 2559 | continue; |
| 2560 | } |
| 2561 | const size_t N = t->getOrderedConfigs().size(); |
| 2562 | for (size_t ci=0; ci<N; ci++) { |
| 2563 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); |
| 2564 | if (c == NULL) { |
| 2565 | continue; |
| 2566 | } |
| 2567 | const size_t N = c->getEntries().size(); |
| 2568 | for (size_t ei=0; ei<N; ei++) { |
| 2569 | sp<Entry> e = c->getEntries().valueAt(ei); |
| 2570 | if (e == NULL) { |
| 2571 | continue; |
| 2572 | } |
| 2573 | status_t err = e->generateAttributes(this, p->getName()); |
| 2574 | if (err != NO_ERROR && firstError == NO_ERROR) { |
| 2575 | firstError = err; |
| 2576 | } |
| 2577 | } |
| 2578 | } |
| 2579 | } |
| 2580 | |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2581 | uint32_t typeIdOffset = 0; |
| 2582 | if (mPackageType == AppFeature && p->getName() == mAssetsPackage) { |
| 2583 | typeIdOffset = mTypeIdOffset; |
| 2584 | } |
| 2585 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2586 | const SourcePos unknown(String8("????"), 0); |
| 2587 | sp<Type> attr = p->getType(String16("attr"), unknown); |
| 2588 | |
| 2589 | // Assign indices... |
Adam Lesinski | 43a0df0 | 2014-08-18 17:14:57 -0700 | [diff] [blame] | 2590 | const size_t typeCount = p->getOrderedTypes().size(); |
| 2591 | for (size_t ti = 0; ti < typeCount; ti++) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2592 | sp<Type> t = p->getOrderedTypes().itemAt(ti); |
| 2593 | if (t == NULL) { |
| 2594 | continue; |
| 2595 | } |
Adam Lesinski | 43a0df0 | 2014-08-18 17:14:57 -0700 | [diff] [blame] | 2596 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2597 | err = t->applyPublicEntryOrder(); |
| 2598 | if (err != NO_ERROR && firstError == NO_ERROR) { |
| 2599 | firstError = err; |
| 2600 | } |
| 2601 | |
| 2602 | const size_t N = t->getOrderedConfigs().size(); |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2603 | t->setIndex(ti + 1 + typeIdOffset); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2604 | |
| 2605 | LOG_ALWAYS_FATAL_IF(ti == 0 && attr != t, |
| 2606 | "First type is not attr!"); |
| 2607 | |
| 2608 | for (size_t ei=0; ei<N; ei++) { |
| 2609 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ei); |
| 2610 | if (c == NULL) { |
| 2611 | continue; |
| 2612 | } |
| 2613 | c->setEntryIndex(ei); |
| 2614 | } |
| 2615 | } |
| 2616 | |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 2617 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2618 | // Assign resource IDs to keys in bags... |
Adam Lesinski | 43a0df0 | 2014-08-18 17:14:57 -0700 | [diff] [blame] | 2619 | for (size_t ti = 0; ti < typeCount; ti++) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2620 | sp<Type> t = p->getOrderedTypes().itemAt(ti); |
| 2621 | if (t == NULL) { |
| 2622 | continue; |
| 2623 | } |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 2624 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2625 | const size_t N = t->getOrderedConfigs().size(); |
| 2626 | for (size_t ci=0; ci<N; ci++) { |
| 2627 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 2628 | if (c == NULL) { |
| 2629 | continue; |
| 2630 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2631 | //printf("Ordered config #%d: %p\n", ci, c.get()); |
| 2632 | const size_t N = c->getEntries().size(); |
| 2633 | for (size_t ei=0; ei<N; ei++) { |
| 2634 | sp<Entry> e = c->getEntries().valueAt(ei); |
| 2635 | if (e == NULL) { |
| 2636 | continue; |
| 2637 | } |
| 2638 | status_t err = e->assignResourceIds(this, p->getName()); |
| 2639 | if (err != NO_ERROR && firstError == NO_ERROR) { |
| 2640 | firstError = err; |
| 2641 | } |
| 2642 | } |
| 2643 | } |
| 2644 | } |
| 2645 | } |
| 2646 | return firstError; |
| 2647 | } |
| 2648 | |
Adrian Roos | 5892248 | 2015-06-01 17:59:41 -0700 | [diff] [blame] | 2649 | status_t ResourceTable::addSymbols(const sp<AaptSymbols>& outSymbols, |
| 2650 | bool skipSymbolsWithoutDefaultLocalization) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2651 | const size_t N = mOrderedPackages.size(); |
Adrian Roos | 5892248 | 2015-06-01 17:59:41 -0700 | [diff] [blame] | 2652 | const String8 defaultLocale; |
| 2653 | const String16 stringType("string"); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2654 | size_t pi; |
| 2655 | |
| 2656 | for (pi=0; pi<N; pi++) { |
| 2657 | sp<Package> p = mOrderedPackages.itemAt(pi); |
| 2658 | if (p->getTypes().size() == 0) { |
| 2659 | // Empty, skip! |
| 2660 | continue; |
| 2661 | } |
| 2662 | |
| 2663 | const size_t N = p->getOrderedTypes().size(); |
| 2664 | size_t ti; |
| 2665 | |
| 2666 | for (ti=0; ti<N; ti++) { |
| 2667 | sp<Type> t = p->getOrderedTypes().itemAt(ti); |
| 2668 | if (t == NULL) { |
| 2669 | continue; |
| 2670 | } |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 2671 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2672 | const size_t N = t->getOrderedConfigs().size(); |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 2673 | sp<AaptSymbols> typeSymbols; |
| 2674 | if (t->getName() == String16(kAttrPrivateType)) { |
| 2675 | typeSymbols = outSymbols->addNestedSymbol(String8("attr"), t->getPos()); |
| 2676 | } else { |
| 2677 | typeSymbols = outSymbols->addNestedSymbol(String8(t->getName()), t->getPos()); |
| 2678 | } |
| 2679 | |
Adam Lesinski | 3fb8c9b | 2014-09-09 16:05:10 -0700 | [diff] [blame] | 2680 | if (typeSymbols == NULL) { |
| 2681 | return UNKNOWN_ERROR; |
| 2682 | } |
| 2683 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2684 | for (size_t ci=0; ci<N; ci++) { |
| 2685 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); |
| 2686 | if (c == NULL) { |
| 2687 | continue; |
| 2688 | } |
| 2689 | uint32_t rid = getResId(p, t, ci); |
| 2690 | if (rid == 0) { |
| 2691 | return UNKNOWN_ERROR; |
| 2692 | } |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2693 | if (Res_GETPACKAGE(rid) + 1 == p->getAssignedId()) { |
Adrian Roos | 5892248 | 2015-06-01 17:59:41 -0700 | [diff] [blame] | 2694 | |
| 2695 | if (skipSymbolsWithoutDefaultLocalization && |
| 2696 | t->getName() == stringType) { |
| 2697 | |
| 2698 | // Don't generate symbols for strings without a default localization. |
| 2699 | if (mHasDefaultLocalization.find(c->getName()) |
| 2700 | == mHasDefaultLocalization.end()) { |
| 2701 | // printf("Skip symbol [%08x] %s\n", rid, |
| 2702 | // String8(c->getName()).string()); |
| 2703 | continue; |
| 2704 | } |
| 2705 | } |
| 2706 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2707 | typeSymbols->addSymbol(String8(c->getName()), rid, c->getPos()); |
| 2708 | |
| 2709 | String16 comment(c->getComment()); |
| 2710 | typeSymbols->appendComment(String8(c->getName()), comment, c->getPos()); |
Adam Lesinski | 8ff15b4 | 2013-10-07 16:54:01 -0700 | [diff] [blame] | 2711 | //printf("Type symbol [%08x] %s comment: %s\n", rid, |
| 2712 | // String8(c->getName()).string(), String8(comment).string()); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2713 | comment = c->getTypeComment(); |
| 2714 | typeSymbols->appendTypeComment(String8(c->getName()), comment); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | } |
| 2718 | } |
| 2719 | return NO_ERROR; |
| 2720 | } |
| 2721 | |
| 2722 | |
| 2723 | void |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2724 | ResourceTable::addLocalization(const String16& name, const String8& locale, const SourcePos& src) |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2725 | { |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2726 | mLocalizations[name][locale] = src; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2727 | } |
| 2728 | |
Adrian Roos | 5892248 | 2015-06-01 17:59:41 -0700 | [diff] [blame] | 2729 | void |
| 2730 | ResourceTable::addDefaultLocalization(const String16& name) |
| 2731 | { |
| 2732 | mHasDefaultLocalization.insert(name); |
| 2733 | } |
| 2734 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2735 | |
| 2736 | /*! |
| 2737 | * Flag various sorts of localization problems. '+' indicates checks already implemented; |
| 2738 | * '-' indicates checks that will be implemented in the future. |
| 2739 | * |
| 2740 | * + A localized string for which no default-locale version exists => warning |
| 2741 | * + A string for which no version in an explicitly-requested locale exists => warning |
| 2742 | * + A localized translation of an translateable="false" string => warning |
| 2743 | * - A localized string not provided in every locale used by the table |
| 2744 | */ |
| 2745 | status_t |
| 2746 | ResourceTable::validateLocalizations(void) |
| 2747 | { |
| 2748 | status_t err = NO_ERROR; |
| 2749 | const String8 defaultLocale; |
| 2750 | |
| 2751 | // For all strings... |
Dan Albert | 030f536 | 2015-03-04 13:54:20 -0800 | [diff] [blame] | 2752 | for (const auto& nameIter : mLocalizations) { |
| 2753 | const std::map<String8, SourcePos>& configSrcMap = nameIter.second; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2754 | |
| 2755 | // Look for strings with no default localization |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2756 | if (configSrcMap.count(defaultLocale) == 0) { |
| 2757 | SourcePos().warning("string '%s' has no default translation.", |
Dan Albert | 030f536 | 2015-03-04 13:54:20 -0800 | [diff] [blame] | 2758 | String8(nameIter.first).string()); |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2759 | if (mBundle->getVerbose()) { |
Dan Albert | 030f536 | 2015-03-04 13:54:20 -0800 | [diff] [blame] | 2760 | for (const auto& locale : configSrcMap) { |
| 2761 | locale.second.printf("locale %s found", locale.first.string()); |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2762 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2763 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2764 | // !!! TODO: throw an error here in some circumstances |
| 2765 | } |
| 2766 | |
| 2767 | // Check that all requested localizations are present for this string |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 2768 | if (mBundle->getConfigurations().size() > 0 && mBundle->getRequireLocalization()) { |
| 2769 | const char* allConfigs = mBundle->getConfigurations().string(); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2770 | const char* start = allConfigs; |
| 2771 | const char* comma; |
Dan Albert | 030f536 | 2015-03-04 13:54:20 -0800 | [diff] [blame] | 2772 | |
| 2773 | std::set<String8> missingConfigs; |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2774 | AaptLocaleValue locale; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2775 | do { |
| 2776 | String8 config; |
| 2777 | comma = strchr(start, ','); |
| 2778 | if (comma != NULL) { |
| 2779 | config.setTo(start, comma - start); |
| 2780 | start = comma + 1; |
| 2781 | } else { |
| 2782 | config.setTo(start); |
| 2783 | } |
| 2784 | |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2785 | if (!locale.initFromFilterString(config)) { |
| 2786 | continue; |
| 2787 | } |
| 2788 | |
Anton Krumin | a2ef5c0 | 2014-03-12 14:46:44 -0700 | [diff] [blame] | 2789 | // don't bother with the pseudolocale "en_XA" or "ar_XB" |
| 2790 | if (config != "en_XA" && config != "ar_XB") { |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2791 | if (configSrcMap.find(config) == configSrcMap.end()) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2792 | // okay, no specific localization found. it's possible that we are |
| 2793 | // requiring a specific regional localization [e.g. de_DE] but there is an |
| 2794 | // available string in the generic language localization [e.g. de]; |
| 2795 | // consider that string to have fulfilled the localization requirement. |
| 2796 | String8 region(config.string(), 2); |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2797 | if (configSrcMap.find(region) == configSrcMap.end() && |
| 2798 | configSrcMap.count(defaultLocale) == 0) { |
| 2799 | missingConfigs.insert(config); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2800 | } |
| 2801 | } |
| 2802 | } |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2803 | } while (comma != NULL); |
| 2804 | |
| 2805 | if (!missingConfigs.empty()) { |
| 2806 | String8 configStr; |
Dan Albert | 030f536 | 2015-03-04 13:54:20 -0800 | [diff] [blame] | 2807 | for (const auto& iter : missingConfigs) { |
| 2808 | configStr.appendFormat(" %s", iter.string()); |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2809 | } |
| 2810 | SourcePos().warning("string '%s' is missing %u required localizations:%s", |
Dan Albert | 030f536 | 2015-03-04 13:54:20 -0800 | [diff] [blame] | 2811 | String8(nameIter.first).string(), |
Adam Lesinski | a01a937 | 2014-03-20 18:04:57 -0700 | [diff] [blame] | 2812 | (unsigned int)missingConfigs.size(), |
| 2813 | configStr.string()); |
| 2814 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | return err; |
| 2819 | } |
| 2820 | |
Adam Lesinski | 27f69f4 | 2014-08-21 13:19:12 -0700 | [diff] [blame] | 2821 | status_t ResourceTable::flatten(Bundle* bundle, const sp<const ResourceFilter>& filter, |
| 2822 | const sp<AaptFile>& dest, |
| 2823 | const bool isBase) |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2824 | { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2825 | const ConfigDescription nullConfig; |
| 2826 | |
| 2827 | const size_t N = mOrderedPackages.size(); |
| 2828 | size_t pi; |
| 2829 | |
| 2830 | const static String16 mipmap16("mipmap"); |
| 2831 | |
| 2832 | bool useUTF8 = !bundle->getUTF16StringsOption(); |
| 2833 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 2834 | // The libraries this table references. |
| 2835 | Vector<sp<Package> > libraryPackages; |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 2836 | const ResTable& table = mAssets->getIncludedResources(); |
| 2837 | const size_t basePackageCount = table.getBasePackageCount(); |
| 2838 | for (size_t i = 0; i < basePackageCount; i++) { |
| 2839 | size_t packageId = table.getBasePackageId(i); |
| 2840 | String16 packageName(table.getBasePackageName(i)); |
| 2841 | if (packageId > 0x01 && packageId != 0x7f && |
| 2842 | packageName != String16("android")) { |
| 2843 | libraryPackages.add(sp<Package>(new Package(packageName, packageId))); |
| 2844 | } |
| 2845 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 2846 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2847 | // Iterate through all data, collecting all values (strings, |
| 2848 | // references, etc). |
| 2849 | StringPool valueStrings(useUTF8); |
| 2850 | Vector<sp<Entry> > allEntries; |
| 2851 | for (pi=0; pi<N; pi++) { |
| 2852 | sp<Package> p = mOrderedPackages.itemAt(pi); |
| 2853 | if (p->getTypes().size() == 0) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2854 | continue; |
| 2855 | } |
| 2856 | |
| 2857 | StringPool typeStrings(useUTF8); |
| 2858 | StringPool keyStrings(useUTF8); |
| 2859 | |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2860 | ssize_t stringsAdded = 0; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2861 | const size_t N = p->getOrderedTypes().size(); |
| 2862 | for (size_t ti=0; ti<N; ti++) { |
| 2863 | sp<Type> t = p->getOrderedTypes().itemAt(ti); |
| 2864 | if (t == NULL) { |
| 2865 | typeStrings.add(String16("<empty>"), false); |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2866 | stringsAdded++; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2867 | continue; |
| 2868 | } |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2869 | |
| 2870 | while (stringsAdded < t->getIndex() - 1) { |
| 2871 | typeStrings.add(String16("<empty>"), false); |
| 2872 | stringsAdded++; |
| 2873 | } |
| 2874 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2875 | const String16 typeName(t->getName()); |
| 2876 | typeStrings.add(typeName, false); |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2877 | stringsAdded++; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2878 | |
| 2879 | // This is a hack to tweak the sorting order of the final strings, |
| 2880 | // to put stuff that is generally not language-specific first. |
| 2881 | String8 configTypeName(typeName); |
| 2882 | if (configTypeName == "drawable" || configTypeName == "layout" |
| 2883 | || configTypeName == "color" || configTypeName == "anim" |
| 2884 | || configTypeName == "interpolator" || configTypeName == "animator" |
| 2885 | || configTypeName == "xml" || configTypeName == "menu" |
| 2886 | || configTypeName == "mipmap" || configTypeName == "raw") { |
| 2887 | configTypeName = "1complex"; |
| 2888 | } else { |
| 2889 | configTypeName = "2value"; |
| 2890 | } |
| 2891 | |
Adam Lesinski | 27f69f4 | 2014-08-21 13:19:12 -0700 | [diff] [blame] | 2892 | // mipmaps don't get filtered, so they will |
| 2893 | // allways end up in the base. Make sure they |
| 2894 | // don't end up in a split. |
| 2895 | if (typeName == mipmap16 && !isBase) { |
| 2896 | continue; |
| 2897 | } |
| 2898 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2899 | const bool filterable = (typeName != mipmap16); |
| 2900 | |
| 2901 | const size_t N = t->getOrderedConfigs().size(); |
| 2902 | for (size_t ci=0; ci<N; ci++) { |
| 2903 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); |
| 2904 | if (c == NULL) { |
| 2905 | continue; |
| 2906 | } |
| 2907 | const size_t N = c->getEntries().size(); |
| 2908 | for (size_t ei=0; ei<N; ei++) { |
| 2909 | ConfigDescription config = c->getEntries().keyAt(ei); |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 2910 | if (filterable && !filter->match(config)) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2911 | continue; |
| 2912 | } |
| 2913 | sp<Entry> e = c->getEntries().valueAt(ei); |
| 2914 | if (e == NULL) { |
| 2915 | continue; |
| 2916 | } |
| 2917 | e->setNameIndex(keyStrings.add(e->getName(), true)); |
| 2918 | |
| 2919 | // If this entry has no values for other configs, |
| 2920 | // and is the default config, then it is special. Otherwise |
| 2921 | // we want to add it with the config info. |
| 2922 | ConfigDescription* valueConfig = NULL; |
| 2923 | if (N != 1 || config == nullConfig) { |
| 2924 | valueConfig = &config; |
| 2925 | } |
| 2926 | |
| 2927 | status_t err = e->prepareFlatten(&valueStrings, this, |
| 2928 | &configTypeName, &config); |
| 2929 | if (err != NO_ERROR) { |
| 2930 | return err; |
| 2931 | } |
| 2932 | allEntries.add(e); |
| 2933 | } |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | p->setTypeStrings(typeStrings.createStringBlock()); |
| 2938 | p->setKeyStrings(keyStrings.createStringBlock()); |
| 2939 | } |
| 2940 | |
| 2941 | if (bundle->getOutputAPKFile() != NULL) { |
| 2942 | // Now we want to sort the value strings for better locality. This will |
| 2943 | // cause the positions of the strings to change, so we need to go back |
| 2944 | // through out resource entries and update them accordingly. Only need |
| 2945 | // to do this if actually writing the output file. |
| 2946 | valueStrings.sortByConfig(); |
| 2947 | for (pi=0; pi<allEntries.size(); pi++) { |
| 2948 | allEntries[pi]->remapStringValue(&valueStrings); |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | ssize_t strAmt = 0; |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 2953 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2954 | // Now build the array of package chunks. |
| 2955 | Vector<sp<AaptFile> > flatPackages; |
| 2956 | for (pi=0; pi<N; pi++) { |
| 2957 | sp<Package> p = mOrderedPackages.itemAt(pi); |
| 2958 | if (p->getTypes().size() == 0) { |
| 2959 | // Empty, skip! |
| 2960 | continue; |
| 2961 | } |
| 2962 | |
| 2963 | const size_t N = p->getTypeStrings().size(); |
| 2964 | |
| 2965 | const size_t baseSize = sizeof(ResTable_package); |
| 2966 | |
| 2967 | // Start the package data. |
| 2968 | sp<AaptFile> data = new AaptFile(String8(), AaptGroupEntry(), String8()); |
| 2969 | ResTable_package* header = (ResTable_package*)data->editData(baseSize); |
| 2970 | if (header == NULL) { |
| 2971 | fprintf(stderr, "ERROR: out of memory creating ResTable_package\n"); |
| 2972 | return NO_MEMORY; |
| 2973 | } |
| 2974 | memset(header, 0, sizeof(*header)); |
| 2975 | header->header.type = htods(RES_TABLE_PACKAGE_TYPE); |
| 2976 | header->header.headerSize = htods(sizeof(*header)); |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 2977 | header->id = htodl(static_cast<uint32_t>(p->getAssignedId())); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2978 | strcpy16_htod(header->name, p->getName().string()); |
| 2979 | |
| 2980 | // Write the string blocks. |
| 2981 | const size_t typeStringsStart = data->getSize(); |
| 2982 | sp<AaptFile> strFile = p->getTypeStringsData(); |
| 2983 | ssize_t amt = data->writeData(strFile->getData(), strFile->getSize()); |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 2984 | if (kPrintStringMetrics) { |
| 2985 | fprintf(stderr, "**** type strings: %zd\n", SSIZE(amt)); |
| 2986 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2987 | strAmt += amt; |
| 2988 | if (amt < 0) { |
| 2989 | return amt; |
| 2990 | } |
| 2991 | const size_t keyStringsStart = data->getSize(); |
| 2992 | strFile = p->getKeyStringsData(); |
| 2993 | amt = data->writeData(strFile->getData(), strFile->getSize()); |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 2994 | if (kPrintStringMetrics) { |
| 2995 | fprintf(stderr, "**** key strings: %zd\n", SSIZE(amt)); |
| 2996 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 2997 | strAmt += amt; |
| 2998 | if (amt < 0) { |
| 2999 | return amt; |
| 3000 | } |
| 3001 | |
Adam Lesinski | 27f69f4 | 2014-08-21 13:19:12 -0700 | [diff] [blame] | 3002 | if (isBase) { |
| 3003 | status_t err = flattenLibraryTable(data, libraryPackages); |
| 3004 | if (err != NO_ERROR) { |
| 3005 | fprintf(stderr, "ERROR: failed to write library table\n"); |
| 3006 | return err; |
| 3007 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3008 | } |
| 3009 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3010 | // Build the type chunks inside of this package. |
| 3011 | for (size_t ti=0; ti<N; ti++) { |
| 3012 | // Retrieve them in the same order as the type string block. |
| 3013 | size_t len; |
| 3014 | String16 typeName(p->getTypeStrings().stringAt(ti, &len)); |
| 3015 | sp<Type> t = p->getTypes().valueFor(typeName); |
| 3016 | LOG_ALWAYS_FATAL_IF(t == NULL && typeName != String16("<empty>"), |
| 3017 | "Type name %s not found", |
| 3018 | String8(typeName).string()); |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 3019 | if (t == NULL) { |
| 3020 | continue; |
| 3021 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3022 | const bool filterable = (typeName != mipmap16); |
Adam Lesinski | 27f69f4 | 2014-08-21 13:19:12 -0700 | [diff] [blame] | 3023 | const bool skipEntireType = (typeName == mipmap16 && !isBase); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3024 | |
| 3025 | const size_t N = t != NULL ? t->getOrderedConfigs().size() : 0; |
| 3026 | |
| 3027 | // Until a non-NO_ENTRY value has been written for a resource, |
| 3028 | // that resource is invalid; validResources[i] represents |
| 3029 | // the item at t->getOrderedConfigs().itemAt(i). |
| 3030 | Vector<bool> validResources; |
| 3031 | validResources.insertAt(false, 0, N); |
| 3032 | |
| 3033 | // First write the typeSpec chunk, containing information about |
| 3034 | // each resource entry in this type. |
| 3035 | { |
| 3036 | const size_t typeSpecSize = sizeof(ResTable_typeSpec) + sizeof(uint32_t)*N; |
| 3037 | const size_t typeSpecStart = data->getSize(); |
| 3038 | ResTable_typeSpec* tsHeader = (ResTable_typeSpec*) |
| 3039 | (((uint8_t*)data->editData(typeSpecStart+typeSpecSize)) + typeSpecStart); |
| 3040 | if (tsHeader == NULL) { |
| 3041 | fprintf(stderr, "ERROR: out of memory creating ResTable_typeSpec\n"); |
| 3042 | return NO_MEMORY; |
| 3043 | } |
| 3044 | memset(tsHeader, 0, sizeof(*tsHeader)); |
| 3045 | tsHeader->header.type = htods(RES_TABLE_TYPE_SPEC_TYPE); |
| 3046 | tsHeader->header.headerSize = htods(sizeof(*tsHeader)); |
| 3047 | tsHeader->header.size = htodl(typeSpecSize); |
| 3048 | tsHeader->id = ti+1; |
| 3049 | tsHeader->entryCount = htodl(N); |
| 3050 | |
| 3051 | uint32_t* typeSpecFlags = (uint32_t*) |
| 3052 | (((uint8_t*)data->editData()) |
| 3053 | + typeSpecStart + sizeof(ResTable_typeSpec)); |
| 3054 | memset(typeSpecFlags, 0, sizeof(uint32_t)*N); |
| 3055 | |
| 3056 | for (size_t ei=0; ei<N; ei++) { |
| 3057 | sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei); |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 3058 | if (cl == NULL) { |
| 3059 | continue; |
| 3060 | } |
| 3061 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3062 | if (cl->getPublic()) { |
| 3063 | typeSpecFlags[ei] |= htodl(ResTable_typeSpec::SPEC_PUBLIC); |
| 3064 | } |
Adam Lesinski | 27f69f4 | 2014-08-21 13:19:12 -0700 | [diff] [blame] | 3065 | |
| 3066 | if (skipEntireType) { |
| 3067 | continue; |
| 3068 | } |
| 3069 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3070 | const size_t CN = cl->getEntries().size(); |
| 3071 | for (size_t ci=0; ci<CN; ci++) { |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3072 | if (filterable && !filter->match(cl->getEntries().keyAt(ci))) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3073 | continue; |
| 3074 | } |
| 3075 | for (size_t cj=ci+1; cj<CN; cj++) { |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3076 | if (filterable && !filter->match(cl->getEntries().keyAt(cj))) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3077 | continue; |
| 3078 | } |
| 3079 | typeSpecFlags[ei] |= htodl( |
| 3080 | cl->getEntries().keyAt(ci).diff(cl->getEntries().keyAt(cj))); |
| 3081 | } |
| 3082 | } |
| 3083 | } |
| 3084 | } |
| 3085 | |
Adam Lesinski | 27f69f4 | 2014-08-21 13:19:12 -0700 | [diff] [blame] | 3086 | if (skipEntireType) { |
| 3087 | continue; |
| 3088 | } |
| 3089 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3090 | // We need to write one type chunk for each configuration for |
| 3091 | // which we have entries in this type. |
Adam Lesinski | e97908d | 2014-12-05 11:06:21 -0800 | [diff] [blame] | 3092 | SortedVector<ConfigDescription> uniqueConfigs; |
| 3093 | if (t != NULL) { |
| 3094 | uniqueConfigs = t->getUniqueConfigs(); |
| 3095 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3096 | |
| 3097 | const size_t typeSize = sizeof(ResTable_type) + sizeof(uint32_t)*N; |
| 3098 | |
Adam Lesinski | e97908d | 2014-12-05 11:06:21 -0800 | [diff] [blame] | 3099 | const size_t NC = uniqueConfigs.size(); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3100 | for (size_t ci=0; ci<NC; ci++) { |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 3101 | const ConfigDescription& config = uniqueConfigs[ci]; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3102 | |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3103 | if (kIsDebug) { |
| 3104 | printf("Writing config %zu config: imsi:%d/%d lang:%c%c cnt:%c%c " |
| 3105 | "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d " |
| 3106 | "sw%ddp w%ddp h%ddp layout:%d\n", |
| 3107 | ti + 1, |
| 3108 | config.mcc, config.mnc, |
| 3109 | config.language[0] ? config.language[0] : '-', |
| 3110 | config.language[1] ? config.language[1] : '-', |
| 3111 | config.country[0] ? config.country[0] : '-', |
| 3112 | config.country[1] ? config.country[1] : '-', |
| 3113 | config.orientation, |
| 3114 | config.uiMode, |
| 3115 | config.touchscreen, |
| 3116 | config.density, |
| 3117 | config.keyboard, |
| 3118 | config.inputFlags, |
| 3119 | config.navigation, |
| 3120 | config.screenWidth, |
| 3121 | config.screenHeight, |
| 3122 | config.smallestScreenWidthDp, |
| 3123 | config.screenWidthDp, |
| 3124 | config.screenHeightDp, |
| 3125 | config.screenLayout); |
| 3126 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3127 | |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3128 | if (filterable && !filter->match(config)) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3129 | continue; |
| 3130 | } |
| 3131 | |
| 3132 | const size_t typeStart = data->getSize(); |
| 3133 | |
| 3134 | ResTable_type* tHeader = (ResTable_type*) |
| 3135 | (((uint8_t*)data->editData(typeStart+typeSize)) + typeStart); |
| 3136 | if (tHeader == NULL) { |
| 3137 | fprintf(stderr, "ERROR: out of memory creating ResTable_type\n"); |
| 3138 | return NO_MEMORY; |
| 3139 | } |
| 3140 | |
| 3141 | memset(tHeader, 0, sizeof(*tHeader)); |
| 3142 | tHeader->header.type = htods(RES_TABLE_TYPE_TYPE); |
| 3143 | tHeader->header.headerSize = htods(sizeof(*tHeader)); |
| 3144 | tHeader->id = ti+1; |
| 3145 | tHeader->entryCount = htodl(N); |
| 3146 | tHeader->entriesStart = htodl(typeSize); |
| 3147 | tHeader->config = config; |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3148 | if (kIsDebug) { |
| 3149 | printf("Writing type %zu config: imsi:%d/%d lang:%c%c cnt:%c%c " |
| 3150 | "orien:%d ui:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d " |
| 3151 | "sw%ddp w%ddp h%ddp layout:%d\n", |
| 3152 | ti + 1, |
| 3153 | tHeader->config.mcc, tHeader->config.mnc, |
| 3154 | tHeader->config.language[0] ? tHeader->config.language[0] : '-', |
| 3155 | tHeader->config.language[1] ? tHeader->config.language[1] : '-', |
| 3156 | tHeader->config.country[0] ? tHeader->config.country[0] : '-', |
| 3157 | tHeader->config.country[1] ? tHeader->config.country[1] : '-', |
| 3158 | tHeader->config.orientation, |
| 3159 | tHeader->config.uiMode, |
| 3160 | tHeader->config.touchscreen, |
| 3161 | tHeader->config.density, |
| 3162 | tHeader->config.keyboard, |
| 3163 | tHeader->config.inputFlags, |
| 3164 | tHeader->config.navigation, |
| 3165 | tHeader->config.screenWidth, |
| 3166 | tHeader->config.screenHeight, |
| 3167 | tHeader->config.smallestScreenWidthDp, |
| 3168 | tHeader->config.screenWidthDp, |
| 3169 | tHeader->config.screenHeightDp, |
| 3170 | tHeader->config.screenLayout); |
| 3171 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3172 | tHeader->config.swapHtoD(); |
| 3173 | |
| 3174 | // Build the entries inside of this type. |
| 3175 | for (size_t ei=0; ei<N; ei++) { |
| 3176 | sp<ConfigList> cl = t->getOrderedConfigs().itemAt(ei); |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 3177 | sp<Entry> e = NULL; |
| 3178 | if (cl != NULL) { |
| 3179 | e = cl->getEntries().valueFor(config); |
| 3180 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3181 | |
| 3182 | // Set the offset for this entry in its type. |
| 3183 | uint32_t* index = (uint32_t*) |
| 3184 | (((uint8_t*)data->editData()) |
| 3185 | + typeStart + sizeof(ResTable_type)); |
| 3186 | if (e != NULL) { |
| 3187 | index[ei] = htodl(data->getSize()-typeStart-typeSize); |
| 3188 | |
| 3189 | // Create the entry. |
| 3190 | ssize_t amt = e->flatten(bundle, data, cl->getPublic()); |
| 3191 | if (amt < 0) { |
| 3192 | return amt; |
| 3193 | } |
| 3194 | validResources.editItemAt(ei) = true; |
| 3195 | } else { |
| 3196 | index[ei] = htodl(ResTable_type::NO_ENTRY); |
| 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | // Fill in the rest of the type information. |
| 3201 | tHeader = (ResTable_type*) |
| 3202 | (((uint8_t*)data->editData()) + typeStart); |
| 3203 | tHeader->header.size = htodl(data->getSize()-typeStart); |
| 3204 | } |
| 3205 | |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 3206 | // If we're building splits, then each invocation of the flattening |
| 3207 | // step will have 'missing' entries. Don't warn/error for this case. |
| 3208 | if (bundle->getSplitConfigurations().isEmpty()) { |
| 3209 | bool missing_entry = false; |
| 3210 | const char* log_prefix = bundle->getErrorOnMissingConfigEntry() ? |
| 3211 | "error" : "warning"; |
| 3212 | for (size_t i = 0; i < N; ++i) { |
| 3213 | if (!validResources[i]) { |
| 3214 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(i); |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 3215 | if (c != NULL) { |
Colin Cross | 01f1856 | 2015-04-08 17:29:00 -0700 | [diff] [blame] | 3216 | fprintf(stderr, "%s: no entries written for %s/%s (0x%08zx)\n", log_prefix, |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 3217 | String8(typeName).string(), String8(c->getName()).string(), |
| 3218 | Res_MAKEID(p->getAssignedId() - 1, ti, i)); |
| 3219 | } |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 3220 | missing_entry = true; |
| 3221 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3222 | } |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 3223 | if (bundle->getErrorOnMissingConfigEntry() && missing_entry) { |
| 3224 | fprintf(stderr, "Error: Missing entries, quit!\n"); |
| 3225 | return NOT_ENOUGH_DATA; |
| 3226 | } |
Ying Wang | cd28bd3 | 2013-11-14 17:12:10 -0800 | [diff] [blame] | 3227 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | // Fill in the rest of the package information. |
| 3231 | header = (ResTable_package*)data->editData(); |
| 3232 | header->header.size = htodl(data->getSize()); |
| 3233 | header->typeStrings = htodl(typeStringsStart); |
| 3234 | header->lastPublicType = htodl(p->getTypeStrings().size()); |
| 3235 | header->keyStrings = htodl(keyStringsStart); |
| 3236 | header->lastPublicKey = htodl(p->getKeyStrings().size()); |
| 3237 | |
| 3238 | flatPackages.add(data); |
| 3239 | } |
| 3240 | |
| 3241 | // And now write out the final chunks. |
| 3242 | const size_t dataStart = dest->getSize(); |
| 3243 | |
| 3244 | { |
| 3245 | // blah |
| 3246 | ResTable_header header; |
| 3247 | memset(&header, 0, sizeof(header)); |
| 3248 | header.header.type = htods(RES_TABLE_TYPE); |
| 3249 | header.header.headerSize = htods(sizeof(header)); |
| 3250 | header.packageCount = htodl(flatPackages.size()); |
| 3251 | status_t err = dest->writeData(&header, sizeof(header)); |
| 3252 | if (err != NO_ERROR) { |
| 3253 | fprintf(stderr, "ERROR: out of memory creating ResTable_header\n"); |
| 3254 | return err; |
| 3255 | } |
| 3256 | } |
| 3257 | |
| 3258 | ssize_t strStart = dest->getSize(); |
Adam Lesinski | fab5087 | 2014-04-16 14:40:42 -0700 | [diff] [blame] | 3259 | status_t err = valueStrings.writeStringBlock(dest); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3260 | if (err != NO_ERROR) { |
| 3261 | return err; |
| 3262 | } |
| 3263 | |
| 3264 | ssize_t amt = (dest->getSize()-strStart); |
| 3265 | strAmt += amt; |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3266 | if (kPrintStringMetrics) { |
| 3267 | fprintf(stderr, "**** value strings: %zd\n", SSIZE(amt)); |
| 3268 | fprintf(stderr, "**** total strings: %zd\n", SSIZE(strAmt)); |
| 3269 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3270 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3271 | for (pi=0; pi<flatPackages.size(); pi++) { |
| 3272 | err = dest->writeData(flatPackages[pi]->getData(), |
| 3273 | flatPackages[pi]->getSize()); |
| 3274 | if (err != NO_ERROR) { |
| 3275 | fprintf(stderr, "ERROR: out of memory creating package chunk for ResTable_header\n"); |
| 3276 | return err; |
| 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | ResTable_header* header = (ResTable_header*) |
| 3281 | (((uint8_t*)dest->getData()) + dataStart); |
| 3282 | header->header.size = htodl(dest->getSize() - dataStart); |
| 3283 | |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3284 | if (kPrintStringMetrics) { |
| 3285 | fprintf(stderr, "**** total resource table size: %zu / %zu%% strings\n", |
| 3286 | dest->getSize(), (size_t)(strAmt*100)/dest->getSize()); |
| 3287 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3288 | |
| 3289 | return NO_ERROR; |
| 3290 | } |
| 3291 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3292 | status_t ResourceTable::flattenLibraryTable(const sp<AaptFile>& dest, const Vector<sp<Package> >& libs) { |
| 3293 | // Write out the library table if necessary |
| 3294 | if (libs.size() > 0) { |
Andreas Gampe | 87332a7 | 2014-10-01 22:03:58 -0700 | [diff] [blame] | 3295 | if (kIsDebug) { |
| 3296 | fprintf(stderr, "Writing library reference table\n"); |
| 3297 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3298 | |
| 3299 | const size_t libStart = dest->getSize(); |
| 3300 | const size_t count = libs.size(); |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 3301 | ResTable_lib_header* libHeader = (ResTable_lib_header*) dest->editDataInRange( |
| 3302 | libStart, sizeof(ResTable_lib_header)); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3303 | |
| 3304 | memset(libHeader, 0, sizeof(*libHeader)); |
| 3305 | libHeader->header.type = htods(RES_TABLE_LIBRARY_TYPE); |
| 3306 | libHeader->header.headerSize = htods(sizeof(*libHeader)); |
| 3307 | libHeader->header.size = htodl(sizeof(*libHeader) + (sizeof(ResTable_lib_entry) * count)); |
| 3308 | libHeader->count = htodl(count); |
| 3309 | |
| 3310 | // Write the library entries |
| 3311 | for (size_t i = 0; i < count; i++) { |
| 3312 | const size_t entryStart = dest->getSize(); |
| 3313 | sp<Package> libPackage = libs[i]; |
Andreas Gampe | 87332a7 | 2014-10-01 22:03:58 -0700 | [diff] [blame] | 3314 | if (kIsDebug) { |
| 3315 | fprintf(stderr, " Entry %s -> 0x%02x\n", |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3316 | String8(libPackage->getName()).string(), |
Andreas Gampe | 87332a7 | 2014-10-01 22:03:58 -0700 | [diff] [blame] | 3317 | (uint8_t)libPackage->getAssignedId()); |
| 3318 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3319 | |
Adam Lesinski | 6022deb | 2014-08-20 14:59:19 -0700 | [diff] [blame] | 3320 | ResTable_lib_entry* entry = (ResTable_lib_entry*) dest->editDataInRange( |
| 3321 | entryStart, sizeof(ResTable_lib_entry)); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 3322 | memset(entry, 0, sizeof(*entry)); |
| 3323 | entry->packageId = htodl(libPackage->getAssignedId()); |
| 3324 | strcpy16_htod(entry->packageName, libPackage->getName().string()); |
| 3325 | } |
| 3326 | } |
| 3327 | return NO_ERROR; |
| 3328 | } |
| 3329 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3330 | void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp) |
| 3331 | { |
| 3332 | fprintf(fp, |
| 3333 | "<!-- This file contains <public> resource definitions for all\n" |
| 3334 | " resources that were generated from the source data. -->\n" |
| 3335 | "\n" |
| 3336 | "<resources>\n"); |
| 3337 | |
| 3338 | writePublicDefinitions(package, fp, true); |
| 3339 | writePublicDefinitions(package, fp, false); |
| 3340 | |
| 3341 | fprintf(fp, |
| 3342 | "\n" |
| 3343 | "</resources>\n"); |
| 3344 | } |
| 3345 | |
| 3346 | void ResourceTable::writePublicDefinitions(const String16& package, FILE* fp, bool pub) |
| 3347 | { |
| 3348 | bool didHeader = false; |
| 3349 | |
| 3350 | sp<Package> pkg = mPackages.valueFor(package); |
| 3351 | if (pkg != NULL) { |
| 3352 | const size_t NT = pkg->getOrderedTypes().size(); |
| 3353 | for (size_t i=0; i<NT; i++) { |
| 3354 | sp<Type> t = pkg->getOrderedTypes().itemAt(i); |
| 3355 | if (t == NULL) { |
| 3356 | continue; |
| 3357 | } |
| 3358 | |
| 3359 | bool didType = false; |
| 3360 | |
| 3361 | const size_t NC = t->getOrderedConfigs().size(); |
| 3362 | for (size_t j=0; j<NC; j++) { |
| 3363 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(j); |
| 3364 | if (c == NULL) { |
| 3365 | continue; |
| 3366 | } |
| 3367 | |
| 3368 | if (c->getPublic() != pub) { |
| 3369 | continue; |
| 3370 | } |
| 3371 | |
| 3372 | if (!didType) { |
| 3373 | fprintf(fp, "\n"); |
| 3374 | didType = true; |
| 3375 | } |
| 3376 | if (!didHeader) { |
| 3377 | if (pub) { |
| 3378 | fprintf(fp," <!-- PUBLIC SECTION. These resources have been declared public.\n"); |
| 3379 | fprintf(fp," Changes to these definitions will break binary compatibility. -->\n\n"); |
| 3380 | } else { |
| 3381 | fprintf(fp," <!-- PRIVATE SECTION. These resources have not been declared public.\n"); |
| 3382 | fprintf(fp," You can make them public my moving these lines into a file in res/values. -->\n\n"); |
| 3383 | } |
| 3384 | didHeader = true; |
| 3385 | } |
| 3386 | if (!pub) { |
| 3387 | const size_t NE = c->getEntries().size(); |
| 3388 | for (size_t k=0; k<NE; k++) { |
| 3389 | const SourcePos& pos = c->getEntries().valueAt(k)->getPos(); |
| 3390 | if (pos.file != "") { |
| 3391 | fprintf(fp," <!-- Declared at %s:%d -->\n", |
| 3392 | pos.file.string(), pos.line); |
| 3393 | } |
| 3394 | } |
| 3395 | } |
| 3396 | fprintf(fp, " <public type=\"%s\" name=\"%s\" id=\"0x%08x\" />\n", |
| 3397 | String8(t->getName()).string(), |
| 3398 | String8(c->getName()).string(), |
| 3399 | getResId(pkg, t, c->getEntryIndex())); |
| 3400 | } |
| 3401 | } |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | ResourceTable::Item::Item(const SourcePos& _sourcePos, |
| 3406 | bool _isId, |
| 3407 | const String16& _value, |
| 3408 | const Vector<StringPool::entry_style_span>* _style, |
| 3409 | int32_t _format) |
| 3410 | : sourcePos(_sourcePos) |
| 3411 | , isId(_isId) |
| 3412 | , value(_value) |
| 3413 | , format(_format) |
| 3414 | , bagKeyId(0) |
| 3415 | , evaluating(false) |
| 3416 | { |
| 3417 | if (_style) { |
| 3418 | style = *_style; |
| 3419 | } |
| 3420 | } |
| 3421 | |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 3422 | ResourceTable::Entry::Entry(const Entry& entry) |
| 3423 | : RefBase() |
| 3424 | , mName(entry.mName) |
| 3425 | , mParent(entry.mParent) |
| 3426 | , mType(entry.mType) |
| 3427 | , mItem(entry.mItem) |
| 3428 | , mItemFormat(entry.mItemFormat) |
| 3429 | , mBag(entry.mBag) |
| 3430 | , mNameIndex(entry.mNameIndex) |
| 3431 | , mParentId(entry.mParentId) |
| 3432 | , mPos(entry.mPos) {} |
| 3433 | |
Adam Lesinski | 978ab9d | 2014-09-24 19:02:52 -0700 | [diff] [blame] | 3434 | ResourceTable::Entry& ResourceTable::Entry::operator=(const Entry& entry) { |
| 3435 | mName = entry.mName; |
| 3436 | mParent = entry.mParent; |
| 3437 | mType = entry.mType; |
| 3438 | mItem = entry.mItem; |
| 3439 | mItemFormat = entry.mItemFormat; |
| 3440 | mBag = entry.mBag; |
| 3441 | mNameIndex = entry.mNameIndex; |
| 3442 | mParentId = entry.mParentId; |
| 3443 | mPos = entry.mPos; |
| 3444 | return *this; |
| 3445 | } |
| 3446 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3447 | status_t ResourceTable::Entry::makeItABag(const SourcePos& sourcePos) |
| 3448 | { |
| 3449 | if (mType == TYPE_BAG) { |
| 3450 | return NO_ERROR; |
| 3451 | } |
| 3452 | if (mType == TYPE_UNKNOWN) { |
| 3453 | mType = TYPE_BAG; |
| 3454 | return NO_ERROR; |
| 3455 | } |
| 3456 | sourcePos.error("Resource entry %s is already defined as a single item.\n" |
| 3457 | "%s:%d: Originally defined here.\n", |
| 3458 | String8(mName).string(), |
| 3459 | mItem.sourcePos.file.string(), mItem.sourcePos.line); |
| 3460 | return UNKNOWN_ERROR; |
| 3461 | } |
| 3462 | |
| 3463 | status_t ResourceTable::Entry::setItem(const SourcePos& sourcePos, |
| 3464 | const String16& value, |
| 3465 | const Vector<StringPool::entry_style_span>* style, |
| 3466 | int32_t format, |
| 3467 | const bool overwrite) |
| 3468 | { |
| 3469 | Item item(sourcePos, false, value, style); |
| 3470 | |
| 3471 | if (mType == TYPE_BAG) { |
Adam Lesinski | 43a0df0 | 2014-08-18 17:14:57 -0700 | [diff] [blame] | 3472 | if (mBag.size() == 0) { |
| 3473 | sourcePos.error("Resource entry %s is already defined as a bag.", |
| 3474 | String8(mName).string()); |
| 3475 | } else { |
| 3476 | const Item& item(mBag.valueAt(0)); |
| 3477 | sourcePos.error("Resource entry %s is already defined as a bag.\n" |
| 3478 | "%s:%d: Originally defined here.\n", |
| 3479 | String8(mName).string(), |
| 3480 | item.sourcePos.file.string(), item.sourcePos.line); |
| 3481 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3482 | return UNKNOWN_ERROR; |
| 3483 | } |
| 3484 | if ( (mType != TYPE_UNKNOWN) && (overwrite == false) ) { |
| 3485 | sourcePos.error("Resource entry %s is already defined.\n" |
| 3486 | "%s:%d: Originally defined here.\n", |
| 3487 | String8(mName).string(), |
| 3488 | mItem.sourcePos.file.string(), mItem.sourcePos.line); |
| 3489 | return UNKNOWN_ERROR; |
| 3490 | } |
| 3491 | |
| 3492 | mType = TYPE_ITEM; |
| 3493 | mItem = item; |
| 3494 | mItemFormat = format; |
| 3495 | return NO_ERROR; |
| 3496 | } |
| 3497 | |
| 3498 | status_t ResourceTable::Entry::addToBag(const SourcePos& sourcePos, |
| 3499 | const String16& key, const String16& value, |
| 3500 | const Vector<StringPool::entry_style_span>* style, |
| 3501 | bool replace, bool isId, int32_t format) |
| 3502 | { |
| 3503 | status_t err = makeItABag(sourcePos); |
| 3504 | if (err != NO_ERROR) { |
| 3505 | return err; |
| 3506 | } |
| 3507 | |
| 3508 | Item item(sourcePos, isId, value, style, format); |
| 3509 | |
| 3510 | // XXX NOTE: there is an error if you try to have a bag with two keys, |
| 3511 | // one an attr and one an id, with the same name. Not something we |
| 3512 | // currently ever have to worry about. |
| 3513 | ssize_t origKey = mBag.indexOfKey(key); |
| 3514 | if (origKey >= 0) { |
| 3515 | if (!replace) { |
| 3516 | const Item& item(mBag.valueAt(origKey)); |
| 3517 | sourcePos.error("Resource entry %s already has bag item %s.\n" |
| 3518 | "%s:%d: Originally defined here.\n", |
| 3519 | String8(mName).string(), String8(key).string(), |
| 3520 | item.sourcePos.file.string(), item.sourcePos.line); |
| 3521 | return UNKNOWN_ERROR; |
| 3522 | } |
| 3523 | //printf("Replacing %s with %s\n", |
| 3524 | // String8(mBag.valueFor(key).value).string(), String8(value).string()); |
| 3525 | mBag.replaceValueFor(key, item); |
| 3526 | } |
| 3527 | |
| 3528 | mBag.add(key, item); |
| 3529 | return NO_ERROR; |
| 3530 | } |
| 3531 | |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 3532 | status_t ResourceTable::Entry::removeFromBag(const String16& key) { |
| 3533 | if (mType != Entry::TYPE_BAG) { |
| 3534 | return NO_ERROR; |
| 3535 | } |
| 3536 | |
| 3537 | if (mBag.removeItem(key) >= 0) { |
| 3538 | return NO_ERROR; |
| 3539 | } |
| 3540 | return UNKNOWN_ERROR; |
| 3541 | } |
| 3542 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3543 | status_t ResourceTable::Entry::emptyBag(const SourcePos& sourcePos) |
| 3544 | { |
| 3545 | status_t err = makeItABag(sourcePos); |
| 3546 | if (err != NO_ERROR) { |
| 3547 | return err; |
| 3548 | } |
| 3549 | |
| 3550 | mBag.clear(); |
| 3551 | return NO_ERROR; |
| 3552 | } |
| 3553 | |
| 3554 | status_t ResourceTable::Entry::generateAttributes(ResourceTable* table, |
| 3555 | const String16& package) |
| 3556 | { |
| 3557 | const String16 attr16("attr"); |
| 3558 | const String16 id16("id"); |
| 3559 | const size_t N = mBag.size(); |
| 3560 | for (size_t i=0; i<N; i++) { |
| 3561 | const String16& key = mBag.keyAt(i); |
| 3562 | const Item& it = mBag.valueAt(i); |
| 3563 | if (it.isId) { |
| 3564 | if (!table->hasBagOrEntry(key, &id16, &package)) { |
| 3565 | String16 value("false"); |
Andreas Gampe | 87332a7 | 2014-10-01 22:03:58 -0700 | [diff] [blame] | 3566 | if (kIsDebug) { |
| 3567 | fprintf(stderr, "Generating %s:id/%s\n", |
| 3568 | String8(package).string(), |
| 3569 | String8(key).string()); |
| 3570 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3571 | status_t err = table->addEntry(SourcePos(String8("<generated>"), 0), package, |
| 3572 | id16, key, value); |
| 3573 | if (err != NO_ERROR) { |
| 3574 | return err; |
| 3575 | } |
| 3576 | } |
| 3577 | } else if (!table->hasBagOrEntry(key, &attr16, &package)) { |
| 3578 | |
| 3579 | #if 1 |
| 3580 | // fprintf(stderr, "ERROR: Bag attribute '%s' has not been defined.\n", |
| 3581 | // String8(key).string()); |
| 3582 | // const Item& item(mBag.valueAt(i)); |
| 3583 | // fprintf(stderr, "Referenced from file %s line %d\n", |
| 3584 | // item.sourcePos.file.string(), item.sourcePos.line); |
| 3585 | // return UNKNOWN_ERROR; |
| 3586 | #else |
| 3587 | char numberStr[16]; |
| 3588 | sprintf(numberStr, "%d", ResTable_map::TYPE_ANY); |
| 3589 | status_t err = table->addBag(SourcePos("<generated>", 0), package, |
| 3590 | attr16, key, String16(""), |
| 3591 | String16("^type"), |
| 3592 | String16(numberStr), NULL, NULL); |
| 3593 | if (err != NO_ERROR) { |
| 3594 | return err; |
| 3595 | } |
| 3596 | #endif |
| 3597 | } |
| 3598 | } |
| 3599 | return NO_ERROR; |
| 3600 | } |
| 3601 | |
| 3602 | status_t ResourceTable::Entry::assignResourceIds(ResourceTable* table, |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3603 | const String16& /* package */) |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3604 | { |
| 3605 | bool hasErrors = false; |
| 3606 | |
| 3607 | if (mType == TYPE_BAG) { |
| 3608 | const char* errorMsg; |
| 3609 | const String16 style16("style"); |
| 3610 | const String16 attr16("attr"); |
| 3611 | const String16 id16("id"); |
| 3612 | mParentId = 0; |
| 3613 | if (mParent.size() > 0) { |
| 3614 | mParentId = table->getResId(mParent, &style16, NULL, &errorMsg); |
| 3615 | if (mParentId == 0) { |
| 3616 | mPos.error("Error retrieving parent for item: %s '%s'.\n", |
| 3617 | errorMsg, String8(mParent).string()); |
| 3618 | hasErrors = true; |
| 3619 | } |
| 3620 | } |
| 3621 | const size_t N = mBag.size(); |
| 3622 | for (size_t i=0; i<N; i++) { |
| 3623 | const String16& key = mBag.keyAt(i); |
| 3624 | Item& it = mBag.editValueAt(i); |
| 3625 | it.bagKeyId = table->getResId(key, |
| 3626 | it.isId ? &id16 : &attr16, NULL, &errorMsg); |
| 3627 | //printf("Bag key of %s: #%08x\n", String8(key).string(), it.bagKeyId); |
| 3628 | if (it.bagKeyId == 0) { |
| 3629 | it.sourcePos.error("Error: %s: %s '%s'.\n", errorMsg, |
| 3630 | String8(it.isId ? id16 : attr16).string(), |
| 3631 | String8(key).string()); |
| 3632 | hasErrors = true; |
| 3633 | } |
| 3634 | } |
| 3635 | } |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3636 | return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | status_t ResourceTable::Entry::prepareFlatten(StringPool* strings, ResourceTable* table, |
| 3640 | const String8* configTypeName, const ConfigDescription* config) |
| 3641 | { |
| 3642 | if (mType == TYPE_ITEM) { |
| 3643 | Item& it = mItem; |
| 3644 | AccessorCookie ac(it.sourcePos, String8(mName), String8(it.value)); |
| 3645 | if (!table->stringToValue(&it.parsedValue, strings, |
| 3646 | it.value, false, true, 0, |
| 3647 | &it.style, NULL, &ac, mItemFormat, |
| 3648 | configTypeName, config)) { |
| 3649 | return UNKNOWN_ERROR; |
| 3650 | } |
| 3651 | } else if (mType == TYPE_BAG) { |
| 3652 | const size_t N = mBag.size(); |
| 3653 | for (size_t i=0; i<N; i++) { |
| 3654 | const String16& key = mBag.keyAt(i); |
| 3655 | Item& it = mBag.editValueAt(i); |
| 3656 | AccessorCookie ac(it.sourcePos, String8(key), String8(it.value)); |
| 3657 | if (!table->stringToValue(&it.parsedValue, strings, |
| 3658 | it.value, false, true, it.bagKeyId, |
| 3659 | &it.style, NULL, &ac, it.format, |
| 3660 | configTypeName, config)) { |
| 3661 | return UNKNOWN_ERROR; |
| 3662 | } |
| 3663 | } |
| 3664 | } else { |
| 3665 | mPos.error("Error: entry %s is not a single item or a bag.\n", |
| 3666 | String8(mName).string()); |
| 3667 | return UNKNOWN_ERROR; |
| 3668 | } |
| 3669 | return NO_ERROR; |
| 3670 | } |
| 3671 | |
| 3672 | status_t ResourceTable::Entry::remapStringValue(StringPool* strings) |
| 3673 | { |
| 3674 | if (mType == TYPE_ITEM) { |
| 3675 | Item& it = mItem; |
| 3676 | if (it.parsedValue.dataType == Res_value::TYPE_STRING) { |
| 3677 | it.parsedValue.data = strings->mapOriginalPosToNewPos(it.parsedValue.data); |
| 3678 | } |
| 3679 | } else if (mType == TYPE_BAG) { |
| 3680 | const size_t N = mBag.size(); |
| 3681 | for (size_t i=0; i<N; i++) { |
| 3682 | Item& it = mBag.editValueAt(i); |
| 3683 | if (it.parsedValue.dataType == Res_value::TYPE_STRING) { |
| 3684 | it.parsedValue.data = strings->mapOriginalPosToNewPos(it.parsedValue.data); |
| 3685 | } |
| 3686 | } |
| 3687 | } else { |
| 3688 | mPos.error("Error: entry %s is not a single item or a bag.\n", |
| 3689 | String8(mName).string()); |
| 3690 | return UNKNOWN_ERROR; |
| 3691 | } |
| 3692 | return NO_ERROR; |
| 3693 | } |
| 3694 | |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3695 | ssize_t ResourceTable::Entry::flatten(Bundle* /* bundle */, const sp<AaptFile>& data, bool isPublic) |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3696 | { |
| 3697 | size_t amt = 0; |
| 3698 | ResTable_entry header; |
| 3699 | memset(&header, 0, sizeof(header)); |
| 3700 | header.size = htods(sizeof(header)); |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3701 | const type ty = mType; |
| 3702 | if (ty == TYPE_BAG) { |
| 3703 | header.flags |= htods(header.FLAG_COMPLEX); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3704 | } |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3705 | if (isPublic) { |
| 3706 | header.flags |= htods(header.FLAG_PUBLIC); |
| 3707 | } |
| 3708 | header.key.index = htodl(mNameIndex); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3709 | if (ty != TYPE_BAG) { |
| 3710 | status_t err = data->writeData(&header, sizeof(header)); |
| 3711 | if (err != NO_ERROR) { |
| 3712 | fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n"); |
| 3713 | return err; |
| 3714 | } |
| 3715 | |
| 3716 | const Item& it = mItem; |
| 3717 | Res_value par; |
| 3718 | memset(&par, 0, sizeof(par)); |
| 3719 | par.size = htods(it.parsedValue.size); |
| 3720 | par.dataType = it.parsedValue.dataType; |
| 3721 | par.res0 = it.parsedValue.res0; |
| 3722 | par.data = htodl(it.parsedValue.data); |
| 3723 | #if 0 |
| 3724 | printf("Writing item (%s): type=%d, data=0x%x, res0=0x%x\n", |
| 3725 | String8(mName).string(), it.parsedValue.dataType, |
| 3726 | it.parsedValue.data, par.res0); |
| 3727 | #endif |
| 3728 | err = data->writeData(&par, it.parsedValue.size); |
| 3729 | if (err != NO_ERROR) { |
| 3730 | fprintf(stderr, "ERROR: out of memory creating Res_value\n"); |
| 3731 | return err; |
| 3732 | } |
| 3733 | amt += it.parsedValue.size; |
| 3734 | } else { |
| 3735 | size_t N = mBag.size(); |
| 3736 | size_t i; |
| 3737 | // Create correct ordering of items. |
| 3738 | KeyedVector<uint32_t, const Item*> items; |
| 3739 | for (i=0; i<N; i++) { |
| 3740 | const Item& it = mBag.valueAt(i); |
| 3741 | items.add(it.bagKeyId, &it); |
| 3742 | } |
| 3743 | N = items.size(); |
| 3744 | |
| 3745 | ResTable_map_entry mapHeader; |
| 3746 | memcpy(&mapHeader, &header, sizeof(header)); |
| 3747 | mapHeader.size = htods(sizeof(mapHeader)); |
| 3748 | mapHeader.parent.ident = htodl(mParentId); |
| 3749 | mapHeader.count = htodl(N); |
| 3750 | status_t err = data->writeData(&mapHeader, sizeof(mapHeader)); |
| 3751 | if (err != NO_ERROR) { |
| 3752 | fprintf(stderr, "ERROR: out of memory creating ResTable_entry\n"); |
| 3753 | return err; |
| 3754 | } |
| 3755 | |
| 3756 | for (i=0; i<N; i++) { |
| 3757 | const Item& it = *items.valueAt(i); |
| 3758 | ResTable_map map; |
| 3759 | map.name.ident = htodl(it.bagKeyId); |
| 3760 | map.value.size = htods(it.parsedValue.size); |
| 3761 | map.value.dataType = it.parsedValue.dataType; |
| 3762 | map.value.res0 = it.parsedValue.res0; |
| 3763 | map.value.data = htodl(it.parsedValue.data); |
| 3764 | err = data->writeData(&map, sizeof(map)); |
| 3765 | if (err != NO_ERROR) { |
| 3766 | fprintf(stderr, "ERROR: out of memory creating Res_value\n"); |
| 3767 | return err; |
| 3768 | } |
| 3769 | amt += sizeof(map); |
| 3770 | } |
| 3771 | } |
| 3772 | return amt; |
| 3773 | } |
| 3774 | |
| 3775 | void ResourceTable::ConfigList::appendComment(const String16& comment, |
| 3776 | bool onlyIfEmpty) |
| 3777 | { |
| 3778 | if (comment.size() <= 0) { |
| 3779 | return; |
| 3780 | } |
| 3781 | if (onlyIfEmpty && mComment.size() > 0) { |
| 3782 | return; |
| 3783 | } |
| 3784 | if (mComment.size() > 0) { |
| 3785 | mComment.append(String16("\n")); |
| 3786 | } |
| 3787 | mComment.append(comment); |
| 3788 | } |
| 3789 | |
| 3790 | void ResourceTable::ConfigList::appendTypeComment(const String16& comment) |
| 3791 | { |
| 3792 | if (comment.size() <= 0) { |
| 3793 | return; |
| 3794 | } |
| 3795 | if (mTypeComment.size() > 0) { |
| 3796 | mTypeComment.append(String16("\n")); |
| 3797 | } |
| 3798 | mTypeComment.append(comment); |
| 3799 | } |
| 3800 | |
| 3801 | status_t ResourceTable::Type::addPublic(const SourcePos& sourcePos, |
| 3802 | const String16& name, |
| 3803 | const uint32_t ident) |
| 3804 | { |
| 3805 | #if 0 |
| 3806 | int32_t entryIdx = Res_GETENTRY(ident); |
| 3807 | if (entryIdx < 0) { |
| 3808 | sourcePos.error("Public resource %s/%s has an invalid 0 identifier (0x%08x).\n", |
| 3809 | String8(mName).string(), String8(name).string(), ident); |
| 3810 | return UNKNOWN_ERROR; |
| 3811 | } |
| 3812 | #endif |
| 3813 | |
| 3814 | int32_t typeIdx = Res_GETTYPE(ident); |
| 3815 | if (typeIdx >= 0) { |
| 3816 | typeIdx++; |
| 3817 | if (mPublicIndex > 0 && mPublicIndex != typeIdx) { |
| 3818 | sourcePos.error("Public resource %s/%s has conflicting type codes for its" |
| 3819 | " public identifiers (0x%x vs 0x%x).\n", |
| 3820 | String8(mName).string(), String8(name).string(), |
| 3821 | mPublicIndex, typeIdx); |
| 3822 | return UNKNOWN_ERROR; |
| 3823 | } |
| 3824 | mPublicIndex = typeIdx; |
| 3825 | } |
| 3826 | |
| 3827 | if (mFirstPublicSourcePos == NULL) { |
| 3828 | mFirstPublicSourcePos = new SourcePos(sourcePos); |
| 3829 | } |
| 3830 | |
| 3831 | if (mPublic.indexOfKey(name) < 0) { |
| 3832 | mPublic.add(name, Public(sourcePos, String16(), ident)); |
| 3833 | } else { |
| 3834 | Public& p = mPublic.editValueFor(name); |
| 3835 | if (p.ident != ident) { |
| 3836 | sourcePos.error("Public resource %s/%s has conflicting public identifiers" |
| 3837 | " (0x%08x vs 0x%08x).\n" |
| 3838 | "%s:%d: Originally defined here.\n", |
| 3839 | String8(mName).string(), String8(name).string(), p.ident, ident, |
| 3840 | p.sourcePos.file.string(), p.sourcePos.line); |
| 3841 | return UNKNOWN_ERROR; |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | return NO_ERROR; |
| 3846 | } |
| 3847 | |
| 3848 | void ResourceTable::Type::canAddEntry(const String16& name) |
| 3849 | { |
| 3850 | mCanAddEntries.add(name); |
| 3851 | } |
| 3852 | |
| 3853 | sp<ResourceTable::Entry> ResourceTable::Type::getEntry(const String16& entry, |
| 3854 | const SourcePos& sourcePos, |
| 3855 | const ResTable_config* config, |
| 3856 | bool doSetIndex, |
| 3857 | bool overlay, |
| 3858 | bool autoAddOverlay) |
| 3859 | { |
| 3860 | int pos = -1; |
| 3861 | sp<ConfigList> c = mConfigs.valueFor(entry); |
| 3862 | if (c == NULL) { |
| 3863 | if (overlay && !autoAddOverlay && mCanAddEntries.indexOf(entry) < 0) { |
| 3864 | sourcePos.error("Resource at %s appears in overlay but not" |
| 3865 | " in the base package; use <add-resource> to add.\n", |
| 3866 | String8(entry).string()); |
| 3867 | return NULL; |
| 3868 | } |
| 3869 | c = new ConfigList(entry, sourcePos); |
| 3870 | mConfigs.add(entry, c); |
| 3871 | pos = (int)mOrderedConfigs.size(); |
| 3872 | mOrderedConfigs.add(c); |
| 3873 | if (doSetIndex) { |
| 3874 | c->setEntryIndex(pos); |
| 3875 | } |
| 3876 | } |
| 3877 | |
| 3878 | ConfigDescription cdesc; |
| 3879 | if (config) cdesc = *config; |
| 3880 | |
| 3881 | sp<Entry> e = c->getEntries().valueFor(cdesc); |
| 3882 | if (e == NULL) { |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3883 | if (kIsDebug) { |
| 3884 | if (config != NULL) { |
| 3885 | printf("New entry at %s:%d: imsi:%d/%d lang:%c%c cnt:%c%c " |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3886 | "orien:%d touch:%d density:%d key:%d inp:%d nav:%d sz:%dx%d " |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3887 | "sw%ddp w%ddp h%ddp layout:%d\n", |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3888 | sourcePos.file.string(), sourcePos.line, |
| 3889 | config->mcc, config->mnc, |
| 3890 | config->language[0] ? config->language[0] : '-', |
| 3891 | config->language[1] ? config->language[1] : '-', |
| 3892 | config->country[0] ? config->country[0] : '-', |
| 3893 | config->country[1] ? config->country[1] : '-', |
| 3894 | config->orientation, |
| 3895 | config->touchscreen, |
| 3896 | config->density, |
| 3897 | config->keyboard, |
| 3898 | config->inputFlags, |
| 3899 | config->navigation, |
| 3900 | config->screenWidth, |
| 3901 | config->screenHeight, |
| 3902 | config->smallestScreenWidthDp, |
| 3903 | config->screenWidthDp, |
| 3904 | config->screenHeightDp, |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 3905 | config->screenLayout); |
| 3906 | } else { |
| 3907 | printf("New entry at %s:%d: NULL config\n", |
| 3908 | sourcePos.file.string(), sourcePos.line); |
| 3909 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3910 | } |
| 3911 | e = new Entry(entry, sourcePos); |
| 3912 | c->addEntry(cdesc, e); |
| 3913 | /* |
| 3914 | if (doSetIndex) { |
| 3915 | if (pos < 0) { |
| 3916 | for (pos=0; pos<(int)mOrderedConfigs.size(); pos++) { |
| 3917 | if (mOrderedConfigs[pos] == c) { |
| 3918 | break; |
| 3919 | } |
| 3920 | } |
| 3921 | if (pos >= (int)mOrderedConfigs.size()) { |
| 3922 | sourcePos.error("Internal error: config not found in mOrderedConfigs when adding entry"); |
| 3923 | return NULL; |
| 3924 | } |
| 3925 | } |
| 3926 | e->setEntryIndex(pos); |
| 3927 | } |
| 3928 | */ |
| 3929 | } |
| 3930 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3931 | return e; |
| 3932 | } |
| 3933 | |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 3934 | sp<ResourceTable::ConfigList> ResourceTable::Type::removeEntry(const String16& entry) { |
| 3935 | ssize_t idx = mConfigs.indexOfKey(entry); |
| 3936 | if (idx < 0) { |
| 3937 | return NULL; |
| 3938 | } |
| 3939 | |
| 3940 | sp<ConfigList> removed = mConfigs.valueAt(idx); |
| 3941 | mConfigs.removeItemsAt(idx); |
| 3942 | |
| 3943 | Vector<sp<ConfigList> >::iterator iter = std::find( |
| 3944 | mOrderedConfigs.begin(), mOrderedConfigs.end(), removed); |
| 3945 | if (iter != mOrderedConfigs.end()) { |
| 3946 | mOrderedConfigs.erase(iter); |
| 3947 | } |
| 3948 | |
| 3949 | mPublic.removeItem(entry); |
| 3950 | return removed; |
| 3951 | } |
| 3952 | |
| 3953 | SortedVector<ConfigDescription> ResourceTable::Type::getUniqueConfigs() const { |
| 3954 | SortedVector<ConfigDescription> unique; |
| 3955 | const size_t entryCount = mOrderedConfigs.size(); |
| 3956 | for (size_t i = 0; i < entryCount; i++) { |
| 3957 | if (mOrderedConfigs[i] == NULL) { |
| 3958 | continue; |
| 3959 | } |
| 3960 | const DefaultKeyedVector<ConfigDescription, sp<Entry> >& configs = |
| 3961 | mOrderedConfigs[i]->getEntries(); |
| 3962 | const size_t configCount = configs.size(); |
| 3963 | for (size_t j = 0; j < configCount; j++) { |
| 3964 | unique.add(configs.keyAt(j)); |
| 3965 | } |
| 3966 | } |
| 3967 | return unique; |
| 3968 | } |
| 3969 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3970 | status_t ResourceTable::Type::applyPublicEntryOrder() |
| 3971 | { |
| 3972 | size_t N = mOrderedConfigs.size(); |
| 3973 | Vector<sp<ConfigList> > origOrder(mOrderedConfigs); |
| 3974 | bool hasError = false; |
| 3975 | |
| 3976 | size_t i; |
| 3977 | for (i=0; i<N; i++) { |
| 3978 | mOrderedConfigs.replaceAt(NULL, i); |
| 3979 | } |
| 3980 | |
| 3981 | const size_t NP = mPublic.size(); |
| 3982 | //printf("Ordering %d configs from %d public defs\n", N, NP); |
| 3983 | size_t j; |
| 3984 | for (j=0; j<NP; j++) { |
| 3985 | const String16& name = mPublic.keyAt(j); |
| 3986 | const Public& p = mPublic.valueAt(j); |
| 3987 | int32_t idx = Res_GETENTRY(p.ident); |
| 3988 | //printf("Looking for entry \"%s\"/\"%s\" (0x%08x) in %d...\n", |
| 3989 | // String8(mName).string(), String8(name).string(), p.ident, N); |
| 3990 | bool found = false; |
| 3991 | for (i=0; i<N; i++) { |
| 3992 | sp<ConfigList> e = origOrder.itemAt(i); |
| 3993 | //printf("#%d: \"%s\"\n", i, String8(e->getName()).string()); |
| 3994 | if (e->getName() == name) { |
| 3995 | if (idx >= (int32_t)mOrderedConfigs.size()) { |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 3996 | mOrderedConfigs.resize(idx + 1); |
| 3997 | } |
| 3998 | |
| 3999 | if (mOrderedConfigs.itemAt(idx) == NULL) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4000 | e->setPublic(true); |
| 4001 | e->setPublicSourcePos(p.sourcePos); |
| 4002 | mOrderedConfigs.replaceAt(e, idx); |
| 4003 | origOrder.removeAt(i); |
| 4004 | N--; |
| 4005 | found = true; |
| 4006 | break; |
| 4007 | } else { |
| 4008 | sp<ConfigList> oe = mOrderedConfigs.itemAt(idx); |
| 4009 | |
| 4010 | p.sourcePos.error("Multiple entry names declared for public entry" |
| 4011 | " identifier 0x%x in type %s (%s vs %s).\n" |
| 4012 | "%s:%d: Originally defined here.", |
| 4013 | idx+1, String8(mName).string(), |
| 4014 | String8(oe->getName()).string(), |
| 4015 | String8(name).string(), |
| 4016 | oe->getPublicSourcePos().file.string(), |
| 4017 | oe->getPublicSourcePos().line); |
| 4018 | hasError = true; |
| 4019 | } |
| 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | if (!found) { |
| 4024 | p.sourcePos.error("Public symbol %s/%s declared here is not defined.", |
| 4025 | String8(mName).string(), String8(name).string()); |
| 4026 | hasError = true; |
| 4027 | } |
| 4028 | } |
| 4029 | |
| 4030 | //printf("Copying back in %d non-public configs, have %d\n", N, origOrder.size()); |
| 4031 | |
| 4032 | if (N != origOrder.size()) { |
| 4033 | printf("Internal error: remaining private symbol count mismatch\n"); |
| 4034 | N = origOrder.size(); |
| 4035 | } |
| 4036 | |
| 4037 | j = 0; |
| 4038 | for (i=0; i<N; i++) { |
| 4039 | sp<ConfigList> e = origOrder.itemAt(i); |
| 4040 | // There will always be enough room for the remaining entries. |
| 4041 | while (mOrderedConfigs.itemAt(j) != NULL) { |
| 4042 | j++; |
| 4043 | } |
| 4044 | mOrderedConfigs.replaceAt(e, j); |
| 4045 | j++; |
| 4046 | } |
| 4047 | |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 4048 | return hasError ? STATUST(UNKNOWN_ERROR) : NO_ERROR; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4049 | } |
| 4050 | |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 4051 | ResourceTable::Package::Package(const String16& name, size_t packageId) |
| 4052 | : mName(name), mPackageId(packageId), |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4053 | mTypeStringsMapping(0xffffffff), |
| 4054 | mKeyStringsMapping(0xffffffff) |
| 4055 | { |
| 4056 | } |
| 4057 | |
| 4058 | sp<ResourceTable::Type> ResourceTable::Package::getType(const String16& type, |
| 4059 | const SourcePos& sourcePos, |
| 4060 | bool doSetIndex) |
| 4061 | { |
| 4062 | sp<Type> t = mTypes.valueFor(type); |
| 4063 | if (t == NULL) { |
| 4064 | t = new Type(type, sourcePos); |
| 4065 | mTypes.add(type, t); |
| 4066 | mOrderedTypes.add(t); |
| 4067 | if (doSetIndex) { |
| 4068 | // For some reason the type's index is set to one plus the index |
| 4069 | // in the mOrderedTypes list, rather than just the index. |
| 4070 | t->setIndex(mOrderedTypes.size()); |
| 4071 | } |
| 4072 | } |
| 4073 | return t; |
| 4074 | } |
| 4075 | |
| 4076 | status_t ResourceTable::Package::setTypeStrings(const sp<AaptFile>& data) |
| 4077 | { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4078 | status_t err = setStrings(data, &mTypeStrings, &mTypeStringsMapping); |
| 4079 | if (err != NO_ERROR) { |
| 4080 | fprintf(stderr, "ERROR: Type string data is corrupt!\n"); |
Adam Lesinski | 5707951 | 2014-07-29 11:51:35 -0700 | [diff] [blame] | 4081 | return err; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4082 | } |
Adam Lesinski | 5707951 | 2014-07-29 11:51:35 -0700 | [diff] [blame] | 4083 | |
| 4084 | // Retain a reference to the new data after we've successfully replaced |
| 4085 | // all uses of the old reference (in setStrings() ). |
| 4086 | mTypeStringsData = data; |
| 4087 | return NO_ERROR; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | status_t ResourceTable::Package::setKeyStrings(const sp<AaptFile>& data) |
| 4091 | { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4092 | status_t err = setStrings(data, &mKeyStrings, &mKeyStringsMapping); |
| 4093 | if (err != NO_ERROR) { |
| 4094 | fprintf(stderr, "ERROR: Key string data is corrupt!\n"); |
Adam Lesinski | 5707951 | 2014-07-29 11:51:35 -0700 | [diff] [blame] | 4095 | return err; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4096 | } |
Adam Lesinski | 5707951 | 2014-07-29 11:51:35 -0700 | [diff] [blame] | 4097 | |
| 4098 | // Retain a reference to the new data after we've successfully replaced |
| 4099 | // all uses of the old reference (in setStrings() ). |
| 4100 | mKeyStringsData = data; |
| 4101 | return NO_ERROR; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | status_t ResourceTable::Package::setStrings(const sp<AaptFile>& data, |
| 4105 | ResStringPool* strings, |
| 4106 | DefaultKeyedVector<String16, uint32_t>* mappings) |
| 4107 | { |
| 4108 | if (data->getData() == NULL) { |
| 4109 | return UNKNOWN_ERROR; |
| 4110 | } |
| 4111 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4112 | status_t err = strings->setTo(data->getData(), data->getSize()); |
| 4113 | if (err == NO_ERROR) { |
| 4114 | const size_t N = strings->size(); |
| 4115 | for (size_t i=0; i<N; i++) { |
| 4116 | size_t len; |
| 4117 | mappings->add(String16(strings->stringAt(i, &len)), i); |
| 4118 | } |
| 4119 | } |
| 4120 | return err; |
| 4121 | } |
| 4122 | |
| 4123 | status_t ResourceTable::Package::applyPublicTypeOrder() |
| 4124 | { |
| 4125 | size_t N = mOrderedTypes.size(); |
| 4126 | Vector<sp<Type> > origOrder(mOrderedTypes); |
| 4127 | |
| 4128 | size_t i; |
| 4129 | for (i=0; i<N; i++) { |
| 4130 | mOrderedTypes.replaceAt(NULL, i); |
| 4131 | } |
| 4132 | |
| 4133 | for (i=0; i<N; i++) { |
| 4134 | sp<Type> t = origOrder.itemAt(i); |
| 4135 | int32_t idx = t->getPublicIndex(); |
| 4136 | if (idx > 0) { |
| 4137 | idx--; |
| 4138 | while (idx >= (int32_t)mOrderedTypes.size()) { |
| 4139 | mOrderedTypes.add(); |
| 4140 | } |
| 4141 | if (mOrderedTypes.itemAt(idx) != NULL) { |
| 4142 | sp<Type> ot = mOrderedTypes.itemAt(idx); |
| 4143 | t->getFirstPublicSourcePos().error("Multiple type names declared for public type" |
| 4144 | " identifier 0x%x (%s vs %s).\n" |
| 4145 | "%s:%d: Originally defined here.", |
| 4146 | idx, String8(ot->getName()).string(), |
| 4147 | String8(t->getName()).string(), |
| 4148 | ot->getFirstPublicSourcePos().file.string(), |
| 4149 | ot->getFirstPublicSourcePos().line); |
| 4150 | return UNKNOWN_ERROR; |
| 4151 | } |
| 4152 | mOrderedTypes.replaceAt(t, idx); |
| 4153 | origOrder.removeAt(i); |
| 4154 | i--; |
| 4155 | N--; |
| 4156 | } |
| 4157 | } |
| 4158 | |
| 4159 | size_t j=0; |
| 4160 | for (i=0; i<N; i++) { |
| 4161 | sp<Type> t = origOrder.itemAt(i); |
| 4162 | // There will always be enough room for the remaining types. |
| 4163 | while (mOrderedTypes.itemAt(j) != NULL) { |
| 4164 | j++; |
| 4165 | } |
| 4166 | mOrderedTypes.replaceAt(t, j); |
| 4167 | } |
| 4168 | |
| 4169 | return NO_ERROR; |
| 4170 | } |
| 4171 | |
Adam Lesinski | 9b624c1 | 2014-11-19 17:49:26 -0800 | [diff] [blame] | 4172 | void ResourceTable::Package::movePrivateAttrs() { |
| 4173 | sp<Type> attr = mTypes.valueFor(String16("attr")); |
| 4174 | if (attr == NULL) { |
| 4175 | // Nothing to do. |
| 4176 | return; |
| 4177 | } |
| 4178 | |
| 4179 | Vector<sp<ConfigList> > privateAttrs; |
| 4180 | |
| 4181 | bool hasPublic = false; |
| 4182 | const Vector<sp<ConfigList> >& configs = attr->getOrderedConfigs(); |
| 4183 | const size_t configCount = configs.size(); |
| 4184 | for (size_t i = 0; i < configCount; i++) { |
| 4185 | if (configs[i] == NULL) { |
| 4186 | continue; |
| 4187 | } |
| 4188 | |
| 4189 | if (attr->isPublic(configs[i]->getName())) { |
| 4190 | hasPublic = true; |
| 4191 | } else { |
| 4192 | privateAttrs.add(configs[i]); |
| 4193 | } |
| 4194 | } |
| 4195 | |
| 4196 | // Only if we have public attributes do we create a separate type for |
| 4197 | // private attributes. |
| 4198 | if (!hasPublic) { |
| 4199 | return; |
| 4200 | } |
| 4201 | |
| 4202 | // Create a new type for private attributes. |
| 4203 | sp<Type> privateAttrType = getType(String16(kAttrPrivateType), SourcePos()); |
| 4204 | |
| 4205 | const size_t privateAttrCount = privateAttrs.size(); |
| 4206 | for (size_t i = 0; i < privateAttrCount; i++) { |
| 4207 | const sp<ConfigList>& cl = privateAttrs[i]; |
| 4208 | |
| 4209 | // Remove the private attributes from their current type. |
| 4210 | attr->removeEntry(cl->getName()); |
| 4211 | |
| 4212 | // Add it to the new type. |
| 4213 | const DefaultKeyedVector<ConfigDescription, sp<Entry> >& entries = cl->getEntries(); |
| 4214 | const size_t entryCount = entries.size(); |
| 4215 | for (size_t j = 0; j < entryCount; j++) { |
| 4216 | const sp<Entry>& oldEntry = entries[j]; |
| 4217 | sp<Entry> entry = privateAttrType->getEntry( |
| 4218 | cl->getName(), oldEntry->getPos(), &entries.keyAt(j)); |
| 4219 | *entry = *oldEntry; |
| 4220 | } |
| 4221 | |
| 4222 | // Move the symbols to the new type. |
| 4223 | |
| 4224 | } |
| 4225 | } |
| 4226 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4227 | sp<ResourceTable::Package> ResourceTable::getPackage(const String16& package) |
| 4228 | { |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 4229 | if (package != mAssetsPackage) { |
| 4230 | return NULL; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4231 | } |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 4232 | return mPackages.valueFor(package); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | sp<ResourceTable::Type> ResourceTable::getType(const String16& package, |
| 4236 | const String16& type, |
| 4237 | const SourcePos& sourcePos, |
| 4238 | bool doSetIndex) |
| 4239 | { |
| 4240 | sp<Package> p = getPackage(package); |
| 4241 | if (p == NULL) { |
| 4242 | return NULL; |
| 4243 | } |
| 4244 | return p->getType(type, sourcePos, doSetIndex); |
| 4245 | } |
| 4246 | |
| 4247 | sp<ResourceTable::Entry> ResourceTable::getEntry(const String16& package, |
| 4248 | const String16& type, |
| 4249 | const String16& name, |
| 4250 | const SourcePos& sourcePos, |
| 4251 | bool overlay, |
| 4252 | const ResTable_config* config, |
| 4253 | bool doSetIndex) |
| 4254 | { |
| 4255 | sp<Type> t = getType(package, type, sourcePos, doSetIndex); |
| 4256 | if (t == NULL) { |
| 4257 | return NULL; |
| 4258 | } |
| 4259 | return t->getEntry(name, sourcePos, config, doSetIndex, overlay, mBundle->getAutoAddOverlay()); |
| 4260 | } |
| 4261 | |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4262 | sp<ResourceTable::ConfigList> ResourceTable::getConfigList(const String16& package, |
| 4263 | const String16& type, const String16& name) const |
| 4264 | { |
| 4265 | const size_t packageCount = mOrderedPackages.size(); |
| 4266 | for (size_t pi = 0; pi < packageCount; pi++) { |
| 4267 | const sp<Package>& p = mOrderedPackages[pi]; |
| 4268 | if (p == NULL || p->getName() != package) { |
| 4269 | continue; |
| 4270 | } |
| 4271 | |
| 4272 | const Vector<sp<Type> >& types = p->getOrderedTypes(); |
| 4273 | const size_t typeCount = types.size(); |
| 4274 | for (size_t ti = 0; ti < typeCount; ti++) { |
| 4275 | const sp<Type>& t = types[ti]; |
| 4276 | if (t == NULL || t->getName() != type) { |
| 4277 | continue; |
| 4278 | } |
| 4279 | |
| 4280 | const Vector<sp<ConfigList> >& configs = t->getOrderedConfigs(); |
| 4281 | const size_t configCount = configs.size(); |
| 4282 | for (size_t ci = 0; ci < configCount; ci++) { |
| 4283 | const sp<ConfigList>& cl = configs[ci]; |
| 4284 | if (cl == NULL || cl->getName() != name) { |
| 4285 | continue; |
| 4286 | } |
| 4287 | |
| 4288 | return cl; |
| 4289 | } |
| 4290 | } |
| 4291 | } |
| 4292 | return NULL; |
| 4293 | } |
| 4294 | |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4295 | sp<const ResourceTable::Entry> ResourceTable::getEntry(uint32_t resID, |
| 4296 | const ResTable_config* config) const |
| 4297 | { |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 4298 | size_t pid = Res_GETPACKAGE(resID)+1; |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4299 | const size_t N = mOrderedPackages.size(); |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4300 | sp<Package> p; |
Adam Lesinski | 833f3cc | 2014-06-18 15:06:01 -0700 | [diff] [blame] | 4301 | for (size_t i = 0; i < N; i++) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4302 | sp<Package> check = mOrderedPackages[i]; |
| 4303 | if (check->getAssignedId() == pid) { |
| 4304 | p = check; |
| 4305 | break; |
| 4306 | } |
| 4307 | |
| 4308 | } |
| 4309 | if (p == NULL) { |
| 4310 | fprintf(stderr, "warning: Package not found for resource #%08x\n", resID); |
| 4311 | return NULL; |
| 4312 | } |
| 4313 | |
| 4314 | int tid = Res_GETTYPE(resID); |
| 4315 | if (tid < 0 || tid >= (int)p->getOrderedTypes().size()) { |
| 4316 | fprintf(stderr, "warning: Type not found for resource #%08x\n", resID); |
| 4317 | return NULL; |
| 4318 | } |
| 4319 | sp<Type> t = p->getOrderedTypes()[tid]; |
| 4320 | |
| 4321 | int eid = Res_GETENTRY(resID); |
| 4322 | if (eid < 0 || eid >= (int)t->getOrderedConfigs().size()) { |
| 4323 | fprintf(stderr, "warning: Entry not found for resource #%08x\n", resID); |
| 4324 | return NULL; |
| 4325 | } |
| 4326 | |
| 4327 | sp<ConfigList> c = t->getOrderedConfigs()[eid]; |
| 4328 | if (c == NULL) { |
| 4329 | fprintf(stderr, "warning: Entry not found for resource #%08x\n", resID); |
| 4330 | return NULL; |
| 4331 | } |
| 4332 | |
| 4333 | ConfigDescription cdesc; |
| 4334 | if (config) cdesc = *config; |
| 4335 | sp<Entry> e = c->getEntries().valueFor(cdesc); |
| 4336 | if (c == NULL) { |
| 4337 | fprintf(stderr, "warning: Entry configuration not found for resource #%08x\n", resID); |
| 4338 | return NULL; |
| 4339 | } |
| 4340 | |
| 4341 | return e; |
| 4342 | } |
| 4343 | |
| 4344 | const ResourceTable::Item* ResourceTable::getItem(uint32_t resID, uint32_t attrID) const |
| 4345 | { |
| 4346 | sp<const Entry> e = getEntry(resID); |
| 4347 | if (e == NULL) { |
| 4348 | return NULL; |
| 4349 | } |
| 4350 | |
| 4351 | const size_t N = e->getBag().size(); |
| 4352 | for (size_t i=0; i<N; i++) { |
| 4353 | const Item& it = e->getBag().valueAt(i); |
| 4354 | if (it.bagKeyId == 0) { |
| 4355 | fprintf(stderr, "warning: ID not yet assigned to '%s' in bag '%s'\n", |
| 4356 | String8(e->getName()).string(), |
| 4357 | String8(e->getBag().keyAt(i)).string()); |
| 4358 | } |
| 4359 | if (it.bagKeyId == attrID) { |
| 4360 | return ⁢ |
| 4361 | } |
| 4362 | } |
| 4363 | |
| 4364 | return NULL; |
| 4365 | } |
| 4366 | |
| 4367 | bool ResourceTable::getItemValue( |
| 4368 | uint32_t resID, uint32_t attrID, Res_value* outValue) |
| 4369 | { |
| 4370 | const Item* item = getItem(resID, attrID); |
| 4371 | |
| 4372 | bool res = false; |
| 4373 | if (item != NULL) { |
| 4374 | if (item->evaluating) { |
| 4375 | sp<const Entry> e = getEntry(resID); |
| 4376 | const size_t N = e->getBag().size(); |
| 4377 | size_t i; |
| 4378 | for (i=0; i<N; i++) { |
| 4379 | if (&e->getBag().valueAt(i) == item) { |
| 4380 | break; |
| 4381 | } |
| 4382 | } |
| 4383 | fprintf(stderr, "warning: Circular reference detected in key '%s' of bag '%s'\n", |
| 4384 | String8(e->getName()).string(), |
| 4385 | String8(e->getBag().keyAt(i)).string()); |
| 4386 | return false; |
| 4387 | } |
| 4388 | item->evaluating = true; |
| 4389 | res = stringToValue(outValue, NULL, item->value, false, false, item->bagKeyId); |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 4390 | if (kIsDebug) { |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4391 | if (res) { |
| 4392 | printf("getItemValue of #%08x[#%08x] (%s): type=#%08x, data=#%08x\n", |
| 4393 | resID, attrID, String8(getEntry(resID)->getName()).string(), |
| 4394 | outValue->dataType, outValue->data); |
| 4395 | } else { |
| 4396 | printf("getItemValue of #%08x[#%08x]: failed\n", |
| 4397 | resID, attrID); |
| 4398 | } |
Andreas Gampe | 2412f84 | 2014-09-30 20:55:57 -0700 | [diff] [blame] | 4399 | } |
Adam Lesinski | 282e181 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 4400 | item->evaluating = false; |
| 4401 | } |
| 4402 | return res; |
| 4403 | } |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4404 | |
| 4405 | /** |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4406 | * Returns the SDK version at which the attribute was |
| 4407 | * made public, or -1 if the resource ID is not an attribute |
| 4408 | * or is not public. |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4409 | */ |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4410 | int ResourceTable::getPublicAttributeSdkLevel(uint32_t attrId) const { |
| 4411 | if (Res_GETPACKAGE(attrId) + 1 != 0x01 || Res_GETTYPE(attrId) + 1 != 0x01) { |
| 4412 | return -1; |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | uint32_t specFlags; |
| 4416 | if (!mAssets->getIncludedResources().getResourceFlags(attrId, &specFlags)) { |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4417 | return -1; |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4418 | } |
| 4419 | |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4420 | if ((specFlags & ResTable_typeSpec::SPEC_PUBLIC) == 0) { |
| 4421 | return -1; |
| 4422 | } |
| 4423 | |
| 4424 | const size_t entryId = Res_GETENTRY(attrId); |
| 4425 | if (entryId <= 0x021c) { |
| 4426 | return 1; |
| 4427 | } else if (entryId <= 0x021d) { |
| 4428 | return 2; |
| 4429 | } else if (entryId <= 0x0269) { |
| 4430 | return SDK_CUPCAKE; |
| 4431 | } else if (entryId <= 0x028d) { |
| 4432 | return SDK_DONUT; |
| 4433 | } else if (entryId <= 0x02ad) { |
| 4434 | return SDK_ECLAIR; |
| 4435 | } else if (entryId <= 0x02b3) { |
| 4436 | return SDK_ECLAIR_0_1; |
| 4437 | } else if (entryId <= 0x02b5) { |
| 4438 | return SDK_ECLAIR_MR1; |
| 4439 | } else if (entryId <= 0x02bd) { |
| 4440 | return SDK_FROYO; |
| 4441 | } else if (entryId <= 0x02cb) { |
| 4442 | return SDK_GINGERBREAD; |
| 4443 | } else if (entryId <= 0x0361) { |
| 4444 | return SDK_HONEYCOMB; |
| 4445 | } else if (entryId <= 0x0366) { |
| 4446 | return SDK_HONEYCOMB_MR1; |
| 4447 | } else if (entryId <= 0x03a6) { |
| 4448 | return SDK_HONEYCOMB_MR2; |
| 4449 | } else if (entryId <= 0x03ae) { |
| 4450 | return SDK_JELLY_BEAN; |
| 4451 | } else if (entryId <= 0x03cc) { |
| 4452 | return SDK_JELLY_BEAN_MR1; |
| 4453 | } else if (entryId <= 0x03da) { |
| 4454 | return SDK_JELLY_BEAN_MR2; |
| 4455 | } else if (entryId <= 0x03f1) { |
| 4456 | return SDK_KITKAT; |
| 4457 | } else if (entryId <= 0x03f6) { |
| 4458 | return SDK_KITKAT_WATCH; |
| 4459 | } else if (entryId <= 0x04ce) { |
| 4460 | return SDK_LOLLIPOP; |
| 4461 | } else { |
| 4462 | // Anything else is marked as defined in |
| 4463 | // SDK_LOLLIPOP_MR1 since after this |
| 4464 | // version no attribute compat work |
| 4465 | // needs to be done. |
| 4466 | return SDK_LOLLIPOP_MR1; |
| 4467 | } |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4468 | } |
| 4469 | |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4470 | /** |
| 4471 | * First check the Manifest, then check the command line flag. |
| 4472 | */ |
| 4473 | static int getMinSdkVersion(const Bundle* bundle) { |
| 4474 | if (bundle->getManifestMinSdkVersion() != NULL && strlen(bundle->getManifestMinSdkVersion()) > 0) { |
| 4475 | return atoi(bundle->getManifestMinSdkVersion()); |
| 4476 | } else if (bundle->getMinSdkVersion() != NULL && strlen(bundle->getMinSdkVersion()) > 0) { |
| 4477 | return atoi(bundle->getMinSdkVersion()); |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4478 | } |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4479 | return 0; |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4480 | } |
| 4481 | |
Adam Lesinski | beb9e33 | 2015-08-14 13:16:18 -0700 | [diff] [blame] | 4482 | bool ResourceTable::shouldGenerateVersionedResource( |
| 4483 | const sp<ResourceTable::ConfigList>& configList, |
| 4484 | const ConfigDescription& sourceConfig, |
| 4485 | const int sdkVersionToGenerate) { |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4486 | assert(sdkVersionToGenerate > sourceConfig.sdkVersion); |
| 4487 | const DefaultKeyedVector<ConfigDescription, sp<ResourceTable::Entry>>& entries |
| 4488 | = configList->getEntries(); |
| 4489 | ssize_t idx = entries.indexOfKey(sourceConfig); |
| 4490 | |
| 4491 | // The source config came from this list, so it should be here. |
| 4492 | assert(idx >= 0); |
| 4493 | |
Adam Lesinski | beb9e33 | 2015-08-14 13:16:18 -0700 | [diff] [blame] | 4494 | // The next configuration either only varies in sdkVersion, or it is completely different |
| 4495 | // and therefore incompatible. If it is incompatible, we must generate the versioned resource. |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4496 | |
Adam Lesinski | beb9e33 | 2015-08-14 13:16:18 -0700 | [diff] [blame] | 4497 | // NOTE: The ordering of configurations takes sdkVersion as higher precedence than other |
| 4498 | // qualifiers, so we need to iterate through the entire list to be sure there |
| 4499 | // are no higher sdk level versions of this resource. |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4500 | ConfigDescription tempConfig(sourceConfig); |
Adam Lesinski | beb9e33 | 2015-08-14 13:16:18 -0700 | [diff] [blame] | 4501 | for (size_t i = static_cast<size_t>(idx) + 1; i < entries.size(); i++) { |
| 4502 | const ConfigDescription& nextConfig = entries.keyAt(i); |
| 4503 | tempConfig.sdkVersion = nextConfig.sdkVersion; |
| 4504 | if (tempConfig == nextConfig) { |
| 4505 | // The two configs are the same, check the sdk version. |
| 4506 | return sdkVersionToGenerate < nextConfig.sdkVersion; |
| 4507 | } |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4508 | } |
Adam Lesinski | beb9e33 | 2015-08-14 13:16:18 -0700 | [diff] [blame] | 4509 | |
| 4510 | // No match was found, so we should generate the versioned resource. |
| 4511 | return true; |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4512 | } |
| 4513 | |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4514 | /** |
| 4515 | * Modifies the entries in the resource table to account for compatibility |
| 4516 | * issues with older versions of Android. |
| 4517 | * |
| 4518 | * This primarily handles the issue of private/public attribute clashes |
| 4519 | * in framework resources. |
| 4520 | * |
| 4521 | * AAPT has traditionally assigned resource IDs to public attributes, |
| 4522 | * and then followed those public definitions with private attributes. |
| 4523 | * |
| 4524 | * --- PUBLIC --- |
| 4525 | * | 0x01010234 | attr/color |
| 4526 | * | 0x01010235 | attr/background |
| 4527 | * |
| 4528 | * --- PRIVATE --- |
| 4529 | * | 0x01010236 | attr/secret |
| 4530 | * | 0x01010237 | attr/shhh |
| 4531 | * |
| 4532 | * Each release, when attributes are added, they take the place of the private |
| 4533 | * attributes and the private attributes are shifted down again. |
| 4534 | * |
| 4535 | * --- PUBLIC --- |
| 4536 | * | 0x01010234 | attr/color |
| 4537 | * | 0x01010235 | attr/background |
| 4538 | * | 0x01010236 | attr/shinyNewAttr |
| 4539 | * | 0x01010237 | attr/highlyValuedFeature |
| 4540 | * |
| 4541 | * --- PRIVATE --- |
| 4542 | * | 0x01010238 | attr/secret |
| 4543 | * | 0x01010239 | attr/shhh |
| 4544 | * |
| 4545 | * Platform code may look for private attributes set in a theme. If an app |
| 4546 | * compiled against a newer version of the platform uses a new public |
| 4547 | * attribute that happens to have the same ID as the private attribute |
| 4548 | * the older platform is expecting, then the behavior is undefined. |
| 4549 | * |
| 4550 | * We get around this by detecting any newly defined attributes (in L), |
| 4551 | * copy the resource into a -v21 qualified resource, and delete the |
| 4552 | * attribute from the original resource. This ensures that older platforms |
| 4553 | * don't see the new attribute, but when running on L+ platforms, the |
| 4554 | * attribute will be respected. |
| 4555 | */ |
| 4556 | status_t ResourceTable::modifyForCompat(const Bundle* bundle) { |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4557 | const int minSdk = getMinSdkVersion(bundle); |
| 4558 | if (minSdk >= SDK_LOLLIPOP_MR1) { |
| 4559 | // Lollipop MR1 and up handles public attributes differently, no |
| 4560 | // need to do any compat modifications. |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4561 | return NO_ERROR; |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | const String16 attr16("attr"); |
| 4565 | |
| 4566 | const size_t packageCount = mOrderedPackages.size(); |
| 4567 | for (size_t pi = 0; pi < packageCount; pi++) { |
| 4568 | sp<Package> p = mOrderedPackages.itemAt(pi); |
| 4569 | if (p == NULL || p->getTypes().size() == 0) { |
| 4570 | // Empty, skip! |
| 4571 | continue; |
| 4572 | } |
| 4573 | |
| 4574 | const size_t typeCount = p->getOrderedTypes().size(); |
| 4575 | for (size_t ti = 0; ti < typeCount; ti++) { |
| 4576 | sp<Type> t = p->getOrderedTypes().itemAt(ti); |
| 4577 | if (t == NULL) { |
| 4578 | continue; |
| 4579 | } |
| 4580 | |
| 4581 | const size_t configCount = t->getOrderedConfigs().size(); |
| 4582 | for (size_t ci = 0; ci < configCount; ci++) { |
| 4583 | sp<ConfigList> c = t->getOrderedConfigs().itemAt(ci); |
| 4584 | if (c == NULL) { |
| 4585 | continue; |
| 4586 | } |
| 4587 | |
| 4588 | Vector<key_value_pair_t<ConfigDescription, sp<Entry> > > entriesToAdd; |
| 4589 | const DefaultKeyedVector<ConfigDescription, sp<Entry> >& entries = |
| 4590 | c->getEntries(); |
| 4591 | const size_t entryCount = entries.size(); |
| 4592 | for (size_t ei = 0; ei < entryCount; ei++) { |
| 4593 | sp<Entry> e = entries.valueAt(ei); |
| 4594 | if (e == NULL || e->getType() != Entry::TYPE_BAG) { |
| 4595 | continue; |
| 4596 | } |
| 4597 | |
| 4598 | const ConfigDescription& config = entries.keyAt(ei); |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4599 | if (config.sdkVersion >= SDK_LOLLIPOP_MR1) { |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4600 | continue; |
| 4601 | } |
| 4602 | |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4603 | KeyedVector<int, Vector<String16> > attributesToRemove; |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4604 | const KeyedVector<String16, Item>& bag = e->getBag(); |
| 4605 | const size_t bagCount = bag.size(); |
| 4606 | for (size_t bi = 0; bi < bagCount; bi++) { |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4607 | const uint32_t attrId = getResId(bag.keyAt(bi), &attr16); |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4608 | const int sdkLevel = getPublicAttributeSdkLevel(attrId); |
| 4609 | if (sdkLevel > 1 && sdkLevel > config.sdkVersion && sdkLevel > minSdk) { |
| 4610 | AaptUtil::appendValue(attributesToRemove, sdkLevel, bag.keyAt(bi)); |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4611 | } |
| 4612 | } |
| 4613 | |
| 4614 | if (attributesToRemove.isEmpty()) { |
| 4615 | continue; |
| 4616 | } |
| 4617 | |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4618 | const size_t sdkCount = attributesToRemove.size(); |
| 4619 | for (size_t i = 0; i < sdkCount; i++) { |
| 4620 | const int sdkLevel = attributesToRemove.keyAt(i); |
| 4621 | |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4622 | if (!shouldGenerateVersionedResource(c, config, sdkLevel)) { |
| 4623 | // There is a style that will override this generated one. |
| 4624 | continue; |
| 4625 | } |
| 4626 | |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4627 | // Duplicate the entry under the same configuration |
| 4628 | // but with sdkVersion == sdkLevel. |
| 4629 | ConfigDescription newConfig(config); |
| 4630 | newConfig.sdkVersion = sdkLevel; |
| 4631 | |
| 4632 | sp<Entry> newEntry = new Entry(*e); |
| 4633 | |
| 4634 | // Remove all items that have a higher SDK level than |
| 4635 | // the one we are synthesizing. |
| 4636 | for (size_t j = 0; j < sdkCount; j++) { |
| 4637 | if (j == i) { |
| 4638 | continue; |
| 4639 | } |
| 4640 | |
| 4641 | if (attributesToRemove.keyAt(j) > sdkLevel) { |
| 4642 | const size_t attrCount = attributesToRemove[j].size(); |
| 4643 | for (size_t k = 0; k < attrCount; k++) { |
| 4644 | newEntry->removeFromBag(attributesToRemove[j][k]); |
| 4645 | } |
| 4646 | } |
| 4647 | } |
| 4648 | |
| 4649 | entriesToAdd.add(key_value_pair_t<ConfigDescription, sp<Entry> >( |
| 4650 | newConfig, newEntry)); |
| 4651 | } |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4652 | |
| 4653 | // Remove the attribute from the original. |
| 4654 | for (size_t i = 0; i < attributesToRemove.size(); i++) { |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4655 | for (size_t j = 0; j < attributesToRemove[i].size(); j++) { |
| 4656 | e->removeFromBag(attributesToRemove[i][j]); |
| 4657 | } |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4658 | } |
| 4659 | } |
| 4660 | |
| 4661 | const size_t entriesToAddCount = entriesToAdd.size(); |
| 4662 | for (size_t i = 0; i < entriesToAddCount; i++) { |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4663 | assert(entries.indexOfKey(entriesToAdd[i].key) < 0); |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4664 | |
Adam Lesinski | f15de2e | 2014-10-03 14:57:28 -0700 | [diff] [blame] | 4665 | if (bundle->getVerbose()) { |
| 4666 | entriesToAdd[i].value->getPos() |
| 4667 | .printf("using v%d attributes; synthesizing resource %s:%s/%s for configuration %s.", |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4668 | entriesToAdd[i].key.sdkVersion, |
Adam Lesinski | f15de2e | 2014-10-03 14:57:28 -0700 | [diff] [blame] | 4669 | String8(p->getName()).string(), |
| 4670 | String8(t->getName()).string(), |
| 4671 | String8(entriesToAdd[i].value->getName()).string(), |
| 4672 | entriesToAdd[i].key.toString().string()); |
| 4673 | } |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4674 | |
Adam Lesinski | 978ab9d | 2014-09-24 19:02:52 -0700 | [diff] [blame] | 4675 | sp<Entry> newEntry = t->getEntry(c->getName(), |
| 4676 | entriesToAdd[i].value->getPos(), |
| 4677 | &entriesToAdd[i].key); |
| 4678 | |
| 4679 | *newEntry = *entriesToAdd[i].value; |
Adam Lesinski | 82a2dd8 | 2014-09-17 18:34:15 -0700 | [diff] [blame] | 4680 | } |
| 4681 | } |
| 4682 | } |
| 4683 | } |
| 4684 | return NO_ERROR; |
| 4685 | } |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4686 | |
| 4687 | status_t ResourceTable::modifyForCompat(const Bundle* bundle, |
| 4688 | const String16& resourceName, |
| 4689 | const sp<AaptFile>& target, |
| 4690 | const sp<XMLNode>& root) { |
Adam Lesinski | 6e46056 | 2015-04-21 14:20:15 -0700 | [diff] [blame] | 4691 | const String16 vector16("vector"); |
| 4692 | const String16 animatedVector16("animated-vector"); |
| 4693 | |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4694 | const int minSdk = getMinSdkVersion(bundle); |
| 4695 | if (minSdk >= SDK_LOLLIPOP_MR1) { |
| 4696 | // Lollipop MR1 and up handles public attributes differently, no |
| 4697 | // need to do any compat modifications. |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4698 | return NO_ERROR; |
| 4699 | } |
| 4700 | |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4701 | const ConfigDescription config(target->getGroupEntry().toParams()); |
| 4702 | if (target->getResourceType() == "" || config.sdkVersion >= SDK_LOLLIPOP_MR1) { |
Adam Lesinski | 6e46056 | 2015-04-21 14:20:15 -0700 | [diff] [blame] | 4703 | // Skip resources that have no type (AndroidManifest.xml) or are already version qualified |
| 4704 | // with v21 or higher. |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4705 | return NO_ERROR; |
| 4706 | } |
| 4707 | |
Adam Lesinski | ea4e5ec | 2014-12-10 15:46:51 -0800 | [diff] [blame] | 4708 | sp<XMLNode> newRoot = NULL; |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4709 | int sdkVersionToGenerate = SDK_LOLLIPOP_MR1; |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4710 | |
| 4711 | Vector<sp<XMLNode> > nodesToVisit; |
| 4712 | nodesToVisit.push(root); |
| 4713 | while (!nodesToVisit.isEmpty()) { |
| 4714 | sp<XMLNode> node = nodesToVisit.top(); |
| 4715 | nodesToVisit.pop(); |
| 4716 | |
Adam Lesinski | 6e46056 | 2015-04-21 14:20:15 -0700 | [diff] [blame] | 4717 | if (bundle->getNoVersionVectors() && (node->getElementName() == vector16 || |
| 4718 | node->getElementName() == animatedVector16)) { |
| 4719 | // We were told not to version vector tags, so skip the children here. |
| 4720 | continue; |
| 4721 | } |
| 4722 | |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4723 | const Vector<XMLNode::attribute_entry>& attrs = node->getAttributes(); |
Adam Lesinski | ea4e5ec | 2014-12-10 15:46:51 -0800 | [diff] [blame] | 4724 | for (size_t i = 0; i < attrs.size(); i++) { |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4725 | const XMLNode::attribute_entry& attr = attrs[i]; |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4726 | const int sdkLevel = getPublicAttributeSdkLevel(attr.nameResId); |
| 4727 | if (sdkLevel > 1 && sdkLevel > config.sdkVersion && sdkLevel > minSdk) { |
Adam Lesinski | ea4e5ec | 2014-12-10 15:46:51 -0800 | [diff] [blame] | 4728 | if (newRoot == NULL) { |
| 4729 | newRoot = root->clone(); |
| 4730 | } |
| 4731 | |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4732 | // Find the smallest sdk version that we need to synthesize for |
| 4733 | // and do that one. Subsequent versions will be processed on |
| 4734 | // the next pass. |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4735 | sdkVersionToGenerate = std::min(sdkLevel, sdkVersionToGenerate); |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4736 | |
Adam Lesinski | ea4e5ec | 2014-12-10 15:46:51 -0800 | [diff] [blame] | 4737 | if (bundle->getVerbose()) { |
| 4738 | SourcePos(node->getFilename(), node->getStartLineNumber()).printf( |
| 4739 | "removing attribute %s%s%s from <%s>", |
| 4740 | String8(attr.ns).string(), |
| 4741 | (attr.ns.size() == 0 ? "" : ":"), |
| 4742 | String8(attr.name).string(), |
| 4743 | String8(node->getElementName()).string()); |
| 4744 | } |
| 4745 | node->removeAttribute(i); |
| 4746 | i--; |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4747 | } |
| 4748 | } |
| 4749 | |
| 4750 | // Schedule a visit to the children. |
| 4751 | const Vector<sp<XMLNode> >& children = node->getChildren(); |
| 4752 | const size_t childCount = children.size(); |
| 4753 | for (size_t i = 0; i < childCount; i++) { |
| 4754 | nodesToVisit.push(children[i]); |
| 4755 | } |
| 4756 | } |
| 4757 | |
Adam Lesinski | ea4e5ec | 2014-12-10 15:46:51 -0800 | [diff] [blame] | 4758 | if (newRoot == NULL) { |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4759 | return NO_ERROR; |
| 4760 | } |
| 4761 | |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4762 | // Look to see if we already have an overriding v21 configuration. |
| 4763 | sp<ConfigList> cl = getConfigList(String16(mAssets->getPackage()), |
| 4764 | String16(target->getResourceType()), resourceName); |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4765 | if (shouldGenerateVersionedResource(cl, config, sdkVersionToGenerate)) { |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4766 | // We don't have an overriding entry for v21, so we must duplicate this one. |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4767 | ConfigDescription newConfig(config); |
| 4768 | newConfig.sdkVersion = sdkVersionToGenerate; |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4769 | sp<AaptFile> newFile = new AaptFile(target->getSourceFile(), |
| 4770 | AaptGroupEntry(newConfig), target->getResourceType()); |
Adam Lesinski | 07dfd2d | 2015-10-28 15:44:27 -0700 | [diff] [blame] | 4771 | String8 resPath = String8::format("res/%s/%s.xml", |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4772 | newFile->getGroupEntry().toDirName(target->getResourceType()).string(), |
Adam Lesinski | 07dfd2d | 2015-10-28 15:44:27 -0700 | [diff] [blame] | 4773 | String8(resourceName).string()); |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4774 | resPath.convertToResPath(); |
| 4775 | |
| 4776 | // Add a resource table entry. |
Adam Lesinski | f15de2e | 2014-10-03 14:57:28 -0700 | [diff] [blame] | 4777 | if (bundle->getVerbose()) { |
| 4778 | SourcePos(target->getSourceFile(), -1).printf( |
| 4779 | "using v%d attributes; synthesizing resource %s:%s/%s for configuration %s.", |
Adam Lesinski | 28994d8 | 2015-01-13 13:42:41 -0800 | [diff] [blame] | 4780 | newConfig.sdkVersion, |
Adam Lesinski | f15de2e | 2014-10-03 14:57:28 -0700 | [diff] [blame] | 4781 | mAssets->getPackage().string(), |
| 4782 | newFile->getResourceType().string(), |
| 4783 | String8(resourceName).string(), |
| 4784 | newConfig.toString().string()); |
| 4785 | } |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4786 | |
| 4787 | addEntry(SourcePos(), |
| 4788 | String16(mAssets->getPackage()), |
| 4789 | String16(target->getResourceType()), |
| 4790 | resourceName, |
| 4791 | String16(resPath), |
| 4792 | NULL, |
| 4793 | &newConfig); |
| 4794 | |
| 4795 | // Schedule this to be compiled. |
| 4796 | CompileResourceWorkItem item; |
| 4797 | item.resourceName = resourceName; |
| 4798 | item.resPath = resPath; |
| 4799 | item.file = newFile; |
Adam Lesinski | 07dfd2d | 2015-10-28 15:44:27 -0700 | [diff] [blame] | 4800 | item.xmlRoot = newRoot; |
| 4801 | item.needsCompiling = false; // This step occurs after we parse/assign, so we don't need |
| 4802 | // to do it again. |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4803 | mWorkQueue.push(item); |
| 4804 | } |
Adam Lesinski | e572c01 | 2014-09-19 15:10:04 -0700 | [diff] [blame] | 4805 | return NO_ERROR; |
| 4806 | } |
Adam Lesinski | de7de47 | 2014-11-03 12:03:08 -0800 | [diff] [blame] | 4807 | |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4808 | void ResourceTable::getDensityVaryingResources( |
| 4809 | KeyedVector<Symbol, Vector<SymbolDefinition> >& resources) { |
Adam Lesinski | de7de47 | 2014-11-03 12:03:08 -0800 | [diff] [blame] | 4810 | const ConfigDescription nullConfig; |
| 4811 | |
| 4812 | const size_t packageCount = mOrderedPackages.size(); |
| 4813 | for (size_t p = 0; p < packageCount; p++) { |
| 4814 | const Vector<sp<Type> >& types = mOrderedPackages[p]->getOrderedTypes(); |
| 4815 | const size_t typeCount = types.size(); |
| 4816 | for (size_t t = 0; t < typeCount; t++) { |
| 4817 | const Vector<sp<ConfigList> >& configs = types[t]->getOrderedConfigs(); |
| 4818 | const size_t configCount = configs.size(); |
| 4819 | for (size_t c = 0; c < configCount; c++) { |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4820 | const DefaultKeyedVector<ConfigDescription, sp<Entry> >& configEntries |
| 4821 | = configs[c]->getEntries(); |
Adam Lesinski | de7de47 | 2014-11-03 12:03:08 -0800 | [diff] [blame] | 4822 | const size_t configEntryCount = configEntries.size(); |
| 4823 | for (size_t ce = 0; ce < configEntryCount; ce++) { |
| 4824 | const ConfigDescription& config = configEntries.keyAt(ce); |
| 4825 | if (AaptConfig::isDensityOnly(config)) { |
| 4826 | // This configuration only varies with regards to density. |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4827 | const Symbol symbol( |
| 4828 | mOrderedPackages[p]->getName(), |
Adam Lesinski | de7de47 | 2014-11-03 12:03:08 -0800 | [diff] [blame] | 4829 | types[t]->getName(), |
| 4830 | configs[c]->getName(), |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4831 | getResId(mOrderedPackages[p], types[t], |
| 4832 | configs[c]->getEntryIndex())); |
Adam Lesinski | de7de47 | 2014-11-03 12:03:08 -0800 | [diff] [blame] | 4833 | |
| 4834 | const sp<Entry>& entry = configEntries.valueAt(ce); |
Adam Lesinski | f45d2fa | 2015-07-28 12:10:36 -0700 | [diff] [blame] | 4835 | AaptUtil::appendValue(resources, symbol, |
| 4836 | SymbolDefinition(symbol, config, entry->getPos())); |
Adam Lesinski | de7de47 | 2014-11-03 12:03:08 -0800 | [diff] [blame] | 4837 | } |
| 4838 | } |
| 4839 | } |
| 4840 | } |
| 4841 | } |
| 4842 | } |
Adam Lesinski | 07dfd2d | 2015-10-28 15:44:27 -0700 | [diff] [blame] | 4843 | |
| 4844 | static String16 buildNamespace(const String16& package) { |
| 4845 | return String16("http://schemas.android.com/apk/res/") + package; |
| 4846 | } |
| 4847 | |
| 4848 | static sp<XMLNode> findOnlyChildElement(const sp<XMLNode>& parent) { |
| 4849 | const Vector<sp<XMLNode> >& children = parent->getChildren(); |
| 4850 | sp<XMLNode> onlyChild; |
| 4851 | for (size_t i = 0; i < children.size(); i++) { |
| 4852 | if (children[i]->getType() != XMLNode::TYPE_CDATA) { |
| 4853 | if (onlyChild != NULL) { |
| 4854 | return NULL; |
| 4855 | } |
| 4856 | onlyChild = children[i]; |
| 4857 | } |
| 4858 | } |
| 4859 | return onlyChild; |
| 4860 | } |
| 4861 | |
| 4862 | /** |
| 4863 | * Detects use of the `bundle' format and extracts nested resources into their own top level |
| 4864 | * resources. The bundle format looks like this: |
| 4865 | * |
| 4866 | * <!-- res/drawable/bundle.xml --> |
| 4867 | * <animated-vector xmlns:aapt="http://schemas.android.com/aapt"> |
| 4868 | * <aapt:attr name="android:drawable"> |
| 4869 | * <vector android:width="60dp" |
| 4870 | * android:height="60dp"> |
| 4871 | * <path android:name="v" |
| 4872 | * android:fillColor="#000000" |
| 4873 | * android:pathData="M300,70 l 0,-70 70,..." /> |
| 4874 | * </vector> |
| 4875 | * </aapt:attr> |
| 4876 | * </animated-vector> |
| 4877 | * |
| 4878 | * When AAPT sees the <aapt:attr> tag, it will extract its single element and its children |
| 4879 | * into a new high-level resource, assigning it a name and ID. Then value of the `name` |
| 4880 | * attribute must be a resource attribute. That resource attribute is inserted into the parent |
| 4881 | * with the reference to the extracted resource as the value. |
| 4882 | * |
| 4883 | * <!-- res/drawable/bundle.xml --> |
| 4884 | * <animated-vector android:drawable="@drawable/bundle_1.xml"> |
| 4885 | * </animated-vector> |
| 4886 | * |
| 4887 | * <!-- res/drawable/bundle_1.xml --> |
| 4888 | * <vector android:width="60dp" |
| 4889 | * android:height="60dp"> |
| 4890 | * <path android:name="v" |
| 4891 | * android:fillColor="#000000" |
| 4892 | * android:pathData="M300,70 l 0,-70 70,..." /> |
| 4893 | * </vector> |
| 4894 | */ |
| 4895 | status_t ResourceTable::processBundleFormat(const Bundle* bundle, |
| 4896 | const String16& resourceName, |
| 4897 | const sp<AaptFile>& target, |
| 4898 | const sp<XMLNode>& root) { |
| 4899 | Vector<sp<XMLNode> > namespaces; |
| 4900 | if (root->getType() == XMLNode::TYPE_NAMESPACE) { |
| 4901 | namespaces.push(root); |
| 4902 | } |
| 4903 | return processBundleFormatImpl(bundle, resourceName, target, root, &namespaces); |
| 4904 | } |
| 4905 | |
| 4906 | status_t ResourceTable::processBundleFormatImpl(const Bundle* bundle, |
| 4907 | const String16& resourceName, |
| 4908 | const sp<AaptFile>& target, |
| 4909 | const sp<XMLNode>& parent, |
| 4910 | Vector<sp<XMLNode> >* namespaces) { |
| 4911 | const String16 kAaptNamespaceUri16("http://schemas.android.com/aapt"); |
| 4912 | const String16 kName16("name"); |
| 4913 | const String16 kAttr16("attr"); |
| 4914 | const String16 kAssetPackage16(mAssets->getPackage()); |
| 4915 | |
| 4916 | Vector<sp<XMLNode> >& children = parent->getChildren(); |
| 4917 | for (size_t i = 0; i < children.size(); i++) { |
| 4918 | const sp<XMLNode>& child = children[i]; |
| 4919 | |
| 4920 | if (child->getType() == XMLNode::TYPE_CDATA) { |
| 4921 | continue; |
| 4922 | } else if (child->getType() == XMLNode::TYPE_NAMESPACE) { |
| 4923 | namespaces->push(child); |
| 4924 | } |
| 4925 | |
| 4926 | if (child->getElementNamespace() != kAaptNamespaceUri16 || |
| 4927 | child->getElementName() != kAttr16) { |
| 4928 | status_t result = processBundleFormatImpl(bundle, resourceName, target, child, |
| 4929 | namespaces); |
| 4930 | if (result != NO_ERROR) { |
| 4931 | return result; |
| 4932 | } |
| 4933 | |
| 4934 | if (child->getType() == XMLNode::TYPE_NAMESPACE) { |
| 4935 | namespaces->pop(); |
| 4936 | } |
| 4937 | continue; |
| 4938 | } |
| 4939 | |
| 4940 | // This is the <aapt:attr> tag. Look for the 'name' attribute. |
| 4941 | SourcePos source(child->getFilename(), child->getStartLineNumber()); |
| 4942 | |
| 4943 | sp<XMLNode> nestedRoot = findOnlyChildElement(child); |
| 4944 | if (nestedRoot == NULL) { |
| 4945 | source.error("<%s:%s> must have exactly one child element", |
| 4946 | String8(child->getElementNamespace()).string(), |
| 4947 | String8(child->getElementName()).string()); |
| 4948 | return UNKNOWN_ERROR; |
| 4949 | } |
| 4950 | |
| 4951 | // Find the special attribute 'parent-attr'. This attribute's value contains |
| 4952 | // the resource attribute for which this element should be assigned in the parent. |
| 4953 | const XMLNode::attribute_entry* attr = child->getAttribute(String16(), kName16); |
| 4954 | if (attr == NULL) { |
| 4955 | source.error("inline resource definition must specify an attribute via 'name'"); |
| 4956 | return UNKNOWN_ERROR; |
| 4957 | } |
| 4958 | |
| 4959 | // Parse the attribute name. |
| 4960 | const char* errorMsg = NULL; |
| 4961 | String16 attrPackage, attrType, attrName; |
| 4962 | bool result = ResTable::expandResourceRef(attr->string.string(), |
| 4963 | attr->string.size(), |
| 4964 | &attrPackage, &attrType, &attrName, |
| 4965 | &kAttr16, &kAssetPackage16, |
| 4966 | &errorMsg, NULL); |
| 4967 | if (!result) { |
| 4968 | source.error("invalid attribute name for 'name': %s", errorMsg); |
| 4969 | return UNKNOWN_ERROR; |
| 4970 | } |
| 4971 | |
| 4972 | if (attrType != kAttr16) { |
| 4973 | // The value of the 'name' attribute must be an attribute reference. |
| 4974 | source.error("value of 'name' must be an attribute reference."); |
| 4975 | return UNKNOWN_ERROR; |
| 4976 | } |
| 4977 | |
| 4978 | // Generate a name for this nested resource and try to add it to the table. |
| 4979 | // We do this in a loop because the name may be taken, in which case we will |
| 4980 | // increment a suffix until we succeed. |
| 4981 | String8 nestedResourceName; |
| 4982 | String8 nestedResourcePath; |
| 4983 | int suffix = 1; |
| 4984 | while (true) { |
| 4985 | // This child element will be extracted into its own resource file. |
| 4986 | // Generate a name and path for it from its parent. |
| 4987 | nestedResourceName = String8::format("%s_%d", |
| 4988 | String8(resourceName).string(), suffix++); |
| 4989 | nestedResourcePath = String8::format("res/%s/%s.xml", |
| 4990 | target->getGroupEntry().toDirName(target->getResourceType()) |
| 4991 | .string(), |
| 4992 | nestedResourceName.string()); |
| 4993 | |
| 4994 | // Lookup or create the entry for this name. |
| 4995 | sp<Entry> entry = getEntry(kAssetPackage16, |
| 4996 | String16(target->getResourceType()), |
| 4997 | String16(nestedResourceName), |
| 4998 | source, |
| 4999 | false, |
| 5000 | &target->getGroupEntry().toParams(), |
| 5001 | true); |
| 5002 | if (entry == NULL) { |
| 5003 | return UNKNOWN_ERROR; |
| 5004 | } |
| 5005 | |
| 5006 | if (entry->getType() == Entry::TYPE_UNKNOWN) { |
| 5007 | // The value for this resource has never been set, |
| 5008 | // meaning we're good! |
| 5009 | entry->setItem(source, String16(nestedResourcePath)); |
| 5010 | break; |
| 5011 | } |
| 5012 | |
| 5013 | // We failed (name already exists), so try with a different name |
| 5014 | // (increment the suffix). |
| 5015 | } |
| 5016 | |
| 5017 | if (bundle->getVerbose()) { |
| 5018 | source.printf("generating nested resource %s:%s/%s", |
| 5019 | mAssets->getPackage().string(), target->getResourceType().string(), |
| 5020 | nestedResourceName.string()); |
| 5021 | } |
| 5022 | |
| 5023 | // Build the attribute reference and assign it to the parent. |
| 5024 | String16 nestedResourceRef = String16(String8::format("@%s:%s/%s", |
| 5025 | mAssets->getPackage().string(), target->getResourceType().string(), |
| 5026 | nestedResourceName.string())); |
| 5027 | |
| 5028 | String16 attrNs = buildNamespace(attrPackage); |
| 5029 | if (parent->getAttribute(attrNs, attrName) != NULL) { |
| 5030 | SourcePos(parent->getFilename(), parent->getStartLineNumber()) |
| 5031 | .error("parent of nested resource already defines attribute '%s:%s'", |
| 5032 | String8(attrPackage).string(), String8(attrName).string()); |
| 5033 | return UNKNOWN_ERROR; |
| 5034 | } |
| 5035 | |
| 5036 | // Add the reference to the inline resource. |
| 5037 | parent->addAttribute(attrNs, attrName, nestedResourceRef); |
| 5038 | |
| 5039 | // Remove the <aapt:attr> child element from here. |
| 5040 | children.removeAt(i); |
| 5041 | i--; |
| 5042 | |
| 5043 | // Append all namespace declarations that we've seen on this branch in the XML tree |
| 5044 | // to this resource. |
| 5045 | // We do this because the order of namespace declarations and prefix usage is determined |
| 5046 | // by the developer and we do not want to override any decisions. Be conservative. |
| 5047 | for (size_t nsIndex = namespaces->size(); nsIndex > 0; nsIndex--) { |
| 5048 | const sp<XMLNode>& ns = namespaces->itemAt(nsIndex - 1); |
| 5049 | sp<XMLNode> newNs = XMLNode::newNamespace(ns->getFilename(), ns->getNamespacePrefix(), |
| 5050 | ns->getNamespaceUri()); |
| 5051 | newNs->addChild(nestedRoot); |
| 5052 | nestedRoot = newNs; |
| 5053 | } |
| 5054 | |
| 5055 | // Schedule compilation of the nested resource. |
| 5056 | CompileResourceWorkItem workItem; |
| 5057 | workItem.resPath = nestedResourcePath; |
| 5058 | workItem.resourceName = String16(nestedResourceName); |
| 5059 | workItem.xmlRoot = nestedRoot; |
| 5060 | workItem.file = new AaptFile(target->getSourceFile(), target->getGroupEntry(), |
| 5061 | target->getResourceType()); |
| 5062 | mWorkQueue.push(workItem); |
| 5063 | } |
| 5064 | return NO_ERROR; |
| 5065 | } |