The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2006 The Android Open Source Project |
| 3 | // |
| 4 | // Build resource files from raw assets. |
| 5 | // |
| 6 | #include "Main.h" |
| 7 | #include "AaptAssets.h" |
| 8 | #include "StringPool.h" |
| 9 | #include "XMLNode.h" |
| 10 | #include "ResourceTable.h" |
| 11 | #include "Images.h" |
| 12 | |
| 13 | #define NOISY(x) // x |
| 14 | |
| 15 | // ========================================================================== |
| 16 | // ========================================================================== |
| 17 | // ========================================================================== |
| 18 | |
| 19 | class PackageInfo |
| 20 | { |
| 21 | public: |
| 22 | PackageInfo() |
| 23 | { |
| 24 | } |
| 25 | ~PackageInfo() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | status_t parsePackage(const sp<AaptGroup>& grp); |
| 30 | }; |
| 31 | |
| 32 | // ========================================================================== |
| 33 | // ========================================================================== |
| 34 | // ========================================================================== |
| 35 | |
| 36 | static String8 parseResourceName(const String8& leaf) |
| 37 | { |
| 38 | const char* firstDot = strchr(leaf.string(), '.'); |
| 39 | const char* str = leaf.string(); |
| 40 | |
| 41 | if (firstDot) { |
| 42 | return String8(str, firstDot-str); |
| 43 | } else { |
| 44 | return String8(str); |
| 45 | } |
| 46 | } |
| 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | ResourceTypeSet::ResourceTypeSet() |
| 49 | :RefBase(), |
| 50 | KeyedVector<String8,sp<AaptGroup> >() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | class ResourceDirIterator |
| 55 | { |
| 56 | public: |
| 57 | ResourceDirIterator(const sp<ResourceTypeSet>& set, const String8& resType) |
| 58 | : mResType(resType), mSet(set), mSetPos(0), mGroupPos(0) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | inline const sp<AaptGroup>& getGroup() const { return mGroup; } |
| 63 | inline const sp<AaptFile>& getFile() const { return mFile; } |
| 64 | |
| 65 | inline const String8& getBaseName() const { return mBaseName; } |
| 66 | inline const String8& getLeafName() const { return mLeafName; } |
| 67 | inline String8 getPath() const { return mPath; } |
| 68 | inline const ResTable_config& getParams() const { return mParams; } |
| 69 | |
| 70 | enum { |
| 71 | EOD = 1 |
| 72 | }; |
| 73 | |
| 74 | ssize_t next() |
| 75 | { |
| 76 | while (true) { |
| 77 | sp<AaptGroup> group; |
| 78 | sp<AaptFile> file; |
| 79 | |
| 80 | // Try to get next file in this current group. |
| 81 | if (mGroup != NULL && mGroupPos < mGroup->getFiles().size()) { |
| 82 | group = mGroup; |
| 83 | file = group->getFiles().valueAt(mGroupPos++); |
| 84 | |
| 85 | // Try to get the next group/file in this directory |
| 86 | } else if (mSetPos < mSet->size()) { |
| 87 | mGroup = group = mSet->valueAt(mSetPos++); |
| 88 | if (group->getFiles().size() < 1) { |
| 89 | continue; |
| 90 | } |
| 91 | file = group->getFiles().valueAt(0); |
| 92 | mGroupPos = 1; |
| 93 | |
| 94 | // All done! |
| 95 | } else { |
| 96 | return EOD; |
| 97 | } |
| 98 | |
| 99 | mFile = file; |
| 100 | |
| 101 | String8 leaf(group->getLeaf()); |
| 102 | mLeafName = String8(leaf); |
| 103 | mParams = file->getGroupEntry().toParams(); |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 104 | NOISY(printf("Dir %s: mcc=%d mnc=%d lang=%c%c cnt=%c%c orient=%d ui=%d density=%d touch=%d key=%d inp=%d nav=%d\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | group->getPath().string(), mParams.mcc, mParams.mnc, |
| 106 | mParams.language[0] ? mParams.language[0] : '-', |
| 107 | mParams.language[1] ? mParams.language[1] : '-', |
| 108 | mParams.country[0] ? mParams.country[0] : '-', |
| 109 | mParams.country[1] ? mParams.country[1] : '-', |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 110 | mParams.orientation, mParams.uiMode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | mParams.density, mParams.touchscreen, mParams.keyboard, |
| 112 | mParams.inputFlags, mParams.navigation)); |
| 113 | mPath = "res"; |
| 114 | mPath.appendPath(file->getGroupEntry().toDirName(mResType)); |
| 115 | mPath.appendPath(leaf); |
| 116 | mBaseName = parseResourceName(leaf); |
| 117 | if (mBaseName == "") { |
| 118 | fprintf(stderr, "Error: malformed resource filename %s\n", |
| 119 | file->getPrintableSource().string()); |
| 120 | return UNKNOWN_ERROR; |
| 121 | } |
| 122 | |
| 123 | NOISY(printf("file name=%s\n", mBaseName.string())); |
| 124 | |
| 125 | return NO_ERROR; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | String8 mResType; |
| 131 | |
| 132 | const sp<ResourceTypeSet> mSet; |
| 133 | size_t mSetPos; |
| 134 | |
| 135 | sp<AaptGroup> mGroup; |
| 136 | size_t mGroupPos; |
| 137 | |
| 138 | sp<AaptFile> mFile; |
| 139 | String8 mBaseName; |
| 140 | String8 mLeafName; |
| 141 | String8 mPath; |
| 142 | ResTable_config mParams; |
| 143 | }; |
| 144 | |
| 145 | // ========================================================================== |
| 146 | // ========================================================================== |
| 147 | // ========================================================================== |
| 148 | |
| 149 | bool isValidResourceType(const String8& type) |
| 150 | { |
| 151 | return type == "anim" || type == "drawable" || type == "layout" |
| 152 | || type == "values" || type == "xml" || type == "raw" |
| 153 | || type == "color" || type == "menu"; |
| 154 | } |
| 155 | |
| 156 | static sp<AaptFile> getResourceFile(const sp<AaptAssets>& assets, bool makeIfNecessary=true) |
| 157 | { |
| 158 | sp<AaptGroup> group = assets->getFiles().valueFor(String8("resources.arsc")); |
| 159 | sp<AaptFile> file; |
| 160 | if (group != NULL) { |
| 161 | file = group->getFiles().valueFor(AaptGroupEntry()); |
| 162 | if (file != NULL) { |
| 163 | return file; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (!makeIfNecessary) { |
| 168 | return NULL; |
| 169 | } |
| 170 | return assets->addFile(String8("resources.arsc"), AaptGroupEntry(), String8(), |
| 171 | NULL, String8()); |
| 172 | } |
| 173 | |
Kenny Root | b5ef7ee | 2009-12-10 13:52:53 -0800 | [diff] [blame] | 174 | static status_t parsePackage(Bundle* bundle, const sp<AaptAssets>& assets, |
| 175 | const sp<AaptGroup>& grp) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | { |
| 177 | if (grp->getFiles().size() != 1) { |
Marco Nelissen | dd93186 | 2009-07-13 13:02:33 -0700 | [diff] [blame] | 178 | fprintf(stderr, "warning: Multiple AndroidManifest.xml files found, using %s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | grp->getFiles().valueAt(0)->getPrintableSource().string()); |
| 180 | } |
| 181 | |
| 182 | sp<AaptFile> file = grp->getFiles().valueAt(0); |
| 183 | |
| 184 | ResXMLTree block; |
| 185 | status_t err = parseXMLResource(file, &block); |
| 186 | if (err != NO_ERROR) { |
| 187 | return err; |
| 188 | } |
| 189 | //printXMLBlock(&block); |
| 190 | |
| 191 | ResXMLTree::event_code_t code; |
| 192 | while ((code=block.next()) != ResXMLTree::START_TAG |
| 193 | && code != ResXMLTree::END_DOCUMENT |
| 194 | && code != ResXMLTree::BAD_DOCUMENT) { |
| 195 | } |
| 196 | |
| 197 | size_t len; |
| 198 | if (code != ResXMLTree::START_TAG) { |
| 199 | fprintf(stderr, "%s:%d: No start tag found\n", |
| 200 | file->getPrintableSource().string(), block.getLineNumber()); |
| 201 | return UNKNOWN_ERROR; |
| 202 | } |
| 203 | if (strcmp16(block.getElementName(&len), String16("manifest").string()) != 0) { |
| 204 | fprintf(stderr, "%s:%d: Invalid start tag %s, expected <manifest>\n", |
| 205 | file->getPrintableSource().string(), block.getLineNumber(), |
| 206 | String8(block.getElementName(&len)).string()); |
| 207 | return UNKNOWN_ERROR; |
| 208 | } |
| 209 | |
| 210 | ssize_t nameIndex = block.indexOfAttribute(NULL, "package"); |
| 211 | if (nameIndex < 0) { |
| 212 | fprintf(stderr, "%s:%d: <manifest> does not have package attribute.\n", |
| 213 | file->getPrintableSource().string(), block.getLineNumber()); |
| 214 | return UNKNOWN_ERROR; |
| 215 | } |
| 216 | |
| 217 | assets->setPackage(String8(block.getAttributeStringValue(nameIndex, &len))); |
| 218 | |
Kenny Root | b5ef7ee | 2009-12-10 13:52:53 -0800 | [diff] [blame] | 219 | String16 uses_sdk16("uses-sdk"); |
| 220 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| 221 | && code != ResXMLTree::BAD_DOCUMENT) { |
| 222 | if (code == ResXMLTree::START_TAG) { |
| 223 | if (strcmp16(block.getElementName(&len), uses_sdk16.string()) == 0) { |
Kenny Root | 5a8ec76 | 2010-02-24 20:00:03 -0800 | [diff] [blame] | 224 | ssize_t minSdkIndex = block.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE, |
Kenny Root | b5ef7ee | 2009-12-10 13:52:53 -0800 | [diff] [blame] | 225 | "minSdkVersion"); |
| 226 | if (minSdkIndex >= 0) { |
Kenny Root | 7ff20e3 | 2010-02-24 23:49:59 -0800 | [diff] [blame] | 227 | const uint16_t* minSdk16 = block.getAttributeStringValue(minSdkIndex, &len); |
| 228 | const char* minSdk8 = strdup(String8(minSdk16).string()); |
| 229 | bundle->setMinSdkVersion(minSdk8); |
Kenny Root | b5ef7ee | 2009-12-10 13:52:53 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | return NO_ERROR; |
| 236 | } |
| 237 | |
| 238 | // ========================================================================== |
| 239 | // ========================================================================== |
| 240 | // ========================================================================== |
| 241 | |
| 242 | static status_t makeFileResources(Bundle* bundle, const sp<AaptAssets>& assets, |
| 243 | ResourceTable* table, |
| 244 | const sp<ResourceTypeSet>& set, |
| 245 | const char* resType) |
| 246 | { |
| 247 | String8 type8(resType); |
| 248 | String16 type16(resType); |
| 249 | |
| 250 | bool hasErrors = false; |
| 251 | |
| 252 | ResourceDirIterator it(set, String8(resType)); |
| 253 | ssize_t res; |
| 254 | while ((res=it.next()) == NO_ERROR) { |
| 255 | if (bundle->getVerbose()) { |
| 256 | printf(" (new resource id %s from %s)\n", |
| 257 | it.getBaseName().string(), it.getFile()->getPrintableSource().string()); |
| 258 | } |
| 259 | String16 baseName(it.getBaseName()); |
| 260 | const char16_t* str = baseName.string(); |
| 261 | const char16_t* const end = str + baseName.size(); |
| 262 | while (str < end) { |
| 263 | if (!((*str >= 'a' && *str <= 'z') |
| 264 | || (*str >= '0' && *str <= '9') |
| 265 | || *str == '_' || *str == '.')) { |
| 266 | fprintf(stderr, "%s: Invalid file name: must contain only [a-z0-9_.]\n", |
| 267 | it.getPath().string()); |
| 268 | hasErrors = true; |
| 269 | } |
| 270 | str++; |
| 271 | } |
| 272 | String8 resPath = it.getPath(); |
| 273 | resPath.convertToResPath(); |
| 274 | table->addEntry(SourcePos(it.getPath(), 0), String16(assets->getPackage()), |
| 275 | type16, |
| 276 | baseName, |
| 277 | String16(resPath), |
| 278 | NULL, |
| 279 | &it.getParams()); |
| 280 | assets->addResource(it.getLeafName(), resPath, it.getFile(), type8); |
| 281 | } |
| 282 | |
| 283 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; |
| 284 | } |
| 285 | |
| 286 | static status_t preProcessImages(Bundle* bundle, const sp<AaptAssets>& assets, |
| 287 | const sp<ResourceTypeSet>& set) |
| 288 | { |
| 289 | ResourceDirIterator it(set, String8("drawable")); |
| 290 | Vector<sp<AaptFile> > newNameFiles; |
| 291 | Vector<String8> newNamePaths; |
Daniel Sandler | 3547f85 | 2009-08-14 13:47:30 -0700 | [diff] [blame] | 292 | bool hasErrors = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | ssize_t res; |
| 294 | while ((res=it.next()) == NO_ERROR) { |
| 295 | res = preProcessImage(bundle, assets, it.getFile(), NULL); |
Daniel Sandler | 3547f85 | 2009-08-14 13:47:30 -0700 | [diff] [blame] | 296 | if (res < NO_ERROR) { |
| 297 | hasErrors = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Daniel Sandler | 3547f85 | 2009-08-14 13:47:30 -0700 | [diff] [blame] | 301 | return (hasErrors || (res < NO_ERROR)) ? UNKNOWN_ERROR : NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | status_t postProcessImages(const sp<AaptAssets>& assets, |
| 305 | ResourceTable* table, |
| 306 | const sp<ResourceTypeSet>& set) |
| 307 | { |
| 308 | ResourceDirIterator it(set, String8("drawable")); |
Daniel Sandler | 3547f85 | 2009-08-14 13:47:30 -0700 | [diff] [blame] | 309 | bool hasErrors = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | ssize_t res; |
| 311 | while ((res=it.next()) == NO_ERROR) { |
| 312 | res = postProcessImage(assets, table, it.getFile()); |
Daniel Sandler | 3547f85 | 2009-08-14 13:47:30 -0700 | [diff] [blame] | 313 | if (res < NO_ERROR) { |
| 314 | hasErrors = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Daniel Sandler | 3547f85 | 2009-08-14 13:47:30 -0700 | [diff] [blame] | 318 | return (hasErrors || (res < NO_ERROR)) ? UNKNOWN_ERROR : NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static void collect_files(const sp<AaptDir>& dir, |
| 322 | KeyedVector<String8, sp<ResourceTypeSet> >* resources) |
| 323 | { |
| 324 | const DefaultKeyedVector<String8, sp<AaptGroup> >& groups = dir->getFiles(); |
| 325 | int N = groups.size(); |
| 326 | for (int i=0; i<N; i++) { |
| 327 | String8 leafName = groups.keyAt(i); |
| 328 | const sp<AaptGroup>& group = groups.valueAt(i); |
| 329 | |
| 330 | const DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> >& files |
| 331 | = group->getFiles(); |
| 332 | |
| 333 | if (files.size() == 0) { |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | String8 resType = files.valueAt(0)->getResourceType(); |
| 338 | |
| 339 | ssize_t index = resources->indexOfKey(resType); |
| 340 | |
| 341 | if (index < 0) { |
| 342 | sp<ResourceTypeSet> set = new ResourceTypeSet(); |
| 343 | set->add(leafName, group); |
| 344 | resources->add(resType, set); |
| 345 | } else { |
| 346 | sp<ResourceTypeSet> set = resources->valueAt(index); |
| 347 | index = set->indexOfKey(leafName); |
| 348 | if (index < 0) { |
| 349 | set->add(leafName, group); |
| 350 | } else { |
| 351 | sp<AaptGroup> existingGroup = set->valueAt(index); |
| 352 | int M = files.size(); |
| 353 | for (int j=0; j<M; j++) { |
| 354 | existingGroup->addFile(files.valueAt(j)); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | static void collect_files(const sp<AaptAssets>& ass, |
| 362 | KeyedVector<String8, sp<ResourceTypeSet> >* resources) |
| 363 | { |
| 364 | const Vector<sp<AaptDir> >& dirs = ass->resDirs(); |
| 365 | int N = dirs.size(); |
| 366 | |
| 367 | for (int i=0; i<N; i++) { |
| 368 | sp<AaptDir> d = dirs.itemAt(i); |
| 369 | collect_files(d, resources); |
| 370 | |
| 371 | // don't try to include the res dir |
| 372 | ass->removeDir(d->getLeaf()); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | enum { |
| 377 | ATTR_OKAY = -1, |
| 378 | ATTR_NOT_FOUND = -2, |
| 379 | ATTR_LEADING_SPACES = -3, |
| 380 | ATTR_TRAILING_SPACES = -4 |
| 381 | }; |
| 382 | static int validateAttr(const String8& path, const ResXMLParser& parser, |
| 383 | const char* ns, const char* attr, const char* validChars, bool required) |
| 384 | { |
| 385 | size_t len; |
| 386 | |
| 387 | ssize_t index = parser.indexOfAttribute(ns, attr); |
| 388 | const uint16_t* str; |
| 389 | if (index >= 0 && (str=parser.getAttributeStringValue(index, &len)) != NULL) { |
| 390 | if (validChars) { |
| 391 | for (size_t i=0; i<len; i++) { |
| 392 | uint16_t c = str[i]; |
| 393 | const char* p = validChars; |
| 394 | bool okay = false; |
| 395 | while (*p) { |
| 396 | if (c == *p) { |
| 397 | okay = true; |
| 398 | break; |
| 399 | } |
| 400 | p++; |
| 401 | } |
| 402 | if (!okay) { |
| 403 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n", |
| 404 | path.string(), parser.getLineNumber(), |
| 405 | String8(parser.getElementName(&len)).string(), attr, (char)str[i]); |
| 406 | return (int)i; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | if (*str == ' ') { |
| 411 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s can not start with a space.\n", |
| 412 | path.string(), parser.getLineNumber(), |
| 413 | String8(parser.getElementName(&len)).string(), attr); |
| 414 | return ATTR_LEADING_SPACES; |
| 415 | } |
| 416 | if (str[len-1] == ' ') { |
| 417 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s can not end with a space.\n", |
| 418 | path.string(), parser.getLineNumber(), |
| 419 | String8(parser.getElementName(&len)).string(), attr); |
| 420 | return ATTR_TRAILING_SPACES; |
| 421 | } |
| 422 | return ATTR_OKAY; |
| 423 | } |
| 424 | if (required) { |
| 425 | fprintf(stderr, "%s:%d: Tag <%s> missing required attribute %s.\n", |
| 426 | path.string(), parser.getLineNumber(), |
| 427 | String8(parser.getElementName(&len)).string(), attr); |
| 428 | return ATTR_NOT_FOUND; |
| 429 | } |
| 430 | return ATTR_OKAY; |
| 431 | } |
| 432 | |
| 433 | static void checkForIds(const String8& path, ResXMLParser& parser) |
| 434 | { |
| 435 | ResXMLTree::event_code_t code; |
| 436 | while ((code=parser.next()) != ResXMLTree::END_DOCUMENT |
| 437 | && code > ResXMLTree::BAD_DOCUMENT) { |
| 438 | if (code == ResXMLTree::START_TAG) { |
| 439 | ssize_t index = parser.indexOfAttribute(NULL, "id"); |
| 440 | if (index >= 0) { |
Marco Nelissen | dd93186 | 2009-07-13 13:02:33 -0700 | [diff] [blame] | 441 | fprintf(stderr, "%s:%d: warning: found plain 'id' attribute; did you mean the new 'android:id' name?\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | path.string(), parser.getLineNumber()); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 448 | static bool applyFileOverlay(Bundle *bundle, |
| 449 | const sp<AaptAssets>& assets, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | const sp<ResourceTypeSet>& baseSet, |
| 451 | const char *resType) |
| 452 | { |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 453 | if (bundle->getVerbose()) { |
| 454 | printf("applyFileOverlay for %s\n", resType); |
| 455 | } |
| 456 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | // Replace any base level files in this category with any found from the overlay |
| 458 | // Also add any found only in the overlay. |
| 459 | sp<AaptAssets> overlay = assets->getOverlay(); |
| 460 | String8 resTypeString(resType); |
Robert Greenwalt | fa5c7e1 | 2009-06-05 18:53:26 -0700 | [diff] [blame] | 461 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | // work through the linked list of overlays |
| 463 | while (overlay.get()) { |
| 464 | KeyedVector<String8, sp<ResourceTypeSet> >* overlayRes = overlay->getResources(); |
| 465 | |
| 466 | // get the overlay resources of the requested type |
| 467 | ssize_t index = overlayRes->indexOfKey(resTypeString); |
| 468 | if (index >= 0) { |
| 469 | sp<ResourceTypeSet> overlaySet = overlayRes->valueAt(index); |
| 470 | |
| 471 | // for each of the resources, check for a match in the previously built |
| 472 | // non-overlay "baseset". |
| 473 | size_t overlayCount = overlaySet->size(); |
| 474 | for (size_t overlayIndex=0; overlayIndex<overlayCount; overlayIndex++) { |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 475 | if (bundle->getVerbose()) { |
| 476 | printf("trying overlaySet Key=%s\n",overlaySet->keyAt(overlayIndex).string()); |
| 477 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | size_t baseIndex = baseSet->indexOfKey(overlaySet->keyAt(overlayIndex)); |
Robert Greenwalt | fa5c7e1 | 2009-06-05 18:53:26 -0700 | [diff] [blame] | 479 | if (baseIndex < UNKNOWN_ERROR) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | // look for same flavor. For a given file (strings.xml, for example) |
| 481 | // there may be a locale specific or other flavors - we want to match |
| 482 | // the same flavor. |
| 483 | sp<AaptGroup> overlayGroup = overlaySet->valueAt(overlayIndex); |
| 484 | sp<AaptGroup> baseGroup = baseSet->valueAt(baseIndex); |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 485 | |
| 486 | DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > overlayFiles = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | overlayGroup->getFiles(); |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 488 | if (bundle->getVerbose()) { |
| 489 | DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > baseFiles = |
| 490 | baseGroup->getFiles(); |
| 491 | for (size_t i=0; i < baseFiles.size(); i++) { |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 492 | printf("baseFile %ld has flavor %s\n", i, |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 493 | baseFiles.keyAt(i).toString().string()); |
| 494 | } |
| 495 | for (size_t i=0; i < overlayFiles.size(); i++) { |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 496 | printf("overlayFile %ld has flavor %s\n", i, |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 497 | overlayFiles.keyAt(i).toString().string()); |
| 498 | } |
| 499 | } |
| 500 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | size_t overlayGroupSize = overlayFiles.size(); |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 502 | for (size_t overlayGroupIndex = 0; |
| 503 | overlayGroupIndex<overlayGroupSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | overlayGroupIndex++) { |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 505 | size_t baseFileIndex = |
| 506 | baseGroup->getFiles().indexOfKey(overlayFiles. |
| 507 | keyAt(overlayGroupIndex)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | if(baseFileIndex < UNKNOWN_ERROR) { |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 509 | if (bundle->getVerbose()) { |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 510 | printf("found a match (%ld) for overlay file %s, for flavor %s\n", |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 511 | baseFileIndex, |
| 512 | overlayGroup->getLeaf().string(), |
| 513 | overlayFiles.keyAt(overlayGroupIndex).toString().string()); |
| 514 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | baseGroup->removeFile(baseFileIndex); |
| 516 | } else { |
| 517 | // didn't find a match fall through and add it.. |
| 518 | } |
| 519 | baseGroup->addFile(overlayFiles.valueAt(overlayGroupIndex)); |
Dianne Hackborn | 64551b2 | 2009-08-15 00:00:33 -0700 | [diff] [blame] | 520 | assets->addGroupEntry(overlayFiles.keyAt(overlayGroupIndex)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | } |
| 522 | } else { |
| 523 | // this group doesn't exist (a file that's only in the overlay) |
Dianne Hackborn | 58c27a0 | 2009-08-13 13:36:00 -0700 | [diff] [blame] | 524 | baseSet->add(overlaySet->keyAt(overlayIndex), |
| 525 | overlaySet->valueAt(overlayIndex)); |
Dianne Hackborn | 64551b2 | 2009-08-15 00:00:33 -0700 | [diff] [blame] | 526 | // make sure all flavors are defined in the resources. |
| 527 | sp<AaptGroup> overlayGroup = overlaySet->valueAt(overlayIndex); |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 528 | DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > overlayFiles = |
Dianne Hackborn | 64551b2 | 2009-08-15 00:00:33 -0700 | [diff] [blame] | 529 | overlayGroup->getFiles(); |
| 530 | size_t overlayGroupSize = overlayFiles.size(); |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 531 | for (size_t overlayGroupIndex = 0; |
| 532 | overlayGroupIndex<overlayGroupSize; |
Dianne Hackborn | 64551b2 | 2009-08-15 00:00:33 -0700 | [diff] [blame] | 533 | overlayGroupIndex++) { |
| 534 | assets->addGroupEntry(overlayFiles.keyAt(overlayGroupIndex)); |
| 535 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | // this overlay didn't have resources for this type |
| 539 | } |
| 540 | // try next overlay |
| 541 | overlay = overlay->getOverlay(); |
| 542 | } |
Robert Greenwalt | fa5c7e1 | 2009-06-05 18:53:26 -0700 | [diff] [blame] | 543 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 546 | void addTagAttribute(const sp<XMLNode>& node, const char* ns8, |
| 547 | const char* attr8, const char* value) |
| 548 | { |
| 549 | if (value == NULL) { |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | const String16 ns(ns8); |
| 554 | const String16 attr(attr8); |
| 555 | |
| 556 | if (node->getAttribute(ns, attr) != NULL) { |
| 557 | fprintf(stderr, "Warning: AndroidManifest.xml already defines %s (in %s)\n", |
| 558 | String8(attr).string(), String8(ns).string()); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | node->addAttribute(ns, attr, String16(value)); |
| 563 | } |
| 564 | |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 565 | static void fullyQualifyClassName(String8& package, sp<XMLNode> node) { |
| 566 | XMLNode::attribute_entry* attr = node->editAttribute( |
| 567 | String16("http://schemas.android.com/apk/res/android"), String16("name")); |
| 568 | if (attr != NULL) { |
| 569 | String8 name(attr->string); |
| 570 | |
| 571 | // asdf --> package.asdf |
| 572 | // .asdf .a.b --> package.asdf package.a.b |
| 573 | // asdf.adsf --> asdf.asdf |
| 574 | String8 className; |
| 575 | const char* p = name.string(); |
| 576 | const char* q = strchr(p, '.'); |
| 577 | if (p == q) { |
| 578 | className += package; |
| 579 | className += name; |
| 580 | } else if (q == NULL) { |
| 581 | className += package; |
| 582 | className += "."; |
| 583 | className += name; |
| 584 | } else { |
| 585 | className += name; |
| 586 | } |
| 587 | NOISY(printf("Qualifying class '%s' to '%s'", name.string(), className.string())); |
| 588 | attr->string.setTo(String16(className)); |
| 589 | } |
| 590 | } |
| 591 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 592 | status_t massageManifest(Bundle* bundle, sp<XMLNode> root) |
| 593 | { |
| 594 | root = root->searchElement(String16(), String16("manifest")); |
| 595 | if (root == NULL) { |
| 596 | fprintf(stderr, "No <manifest> tag.\n"); |
| 597 | return UNKNOWN_ERROR; |
| 598 | } |
| 599 | |
| 600 | addTagAttribute(root, RESOURCES_ANDROID_NAMESPACE, "versionCode", |
| 601 | bundle->getVersionCode()); |
| 602 | addTagAttribute(root, RESOURCES_ANDROID_NAMESPACE, "versionName", |
| 603 | bundle->getVersionName()); |
| 604 | |
| 605 | if (bundle->getMinSdkVersion() != NULL |
| 606 | || bundle->getTargetSdkVersion() != NULL |
| 607 | || bundle->getMaxSdkVersion() != NULL) { |
| 608 | sp<XMLNode> vers = root->getChildElement(String16(), String16("uses-sdk")); |
| 609 | if (vers == NULL) { |
| 610 | vers = XMLNode::newElement(root->getFilename(), String16(), String16("uses-sdk")); |
| 611 | root->insertChildAt(vers, 0); |
| 612 | } |
| 613 | |
| 614 | addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "minSdkVersion", |
| 615 | bundle->getMinSdkVersion()); |
| 616 | addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "targetSdkVersion", |
| 617 | bundle->getTargetSdkVersion()); |
| 618 | addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "maxSdkVersion", |
| 619 | bundle->getMaxSdkVersion()); |
| 620 | } |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 621 | |
| 622 | // Deal with manifest package name overrides |
| 623 | const char* manifestPackageNameOverride = bundle->getManifestPackageNameOverride(); |
| 624 | if (manifestPackageNameOverride != NULL) { |
| 625 | // Update the actual package name |
| 626 | XMLNode::attribute_entry* attr = root->editAttribute(String16(), String16("package")); |
| 627 | if (attr == NULL) { |
| 628 | fprintf(stderr, "package name is required with --rename-manifest-package.\n"); |
| 629 | return UNKNOWN_ERROR; |
| 630 | } |
| 631 | String8 origPackage(attr->string); |
| 632 | attr->string.setTo(String16(manifestPackageNameOverride)); |
| 633 | NOISY(printf("Overriding package '%s' to be '%s'\n", origPackage.string(), manifestPackageNameOverride)); |
| 634 | |
| 635 | // Make class names fully qualified |
| 636 | sp<XMLNode> application = root->getChildElement(String16(), String16("application")); |
| 637 | if (application != NULL) { |
| 638 | fullyQualifyClassName(origPackage, application); |
| 639 | |
| 640 | Vector<sp<XMLNode> >& children = const_cast<Vector<sp<XMLNode> >&>(application->getChildren()); |
| 641 | for (size_t i = 0; i < children.size(); i++) { |
| 642 | sp<XMLNode> child = children.editItemAt(i); |
| 643 | String8 tag(child->getElementName()); |
| 644 | if (tag == "activity" || tag == "service" || tag == "receiver" || tag == "provider") { |
| 645 | fullyQualifyClassName(origPackage, child); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 651 | return NO_ERROR; |
| 652 | } |
| 653 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | #define ASSIGN_IT(n) \ |
| 655 | do { \ |
| 656 | ssize_t index = resources->indexOfKey(String8(#n)); \ |
| 657 | if (index >= 0) { \ |
| 658 | n ## s = resources->valueAt(index); \ |
| 659 | } \ |
| 660 | } while (0) |
| 661 | |
| 662 | status_t buildResources(Bundle* bundle, const sp<AaptAssets>& assets) |
| 663 | { |
| 664 | // First, look for a package file to parse. This is required to |
| 665 | // be able to generate the resource information. |
| 666 | sp<AaptGroup> androidManifestFile = |
| 667 | assets->getFiles().valueFor(String8("AndroidManifest.xml")); |
| 668 | if (androidManifestFile == NULL) { |
| 669 | fprintf(stderr, "ERROR: No AndroidManifest.xml file found.\n"); |
| 670 | return UNKNOWN_ERROR; |
| 671 | } |
| 672 | |
Kenny Root | b5ef7ee | 2009-12-10 13:52:53 -0800 | [diff] [blame] | 673 | status_t err = parsePackage(bundle, assets, androidManifestFile); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | if (err != NO_ERROR) { |
| 675 | return err; |
| 676 | } |
| 677 | |
| 678 | NOISY(printf("Creating resources for package %s\n", |
| 679 | assets->getPackage().string())); |
| 680 | |
| 681 | ResourceTable table(bundle, String16(assets->getPackage())); |
| 682 | err = table.addIncludedResources(bundle, assets); |
| 683 | if (err != NO_ERROR) { |
| 684 | return err; |
| 685 | } |
| 686 | |
| 687 | NOISY(printf("Found %d included resource packages\n", (int)table.size())); |
| 688 | |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 689 | // Standard flags for compiled XML and optional UTF-8 encoding |
| 690 | int xmlFlags = XML_COMPILE_STANDARD_RESOURCE; |
| 691 | if (bundle->getUTF8()) { |
| 692 | xmlFlags |= XML_COMPILE_UTF8; |
| 693 | } |
| 694 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | // -------------------------------------------------------------- |
| 696 | // First, gather all resource information. |
| 697 | // -------------------------------------------------------------- |
| 698 | |
| 699 | // resType -> leafName -> group |
| 700 | KeyedVector<String8, sp<ResourceTypeSet> > *resources = |
| 701 | new KeyedVector<String8, sp<ResourceTypeSet> >; |
| 702 | collect_files(assets, resources); |
| 703 | |
| 704 | sp<ResourceTypeSet> drawables; |
| 705 | sp<ResourceTypeSet> layouts; |
| 706 | sp<ResourceTypeSet> anims; |
| 707 | sp<ResourceTypeSet> xmls; |
| 708 | sp<ResourceTypeSet> raws; |
| 709 | sp<ResourceTypeSet> colors; |
| 710 | sp<ResourceTypeSet> menus; |
| 711 | |
| 712 | ASSIGN_IT(drawable); |
| 713 | ASSIGN_IT(layout); |
| 714 | ASSIGN_IT(anim); |
| 715 | ASSIGN_IT(xml); |
| 716 | ASSIGN_IT(raw); |
| 717 | ASSIGN_IT(color); |
| 718 | ASSIGN_IT(menu); |
| 719 | |
| 720 | assets->setResources(resources); |
| 721 | // now go through any resource overlays and collect their files |
| 722 | sp<AaptAssets> current = assets->getOverlay(); |
| 723 | while(current.get()) { |
| 724 | KeyedVector<String8, sp<ResourceTypeSet> > *resources = |
| 725 | new KeyedVector<String8, sp<ResourceTypeSet> >; |
| 726 | current->setResources(resources); |
| 727 | collect_files(current, resources); |
| 728 | current = current->getOverlay(); |
| 729 | } |
| 730 | // apply the overlay files to the base set |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 731 | if (!applyFileOverlay(bundle, assets, drawables, "drawable") || |
| 732 | !applyFileOverlay(bundle, assets, layouts, "layout") || |
| 733 | !applyFileOverlay(bundle, assets, anims, "anim") || |
| 734 | !applyFileOverlay(bundle, assets, xmls, "xml") || |
| 735 | !applyFileOverlay(bundle, assets, raws, "raw") || |
| 736 | !applyFileOverlay(bundle, assets, colors, "color") || |
| 737 | !applyFileOverlay(bundle, assets, menus, "menu")) { |
Robert Greenwalt | fa5c7e1 | 2009-06-05 18:53:26 -0700 | [diff] [blame] | 738 | return UNKNOWN_ERROR; |
| 739 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | |
| 741 | bool hasErrors = false; |
| 742 | |
| 743 | if (drawables != NULL) { |
| 744 | err = preProcessImages(bundle, assets, drawables); |
| 745 | if (err == NO_ERROR) { |
| 746 | err = makeFileResources(bundle, assets, &table, drawables, "drawable"); |
| 747 | if (err != NO_ERROR) { |
| 748 | hasErrors = true; |
| 749 | } |
| 750 | } else { |
| 751 | hasErrors = true; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | if (layouts != NULL) { |
| 756 | err = makeFileResources(bundle, assets, &table, layouts, "layout"); |
| 757 | if (err != NO_ERROR) { |
| 758 | hasErrors = true; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | if (anims != NULL) { |
| 763 | err = makeFileResources(bundle, assets, &table, anims, "anim"); |
| 764 | if (err != NO_ERROR) { |
| 765 | hasErrors = true; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (xmls != NULL) { |
| 770 | err = makeFileResources(bundle, assets, &table, xmls, "xml"); |
| 771 | if (err != NO_ERROR) { |
| 772 | hasErrors = true; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | if (raws != NULL) { |
| 777 | err = makeFileResources(bundle, assets, &table, raws, "raw"); |
| 778 | if (err != NO_ERROR) { |
| 779 | hasErrors = true; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | // compile resources |
| 784 | current = assets; |
| 785 | while(current.get()) { |
| 786 | KeyedVector<String8, sp<ResourceTypeSet> > *resources = |
| 787 | current->getResources(); |
| 788 | |
| 789 | ssize_t index = resources->indexOfKey(String8("values")); |
| 790 | if (index >= 0) { |
| 791 | ResourceDirIterator it(resources->valueAt(index), String8("values")); |
| 792 | ssize_t res; |
| 793 | while ((res=it.next()) == NO_ERROR) { |
| 794 | sp<AaptFile> file = it.getFile(); |
| 795 | res = compileResourceFile(bundle, assets, file, it.getParams(), |
| 796 | (current!=assets), &table); |
| 797 | if (res != NO_ERROR) { |
| 798 | hasErrors = true; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | current = current->getOverlay(); |
| 803 | } |
| 804 | |
| 805 | if (colors != NULL) { |
| 806 | err = makeFileResources(bundle, assets, &table, colors, "color"); |
| 807 | if (err != NO_ERROR) { |
| 808 | hasErrors = true; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | if (menus != NULL) { |
| 813 | err = makeFileResources(bundle, assets, &table, menus, "menu"); |
| 814 | if (err != NO_ERROR) { |
| 815 | hasErrors = true; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | // -------------------------------------------------------------------- |
| 820 | // Assignment of resource IDs and initial generation of resource table. |
| 821 | // -------------------------------------------------------------------- |
| 822 | |
| 823 | if (table.hasResources()) { |
| 824 | sp<AaptFile> resFile(getResourceFile(assets)); |
| 825 | if (resFile == NULL) { |
| 826 | fprintf(stderr, "Error: unable to generate entry for resource data\n"); |
| 827 | return UNKNOWN_ERROR; |
| 828 | } |
| 829 | |
| 830 | err = table.assignResourceIds(); |
| 831 | if (err < NO_ERROR) { |
| 832 | return err; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | // -------------------------------------------------------------- |
| 837 | // Finally, we can now we can compile XML files, which may reference |
| 838 | // resources. |
| 839 | // -------------------------------------------------------------- |
| 840 | |
| 841 | if (layouts != NULL) { |
| 842 | ResourceDirIterator it(layouts, String8("layout")); |
| 843 | while ((err=it.next()) == NO_ERROR) { |
| 844 | String8 src = it.getFile()->getPrintableSource(); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 845 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | if (err == NO_ERROR) { |
| 847 | ResXMLTree block; |
| 848 | block.setTo(it.getFile()->getData(), it.getFile()->getSize(), true); |
| 849 | checkForIds(src, block); |
| 850 | } else { |
| 851 | hasErrors = true; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | if (err < NO_ERROR) { |
| 856 | hasErrors = true; |
| 857 | } |
| 858 | err = NO_ERROR; |
| 859 | } |
| 860 | |
| 861 | if (anims != NULL) { |
| 862 | ResourceDirIterator it(anims, String8("anim")); |
| 863 | while ((err=it.next()) == NO_ERROR) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 864 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | if (err != NO_ERROR) { |
| 866 | hasErrors = true; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | if (err < NO_ERROR) { |
| 871 | hasErrors = true; |
| 872 | } |
| 873 | err = NO_ERROR; |
| 874 | } |
| 875 | |
| 876 | if (xmls != NULL) { |
| 877 | ResourceDirIterator it(xmls, String8("xml")); |
| 878 | while ((err=it.next()) == NO_ERROR) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 879 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | if (err != NO_ERROR) { |
| 881 | hasErrors = true; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | if (err < NO_ERROR) { |
| 886 | hasErrors = true; |
| 887 | } |
| 888 | err = NO_ERROR; |
| 889 | } |
| 890 | |
| 891 | if (drawables != NULL) { |
| 892 | err = postProcessImages(assets, &table, drawables); |
| 893 | if (err != NO_ERROR) { |
| 894 | hasErrors = true; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | if (colors != NULL) { |
| 899 | ResourceDirIterator it(colors, String8("color")); |
| 900 | while ((err=it.next()) == NO_ERROR) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 901 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | if (err != NO_ERROR) { |
| 903 | hasErrors = true; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | if (err < NO_ERROR) { |
| 908 | hasErrors = true; |
| 909 | } |
| 910 | err = NO_ERROR; |
| 911 | } |
| 912 | |
| 913 | if (menus != NULL) { |
| 914 | ResourceDirIterator it(menus, String8("menu")); |
| 915 | while ((err=it.next()) == NO_ERROR) { |
| 916 | String8 src = it.getFile()->getPrintableSource(); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 917 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | if (err != NO_ERROR) { |
| 919 | hasErrors = true; |
| 920 | } |
| 921 | ResXMLTree block; |
| 922 | block.setTo(it.getFile()->getData(), it.getFile()->getSize(), true); |
| 923 | checkForIds(src, block); |
| 924 | } |
| 925 | |
| 926 | if (err < NO_ERROR) { |
| 927 | hasErrors = true; |
| 928 | } |
| 929 | err = NO_ERROR; |
| 930 | } |
| 931 | |
| 932 | const sp<AaptFile> manifestFile(androidManifestFile->getFiles().valueAt(0)); |
| 933 | String8 manifestPath(manifestFile->getPrintableSource()); |
| 934 | |
| 935 | // Perform a basic validation of the manifest file. This time we |
| 936 | // parse it with the comments intact, so that we can use them to |
| 937 | // generate java docs... so we are not going to write this one |
| 938 | // back out to the final manifest data. |
| 939 | err = compileXmlFile(assets, manifestFile, &table, |
| 940 | XML_COMPILE_ASSIGN_ATTRIBUTE_IDS |
| 941 | | XML_COMPILE_STRIP_WHITESPACE | XML_COMPILE_STRIP_RAW_VALUES); |
| 942 | if (err < NO_ERROR) { |
| 943 | return err; |
| 944 | } |
| 945 | ResXMLTree block; |
| 946 | block.setTo(manifestFile->getData(), manifestFile->getSize(), true); |
| 947 | String16 manifest16("manifest"); |
| 948 | String16 permission16("permission"); |
| 949 | String16 permission_group16("permission-group"); |
| 950 | String16 uses_permission16("uses-permission"); |
| 951 | String16 instrumentation16("instrumentation"); |
| 952 | String16 application16("application"); |
| 953 | String16 provider16("provider"); |
| 954 | String16 service16("service"); |
| 955 | String16 receiver16("receiver"); |
| 956 | String16 activity16("activity"); |
| 957 | String16 action16("action"); |
| 958 | String16 category16("category"); |
| 959 | String16 data16("scheme"); |
| 960 | const char* packageIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 961 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789"; |
| 962 | const char* packageIdentCharsWithTheStupid = "abcdefghijklmnopqrstuvwxyz" |
| 963 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; |
| 964 | const char* classIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 965 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$"; |
| 966 | const char* processIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 967 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:"; |
| 968 | const char* authoritiesIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 969 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;"; |
| 970 | const char* typeIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 971 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+"; |
| 972 | const char* schemeIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 973 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; |
| 974 | ResXMLTree::event_code_t code; |
| 975 | sp<AaptSymbols> permissionSymbols; |
| 976 | sp<AaptSymbols> permissionGroupSymbols; |
| 977 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| 978 | && code > ResXMLTree::BAD_DOCUMENT) { |
| 979 | if (code == ResXMLTree::START_TAG) { |
| 980 | size_t len; |
| 981 | if (block.getElementNamespace(&len) != NULL) { |
| 982 | continue; |
| 983 | } |
| 984 | if (strcmp16(block.getElementName(&len), manifest16.string()) == 0) { |
| 985 | if (validateAttr(manifestPath, block, NULL, "package", |
| 986 | packageIdentChars, true) != ATTR_OKAY) { |
| 987 | hasErrors = true; |
| 988 | } |
| 989 | } else if (strcmp16(block.getElementName(&len), permission16.string()) == 0 |
| 990 | || strcmp16(block.getElementName(&len), permission_group16.string()) == 0) { |
| 991 | const bool isGroup = strcmp16(block.getElementName(&len), |
| 992 | permission_group16.string()) == 0; |
| 993 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", |
| 994 | isGroup ? packageIdentCharsWithTheStupid |
| 995 | : packageIdentChars, true) != ATTR_OKAY) { |
| 996 | hasErrors = true; |
| 997 | } |
| 998 | SourcePos srcPos(manifestPath, block.getLineNumber()); |
| 999 | sp<AaptSymbols> syms; |
| 1000 | if (!isGroup) { |
| 1001 | syms = permissionSymbols; |
| 1002 | if (syms == NULL) { |
| 1003 | sp<AaptSymbols> symbols = |
| 1004 | assets->getSymbolsFor(String8("Manifest")); |
| 1005 | syms = permissionSymbols = symbols->addNestedSymbol( |
| 1006 | String8("permission"), srcPos); |
| 1007 | } |
| 1008 | } else { |
| 1009 | syms = permissionGroupSymbols; |
| 1010 | if (syms == NULL) { |
| 1011 | sp<AaptSymbols> symbols = |
| 1012 | assets->getSymbolsFor(String8("Manifest")); |
| 1013 | syms = permissionGroupSymbols = symbols->addNestedSymbol( |
| 1014 | String8("permission_group"), srcPos); |
| 1015 | } |
| 1016 | } |
| 1017 | size_t len; |
| 1018 | ssize_t index = block.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE, "name"); |
| 1019 | const uint16_t* id = block.getAttributeStringValue(index, &len); |
| 1020 | if (id == NULL) { |
| 1021 | fprintf(stderr, "%s:%d: missing name attribute in element <%s>.\n", |
| 1022 | manifestPath.string(), block.getLineNumber(), |
| 1023 | String8(block.getElementName(&len)).string()); |
| 1024 | hasErrors = true; |
| 1025 | break; |
| 1026 | } |
| 1027 | String8 idStr(id); |
| 1028 | char* p = idStr.lockBuffer(idStr.size()); |
| 1029 | char* e = p + idStr.size(); |
| 1030 | bool begins_with_digit = true; // init to true so an empty string fails |
| 1031 | while (e > p) { |
| 1032 | e--; |
| 1033 | if (*e >= '0' && *e <= '9') { |
| 1034 | begins_with_digit = true; |
| 1035 | continue; |
| 1036 | } |
| 1037 | if ((*e >= 'a' && *e <= 'z') || |
| 1038 | (*e >= 'A' && *e <= 'Z') || |
| 1039 | (*e == '_')) { |
| 1040 | begins_with_digit = false; |
| 1041 | continue; |
| 1042 | } |
| 1043 | if (isGroup && (*e == '-')) { |
| 1044 | *e = '_'; |
| 1045 | begins_with_digit = false; |
| 1046 | continue; |
| 1047 | } |
| 1048 | e++; |
| 1049 | break; |
| 1050 | } |
| 1051 | idStr.unlockBuffer(); |
| 1052 | // verify that we stopped because we hit a period or |
| 1053 | // the beginning of the string, and that the |
| 1054 | // identifier didn't begin with a digit. |
| 1055 | if (begins_with_digit || (e != p && *(e-1) != '.')) { |
| 1056 | fprintf(stderr, |
| 1057 | "%s:%d: Permission name <%s> is not a valid Java symbol\n", |
| 1058 | manifestPath.string(), block.getLineNumber(), idStr.string()); |
| 1059 | hasErrors = true; |
| 1060 | } |
| 1061 | syms->addStringSymbol(String8(e), idStr, srcPos); |
| 1062 | const uint16_t* cmt = block.getComment(&len); |
| 1063 | if (cmt != NULL && *cmt != 0) { |
| 1064 | //printf("Comment of %s: %s\n", String8(e).string(), |
| 1065 | // String8(cmt).string()); |
| 1066 | syms->appendComment(String8(e), String16(cmt), srcPos); |
| 1067 | } else { |
| 1068 | //printf("No comment for %s\n", String8(e).string()); |
| 1069 | } |
| 1070 | syms->makeSymbolPublic(String8(e), srcPos); |
| 1071 | } else if (strcmp16(block.getElementName(&len), uses_permission16.string()) == 0) { |
| 1072 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", |
| 1073 | packageIdentChars, true) != ATTR_OKAY) { |
| 1074 | hasErrors = true; |
| 1075 | } |
| 1076 | } else if (strcmp16(block.getElementName(&len), instrumentation16.string()) == 0) { |
| 1077 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", |
| 1078 | classIdentChars, true) != ATTR_OKAY) { |
| 1079 | hasErrors = true; |
| 1080 | } |
| 1081 | if (validateAttr(manifestPath, block, |
| 1082 | RESOURCES_ANDROID_NAMESPACE, "targetPackage", |
| 1083 | packageIdentChars, true) != ATTR_OKAY) { |
| 1084 | hasErrors = true; |
| 1085 | } |
| 1086 | } else if (strcmp16(block.getElementName(&len), application16.string()) == 0) { |
| 1087 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", |
| 1088 | classIdentChars, false) != ATTR_OKAY) { |
| 1089 | hasErrors = true; |
| 1090 | } |
| 1091 | if (validateAttr(manifestPath, block, |
| 1092 | RESOURCES_ANDROID_NAMESPACE, "permission", |
| 1093 | packageIdentChars, false) != ATTR_OKAY) { |
| 1094 | hasErrors = true; |
| 1095 | } |
| 1096 | if (validateAttr(manifestPath, block, |
| 1097 | RESOURCES_ANDROID_NAMESPACE, "process", |
| 1098 | processIdentChars, false) != ATTR_OKAY) { |
| 1099 | hasErrors = true; |
| 1100 | } |
| 1101 | if (validateAttr(manifestPath, block, |
| 1102 | RESOURCES_ANDROID_NAMESPACE, "taskAffinity", |
| 1103 | processIdentChars, false) != ATTR_OKAY) { |
| 1104 | hasErrors = true; |
| 1105 | } |
| 1106 | } else if (strcmp16(block.getElementName(&len), provider16.string()) == 0) { |
| 1107 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", |
| 1108 | classIdentChars, true) != ATTR_OKAY) { |
| 1109 | hasErrors = true; |
| 1110 | } |
| 1111 | if (validateAttr(manifestPath, block, |
| 1112 | RESOURCES_ANDROID_NAMESPACE, "authorities", |
| 1113 | authoritiesIdentChars, true) != ATTR_OKAY) { |
| 1114 | hasErrors = true; |
| 1115 | } |
| 1116 | if (validateAttr(manifestPath, block, |
| 1117 | RESOURCES_ANDROID_NAMESPACE, "permission", |
| 1118 | packageIdentChars, false) != ATTR_OKAY) { |
| 1119 | hasErrors = true; |
| 1120 | } |
| 1121 | if (validateAttr(manifestPath, block, |
| 1122 | RESOURCES_ANDROID_NAMESPACE, "process", |
| 1123 | processIdentChars, false) != ATTR_OKAY) { |
| 1124 | hasErrors = true; |
| 1125 | } |
| 1126 | } else if (strcmp16(block.getElementName(&len), service16.string()) == 0 |
| 1127 | || strcmp16(block.getElementName(&len), receiver16.string()) == 0 |
| 1128 | || strcmp16(block.getElementName(&len), activity16.string()) == 0) { |
| 1129 | if (validateAttr(manifestPath, block, RESOURCES_ANDROID_NAMESPACE, "name", |
| 1130 | classIdentChars, true) != ATTR_OKAY) { |
| 1131 | hasErrors = true; |
| 1132 | } |
| 1133 | if (validateAttr(manifestPath, block, |
| 1134 | RESOURCES_ANDROID_NAMESPACE, "permission", |
| 1135 | packageIdentChars, false) != ATTR_OKAY) { |
| 1136 | hasErrors = true; |
| 1137 | } |
| 1138 | if (validateAttr(manifestPath, block, |
| 1139 | RESOURCES_ANDROID_NAMESPACE, "process", |
| 1140 | processIdentChars, false) != ATTR_OKAY) { |
| 1141 | hasErrors = true; |
| 1142 | } |
| 1143 | if (validateAttr(manifestPath, block, |
| 1144 | RESOURCES_ANDROID_NAMESPACE, "taskAffinity", |
| 1145 | processIdentChars, false) != ATTR_OKAY) { |
| 1146 | hasErrors = true; |
| 1147 | } |
| 1148 | } else if (strcmp16(block.getElementName(&len), action16.string()) == 0 |
| 1149 | || strcmp16(block.getElementName(&len), category16.string()) == 0) { |
| 1150 | if (validateAttr(manifestPath, block, |
| 1151 | RESOURCES_ANDROID_NAMESPACE, "name", |
| 1152 | packageIdentChars, true) != ATTR_OKAY) { |
| 1153 | hasErrors = true; |
| 1154 | } |
| 1155 | } else if (strcmp16(block.getElementName(&len), data16.string()) == 0) { |
| 1156 | if (validateAttr(manifestPath, block, |
| 1157 | RESOURCES_ANDROID_NAMESPACE, "mimeType", |
| 1158 | typeIdentChars, true) != ATTR_OKAY) { |
| 1159 | hasErrors = true; |
| 1160 | } |
| 1161 | if (validateAttr(manifestPath, block, |
| 1162 | RESOURCES_ANDROID_NAMESPACE, "scheme", |
| 1163 | schemeIdentChars, true) != ATTR_OKAY) { |
| 1164 | hasErrors = true; |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | if (table.validateLocalizations()) { |
| 1171 | hasErrors = true; |
| 1172 | } |
| 1173 | |
| 1174 | if (hasErrors) { |
| 1175 | return UNKNOWN_ERROR; |
| 1176 | } |
| 1177 | |
| 1178 | // Generate final compiled manifest file. |
| 1179 | manifestFile->clearData(); |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 1180 | sp<XMLNode> manifestTree = XMLNode::parse(manifestFile); |
| 1181 | if (manifestTree == NULL) { |
| 1182 | return UNKNOWN_ERROR; |
| 1183 | } |
| 1184 | err = massageManifest(bundle, manifestTree); |
| 1185 | if (err < NO_ERROR) { |
| 1186 | return err; |
| 1187 | } |
| 1188 | err = compileXmlFile(assets, manifestTree, manifestFile, &table); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | if (err < NO_ERROR) { |
| 1190 | return err; |
| 1191 | } |
| 1192 | |
| 1193 | //block.restart(); |
| 1194 | //printXMLBlock(&block); |
| 1195 | |
| 1196 | // -------------------------------------------------------------- |
| 1197 | // Generate the final resource table. |
| 1198 | // Re-flatten because we may have added new resource IDs |
| 1199 | // -------------------------------------------------------------- |
| 1200 | |
| 1201 | if (table.hasResources()) { |
| 1202 | sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); |
| 1203 | err = table.addSymbols(symbols); |
| 1204 | if (err < NO_ERROR) { |
| 1205 | return err; |
| 1206 | } |
| 1207 | |
| 1208 | sp<AaptFile> resFile(getResourceFile(assets)); |
| 1209 | if (resFile == NULL) { |
| 1210 | fprintf(stderr, "Error: unable to generate entry for resource data\n"); |
| 1211 | return UNKNOWN_ERROR; |
| 1212 | } |
| 1213 | |
| 1214 | err = table.flatten(bundle, resFile); |
| 1215 | if (err < NO_ERROR) { |
| 1216 | return err; |
| 1217 | } |
| 1218 | |
| 1219 | if (bundle->getPublicOutputFile()) { |
| 1220 | FILE* fp = fopen(bundle->getPublicOutputFile(), "w+"); |
| 1221 | if (fp == NULL) { |
| 1222 | fprintf(stderr, "ERROR: Unable to open public definitions output file %s: %s\n", |
| 1223 | (const char*)bundle->getPublicOutputFile(), strerror(errno)); |
| 1224 | return UNKNOWN_ERROR; |
| 1225 | } |
| 1226 | if (bundle->getVerbose()) { |
| 1227 | printf(" Writing public definitions to %s.\n", bundle->getPublicOutputFile()); |
| 1228 | } |
| 1229 | table.writePublicDefinitions(String16(assets->getPackage()), fp); |
Marco Nelissen | 6a1fade | 2009-04-20 16:16:01 -0700 | [diff] [blame] | 1230 | fclose(fp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1231 | } |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 1232 | #if 0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1233 | NOISY( |
| 1234 | ResTable rt; |
| 1235 | rt.add(resFile->getData(), resFile->getSize(), NULL); |
| 1236 | printf("Generated resources:\n"); |
| 1237 | rt.print(); |
| 1238 | ) |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 1239 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1240 | // These resources are now considered to be a part of the included |
| 1241 | // resources, for others to reference. |
| 1242 | err = assets->addIncludedResources(resFile); |
| 1243 | if (err < NO_ERROR) { |
| 1244 | fprintf(stderr, "ERROR: Unable to parse generated resources, aborting.\n"); |
| 1245 | return err; |
| 1246 | } |
| 1247 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | return err; |
| 1249 | } |
| 1250 | |
| 1251 | static const char* getIndentSpace(int indent) |
| 1252 | { |
| 1253 | static const char whitespace[] = |
| 1254 | " "; |
| 1255 | |
| 1256 | return whitespace + sizeof(whitespace) - 1 - indent*4; |
| 1257 | } |
| 1258 | |
| 1259 | static status_t fixupSymbol(String16* inoutSymbol) |
| 1260 | { |
| 1261 | inoutSymbol->replaceAll('.', '_'); |
| 1262 | inoutSymbol->replaceAll(':', '_'); |
| 1263 | return NO_ERROR; |
| 1264 | } |
| 1265 | |
| 1266 | static String16 getAttributeComment(const sp<AaptAssets>& assets, |
| 1267 | const String8& name, |
| 1268 | String16* outTypeComment = NULL) |
| 1269 | { |
| 1270 | sp<AaptSymbols> asym = assets->getSymbolsFor(String8("R")); |
| 1271 | if (asym != NULL) { |
| 1272 | //printf("Got R symbols!\n"); |
| 1273 | asym = asym->getNestedSymbols().valueFor(String8("attr")); |
| 1274 | if (asym != NULL) { |
| 1275 | //printf("Got attrs symbols! comment %s=%s\n", |
| 1276 | // name.string(), String8(asym->getComment(name)).string()); |
| 1277 | if (outTypeComment != NULL) { |
| 1278 | *outTypeComment = asym->getTypeComment(name); |
| 1279 | } |
| 1280 | return asym->getComment(name); |
| 1281 | } |
| 1282 | } |
| 1283 | return String16(); |
| 1284 | } |
| 1285 | |
| 1286 | static status_t writeLayoutClasses( |
| 1287 | FILE* fp, const sp<AaptAssets>& assets, |
| 1288 | const sp<AaptSymbols>& symbols, int indent, bool includePrivate) |
| 1289 | { |
| 1290 | const char* indentStr = getIndentSpace(indent); |
| 1291 | if (!includePrivate) { |
| 1292 | fprintf(fp, "%s/** @doconly */\n", indentStr); |
| 1293 | } |
| 1294 | fprintf(fp, "%spublic static final class styleable {\n", indentStr); |
| 1295 | indent++; |
| 1296 | |
| 1297 | String16 attr16("attr"); |
| 1298 | String16 package16(assets->getPackage()); |
| 1299 | |
| 1300 | indentStr = getIndentSpace(indent); |
| 1301 | bool hasErrors = false; |
| 1302 | |
| 1303 | size_t i; |
| 1304 | size_t N = symbols->getNestedSymbols().size(); |
| 1305 | for (i=0; i<N; i++) { |
| 1306 | sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); |
| 1307 | String16 nclassName16(symbols->getNestedSymbols().keyAt(i)); |
| 1308 | String8 realClassName(nclassName16); |
| 1309 | if (fixupSymbol(&nclassName16) != NO_ERROR) { |
| 1310 | hasErrors = true; |
| 1311 | } |
| 1312 | String8 nclassName(nclassName16); |
| 1313 | |
| 1314 | SortedVector<uint32_t> idents; |
| 1315 | Vector<uint32_t> origOrder; |
| 1316 | Vector<bool> publicFlags; |
| 1317 | |
| 1318 | size_t a; |
| 1319 | size_t NA = nsymbols->getSymbols().size(); |
| 1320 | for (a=0; a<NA; a++) { |
| 1321 | const AaptSymbolEntry& sym(nsymbols->getSymbols().valueAt(a)); |
| 1322 | int32_t code = sym.typeCode == AaptSymbolEntry::TYPE_INT32 |
| 1323 | ? sym.int32Val : 0; |
| 1324 | bool isPublic = true; |
| 1325 | if (code == 0) { |
| 1326 | String16 name16(sym.name); |
| 1327 | uint32_t typeSpecFlags; |
| 1328 | code = assets->getIncludedResources().identifierForName( |
| 1329 | name16.string(), name16.size(), |
| 1330 | attr16.string(), attr16.size(), |
| 1331 | package16.string(), package16.size(), &typeSpecFlags); |
| 1332 | if (code == 0) { |
| 1333 | fprintf(stderr, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n", |
| 1334 | nclassName.string(), sym.name.string()); |
| 1335 | hasErrors = true; |
| 1336 | } |
| 1337 | isPublic = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; |
| 1338 | } |
| 1339 | idents.add(code); |
| 1340 | origOrder.add(code); |
| 1341 | publicFlags.add(isPublic); |
| 1342 | } |
| 1343 | |
| 1344 | NA = idents.size(); |
| 1345 | |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1346 | bool deprecated = false; |
| 1347 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1348 | String16 comment = symbols->getComment(realClassName); |
| 1349 | fprintf(fp, "%s/** ", indentStr); |
| 1350 | if (comment.size() > 0) { |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1351 | String8 cmt(comment); |
| 1352 | fprintf(fp, "%s\n", cmt.string()); |
| 1353 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 1354 | deprecated = true; |
| 1355 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | } else { |
| 1357 | fprintf(fp, "Attributes that can be used with a %s.\n", nclassName.string()); |
| 1358 | } |
| 1359 | bool hasTable = false; |
| 1360 | for (a=0; a<NA; a++) { |
| 1361 | ssize_t pos = idents.indexOf(origOrder.itemAt(a)); |
| 1362 | if (pos >= 0) { |
| 1363 | if (!hasTable) { |
| 1364 | hasTable = true; |
| 1365 | fprintf(fp, |
| 1366 | "%s <p>Includes the following attributes:</p>\n" |
Dirk Dougherty | 59ad275 | 2009-11-03 15:33:37 -0800 | [diff] [blame] | 1367 | "%s <table>\n" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1368 | "%s <colgroup align=\"left\" />\n" |
| 1369 | "%s <colgroup align=\"left\" />\n" |
Dirk Dougherty | 59ad275 | 2009-11-03 15:33:37 -0800 | [diff] [blame] | 1370 | "%s <tr><th>Attribute</th><th>Description</th></tr>\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1371 | indentStr, |
| 1372 | indentStr, |
| 1373 | indentStr, |
| 1374 | indentStr, |
| 1375 | indentStr); |
| 1376 | } |
| 1377 | const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); |
| 1378 | if (!publicFlags.itemAt(a) && !includePrivate) { |
| 1379 | continue; |
| 1380 | } |
| 1381 | String8 name8(sym.name); |
| 1382 | String16 comment(sym.comment); |
| 1383 | if (comment.size() <= 0) { |
| 1384 | comment = getAttributeComment(assets, name8); |
| 1385 | } |
| 1386 | if (comment.size() > 0) { |
| 1387 | const char16_t* p = comment.string(); |
| 1388 | while (*p != 0 && *p != '.') { |
| 1389 | if (*p == '{') { |
| 1390 | while (*p != 0 && *p != '}') { |
| 1391 | p++; |
| 1392 | } |
| 1393 | } else { |
| 1394 | p++; |
| 1395 | } |
| 1396 | } |
| 1397 | if (*p == '.') { |
| 1398 | p++; |
| 1399 | } |
| 1400 | comment = String16(comment.string(), p-comment.string()); |
| 1401 | } |
| 1402 | String16 name(name8); |
| 1403 | fixupSymbol(&name); |
Dirk Dougherty | 59ad275 | 2009-11-03 15:33:37 -0800 | [diff] [blame] | 1404 | fprintf(fp, "%s <tr><td><code>{@link #%s_%s %s:%s}</code></td><td>%s</td></tr>\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | indentStr, nclassName.string(), |
| 1406 | String8(name).string(), |
| 1407 | assets->getPackage().string(), |
| 1408 | String8(name).string(), |
| 1409 | String8(comment).string()); |
| 1410 | } |
| 1411 | } |
| 1412 | if (hasTable) { |
| 1413 | fprintf(fp, "%s </table>\n", indentStr); |
| 1414 | } |
| 1415 | for (a=0; a<NA; a++) { |
| 1416 | ssize_t pos = idents.indexOf(origOrder.itemAt(a)); |
| 1417 | if (pos >= 0) { |
| 1418 | const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); |
| 1419 | if (!publicFlags.itemAt(a) && !includePrivate) { |
| 1420 | continue; |
| 1421 | } |
| 1422 | String16 name(sym.name); |
| 1423 | fixupSymbol(&name); |
| 1424 | fprintf(fp, "%s @see #%s_%s\n", |
| 1425 | indentStr, nclassName.string(), |
| 1426 | String8(name).string()); |
| 1427 | } |
| 1428 | } |
| 1429 | fprintf(fp, "%s */\n", getIndentSpace(indent)); |
| 1430 | |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1431 | if (deprecated) { |
| 1432 | fprintf(fp, "%s@Deprecated\n", indentStr); |
| 1433 | } |
| 1434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | fprintf(fp, |
| 1436 | "%spublic static final int[] %s = {\n" |
| 1437 | "%s", |
| 1438 | indentStr, nclassName.string(), |
| 1439 | getIndentSpace(indent+1)); |
| 1440 | |
| 1441 | for (a=0; a<NA; a++) { |
| 1442 | if (a != 0) { |
| 1443 | if ((a&3) == 0) { |
| 1444 | fprintf(fp, ",\n%s", getIndentSpace(indent+1)); |
| 1445 | } else { |
| 1446 | fprintf(fp, ", "); |
| 1447 | } |
| 1448 | } |
| 1449 | fprintf(fp, "0x%08x", idents[a]); |
| 1450 | } |
| 1451 | |
| 1452 | fprintf(fp, "\n%s};\n", indentStr); |
| 1453 | |
| 1454 | for (a=0; a<NA; a++) { |
| 1455 | ssize_t pos = idents.indexOf(origOrder.itemAt(a)); |
| 1456 | if (pos >= 0) { |
| 1457 | const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); |
| 1458 | if (!publicFlags.itemAt(a) && !includePrivate) { |
| 1459 | continue; |
| 1460 | } |
| 1461 | String8 name8(sym.name); |
| 1462 | String16 comment(sym.comment); |
| 1463 | String16 typeComment; |
| 1464 | if (comment.size() <= 0) { |
| 1465 | comment = getAttributeComment(assets, name8, &typeComment); |
| 1466 | } else { |
| 1467 | getAttributeComment(assets, name8, &typeComment); |
| 1468 | } |
| 1469 | String16 name(name8); |
| 1470 | if (fixupSymbol(&name) != NO_ERROR) { |
| 1471 | hasErrors = true; |
| 1472 | } |
| 1473 | |
| 1474 | uint32_t typeSpecFlags = 0; |
| 1475 | String16 name16(sym.name); |
| 1476 | assets->getIncludedResources().identifierForName( |
| 1477 | name16.string(), name16.size(), |
| 1478 | attr16.string(), attr16.size(), |
| 1479 | package16.string(), package16.size(), &typeSpecFlags); |
| 1480 | //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(), |
| 1481 | // String8(attr16).string(), String8(name16).string(), typeSpecFlags); |
| 1482 | const bool pub = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1483 | |
| 1484 | bool deprecated = false; |
| 1485 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1486 | fprintf(fp, "%s/**\n", indentStr); |
| 1487 | if (comment.size() > 0) { |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1488 | String8 cmt(comment); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1489 | fprintf(fp, "%s <p>\n%s @attr description\n", indentStr, indentStr); |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1490 | fprintf(fp, "%s %s\n", indentStr, cmt.string()); |
| 1491 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 1492 | deprecated = true; |
| 1493 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1494 | } else { |
| 1495 | fprintf(fp, |
| 1496 | "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n" |
| 1497 | "%s attribute's value can be found in the {@link #%s} array.\n", |
| 1498 | indentStr, |
| 1499 | pub ? assets->getPackage().string() |
| 1500 | : assets->getSymbolsPrivatePackage().string(), |
| 1501 | String8(name).string(), |
| 1502 | indentStr, nclassName.string()); |
| 1503 | } |
| 1504 | if (typeComment.size() > 0) { |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1505 | String8 cmt(typeComment); |
| 1506 | fprintf(fp, "\n\n%s %s\n", indentStr, cmt.string()); |
| 1507 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 1508 | deprecated = true; |
| 1509 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | } |
| 1511 | if (comment.size() > 0) { |
| 1512 | if (pub) { |
| 1513 | fprintf(fp, |
| 1514 | "%s <p>This corresponds to the global attribute" |
| 1515 | "%s resource symbol {@link %s.R.attr#%s}.\n", |
| 1516 | indentStr, indentStr, |
| 1517 | assets->getPackage().string(), |
| 1518 | String8(name).string()); |
| 1519 | } else { |
| 1520 | fprintf(fp, |
| 1521 | "%s <p>This is a private symbol.\n", indentStr); |
| 1522 | } |
| 1523 | } |
| 1524 | fprintf(fp, "%s @attr name %s:%s\n", indentStr, |
| 1525 | "android", String8(name).string()); |
| 1526 | fprintf(fp, "%s*/\n", indentStr); |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1527 | if (deprecated) { |
| 1528 | fprintf(fp, "%s@Deprecated\n", indentStr); |
| 1529 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1530 | fprintf(fp, |
| 1531 | "%spublic static final int %s_%s = %d;\n", |
| 1532 | indentStr, nclassName.string(), |
| 1533 | String8(name).string(), (int)pos); |
| 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | indent--; |
| 1539 | fprintf(fp, "%s};\n", getIndentSpace(indent)); |
| 1540 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; |
| 1541 | } |
| 1542 | |
| 1543 | static status_t writeSymbolClass( |
| 1544 | FILE* fp, const sp<AaptAssets>& assets, bool includePrivate, |
| 1545 | const sp<AaptSymbols>& symbols, const String8& className, int indent) |
| 1546 | { |
| 1547 | fprintf(fp, "%spublic %sfinal class %s {\n", |
| 1548 | getIndentSpace(indent), |
| 1549 | indent != 0 ? "static " : "", className.string()); |
| 1550 | indent++; |
| 1551 | |
| 1552 | size_t i; |
| 1553 | status_t err = NO_ERROR; |
| 1554 | |
| 1555 | size_t N = symbols->getSymbols().size(); |
| 1556 | for (i=0; i<N; i++) { |
| 1557 | const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); |
| 1558 | if (sym.typeCode != AaptSymbolEntry::TYPE_INT32) { |
| 1559 | continue; |
| 1560 | } |
| 1561 | if (!includePrivate && !sym.isPublic) { |
| 1562 | continue; |
| 1563 | } |
| 1564 | String16 name(sym.name); |
| 1565 | String8 realName(name); |
| 1566 | if (fixupSymbol(&name) != NO_ERROR) { |
| 1567 | return UNKNOWN_ERROR; |
| 1568 | } |
| 1569 | String16 comment(sym.comment); |
| 1570 | bool haveComment = false; |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1571 | bool deprecated = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | if (comment.size() > 0) { |
| 1573 | haveComment = true; |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1574 | String8 cmt(comment); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | fprintf(fp, |
| 1576 | "%s/** %s\n", |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1577 | getIndentSpace(indent), cmt.string()); |
| 1578 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 1579 | deprecated = true; |
| 1580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1581 | } else if (sym.isPublic && !includePrivate) { |
| 1582 | sym.sourcePos.warning("No comment for public symbol %s:%s/%s", |
| 1583 | assets->getPackage().string(), className.string(), |
| 1584 | String8(sym.name).string()); |
| 1585 | } |
| 1586 | String16 typeComment(sym.typeComment); |
| 1587 | if (typeComment.size() > 0) { |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1588 | String8 cmt(typeComment); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | if (!haveComment) { |
| 1590 | haveComment = true; |
| 1591 | fprintf(fp, |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1592 | "%s/** %s\n", getIndentSpace(indent), cmt.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | } else { |
| 1594 | fprintf(fp, |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1595 | "%s %s\n", getIndentSpace(indent), cmt.string()); |
| 1596 | } |
| 1597 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 1598 | deprecated = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | if (haveComment) { |
| 1602 | fprintf(fp,"%s */\n", getIndentSpace(indent)); |
| 1603 | } |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1604 | if (deprecated) { |
| 1605 | fprintf(fp, "%s@Deprecated\n", getIndentSpace(indent)); |
| 1606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | fprintf(fp, "%spublic static final int %s=0x%08x;\n", |
| 1608 | getIndentSpace(indent), |
| 1609 | String8(name).string(), (int)sym.int32Val); |
| 1610 | } |
| 1611 | |
| 1612 | for (i=0; i<N; i++) { |
| 1613 | const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); |
| 1614 | if (sym.typeCode != AaptSymbolEntry::TYPE_STRING) { |
| 1615 | continue; |
| 1616 | } |
| 1617 | if (!includePrivate && !sym.isPublic) { |
| 1618 | continue; |
| 1619 | } |
| 1620 | String16 name(sym.name); |
| 1621 | if (fixupSymbol(&name) != NO_ERROR) { |
| 1622 | return UNKNOWN_ERROR; |
| 1623 | } |
| 1624 | String16 comment(sym.comment); |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1625 | bool deprecated = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | if (comment.size() > 0) { |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1627 | String8 cmt(comment); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | fprintf(fp, |
| 1629 | "%s/** %s\n" |
| 1630 | "%s */\n", |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1631 | getIndentSpace(indent), cmt.string(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1632 | getIndentSpace(indent)); |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1633 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 1634 | deprecated = true; |
| 1635 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | } else if (sym.isPublic && !includePrivate) { |
| 1637 | sym.sourcePos.warning("No comment for public symbol %s:%s/%s", |
| 1638 | assets->getPackage().string(), className.string(), |
| 1639 | String8(sym.name).string()); |
| 1640 | } |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1641 | if (deprecated) { |
| 1642 | fprintf(fp, "%s@Deprecated\n", getIndentSpace(indent)); |
| 1643 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1644 | fprintf(fp, "%spublic static final String %s=\"%s\";\n", |
| 1645 | getIndentSpace(indent), |
| 1646 | String8(name).string(), sym.stringVal.string()); |
| 1647 | } |
| 1648 | |
| 1649 | sp<AaptSymbols> styleableSymbols; |
| 1650 | |
| 1651 | N = symbols->getNestedSymbols().size(); |
| 1652 | for (i=0; i<N; i++) { |
| 1653 | sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); |
| 1654 | String8 nclassName(symbols->getNestedSymbols().keyAt(i)); |
| 1655 | if (nclassName == "styleable") { |
| 1656 | styleableSymbols = nsymbols; |
| 1657 | } else { |
| 1658 | err = writeSymbolClass(fp, assets, includePrivate, nsymbols, nclassName, indent); |
| 1659 | } |
| 1660 | if (err != NO_ERROR) { |
| 1661 | return err; |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | if (styleableSymbols != NULL) { |
| 1666 | err = writeLayoutClasses(fp, assets, styleableSymbols, indent, includePrivate); |
| 1667 | if (err != NO_ERROR) { |
| 1668 | return err; |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | indent--; |
| 1673 | fprintf(fp, "%s}\n", getIndentSpace(indent)); |
| 1674 | return NO_ERROR; |
| 1675 | } |
| 1676 | |
| 1677 | status_t writeResourceSymbols(Bundle* bundle, const sp<AaptAssets>& assets, |
| 1678 | const String8& package, bool includePrivate) |
| 1679 | { |
| 1680 | if (!bundle->getRClassDir()) { |
| 1681 | return NO_ERROR; |
| 1682 | } |
| 1683 | |
| 1684 | const size_t N = assets->getSymbols().size(); |
| 1685 | for (size_t i=0; i<N; i++) { |
| 1686 | sp<AaptSymbols> symbols = assets->getSymbols().valueAt(i); |
| 1687 | String8 className(assets->getSymbols().keyAt(i)); |
| 1688 | String8 dest(bundle->getRClassDir()); |
| 1689 | if (bundle->getMakePackageDirs()) { |
| 1690 | String8 pkg(package); |
| 1691 | const char* last = pkg.string(); |
| 1692 | const char* s = last-1; |
| 1693 | do { |
| 1694 | s++; |
| 1695 | if (s > last && (*s == '.' || *s == 0)) { |
| 1696 | String8 part(last, s-last); |
| 1697 | dest.appendPath(part); |
| 1698 | #ifdef HAVE_MS_C_RUNTIME |
| 1699 | _mkdir(dest.string()); |
| 1700 | #else |
| 1701 | mkdir(dest.string(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); |
| 1702 | #endif |
| 1703 | last = s+1; |
| 1704 | } |
| 1705 | } while (*s); |
| 1706 | } |
| 1707 | dest.appendPath(className); |
| 1708 | dest.append(".java"); |
| 1709 | FILE* fp = fopen(dest.string(), "w+"); |
| 1710 | if (fp == NULL) { |
| 1711 | fprintf(stderr, "ERROR: Unable to open class file %s: %s\n", |
| 1712 | dest.string(), strerror(errno)); |
| 1713 | return UNKNOWN_ERROR; |
| 1714 | } |
| 1715 | if (bundle->getVerbose()) { |
| 1716 | printf(" Writing symbols for class %s.\n", className.string()); |
| 1717 | } |
| 1718 | |
| 1719 | fprintf(fp, |
| 1720 | "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" |
| 1721 | " *\n" |
| 1722 | " * This class was automatically generated by the\n" |
| 1723 | " * aapt tool from the resource data it found. It\n" |
| 1724 | " * should not be modified by hand.\n" |
| 1725 | " */\n" |
| 1726 | "\n" |
| 1727 | "package %s;\n\n", package.string()); |
| 1728 | |
| 1729 | status_t err = writeSymbolClass(fp, assets, includePrivate, symbols, className, 0); |
| 1730 | if (err != NO_ERROR) { |
| 1731 | return err; |
| 1732 | } |
| 1733 | fclose(fp); |
| 1734 | } |
| 1735 | |
| 1736 | return NO_ERROR; |
| 1737 | } |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 1738 | |
| 1739 | |
| 1740 | |
| 1741 | class ProguardKeepSet |
| 1742 | { |
| 1743 | public: |
| 1744 | // { rule --> { file locations } } |
| 1745 | KeyedVector<String8, SortedVector<String8> > rules; |
| 1746 | |
| 1747 | void add(const String8& rule, const String8& where); |
| 1748 | }; |
| 1749 | |
| 1750 | void ProguardKeepSet::add(const String8& rule, const String8& where) |
| 1751 | { |
| 1752 | ssize_t index = rules.indexOfKey(rule); |
| 1753 | if (index < 0) { |
| 1754 | index = rules.add(rule, SortedVector<String8>()); |
| 1755 | } |
| 1756 | rules.editValueAt(index).add(where); |
| 1757 | } |
| 1758 | |
| 1759 | status_t |
| 1760 | writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp<AaptAssets>& assets) |
| 1761 | { |
| 1762 | status_t err; |
| 1763 | ResXMLTree tree; |
| 1764 | size_t len; |
| 1765 | ResXMLTree::event_code_t code; |
| 1766 | int depth = 0; |
| 1767 | bool inApplication = false; |
| 1768 | String8 error; |
| 1769 | sp<AaptGroup> assGroup; |
| 1770 | sp<AaptFile> assFile; |
| 1771 | String8 pkg; |
| 1772 | |
| 1773 | // First, look for a package file to parse. This is required to |
| 1774 | // be able to generate the resource information. |
| 1775 | assGroup = assets->getFiles().valueFor(String8("AndroidManifest.xml")); |
| 1776 | if (assGroup == NULL) { |
| 1777 | fprintf(stderr, "ERROR: No AndroidManifest.xml file found.\n"); |
| 1778 | return -1; |
| 1779 | } |
| 1780 | |
| 1781 | if (assGroup->getFiles().size() != 1) { |
| 1782 | fprintf(stderr, "warning: Multiple AndroidManifest.xml files found, using %s\n", |
| 1783 | assGroup->getFiles().valueAt(0)->getPrintableSource().string()); |
| 1784 | } |
| 1785 | |
| 1786 | assFile = assGroup->getFiles().valueAt(0); |
| 1787 | |
| 1788 | err = parseXMLResource(assFile, &tree); |
| 1789 | if (err != NO_ERROR) { |
| 1790 | return err; |
| 1791 | } |
| 1792 | |
| 1793 | tree.restart(); |
| 1794 | |
| 1795 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 1796 | if (code == ResXMLTree::END_TAG) { |
| 1797 | if (/* name == "Application" && */ depth == 2) { |
| 1798 | inApplication = false; |
| 1799 | } |
| 1800 | depth--; |
| 1801 | continue; |
| 1802 | } |
| 1803 | if (code != ResXMLTree::START_TAG) { |
| 1804 | continue; |
| 1805 | } |
| 1806 | depth++; |
| 1807 | String8 tag(tree.getElementName(&len)); |
| 1808 | // printf("Depth %d tag %s\n", depth, tag.string()); |
Ying Wang | 46f4b98 | 2010-01-13 14:18:11 -0800 | [diff] [blame] | 1809 | bool keepTag = false; |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 1810 | if (depth == 1) { |
| 1811 | if (tag != "manifest") { |
| 1812 | fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n"); |
| 1813 | return -1; |
| 1814 | } |
| 1815 | pkg = getAttribute(tree, NULL, "package", NULL); |
Ying Wang | 46f4b98 | 2010-01-13 14:18:11 -0800 | [diff] [blame] | 1816 | } else if (depth == 2) { |
| 1817 | if (tag == "application") { |
| 1818 | inApplication = true; |
| 1819 | keepTag = true; |
| 1820 | } else if (tag == "instrumentation") { |
| 1821 | keepTag = true; |
| 1822 | } |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 1823 | } |
Ying Wang | 46f4b98 | 2010-01-13 14:18:11 -0800 | [diff] [blame] | 1824 | if (!keepTag && inApplication && depth == 3) { |
| 1825 | if (tag == "activity" || tag == "service" || tag == "receiver" || tag == "provider") { |
| 1826 | keepTag = true; |
| 1827 | } |
| 1828 | } |
| 1829 | if (keepTag) { |
| 1830 | String8 name = getAttribute(tree, "http://schemas.android.com/apk/res/android", |
| 1831 | "name", &error); |
| 1832 | if (error != "") { |
| 1833 | fprintf(stderr, "ERROR: %s\n", error.string()); |
| 1834 | return -1; |
| 1835 | } |
| 1836 | if (name.length() > 0) { |
| 1837 | // asdf --> package.asdf |
| 1838 | // .asdf .a.b --> package.asdf package.a.b |
| 1839 | // asdf.adsf --> asdf.asdf |
| 1840 | String8 rule("-keep class "); |
| 1841 | const char* p = name.string(); |
| 1842 | const char* q = strchr(p, '.'); |
| 1843 | if (p == q) { |
| 1844 | rule += pkg; |
| 1845 | rule += name; |
| 1846 | } else if (q == NULL) { |
| 1847 | rule += pkg; |
| 1848 | rule += "."; |
| 1849 | rule += name; |
| 1850 | } else { |
| 1851 | rule += name; |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 1852 | } |
Ying Wang | 4199528a | 2010-01-12 16:08:23 -0800 | [diff] [blame] | 1853 | |
Ying Wang | 46f4b98 | 2010-01-13 14:18:11 -0800 | [diff] [blame] | 1854 | String8 location = tag; |
| 1855 | location += " "; |
| 1856 | location += assFile->getSourceFile(); |
| 1857 | char lineno[20]; |
| 1858 | sprintf(lineno, ":%d", tree.getLineNumber()); |
| 1859 | location += lineno; |
Ying Wang | 4199528a | 2010-01-12 16:08:23 -0800 | [diff] [blame] | 1860 | |
Ying Wang | 46f4b98 | 2010-01-13 14:18:11 -0800 | [diff] [blame] | 1861 | keep->add(rule, location); |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | return NO_ERROR; |
| 1867 | } |
| 1868 | |
| 1869 | status_t |
| 1870 | writeProguardForLayout(ProguardKeepSet* keep, const sp<AaptFile>& layoutFile) |
| 1871 | { |
| 1872 | status_t err; |
| 1873 | ResXMLTree tree; |
| 1874 | size_t len; |
| 1875 | ResXMLTree::event_code_t code; |
| 1876 | |
| 1877 | err = parseXMLResource(layoutFile, &tree); |
| 1878 | if (err != NO_ERROR) { |
| 1879 | return err; |
| 1880 | } |
| 1881 | |
| 1882 | tree.restart(); |
| 1883 | |
| 1884 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 1885 | if (code != ResXMLTree::START_TAG) { |
| 1886 | continue; |
| 1887 | } |
| 1888 | String8 tag(tree.getElementName(&len)); |
| 1889 | |
| 1890 | // If there is no '.', we'll assume that it's one of the built in names. |
| 1891 | if (strchr(tag.string(), '.')) { |
| 1892 | String8 rule("-keep class "); |
| 1893 | rule += tag; |
| 1894 | rule += " { <init>(...); }"; |
| 1895 | |
| 1896 | String8 location("view "); |
| 1897 | location += layoutFile->getSourceFile(); |
| 1898 | char lineno[20]; |
| 1899 | sprintf(lineno, ":%d", tree.getLineNumber()); |
| 1900 | location += lineno; |
| 1901 | |
| 1902 | keep->add(rule, location); |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | return NO_ERROR; |
| 1907 | } |
| 1908 | |
| 1909 | status_t |
| 1910 | writeProguardForLayouts(ProguardKeepSet* keep, const sp<AaptAssets>& assets) |
| 1911 | { |
| 1912 | status_t err; |
Ying Wang | c111296 | 2010-01-20 22:12:46 -0800 | [diff] [blame] | 1913 | const Vector<sp<AaptDir> >& dirs = assets->resDirs(); |
| 1914 | const size_t K = dirs.size(); |
| 1915 | for (size_t k=0; k<K; k++) { |
| 1916 | const sp<AaptDir>& d = dirs.itemAt(k); |
| 1917 | const String8& dirName = d->getLeaf(); |
| 1918 | if ((dirName != String8("layout")) && (strncmp(dirName.string(), "layout-", 7) != 0)) { |
| 1919 | continue; |
| 1920 | } |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 1921 | |
Ying Wang | c111296 | 2010-01-20 22:12:46 -0800 | [diff] [blame] | 1922 | const KeyedVector<String8,sp<AaptGroup> > groups = d->getFiles(); |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 1923 | const size_t N = groups.size(); |
| 1924 | for (size_t i=0; i<N; i++) { |
| 1925 | const sp<AaptGroup>& group = groups.valueAt(i); |
| 1926 | const DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> >& files = group->getFiles(); |
| 1927 | const size_t M = files.size(); |
| 1928 | for (size_t j=0; j<M; j++) { |
| 1929 | err = writeProguardForLayout(keep, files.valueAt(j)); |
| 1930 | if (err < 0) { |
| 1931 | return err; |
| 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | } |
| 1936 | return NO_ERROR; |
| 1937 | } |
| 1938 | |
| 1939 | status_t |
| 1940 | writeProguardFile(Bundle* bundle, const sp<AaptAssets>& assets) |
| 1941 | { |
| 1942 | status_t err = -1; |
| 1943 | |
| 1944 | if (!bundle->getProguardFile()) { |
| 1945 | return NO_ERROR; |
| 1946 | } |
| 1947 | |
| 1948 | ProguardKeepSet keep; |
| 1949 | |
| 1950 | err = writeProguardForAndroidManifest(&keep, assets); |
| 1951 | if (err < 0) { |
| 1952 | return err; |
| 1953 | } |
| 1954 | |
| 1955 | err = writeProguardForLayouts(&keep, assets); |
| 1956 | if (err < 0) { |
| 1957 | return err; |
| 1958 | } |
| 1959 | |
| 1960 | FILE* fp = fopen(bundle->getProguardFile(), "w+"); |
| 1961 | if (fp == NULL) { |
| 1962 | fprintf(stderr, "ERROR: Unable to open class file %s: %s\n", |
| 1963 | bundle->getProguardFile(), strerror(errno)); |
| 1964 | return UNKNOWN_ERROR; |
| 1965 | } |
| 1966 | |
| 1967 | const KeyedVector<String8, SortedVector<String8> >& rules = keep.rules; |
| 1968 | const size_t N = rules.size(); |
| 1969 | for (size_t i=0; i<N; i++) { |
| 1970 | const SortedVector<String8>& locations = rules.valueAt(i); |
| 1971 | const size_t M = locations.size(); |
| 1972 | for (size_t j=0; j<M; j++) { |
| 1973 | fprintf(fp, "# %s\n", locations.itemAt(j).string()); |
| 1974 | } |
| 1975 | fprintf(fp, "%s\n\n", rules.keyAt(i).string()); |
| 1976 | } |
| 1977 | fclose(fp); |
| 1978 | |
| 1979 | return err; |
| 1980 | } |