| 1 | // |
| 2 | // Copyright 2006 The Android Open Source Project |
| 3 | // |
| 4 | // Android Asset Packaging Tool main entry point. |
| 5 | // |
| 6 | #include "Main.h" |
| 7 | #include "Bundle.h" |
| 8 | #include "ResourceTable.h" |
| 9 | #include "XMLNode.h" |
| 10 | |
| 11 | #include <utils.h> |
| 12 | #include <utils/ZipFile.h> |
| 13 | |
| 14 | #include <fcntl.h> |
| 15 | #include <errno.h> |
| 16 | |
| 17 | using namespace android; |
| 18 | |
| 19 | /* |
| 20 | * Show version info. All the cool kids do it. |
| 21 | */ |
| 22 | int doVersion(Bundle* bundle) |
| 23 | { |
| 24 | if (bundle->getFileSpecCount() != 0) |
| 25 | printf("(ignoring extra arguments)\n"); |
| 26 | printf("Android Asset Packaging Tool, v0.2\n"); |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /* |
| 33 | * Open the file read only. The call fails if the file doesn't exist. |
| 34 | * |
| 35 | * Returns NULL on failure. |
| 36 | */ |
| 37 | ZipFile* openReadOnly(const char* fileName) |
| 38 | { |
| 39 | ZipFile* zip; |
| 40 | status_t result; |
| 41 | |
| 42 | zip = new ZipFile; |
| 43 | result = zip->open(fileName, ZipFile::kOpenReadOnly); |
| 44 | if (result != NO_ERROR) { |
| 45 | if (result == NAME_NOT_FOUND) |
| 46 | fprintf(stderr, "ERROR: '%s' not found\n", fileName); |
| 47 | else if (result == PERMISSION_DENIED) |
| 48 | fprintf(stderr, "ERROR: '%s' access denied\n", fileName); |
| 49 | else |
| 50 | fprintf(stderr, "ERROR: failed opening '%s' as Zip file\n", |
| 51 | fileName); |
| 52 | delete zip; |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | return zip; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Open the file read-write. The file will be created if it doesn't |
| 61 | * already exist and "okayToCreate" is set. |
| 62 | * |
| 63 | * Returns NULL on failure. |
| 64 | */ |
| 65 | ZipFile* openReadWrite(const char* fileName, bool okayToCreate) |
| 66 | { |
| 67 | ZipFile* zip = NULL; |
| 68 | status_t result; |
| 69 | int flags; |
| 70 | |
| 71 | flags = ZipFile::kOpenReadWrite; |
| 72 | if (okayToCreate) |
| 73 | flags |= ZipFile::kOpenCreate; |
| 74 | |
| 75 | zip = new ZipFile; |
| 76 | result = zip->open(fileName, flags); |
| 77 | if (result != NO_ERROR) { |
| 78 | delete zip; |
| 79 | zip = NULL; |
| 80 | goto bail; |
| 81 | } |
| 82 | |
| 83 | bail: |
| 84 | return zip; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /* |
| 89 | * Return a short string describing the compression method. |
| 90 | */ |
| 91 | const char* compressionName(int method) |
| 92 | { |
| 93 | if (method == ZipEntry::kCompressStored) |
| 94 | return "Stored"; |
| 95 | else if (method == ZipEntry::kCompressDeflated) |
| 96 | return "Deflated"; |
| 97 | else |
| 98 | return "Unknown"; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Return the percent reduction in size (0% == no compression). |
| 103 | */ |
| 104 | int calcPercent(long uncompressedLen, long compressedLen) |
| 105 | { |
| 106 | if (!uncompressedLen) |
| 107 | return 0; |
| 108 | else |
| 109 | return (int) (100.0 - (compressedLen * 100.0) / uncompressedLen + 0.5); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Handle the "list" command, which can be a simple file dump or |
| 114 | * a verbose listing. |
| 115 | * |
| 116 | * The verbose listing closely matches the output of the Info-ZIP "unzip" |
| 117 | * command. |
| 118 | */ |
| 119 | int doList(Bundle* bundle) |
| 120 | { |
| 121 | int result = 1; |
| 122 | ZipFile* zip = NULL; |
| 123 | const ZipEntry* entry; |
| 124 | long totalUncLen, totalCompLen; |
| 125 | const char* zipFileName; |
| 126 | |
| 127 | if (bundle->getFileSpecCount() != 1) { |
| 128 | fprintf(stderr, "ERROR: specify zip file name (only)\n"); |
| 129 | goto bail; |
| 130 | } |
| 131 | zipFileName = bundle->getFileSpecEntry(0); |
| 132 | |
| 133 | zip = openReadOnly(zipFileName); |
| 134 | if (zip == NULL) |
| 135 | goto bail; |
| 136 | |
| 137 | int count, i; |
| 138 | |
| 139 | if (bundle->getVerbose()) { |
| 140 | printf("Archive: %s\n", zipFileName); |
| 141 | printf( |
| 142 | " Length Method Size Ratio Date Time CRC-32 Name\n"); |
| 143 | printf( |
| 144 | "-------- ------ ------- ----- ---- ---- ------ ----\n"); |
| 145 | } |
| 146 | |
| 147 | totalUncLen = totalCompLen = 0; |
| 148 | |
| 149 | count = zip->getNumEntries(); |
| 150 | for (i = 0; i < count; i++) { |
| 151 | entry = zip->getEntryByIndex(i); |
| 152 | if (bundle->getVerbose()) { |
| 153 | char dateBuf[32]; |
| 154 | time_t when; |
| 155 | |
| 156 | when = entry->getModWhen(); |
| 157 | strftime(dateBuf, sizeof(dateBuf), "%m-%d-%y %H:%M", |
| 158 | localtime(&when)); |
| 159 | |
| 160 | printf("%8ld %-7.7s %7ld %3d%% %s %08lx %s\n", |
| 161 | (long) entry->getUncompressedLen(), |
| 162 | compressionName(entry->getCompressionMethod()), |
| 163 | (long) entry->getCompressedLen(), |
| 164 | calcPercent(entry->getUncompressedLen(), |
| 165 | entry->getCompressedLen()), |
| 166 | dateBuf, |
| 167 | entry->getCRC32(), |
| 168 | entry->getFileName()); |
| 169 | } else { |
| 170 | printf("%s\n", entry->getFileName()); |
| 171 | } |
| 172 | |
| 173 | totalUncLen += entry->getUncompressedLen(); |
| 174 | totalCompLen += entry->getCompressedLen(); |
| 175 | } |
| 176 | |
| 177 | if (bundle->getVerbose()) { |
| 178 | printf( |
| 179 | "-------- ------- --- -------\n"); |
| 180 | printf("%8ld %7ld %2d%% %d files\n", |
| 181 | totalUncLen, |
| 182 | totalCompLen, |
| 183 | calcPercent(totalUncLen, totalCompLen), |
| 184 | zip->getNumEntries()); |
| 185 | } |
| 186 | |
| 187 | if (bundle->getAndroidList()) { |
| 188 | AssetManager assets; |
| 189 | if (!assets.addAssetPath(String8(zipFileName), NULL)) { |
| 190 | fprintf(stderr, "ERROR: list -a failed because assets could not be loaded\n"); |
| 191 | goto bail; |
| 192 | } |
| 193 | |
| 194 | const ResTable& res = assets.getResources(false); |
| 195 | if (&res == NULL) { |
| 196 | printf("\nNo resource table found.\n"); |
| 197 | } else { |
| 198 | printf("\nResource table:\n"); |
| 199 | res.print(false); |
| 200 | } |
| 201 | |
| 202 | Asset* manifestAsset = assets.openNonAsset("AndroidManifest.xml", |
| 203 | Asset::ACCESS_BUFFER); |
| 204 | if (manifestAsset == NULL) { |
| 205 | printf("\nNo AndroidManifest.xml found.\n"); |
| 206 | } else { |
| 207 | printf("\nAndroid manifest:\n"); |
| 208 | ResXMLTree tree; |
| 209 | tree.setTo(manifestAsset->getBuffer(true), |
| 210 | manifestAsset->getLength()); |
| 211 | printXMLBlock(&tree); |
| 212 | } |
| 213 | delete manifestAsset; |
| 214 | } |
| 215 | |
| 216 | result = 0; |
| 217 | |
| 218 | bail: |
| 219 | delete zip; |
| 220 | return result; |
| 221 | } |
| 222 | |
| 223 | static ssize_t indexOfAttribute(const ResXMLTree& tree, uint32_t attrRes) |
| 224 | { |
| 225 | size_t N = tree.getAttributeCount(); |
| 226 | for (size_t i=0; i<N; i++) { |
| 227 | if (tree.getAttributeNameResID(i) == attrRes) { |
| 228 | return (ssize_t)i; |
| 229 | } |
| 230 | } |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | static String8 getAttribute(const ResXMLTree& tree, const char* ns, |
| 235 | const char* attr, String8* outError) |
| 236 | { |
| 237 | ssize_t idx = tree.indexOfAttribute(ns, attr); |
| 238 | if (idx < 0) { |
| 239 | return String8(); |
| 240 | } |
| 241 | Res_value value; |
| 242 | if (tree.getAttributeValue(idx, &value) != NO_ERROR) { |
| 243 | if (value.dataType != Res_value::TYPE_STRING) { |
| 244 | if (outError != NULL) *outError = "attribute is not a string value"; |
| 245 | return String8(); |
| 246 | } |
| 247 | } |
| 248 | size_t len; |
| 249 | const uint16_t* str = tree.getAttributeStringValue(idx, &len); |
| 250 | return str ? String8(str, len) : String8(); |
| 251 | } |
| 252 | |
| 253 | static String8 getAttribute(const ResXMLTree& tree, uint32_t attrRes, String8* outError) |
| 254 | { |
| 255 | ssize_t idx = indexOfAttribute(tree, attrRes); |
| 256 | if (idx < 0) { |
| 257 | return String8(); |
| 258 | } |
| 259 | Res_value value; |
| 260 | if (tree.getAttributeValue(idx, &value) != NO_ERROR) { |
| 261 | if (value.dataType != Res_value::TYPE_STRING) { |
| 262 | if (outError != NULL) *outError = "attribute is not a string value"; |
| 263 | return String8(); |
| 264 | } |
| 265 | } |
| 266 | size_t len; |
| 267 | const uint16_t* str = tree.getAttributeStringValue(idx, &len); |
| 268 | return str ? String8(str, len) : String8(); |
| 269 | } |
| 270 | |
| 271 | static int32_t getIntegerAttribute(const ResXMLTree& tree, uint32_t attrRes, |
| 272 | String8* outError, int32_t defValue = -1) |
| 273 | { |
| 274 | ssize_t idx = indexOfAttribute(tree, attrRes); |
| 275 | if (idx < 0) { |
| 276 | return defValue; |
| 277 | } |
| 278 | Res_value value; |
| 279 | if (tree.getAttributeValue(idx, &value) != NO_ERROR) { |
| 280 | if (value.dataType < Res_value::TYPE_FIRST_INT |
| 281 | || value.dataType > Res_value::TYPE_LAST_INT) { |
| 282 | if (outError != NULL) *outError = "attribute is not an integer value"; |
| 283 | return defValue; |
| 284 | } |
| 285 | } |
| 286 | return value.data; |
| 287 | } |
| 288 | |
| 289 | static String8 getResolvedAttribute(const ResTable* resTable, const ResXMLTree& tree, |
| 290 | uint32_t attrRes, String8* outError) |
| 291 | { |
| 292 | ssize_t idx = indexOfAttribute(tree, attrRes); |
| 293 | if (idx < 0) { |
| 294 | return String8(); |
| 295 | } |
| 296 | Res_value value; |
| 297 | if (tree.getAttributeValue(idx, &value) != NO_ERROR) { |
| 298 | if (value.dataType == Res_value::TYPE_STRING) { |
| 299 | size_t len; |
| 300 | const uint16_t* str = tree.getAttributeStringValue(idx, &len); |
| 301 | return str ? String8(str, len) : String8(); |
| 302 | } |
| 303 | resTable->resolveReference(&value, 0); |
| 304 | if (value.dataType != Res_value::TYPE_STRING) { |
| 305 | if (outError != NULL) *outError = "attribute is not a string value"; |
| 306 | return String8(); |
| 307 | } |
| 308 | } |
| 309 | size_t len; |
| 310 | const Res_value* value2 = &value; |
| 311 | const char16_t* str = const_cast<ResTable*>(resTable)->valueToString(value2, 0, NULL, &len); |
| 312 | return str ? String8(str, len) : String8(); |
| 313 | } |
| 314 | |
| 315 | // These are attribute resource constants for the platform, as found |
| 316 | // in android.R.attr |
| 317 | enum { |
| 318 | NAME_ATTR = 0x01010003, |
| 319 | VERSION_CODE_ATTR = 0x0101021b, |
| 320 | VERSION_NAME_ATTR = 0x0101021c, |
| 321 | LABEL_ATTR = 0x01010001, |
| 322 | ICON_ATTR = 0x01010002, |
| 323 | MIN_SDK_VERSION_ATTR = 0x0101020c, |
| 324 | REQ_TOUCH_SCREEN_ATTR = 0x01010227, |
| 325 | REQ_KEYBOARD_TYPE_ATTR = 0x01010228, |
| 326 | REQ_HARD_KEYBOARD_ATTR = 0x01010229, |
| 327 | REQ_NAVIGATION_ATTR = 0x0101022a, |
| 328 | REQ_FIVE_WAY_NAV_ATTR = 0x01010232, |
| 329 | TARGET_SDK_VERSION_ATTR = 0x01010270, |
| 330 | TEST_ONLY_ATTR = 0x01010272, |
| 331 | DENSITY_ATTR = 0x0101026c, |
| 332 | SMALL_SCREEN_ATTR = 0x01010284, |
| 333 | NORMAL_SCREEN_ATTR = 0x01010285, |
| 334 | LARGE_SCREEN_ATTR = 0x01010286, |
| 335 | }; |
| 336 | |
| 337 | const char *getComponentName(String8 &pkgName, String8 &componentName) { |
| 338 | ssize_t idx = componentName.find("."); |
| 339 | String8 retStr(pkgName); |
| 340 | if (idx == 0) { |
| 341 | retStr += componentName; |
| 342 | } else if (idx < 0) { |
| 343 | retStr += "."; |
| 344 | retStr += componentName; |
| 345 | } else { |
| 346 | return componentName.string(); |
| 347 | } |
| 348 | return retStr.string(); |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Handle the "dump" command, to extract select data from an archive. |
| 353 | */ |
| 354 | int doDump(Bundle* bundle) |
| 355 | { |
| 356 | status_t result = UNKNOWN_ERROR; |
| 357 | Asset* asset = NULL; |
| 358 | |
| 359 | if (bundle->getFileSpecCount() < 1) { |
| 360 | fprintf(stderr, "ERROR: no dump option specified\n"); |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | if (bundle->getFileSpecCount() < 2) { |
| 365 | fprintf(stderr, "ERROR: no dump file specified\n"); |
| 366 | return 1; |
| 367 | } |
| 368 | |
| 369 | const char* option = bundle->getFileSpecEntry(0); |
| 370 | const char* filename = bundle->getFileSpecEntry(1); |
| 371 | |
| 372 | AssetManager assets; |
| 373 | void* assetsCookie; |
| 374 | if (!assets.addAssetPath(String8(filename), &assetsCookie)) { |
| 375 | fprintf(stderr, "ERROR: dump failed because assets could not be loaded\n"); |
| 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | const ResTable& res = assets.getResources(false); |
| 380 | if (&res == NULL) { |
| 381 | fprintf(stderr, "ERROR: dump failed because no resource table was found\n"); |
| 382 | goto bail; |
| 383 | } |
| 384 | |
| 385 | if (strcmp("resources", option) == 0) { |
| 386 | res.print(bundle->getValues()); |
| 387 | |
| 388 | } else if (strcmp("xmltree", option) == 0) { |
| 389 | if (bundle->getFileSpecCount() < 3) { |
| 390 | fprintf(stderr, "ERROR: no dump xmltree resource file specified\n"); |
| 391 | goto bail; |
| 392 | } |
| 393 | |
| 394 | for (int i=2; i<bundle->getFileSpecCount(); i++) { |
| 395 | const char* resname = bundle->getFileSpecEntry(i); |
| 396 | ResXMLTree tree; |
| 397 | asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER); |
| 398 | if (asset == NULL) { |
| 399 | fprintf(stderr, "ERROR: dump failed because resource %p found\n", resname); |
| 400 | goto bail; |
| 401 | } |
| 402 | |
| 403 | if (tree.setTo(asset->getBuffer(true), |
| 404 | asset->getLength()) != NO_ERROR) { |
| 405 | fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname); |
| 406 | goto bail; |
| 407 | } |
| 408 | tree.restart(); |
| 409 | printXMLBlock(&tree); |
| 410 | delete asset; |
| 411 | asset = NULL; |
| 412 | } |
| 413 | |
| 414 | } else if (strcmp("xmlstrings", option) == 0) { |
| 415 | if (bundle->getFileSpecCount() < 3) { |
| 416 | fprintf(stderr, "ERROR: no dump xmltree resource file specified\n"); |
| 417 | goto bail; |
| 418 | } |
| 419 | |
| 420 | for (int i=2; i<bundle->getFileSpecCount(); i++) { |
| 421 | const char* resname = bundle->getFileSpecEntry(i); |
| 422 | ResXMLTree tree; |
| 423 | asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER); |
| 424 | if (asset == NULL) { |
| 425 | fprintf(stderr, "ERROR: dump failed because resource %p found\n", resname); |
| 426 | goto bail; |
| 427 | } |
| 428 | |
| 429 | if (tree.setTo(asset->getBuffer(true), |
| 430 | asset->getLength()) != NO_ERROR) { |
| 431 | fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname); |
| 432 | goto bail; |
| 433 | } |
| 434 | printStringPool(&tree.getStrings()); |
| 435 | delete asset; |
| 436 | asset = NULL; |
| 437 | } |
| 438 | |
| 439 | } else { |
| 440 | ResXMLTree tree; |
| 441 | asset = assets.openNonAsset("AndroidManifest.xml", |
| 442 | Asset::ACCESS_BUFFER); |
| 443 | if (asset == NULL) { |
| 444 | fprintf(stderr, "ERROR: dump failed because no AndroidManifest.xml found\n"); |
| 445 | goto bail; |
| 446 | } |
| 447 | |
| 448 | if (tree.setTo(asset->getBuffer(true), |
| 449 | asset->getLength()) != NO_ERROR) { |
| 450 | fprintf(stderr, "ERROR: AndroidManifest.xml is corrupt\n"); |
| 451 | goto bail; |
| 452 | } |
| 453 | tree.restart(); |
| 454 | |
| 455 | if (strcmp("permissions", option) == 0) { |
| 456 | size_t len; |
| 457 | ResXMLTree::event_code_t code; |
| 458 | int depth = 0; |
| 459 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 460 | if (code == ResXMLTree::END_TAG) { |
| 461 | depth--; |
| 462 | continue; |
| 463 | } |
| 464 | if (code != ResXMLTree::START_TAG) { |
| 465 | continue; |
| 466 | } |
| 467 | depth++; |
| 468 | String8 tag(tree.getElementName(&len)); |
| 469 | //printf("Depth %d tag %s\n", depth, tag.string()); |
| 470 | if (depth == 1) { |
| 471 | if (tag != "manifest") { |
| 472 | fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n"); |
| 473 | goto bail; |
| 474 | } |
| 475 | String8 pkg = getAttribute(tree, NULL, "package", NULL); |
| 476 | printf("package: %s\n", pkg.string()); |
| 477 | } else if (depth == 2 && tag == "permission") { |
| 478 | String8 error; |
| 479 | String8 name = getAttribute(tree, NAME_ATTR, &error); |
| 480 | if (error != "") { |
| 481 | fprintf(stderr, "ERROR: %s\n", error.string()); |
| 482 | goto bail; |
| 483 | } |
| 484 | printf("permission: %s\n", name.string()); |
| 485 | } else if (depth == 2 && tag == "uses-permission") { |
| 486 | String8 error; |
| 487 | String8 name = getAttribute(tree, NAME_ATTR, &error); |
| 488 | if (error != "") { |
| 489 | fprintf(stderr, "ERROR: %s\n", error.string()); |
| 490 | goto bail; |
| 491 | } |
| 492 | printf("uses-permission: %s\n", name.string()); |
| 493 | } |
| 494 | } |
| 495 | } else if (strcmp("badging", option) == 0) { |
| 496 | size_t len; |
| 497 | ResXMLTree::event_code_t code; |
| 498 | int depth = 0; |
| 499 | String8 error; |
| 500 | bool withinActivity = false; |
| 501 | bool isMainActivity = false; |
| 502 | bool isLauncherActivity = false; |
| 503 | bool withinApplication = false; |
| 504 | bool withinReceiver = false; |
| 505 | int targetSdk = 0; |
| 506 | int smallScreen = 1; |
| 507 | int normalScreen = 1; |
| 508 | int largeScreen = 1; |
| 509 | String8 pkg; |
| 510 | String8 activityName; |
| 511 | String8 activityLabel; |
| 512 | String8 activityIcon; |
| 513 | String8 receiverName; |
| 514 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 515 | if (code == ResXMLTree::END_TAG) { |
| 516 | depth--; |
| 517 | continue; |
| 518 | } |
| 519 | if (code != ResXMLTree::START_TAG) { |
| 520 | continue; |
| 521 | } |
| 522 | depth++; |
| 523 | String8 tag(tree.getElementName(&len)); |
| 524 | //printf("Depth %d tag %s\n", depth, tag.string()); |
| 525 | if (depth == 1) { |
| 526 | if (tag != "manifest") { |
| 527 | fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n"); |
| 528 | goto bail; |
| 529 | } |
| 530 | pkg = getAttribute(tree, NULL, "package", NULL); |
| 531 | printf("package: name='%s' ", pkg.string()); |
| 532 | int32_t versionCode = getIntegerAttribute(tree, VERSION_CODE_ATTR, &error); |
| 533 | if (error != "") { |
| 534 | fprintf(stderr, "ERROR getting 'android:versionCode' attribute: %s\n", error.string()); |
| 535 | goto bail; |
| 536 | } |
| 537 | if (versionCode > 0) { |
| 538 | printf("versionCode='%d' ", versionCode); |
| 539 | } else { |
| 540 | printf("versionCode='' "); |
| 541 | } |
| 542 | String8 versionName = getAttribute(tree, VERSION_NAME_ATTR, &error); |
| 543 | if (error != "") { |
| 544 | fprintf(stderr, "ERROR getting 'android:versionName' attribute: %s\n", error.string()); |
| 545 | goto bail; |
| 546 | } |
| 547 | printf("versionName='%s'\n", versionName.string()); |
| 548 | } else if (depth == 2) { |
| 549 | withinApplication = false; |
| 550 | if (tag == "application") { |
| 551 | withinApplication = true; |
| 552 | String8 label = getResolvedAttribute(&res, tree, LABEL_ATTR, &error); |
| 553 | if (error != "") { |
| 554 | fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string()); |
| 555 | goto bail; |
| 556 | } |
| 557 | printf("application: label='%s' ", label.string()); |
| 558 | String8 icon = getResolvedAttribute(&res, tree, ICON_ATTR, &error); |
| 559 | if (error != "") { |
| 560 | fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string()); |
| 561 | goto bail; |
| 562 | } |
| 563 | printf("icon='%s'\n", icon.string()); |
| 564 | int32_t testOnly = getIntegerAttribute(tree, TEST_ONLY_ATTR, &error, 0); |
| 565 | if (error != "") { |
| 566 | fprintf(stderr, "ERROR getting 'android:testOnly' attribute: %s\n", error.string()); |
| 567 | goto bail; |
| 568 | } |
| 569 | if (testOnly != 0) { |
| 570 | printf("testOnly='%d'\n", testOnly); |
| 571 | } |
| 572 | } else if (tag == "uses-sdk") { |
| 573 | int32_t code = getIntegerAttribute(tree, MIN_SDK_VERSION_ATTR, &error); |
| 574 | if (error != "") { |
| 575 | error = ""; |
| 576 | String8 name = getResolvedAttribute(&res, tree, MIN_SDK_VERSION_ATTR, &error); |
| 577 | if (error != "") { |
| 578 | fprintf(stderr, "ERROR getting 'android:minSdkVersion' attribute: %s\n", |
| 579 | error.string()); |
| 580 | goto bail; |
| 581 | } |
| 582 | if (name == "Donut") targetSdk = 4; |
| 583 | printf("sdkVersion:'%s'\n", name.string()); |
| 584 | } else if (code != -1) { |
| 585 | targetSdk = code; |
| 586 | printf("sdkVersion:'%d'\n", code); |
| 587 | } |
| 588 | code = getIntegerAttribute(tree, TARGET_SDK_VERSION_ATTR, &error); |
| 589 | if (error != "") { |
| 590 | error = ""; |
| 591 | String8 name = getResolvedAttribute(&res, tree, TARGET_SDK_VERSION_ATTR, &error); |
| 592 | if (error != "") { |
| 593 | fprintf(stderr, "ERROR getting 'android:targetSdkVersion' attribute: %s\n", |
| 594 | error.string()); |
| 595 | goto bail; |
| 596 | } |
| 597 | if (name == "Donut" && targetSdk < 4) targetSdk = 4; |
| 598 | printf("targetSdkVersion:'%s'\n", name.string()); |
| 599 | } else if (code != -1) { |
| 600 | if (targetSdk < code) { |
| 601 | targetSdk = code; |
| 602 | } |
| 603 | printf("targetSdkVersion:'%d'\n", code); |
| 604 | } |
| 605 | } else if (tag == "uses-configuration") { |
| 606 | int32_t reqTouchScreen = getIntegerAttribute(tree, |
| 607 | REQ_TOUCH_SCREEN_ATTR, NULL, 0); |
| 608 | int32_t reqKeyboardType = getIntegerAttribute(tree, |
| 609 | REQ_KEYBOARD_TYPE_ATTR, NULL, 0); |
| 610 | int32_t reqHardKeyboard = getIntegerAttribute(tree, |
| 611 | REQ_HARD_KEYBOARD_ATTR, NULL, 0); |
| 612 | int32_t reqNavigation = getIntegerAttribute(tree, |
| 613 | REQ_NAVIGATION_ATTR, NULL, 0); |
| 614 | int32_t reqFiveWayNav = getIntegerAttribute(tree, |
| 615 | REQ_FIVE_WAY_NAV_ATTR, NULL, 0); |
| 616 | printf("uses-configuation:"); |
| 617 | if (reqTouchScreen != 0) { |
| 618 | printf(" reqTouchScreen='%d'", reqTouchScreen); |
| 619 | } |
| 620 | if (reqKeyboardType != 0) { |
| 621 | printf(" reqKeyboardType='%d'", reqKeyboardType); |
| 622 | } |
| 623 | if (reqHardKeyboard != 0) { |
| 624 | printf(" reqHardKeyboard='%d'", reqHardKeyboard); |
| 625 | } |
| 626 | if (reqNavigation != 0) { |
| 627 | printf(" reqNavigation='%d'", reqNavigation); |
| 628 | } |
| 629 | if (reqFiveWayNav != 0) { |
| 630 | printf(" reqFiveWayNav='%d'", reqFiveWayNav); |
| 631 | } |
| 632 | printf("\n"); |
| 633 | } else if (tag == "supports-density") { |
| 634 | int32_t dens = getIntegerAttribute(tree, DENSITY_ATTR, &error); |
| 635 | if (error != "") { |
| 636 | fprintf(stderr, "ERROR getting 'android:density' attribute: %s\n", |
| 637 | error.string()); |
| 638 | goto bail; |
| 639 | } |
| 640 | printf("supports-density:'%d'\n", dens); |
| 641 | } else if (tag == "supports-screens") { |
| 642 | smallScreen = getIntegerAttribute(tree, |
| 643 | SMALL_SCREEN_ATTR, NULL, 1); |
| 644 | normalScreen = getIntegerAttribute(tree, |
| 645 | NORMAL_SCREEN_ATTR, NULL, 1); |
| 646 | largeScreen = getIntegerAttribute(tree, |
| 647 | LARGE_SCREEN_ATTR, NULL, 1); |
| 648 | } |
| 649 | } else if (depth == 3 && withinApplication) { |
| 650 | withinActivity = false; |
| 651 | withinReceiver = false; |
| 652 | if(tag == "activity") { |
| 653 | withinActivity = true; |
| 654 | activityName = getAttribute(tree, NAME_ATTR, &error); |
| 655 | if (error != "") { |
| 656 | fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string()); |
| 657 | goto bail; |
| 658 | } |
| 659 | |
| 660 | activityLabel = getResolvedAttribute(&res, tree, LABEL_ATTR, &error); |
| 661 | if (error != "") { |
| 662 | fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string()); |
| 663 | goto bail; |
| 664 | } |
| 665 | |
| 666 | activityIcon = getResolvedAttribute(&res, tree, ICON_ATTR, &error); |
| 667 | if (error != "") { |
| 668 | fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string()); |
| 669 | goto bail; |
| 670 | } |
| 671 | } else if (tag == "uses-library") { |
| 672 | String8 libraryName = getAttribute(tree, NAME_ATTR, &error); |
| 673 | if (error != "") { |
| 674 | fprintf(stderr, "ERROR getting 'android:name' attribute for uses-library: %s\n", error.string()); |
| 675 | goto bail; |
| 676 | } |
| 677 | printf("uses-library:'%s'\n", libraryName.string()); |
| 678 | } else if (tag == "receiver") { |
| 679 | withinReceiver = true; |
| 680 | receiverName = getAttribute(tree, NAME_ATTR, &error); |
| 681 | |
| 682 | if (error != "") { |
| 683 | fprintf(stderr, "ERROR getting 'android:name' attribute for receiver: %s\n", error.string()); |
| 684 | goto bail; |
| 685 | } |
| 686 | } |
| 687 | } else if (depth == 5) { |
| 688 | if (withinActivity) { |
| 689 | if (tag == "action") { |
| 690 | //printf("LOG: action tag\n"); |
| 691 | String8 action = getAttribute(tree, NAME_ATTR, &error); |
| 692 | if (error != "") { |
| 693 | fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string()); |
| 694 | goto bail; |
| 695 | } |
| 696 | if (action == "android.intent.action.MAIN") { |
| 697 | isMainActivity = true; |
| 698 | //printf("LOG: isMainActivity==true\n"); |
| 699 | } |
| 700 | } else if (tag == "category") { |
| 701 | String8 category = getAttribute(tree, NAME_ATTR, &error); |
| 702 | if (error != "") { |
| 703 | fprintf(stderr, "ERROR getting 'name' attribute: %s\n", error.string()); |
| 704 | goto bail; |
| 705 | } |
| 706 | if (category == "android.intent.category.LAUNCHER") { |
| 707 | isLauncherActivity = true; |
| 708 | //printf("LOG: isLauncherActivity==true\n"); |
| 709 | } |
| 710 | } |
| 711 | } else if (withinReceiver) { |
| 712 | if (tag == "action") { |
| 713 | String8 action = getAttribute(tree, NAME_ATTR, &error); |
| 714 | if (error != "") { |
| 715 | fprintf(stderr, "ERROR getting 'android:name' attribute for receiver: %s\n", error.string()); |
| 716 | goto bail; |
| 717 | } |
| 718 | if (action == "android.appwidget.action.APPWIDGET_UPDATE") { |
| 719 | const char *rName = getComponentName(pkg, receiverName); |
| 720 | if (rName != NULL) { |
| 721 | printf("gadget-receiver:'%s/%s'\n", pkg.string(), rName); |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | if (depth < 2) { |
| 729 | withinApplication = false; |
| 730 | } |
| 731 | if (depth < 3) { |
| 732 | //if (withinActivity) printf("LOG: withinActivity==false\n"); |
| 733 | withinActivity = false; |
| 734 | withinReceiver = false; |
| 735 | } |
| 736 | |
| 737 | if (depth < 5) { |
| 738 | //if (isMainActivity) printf("LOG: isMainActivity==false\n"); |
| 739 | //if (isLauncherActivity) printf("LOG: isLauncherActivity==false\n"); |
| 740 | isMainActivity = false; |
| 741 | isLauncherActivity = false; |
| 742 | } |
| 743 | |
| 744 | if (withinActivity && isMainActivity && isLauncherActivity) { |
| 745 | printf("launchable activity:"); |
| 746 | const char *aName = getComponentName(pkg, activityName); |
| 747 | if (aName != NULL) { |
| 748 | printf(" name='%s'", aName); |
| 749 | } |
| 750 | printf("label='%s' icon='%s'\n", |
| 751 | activityLabel.string(), |
| 752 | activityIcon.string()); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | // Determine default values for any unspecified screen sizes, |
| 757 | // based on the target SDK of the package. As of 4 (donut) |
| 758 | // the screen size support was introduced, so all default to |
| 759 | // enabled. |
| 760 | if (smallScreen > 0) { |
| 761 | smallScreen = targetSdk >= 4 ? -1 : 0; |
| 762 | } |
| 763 | if (normalScreen > 0) { |
| 764 | normalScreen = -1; |
| 765 | } |
| 766 | if (largeScreen > 0) { |
| 767 | largeScreen = targetSdk >= 4 ? -1 : 0; |
| 768 | } |
| 769 | printf("supports-screens:"); |
| 770 | if (smallScreen != 0) printf(" 'small'"); |
| 771 | if (normalScreen != 0) printf(" 'normal'"); |
| 772 | if (largeScreen != 0) printf(" 'large'"); |
| 773 | printf("\n"); |
| 774 | |
| 775 | printf("locales:"); |
| 776 | Vector<String8> locales; |
| 777 | res.getLocales(&locales); |
| 778 | const size_t NL = locales.size(); |
| 779 | for (size_t i=0; i<NL; i++) { |
| 780 | const char* localeStr = locales[i].string(); |
| 781 | if (localeStr == NULL || strlen(localeStr) == 0) { |
| 782 | localeStr = "--_--"; |
| 783 | } |
| 784 | printf(" '%s'", localeStr); |
| 785 | } |
| 786 | printf("\n"); |
| 787 | |
| 788 | Vector<ResTable_config> configs; |
| 789 | res.getConfigurations(&configs); |
| 790 | SortedVector<int> densities; |
| 791 | const size_t NC = configs.size(); |
| 792 | for (size_t i=0; i<NC; i++) { |
| 793 | int dens = configs[i].density; |
| 794 | if (dens == 0) dens = 160; |
| 795 | densities.add(dens); |
| 796 | } |
| 797 | |
| 798 | printf("densities:"); |
| 799 | const size_t ND = densities.size(); |
| 800 | for (size_t i=0; i<ND; i++) { |
| 801 | printf(" '%d'", densities[i]); |
| 802 | } |
| 803 | printf("\n"); |
| 804 | |
| 805 | AssetDir* dir = assets.openNonAssetDir(assetsCookie, "lib"); |
| 806 | if (dir != NULL) { |
| 807 | if (dir->getFileCount() > 0) { |
| 808 | printf("native-code:"); |
| 809 | for (size_t i=0; i<dir->getFileCount(); i++) { |
| 810 | printf(" '%s'", dir->getFileName(i).string()); |
| 811 | } |
| 812 | printf("\n"); |
| 813 | } |
| 814 | delete dir; |
| 815 | } |
| 816 | } else if (strcmp("configurations", option) == 0) { |
| 817 | Vector<ResTable_config> configs; |
| 818 | res.getConfigurations(&configs); |
| 819 | const size_t N = configs.size(); |
| 820 | for (size_t i=0; i<N; i++) { |
| 821 | printf("%s\n", configs[i].toString().string()); |
| 822 | } |
| 823 | } else { |
| 824 | fprintf(stderr, "ERROR: unknown dump option '%s'\n", option); |
| 825 | goto bail; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | result = NO_ERROR; |
| 830 | |
| 831 | bail: |
| 832 | if (asset) { |
| 833 | delete asset; |
| 834 | } |
| 835 | return (result != NO_ERROR); |
| 836 | } |
| 837 | |
| 838 | |
| 839 | /* |
| 840 | * Handle the "add" command, which wants to add files to a new or |
| 841 | * pre-existing archive. |
| 842 | */ |
| 843 | int doAdd(Bundle* bundle) |
| 844 | { |
| 845 | ZipFile* zip = NULL; |
| 846 | status_t result = UNKNOWN_ERROR; |
| 847 | const char* zipFileName; |
| 848 | |
| 849 | if (bundle->getUpdate()) { |
| 850 | /* avoid confusion */ |
| 851 | fprintf(stderr, "ERROR: can't use '-u' with add\n"); |
| 852 | goto bail; |
| 853 | } |
| 854 | |
| 855 | if (bundle->getFileSpecCount() < 1) { |
| 856 | fprintf(stderr, "ERROR: must specify zip file name\n"); |
| 857 | goto bail; |
| 858 | } |
| 859 | zipFileName = bundle->getFileSpecEntry(0); |
| 860 | |
| 861 | if (bundle->getFileSpecCount() < 2) { |
| 862 | fprintf(stderr, "NOTE: nothing to do\n"); |
| 863 | goto bail; |
| 864 | } |
| 865 | |
| 866 | zip = openReadWrite(zipFileName, true); |
| 867 | if (zip == NULL) { |
| 868 | fprintf(stderr, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName); |
| 869 | goto bail; |
| 870 | } |
| 871 | |
| 872 | for (int i = 1; i < bundle->getFileSpecCount(); i++) { |
| 873 | const char* fileName = bundle->getFileSpecEntry(i); |
| 874 | |
| 875 | if (strcasecmp(String8(fileName).getPathExtension().string(), ".gz") == 0) { |
| 876 | printf(" '%s'... (from gzip)\n", fileName); |
| 877 | result = zip->addGzip(fileName, String8(fileName).getBasePath().string(), NULL); |
| 878 | } else { |
| 879 | printf(" '%s'...\n", fileName); |
| 880 | result = zip->add(fileName, bundle->getCompressionMethod(), NULL); |
| 881 | } |
| 882 | if (result != NO_ERROR) { |
| 883 | fprintf(stderr, "Unable to add '%s' to '%s'", bundle->getFileSpecEntry(i), zipFileName); |
| 884 | if (result == NAME_NOT_FOUND) |
| 885 | fprintf(stderr, ": file not found\n"); |
| 886 | else if (result == ALREADY_EXISTS) |
| 887 | fprintf(stderr, ": already exists in archive\n"); |
| 888 | else |
| 889 | fprintf(stderr, "\n"); |
| 890 | goto bail; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | result = NO_ERROR; |
| 895 | |
| 896 | bail: |
| 897 | delete zip; |
| 898 | return (result != NO_ERROR); |
| 899 | } |
| 900 | |
| 901 | |
| 902 | /* |
| 903 | * Delete files from an existing archive. |
| 904 | */ |
| 905 | int doRemove(Bundle* bundle) |
| 906 | { |
| 907 | ZipFile* zip = NULL; |
| 908 | status_t result = UNKNOWN_ERROR; |
| 909 | const char* zipFileName; |
| 910 | |
| 911 | if (bundle->getFileSpecCount() < 1) { |
| 912 | fprintf(stderr, "ERROR: must specify zip file name\n"); |
| 913 | goto bail; |
| 914 | } |
| 915 | zipFileName = bundle->getFileSpecEntry(0); |
| 916 | |
| 917 | if (bundle->getFileSpecCount() < 2) { |
| 918 | fprintf(stderr, "NOTE: nothing to do\n"); |
| 919 | goto bail; |
| 920 | } |
| 921 | |
| 922 | zip = openReadWrite(zipFileName, false); |
| 923 | if (zip == NULL) { |
| 924 | fprintf(stderr, "ERROR: failed opening Zip archive '%s'\n", |
| 925 | zipFileName); |
| 926 | goto bail; |
| 927 | } |
| 928 | |
| 929 | for (int i = 1; i < bundle->getFileSpecCount(); i++) { |
| 930 | const char* fileName = bundle->getFileSpecEntry(i); |
| 931 | ZipEntry* entry; |
| 932 | |
| 933 | entry = zip->getEntryByName(fileName); |
| 934 | if (entry == NULL) { |
| 935 | printf(" '%s' NOT FOUND\n", fileName); |
| 936 | continue; |
| 937 | } |
| 938 | |
| 939 | result = zip->remove(entry); |
| 940 | |
| 941 | if (result != NO_ERROR) { |
| 942 | fprintf(stderr, "Unable to delete '%s' from '%s'\n", |
| 943 | bundle->getFileSpecEntry(i), zipFileName); |
| 944 | goto bail; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | /* update the archive */ |
| 949 | zip->flush(); |
| 950 | |
| 951 | bail: |
| 952 | delete zip; |
| 953 | return (result != NO_ERROR); |
| 954 | } |
| 955 | |
| 956 | |
| 957 | /* |
| 958 | * Package up an asset directory and associated application files. |
| 959 | */ |
| 960 | int doPackage(Bundle* bundle) |
| 961 | { |
| 962 | const char* outputAPKFile; |
| 963 | int retVal = 1; |
| 964 | status_t err; |
| 965 | sp<AaptAssets> assets; |
| 966 | int N; |
| 967 | |
| 968 | // -c zz_ZZ means do pseudolocalization |
| 969 | ResourceFilter filter; |
| 970 | err = filter.parse(bundle->getConfigurations()); |
| 971 | if (err != NO_ERROR) { |
| 972 | goto bail; |
| 973 | } |
| 974 | if (filter.containsPseudo()) { |
| 975 | bundle->setPseudolocalize(true); |
| 976 | } |
| 977 | |
| 978 | N = bundle->getFileSpecCount(); |
| 979 | if (N < 1 && bundle->getResourceSourceDirs().size() == 0 && bundle->getJarFiles().size() == 0 |
| 980 | && bundle->getAndroidManifestFile() == NULL && bundle->getAssetSourceDir() == NULL) { |
| 981 | fprintf(stderr, "ERROR: no input files\n"); |
| 982 | goto bail; |
| 983 | } |
| 984 | |
| 985 | outputAPKFile = bundle->getOutputAPKFile(); |
| 986 | |
| 987 | // Make sure the filenames provided exist and are of the appropriate type. |
| 988 | if (outputAPKFile) { |
| 989 | FileType type; |
| 990 | type = getFileType(outputAPKFile); |
| 991 | if (type != kFileTypeNonexistent && type != kFileTypeRegular) { |
| 992 | fprintf(stderr, |
| 993 | "ERROR: output file '%s' exists but is not regular file\n", |
| 994 | outputAPKFile); |
| 995 | goto bail; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | // Load the assets. |
| 1000 | assets = new AaptAssets(); |
| 1001 | err = assets->slurpFromArgs(bundle); |
| 1002 | if (err < 0) { |
| 1003 | goto bail; |
| 1004 | } |
| 1005 | |
| 1006 | if (bundle->getVerbose()) { |
| 1007 | assets->print(); |
| 1008 | } |
| 1009 | |
| 1010 | // If they asked for any files that need to be compiled, do so. |
| 1011 | if (bundle->getResourceSourceDirs().size() || bundle->getAndroidManifestFile()) { |
| 1012 | err = buildResources(bundle, assets); |
| 1013 | if (err != 0) { |
| 1014 | goto bail; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | // At this point we've read everything and processed everything. From here |
| 1019 | // on out it's just writing output files. |
| 1020 | if (SourcePos::hasErrors()) { |
| 1021 | goto bail; |
| 1022 | } |
| 1023 | |
| 1024 | // Write out R.java constants |
| 1025 | if (assets->getPackage() == assets->getSymbolsPrivatePackage()) { |
| 1026 | err = writeResourceSymbols(bundle, assets, assets->getPackage(), true); |
| 1027 | if (err < 0) { |
| 1028 | goto bail; |
| 1029 | } |
| 1030 | } else { |
| 1031 | err = writeResourceSymbols(bundle, assets, assets->getPackage(), false); |
| 1032 | if (err < 0) { |
| 1033 | goto bail; |
| 1034 | } |
| 1035 | err = writeResourceSymbols(bundle, assets, assets->getSymbolsPrivatePackage(), true); |
| 1036 | if (err < 0) { |
| 1037 | goto bail; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // Write the apk |
| 1042 | if (outputAPKFile) { |
| 1043 | err = writeAPK(bundle, assets, String8(outputAPKFile)); |
| 1044 | if (err != NO_ERROR) { |
| 1045 | fprintf(stderr, "ERROR: packaging of '%s' failed\n", outputAPKFile); |
| 1046 | goto bail; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | retVal = 0; |
| 1051 | bail: |
| 1052 | if (SourcePos::hasErrors()) { |
| 1053 | SourcePos::printErrors(stderr); |
| 1054 | } |
| 1055 | return retVal; |
| 1056 | } |