]> git.saurik.com Git - android/aapt.git/blame - Command.cpp
am eb0e0f46: am 2e7427f3: Merge "Add LFH offset to verbose list of APK" into gingerbread
[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(
4838f309 144 " Length Method Size Ratio Offset Date Time CRC-32 Name\n");
a534180c 145 printf(
4838f309 146 "-------- ------ ------- ----- ------- ---- ---- ------ ----\n");
a534180c
TAOSP
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
4838f309 162 printf("%8ld %-7.7s %7ld %3d%% %8zd %s %08lx %s\n",
a534180c
TAOSP
163 (long) entry->getUncompressedLen(),
164 compressionName(entry->getCompressionMethod()),
165 (long) entry->getCompressedLen(),
166 calcPercent(entry->getUncompressedLen(),
167 entry->getCompressedLen()),
4838f309 168 (size_t) entry->getLFHOffset(),
a534180c
TAOSP
169 dateBuf,
170 entry->getCRC32(),
171 entry->getFileName());
172 } else {
173 printf("%s\n", entry->getFileName());
174 }
175
176 totalUncLen += entry->getUncompressedLen();
177 totalCompLen += entry->getCompressedLen();
178 }
179
180 if (bundle->getVerbose()) {
181 printf(
182 "-------- ------- --- -------\n");
183 printf("%8ld %7ld %2d%% %d files\n",
184 totalUncLen,
185 totalCompLen,
186 calcPercent(totalUncLen, totalCompLen),
187 zip->getNumEntries());
188 }
189
190 if (bundle->getAndroidList()) {
191 AssetManager assets;
192 if (!assets.addAssetPath(String8(zipFileName), NULL)) {
193 fprintf(stderr, "ERROR: list -a failed because assets could not be loaded\n");
194 goto bail;
195 }
36b80681 196
a534180c
TAOSP
197 const ResTable& res = assets.getResources(false);
198 if (&res == NULL) {
199 printf("\nNo resource table found.\n");
200 } else {
c70f9935 201#ifndef HAVE_ANDROID_OS
a534180c 202 printf("\nResource table:\n");
7751daa4 203 res.print(false);
c70f9935 204#endif
a534180c 205 }
36b80681 206
a534180c
TAOSP
207 Asset* manifestAsset = assets.openNonAsset("AndroidManifest.xml",
208 Asset::ACCESS_BUFFER);
209 if (manifestAsset == NULL) {
210 printf("\nNo AndroidManifest.xml found.\n");
211 } else {
212 printf("\nAndroid manifest:\n");
213 ResXMLTree tree;
214 tree.setTo(manifestAsset->getBuffer(true),
215 manifestAsset->getLength());
216 printXMLBlock(&tree);
217 }
218 delete manifestAsset;
219 }
36b80681 220
a534180c
TAOSP
221 result = 0;
222
223bail:
224 delete zip;
225 return result;
226}
227
228static ssize_t indexOfAttribute(const ResXMLTree& tree, uint32_t attrRes)
229{
230 size_t N = tree.getAttributeCount();
231 for (size_t i=0; i<N; i++) {
232 if (tree.getAttributeNameResID(i) == attrRes) {
233 return (ssize_t)i;
234 }
235 }
236 return -1;
237}
238
6648ff78 239String8 getAttribute(const ResXMLTree& tree, const char* ns,
a534180c
TAOSP
240 const char* attr, String8* outError)
241{
242 ssize_t idx = tree.indexOfAttribute(ns, attr);
243 if (idx < 0) {
244 return String8();
245 }
246 Res_value value;
247 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
248 if (value.dataType != Res_value::TYPE_STRING) {
249 if (outError != NULL) *outError = "attribute is not a string value";
250 return String8();
251 }
252 }
253 size_t len;
254 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
255 return str ? String8(str, len) : String8();
256}
257
258static String8 getAttribute(const ResXMLTree& tree, uint32_t attrRes, String8* outError)
259{
260 ssize_t idx = indexOfAttribute(tree, attrRes);
261 if (idx < 0) {
262 return String8();
263 }
264 Res_value value;
265 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
266 if (value.dataType != Res_value::TYPE_STRING) {
267 if (outError != NULL) *outError = "attribute is not a string value";
268 return String8();
269 }
270 }
271 size_t len;
272 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
273 return str ? String8(str, len) : String8();
274}
275
59308677
DH
276static int32_t getIntegerAttribute(const ResXMLTree& tree, uint32_t attrRes,
277 String8* outError, int32_t defValue = -1)
a534180c
TAOSP
278{
279 ssize_t idx = indexOfAttribute(tree, attrRes);
280 if (idx < 0) {
59308677 281 return defValue;
a534180c
TAOSP
282 }
283 Res_value value;
284 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
59308677
DH
285 if (value.dataType < Res_value::TYPE_FIRST_INT
286 || value.dataType > Res_value::TYPE_LAST_INT) {
a534180c 287 if (outError != NULL) *outError = "attribute is not an integer value";
59308677 288 return defValue;
a534180c
TAOSP
289 }
290 }
291 return value.data;
292}
293
294static String8 getResolvedAttribute(const ResTable* resTable, const ResXMLTree& tree,
295 uint32_t attrRes, String8* outError)
296{
297 ssize_t idx = indexOfAttribute(tree, attrRes);
298 if (idx < 0) {
299 return String8();
300 }
301 Res_value value;
302 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
303 if (value.dataType == Res_value::TYPE_STRING) {
304 size_t len;
305 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
306 return str ? String8(str, len) : String8();
307 }
308 resTable->resolveReference(&value, 0);
309 if (value.dataType != Res_value::TYPE_STRING) {
310 if (outError != NULL) *outError = "attribute is not a string value";
311 return String8();
312 }
313 }
314 size_t len;
315 const Res_value* value2 = &value;
316 const char16_t* str = const_cast<ResTable*>(resTable)->valueToString(value2, 0, NULL, &len);
317 return str ? String8(str, len) : String8();
318}
319
320// These are attribute resource constants for the platform, as found
321// in android.R.attr
322enum {
323 NAME_ATTR = 0x01010003,
324 VERSION_CODE_ATTR = 0x0101021b,
325 VERSION_NAME_ATTR = 0x0101021c,
326 LABEL_ATTR = 0x01010001,
327 ICON_ATTR = 0x01010002,
59308677 328 MIN_SDK_VERSION_ATTR = 0x0101020c,
b6cda18d 329 MAX_SDK_VERSION_ATTR = 0x01010271,
59308677
DH
330 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
331 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
332 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
333 REQ_NAVIGATION_ATTR = 0x0101022a,
334 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
335 TARGET_SDK_VERSION_ATTR = 0x01010270,
336 TEST_ONLY_ATTR = 0x01010272,
337 DENSITY_ATTR = 0x0101026c,
7e029053 338 GL_ES_VERSION_ATTR = 0x01010281,
7ea6703c
DH
339 SMALL_SCREEN_ATTR = 0x01010284,
340 NORMAL_SCREEN_ATTR = 0x01010285,
341 LARGE_SCREEN_ATTR = 0x01010286,
74fa3103 342 XLARGE_SCREEN_ATTR = 0x010102bf,
7e029053 343 REQUIRED_ATTR = 0x0101028e,
a534180c
TAOSP
344};
345
36b80681
SA
346const char *getComponentName(String8 &pkgName, String8 &componentName) {
347 ssize_t idx = componentName.find(".");
348 String8 retStr(pkgName);
349 if (idx == 0) {
350 retStr += componentName;
351 } else if (idx < 0) {
352 retStr += ".";
353 retStr += componentName;
354 } else {
355 return componentName.string();
356 }
357 return retStr.string();
358}
359
a534180c
TAOSP
360/*
361 * Handle the "dump" command, to extract select data from an archive.
362 */
363int doDump(Bundle* bundle)
364{
365 status_t result = UNKNOWN_ERROR;
366 Asset* asset = NULL;
36b80681 367
a534180c
TAOSP
368 if (bundle->getFileSpecCount() < 1) {
369 fprintf(stderr, "ERROR: no dump option specified\n");
370 return 1;
371 }
36b80681 372
a534180c
TAOSP
373 if (bundle->getFileSpecCount() < 2) {
374 fprintf(stderr, "ERROR: no dump file specified\n");
375 return 1;
376 }
36b80681 377
a534180c
TAOSP
378 const char* option = bundle->getFileSpecEntry(0);
379 const char* filename = bundle->getFileSpecEntry(1);
36b80681 380
a534180c 381 AssetManager assets;
59308677
DH
382 void* assetsCookie;
383 if (!assets.addAssetPath(String8(filename), &assetsCookie)) {
a534180c
TAOSP
384 fprintf(stderr, "ERROR: dump failed because assets could not be loaded\n");
385 return 1;
386 }
36b80681 387
a534180c
TAOSP
388 const ResTable& res = assets.getResources(false);
389 if (&res == NULL) {
390 fprintf(stderr, "ERROR: dump failed because no resource table was found\n");
391 goto bail;
392 }
36b80681 393
a534180c 394 if (strcmp("resources", option) == 0) {
c70f9935 395#ifndef HAVE_ANDROID_OS
7751daa4 396 res.print(bundle->getValues());
c70f9935 397#endif
a534180c
TAOSP
398 } else if (strcmp("xmltree", option) == 0) {
399 if (bundle->getFileSpecCount() < 3) {
400 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
401 goto bail;
402 }
36b80681 403
a534180c
TAOSP
404 for (int i=2; i<bundle->getFileSpecCount(); i++) {
405 const char* resname = bundle->getFileSpecEntry(i);
406 ResXMLTree tree;
407 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
408 if (asset == NULL) {
7a97f12f 409 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
a534180c
TAOSP
410 goto bail;
411 }
36b80681 412
a534180c
TAOSP
413 if (tree.setTo(asset->getBuffer(true),
414 asset->getLength()) != NO_ERROR) {
415 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
416 goto bail;
417 }
418 tree.restart();
419 printXMLBlock(&tree);
15c62a5b 420 tree.uninit();
a534180c
TAOSP
421 delete asset;
422 asset = NULL;
423 }
36b80681 424
a534180c
TAOSP
425 } else if (strcmp("xmlstrings", option) == 0) {
426 if (bundle->getFileSpecCount() < 3) {
427 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
428 goto bail;
429 }
36b80681 430
a534180c
TAOSP
431 for (int i=2; i<bundle->getFileSpecCount(); i++) {
432 const char* resname = bundle->getFileSpecEntry(i);
433 ResXMLTree tree;
434 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
435 if (asset == NULL) {
7a97f12f 436 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
a534180c
TAOSP
437 goto bail;
438 }
36b80681 439
a534180c
TAOSP
440 if (tree.setTo(asset->getBuffer(true),
441 asset->getLength()) != NO_ERROR) {
442 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
443 goto bail;
444 }
445 printStringPool(&tree.getStrings());
446 delete asset;
447 asset = NULL;
448 }
36b80681 449
a534180c
TAOSP
450 } else {
451 ResXMLTree tree;
452 asset = assets.openNonAsset("AndroidManifest.xml",
453 Asset::ACCESS_BUFFER);
454 if (asset == NULL) {
455 fprintf(stderr, "ERROR: dump failed because no AndroidManifest.xml found\n");
456 goto bail;
457 }
36b80681 458
a534180c
TAOSP
459 if (tree.setTo(asset->getBuffer(true),
460 asset->getLength()) != NO_ERROR) {
461 fprintf(stderr, "ERROR: AndroidManifest.xml is corrupt\n");
462 goto bail;
463 }
464 tree.restart();
36b80681 465
a534180c
TAOSP
466 if (strcmp("permissions", option) == 0) {
467 size_t len;
468 ResXMLTree::event_code_t code;
469 int depth = 0;
470 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
471 if (code == ResXMLTree::END_TAG) {
472 depth--;
473 continue;
474 }
475 if (code != ResXMLTree::START_TAG) {
476 continue;
477 }
478 depth++;
479 String8 tag(tree.getElementName(&len));
480 //printf("Depth %d tag %s\n", depth, tag.string());
481 if (depth == 1) {
482 if (tag != "manifest") {
483 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
484 goto bail;
485 }
486 String8 pkg = getAttribute(tree, NULL, "package", NULL);
487 printf("package: %s\n", pkg.string());
488 } else if (depth == 2 && tag == "permission") {
489 String8 error;
490 String8 name = getAttribute(tree, NAME_ATTR, &error);
491 if (error != "") {
492 fprintf(stderr, "ERROR: %s\n", error.string());
493 goto bail;
494 }
495 printf("permission: %s\n", name.string());
496 } else if (depth == 2 && tag == "uses-permission") {
497 String8 error;
498 String8 name = getAttribute(tree, NAME_ATTR, &error);
499 if (error != "") {
500 fprintf(stderr, "ERROR: %s\n", error.string());
501 goto bail;
502 }
503 printf("uses-permission: %s\n", name.string());
504 }
505 }
506 } else if (strcmp("badging", option) == 0) {
507 size_t len;
508 ResXMLTree::event_code_t code;
509 int depth = 0;
510 String8 error;
511 bool withinActivity = false;
512 bool isMainActivity = false;
513 bool isLauncherActivity = false;
a3055f1b 514 bool isSearchable = false;
36b80681
SA
515 bool withinApplication = false;
516 bool withinReceiver = false;
a3055f1b
SA
517 bool withinService = false;
518 bool withinIntentFilter = false;
519 bool hasMainActivity = false;
520 bool hasOtherActivities = false;
521 bool hasOtherReceivers = false;
522 bool hasOtherServices = false;
523 bool hasWallpaperService = false;
524 bool hasImeService = false;
525 bool hasWidgetReceivers = false;
526 bool hasIntentFilter = false;
527 bool actMainActivity = false;
528 bool actWidgetReceivers = false;
529 bool actImeService = false;
530 bool actWallpaperService = false;
7276d4a4
DM
531
532 // This next group of variables is used to implement a group of
533 // backward-compatibility heuristics necessitated by the addition of
534 // some new uses-feature constants in 2.1 and 2.2. In most cases, the
535 // heuristic is "if an app requests a permission but doesn't explicitly
536 // request the corresponding <uses-feature>, presume it's there anyway".
537 bool specCameraFeature = false; // camera-related
538 bool specCameraAutofocusFeature = false;
539 bool reqCameraAutofocusFeature = false;
540 bool reqCameraFlashFeature = false;
7e029053 541 bool hasCameraPermission = false;
7276d4a4
DM
542 bool specLocationFeature = false; // location-related
543 bool specNetworkLocFeature = false;
544 bool reqNetworkLocFeature = false;
af945cf3 545 bool specGpsFeature = false;
7276d4a4
DM
546 bool reqGpsFeature = false;
547 bool hasMockLocPermission = false;
548 bool hasCoarseLocPermission = false;
af945cf3 549 bool hasGpsPermission = false;
7276d4a4
DM
550 bool hasGeneralLocPermission = false;
551 bool specBluetoothFeature = false; // Bluetooth API-related
552 bool hasBluetoothPermission = false;
553 bool specMicrophoneFeature = false; // microphone-related
554 bool hasRecordAudioPermission = false;
555 bool specWiFiFeature = false;
556 bool hasWiFiPermission = false;
557 bool specTelephonyFeature = false; // telephony-related
558 bool reqTelephonySubFeature = false;
559 bool hasTelephonyPermission = false;
560 bool specTouchscreenFeature = false; // touchscreen-related
561 bool specMultitouchFeature = false;
562 bool reqDistinctMultitouchFeature = false;
563 // 2.2 also added some other features that apps can request, but that
564 // have no corresponding permission, so we cannot implement any
565 // back-compatibility heuristic for them. The below are thus unnecessary
566 // (but are retained here for documentary purposes.)
567 //bool specCompassFeature = false;
568 //bool specAccelerometerFeature = false;
569 //bool specProximityFeature = false;
570 //bool specAmbientLightFeature = false;
571 //bool specLiveWallpaperFeature = false;
572
7ea6703c
DH
573 int targetSdk = 0;
574 int smallScreen = 1;
575 int normalScreen = 1;
576 int largeScreen = 1;
74fa3103 577 int xlargeScreen = 1;
36b80681 578 String8 pkg;
a534180c
TAOSP
579 String8 activityName;
580 String8 activityLabel;
581 String8 activityIcon;
36b80681 582 String8 receiverName;
a3055f1b 583 String8 serviceName;
a534180c
TAOSP
584 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
585 if (code == ResXMLTree::END_TAG) {
586 depth--;
a3055f1b
SA
587 if (depth < 2) {
588 withinApplication = false;
589 } else if (depth < 3) {
590 if (withinActivity && isMainActivity && isLauncherActivity) {
591 const char *aName = getComponentName(pkg, activityName);
592 if (aName != NULL) {
593 printf("launchable activity name='%s'", aName);
594 }
595 printf("label='%s' icon='%s'\n",
596 activityLabel.string(),
597 activityIcon.string());
598 }
599 if (!hasIntentFilter) {
600 hasOtherActivities |= withinActivity;
601 hasOtherReceivers |= withinReceiver;
602 hasOtherServices |= withinService;
603 }
604 withinActivity = false;
605 withinService = false;
606 withinReceiver = false;
607 hasIntentFilter = false;
608 isMainActivity = isLauncherActivity = false;
609 } else if (depth < 4) {
610 if (withinIntentFilter) {
611 if (withinActivity) {
612 hasMainActivity |= actMainActivity;
613 hasOtherActivities |= !actMainActivity;
614 } else if (withinReceiver) {
615 hasWidgetReceivers |= actWidgetReceivers;
616 hasOtherReceivers |= !actWidgetReceivers;
617 } else if (withinService) {
618 hasImeService |= actImeService;
619 hasWallpaperService |= actWallpaperService;
620 hasOtherServices |= (!actImeService && !actWallpaperService);
621 }
622 }
623 withinIntentFilter = false;
624 }
a534180c
TAOSP
625 continue;
626 }
627 if (code != ResXMLTree::START_TAG) {
628 continue;
629 }
630 depth++;
631 String8 tag(tree.getElementName(&len));
a3055f1b 632 //printf("Depth %d, %s\n", depth, tag.string());
a534180c
TAOSP
633 if (depth == 1) {
634 if (tag != "manifest") {
635 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
636 goto bail;
637 }
36b80681 638 pkg = getAttribute(tree, NULL, "package", NULL);
a534180c
TAOSP
639 printf("package: name='%s' ", pkg.string());
640 int32_t versionCode = getIntegerAttribute(tree, VERSION_CODE_ATTR, &error);
641 if (error != "") {
642 fprintf(stderr, "ERROR getting 'android:versionCode' attribute: %s\n", error.string());
643 goto bail;
644 }
645 if (versionCode > 0) {
646 printf("versionCode='%d' ", versionCode);
647 } else {
648 printf("versionCode='' ");
649 }
1423619a 650 String8 versionName = getResolvedAttribute(&res, tree, VERSION_NAME_ATTR, &error);
a534180c
TAOSP
651 if (error != "") {
652 fprintf(stderr, "ERROR getting 'android:versionName' attribute: %s\n", error.string());
653 goto bail;
654 }
655 printf("versionName='%s'\n", versionName.string());
36b80681
SA
656 } else if (depth == 2) {
657 withinApplication = false;
658 if (tag == "application") {
659 withinApplication = true;
660 String8 label = 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 printf("application: label='%s' ", label.string());
666 String8 icon = 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 printf("icon='%s'\n", icon.string());
59308677 672 int32_t testOnly = getIntegerAttribute(tree, TEST_ONLY_ATTR, &error, 0);
36b80681 673 if (error != "") {
59308677 674 fprintf(stderr, "ERROR getting 'android:testOnly' attribute: %s\n", error.string());
36b80681
SA
675 goto bail;
676 }
59308677
DH
677 if (testOnly != 0) {
678 printf("testOnly='%d'\n", testOnly);
679 }
680 } else if (tag == "uses-sdk") {
681 int32_t code = getIntegerAttribute(tree, MIN_SDK_VERSION_ATTR, &error);
682 if (error != "") {
683 error = "";
684 String8 name = getResolvedAttribute(&res, tree, MIN_SDK_VERSION_ATTR, &error);
685 if (error != "") {
686 fprintf(stderr, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
687 error.string());
688 goto bail;
689 }
7ea6703c 690 if (name == "Donut") targetSdk = 4;
59308677
DH
691 printf("sdkVersion:'%s'\n", name.string());
692 } else if (code != -1) {
7ea6703c 693 targetSdk = code;
59308677
DH
694 printf("sdkVersion:'%d'\n", code);
695 }
b6cda18d
SA
696 code = getIntegerAttribute(tree, MAX_SDK_VERSION_ATTR, NULL, -1);
697 if (code != -1) {
698 printf("maxSdkVersion:'%d'\n", code);
699 }
59308677
DH
700 code = getIntegerAttribute(tree, TARGET_SDK_VERSION_ATTR, &error);
701 if (error != "") {
702 error = "";
703 String8 name = getResolvedAttribute(&res, tree, TARGET_SDK_VERSION_ATTR, &error);
704 if (error != "") {
705 fprintf(stderr, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
706 error.string());
707 goto bail;
708 }
7ea6703c 709 if (name == "Donut" && targetSdk < 4) targetSdk = 4;
59308677
DH
710 printf("targetSdkVersion:'%s'\n", name.string());
711 } else if (code != -1) {
7ea6703c
DH
712 if (targetSdk < code) {
713 targetSdk = code;
714 }
59308677
DH
715 printf("targetSdkVersion:'%d'\n", code);
716 }
717 } else if (tag == "uses-configuration") {
718 int32_t reqTouchScreen = getIntegerAttribute(tree,
719 REQ_TOUCH_SCREEN_ATTR, NULL, 0);
720 int32_t reqKeyboardType = getIntegerAttribute(tree,
721 REQ_KEYBOARD_TYPE_ATTR, NULL, 0);
722 int32_t reqHardKeyboard = getIntegerAttribute(tree,
723 REQ_HARD_KEYBOARD_ATTR, NULL, 0);
724 int32_t reqNavigation = getIntegerAttribute(tree,
725 REQ_NAVIGATION_ATTR, NULL, 0);
726 int32_t reqFiveWayNav = getIntegerAttribute(tree,
727 REQ_FIVE_WAY_NAV_ATTR, NULL, 0);
9c0366d0 728 printf("uses-configuration:");
59308677
DH
729 if (reqTouchScreen != 0) {
730 printf(" reqTouchScreen='%d'", reqTouchScreen);
731 }
732 if (reqKeyboardType != 0) {
733 printf(" reqKeyboardType='%d'", reqKeyboardType);
734 }
735 if (reqHardKeyboard != 0) {
736 printf(" reqHardKeyboard='%d'", reqHardKeyboard);
737 }
738 if (reqNavigation != 0) {
739 printf(" reqNavigation='%d'", reqNavigation);
36b80681 740 }
59308677
DH
741 if (reqFiveWayNav != 0) {
742 printf(" reqFiveWayNav='%d'", reqFiveWayNav);
743 }
744 printf("\n");
745 } else if (tag == "supports-density") {
746 int32_t dens = getIntegerAttribute(tree, DENSITY_ATTR, &error);
747 if (error != "") {
748 fprintf(stderr, "ERROR getting 'android:density' attribute: %s\n",
749 error.string());
750 goto bail;
751 }
752 printf("supports-density:'%d'\n", dens);
7ea6703c
DH
753 } else if (tag == "supports-screens") {
754 smallScreen = getIntegerAttribute(tree,
755 SMALL_SCREEN_ATTR, NULL, 1);
756 normalScreen = getIntegerAttribute(tree,
757 NORMAL_SCREEN_ATTR, NULL, 1);
758 largeScreen = getIntegerAttribute(tree,
759 LARGE_SCREEN_ATTR, NULL, 1);
74fa3103
DH
760 xlargeScreen = getIntegerAttribute(tree,
761 XLARGE_SCREEN_ATTR, NULL, 1);
7e029053
DH
762 } else if (tag == "uses-feature") {
763 String8 name = getAttribute(tree, NAME_ATTR, &error);
774d89bf
SA
764
765 if (name != "" && error == "") {
7e029053
DH
766 int req = getIntegerAttribute(tree,
767 REQUIRED_ATTR, NULL, 1);
7276d4a4 768
7e029053
DH
769 if (name == "android.hardware.camera") {
770 specCameraFeature = true;
7276d4a4
DM
771 } else if (name == "android.hardware.camera.autofocus") {
772 // these have no corresponding permission to check for,
773 // but should imply the foundational camera permission
774 reqCameraAutofocusFeature = reqCameraAutofocusFeature || req;
775 specCameraAutofocusFeature = true;
776 } else if (req && (name == "android.hardware.camera.flash")) {
777 // these have no corresponding permission to check for,
778 // but should imply the foundational camera permission
779 reqCameraFlashFeature = true;
780 } else if (name == "android.hardware.location") {
781 specLocationFeature = true;
782 } else if (name == "android.hardware.location.network") {
783 specNetworkLocFeature = true;
784 reqNetworkLocFeature = reqNetworkLocFeature || req;
af945cf3
DH
785 } else if (name == "android.hardware.location.gps") {
786 specGpsFeature = true;
7276d4a4
DM
787 reqGpsFeature = reqGpsFeature || req;
788 } else if (name == "android.hardware.bluetooth") {
789 specBluetoothFeature = true;
790 } else if (name == "android.hardware.touchscreen") {
791 specTouchscreenFeature = true;
792 } else if (name == "android.hardware.touchscreen.multitouch") {
793 specMultitouchFeature = true;
794 } else if (name == "android.hardware.touchscreen.multitouch.distinct") {
795 reqDistinctMultitouchFeature = reqDistinctMultitouchFeature || req;
796 } else if (name == "android.hardware.microphone") {
797 specMicrophoneFeature = true;
798 } else if (name == "android.hardware.wifi") {
799 specWiFiFeature = true;
800 } else if (name == "android.hardware.telephony") {
801 specTelephonyFeature = true;
802 } else if (req && (name == "android.hardware.telephony.gsm" ||
803 name == "android.hardware.telephony.cdma")) {
804 // these have no corresponding permission to check for,
805 // but should imply the foundational telephony permission
806 reqTelephonySubFeature = true;
7e029053
DH
807 }
808 printf("uses-feature%s:'%s'\n",
809 req ? "" : "-not-required", name.string());
810 } else {
811 int vers = getIntegerAttribute(tree,
812 GL_ES_VERSION_ATTR, &error);
813 if (error == "") {
814 printf("uses-gl-es:'0x%x'\n", vers);
815 }
816 }
817 } else if (tag == "uses-permission") {
818 String8 name = getAttribute(tree, NAME_ATTR, &error);
774d89bf 819 if (name != "" && error == "") {
7e029053
DH
820 if (name == "android.permission.CAMERA") {
821 hasCameraPermission = true;
af945cf3
DH
822 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
823 hasGpsPermission = true;
7276d4a4
DM
824 } else if (name == "android.permission.ACCESS_MOCK_LOCATION") {
825 hasMockLocPermission = true;
826 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
827 hasCoarseLocPermission = true;
828 } else if (name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
829 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
830 hasGeneralLocPermission = true;
831 } else if (name == "android.permission.BLUETOOTH" ||
832 name == "android.permission.BLUETOOTH_ADMIN") {
833 hasBluetoothPermission = true;
834 } else if (name == "android.permission.RECORD_AUDIO") {
835 hasRecordAudioPermission = true;
836 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
837 name == "android.permission.CHANGE_WIFI_STATE" ||
838 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
839 hasWiFiPermission = true;
840 } else if (name == "android.permission.CALL_PHONE" ||
841 name == "android.permission.CALL_PRIVILEGED" ||
842 name == "android.permission.MODIFY_PHONE_STATE" ||
843 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
844 name == "android.permission.READ_SMS" ||
845 name == "android.permission.RECEIVE_SMS" ||
846 name == "android.permission.RECEIVE_MMS" ||
847 name == "android.permission.RECEIVE_WAP_PUSH" ||
848 name == "android.permission.SEND_SMS" ||
849 name == "android.permission.WRITE_APN_SETTINGS" ||
850 name == "android.permission.WRITE_SMS") {
851 hasTelephonyPermission = true;
7e029053
DH
852 }
853 printf("uses-permission:'%s'\n", name.string());
854 } else {
855 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
856 error.string());
857 goto bail;
858 }
2db8b02f
JH
859 } else if (tag == "original-package") {
860 String8 name = getAttribute(tree, NAME_ATTR, &error);
861 if (name != "" && error == "") {
862 printf("original-package:'%s'\n", name.string());
863 } else {
864 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
865 error.string());
866 goto bail;
867 }
a534180c 868 }
36b80681
SA
869 } else if (depth == 3 && withinApplication) {
870 withinActivity = false;
871 withinReceiver = false;
a3055f1b
SA
872 withinService = false;
873 hasIntentFilter = false;
36b80681
SA
874 if(tag == "activity") {
875 withinActivity = true;
876 activityName = getAttribute(tree, NAME_ATTR, &error);
877 if (error != "") {
878 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
879 goto bail;
880 }
a534180c 881
36b80681
SA
882 activityLabel = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
883 if (error != "") {
884 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
885 goto bail;
886 }
a534180c 887
36b80681 888 activityIcon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
a534180c 889 if (error != "") {
36b80681 890 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
a534180c
TAOSP
891 goto bail;
892 }
36b80681
SA
893 } else if (tag == "uses-library") {
894 String8 libraryName = getAttribute(tree, NAME_ATTR, &error);
895 if (error != "") {
896 fprintf(stderr, "ERROR getting 'android:name' attribute for uses-library: %s\n", error.string());
897 goto bail;
a534180c 898 }
826ff9a6
DH
899 int req = getIntegerAttribute(tree,
900 REQUIRED_ATTR, NULL, 1);
901 printf("uses-library%s:'%s'\n",
902 req ? "" : "-not-required", libraryName.string());
36b80681
SA
903 } else if (tag == "receiver") {
904 withinReceiver = true;
905 receiverName = getAttribute(tree, NAME_ATTR, &error);
906
a534180c 907 if (error != "") {
36b80681 908 fprintf(stderr, "ERROR getting 'android:name' attribute for receiver: %s\n", error.string());
a534180c
TAOSP
909 goto bail;
910 }
a3055f1b
SA
911 } else if (tag == "service") {
912 withinService = true;
913 serviceName = getAttribute(tree, NAME_ATTR, &error);
914
915 if (error != "") {
916 fprintf(stderr, "ERROR getting 'android:name' attribute for service: %s\n", error.string());
917 goto bail;
918 }
36b80681 919 }
a3055f1b
SA
920 } else if ((depth == 4) && (tag == "intent-filter")) {
921 hasIntentFilter = true;
922 withinIntentFilter = true;
923 actMainActivity = actWidgetReceivers = actImeService = actWallpaperService = false;
924 } else if ((depth == 5) && withinIntentFilter){
925 String8 action;
926 if (tag == "action") {
927 action = getAttribute(tree, NAME_ATTR, &error);
928 if (error != "") {
929 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
930 goto bail;
931 }
932 if (withinActivity) {
59308677
DH
933 if (action == "android.intent.action.MAIN") {
934 isMainActivity = true;
a3055f1b 935 actMainActivity = true;
59308677 936 }
a3055f1b
SA
937 } else if (withinReceiver) {
938 if (action == "android.appwidget.action.APPWIDGET_UPDATE") {
939 actWidgetReceivers = true;
36b80681 940 }
a3055f1b
SA
941 } else if (withinService) {
942 if (action == "android.view.InputMethod") {
943 actImeService = true;
944 } else if (action == "android.service.wallpaper.WallpaperService") {
945 actWallpaperService = true;
36b80681
SA
946 }
947 }
a3055f1b
SA
948 if (action == "android.intent.action.SEARCH") {
949 isSearchable = true;
36b80681 950 }
a534180c 951 }
a534180c 952
a3055f1b
SA
953 if (tag == "category") {
954 String8 category = getAttribute(tree, NAME_ATTR, &error);
955 if (error != "") {
956 fprintf(stderr, "ERROR getting 'name' attribute: %s\n", error.string());
957 goto bail;
958 }
959 if (withinActivity) {
960 if (category == "android.intent.category.LAUNCHER") {
961 isLauncherActivity = true;
962 }
963 }
36b80681 964 }
a534180c
TAOSP
965 }
966 }
a3055f1b 967
7276d4a4
DM
968 /* The following blocks handle printing "inferred" uses-features, based
969 * on whether related features or permissions are used by the app.
970 * Note that the various spec*Feature variables denote whether the
971 * relevant tag was *present* in the AndroidManfest, not that it was
972 * present and set to true.
973 */
974 // Camera-related back-compatibility logic
975 if (!specCameraFeature) {
976 if (reqCameraFlashFeature || reqCameraAutofocusFeature) {
977 // if app requested a sub-feature (autofocus or flash) and didn't
978 // request the base camera feature, we infer that it meant to
979 printf("uses-feature:'android.hardware.camera'\n");
980 } else if (hasCameraPermission) {
981 // if app wants to use camera but didn't request the feature, we infer
982 // that it meant to, and further that it wants autofocus
983 // (which was the 1.0 - 1.5 behavior)
984 printf("uses-feature:'android.hardware.camera'\n");
985 if (!specCameraAutofocusFeature) {
986 printf("uses-feature:'android.hardware.camera.autofocus'\n");
987 }
988 }
7e029053 989 }
1e8883fc 990
7276d4a4
DM
991 // Location-related back-compatibility logic
992 if (!specLocationFeature &&
993 (hasMockLocPermission || hasCoarseLocPermission || hasGpsPermission ||
994 hasGeneralLocPermission || reqNetworkLocFeature || reqGpsFeature)) {
995 // if app either takes a location-related permission or requests one of the
996 // sub-features, we infer that it also meant to request the base location feature
997 printf("uses-feature:'android.hardware.location'\n");
998 }
af945cf3 999 if (!specGpsFeature && hasGpsPermission) {
7276d4a4
DM
1000 // if app takes GPS (FINE location) perm but does not request the GPS
1001 // feature, we infer that it meant to
af945cf3
DH
1002 printf("uses-feature:'android.hardware.location.gps'\n");
1003 }
7276d4a4
DM
1004 if (!specNetworkLocFeature && hasCoarseLocPermission) {
1005 // if app takes Network location (COARSE location) perm but does not request the
1006 // network location feature, we infer that it meant to
1007 printf("uses-feature:'android.hardware.location.network'\n");
1008 }
1009
1010 // Bluetooth-related compatibility logic
b1b45912 1011 if (!specBluetoothFeature && hasBluetoothPermission && (targetSdk > 4)) {
7276d4a4
DM
1012 // if app takes a Bluetooth permission but does not request the Bluetooth
1013 // feature, we infer that it meant to
1014 printf("uses-feature:'android.hardware.bluetooth'\n");
1015 }
1016
1017 // Microphone-related compatibility logic
1018 if (!specMicrophoneFeature && hasRecordAudioPermission) {
1019 // if app takes the record-audio permission but does not request the microphone
1020 // feature, we infer that it meant to
1021 printf("uses-feature:'android.hardware.microphone'\n");
1022 }
1023
1024 // WiFi-related compatibility logic
1025 if (!specWiFiFeature && hasWiFiPermission) {
1026 // if app takes one of the WiFi permissions but does not request the WiFi
1027 // feature, we infer that it meant to
1028 printf("uses-feature:'android.hardware.wifi'\n");
1029 }
1030
1031 // Telephony-related compatibility logic
1032 if (!specTelephonyFeature && (hasTelephonyPermission || reqTelephonySubFeature)) {
1033 // if app takes one of the telephony permissions or requests a sub-feature but
1034 // does not request the base telephony feature, we infer that it meant to
1035 printf("uses-feature:'android.hardware.telephony'\n");
1036 }
1037
1038 // Touchscreen-related back-compatibility logic
1039 if (!specTouchscreenFeature) { // not a typo!
1040 // all apps are presumed to require a touchscreen, unless they explicitly say
1041 // <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
1042 // Note that specTouchscreenFeature is true if the tag is present, regardless
1043 // of whether its value is true or false, so this is safe
1044 printf("uses-feature:'android.hardware.touchscreen'\n");
1045 }
1046 if (!specMultitouchFeature && reqDistinctMultitouchFeature) {
1047 // if app takes one of the telephony permissions or requests a sub-feature but
1048 // does not request the base telephony feature, we infer that it meant to
1049 printf("uses-feature:'android.hardware.touchscreen.multitouch'\n");
1050 }
af945cf3 1051
a3055f1b
SA
1052 if (hasMainActivity) {
1053 printf("main\n");
1054 }
1055 if (hasWidgetReceivers) {
1056 printf("app-widget\n");
1057 }
1058 if (hasImeService) {
1059 printf("ime\n");
1060 }
1061 if (hasWallpaperService) {
1062 printf("wallpaper\n");
1063 }
1064 if (hasOtherActivities) {
1065 printf("other-activities\n");
1066 }
1067 if (isSearchable) {
1068 printf("search\n");
1069 }
1070 if (hasOtherReceivers) {
1071 printf("other-receivers\n");
1072 }
1073 if (hasOtherServices) {
1074 printf("other-services\n");
1075 }
1076
7ea6703c
DH
1077 // Determine default values for any unspecified screen sizes,
1078 // based on the target SDK of the package. As of 4 (donut)
1079 // the screen size support was introduced, so all default to
1080 // enabled.
1081 if (smallScreen > 0) {
1082 smallScreen = targetSdk >= 4 ? -1 : 0;
1083 }
1084 if (normalScreen > 0) {
1085 normalScreen = -1;
1086 }
1087 if (largeScreen > 0) {
1088 largeScreen = targetSdk >= 4 ? -1 : 0;
1089 }
74fa3103
DH
1090 if (xlargeScreen > 0) {
1091 // Introduced in Honeycomb.
1092 xlargeScreen = targetSdk >= 10 ? -1 : 0;
1093 }
7ea6703c
DH
1094 printf("supports-screens:");
1095 if (smallScreen != 0) printf(" 'small'");
1096 if (normalScreen != 0) printf(" 'normal'");
1097 if (largeScreen != 0) printf(" 'large'");
74fa3103 1098 if (xlargeScreen != 0) printf(" 'xlarge'");
7ea6703c 1099 printf("\n");
a3055f1b 1100
a534180c
TAOSP
1101 printf("locales:");
1102 Vector<String8> locales;
1103 res.getLocales(&locales);
7751daa4
DH
1104 const size_t NL = locales.size();
1105 for (size_t i=0; i<NL; i++) {
a534180c
TAOSP
1106 const char* localeStr = locales[i].string();
1107 if (localeStr == NULL || strlen(localeStr) == 0) {
1108 localeStr = "--_--";
1109 }
1110 printf(" '%s'", localeStr);
1111 }
1112 printf("\n");
a3055f1b 1113
7751daa4
DH
1114 Vector<ResTable_config> configs;
1115 res.getConfigurations(&configs);
1116 SortedVector<int> densities;
1117 const size_t NC = configs.size();
1118 for (size_t i=0; i<NC; i++) {
1119 int dens = configs[i].density;
1120 if (dens == 0) dens = 160;
1121 densities.add(dens);
1122 }
a3055f1b 1123
7751daa4
DH
1124 printf("densities:");
1125 const size_t ND = densities.size();
1126 for (size_t i=0; i<ND; i++) {
1127 printf(" '%d'", densities[i]);
1128 }
1129 printf("\n");
a3055f1b 1130
59308677
DH
1131 AssetDir* dir = assets.openNonAssetDir(assetsCookie, "lib");
1132 if (dir != NULL) {
1133 if (dir->getFileCount() > 0) {
1134 printf("native-code:");
1135 for (size_t i=0; i<dir->getFileCount(); i++) {
1136 printf(" '%s'", dir->getFileName(i).string());
1137 }
1138 printf("\n");
1139 }
1140 delete dir;
1141 }
a534180c
TAOSP
1142 } else if (strcmp("configurations", option) == 0) {
1143 Vector<ResTable_config> configs;
1144 res.getConfigurations(&configs);
1145 const size_t N = configs.size();
1146 for (size_t i=0; i<N; i++) {
1147 printf("%s\n", configs[i].toString().string());
1148 }
1149 } else {
1150 fprintf(stderr, "ERROR: unknown dump option '%s'\n", option);
1151 goto bail;
1152 }
1153 }
1154
1155 result = NO_ERROR;
36b80681 1156
a534180c
TAOSP
1157bail:
1158 if (asset) {
1159 delete asset;
1160 }
1161 return (result != NO_ERROR);
1162}
1163
1164
1165/*
1166 * Handle the "add" command, which wants to add files to a new or
1167 * pre-existing archive.
1168 */
1169int doAdd(Bundle* bundle)
1170{
1171 ZipFile* zip = NULL;
1172 status_t result = UNKNOWN_ERROR;
1173 const char* zipFileName;
1174
1175 if (bundle->getUpdate()) {
1176 /* avoid confusion */
1177 fprintf(stderr, "ERROR: can't use '-u' with add\n");
1178 goto bail;
1179 }
1180
1181 if (bundle->getFileSpecCount() < 1) {
1182 fprintf(stderr, "ERROR: must specify zip file name\n");
1183 goto bail;
1184 }
1185 zipFileName = bundle->getFileSpecEntry(0);
1186
1187 if (bundle->getFileSpecCount() < 2) {
1188 fprintf(stderr, "NOTE: nothing to do\n");
1189 goto bail;
1190 }
1191
1192 zip = openReadWrite(zipFileName, true);
1193 if (zip == NULL) {
1194 fprintf(stderr, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName);
1195 goto bail;
1196 }
1197
1198 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1199 const char* fileName = bundle->getFileSpecEntry(i);
1200
1201 if (strcasecmp(String8(fileName).getPathExtension().string(), ".gz") == 0) {
1202 printf(" '%s'... (from gzip)\n", fileName);
1203 result = zip->addGzip(fileName, String8(fileName).getBasePath().string(), NULL);
1204 } else {
1e8883fc
DZ
1205 if (bundle->getJunkPath()) {
1206 String8 storageName = String8(fileName).getPathLeaf();
1207 printf(" '%s' as '%s'...\n", fileName, storageName.string());
1208 result = zip->add(fileName, storageName.string(),
1209 bundle->getCompressionMethod(), NULL);
1210 } else {
1211 printf(" '%s'...\n", fileName);
1212 result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
1213 }
a534180c
TAOSP
1214 }
1215 if (result != NO_ERROR) {
1216 fprintf(stderr, "Unable to add '%s' to '%s'", bundle->getFileSpecEntry(i), zipFileName);
1217 if (result == NAME_NOT_FOUND)
1218 fprintf(stderr, ": file not found\n");
1219 else if (result == ALREADY_EXISTS)
1220 fprintf(stderr, ": already exists in archive\n");
1221 else
1222 fprintf(stderr, "\n");
1223 goto bail;
1224 }
1225 }
1226
1227 result = NO_ERROR;
1228
1229bail:
1230 delete zip;
1231 return (result != NO_ERROR);
1232}
1233
1234
1235/*
1236 * Delete files from an existing archive.
1237 */
1238int doRemove(Bundle* bundle)
1239{
1240 ZipFile* zip = NULL;
1241 status_t result = UNKNOWN_ERROR;
1242 const char* zipFileName;
1243
1244 if (bundle->getFileSpecCount() < 1) {
1245 fprintf(stderr, "ERROR: must specify zip file name\n");
1246 goto bail;
1247 }
1248 zipFileName = bundle->getFileSpecEntry(0);
1249
1250 if (bundle->getFileSpecCount() < 2) {
1251 fprintf(stderr, "NOTE: nothing to do\n");
1252 goto bail;
1253 }
1254
1255 zip = openReadWrite(zipFileName, false);
1256 if (zip == NULL) {
1257 fprintf(stderr, "ERROR: failed opening Zip archive '%s'\n",
1258 zipFileName);
1259 goto bail;
1260 }
1261
1262 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1263 const char* fileName = bundle->getFileSpecEntry(i);
1264 ZipEntry* entry;
1265
1266 entry = zip->getEntryByName(fileName);
1267 if (entry == NULL) {
1268 printf(" '%s' NOT FOUND\n", fileName);
1269 continue;
1270 }
1271
1272 result = zip->remove(entry);
1273
1274 if (result != NO_ERROR) {
1275 fprintf(stderr, "Unable to delete '%s' from '%s'\n",
1276 bundle->getFileSpecEntry(i), zipFileName);
1277 goto bail;
1278 }
1279 }
1280
1281 /* update the archive */
1282 zip->flush();
1283
1284bail:
1285 delete zip;
1286 return (result != NO_ERROR);
1287}
1288
1289
1290/*
1291 * Package up an asset directory and associated application files.
1292 */
1293int doPackage(Bundle* bundle)
1294{
1295 const char* outputAPKFile;
1296 int retVal = 1;
1297 status_t err;
1298 sp<AaptAssets> assets;
1299 int N;
1300
1301 // -c zz_ZZ means do pseudolocalization
1302 ResourceFilter filter;
1303 err = filter.parse(bundle->getConfigurations());
1304 if (err != NO_ERROR) {
1305 goto bail;
1306 }
1307 if (filter.containsPseudo()) {
1308 bundle->setPseudolocalize(true);
1309 }
1310
1311 N = bundle->getFileSpecCount();
1312 if (N < 1 && bundle->getResourceSourceDirs().size() == 0 && bundle->getJarFiles().size() == 0
1313 && bundle->getAndroidManifestFile() == NULL && bundle->getAssetSourceDir() == NULL) {
1314 fprintf(stderr, "ERROR: no input files\n");
1315 goto bail;
1316 }
1317
1318 outputAPKFile = bundle->getOutputAPKFile();
1319
1320 // Make sure the filenames provided exist and are of the appropriate type.
1321 if (outputAPKFile) {
1322 FileType type;
1323 type = getFileType(outputAPKFile);
1324 if (type != kFileTypeNonexistent && type != kFileTypeRegular) {
1325 fprintf(stderr,
1326 "ERROR: output file '%s' exists but is not regular file\n",
1327 outputAPKFile);
1328 goto bail;
1329 }
1330 }
1331
1332 // Load the assets.
1333 assets = new AaptAssets();
1334 err = assets->slurpFromArgs(bundle);
1335 if (err < 0) {
1336 goto bail;
1337 }
1338
1339 if (bundle->getVerbose()) {
1340 assets->print();
1341 }
1342
1343 // If they asked for any files that need to be compiled, do so.
1344 if (bundle->getResourceSourceDirs().size() || bundle->getAndroidManifestFile()) {
1345 err = buildResources(bundle, assets);
1346 if (err != 0) {
1347 goto bail;
1348 }
1349 }
1350
1351 // At this point we've read everything and processed everything. From here
1352 // on out it's just writing output files.
1353 if (SourcePos::hasErrors()) {
1354 goto bail;
1355 }
1356
1357 // Write out R.java constants
1358 if (assets->getPackage() == assets->getSymbolsPrivatePackage()) {
fa19db5e
XD
1359 if (bundle->getCustomPackage() == NULL) {
1360 err = writeResourceSymbols(bundle, assets, assets->getPackage(), true);
1361 } else {
1362 const String8 customPkg(bundle->getCustomPackage());
1363 err = writeResourceSymbols(bundle, assets, customPkg, true);
1364 }
a534180c
TAOSP
1365 if (err < 0) {
1366 goto bail;
1367 }
1368 } else {
1369 err = writeResourceSymbols(bundle, assets, assets->getPackage(), false);
1370 if (err < 0) {
1371 goto bail;
1372 }
1373 err = writeResourceSymbols(bundle, assets, assets->getSymbolsPrivatePackage(), true);
1374 if (err < 0) {
1375 goto bail;
1376 }
1377 }
1378
6648ff78
JO
1379 // Write out the ProGuard file
1380 err = writeProguardFile(bundle, assets);
1381 if (err < 0) {
1382 goto bail;
1383 }
1384
a534180c
TAOSP
1385 // Write the apk
1386 if (outputAPKFile) {
1387 err = writeAPK(bundle, assets, String8(outputAPKFile));
1388 if (err != NO_ERROR) {
1389 fprintf(stderr, "ERROR: packaging of '%s' failed\n", outputAPKFile);
1390 goto bail;
1391 }
1392 }
1393
1394 retVal = 0;
1395bail:
1396 if (SourcePos::hasErrors()) {
1397 SourcePos::printErrors(stderr);
1398 }
1399 return retVal;
1400}