blob: 89f45df1b853f817986e7c23c2d98a64412711b2 [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 {
201 printf("\nResource table:\n");
Dianne Hackborne17086b2009-06-19 15:13:28 -0700202 res.print(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 Asset* manifestAsset = assets.openNonAsset("AndroidManifest.xml",
206 Asset::ACCESS_BUFFER);
207 if (manifestAsset == NULL) {
208 printf("\nNo AndroidManifest.xml found.\n");
209 } else {
210 printf("\nAndroid manifest:\n");
211 ResXMLTree tree;
212 tree.setTo(manifestAsset->getBuffer(true),
213 manifestAsset->getLength());
214 printXMLBlock(&tree);
215 }
216 delete manifestAsset;
217 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 result = 0;
220
221bail:
222 delete zip;
223 return result;
224}
225
226static ssize_t indexOfAttribute(const ResXMLTree& tree, uint32_t attrRes)
227{
228 size_t N = tree.getAttributeCount();
229 for (size_t i=0; i<N; i++) {
230 if (tree.getAttributeNameResID(i) == attrRes) {
231 return (ssize_t)i;
232 }
233 }
234 return -1;
235}
236
Joe Onorato1553c822009-08-30 13:36:22 -0700237String8 getAttribute(const ResXMLTree& tree, const char* ns,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 const char* attr, String8* outError)
239{
240 ssize_t idx = tree.indexOfAttribute(ns, attr);
241 if (idx < 0) {
242 return String8();
243 }
244 Res_value value;
245 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
246 if (value.dataType != Res_value::TYPE_STRING) {
247 if (outError != NULL) *outError = "attribute is not a string value";
248 return String8();
249 }
250 }
251 size_t len;
252 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
253 return str ? String8(str, len) : String8();
254}
255
256static String8 getAttribute(const ResXMLTree& tree, uint32_t attrRes, String8* outError)
257{
258 ssize_t idx = indexOfAttribute(tree, attrRes);
259 if (idx < 0) {
260 return String8();
261 }
262 Res_value value;
263 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
264 if (value.dataType != Res_value::TYPE_STRING) {
265 if (outError != NULL) *outError = "attribute is not a string value";
266 return String8();
267 }
268 }
269 size_t len;
270 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
271 return str ? String8(str, len) : String8();
272}
273
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700274static int32_t getIntegerAttribute(const ResXMLTree& tree, uint32_t attrRes,
275 String8* outError, int32_t defValue = -1)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276{
277 ssize_t idx = indexOfAttribute(tree, attrRes);
278 if (idx < 0) {
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700279 return defValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
281 Res_value value;
282 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700283 if (value.dataType < Res_value::TYPE_FIRST_INT
284 || value.dataType > Res_value::TYPE_LAST_INT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 if (outError != NULL) *outError = "attribute is not an integer value";
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700286 return defValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288 }
289 return value.data;
290}
291
292static String8 getResolvedAttribute(const ResTable* resTable, const ResXMLTree& tree,
293 uint32_t attrRes, String8* outError)
294{
295 ssize_t idx = indexOfAttribute(tree, attrRes);
296 if (idx < 0) {
297 return String8();
298 }
299 Res_value value;
300 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
301 if (value.dataType == Res_value::TYPE_STRING) {
302 size_t len;
303 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
304 return str ? String8(str, len) : String8();
305 }
306 resTable->resolveReference(&value, 0);
307 if (value.dataType != Res_value::TYPE_STRING) {
308 if (outError != NULL) *outError = "attribute is not a string value";
309 return String8();
310 }
311 }
312 size_t len;
313 const Res_value* value2 = &value;
314 const char16_t* str = const_cast<ResTable*>(resTable)->valueToString(value2, 0, NULL, &len);
315 return str ? String8(str, len) : String8();
316}
317
318// These are attribute resource constants for the platform, as found
319// in android.R.attr
320enum {
321 NAME_ATTR = 0x01010003,
322 VERSION_CODE_ATTR = 0x0101021b,
323 VERSION_NAME_ATTR = 0x0101021c,
324 LABEL_ATTR = 0x01010001,
325 ICON_ATTR = 0x01010002,
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700326 MIN_SDK_VERSION_ATTR = 0x0101020c,
Suchi Amalapurapu75c49842009-08-14 15:13:09 -0700327 MAX_SDK_VERSION_ATTR = 0x01010271,
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700328 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
329 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
330 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
331 REQ_NAVIGATION_ATTR = 0x0101022a,
332 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
333 TARGET_SDK_VERSION_ATTR = 0x01010270,
334 TEST_ONLY_ATTR = 0x01010272,
Dianne Hackborna6d9c7c2010-10-21 15:32:06 -0700335 ANY_DENSITY_ATTR = 0x0101026c,
Dianne Hackborne5276a72009-08-27 16:28:44 -0700336 GL_ES_VERSION_ATTR = 0x01010281,
Dianne Hackborn723738c2009-06-25 19:48:04 -0700337 SMALL_SCREEN_ATTR = 0x01010284,
338 NORMAL_SCREEN_ATTR = 0x01010285,
339 LARGE_SCREEN_ATTR = 0x01010286,
Dianne Hackbornf43489d2010-08-20 12:44:33 -0700340 XLARGE_SCREEN_ATTR = 0x010102bf,
Dianne Hackborne5276a72009-08-27 16:28:44 -0700341 REQUIRED_ATTR = 0x0101028e,
Dianne Hackborna6d9c7c2010-10-21 15:32:06 -0700342 SCREEN_SIZE_ATTR = 0x010102ca,
343 SCREEN_DENSITY_ATTR = 0x010102cb,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344};
345
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700346const 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
Dianne Hackborna6d9c7c2010-10-21 15:32:06 -0700360static void printCompatibleScreens(ResXMLTree& tree) {
361 size_t len;
362 ResXMLTree::event_code_t code;
363 int depth = 0;
364 bool first = true;
365 printf("compatible-screens:");
366 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
367 if (code == ResXMLTree::END_TAG) {
368 depth--;
369 if (depth < 0) {
370 break;
371 }
372 continue;
373 }
374 if (code != ResXMLTree::START_TAG) {
375 continue;
376 }
377 depth++;
378 String8 tag(tree.getElementName(&len));
379 if (tag == "screen") {
380 int32_t screenSize = getIntegerAttribute(tree,
381 SCREEN_SIZE_ATTR, NULL, -1);
382 int32_t screenDensity = getIntegerAttribute(tree,
383 SCREEN_DENSITY_ATTR, NULL, -1);
384 if (screenSize > 0 && screenDensity > 0) {
385 if (!first) {
386 printf(",");
387 }
388 first = false;
389 printf("'%d/%d'", screenSize, screenDensity);
390 }
391 }
392 }
393 printf("\n");
394}
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396/*
397 * Handle the "dump" command, to extract select data from an archive.
398 */
399int doDump(Bundle* bundle)
400{
401 status_t result = UNKNOWN_ERROR;
402 Asset* asset = NULL;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 if (bundle->getFileSpecCount() < 1) {
405 fprintf(stderr, "ERROR: no dump option specified\n");
406 return 1;
407 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 if (bundle->getFileSpecCount() < 2) {
410 fprintf(stderr, "ERROR: no dump file specified\n");
411 return 1;
412 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 const char* option = bundle->getFileSpecEntry(0);
415 const char* filename = bundle->getFileSpecEntry(1);
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 AssetManager assets;
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700418 void* assetsCookie;
419 if (!assets.addAssetPath(String8(filename), &assetsCookie)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 fprintf(stderr, "ERROR: dump failed because assets could not be loaded\n");
421 return 1;
422 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 const ResTable& res = assets.getResources(false);
425 if (&res == NULL) {
426 fprintf(stderr, "ERROR: dump failed because no resource table was found\n");
427 goto bail;
428 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 if (strcmp("resources", option) == 0) {
Dianne Hackborne17086b2009-06-19 15:13:28 -0700431 res.print(bundle->getValues());
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 } else if (strcmp("xmltree", option) == 0) {
434 if (bundle->getFileSpecCount() < 3) {
435 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
436 goto bail;
437 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 for (int i=2; i<bundle->getFileSpecCount(); i++) {
440 const char* resname = bundle->getFileSpecEntry(i);
441 ResXMLTree tree;
442 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
443 if (asset == NULL) {
Kenny Root44b283d2009-09-01 19:03:11 -0500444 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 goto bail;
446 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 if (tree.setTo(asset->getBuffer(true),
449 asset->getLength()) != NO_ERROR) {
450 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
451 goto bail;
452 }
453 tree.restart();
454 printXMLBlock(&tree);
Kenny Root19138462009-12-04 09:38:48 -0800455 tree.uninit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 delete asset;
457 asset = NULL;
458 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 } else if (strcmp("xmlstrings", option) == 0) {
461 if (bundle->getFileSpecCount() < 3) {
462 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
463 goto bail;
464 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 for (int i=2; i<bundle->getFileSpecCount(); i++) {
467 const char* resname = bundle->getFileSpecEntry(i);
468 ResXMLTree tree;
469 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
470 if (asset == NULL) {
Kenny Root44b283d2009-09-01 19:03:11 -0500471 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 goto bail;
473 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 if (tree.setTo(asset->getBuffer(true),
476 asset->getLength()) != NO_ERROR) {
477 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
478 goto bail;
479 }
480 printStringPool(&tree.getStrings());
481 delete asset;
482 asset = NULL;
483 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 } else {
486 ResXMLTree tree;
487 asset = assets.openNonAsset("AndroidManifest.xml",
488 Asset::ACCESS_BUFFER);
489 if (asset == NULL) {
490 fprintf(stderr, "ERROR: dump failed because no AndroidManifest.xml found\n");
491 goto bail;
492 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 if (tree.setTo(asset->getBuffer(true),
495 asset->getLength()) != NO_ERROR) {
496 fprintf(stderr, "ERROR: AndroidManifest.xml is corrupt\n");
497 goto bail;
498 }
499 tree.restart();
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 if (strcmp("permissions", option) == 0) {
502 size_t len;
503 ResXMLTree::event_code_t code;
504 int depth = 0;
505 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
506 if (code == ResXMLTree::END_TAG) {
507 depth--;
508 continue;
509 }
510 if (code != ResXMLTree::START_TAG) {
511 continue;
512 }
513 depth++;
514 String8 tag(tree.getElementName(&len));
515 //printf("Depth %d tag %s\n", depth, tag.string());
516 if (depth == 1) {
517 if (tag != "manifest") {
518 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
519 goto bail;
520 }
521 String8 pkg = getAttribute(tree, NULL, "package", NULL);
522 printf("package: %s\n", pkg.string());
523 } else if (depth == 2 && tag == "permission") {
524 String8 error;
525 String8 name = getAttribute(tree, NAME_ATTR, &error);
526 if (error != "") {
527 fprintf(stderr, "ERROR: %s\n", error.string());
528 goto bail;
529 }
530 printf("permission: %s\n", name.string());
531 } else if (depth == 2 && tag == "uses-permission") {
532 String8 error;
533 String8 name = getAttribute(tree, NAME_ATTR, &error);
534 if (error != "") {
535 fprintf(stderr, "ERROR: %s\n", error.string());
536 goto bail;
537 }
538 printf("uses-permission: %s\n", name.string());
539 }
540 }
541 } else if (strcmp("badging", option) == 0) {
542 size_t len;
543 ResXMLTree::event_code_t code;
544 int depth = 0;
545 String8 error;
546 bool withinActivity = false;
547 bool isMainActivity = false;
548 bool isLauncherActivity = false;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700549 bool isSearchable = false;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700550 bool withinApplication = false;
551 bool withinReceiver = false;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700552 bool withinService = false;
553 bool withinIntentFilter = false;
554 bool hasMainActivity = false;
555 bool hasOtherActivities = false;
556 bool hasOtherReceivers = false;
557 bool hasOtherServices = false;
558 bool hasWallpaperService = false;
559 bool hasImeService = false;
560 bool hasWidgetReceivers = false;
561 bool hasIntentFilter = false;
562 bool actMainActivity = false;
563 bool actWidgetReceivers = false;
564 bool actImeService = false;
565 bool actWallpaperService = false;
Dan Morrill89d97c12010-05-03 16:13:14 -0700566
567 // This next group of variables is used to implement a group of
568 // backward-compatibility heuristics necessitated by the addition of
569 // some new uses-feature constants in 2.1 and 2.2. In most cases, the
570 // heuristic is "if an app requests a permission but doesn't explicitly
571 // request the corresponding <uses-feature>, presume it's there anyway".
572 bool specCameraFeature = false; // camera-related
573 bool specCameraAutofocusFeature = false;
574 bool reqCameraAutofocusFeature = false;
575 bool reqCameraFlashFeature = false;
Dianne Hackborne5276a72009-08-27 16:28:44 -0700576 bool hasCameraPermission = false;
Dan Morrill89d97c12010-05-03 16:13:14 -0700577 bool specLocationFeature = false; // location-related
578 bool specNetworkLocFeature = false;
579 bool reqNetworkLocFeature = false;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800580 bool specGpsFeature = false;
Dan Morrill89d97c12010-05-03 16:13:14 -0700581 bool reqGpsFeature = false;
582 bool hasMockLocPermission = false;
583 bool hasCoarseLocPermission = false;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800584 bool hasGpsPermission = false;
Dan Morrill89d97c12010-05-03 16:13:14 -0700585 bool hasGeneralLocPermission = false;
586 bool specBluetoothFeature = false; // Bluetooth API-related
587 bool hasBluetoothPermission = false;
588 bool specMicrophoneFeature = false; // microphone-related
589 bool hasRecordAudioPermission = false;
590 bool specWiFiFeature = false;
591 bool hasWiFiPermission = false;
592 bool specTelephonyFeature = false; // telephony-related
593 bool reqTelephonySubFeature = false;
594 bool hasTelephonyPermission = false;
595 bool specTouchscreenFeature = false; // touchscreen-related
596 bool specMultitouchFeature = false;
597 bool reqDistinctMultitouchFeature = false;
598 // 2.2 also added some other features that apps can request, but that
599 // have no corresponding permission, so we cannot implement any
600 // back-compatibility heuristic for them. The below are thus unnecessary
601 // (but are retained here for documentary purposes.)
602 //bool specCompassFeature = false;
603 //bool specAccelerometerFeature = false;
604 //bool specProximityFeature = false;
605 //bool specAmbientLightFeature = false;
606 //bool specLiveWallpaperFeature = false;
607
Dianne Hackborn723738c2009-06-25 19:48:04 -0700608 int targetSdk = 0;
609 int smallScreen = 1;
610 int normalScreen = 1;
611 int largeScreen = 1;
Dianne Hackbornf43489d2010-08-20 12:44:33 -0700612 int xlargeScreen = 1;
Dianne Hackborna6d9c7c2010-10-21 15:32:06 -0700613 int anyDensity = 1;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700614 String8 pkg;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 String8 activityName;
616 String8 activityLabel;
617 String8 activityIcon;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700618 String8 receiverName;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700619 String8 serviceName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
621 if (code == ResXMLTree::END_TAG) {
622 depth--;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700623 if (depth < 2) {
624 withinApplication = false;
625 } else if (depth < 3) {
626 if (withinActivity && isMainActivity && isLauncherActivity) {
627 const char *aName = getComponentName(pkg, activityName);
628 if (aName != NULL) {
629 printf("launchable activity name='%s'", aName);
630 }
631 printf("label='%s' icon='%s'\n",
632 activityLabel.string(),
633 activityIcon.string());
634 }
635 if (!hasIntentFilter) {
636 hasOtherActivities |= withinActivity;
637 hasOtherReceivers |= withinReceiver;
638 hasOtherServices |= withinService;
639 }
640 withinActivity = false;
641 withinService = false;
642 withinReceiver = false;
643 hasIntentFilter = false;
644 isMainActivity = isLauncherActivity = false;
645 } else if (depth < 4) {
646 if (withinIntentFilter) {
647 if (withinActivity) {
648 hasMainActivity |= actMainActivity;
649 hasOtherActivities |= !actMainActivity;
650 } else if (withinReceiver) {
651 hasWidgetReceivers |= actWidgetReceivers;
652 hasOtherReceivers |= !actWidgetReceivers;
653 } else if (withinService) {
654 hasImeService |= actImeService;
655 hasWallpaperService |= actWallpaperService;
656 hasOtherServices |= (!actImeService && !actWallpaperService);
657 }
658 }
659 withinIntentFilter = false;
660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 continue;
662 }
663 if (code != ResXMLTree::START_TAG) {
664 continue;
665 }
666 depth++;
667 String8 tag(tree.getElementName(&len));
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700668 //printf("Depth %d, %s\n", depth, tag.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 if (depth == 1) {
670 if (tag != "manifest") {
671 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
672 goto bail;
673 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700674 pkg = getAttribute(tree, NULL, "package", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 printf("package: name='%s' ", pkg.string());
676 int32_t versionCode = getIntegerAttribute(tree, VERSION_CODE_ATTR, &error);
677 if (error != "") {
678 fprintf(stderr, "ERROR getting 'android:versionCode' attribute: %s\n", error.string());
679 goto bail;
680 }
681 if (versionCode > 0) {
682 printf("versionCode='%d' ", versionCode);
683 } else {
684 printf("versionCode='' ");
685 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800686 String8 versionName = getResolvedAttribute(&res, tree, VERSION_NAME_ATTR, &error);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 if (error != "") {
688 fprintf(stderr, "ERROR getting 'android:versionName' attribute: %s\n", error.string());
689 goto bail;
690 }
691 printf("versionName='%s'\n", versionName.string());
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700692 } else if (depth == 2) {
693 withinApplication = false;
694 if (tag == "application") {
695 withinApplication = true;
696 String8 label = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
697 if (error != "") {
698 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
699 goto bail;
700 }
701 printf("application: label='%s' ", label.string());
702 String8 icon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
703 if (error != "") {
704 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
705 goto bail;
706 }
707 printf("icon='%s'\n", icon.string());
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700708 int32_t testOnly = getIntegerAttribute(tree, TEST_ONLY_ATTR, &error, 0);
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700709 if (error != "") {
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700710 fprintf(stderr, "ERROR getting 'android:testOnly' attribute: %s\n", error.string());
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700711 goto bail;
712 }
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700713 if (testOnly != 0) {
714 printf("testOnly='%d'\n", testOnly);
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700715 }
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700716 } else if (tag == "uses-sdk") {
717 int32_t code = getIntegerAttribute(tree, MIN_SDK_VERSION_ATTR, &error);
718 if (error != "") {
719 error = "";
720 String8 name = getResolvedAttribute(&res, tree, MIN_SDK_VERSION_ATTR, &error);
721 if (error != "") {
722 fprintf(stderr, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
723 error.string());
724 goto bail;
725 }
Dianne Hackborn723738c2009-06-25 19:48:04 -0700726 if (name == "Donut") targetSdk = 4;
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700727 printf("sdkVersion:'%s'\n", name.string());
728 } else if (code != -1) {
Dianne Hackborn723738c2009-06-25 19:48:04 -0700729 targetSdk = code;
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700730 printf("sdkVersion:'%d'\n", code);
731 }
Suchi Amalapurapu75c49842009-08-14 15:13:09 -0700732 code = getIntegerAttribute(tree, MAX_SDK_VERSION_ATTR, NULL, -1);
733 if (code != -1) {
734 printf("maxSdkVersion:'%d'\n", code);
735 }
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700736 code = getIntegerAttribute(tree, TARGET_SDK_VERSION_ATTR, &error);
737 if (error != "") {
738 error = "";
739 String8 name = getResolvedAttribute(&res, tree, TARGET_SDK_VERSION_ATTR, &error);
740 if (error != "") {
741 fprintf(stderr, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
742 error.string());
743 goto bail;
744 }
Dianne Hackborn723738c2009-06-25 19:48:04 -0700745 if (name == "Donut" && targetSdk < 4) targetSdk = 4;
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700746 printf("targetSdkVersion:'%s'\n", name.string());
747 } else if (code != -1) {
Dianne Hackborn723738c2009-06-25 19:48:04 -0700748 if (targetSdk < code) {
749 targetSdk = code;
750 }
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700751 printf("targetSdkVersion:'%d'\n", code);
752 }
753 } else if (tag == "uses-configuration") {
754 int32_t reqTouchScreen = getIntegerAttribute(tree,
755 REQ_TOUCH_SCREEN_ATTR, NULL, 0);
756 int32_t reqKeyboardType = getIntegerAttribute(tree,
757 REQ_KEYBOARD_TYPE_ATTR, NULL, 0);
758 int32_t reqHardKeyboard = getIntegerAttribute(tree,
759 REQ_HARD_KEYBOARD_ATTR, NULL, 0);
760 int32_t reqNavigation = getIntegerAttribute(tree,
761 REQ_NAVIGATION_ATTR, NULL, 0);
762 int32_t reqFiveWayNav = getIntegerAttribute(tree,
763 REQ_FIVE_WAY_NAV_ATTR, NULL, 0);
Dianne Hackborncb2d50d2010-01-06 11:29:54 -0800764 printf("uses-configuration:");
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700765 if (reqTouchScreen != 0) {
766 printf(" reqTouchScreen='%d'", reqTouchScreen);
767 }
768 if (reqKeyboardType != 0) {
769 printf(" reqKeyboardType='%d'", reqKeyboardType);
770 }
771 if (reqHardKeyboard != 0) {
772 printf(" reqHardKeyboard='%d'", reqHardKeyboard);
773 }
774 if (reqNavigation != 0) {
775 printf(" reqNavigation='%d'", reqNavigation);
776 }
777 if (reqFiveWayNav != 0) {
778 printf(" reqFiveWayNav='%d'", reqFiveWayNav);
779 }
780 printf("\n");
Dianne Hackborn723738c2009-06-25 19:48:04 -0700781 } else if (tag == "supports-screens") {
782 smallScreen = getIntegerAttribute(tree,
783 SMALL_SCREEN_ATTR, NULL, 1);
784 normalScreen = getIntegerAttribute(tree,
785 NORMAL_SCREEN_ATTR, NULL, 1);
786 largeScreen = getIntegerAttribute(tree,
787 LARGE_SCREEN_ATTR, NULL, 1);
Dianne Hackbornf43489d2010-08-20 12:44:33 -0700788 xlargeScreen = getIntegerAttribute(tree,
789 XLARGE_SCREEN_ATTR, NULL, 1);
Dianne Hackborna6d9c7c2010-10-21 15:32:06 -0700790 anyDensity = getIntegerAttribute(tree,
791 ANY_DENSITY_ATTR, NULL, 1);
Dianne Hackborne5276a72009-08-27 16:28:44 -0700792 } else if (tag == "uses-feature") {
793 String8 name = getAttribute(tree, NAME_ATTR, &error);
Suchi Amalapurapu40b94722009-09-20 13:39:37 -0700794
795 if (name != "" && error == "") {
Dianne Hackborne5276a72009-08-27 16:28:44 -0700796 int req = getIntegerAttribute(tree,
797 REQUIRED_ATTR, NULL, 1);
Dan Morrill89d97c12010-05-03 16:13:14 -0700798
Dianne Hackborne5276a72009-08-27 16:28:44 -0700799 if (name == "android.hardware.camera") {
800 specCameraFeature = true;
Dan Morrill89d97c12010-05-03 16:13:14 -0700801 } else if (name == "android.hardware.camera.autofocus") {
802 // these have no corresponding permission to check for,
803 // but should imply the foundational camera permission
804 reqCameraAutofocusFeature = reqCameraAutofocusFeature || req;
805 specCameraAutofocusFeature = true;
806 } else if (req && (name == "android.hardware.camera.flash")) {
807 // these have no corresponding permission to check for,
808 // but should imply the foundational camera permission
809 reqCameraFlashFeature = true;
810 } else if (name == "android.hardware.location") {
811 specLocationFeature = true;
812 } else if (name == "android.hardware.location.network") {
813 specNetworkLocFeature = true;
814 reqNetworkLocFeature = reqNetworkLocFeature || req;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800815 } else if (name == "android.hardware.location.gps") {
816 specGpsFeature = true;
Dan Morrill89d97c12010-05-03 16:13:14 -0700817 reqGpsFeature = reqGpsFeature || req;
818 } else if (name == "android.hardware.bluetooth") {
819 specBluetoothFeature = true;
820 } else if (name == "android.hardware.touchscreen") {
821 specTouchscreenFeature = true;
822 } else if (name == "android.hardware.touchscreen.multitouch") {
823 specMultitouchFeature = true;
824 } else if (name == "android.hardware.touchscreen.multitouch.distinct") {
825 reqDistinctMultitouchFeature = reqDistinctMultitouchFeature || req;
826 } else if (name == "android.hardware.microphone") {
827 specMicrophoneFeature = true;
828 } else if (name == "android.hardware.wifi") {
829 specWiFiFeature = true;
830 } else if (name == "android.hardware.telephony") {
831 specTelephonyFeature = true;
832 } else if (req && (name == "android.hardware.telephony.gsm" ||
833 name == "android.hardware.telephony.cdma")) {
834 // these have no corresponding permission to check for,
835 // but should imply the foundational telephony permission
836 reqTelephonySubFeature = true;
Dianne Hackborne5276a72009-08-27 16:28:44 -0700837 }
838 printf("uses-feature%s:'%s'\n",
839 req ? "" : "-not-required", name.string());
840 } else {
841 int vers = getIntegerAttribute(tree,
842 GL_ES_VERSION_ATTR, &error);
843 if (error == "") {
844 printf("uses-gl-es:'0x%x'\n", vers);
845 }
846 }
847 } else if (tag == "uses-permission") {
848 String8 name = getAttribute(tree, NAME_ATTR, &error);
Suchi Amalapurapu40b94722009-09-20 13:39:37 -0700849 if (name != "" && error == "") {
Dianne Hackborne5276a72009-08-27 16:28:44 -0700850 if (name == "android.permission.CAMERA") {
851 hasCameraPermission = true;
Dianne Hackbornef05e072010-03-01 17:43:39 -0800852 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
853 hasGpsPermission = true;
Dan Morrill89d97c12010-05-03 16:13:14 -0700854 } else if (name == "android.permission.ACCESS_MOCK_LOCATION") {
855 hasMockLocPermission = true;
856 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
857 hasCoarseLocPermission = true;
858 } else if (name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
859 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
860 hasGeneralLocPermission = true;
861 } else if (name == "android.permission.BLUETOOTH" ||
862 name == "android.permission.BLUETOOTH_ADMIN") {
863 hasBluetoothPermission = true;
864 } else if (name == "android.permission.RECORD_AUDIO") {
865 hasRecordAudioPermission = true;
866 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
867 name == "android.permission.CHANGE_WIFI_STATE" ||
868 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
869 hasWiFiPermission = true;
870 } else if (name == "android.permission.CALL_PHONE" ||
871 name == "android.permission.CALL_PRIVILEGED" ||
872 name == "android.permission.MODIFY_PHONE_STATE" ||
873 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
874 name == "android.permission.READ_SMS" ||
875 name == "android.permission.RECEIVE_SMS" ||
876 name == "android.permission.RECEIVE_MMS" ||
877 name == "android.permission.RECEIVE_WAP_PUSH" ||
878 name == "android.permission.SEND_SMS" ||
879 name == "android.permission.WRITE_APN_SETTINGS" ||
880 name == "android.permission.WRITE_SMS") {
881 hasTelephonyPermission = true;
Dianne Hackborne5276a72009-08-27 16:28:44 -0700882 }
883 printf("uses-permission:'%s'\n", name.string());
884 } else {
885 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
886 error.string());
887 goto bail;
888 }
Dianne Hackborn43b68032010-09-02 17:14:41 -0700889 } else if (tag == "uses-package") {
890 String8 name = getAttribute(tree, NAME_ATTR, &error);
891 if (name != "" && error == "") {
892 printf("uses-package:'%s'\n", name.string());
893 } else {
894 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
895 error.string());
896 goto bail;
897 }
Jeff Hamiltone2c17f92010-02-12 13:45:16 -0600898 } else if (tag == "original-package") {
899 String8 name = getAttribute(tree, NAME_ATTR, &error);
900 if (name != "" && error == "") {
901 printf("original-package:'%s'\n", name.string());
902 } else {
903 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
904 error.string());
905 goto bail;
906 }
Dan Morrill096b67f2010-12-13 16:25:54 -0800907 } else if (tag == "supports-gl-texture") {
Dan Morrill6f51fc12010-10-13 14:33:43 -0700908 String8 name = getAttribute(tree, NAME_ATTR, &error);
909 if (name != "" && error == "") {
Dan Morrill096b67f2010-12-13 16:25:54 -0800910 printf("supports-gl-texture:'%s'\n", name.string());
Dan Morrill6f51fc12010-10-13 14:33:43 -0700911 } else {
912 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
913 error.string());
914 goto bail;
915 }
Dianne Hackborna6d9c7c2010-10-21 15:32:06 -0700916 } else if (tag == "compatible-screens") {
917 printCompatibleScreens(tree);
918 depth--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700920 } else if (depth == 3 && withinApplication) {
921 withinActivity = false;
922 withinReceiver = false;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700923 withinService = false;
924 hasIntentFilter = false;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700925 if(tag == "activity") {
926 withinActivity = true;
927 activityName = getAttribute(tree, NAME_ATTR, &error);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 if (error != "") {
929 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
930 goto bail;
931 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700932
933 activityLabel = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 if (error != "") {
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700935 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 goto bail;
937 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700938
939 activityIcon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
940 if (error != "") {
941 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
942 goto bail;
943 }
944 } else if (tag == "uses-library") {
945 String8 libraryName = getAttribute(tree, NAME_ATTR, &error);
946 if (error != "") {
947 fprintf(stderr, "ERROR getting 'android:name' attribute for uses-library: %s\n", error.string());
948 goto bail;
949 }
Dianne Hackborn49237342009-08-27 20:08:01 -0700950 int req = getIntegerAttribute(tree,
951 REQUIRED_ATTR, NULL, 1);
952 printf("uses-library%s:'%s'\n",
953 req ? "" : "-not-required", libraryName.string());
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700954 } else if (tag == "receiver") {
955 withinReceiver = true;
956 receiverName = getAttribute(tree, NAME_ATTR, &error);
957
958 if (error != "") {
959 fprintf(stderr, "ERROR getting 'android:name' attribute for receiver: %s\n", error.string());
960 goto bail;
961 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700962 } else if (tag == "service") {
963 withinService = true;
964 serviceName = getAttribute(tree, NAME_ATTR, &error);
965
966 if (error != "") {
967 fprintf(stderr, "ERROR getting 'android:name' attribute for service: %s\n", error.string());
968 goto bail;
969 }
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700970 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700971 } else if ((depth == 4) && (tag == "intent-filter")) {
972 hasIntentFilter = true;
973 withinIntentFilter = true;
974 actMainActivity = actWidgetReceivers = actImeService = actWallpaperService = false;
975 } else if ((depth == 5) && withinIntentFilter){
976 String8 action;
977 if (tag == "action") {
978 action = getAttribute(tree, NAME_ATTR, &error);
979 if (error != "") {
980 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
981 goto bail;
982 }
983 if (withinActivity) {
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700984 if (action == "android.intent.action.MAIN") {
985 isMainActivity = true;
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700986 actMainActivity = true;
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700987 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700988 } else if (withinReceiver) {
989 if (action == "android.appwidget.action.APPWIDGET_UPDATE") {
990 actWidgetReceivers = true;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -0700991 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -0700992 } else if (withinService) {
993 if (action == "android.view.InputMethod") {
994 actImeService = true;
995 } else if (action == "android.service.wallpaper.WallpaperService") {
996 actWallpaperService = true;
997 }
998 }
999 if (action == "android.intent.action.SEARCH") {
1000 isSearchable = true;
1001 }
1002 }
1003
1004 if (tag == "category") {
1005 String8 category = getAttribute(tree, NAME_ATTR, &error);
1006 if (error != "") {
1007 fprintf(stderr, "ERROR getting 'name' attribute: %s\n", error.string());
1008 goto bail;
1009 }
1010 if (withinActivity) {
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -07001011 if (category == "android.intent.category.LAUNCHER") {
1012 isLauncherActivity = true;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -07001013 }
1014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 }
1016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001018
Dan Morrill89d97c12010-05-03 16:13:14 -07001019 /* The following blocks handle printing "inferred" uses-features, based
1020 * on whether related features or permissions are used by the app.
1021 * Note that the various spec*Feature variables denote whether the
1022 * relevant tag was *present* in the AndroidManfest, not that it was
1023 * present and set to true.
1024 */
1025 // Camera-related back-compatibility logic
1026 if (!specCameraFeature) {
1027 if (reqCameraFlashFeature || reqCameraAutofocusFeature) {
1028 // if app requested a sub-feature (autofocus or flash) and didn't
1029 // request the base camera feature, we infer that it meant to
1030 printf("uses-feature:'android.hardware.camera'\n");
1031 } else if (hasCameraPermission) {
1032 // if app wants to use camera but didn't request the feature, we infer
1033 // that it meant to, and further that it wants autofocus
1034 // (which was the 1.0 - 1.5 behavior)
1035 printf("uses-feature:'android.hardware.camera'\n");
1036 if (!specCameraAutofocusFeature) {
1037 printf("uses-feature:'android.hardware.camera.autofocus'\n");
1038 }
1039 }
Dianne Hackborne5276a72009-08-27 16:28:44 -07001040 }
Doug Zongkerdbe7a682009-10-09 11:24:51 -07001041
Dan Morrill89d97c12010-05-03 16:13:14 -07001042 // Location-related back-compatibility logic
1043 if (!specLocationFeature &&
1044 (hasMockLocPermission || hasCoarseLocPermission || hasGpsPermission ||
1045 hasGeneralLocPermission || reqNetworkLocFeature || reqGpsFeature)) {
1046 // if app either takes a location-related permission or requests one of the
1047 // sub-features, we infer that it also meant to request the base location feature
1048 printf("uses-feature:'android.hardware.location'\n");
1049 }
Dianne Hackbornef05e072010-03-01 17:43:39 -08001050 if (!specGpsFeature && hasGpsPermission) {
Dan Morrill89d97c12010-05-03 16:13:14 -07001051 // if app takes GPS (FINE location) perm but does not request the GPS
1052 // feature, we infer that it meant to
Dianne Hackbornef05e072010-03-01 17:43:39 -08001053 printf("uses-feature:'android.hardware.location.gps'\n");
1054 }
Dan Morrill89d97c12010-05-03 16:13:14 -07001055 if (!specNetworkLocFeature && hasCoarseLocPermission) {
1056 // if app takes Network location (COARSE location) perm but does not request the
1057 // network location feature, we infer that it meant to
1058 printf("uses-feature:'android.hardware.location.network'\n");
1059 }
1060
1061 // Bluetooth-related compatibility logic
Dan Morrill6b22d812010-06-15 21:41:42 -07001062 if (!specBluetoothFeature && hasBluetoothPermission && (targetSdk > 4)) {
Dan Morrill89d97c12010-05-03 16:13:14 -07001063 // if app takes a Bluetooth permission but does not request the Bluetooth
1064 // feature, we infer that it meant to
1065 printf("uses-feature:'android.hardware.bluetooth'\n");
1066 }
1067
1068 // Microphone-related compatibility logic
1069 if (!specMicrophoneFeature && hasRecordAudioPermission) {
1070 // if app takes the record-audio permission but does not request the microphone
1071 // feature, we infer that it meant to
1072 printf("uses-feature:'android.hardware.microphone'\n");
1073 }
1074
1075 // WiFi-related compatibility logic
1076 if (!specWiFiFeature && hasWiFiPermission) {
1077 // if app takes one of the WiFi permissions but does not request the WiFi
1078 // feature, we infer that it meant to
1079 printf("uses-feature:'android.hardware.wifi'\n");
1080 }
1081
1082 // Telephony-related compatibility logic
1083 if (!specTelephonyFeature && (hasTelephonyPermission || reqTelephonySubFeature)) {
1084 // if app takes one of the telephony permissions or requests a sub-feature but
1085 // does not request the base telephony feature, we infer that it meant to
1086 printf("uses-feature:'android.hardware.telephony'\n");
1087 }
1088
1089 // Touchscreen-related back-compatibility logic
1090 if (!specTouchscreenFeature) { // not a typo!
1091 // all apps are presumed to require a touchscreen, unless they explicitly say
1092 // <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
1093 // Note that specTouchscreenFeature is true if the tag is present, regardless
1094 // of whether its value is true or false, so this is safe
1095 printf("uses-feature:'android.hardware.touchscreen'\n");
1096 }
1097 if (!specMultitouchFeature && reqDistinctMultitouchFeature) {
1098 // if app takes one of the telephony permissions or requests a sub-feature but
1099 // does not request the base telephony feature, we infer that it meant to
1100 printf("uses-feature:'android.hardware.touchscreen.multitouch'\n");
1101 }
Dianne Hackbornef05e072010-03-01 17:43:39 -08001102
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001103 if (hasMainActivity) {
1104 printf("main\n");
1105 }
1106 if (hasWidgetReceivers) {
1107 printf("app-widget\n");
1108 }
1109 if (hasImeService) {
1110 printf("ime\n");
1111 }
1112 if (hasWallpaperService) {
1113 printf("wallpaper\n");
1114 }
1115 if (hasOtherActivities) {
1116 printf("other-activities\n");
1117 }
1118 if (isSearchable) {
1119 printf("search\n");
1120 }
1121 if (hasOtherReceivers) {
1122 printf("other-receivers\n");
1123 }
1124 if (hasOtherServices) {
1125 printf("other-services\n");
1126 }
1127
Dianne Hackborn723738c2009-06-25 19:48:04 -07001128 // Determine default values for any unspecified screen sizes,
1129 // based on the target SDK of the package. As of 4 (donut)
1130 // the screen size support was introduced, so all default to
1131 // enabled.
1132 if (smallScreen > 0) {
1133 smallScreen = targetSdk >= 4 ? -1 : 0;
1134 }
1135 if (normalScreen > 0) {
1136 normalScreen = -1;
1137 }
1138 if (largeScreen > 0) {
1139 largeScreen = targetSdk >= 4 ? -1 : 0;
1140 }
Dianne Hackbornf43489d2010-08-20 12:44:33 -07001141 if (xlargeScreen > 0) {
Scott Maind58fb972010-11-04 18:32:00 -07001142 // Introduced in Gingerbread.
1143 xlargeScreen = targetSdk >= 9 ? -1 : 0;
Dianne Hackbornf43489d2010-08-20 12:44:33 -07001144 }
Dianne Hackborna6d9c7c2010-10-21 15:32:06 -07001145 if (anyDensity > 0) {
1146 anyDensity = targetSdk >= 4 ? -1 : 0;
1147 }
Dianne Hackborn723738c2009-06-25 19:48:04 -07001148 printf("supports-screens:");
1149 if (smallScreen != 0) printf(" 'small'");
1150 if (normalScreen != 0) printf(" 'normal'");
1151 if (largeScreen != 0) printf(" 'large'");
Dianne Hackbornf43489d2010-08-20 12:44:33 -07001152 if (xlargeScreen != 0) printf(" 'xlarge'");
Dianne Hackborn723738c2009-06-25 19:48:04 -07001153 printf("\n");
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001154
Dianne Hackborna6d9c7c2010-10-21 15:32:06 -07001155 printf("supports-any-density: '%s'\n", anyDensity ? "true" : "false");
1156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 printf("locales:");
1158 Vector<String8> locales;
1159 res.getLocales(&locales);
Dianne Hackborne17086b2009-06-19 15:13:28 -07001160 const size_t NL = locales.size();
1161 for (size_t i=0; i<NL; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 const char* localeStr = locales[i].string();
1163 if (localeStr == NULL || strlen(localeStr) == 0) {
1164 localeStr = "--_--";
1165 }
1166 printf(" '%s'", localeStr);
1167 }
1168 printf("\n");
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001169
Dianne Hackborne17086b2009-06-19 15:13:28 -07001170 Vector<ResTable_config> configs;
1171 res.getConfigurations(&configs);
1172 SortedVector<int> densities;
1173 const size_t NC = configs.size();
1174 for (size_t i=0; i<NC; i++) {
1175 int dens = configs[i].density;
1176 if (dens == 0) dens = 160;
1177 densities.add(dens);
1178 }
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001179
Dianne Hackborne17086b2009-06-19 15:13:28 -07001180 printf("densities:");
1181 const size_t ND = densities.size();
1182 for (size_t i=0; i<ND; i++) {
1183 printf(" '%d'", densities[i]);
1184 }
1185 printf("\n");
Suchi Amalapurapu1b125982009-08-18 01:42:27 -07001186
Dianne Hackbornbb9ea302009-05-18 15:22:00 -07001187 AssetDir* dir = assets.openNonAssetDir(assetsCookie, "lib");
1188 if (dir != NULL) {
1189 if (dir->getFileCount() > 0) {
1190 printf("native-code:");
1191 for (size_t i=0; i<dir->getFileCount(); i++) {
1192 printf(" '%s'", dir->getFileName(i).string());
1193 }
1194 printf("\n");
1195 }
1196 delete dir;
1197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 } else if (strcmp("configurations", option) == 0) {
1199 Vector<ResTable_config> configs;
1200 res.getConfigurations(&configs);
1201 const size_t N = configs.size();
1202 for (size_t i=0; i<N; i++) {
1203 printf("%s\n", configs[i].toString().string());
1204 }
1205 } else {
1206 fprintf(stderr, "ERROR: unknown dump option '%s'\n", option);
1207 goto bail;
1208 }
1209 }
1210
1211 result = NO_ERROR;
Suchi Amalapurapu7ef189d2009-04-02 15:20:29 -07001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213bail:
1214 if (asset) {
1215 delete asset;
1216 }
1217 return (result != NO_ERROR);
1218}
1219
1220
1221/*
1222 * Handle the "add" command, which wants to add files to a new or
1223 * pre-existing archive.
1224 */
1225int doAdd(Bundle* bundle)
1226{
1227 ZipFile* zip = NULL;
1228 status_t result = UNKNOWN_ERROR;
1229 const char* zipFileName;
1230
1231 if (bundle->getUpdate()) {
1232 /* avoid confusion */
1233 fprintf(stderr, "ERROR: can't use '-u' with add\n");
1234 goto bail;
1235 }
1236
1237 if (bundle->getFileSpecCount() < 1) {
1238 fprintf(stderr, "ERROR: must specify zip file name\n");
1239 goto bail;
1240 }
1241 zipFileName = bundle->getFileSpecEntry(0);
1242
1243 if (bundle->getFileSpecCount() < 2) {
1244 fprintf(stderr, "NOTE: nothing to do\n");
1245 goto bail;
1246 }
1247
1248 zip = openReadWrite(zipFileName, true);
1249 if (zip == NULL) {
1250 fprintf(stderr, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName);
1251 goto bail;
1252 }
1253
1254 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1255 const char* fileName = bundle->getFileSpecEntry(i);
1256
1257 if (strcasecmp(String8(fileName).getPathExtension().string(), ".gz") == 0) {
1258 printf(" '%s'... (from gzip)\n", fileName);
1259 result = zip->addGzip(fileName, String8(fileName).getBasePath().string(), NULL);
1260 } else {
Doug Zongkerdbe7a682009-10-09 11:24:51 -07001261 if (bundle->getJunkPath()) {
1262 String8 storageName = String8(fileName).getPathLeaf();
1263 printf(" '%s' as '%s'...\n", fileName, storageName.string());
1264 result = zip->add(fileName, storageName.string(),
1265 bundle->getCompressionMethod(), NULL);
1266 } else {
1267 printf(" '%s'...\n", fileName);
1268 result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
1269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
1271 if (result != NO_ERROR) {
1272 fprintf(stderr, "Unable to add '%s' to '%s'", bundle->getFileSpecEntry(i), zipFileName);
1273 if (result == NAME_NOT_FOUND)
1274 fprintf(stderr, ": file not found\n");
1275 else if (result == ALREADY_EXISTS)
1276 fprintf(stderr, ": already exists in archive\n");
1277 else
1278 fprintf(stderr, "\n");
1279 goto bail;
1280 }
1281 }
1282
1283 result = NO_ERROR;
1284
1285bail:
1286 delete zip;
1287 return (result != NO_ERROR);
1288}
1289
1290
1291/*
1292 * Delete files from an existing archive.
1293 */
1294int doRemove(Bundle* bundle)
1295{
1296 ZipFile* zip = NULL;
1297 status_t result = UNKNOWN_ERROR;
1298 const char* zipFileName;
1299
1300 if (bundle->getFileSpecCount() < 1) {
1301 fprintf(stderr, "ERROR: must specify zip file name\n");
1302 goto bail;
1303 }
1304 zipFileName = bundle->getFileSpecEntry(0);
1305
1306 if (bundle->getFileSpecCount() < 2) {
1307 fprintf(stderr, "NOTE: nothing to do\n");
1308 goto bail;
1309 }
1310
1311 zip = openReadWrite(zipFileName, false);
1312 if (zip == NULL) {
1313 fprintf(stderr, "ERROR: failed opening Zip archive '%s'\n",
1314 zipFileName);
1315 goto bail;
1316 }
1317
1318 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1319 const char* fileName = bundle->getFileSpecEntry(i);
1320 ZipEntry* entry;
1321
1322 entry = zip->getEntryByName(fileName);
1323 if (entry == NULL) {
1324 printf(" '%s' NOT FOUND\n", fileName);
1325 continue;
1326 }
1327
1328 result = zip->remove(entry);
1329
1330 if (result != NO_ERROR) {
1331 fprintf(stderr, "Unable to delete '%s' from '%s'\n",
1332 bundle->getFileSpecEntry(i), zipFileName);
1333 goto bail;
1334 }
1335 }
1336
1337 /* update the archive */
1338 zip->flush();
1339
1340bail:
1341 delete zip;
1342 return (result != NO_ERROR);
1343}
1344
1345
1346/*
1347 * Package up an asset directory and associated application files.
1348 */
1349int doPackage(Bundle* bundle)
1350{
1351 const char* outputAPKFile;
1352 int retVal = 1;
1353 status_t err;
1354 sp<AaptAssets> assets;
1355 int N;
Josiah Gaskin9bf34ca2011-06-14 13:57:09 -07001356 FILE* fp;
1357 String8 dependencyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358
1359 // -c zz_ZZ means do pseudolocalization
1360 ResourceFilter filter;
1361 err = filter.parse(bundle->getConfigurations());
1362 if (err != NO_ERROR) {
1363 goto bail;
1364 }
1365 if (filter.containsPseudo()) {
1366 bundle->setPseudolocalize(true);
1367 }
1368
1369 N = bundle->getFileSpecCount();
1370 if (N < 1 && bundle->getResourceSourceDirs().size() == 0 && bundle->getJarFiles().size() == 0
1371 && bundle->getAndroidManifestFile() == NULL && bundle->getAssetSourceDir() == NULL) {
1372 fprintf(stderr, "ERROR: no input files\n");
1373 goto bail;
1374 }
1375
1376 outputAPKFile = bundle->getOutputAPKFile();
1377
1378 // Make sure the filenames provided exist and are of the appropriate type.
1379 if (outputAPKFile) {
1380 FileType type;
1381 type = getFileType(outputAPKFile);
1382 if (type != kFileTypeNonexistent && type != kFileTypeRegular) {
1383 fprintf(stderr,
1384 "ERROR: output file '%s' exists but is not regular file\n",
1385 outputAPKFile);
1386 goto bail;
1387 }
1388 }
1389
1390 // Load the assets.
1391 assets = new AaptAssets();
Josiah Gaskin9bf34ca2011-06-14 13:57:09 -07001392
1393 // Set up the resource gathering in assets if we're trying to make R.java
1394 if (bundle->getGenDependencies()) {
1395 sp<FilePathStore> pathStore = new FilePathStore;
1396 assets->setFullResPaths(pathStore);
1397 }
1398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 err = assets->slurpFromArgs(bundle);
1400 if (err < 0) {
1401 goto bail;
1402 }
1403
1404 if (bundle->getVerbose()) {
1405 assets->print();
1406 }
1407
Josiah Gaskin9bf34ca2011-06-14 13:57:09 -07001408 // If they asked for any fileAs that need to be compiled, do so.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 if (bundle->getResourceSourceDirs().size() || bundle->getAndroidManifestFile()) {
1410 err = buildResources(bundle, assets);
1411 if (err != 0) {
1412 goto bail;
1413 }
1414 }
1415
1416 // At this point we've read everything and processed everything. From here
1417 // on out it's just writing output files.
1418 if (SourcePos::hasErrors()) {
1419 goto bail;
1420 }
1421
Josiah Gaskin9bf34ca2011-06-14 13:57:09 -07001422 if (bundle->getGenDependencies()) {
1423 dependencyFile = String8(bundle->getRClassDir());
1424 // Make sure we have a clean dependency file to start with
1425 dependencyFile.appendPath("R.d");
1426 fp = fopen(dependencyFile, "w");
1427 fclose(fp);
1428 }
1429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 // Write out R.java constants
1431 if (assets->getPackage() == assets->getSymbolsPrivatePackage()) {
Xavier Ducrohet63459ad2009-11-30 18:05:10 -08001432 if (bundle->getCustomPackage() == NULL) {
1433 err = writeResourceSymbols(bundle, assets, assets->getPackage(), true);
Josiah Gaskince89f152011-06-08 19:31:40 -07001434 // Copy R.java for libraries
1435 if (bundle->getExtraPackages() != NULL) {
Josiah Gaskin9bf34ca2011-06-14 13:57:09 -07001436 // Split on colon
Josiah Gaskince89f152011-06-08 19:31:40 -07001437 String8 libs(bundle->getExtraPackages());
Josiah Gaskin9bf34ca2011-06-14 13:57:09 -07001438 char* packageString = strtok(libs.lockBuffer(libs.length()), ":");
Josiah Gaskince89f152011-06-08 19:31:40 -07001439 while (packageString != NULL) {
1440 err = writeResourceSymbols(bundle, assets, String8(packageString), true);
Josiah Gaskin9bf34ca2011-06-14 13:57:09 -07001441 packageString = strtok(NULL, ":");
Josiah Gaskince89f152011-06-08 19:31:40 -07001442 }
1443 libs.unlockBuffer();
1444 }
Xavier Ducrohet63459ad2009-11-30 18:05:10 -08001445 } else {
1446 const String8 customPkg(bundle->getCustomPackage());
1447 err = writeResourceSymbols(bundle, assets, customPkg, true);
1448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 if (err < 0) {
1450 goto bail;
1451 }
1452 } else {
1453 err = writeResourceSymbols(bundle, assets, assets->getPackage(), false);
1454 if (err < 0) {
1455 goto bail;
1456 }
1457 err = writeResourceSymbols(bundle, assets, assets->getSymbolsPrivatePackage(), true);
1458 if (err < 0) {
1459 goto bail;
1460 }
1461 }
1462
Josiah Gaskin9bf34ca2011-06-14 13:57:09 -07001463 if (bundle->getGenDependencies()) {
1464 // Now that writeResourceSymbols has taken care of writing the
1465 // dependency targets to the dependencyFile, we'll write the
1466 // pre-requisites.
1467 fp = fopen(dependencyFile, "a+");
1468 fprintf(fp, " : ");
1469 err = writeDependencyPreReqs(bundle, assets, fp);
1470
1471 // Also manually add the AndroidManifeset since it's a non-asset
1472 fprintf(fp, "%s \\\n", bundle->getAndroidManifestFile());
1473 fclose(fp);
1474 }
1475
Joe Onorato1553c822009-08-30 13:36:22 -07001476 // Write out the ProGuard file
1477 err = writeProguardFile(bundle, assets);
1478 if (err < 0) {
1479 goto bail;
1480 }
1481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 // Write the apk
1483 if (outputAPKFile) {
1484 err = writeAPK(bundle, assets, String8(outputAPKFile));
1485 if (err != NO_ERROR) {
1486 fprintf(stderr, "ERROR: packaging of '%s' failed\n", outputAPKFile);
1487 goto bail;
1488 }
1489 }
1490
1491 retVal = 0;
1492bail:
1493 if (SourcePos::hasErrors()) {
1494 SourcePos::printErrors(stderr);
1495 }
1496 return retVal;
1497}