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