blob: e9833c993c8306981227bc24ee562ef689e97070 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001//
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
Mathias Agopian3b4062e2009-05-31 19:13:00 -070011#include <utils/Log.h>
12#include <utils/threads.h>
13#include <utils/List.h>
14#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015
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(
Kenny Rootfb2a9462010-08-25 07:36:31 -0700144 " Length Method Size Ratio Offset Date Time CRC-32 Name\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 printf(
Kenny Rootfb2a9462010-08-25 07:36:31 -0700146 "-------- ------ ------- ----- ------- ---- ---- ------ ----\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
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
Kenny Rootfb2a9462010-08-25 07:36:31 -0700162 printf("%8ld %-7.7s %7ld %3d%% %8zd %s %08lx %s\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 (long) entry->getUncompressedLen(),
164 compressionName(entry->getCompressionMethod()),
165 (long) entry->getCompressedLen(),
166 calcPercent(entry->getUncompressedLen(),
167 entry->getCompressedLen()),
Kenny Rootfb2a9462010-08-25 07:36:31 -0700168 (size_t) entry->getLFHOffset(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 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 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 const ResTable& res = assets.getResources(false);
198 if (&res == NULL) {
199 printf("\nNo resource table found.\n");
200 } else {
Steve Blockf1ff21a2010-06-14 17:34:04 +0100201#ifndef HAVE_ANDROID_OS
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 printf("\nResource table:\n");
Dianne Hackborne17086b2009-06-19 15:13:28 -0700203 res.print(false);
Steve Blockf1ff21a2010-06-14 17:34:04 +0100204#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 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 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 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
Joe Onorato1553c822009-08-30 13:36:22 -0700239String8 getAttribute(const ResXMLTree& tree, const char* ns,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 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
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700276static int32_t getIntegerAttribute(const ResXMLTree& tree, uint32_t attrRes,
277 String8* outError, int32_t defValue = -1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278{
279 ssize_t idx = indexOfAttribute(tree, attrRes);
280 if (idx < 0) {
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700281 return defValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
283 Res_value value;
284 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700285 if (value.dataType < Res_value::TYPE_FIRST_INT
286 || value.dataType > Res_value::TYPE_LAST_INT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 if (outError != NULL) *outError = "attribute is not an integer value";
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700288 return defValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 }
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,
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700328 MIN_SDK_VERSION_ATTR = 0x0101020c,
Suchi Amalapurapu75c49842009-08-14 15:13:09 -0700329 MAX_SDK_VERSION_ATTR = 0x01010271,
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700330 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,
Dianne Hackborna0b46c92010-10-21 15:32:06 -0700337 ANY_DENSITY_ATTR = 0x0101026c,
Dianne Hackborne5276a72009-08-27 16:28:44 -0700338 GL_ES_VERSION_ATTR = 0x01010281,
Dianne Hackborn723738c2009-06-25 19:48:04 -0700339 SMALL_SCREEN_ATTR = 0x01010284,
340 NORMAL_SCREEN_ATTR = 0x01010285,
341 LARGE_SCREEN_ATTR = 0x01010286,
Dianne Hackbornf43489d2010-08-20 12:44:33 -0700342 XLARGE_SCREEN_ATTR = 0x010102bf,
Dianne Hackborne5276a72009-08-27 16:28:44 -0700343 REQUIRED_ATTR = 0x0101028e,
Dianne Hackborna0b46c92010-10-21 15:32:06 -0700344 SCREEN_SIZE_ATTR = 0x010102ca,
345 SCREEN_DENSITY_ATTR = 0x010102cb,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346};
347
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700348const char *getComponentName(String8 &pkgName, String8 &componentName) {
349 ssize_t idx = componentName.find(".");
350 String8 retStr(pkgName);
351 if (idx == 0) {
352 retStr += componentName;
353 } else if (idx < 0) {
354 retStr += ".";
355 retStr += componentName;
356 } else {
357 return componentName.string();
358 }
359 return retStr.string();
360}
361
Dianne Hackborna0b46c92010-10-21 15:32:06 -0700362static void printCompatibleScreens(ResXMLTree& tree) {
363 size_t len;
364 ResXMLTree::event_code_t code;
365 int depth = 0;
366 bool first = true;
367 printf("compatible-screens:");
368 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
369 if (code == ResXMLTree::END_TAG) {
370 depth--;
371 if (depth < 0) {
372 break;
373 }
374 continue;
375 }
376 if (code != ResXMLTree::START_TAG) {
377 continue;
378 }
379 depth++;
380 String8 tag(tree.getElementName(&len));
381 if (tag == "screen") {
382 int32_t screenSize = getIntegerAttribute(tree,
383 SCREEN_SIZE_ATTR, NULL, -1);
384 int32_t screenDensity = getIntegerAttribute(tree,
385 SCREEN_DENSITY_ATTR, NULL, -1);
386 if (screenSize > 0 && screenDensity > 0) {
387 if (!first) {
388 printf(",");
389 }
390 first = false;
391 printf("'%d/%d'", screenSize, screenDensity);
392 }
393 }
394 }
395 printf("\n");
396}
397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398/*
399 * Handle the "dump" command, to extract select data from an archive.
400 */
401int doDump(Bundle* bundle)
402{
403 status_t result = UNKNOWN_ERROR;
404 Asset* asset = NULL;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 if (bundle->getFileSpecCount() < 1) {
407 fprintf(stderr, "ERROR: no dump option specified\n");
408 return 1;
409 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 if (bundle->getFileSpecCount() < 2) {
412 fprintf(stderr, "ERROR: no dump file specified\n");
413 return 1;
414 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 const char* option = bundle->getFileSpecEntry(0);
417 const char* filename = bundle->getFileSpecEntry(1);
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 AssetManager assets;
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700420 void* assetsCookie;
421 if (!assets.addAssetPath(String8(filename), &assetsCookie)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 fprintf(stderr, "ERROR: dump failed because assets could not be loaded\n");
423 return 1;
424 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 const ResTable& res = assets.getResources(false);
427 if (&res == NULL) {
428 fprintf(stderr, "ERROR: dump failed because no resource table was found\n");
429 goto bail;
430 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 if (strcmp("resources", option) == 0) {
Steve Blockf1ff21a2010-06-14 17:34:04 +0100433#ifndef HAVE_ANDROID_OS
Dianne Hackborne17086b2009-06-19 15:13:28 -0700434 res.print(bundle->getValues());
Steve Blockf1ff21a2010-06-14 17:34:04 +0100435#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 } else if (strcmp("xmltree", option) == 0) {
437 if (bundle->getFileSpecCount() < 3) {
438 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
439 goto bail;
440 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 for (int i=2; i<bundle->getFileSpecCount(); i++) {
443 const char* resname = bundle->getFileSpecEntry(i);
444 ResXMLTree tree;
445 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
446 if (asset == NULL) {
Kenny Root44b283d2009-09-01 19:03:11 -0500447 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 goto bail;
449 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 if (tree.setTo(asset->getBuffer(true),
452 asset->getLength()) != NO_ERROR) {
453 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
454 goto bail;
455 }
456 tree.restart();
457 printXMLBlock(&tree);
Kenny Root19138462009-12-04 09:38:48 -0800458 tree.uninit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 delete asset;
460 asset = NULL;
461 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 } else if (strcmp("xmlstrings", option) == 0) {
464 if (bundle->getFileSpecCount() < 3) {
465 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
466 goto bail;
467 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 for (int i=2; i<bundle->getFileSpecCount(); i++) {
470 const char* resname = bundle->getFileSpecEntry(i);
471 ResXMLTree tree;
472 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
473 if (asset == NULL) {
Kenny Root44b283d2009-09-01 19:03:11 -0500474 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 goto bail;
476 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 if (tree.setTo(asset->getBuffer(true),
479 asset->getLength()) != NO_ERROR) {
480 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
481 goto bail;
482 }
483 printStringPool(&tree.getStrings());
484 delete asset;
485 asset = NULL;
486 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 } else {
489 ResXMLTree tree;
490 asset = assets.openNonAsset("AndroidManifest.xml",
491 Asset::ACCESS_BUFFER);
492 if (asset == NULL) {
493 fprintf(stderr, "ERROR: dump failed because no AndroidManifest.xml found\n");
494 goto bail;
495 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 if (tree.setTo(asset->getBuffer(true),
498 asset->getLength()) != NO_ERROR) {
499 fprintf(stderr, "ERROR: AndroidManifest.xml is corrupt\n");
500 goto bail;
501 }
502 tree.restart();
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 if (strcmp("permissions", option) == 0) {
505 size_t len;
506 ResXMLTree::event_code_t code;
507 int depth = 0;
508 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
509 if (code == ResXMLTree::END_TAG) {
510 depth--;
511 continue;
512 }
513 if (code != ResXMLTree::START_TAG) {
514 continue;
515 }
516 depth++;
517 String8 tag(tree.getElementName(&len));
518 //printf("Depth %d tag %s\n", depth, tag.string());
519 if (depth == 1) {
520 if (tag != "manifest") {
521 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
522 goto bail;
523 }
524 String8 pkg = getAttribute(tree, NULL, "package", NULL);
525 printf("package: %s\n", pkg.string());
526 } else if (depth == 2 && tag == "permission") {
527 String8 error;
528 String8 name = getAttribute(tree, NAME_ATTR, &error);
529 if (error != "") {
530 fprintf(stderr, "ERROR: %s\n", error.string());
531 goto bail;
532 }
533 printf("permission: %s\n", name.string());
534 } else if (depth == 2 && tag == "uses-permission") {
535 String8 error;
536 String8 name = getAttribute(tree, NAME_ATTR, &error);
537 if (error != "") {
538 fprintf(stderr, "ERROR: %s\n", error.string());
539 goto bail;
540 }
541 printf("uses-permission: %s\n", name.string());
542 }
543 }
544 } else if (strcmp("badging", option) == 0) {
545 size_t len;
546 ResXMLTree::event_code_t code;
547 int depth = 0;
548 String8 error;
549 bool withinActivity = false;
550 bool isMainActivity = false;
551 bool isLauncherActivity = false;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700552 bool isSearchable = false;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700553 bool withinApplication = false;
554 bool withinReceiver = false;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700555 bool withinService = false;
556 bool withinIntentFilter = false;
557 bool hasMainActivity = false;
558 bool hasOtherActivities = false;
559 bool hasOtherReceivers = false;
560 bool hasOtherServices = false;
561 bool hasWallpaperService = false;
562 bool hasImeService = false;
563 bool hasWidgetReceivers = false;
564 bool hasIntentFilter = false;
565 bool actMainActivity = false;
566 bool actWidgetReceivers = false;
567 bool actImeService = false;
568 bool actWallpaperService = false;
Dan Morrill89d97c12010-05-03 16:13:14 -0700569
570 // This next group of variables is used to implement a group of
571 // backward-compatibility heuristics necessitated by the addition of
572 // some new uses-feature constants in 2.1 and 2.2. In most cases, the
573 // heuristic is "if an app requests a permission but doesn't explicitly
574 // request the corresponding <uses-feature>, presume it's there anyway".
575 bool specCameraFeature = false; // camera-related
576 bool specCameraAutofocusFeature = false;
577 bool reqCameraAutofocusFeature = false;
578 bool reqCameraFlashFeature = false;
Dianne Hackborne5276a72009-08-27 16:28:44 -0700579 bool hasCameraPermission = false;
Dan Morrill89d97c12010-05-03 16:13:14 -0700580 bool specLocationFeature = false; // location-related
581 bool specNetworkLocFeature = false;
582 bool reqNetworkLocFeature = false;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800583 bool specGpsFeature = false;
Dan Morrill89d97c12010-05-03 16:13:14 -0700584 bool reqGpsFeature = false;
585 bool hasMockLocPermission = false;
586 bool hasCoarseLocPermission = false;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800587 bool hasGpsPermission = false;
Dan Morrill89d97c12010-05-03 16:13:14 -0700588 bool hasGeneralLocPermission = false;
589 bool specBluetoothFeature = false; // Bluetooth API-related
590 bool hasBluetoothPermission = false;
591 bool specMicrophoneFeature = false; // microphone-related
592 bool hasRecordAudioPermission = false;
593 bool specWiFiFeature = false;
594 bool hasWiFiPermission = false;
595 bool specTelephonyFeature = false; // telephony-related
596 bool reqTelephonySubFeature = false;
597 bool hasTelephonyPermission = false;
598 bool specTouchscreenFeature = false; // touchscreen-related
599 bool specMultitouchFeature = false;
600 bool reqDistinctMultitouchFeature = false;
601 // 2.2 also added some other features that apps can request, but that
602 // have no corresponding permission, so we cannot implement any
603 // back-compatibility heuristic for them. The below are thus unnecessary
604 // (but are retained here for documentary purposes.)
605 //bool specCompassFeature = false;
606 //bool specAccelerometerFeature = false;
607 //bool specProximityFeature = false;
608 //bool specAmbientLightFeature = false;
609 //bool specLiveWallpaperFeature = false;
610
Dianne Hackborn723738c2009-06-25 19:48:04 -0700611 int targetSdk = 0;
612 int smallScreen = 1;
613 int normalScreen = 1;
614 int largeScreen = 1;
Dianne Hackbornf43489d2010-08-20 12:44:33 -0700615 int xlargeScreen = 1;
Dianne Hackborna0b46c92010-10-21 15:32:06 -0700616 int anyDensity = 1;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700617 String8 pkg;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 String8 activityName;
619 String8 activityLabel;
620 String8 activityIcon;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700621 String8 receiverName;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700622 String8 serviceName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
624 if (code == ResXMLTree::END_TAG) {
625 depth--;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700626 if (depth < 2) {
627 withinApplication = false;
628 } else if (depth < 3) {
629 if (withinActivity && isMainActivity && isLauncherActivity) {
630 const char *aName = getComponentName(pkg, activityName);
631 if (aName != NULL) {
632 printf("launchable activity name='%s'", aName);
633 }
634 printf("label='%s' icon='%s'\n",
635 activityLabel.string(),
636 activityIcon.string());
637 }
638 if (!hasIntentFilter) {
639 hasOtherActivities |= withinActivity;
640 hasOtherReceivers |= withinReceiver;
641 hasOtherServices |= withinService;
642 }
643 withinActivity = false;
644 withinService = false;
645 withinReceiver = false;
646 hasIntentFilter = false;
647 isMainActivity = isLauncherActivity = false;
648 } else if (depth < 4) {
649 if (withinIntentFilter) {
650 if (withinActivity) {
651 hasMainActivity |= actMainActivity;
652 hasOtherActivities |= !actMainActivity;
653 } else if (withinReceiver) {
654 hasWidgetReceivers |= actWidgetReceivers;
655 hasOtherReceivers |= !actWidgetReceivers;
656 } else if (withinService) {
657 hasImeService |= actImeService;
658 hasWallpaperService |= actWallpaperService;
659 hasOtherServices |= (!actImeService && !actWallpaperService);
660 }
661 }
662 withinIntentFilter = false;
663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 continue;
665 }
666 if (code != ResXMLTree::START_TAG) {
667 continue;
668 }
669 depth++;
670 String8 tag(tree.getElementName(&len));
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700671 //printf("Depth %d, %s\n", depth, tag.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 if (depth == 1) {
673 if (tag != "manifest") {
674 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
675 goto bail;
676 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700677 pkg = getAttribute(tree, NULL, "package", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 printf("package: name='%s' ", pkg.string());
679 int32_t versionCode = getIntegerAttribute(tree, VERSION_CODE_ATTR, &error);
680 if (error != "") {
681 fprintf(stderr, "ERROR getting 'android:versionCode' attribute: %s\n", error.string());
682 goto bail;
683 }
684 if (versionCode > 0) {
685 printf("versionCode='%d' ", versionCode);
686 } else {
687 printf("versionCode='' ");
688 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800689 String8 versionName = getResolvedAttribute(&res, tree, VERSION_NAME_ATTR, &error);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 if (error != "") {
691 fprintf(stderr, "ERROR getting 'android:versionName' attribute: %s\n", error.string());
692 goto bail;
693 }
694 printf("versionName='%s'\n", versionName.string());
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700695 } else if (depth == 2) {
696 withinApplication = false;
697 if (tag == "application") {
698 withinApplication = true;
699 String8 label = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
700 if (error != "") {
701 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
702 goto bail;
703 }
704 printf("application: label='%s' ", label.string());
705 String8 icon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
706 if (error != "") {
707 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
708 goto bail;
709 }
710 printf("icon='%s'\n", icon.string());
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700711 int32_t testOnly = getIntegerAttribute(tree, TEST_ONLY_ATTR, &error, 0);
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700712 if (error != "") {
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700713 fprintf(stderr, "ERROR getting 'android:testOnly' attribute: %s\n", error.string());
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700714 goto bail;
715 }
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700716 if (testOnly != 0) {
717 printf("testOnly='%d'\n", testOnly);
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700718 }
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700719 } else if (tag == "uses-sdk") {
720 int32_t code = getIntegerAttribute(tree, MIN_SDK_VERSION_ATTR, &error);
721 if (error != "") {
722 error = "";
723 String8 name = getResolvedAttribute(&res, tree, MIN_SDK_VERSION_ATTR, &error);
724 if (error != "") {
725 fprintf(stderr, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
726 error.string());
727 goto bail;
728 }
Dianne Hackborn723738c2009-06-25 19:48:04 -0700729 if (name == "Donut") targetSdk = 4;
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700730 printf("sdkVersion:'%s'\n", name.string());
731 } else if (code != -1) {
Dianne Hackborn723738c2009-06-25 19:48:04 -0700732 targetSdk = code;
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700733 printf("sdkVersion:'%d'\n", code);
734 }
Suchi Amalapurapu75c49842009-08-14 15:13:09 -0700735 code = getIntegerAttribute(tree, MAX_SDK_VERSION_ATTR, NULL, -1);
736 if (code != -1) {
737 printf("maxSdkVersion:'%d'\n", code);
738 }
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700739 code = getIntegerAttribute(tree, TARGET_SDK_VERSION_ATTR, &error);
740 if (error != "") {
741 error = "";
742 String8 name = getResolvedAttribute(&res, tree, TARGET_SDK_VERSION_ATTR, &error);
743 if (error != "") {
744 fprintf(stderr, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
745 error.string());
746 goto bail;
747 }
Dianne Hackborn723738c2009-06-25 19:48:04 -0700748 if (name == "Donut" && targetSdk < 4) targetSdk = 4;
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700749 printf("targetSdkVersion:'%s'\n", name.string());
750 } else if (code != -1) {
Dianne Hackborn723738c2009-06-25 19:48:04 -0700751 if (targetSdk < code) {
752 targetSdk = code;
753 }
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700754 printf("targetSdkVersion:'%d'\n", code);
755 }
756 } else if (tag == "uses-configuration") {
757 int32_t reqTouchScreen = getIntegerAttribute(tree,
758 REQ_TOUCH_SCREEN_ATTR, NULL, 0);
759 int32_t reqKeyboardType = getIntegerAttribute(tree,
760 REQ_KEYBOARD_TYPE_ATTR, NULL, 0);
761 int32_t reqHardKeyboard = getIntegerAttribute(tree,
762 REQ_HARD_KEYBOARD_ATTR, NULL, 0);
763 int32_t reqNavigation = getIntegerAttribute(tree,
764 REQ_NAVIGATION_ATTR, NULL, 0);
765 int32_t reqFiveWayNav = getIntegerAttribute(tree,
766 REQ_FIVE_WAY_NAV_ATTR, NULL, 0);
Dianne Hackborncb2d50d2010-01-06 11:29:54 -0800767 printf("uses-configuration:");
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700768 if (reqTouchScreen != 0) {
769 printf(" reqTouchScreen='%d'", reqTouchScreen);
770 }
771 if (reqKeyboardType != 0) {
772 printf(" reqKeyboardType='%d'", reqKeyboardType);
773 }
774 if (reqHardKeyboard != 0) {
775 printf(" reqHardKeyboard='%d'", reqHardKeyboard);
776 }
777 if (reqNavigation != 0) {
778 printf(" reqNavigation='%d'", reqNavigation);
779 }
780 if (reqFiveWayNav != 0) {
781 printf(" reqFiveWayNav='%d'", reqFiveWayNav);
782 }
783 printf("\n");
Dianne Hackborn723738c2009-06-25 19:48:04 -0700784 } else if (tag == "supports-screens") {
785 smallScreen = getIntegerAttribute(tree,
786 SMALL_SCREEN_ATTR, NULL, 1);
787 normalScreen = getIntegerAttribute(tree,
788 NORMAL_SCREEN_ATTR, NULL, 1);
789 largeScreen = getIntegerAttribute(tree,
790 LARGE_SCREEN_ATTR, NULL, 1);
Dianne Hackbornf43489d2010-08-20 12:44:33 -0700791 xlargeScreen = getIntegerAttribute(tree,
792 XLARGE_SCREEN_ATTR, NULL, 1);
Dianne Hackborna0b46c92010-10-21 15:32:06 -0700793 anyDensity = getIntegerAttribute(tree,
794 ANY_DENSITY_ATTR, NULL, 1);
Dianne Hackborne5276a72009-08-27 16:28:44 -0700795 } else if (tag == "uses-feature") {
796 String8 name = getAttribute(tree, NAME_ATTR, &error);
Suchi Amalapurapu40b94722009-09-20 13:39:37 -0700797
798 if (name != "" && error == "") {
Dianne Hackborne5276a72009-08-27 16:28:44 -0700799 int req = getIntegerAttribute(tree,
800 REQUIRED_ATTR, NULL, 1);
Dan Morrill89d97c12010-05-03 16:13:14 -0700801
Dianne Hackborne5276a72009-08-27 16:28:44 -0700802 if (name == "android.hardware.camera") {
803 specCameraFeature = true;
Dan Morrill89d97c12010-05-03 16:13:14 -0700804 } else if (name == "android.hardware.camera.autofocus") {
805 // these have no corresponding permission to check for,
806 // but should imply the foundational camera permission
807 reqCameraAutofocusFeature = reqCameraAutofocusFeature || req;
808 specCameraAutofocusFeature = true;
809 } else if (req && (name == "android.hardware.camera.flash")) {
810 // these have no corresponding permission to check for,
811 // but should imply the foundational camera permission
812 reqCameraFlashFeature = true;
813 } else if (name == "android.hardware.location") {
814 specLocationFeature = true;
815 } else if (name == "android.hardware.location.network") {
816 specNetworkLocFeature = true;
817 reqNetworkLocFeature = reqNetworkLocFeature || req;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800818 } else if (name == "android.hardware.location.gps") {
819 specGpsFeature = true;
Dan Morrill89d97c12010-05-03 16:13:14 -0700820 reqGpsFeature = reqGpsFeature || req;
821 } else if (name == "android.hardware.bluetooth") {
822 specBluetoothFeature = true;
823 } else if (name == "android.hardware.touchscreen") {
824 specTouchscreenFeature = true;
825 } else if (name == "android.hardware.touchscreen.multitouch") {
826 specMultitouchFeature = true;
827 } else if (name == "android.hardware.touchscreen.multitouch.distinct") {
828 reqDistinctMultitouchFeature = reqDistinctMultitouchFeature || req;
829 } else if (name == "android.hardware.microphone") {
830 specMicrophoneFeature = true;
831 } else if (name == "android.hardware.wifi") {
832 specWiFiFeature = true;
833 } else if (name == "android.hardware.telephony") {
834 specTelephonyFeature = true;
835 } else if (req && (name == "android.hardware.telephony.gsm" ||
836 name == "android.hardware.telephony.cdma")) {
837 // these have no corresponding permission to check for,
838 // but should imply the foundational telephony permission
839 reqTelephonySubFeature = true;
Dianne Hackborne5276a72009-08-27 16:28:44 -0700840 }
841 printf("uses-feature%s:'%s'\n",
842 req ? "" : "-not-required", name.string());
843 } else {
844 int vers = getIntegerAttribute(tree,
845 GL_ES_VERSION_ATTR, &error);
846 if (error == "") {
847 printf("uses-gl-es:'0x%x'\n", vers);
848 }
849 }
850 } else if (tag == "uses-permission") {
851 String8 name = getAttribute(tree, NAME_ATTR, &error);
Suchi Amalapurapu40b94722009-09-20 13:39:37 -0700852 if (name != "" && error == "") {
Dianne Hackborne5276a72009-08-27 16:28:44 -0700853 if (name == "android.permission.CAMERA") {
854 hasCameraPermission = true;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800855 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
856 hasGpsPermission = true;
Dan Morrill89d97c12010-05-03 16:13:14 -0700857 } else if (name == "android.permission.ACCESS_MOCK_LOCATION") {
858 hasMockLocPermission = true;
859 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
860 hasCoarseLocPermission = true;
861 } else if (name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
862 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
863 hasGeneralLocPermission = true;
864 } else if (name == "android.permission.BLUETOOTH" ||
865 name == "android.permission.BLUETOOTH_ADMIN") {
866 hasBluetoothPermission = true;
867 } else if (name == "android.permission.RECORD_AUDIO") {
868 hasRecordAudioPermission = true;
869 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
870 name == "android.permission.CHANGE_WIFI_STATE" ||
871 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
872 hasWiFiPermission = true;
873 } else if (name == "android.permission.CALL_PHONE" ||
874 name == "android.permission.CALL_PRIVILEGED" ||
875 name == "android.permission.MODIFY_PHONE_STATE" ||
876 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
877 name == "android.permission.READ_SMS" ||
878 name == "android.permission.RECEIVE_SMS" ||
879 name == "android.permission.RECEIVE_MMS" ||
880 name == "android.permission.RECEIVE_WAP_PUSH" ||
881 name == "android.permission.SEND_SMS" ||
882 name == "android.permission.WRITE_APN_SETTINGS" ||
883 name == "android.permission.WRITE_SMS") {
884 hasTelephonyPermission = true;
Dianne Hackborne5276a72009-08-27 16:28:44 -0700885 }
886 printf("uses-permission:'%s'\n", name.string());
887 } else {
888 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
889 error.string());
890 goto bail;
891 }
Dianne Hackborn43b68032010-09-02 17:14:41 -0700892 } else if (tag == "uses-package") {
893 String8 name = getAttribute(tree, NAME_ATTR, &error);
894 if (name != "" && error == "") {
895 printf("uses-package:'%s'\n", name.string());
896 } else {
897 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
898 error.string());
899 goto bail;
900 }
Jeff Hamiltone2c17f92010-02-12 13:45:16 -0600901 } else if (tag == "original-package") {
902 String8 name = getAttribute(tree, NAME_ATTR, &error);
903 if (name != "" && error == "") {
904 printf("original-package:'%s'\n", name.string());
905 } else {
906 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
907 error.string());
908 goto bail;
909 }
Dan Morrill6f51fc12010-10-13 14:33:43 -0700910 } else if (tag == "uses-gl-texture") {
911 String8 name = getAttribute(tree, NAME_ATTR, &error);
912 if (name != "" && error == "") {
913 printf("uses-gl-texture:'%s'\n", name.string());
914 } else {
915 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
916 error.string());
917 goto bail;
918 }
Dianne Hackborna0b46c92010-10-21 15:32:06 -0700919 } else if (tag == "compatible-screens") {
920 printCompatibleScreens(tree);
921 depth--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700923 } else if (depth == 3 && withinApplication) {
924 withinActivity = false;
925 withinReceiver = false;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700926 withinService = false;
927 hasIntentFilter = false;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700928 if(tag == "activity") {
929 withinActivity = true;
930 activityName = getAttribute(tree, NAME_ATTR, &error);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 if (error != "") {
932 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
933 goto bail;
934 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700935
936 activityLabel = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 if (error != "") {
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700938 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 goto bail;
940 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700941
942 activityIcon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
943 if (error != "") {
944 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
945 goto bail;
946 }
947 } else if (tag == "uses-library") {
948 String8 libraryName = getAttribute(tree, NAME_ATTR, &error);
949 if (error != "") {
950 fprintf(stderr, "ERROR getting 'android:name' attribute for uses-library: %s\n", error.string());
951 goto bail;
952 }
Dianne Hackborn49237342009-08-27 20:08:01 -0700953 int req = getIntegerAttribute(tree,
954 REQUIRED_ATTR, NULL, 1);
955 printf("uses-library%s:'%s'\n",
956 req ? "" : "-not-required", libraryName.string());
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700957 } else if (tag == "receiver") {
958 withinReceiver = true;
959 receiverName = getAttribute(tree, NAME_ATTR, &error);
960
961 if (error != "") {
962 fprintf(stderr, "ERROR getting 'android:name' attribute for receiver: %s\n", error.string());
963 goto bail;
964 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700965 } else if (tag == "service") {
966 withinService = true;
967 serviceName = getAttribute(tree, NAME_ATTR, &error);
968
969 if (error != "") {
970 fprintf(stderr, "ERROR getting 'android:name' attribute for service: %s\n", error.string());
971 goto bail;
972 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700973 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700974 } else if ((depth == 4) && (tag == "intent-filter")) {
975 hasIntentFilter = true;
976 withinIntentFilter = true;
977 actMainActivity = actWidgetReceivers = actImeService = actWallpaperService = false;
978 } else if ((depth == 5) && withinIntentFilter){
979 String8 action;
980 if (tag == "action") {
981 action = getAttribute(tree, NAME_ATTR, &error);
982 if (error != "") {
983 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
984 goto bail;
985 }
986 if (withinActivity) {
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700987 if (action == "android.intent.action.MAIN") {
988 isMainActivity = true;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700989 actMainActivity = true;
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700990 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700991 } else if (withinReceiver) {
992 if (action == "android.appwidget.action.APPWIDGET_UPDATE") {
993 actWidgetReceivers = true;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700994 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700995 } else if (withinService) {
996 if (action == "android.view.InputMethod") {
997 actImeService = true;
998 } else if (action == "android.service.wallpaper.WallpaperService") {
999 actWallpaperService = true;
1000 }
1001 }
1002 if (action == "android.intent.action.SEARCH") {
1003 isSearchable = true;
1004 }
1005 }
1006
1007 if (tag == "category") {
1008 String8 category = getAttribute(tree, NAME_ATTR, &error);
1009 if (error != "") {
1010 fprintf(stderr, "ERROR getting 'name' attribute: %s\n", error.string());
1011 goto bail;
1012 }
1013 if (withinActivity) {
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -07001014 if (category == "android.intent.category.LAUNCHER") {
1015 isLauncherActivity = true;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -07001016 }
1017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
1019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001021
Dan Morrill89d97c12010-05-03 16:13:14 -07001022 /* The following blocks handle printing "inferred" uses-features, based
1023 * on whether related features or permissions are used by the app.
1024 * Note that the various spec*Feature variables denote whether the
1025 * relevant tag was *present* in the AndroidManfest, not that it was
1026 * present and set to true.
1027 */
1028 // Camera-related back-compatibility logic
1029 if (!specCameraFeature) {
1030 if (reqCameraFlashFeature || reqCameraAutofocusFeature) {
1031 // if app requested a sub-feature (autofocus or flash) and didn't
1032 // request the base camera feature, we infer that it meant to
1033 printf("uses-feature:'android.hardware.camera'\n");
1034 } else if (hasCameraPermission) {
1035 // if app wants to use camera but didn't request the feature, we infer
1036 // that it meant to, and further that it wants autofocus
1037 // (which was the 1.0 - 1.5 behavior)
1038 printf("uses-feature:'android.hardware.camera'\n");
1039 if (!specCameraAutofocusFeature) {
1040 printf("uses-feature:'android.hardware.camera.autofocus'\n");
1041 }
1042 }
Dianne Hackborne5276a72009-08-27 16:28:44 -07001043 }
Doug Zongkerdbe7a682009-10-09 11:24:51 -07001044
Dan Morrill89d97c12010-05-03 16:13:14 -07001045 // Location-related back-compatibility logic
1046 if (!specLocationFeature &&
1047 (hasMockLocPermission || hasCoarseLocPermission || hasGpsPermission ||
1048 hasGeneralLocPermission || reqNetworkLocFeature || reqGpsFeature)) {
1049 // if app either takes a location-related permission or requests one of the
1050 // sub-features, we infer that it also meant to request the base location feature
1051 printf("uses-feature:'android.hardware.location'\n");
1052 }
Dianne Hackbornef05e072010-03-01 17:43:39 -08001053 if (!specGpsFeature && hasGpsPermission) {
Dan Morrill89d97c12010-05-03 16:13:14 -07001054 // if app takes GPS (FINE location) perm but does not request the GPS
1055 // feature, we infer that it meant to
Dianne Hackbornef05e072010-03-01 17:43:39 -08001056 printf("uses-feature:'android.hardware.location.gps'\n");
1057 }
Dan Morrill89d97c12010-05-03 16:13:14 -07001058 if (!specNetworkLocFeature && hasCoarseLocPermission) {
1059 // if app takes Network location (COARSE location) perm but does not request the
1060 // network location feature, we infer that it meant to
1061 printf("uses-feature:'android.hardware.location.network'\n");
1062 }
1063
1064 // Bluetooth-related compatibility logic
Dan Morrill6b22d812010-06-15 21:41:42 -07001065 if (!specBluetoothFeature && hasBluetoothPermission && (targetSdk > 4)) {
Dan Morrill89d97c12010-05-03 16:13:14 -07001066 // if app takes a Bluetooth permission but does not request the Bluetooth
1067 // feature, we infer that it meant to
1068 printf("uses-feature:'android.hardware.bluetooth'\n");
1069 }
1070
1071 // Microphone-related compatibility logic
1072 if (!specMicrophoneFeature && hasRecordAudioPermission) {
1073 // if app takes the record-audio permission but does not request the microphone
1074 // feature, we infer that it meant to
1075 printf("uses-feature:'android.hardware.microphone'\n");
1076 }
1077
1078 // WiFi-related compatibility logic
1079 if (!specWiFiFeature && hasWiFiPermission) {
1080 // if app takes one of the WiFi permissions but does not request the WiFi
1081 // feature, we infer that it meant to
1082 printf("uses-feature:'android.hardware.wifi'\n");
1083 }
1084
1085 // Telephony-related compatibility logic
1086 if (!specTelephonyFeature && (hasTelephonyPermission || reqTelephonySubFeature)) {
1087 // if app takes one of the telephony permissions or requests a sub-feature but
1088 // does not request the base telephony feature, we infer that it meant to
1089 printf("uses-feature:'android.hardware.telephony'\n");
1090 }
1091
1092 // Touchscreen-related back-compatibility logic
1093 if (!specTouchscreenFeature) { // not a typo!
1094 // all apps are presumed to require a touchscreen, unless they explicitly say
1095 // <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
1096 // Note that specTouchscreenFeature is true if the tag is present, regardless
1097 // of whether its value is true or false, so this is safe
1098 printf("uses-feature:'android.hardware.touchscreen'\n");
1099 }
1100 if (!specMultitouchFeature && reqDistinctMultitouchFeature) {
1101 // if app takes one of the telephony permissions or requests a sub-feature but
1102 // does not request the base telephony feature, we infer that it meant to
1103 printf("uses-feature:'android.hardware.touchscreen.multitouch'\n");
1104 }
Dianne Hackbornef05e072010-03-01 17:43:39 -08001105
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001106 if (hasMainActivity) {
1107 printf("main\n");
1108 }
1109 if (hasWidgetReceivers) {
1110 printf("app-widget\n");
1111 }
1112 if (hasImeService) {
1113 printf("ime\n");
1114 }
1115 if (hasWallpaperService) {
1116 printf("wallpaper\n");
1117 }
1118 if (hasOtherActivities) {
1119 printf("other-activities\n");
1120 }
1121 if (isSearchable) {
1122 printf("search\n");
1123 }
1124 if (hasOtherReceivers) {
1125 printf("other-receivers\n");
1126 }
1127 if (hasOtherServices) {
1128 printf("other-services\n");
1129 }
1130
Dianne Hackborn723738c2009-06-25 19:48:04 -07001131 // Determine default values for any unspecified screen sizes,
1132 // based on the target SDK of the package. As of 4 (donut)
1133 // the screen size support was introduced, so all default to
1134 // enabled.
1135 if (smallScreen > 0) {
1136 smallScreen = targetSdk >= 4 ? -1 : 0;
1137 }
1138 if (normalScreen > 0) {
1139 normalScreen = -1;
1140 }
1141 if (largeScreen > 0) {
1142 largeScreen = targetSdk >= 4 ? -1 : 0;
1143 }
Dianne Hackbornf43489d2010-08-20 12:44:33 -07001144 if (xlargeScreen > 0) {
1145 // Introduced in Honeycomb.
1146 xlargeScreen = targetSdk >= 10 ? -1 : 0;
1147 }
Dianne Hackborna0b46c92010-10-21 15:32:06 -07001148 if (anyDensity > 0) {
1149 anyDensity = targetSdk >= 4 ? -1 : 0;
1150 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07001151 printf("supports-screens:");
1152 if (smallScreen != 0) printf(" 'small'");
1153 if (normalScreen != 0) printf(" 'normal'");
1154 if (largeScreen != 0) printf(" 'large'");
Dianne Hackbornf43489d2010-08-20 12:44:33 -07001155 if (xlargeScreen != 0) printf(" 'xlarge'");
Dianne Hackborn723738c2009-06-25 19:48:04 -07001156 printf("\n");
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001157
Dianne Hackborna0b46c92010-10-21 15:32:06 -07001158 printf("supports-any-density: '%s'\n", anyDensity ? "true" : "false");
1159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 printf("locales:");
1161 Vector<String8> locales;
1162 res.getLocales(&locales);
Dianne Hackborne17086b2009-06-19 15:13:28 -07001163 const size_t NL = locales.size();
1164 for (size_t i=0; i<NL; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 const char* localeStr = locales[i].string();
1166 if (localeStr == NULL || strlen(localeStr) == 0) {
1167 localeStr = "--_--";
1168 }
1169 printf(" '%s'", localeStr);
1170 }
1171 printf("\n");
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001172
Dianne Hackborne17086b2009-06-19 15:13:28 -07001173 Vector<ResTable_config> configs;
1174 res.getConfigurations(&configs);
1175 SortedVector<int> densities;
1176 const size_t NC = configs.size();
1177 for (size_t i=0; i<NC; i++) {
1178 int dens = configs[i].density;
1179 if (dens == 0) dens = 160;
1180 densities.add(dens);
1181 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001182
Dianne Hackborne17086b2009-06-19 15:13:28 -07001183 printf("densities:");
1184 const size_t ND = densities.size();
1185 for (size_t i=0; i<ND; i++) {
1186 printf(" '%d'", densities[i]);
1187 }
1188 printf("\n");
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001189
Dianne Hackbornbb9ea302009-05-18 15:22:00 -07001190 AssetDir* dir = assets.openNonAssetDir(assetsCookie, "lib");
1191 if (dir != NULL) {
1192 if (dir->getFileCount() > 0) {
1193 printf("native-code:");
1194 for (size_t i=0; i<dir->getFileCount(); i++) {
1195 printf(" '%s'", dir->getFileName(i).string());
1196 }
1197 printf("\n");
1198 }
1199 delete dir;
1200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 } else if (strcmp("configurations", option) == 0) {
1202 Vector<ResTable_config> configs;
1203 res.getConfigurations(&configs);
1204 const size_t N = configs.size();
1205 for (size_t i=0; i<N; i++) {
1206 printf("%s\n", configs[i].toString().string());
1207 }
1208 } else {
1209 fprintf(stderr, "ERROR: unknown dump option '%s'\n", option);
1210 goto bail;
1211 }
1212 }
1213
1214 result = NO_ERROR;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -07001215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216bail:
1217 if (asset) {
1218 delete asset;
1219 }
1220 return (result != NO_ERROR);
1221}
1222
1223
1224/*
1225 * Handle the "add" command, which wants to add files to a new or
1226 * pre-existing archive.
1227 */
1228int doAdd(Bundle* bundle)
1229{
1230 ZipFile* zip = NULL;
1231 status_t result = UNKNOWN_ERROR;
1232 const char* zipFileName;
1233
1234 if (bundle->getUpdate()) {
1235 /* avoid confusion */
1236 fprintf(stderr, "ERROR: can't use '-u' with add\n");
1237 goto bail;
1238 }
1239
1240 if (bundle->getFileSpecCount() < 1) {
1241 fprintf(stderr, "ERROR: must specify zip file name\n");
1242 goto bail;
1243 }
1244 zipFileName = bundle->getFileSpecEntry(0);
1245
1246 if (bundle->getFileSpecCount() < 2) {
1247 fprintf(stderr, "NOTE: nothing to do\n");
1248 goto bail;
1249 }
1250
1251 zip = openReadWrite(zipFileName, true);
1252 if (zip == NULL) {
1253 fprintf(stderr, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName);
1254 goto bail;
1255 }
1256
1257 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1258 const char* fileName = bundle->getFileSpecEntry(i);
1259
1260 if (strcasecmp(String8(fileName).getPathExtension().string(), ".gz") == 0) {
1261 printf(" '%s'... (from gzip)\n", fileName);
1262 result = zip->addGzip(fileName, String8(fileName).getBasePath().string(), NULL);
1263 } else {
Doug Zongkerdbe7a682009-10-09 11:24:51 -07001264 if (bundle->getJunkPath()) {
1265 String8 storageName = String8(fileName).getPathLeaf();
1266 printf(" '%s' as '%s'...\n", fileName, storageName.string());
1267 result = zip->add(fileName, storageName.string(),
1268 bundle->getCompressionMethod(), NULL);
1269 } else {
1270 printf(" '%s'...\n", fileName);
1271 result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
1272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 }
1274 if (result != NO_ERROR) {
1275 fprintf(stderr, "Unable to add '%s' to '%s'", bundle->getFileSpecEntry(i), zipFileName);
1276 if (result == NAME_NOT_FOUND)
1277 fprintf(stderr, ": file not found\n");
1278 else if (result == ALREADY_EXISTS)
1279 fprintf(stderr, ": already exists in archive\n");
1280 else
1281 fprintf(stderr, "\n");
1282 goto bail;
1283 }
1284 }
1285
1286 result = NO_ERROR;
1287
1288bail:
1289 delete zip;
1290 return (result != NO_ERROR);
1291}
1292
1293
1294/*
1295 * Delete files from an existing archive.
1296 */
1297int doRemove(Bundle* bundle)
1298{
1299 ZipFile* zip = NULL;
1300 status_t result = UNKNOWN_ERROR;
1301 const char* zipFileName;
1302
1303 if (bundle->getFileSpecCount() < 1) {
1304 fprintf(stderr, "ERROR: must specify zip file name\n");
1305 goto bail;
1306 }
1307 zipFileName = bundle->getFileSpecEntry(0);
1308
1309 if (bundle->getFileSpecCount() < 2) {
1310 fprintf(stderr, "NOTE: nothing to do\n");
1311 goto bail;
1312 }
1313
1314 zip = openReadWrite(zipFileName, false);
1315 if (zip == NULL) {
1316 fprintf(stderr, "ERROR: failed opening Zip archive '%s'\n",
1317 zipFileName);
1318 goto bail;
1319 }
1320
1321 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1322 const char* fileName = bundle->getFileSpecEntry(i);
1323 ZipEntry* entry;
1324
1325 entry = zip->getEntryByName(fileName);
1326 if (entry == NULL) {
1327 printf(" '%s' NOT FOUND\n", fileName);
1328 continue;
1329 }
1330
1331 result = zip->remove(entry);
1332
1333 if (result != NO_ERROR) {
1334 fprintf(stderr, "Unable to delete '%s' from '%s'\n",
1335 bundle->getFileSpecEntry(i), zipFileName);
1336 goto bail;
1337 }
1338 }
1339
1340 /* update the archive */
1341 zip->flush();
1342
1343bail:
1344 delete zip;
1345 return (result != NO_ERROR);
1346}
1347
1348
1349/*
1350 * Package up an asset directory and associated application files.
1351 */
1352int doPackage(Bundle* bundle)
1353{
1354 const char* outputAPKFile;
1355 int retVal = 1;
1356 status_t err;
1357 sp<AaptAssets> assets;
1358 int N;
1359
1360 // -c zz_ZZ means do pseudolocalization
1361 ResourceFilter filter;
1362 err = filter.parse(bundle->getConfigurations());
1363 if (err != NO_ERROR) {
1364 goto bail;
1365 }
1366 if (filter.containsPseudo()) {
1367 bundle->setPseudolocalize(true);
1368 }
1369
1370 N = bundle->getFileSpecCount();
1371 if (N < 1 && bundle->getResourceSourceDirs().size() == 0 && bundle->getJarFiles().size() == 0
1372 && bundle->getAndroidManifestFile() == NULL && bundle->getAssetSourceDir() == NULL) {
1373 fprintf(stderr, "ERROR: no input files\n");
1374 goto bail;
1375 }
1376
1377 outputAPKFile = bundle->getOutputAPKFile();
1378
1379 // Make sure the filenames provided exist and are of the appropriate type.
1380 if (outputAPKFile) {
1381 FileType type;
1382 type = getFileType(outputAPKFile);
1383 if (type != kFileTypeNonexistent && type != kFileTypeRegular) {
1384 fprintf(stderr,
1385 "ERROR: output file '%s' exists but is not regular file\n",
1386 outputAPKFile);
1387 goto bail;
1388 }
1389 }
1390
1391 // Load the assets.
1392 assets = new AaptAssets();
1393 err = assets->slurpFromArgs(bundle);
1394 if (err < 0) {
1395 goto bail;
1396 }
1397
1398 if (bundle->getVerbose()) {
1399 assets->print();
1400 }
1401
1402 // If they asked for any files that need to be compiled, do so.
1403 if (bundle->getResourceSourceDirs().size() || bundle->getAndroidManifestFile()) {
1404 err = buildResources(bundle, assets);
1405 if (err != 0) {
1406 goto bail;
1407 }
1408 }
1409
1410 // At this point we've read everything and processed everything. From here
1411 // on out it's just writing output files.
1412 if (SourcePos::hasErrors()) {
1413 goto bail;
1414 }
1415
1416 // Write out R.java constants
1417 if (assets->getPackage() == assets->getSymbolsPrivatePackage()) {
Xavier Ducrohet63459ad2009-11-30 18:05:10 -08001418 if (bundle->getCustomPackage() == NULL) {
1419 err = writeResourceSymbols(bundle, assets, assets->getPackage(), true);
1420 } else {
1421 const String8 customPkg(bundle->getCustomPackage());
1422 err = writeResourceSymbols(bundle, assets, customPkg, true);
1423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 if (err < 0) {
1425 goto bail;
1426 }
1427 } else {
1428 err = writeResourceSymbols(bundle, assets, assets->getPackage(), false);
1429 if (err < 0) {
1430 goto bail;
1431 }
1432 err = writeResourceSymbols(bundle, assets, assets->getSymbolsPrivatePackage(), true);
1433 if (err < 0) {
1434 goto bail;
1435 }
1436 }
1437
Joe Onorato1553c822009-08-30 13:36:22 -07001438 // Write out the ProGuard file
1439 err = writeProguardFile(bundle, assets);
1440 if (err < 0) {
1441 goto bail;
1442 }
1443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 // Write the apk
1445 if (outputAPKFile) {
1446 err = writeAPK(bundle, assets, String8(outputAPKFile));
1447 if (err != NO_ERROR) {
1448 fprintf(stderr, "ERROR: packaging of '%s' failed\n", outputAPKFile);
1449 goto bail;
1450 }
1451 }
1452
1453 retVal = 0;
1454bail:
1455 if (SourcePos::hasErrors()) {
1456 SourcePos::printErrors(stderr);
1457 }
1458 return retVal;
1459}