19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Copyright 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// Android Asset Packaging Tool main entry point.
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project//
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "Main.h"
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "Bundle.h"
8e6b680364dd992907a8d2037685a2e500d188dfbDianne Hackborn#include "ResourceFilter.h"
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "ResourceTable.h"
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "XMLNode.h"
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
123b4062eeb01be33951ba214e027da523cf09f8b1Mathias Agopian#include <utils/Log.h>
133b4062eeb01be33951ba214e027da523cf09f8b1Mathias Agopian#include <utils/threads.h>
143b4062eeb01be33951ba214e027da523cf09f8b1Mathias Agopian#include <utils/List.h>
153b4062eeb01be33951ba214e027da523cf09f8b1Mathias Agopian#include <utils/Errors.h>
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <fcntl.h>
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <errno.h>
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectusing namespace android;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Show version info.  All the cool kids do it.
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint doVersion(Bundle* bundle)
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (bundle->getFileSpecCount() != 0)
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        printf("(ignoring extra arguments)\n");
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    printf("Android Asset Packaging Tool, v0.2\n");
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return 0;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Open the file read only.  The call fails if the file doesn't exist.
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns NULL on failure.
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectZipFile* openReadOnly(const char* fileName)
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ZipFile* zip;
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    status_t result;
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    zip = new ZipFile;
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    result = zip->open(fileName, ZipFile::kOpenReadOnly);
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (result != NO_ERROR) {
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (result == NAME_NOT_FOUND)
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr, "ERROR: '%s' not found\n", fileName);
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        else if (result == PERMISSION_DENIED)
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr, "ERROR: '%s' access denied\n", fileName);
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        else
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr, "ERROR: failed opening '%s' as Zip file\n",
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                fileName);
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete zip;
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NULL;
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return zip;
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Open the file read-write.  The file will be created if it doesn't
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * already exist and "okayToCreate" is set.
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Returns NULL on failure.
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectZipFile* openReadWrite(const char* fileName, bool okayToCreate)
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ZipFile* zip = NULL;
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    status_t result;
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int flags;
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    flags = ZipFile::kOpenReadWrite;
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (okayToCreate)
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        flags |= ZipFile::kOpenCreate;
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    zip = new ZipFile;
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    result = zip->open(fileName, flags);
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (result != NO_ERROR) {
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete zip;
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        zip = NULL;
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        goto bail;
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbail:
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return zip;
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Return a short string describing the compression method.
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectconst char* compressionName(int method)
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (method == ZipEntry::kCompressStored)
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return "Stored";
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    else if (method == ZipEntry::kCompressDeflated)
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return "Deflated";
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    else
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return "Unknown";
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Return the percent reduction in size (0% == no compression).
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint calcPercent(long uncompressedLen, long compressedLen)
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (!uncompressedLen)
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    else
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (int) (100.0 - (compressedLen * 100.0) / uncompressedLen + 0.5);
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Handle the "list" command, which can be a simple file dump or
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * a verbose listing.
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The verbose listing closely matches the output of the Info-ZIP "unzip"
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * command.
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint doList(Bundle* bundle)
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int result = 1;
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ZipFile* zip = NULL;
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const ZipEntry* entry;
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    long totalUncLen, totalCompLen;
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const char* zipFileName;
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (bundle->getFileSpecCount() != 1) {
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fprintf(stderr, "ERROR: specify zip file name (only)\n");
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        goto bail;
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    zipFileName = bundle->getFileSpecEntry(0);
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    zip = openReadOnly(zipFileName);
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (zip == NULL)
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        goto bail;
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    int count, i;
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (bundle->getVerbose()) {
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        printf("Archive:  %s\n", zipFileName);
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        printf(
145fb2a9467ad77336edbf2a1a272dbe6ec30ef8a1bKenny Root            " Length   Method    Size  Ratio   Offset      Date  Time  CRC-32    Name\n");
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        printf(
147fb2a9467ad77336edbf2a1a272dbe6ec30ef8a1bKenny Root            "--------  ------  ------- -----  -------      ----  ----  ------    ----\n");
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    totalUncLen = totalCompLen = 0;
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    count = zip->getNumEntries();
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (i = 0; i < count; i++) {
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        entry = zip->getEntryByIndex(i);
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bundle->getVerbose()) {
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            char dateBuf[32];
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            time_t when;
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            when = entry->getModWhen();
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            strftime(dateBuf, sizeof(dateBuf), "%m-%d-%y %H:%M",
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                localtime(&when));
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
163fb2a9467ad77336edbf2a1a272dbe6ec30ef8a1bKenny Root            printf("%8ld  %-7.7s %7ld %3d%%  %8zd  %s  %08lx  %s\n",
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (long) entry->getUncompressedLen(),
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                compressionName(entry->getCompressionMethod()),
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (long) entry->getCompressedLen(),
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                calcPercent(entry->getUncompressedLen(),
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            entry->getCompressedLen()),
169fb2a9467ad77336edbf2a1a272dbe6ec30ef8a1bKenny Root                (size_t) entry->getLFHOffset(),
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                dateBuf,
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                entry->getCRC32(),
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                entry->getFileName());
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            printf("%s\n", entry->getFileName());
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        totalUncLen += entry->getUncompressedLen();
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        totalCompLen += entry->getCompressedLen();
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (bundle->getVerbose()) {
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        printf(
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        "--------          -------  ---                            -------\n");
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        printf("%8ld          %7ld  %2d%%                            %d files\n",
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            totalUncLen,
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            totalCompLen,
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            calcPercent(totalUncLen, totalCompLen),
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            zip->getNumEntries());
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (bundle->getAndroidList()) {
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AssetManager assets;
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!assets.addAssetPath(String8(zipFileName), NULL)) {
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr, "ERROR: list -a failed because assets could not be loaded\n");
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            goto bail;
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1977ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const ResTable& res = assets.getResources(false);
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (&res == NULL) {
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            printf("\nNo resource table found.\n");
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
202f1ff21ac62a51f5ba8ca0821ea8a90f70957e25dSteve Block#ifndef HAVE_ANDROID_OS
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            printf("\nResource table:\n");
204e17086ba346009a26cc2299b133a0fe602acb6ecDianne Hackborn            res.print(false);
205f1ff21ac62a51f5ba8ca0821ea8a90f70957e25dSteve Block#endif
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2077ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Asset* manifestAsset = assets.openNonAsset("AndroidManifest.xml",
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                   Asset::ACCESS_BUFFER);
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (manifestAsset == NULL) {
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            printf("\nNo AndroidManifest.xml found.\n");
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            printf("\nAndroid manifest:\n");
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ResXMLTree tree;
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            tree.setTo(manifestAsset->getBuffer(true),
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                       manifestAsset->getLength());
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            printXMLBlock(&tree);
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete manifestAsset;
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2217ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    result = 0;
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectbail:
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    delete zip;
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return result;
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic ssize_t indexOfAttribute(const ResXMLTree& tree, uint32_t attrRes)
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    size_t N = tree.getAttributeCount();
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    for (size_t i=0; i<N; i++) {
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (tree.getAttributeNameResID(i) == attrRes) {
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (ssize_t)i;
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return -1;
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2401553c82e084ac5763fb393857224145506771b99Joe OnoratoString8 getAttribute(const ResXMLTree& tree, const char* ns,
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            const char* attr, String8* outError)
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ssize_t idx = tree.indexOfAttribute(ns, attr);
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (idx < 0) {
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return String8();
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Res_value value;
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (value.dataType != Res_value::TYPE_STRING) {
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (outError != NULL) *outError = "attribute is not a string value";
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return String8();
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    size_t len;
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint16_t* str = tree.getAttributeStringValue(idx, &len);
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return str ? String8(str, len) : String8();
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic String8 getAttribute(const ResXMLTree& tree, uint32_t attrRes, String8* outError)
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ssize_t idx = indexOfAttribute(tree, attrRes);
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (idx < 0) {
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return String8();
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Res_value value;
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (value.dataType != Res_value::TYPE_STRING) {
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (outError != NULL) *outError = "attribute is not a string value";
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return String8();
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    size_t len;
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const uint16_t* str = tree.getAttributeStringValue(idx, &len);
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return str ? String8(str, len) : String8();
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
277bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackbornstatic int32_t getIntegerAttribute(const ResXMLTree& tree, uint32_t attrRes,
278bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn        String8* outError, int32_t defValue = -1)
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ssize_t idx = indexOfAttribute(tree, attrRes);
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (idx < 0) {
282bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn        return defValue;
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Res_value value;
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
286bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn        if (value.dataType < Res_value::TYPE_FIRST_INT
287bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                || value.dataType > Res_value::TYPE_LAST_INT) {
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (outError != NULL) *outError = "attribute is not an integer value";
289bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn            return defValue;
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return value.data;
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
295f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackbornstatic int32_t getResolvedIntegerAttribute(const ResTable* resTable, const ResXMLTree& tree,
296f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn        uint32_t attrRes, String8* outError, int32_t defValue = -1)
297f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn{
298f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn    ssize_t idx = indexOfAttribute(tree, attrRes);
299f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn    if (idx < 0) {
300f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn        return defValue;
301f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn    }
302f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn    Res_value value;
303f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn    if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
304f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn        if (value.dataType == Res_value::TYPE_REFERENCE) {
305f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn            resTable->resolveReference(&value, 0);
306f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn        }
307f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn        if (value.dataType < Res_value::TYPE_FIRST_INT
308f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn                || value.dataType > Res_value::TYPE_LAST_INT) {
309f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn            if (outError != NULL) *outError = "attribute is not an integer value";
310f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn            return defValue;
311f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn        }
312f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn    }
313f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn    return value.data;
314f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn}
315f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic String8 getResolvedAttribute(const ResTable* resTable, const ResXMLTree& tree,
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        uint32_t attrRes, String8* outError)
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    ssize_t idx = indexOfAttribute(tree, attrRes);
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (idx < 0) {
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return String8();
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Res_value value;
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (value.dataType == Res_value::TYPE_STRING) {
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            size_t len;
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const uint16_t* str = tree.getAttributeStringValue(idx, &len);
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return str ? String8(str, len) : String8();
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        resTable->resolveReference(&value, 0);
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (value.dataType != Res_value::TYPE_STRING) {
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (outError != NULL) *outError = "attribute is not a string value";
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return String8();
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    size_t len;
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const Res_value* value2 = &value;
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const char16_t* str = const_cast<ResTable*>(resTable)->valueToString(value2, 0, NULL, &len);
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return str ? String8(str, len) : String8();
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// These are attribute resource constants for the platform, as found
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// in android.R.attr
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectenum {
345f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn    LABEL_ATTR = 0x01010001,
346f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn    ICON_ATTR = 0x01010002,
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    NAME_ATTR = 0x01010003,
348b6ec11ef372a498c4d5fd2317a015b808495a2edDan Morrill    DEBUGGABLE_ATTR = 0x0101000f,
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    VERSION_CODE_ATTR = 0x0101021b,
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    VERSION_NAME_ATTR = 0x0101021c,
351f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn    SCREEN_ORIENTATION_ATTR = 0x0101001e,
352bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn    MIN_SDK_VERSION_ATTR = 0x0101020c,
35375c498426a17b47d445711bb9e36794b05876a56Suchi Amalapurapu    MAX_SDK_VERSION_ATTR = 0x01010271,
354bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn    REQ_TOUCH_SCREEN_ATTR = 0x01010227,
355bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn    REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
356bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn    REQ_HARD_KEYBOARD_ATTR = 0x01010229,
357bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn    REQ_NAVIGATION_ATTR = 0x0101022a,
358bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn    REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
359bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn    TARGET_SDK_VERSION_ATTR = 0x01010270,
360bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn    TEST_ONLY_ATTR = 0x01010272,
361a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    ANY_DENSITY_ATTR = 0x0101026c,
362e5276a74746b5e8f09b05a50155e7aa0bbb4a747Dianne Hackborn    GL_ES_VERSION_ATTR = 0x01010281,
363723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn    SMALL_SCREEN_ATTR = 0x01010284,
364723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn    NORMAL_SCREEN_ATTR = 0x01010285,
365723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn    LARGE_SCREEN_ATTR = 0x01010286,
366f43489d7d5e1c0c5c9a7e54c22845fcb99765f81Dianne Hackborn    XLARGE_SCREEN_ATTR = 0x010102bf,
367e5276a74746b5e8f09b05a50155e7aa0bbb4a747Dianne Hackborn    REQUIRED_ATTR = 0x0101028e,
368a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    SCREEN_SIZE_ATTR = 0x010102ca,
369a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    SCREEN_DENSITY_ATTR = 0x010102cb,
370e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    REQUIRES_SMALLEST_WIDTH_DP_ATTR = 0x01010364,
371e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    COMPATIBLE_WIDTH_LIMIT_DP_ATTR = 0x01010365,
372e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    LARGEST_WIDTH_LIMIT_DP_ATTR = 0x01010366,
37356088a55ca6c9277f09040a7883117cb127e1e56Kenny Root    PUBLIC_KEY_ATTR = 0x010103a6,
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project};
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3767ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapuconst char *getComponentName(String8 &pkgName, String8 &componentName) {
3777ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu    ssize_t idx = componentName.find(".");
3787ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu    String8 retStr(pkgName);
3797ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu    if (idx == 0) {
3807ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu        retStr += componentName;
3817ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu    } else if (idx < 0) {
3827ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu        retStr += ".";
3837ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu        retStr += componentName;
3847ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu    } else {
3857ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu        return componentName.string();
3867ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu    }
3877ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu    return retStr.string();
3887ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu}
3897ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
390a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackbornstatic void printCompatibleScreens(ResXMLTree& tree) {
391a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    size_t len;
392a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    ResXMLTree::event_code_t code;
393a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    int depth = 0;
394a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    bool first = true;
395a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    printf("compatible-screens:");
396a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
397a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn        if (code == ResXMLTree::END_TAG) {
398a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn            depth--;
399a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn            if (depth < 0) {
400a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn                break;
401a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn            }
402a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn            continue;
403a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn        }
404a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn        if (code != ResXMLTree::START_TAG) {
405a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn            continue;
406a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn        }
407a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn        depth++;
408a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn        String8 tag(tree.getElementName(&len));
409a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn        if (tag == "screen") {
410a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn            int32_t screenSize = getIntegerAttribute(tree,
411a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn                    SCREEN_SIZE_ATTR, NULL, -1);
412a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn            int32_t screenDensity = getIntegerAttribute(tree,
413a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn                    SCREEN_DENSITY_ATTR, NULL, -1);
414a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn            if (screenSize > 0 && screenDensity > 0) {
415a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn                if (!first) {
416a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn                    printf(",");
417a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn                }
418a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn                first = false;
419a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn                printf("'%d/%d'", screenSize, screenDensity);
420a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn            }
421a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn        }
422a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    }
423a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn    printf("\n");
424a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn}
425a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Handle the "dump" command, to extract select data from an archive.
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
429e74763e26f760e8de23a4edfbe599a166ef6bb53Dan Morrillextern char CONSOLE_DATA[2925]; // see EOF
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectint doDump(Bundle* bundle)
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    status_t result = UNKNOWN_ERROR;
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Asset* asset = NULL;
4347ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (bundle->getFileSpecCount() < 1) {
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fprintf(stderr, "ERROR: no dump option specified\n");
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 1;
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4397ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (bundle->getFileSpecCount() < 2) {
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fprintf(stderr, "ERROR: no dump file specified\n");
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 1;
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4447ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const char* option = bundle->getFileSpecEntry(0);
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const char* filename = bundle->getFileSpecEntry(1);
4477ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    AssetManager assets;
449bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn    void* assetsCookie;
450bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn    if (!assets.addAssetPath(String8(filename), &assetsCookie)) {
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fprintf(stderr, "ERROR: dump failed because assets could not be loaded\n");
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 1;
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4547ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
455e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    // Make a dummy config for retrieving resources...  we need to supply
456e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    // non-default values for some configs so that we can retrieve resources
457e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    // in the app that don't have a default.  The most important of these is
458e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    // the API version because key resources like icons will have an implicit
459e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    // version if they are using newer config types like density.
460e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    ResTable_config config;
461e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    config.language[0] = 'e';
462e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    config.language[1] = 'n';
463e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    config.country[0] = 'U';
464e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    config.country[1] = 'S';
465e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    config.orientation = ResTable_config::ORIENTATION_PORT;
466e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    config.density = ResTable_config::DENSITY_MEDIUM;
467e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    config.sdkVersion = 10000; // Very high.
468e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    config.screenWidthDp = 320;
469e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    config.screenHeightDp = 480;
470e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    config.smallestScreenWidthDp = 320;
471e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn    assets.setConfiguration(config);
472e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const ResTable& res = assets.getResources(false);
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (&res == NULL) {
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        fprintf(stderr, "ERROR: dump failed because no resource table was found\n");
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        goto bail;
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4787ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (strcmp("resources", option) == 0) {
480f1ff21ac62a51f5ba8ca0821ea8a90f70957e25dSteve Block#ifndef HAVE_ANDROID_OS
481e17086ba346009a26cc2299b133a0fe602acb6ecDianne Hackborn        res.print(bundle->getValues());
482f1ff21ac62a51f5ba8ca0821ea8a90f70957e25dSteve Block#endif
4836c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
4846c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn    } else if (strcmp("strings", option) == 0) {
4856c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        const ResStringPool* pool = res.getTableStringBlock(0);
4866c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn        printStringPool(pool);
4876c997a9e880e08c354ffd809bd62df9e25e9c4d4Dianne Hackborn
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else if (strcmp("xmltree", option) == 0) {
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bundle->getFileSpecCount() < 3) {
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            goto bail;
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4937ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (int i=2; i<bundle->getFileSpecCount(); i++) {
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const char* resname = bundle->getFileSpecEntry(i);
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ResXMLTree tree;
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (asset == NULL) {
49944b283dc27ed8ba5a946d809489f29b4ae88cb15Kenny Root                fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                goto bail;
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5027ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (tree.setTo(asset->getBuffer(true),
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           asset->getLength()) != NO_ERROR) {
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                goto bail;
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            tree.restart();
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            printXMLBlock(&tree);
51019138468caf7050d482dc15f35a344eab11bb756Kenny Root            tree.uninit();
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            delete asset;
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            asset = NULL;
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5147ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else if (strcmp("xmlstrings", option) == 0) {
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bundle->getFileSpecCount() < 3) {
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            goto bail;
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5207ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (int i=2; i<bundle->getFileSpecCount(); i++) {
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const char* resname = bundle->getFileSpecEntry(i);
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ResXMLTree tree;
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (asset == NULL) {
52644b283dc27ed8ba5a946d809489f29b4ae88cb15Kenny Root                fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                goto bail;
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5297ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (tree.setTo(asset->getBuffer(true),
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           asset->getLength()) != NO_ERROR) {
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                goto bail;
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            printStringPool(&tree.getStrings());
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            delete asset;
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            asset = NULL;
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5397ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } else {
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ResXMLTree tree;
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        asset = assets.openNonAsset("AndroidManifest.xml",
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            Asset::ACCESS_BUFFER);
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (asset == NULL) {
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr, "ERROR: dump failed because no AndroidManifest.xml found\n");
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            goto bail;
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5487ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (tree.setTo(asset->getBuffer(true),
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                       asset->getLength()) != NO_ERROR) {
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            fprintf(stderr, "ERROR: AndroidManifest.xml is corrupt\n");
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            goto bail;
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        tree.restart();
5557ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (strcmp("permissions", option) == 0) {
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            size_t len;
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ResXMLTree::event_code_t code;
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int depth = 0;
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (code == ResXMLTree::END_TAG) {
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    depth--;
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    continue;
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (code != ResXMLTree::START_TAG) {
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    continue;
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                depth++;
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                String8 tag(tree.getElementName(&len));
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                //printf("Depth %d tag %s\n", depth, tag.string());
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (depth == 1) {
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (tag != "manifest") {
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        goto bail;
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    String8 pkg = getAttribute(tree, NULL, "package", NULL);
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    printf("package: %s\n", pkg.string());
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (depth == 2 && tag == "permission") {
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    String8 error;
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    String8 name = getAttribute(tree, NAME_ATTR, &error);
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (error != "") {
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        fprintf(stderr, "ERROR: %s\n", error.string());
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        goto bail;
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    printf("permission: %s\n", name.string());
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (depth == 2 && tag == "uses-permission") {
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    String8 error;
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    String8 name = getAttribute(tree, NAME_ATTR, &error);
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (error != "") {
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        fprintf(stderr, "ERROR: %s\n", error.string());
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        goto bail;
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    printf("uses-permission: %s\n", name.string());
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (strcmp("badging", option) == 0) {
597e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            Vector<String8> locales;
598e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            res.getLocales(&locales);
599e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn
600e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            Vector<ResTable_config> configs;
601e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            res.getConfigurations(&configs);
602e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            SortedVector<int> densities;
603e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            const size_t NC = configs.size();
604e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            for (size_t i=0; i<NC; i++) {
605e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                int dens = configs[i].density;
606e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                if (dens == 0) dens = 160;
607e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                densities.add(dens);
608e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            }
609e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            size_t len;
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ResXMLTree::event_code_t code;
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int depth = 0;
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String8 error;
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bool withinActivity = false;
6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bool isMainActivity = false;
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bool isLauncherActivity = false;
6171b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool isSearchable = false;
6187ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu            bool withinApplication = false;
6197ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu            bool withinReceiver = false;
6201b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool withinService = false;
6211b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool withinIntentFilter = false;
6221b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool hasMainActivity = false;
6231b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool hasOtherActivities = false;
6241b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool hasOtherReceivers = false;
6251b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool hasOtherServices = false;
6261b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool hasWallpaperService = false;
6271b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool hasImeService = false;
6281b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool hasWidgetReceivers = false;
6291b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool hasIntentFilter = false;
6301b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool actMainActivity = false;
6311b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool actWidgetReceivers = false;
6321b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool actImeService = false;
6331b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            bool actWallpaperService = false;
63489d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill
635063a44e541335819b673fb1c20a338bbcaf3a3f5Kenny Root            // These two implement the implicit permissions that are granted
636063a44e541335819b673fb1c20a338bbcaf3a3f5Kenny Root            // to pre-1.6 applications.
637063a44e541335819b673fb1c20a338bbcaf3a3f5Kenny Root            bool hasWriteExternalStoragePermission = false;
638063a44e541335819b673fb1c20a338bbcaf3a3f5Kenny Root            bool hasReadPhoneStatePermission = false;
639063a44e541335819b673fb1c20a338bbcaf3a3f5Kenny Root
6407924512aa12c6af37d90e8ccfcdf04eb78a294a3Dianne Hackborn            // If an app requests write storage, they will also get read storage.
6417924512aa12c6af37d90e8ccfcdf04eb78a294a3Dianne Hackborn            bool hasReadExternalStoragePermission = false;
6427924512aa12c6af37d90e8ccfcdf04eb78a294a3Dianne Hackborn
64331b0e0e86ad061cd8005e80817bcad017e2d56ddDianne Hackborn            // Implement transition to read and write call log.
64431b0e0e86ad061cd8005e80817bcad017e2d56ddDianne Hackborn            bool hasReadContactsPermission = false;
64531b0e0e86ad061cd8005e80817bcad017e2d56ddDianne Hackborn            bool hasWriteContactsPermission = false;
64631b0e0e86ad061cd8005e80817bcad017e2d56ddDianne Hackborn            bool hasReadCallLogPermission = false;
64731b0e0e86ad061cd8005e80817bcad017e2d56ddDianne Hackborn            bool hasWriteCallLogPermission = false;
64831b0e0e86ad061cd8005e80817bcad017e2d56ddDianne Hackborn
64989d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            // This next group of variables is used to implement a group of
65089d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            // backward-compatibility heuristics necessitated by the addition of
65189d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            // some new uses-feature constants in 2.1 and 2.2. In most cases, the
65289d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            // heuristic is "if an app requests a permission but doesn't explicitly
65389d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            // request the corresponding <uses-feature>, presume it's there anyway".
65489d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool specCameraFeature = false; // camera-related
65589d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool specCameraAutofocusFeature = false;
65689d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool reqCameraAutofocusFeature = false;
65789d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool reqCameraFlashFeature = false;
658e5276a74746b5e8f09b05a50155e7aa0bbb4a747Dianne Hackborn            bool hasCameraPermission = false;
65989d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool specLocationFeature = false; // location-related
66089d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool specNetworkLocFeature = false;
66189d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool reqNetworkLocFeature = false;
662ef05e076ced1a32c5c0aaee28403779834adb2baDianne Hackborn            bool specGpsFeature = false;
66389d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool reqGpsFeature = false;
66489d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool hasMockLocPermission = false;
66589d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool hasCoarseLocPermission = false;
666ef05e076ced1a32c5c0aaee28403779834adb2baDianne Hackborn            bool hasGpsPermission = false;
66789d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool hasGeneralLocPermission = false;
66889d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool specBluetoothFeature = false; // Bluetooth API-related
66989d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool hasBluetoothPermission = false;
67089d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool specMicrophoneFeature = false; // microphone-related
67189d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool hasRecordAudioPermission = false;
67289d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool specWiFiFeature = false;
67389d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool hasWiFiPermission = false;
67489d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool specTelephonyFeature = false; // telephony-related
67589d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool reqTelephonySubFeature = false;
67689d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool hasTelephonyPermission = false;
67789d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool specTouchscreenFeature = false; // touchscreen-related
67889d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool specMultitouchFeature = false;
67989d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            bool reqDistinctMultitouchFeature = false;
680e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            bool specScreenPortraitFeature = false;
681e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            bool specScreenLandscapeFeature = false;
682f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn            bool reqScreenPortraitFeature = false;
683f77ae6e65a7ab47e7ab0ee1536d708b6e56fe45bDianne Hackborn            bool reqScreenLandscapeFeature = false;
68489d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            // 2.2 also added some other features that apps can request, but that
68589d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            // have no corresponding permission, so we cannot implement any
68689d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            // back-compatibility heuristic for them. The below are thus unnecessary
68789d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            // (but are retained here for documentary purposes.)
68889d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            //bool specCompassFeature = false;
68989d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            //bool specAccelerometerFeature = false;
69089d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            //bool specProximityFeature = false;
69189d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            //bool specAmbientLightFeature = false;
69289d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill            //bool specLiveWallpaperFeature = false;
69389d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill
694723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn            int targetSdk = 0;
695723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn            int smallScreen = 1;
696723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn            int normalScreen = 1;
697723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn            int largeScreen = 1;
698f43489d7d5e1c0c5c9a7e54c22845fcb99765f81Dianne Hackborn            int xlargeScreen = 1;
699a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn            int anyDensity = 1;
700e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            int requiresSmallestWidthDp = 0;
701e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            int compatibleWidthLimitDp = 0;
702e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn            int largestWidthLimitDp = 0;
7037ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu            String8 pkg;
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String8 activityName;
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String8 activityLabel;
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String8 activityIcon;
7077ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu            String8 receiverName;
7081b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu            String8 serviceName;
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (code == ResXMLTree::END_TAG) {
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    depth--;
7121b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                    if (depth < 2) {
7131b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        withinApplication = false;
7141b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                    } else if (depth < 3) {
7151b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        if (withinActivity && isMainActivity && isLauncherActivity) {
7161b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                            const char *aName = getComponentName(pkg, activityName);
717e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            printf("launchable-activity:");
7181b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                            if (aName != NULL) {
719e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                printf(" name='%s' ", aName);
7201b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                            }
721e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            printf(" label='%s' icon='%s'\n",
7221b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                                    activityLabel.string(),
7231b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                                    activityIcon.string());
7241b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        }
7251b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        if (!hasIntentFilter) {
7261b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                            hasOtherActivities |= withinActivity;
7271b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                            hasOtherReceivers |= withinReceiver;
7281b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                            hasOtherServices |= withinService;
7291b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        }
7301b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        withinActivity = false;
7311b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        withinService = false;
7321b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        withinReceiver = false;
7331b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        hasIntentFilter = false;
7341b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        isMainActivity = isLauncherActivity = false;
7351b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                    } else if (depth < 4) {
7361b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        if (withinIntentFilter) {
7371b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                            if (withinActivity) {
7381b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                                hasMainActivity |= actMainActivity;
7391b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                                hasOtherActivities |= !actMainActivity;
7401b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                            } else if (withinReceiver) {
7411b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                                hasWidgetReceivers |= actWidgetReceivers;
7421b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                                hasOtherReceivers |= !actWidgetReceivers;
7431b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                            } else if (withinService) {
7441b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                                hasImeService |= actImeService;
7451b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                                hasWallpaperService |= actWallpaperService;
7461b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                                hasOtherServices |= (!actImeService && !actWallpaperService);
7471b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                            }
7481b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        }
7491b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                        withinIntentFilter = false;
7501b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                    }
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    continue;
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (code != ResXMLTree::START_TAG) {
7549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    continue;
7559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
7569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                depth++;
7579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                String8 tag(tree.getElementName(&len));
7581b125980c5de597c18a179b9b02b9a5afe106895Suchi Amalapurapu                //printf("Depth %d,  %s\n", depth, tag.string());
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (depth == 1) {
7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (tag != "manifest") {
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        goto bail;
7639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
7647ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                    pkg = getAttribute(tree, NULL, "package", NULL);
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    printf("package: name='%s' ", pkg.string());
7669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    int32_t versionCode = getIntegerAttribute(tree, VERSION_CODE_ATTR, &error);
7679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (error != "") {
7689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        fprintf(stderr, "ERROR getting 'android:versionCode' attribute: %s\n", error.string());
7699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        goto bail;
7709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (versionCode > 0) {
7729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        printf("versionCode='%d' ", versionCode);
7739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } else {
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        printf("versionCode='' ");
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
776cf244ada58539ce857ec041d7288d0271204fbb6Dianne Hackborn                    String8 versionName = getResolvedAttribute(&res, tree, VERSION_NAME_ATTR, &error);
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (error != "") {
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        fprintf(stderr, "ERROR getting 'android:versionName' attribute: %s\n", error.string());
7799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        goto bail;
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    printf("versionName='%s'\n", versionName.string());
7827ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                } else if (depth == 2) {
7837ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                    withinApplication = false;
7847ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                    if (tag == "application") {
7857ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                        withinApplication = true;
786e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn
787e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        String8 label;
788e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        const size_t NL = locales.size();
789e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        for (size_t i=0; i<NL; i++) {
790e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            const char* localeStr =  locales[i].string();
791e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            assets.setLocale(localeStr != NULL ? localeStr : "");
792e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            String8 llabel = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
793e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            if (llabel != "") {
794e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                if (localeStr == NULL || strlen(localeStr) == 0) {
795e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                    label = llabel;
796e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                    printf("application-label:'%s'\n", llabel.string());
797e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                } else {
798e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                    if (label == "") {
799e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                        label = llabel;
800e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                    }
801e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                    printf("application-label-%s:'%s'\n", localeStr,
802e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                            llabel.string());
803e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                }
804e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            }
8057ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                        }
806e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn
807e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        ResTable_config tmpConfig = config;
808e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        const size_t ND = densities.size();
809e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        for (size_t i=0; i<ND; i++) {
810e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            tmpConfig.density = densities[i];
811e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            assets.setConfiguration(tmpConfig);
812e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            String8 icon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
813e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            if (icon != "") {
814e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                printf("application-icon-%d:'%s'\n", densities[i], icon.string());
815e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                            }
816e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        }
817e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        assets.setConfiguration(config);
818e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn
8197ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                        String8 icon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
8207ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                        if (error != "") {
8217ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                            fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
8227ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                            goto bail;
8237ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                        }
824bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        int32_t testOnly = getIntegerAttribute(tree, TEST_ONLY_ATTR, &error, 0);
8257ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                        if (error != "") {
826bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            fprintf(stderr, "ERROR getting 'android:testOnly' attribute: %s\n", error.string());
8277ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                            goto bail;
8287ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                        }
829e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        printf("application: label='%s' ", label.string());
830e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        printf("icon='%s'\n", icon.string());
831bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        if (testOnly != 0) {
832bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            printf("testOnly='%d'\n", testOnly);
833bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        }
834b6ec11ef372a498c4d5fd2317a015b808495a2edDan Morrill
835b6ec11ef372a498c4d5fd2317a015b808495a2edDan Morrill                        int32_t debuggable = getResolvedIntegerAttribute(&res, tree, DEBUGGABLE_ATTR, &error, 0);
836b6ec11ef372a498c4d5fd2317a015b808495a2edDan Morrill                        if (error != "") {
837b6ec11ef372a498c4d5fd2317a015b808495a2edDan Morrill                            fprintf(stderr, "ERROR getting 'android:debuggable' attribute: %s\n", error.string());
838b6ec11ef372a498c4d5fd2317a015b808495a2edDan Morrill                            goto bail;
839b6ec11ef372a498c4d5fd2317a015b808495a2edDan Morrill                        }
840b6ec11ef372a498c4d5fd2317a015b808495a2edDan Morrill                        if (debuggable != 0) {
841b6ec11ef372a498c4d5fd2317a015b808495a2edDan Morrill                            printf("application-debuggable\n");
842b6ec11ef372a498c4d5fd2317a015b808495a2edDan Morrill                        }
843bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                    } else if (tag == "uses-sdk") {
844bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        int32_t code = getIntegerAttribute(tree, MIN_SDK_VERSION_ATTR, &error);
845bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        if (error != "") {
846bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            error = "";
847bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            String8 name = getResolvedAttribute(&res, tree, MIN_SDK_VERSION_ATTR, &error);
848bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            if (error != "") {
849bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                fprintf(stderr, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
850bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                        error.string());
851bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                goto bail;
852bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            }
853723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                            if (name == "Donut") targetSdk = 4;
854bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            printf("sdkVersion:'%s'\n", name.string());
855bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        } else if (code != -1) {
856723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                            targetSdk = code;
857bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            printf("sdkVersion:'%d'\n", code);
858bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        }
85975c498426a17b47d445711bb9e36794b05876a56Suchi Amalapurapu                        code = getIntegerAttribute(tree, MAX_SDK_VERSION_ATTR, NULL, -1);
86075c498426a17b47d445711bb9e36794b05876a56Suchi Amalapurapu                        if (code != -1) {
86175c498426a17b47d445711bb9e36794b05876a56Suchi Amalapurapu                            printf("maxSdkVersion:'%d'\n", code);
86275c498426a17b47d445711bb9e36794b05876a56Suchi Amalapurapu                        }
863bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        code = getIntegerAttribute(tree, TARGET_SDK_VERSION_ATTR, &error);
864bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        if (error != "") {
865bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            error = "";
866bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            String8 name = getResolvedAttribute(&res, tree, TARGET_SDK_VERSION_ATTR, &error);
867bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            if (error != "") {
868bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                fprintf(stderr, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
869bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                        error.string());
870bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                goto bail;
871bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            }
872723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                            if (name == "Donut" && targetSdk < 4) targetSdk = 4;
873bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            printf("targetSdkVersion:'%s'\n", name.string());
874bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        } else if (code != -1) {
875723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                            if (targetSdk < code) {
876723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                                targetSdk = code;
877723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                            }
878bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            printf("targetSdkVersion:'%d'\n", code);
879bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        }
880bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                    } else if (tag == "uses-configuration") {
881bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        int32_t reqTouchScreen = getIntegerAttribute(tree,
882bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                REQ_TOUCH_SCREEN_ATTR, NULL, 0);
883bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        int32_t reqKeyboardType = getIntegerAttribute(tree,
884bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                REQ_KEYBOARD_TYPE_ATTR, NULL, 0);
885bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        int32_t reqHardKeyboard = getIntegerAttribute(tree,
886bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                REQ_HARD_KEYBOARD_ATTR, NULL, 0);
887bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        int32_t reqNavigation = getIntegerAttribute(tree,
888bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                REQ_NAVIGATION_ATTR, NULL, 0);
889bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        int32_t reqFiveWayNav = getIntegerAttribute(tree,
890bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                                REQ_FIVE_WAY_NAV_ATTR, NULL, 0);
891cb2d50d0d6e2e37ae02b014999da23ecd32038b8Dianne Hackborn                        printf("uses-configuration:");
892bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        if (reqTouchScreen != 0) {
893bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            printf(" reqTouchScreen='%d'", reqTouchScreen);
894bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        }
895bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        if (reqKeyboardType != 0) {
896bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            printf(" reqKeyboardType='%d'", reqKeyboardType);
897bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        }
898bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        if (reqHardKeyboard != 0) {
899bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            printf(" reqHardKeyboard='%d'", reqHardKeyboard);
900bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        }
901bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        if (reqNavigation != 0) {
902bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            printf(" reqNavigation='%d'", reqNavigation);
9037ef189d0a90f3c9a409512ab7dce9304639df481Suchi Amalapurapu                        }
904bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        if (reqFiveWayNav != 0) {
905bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                            printf(" reqFiveWayNav='%d'", reqFiveWayNav);
906bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        }
907bb9ea30ea9e390e69602935571795d2c80dc7b91Dianne Hackborn                        printf("\n");
908723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                    } else if (tag == "supports-screens") {
909723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                        smallScreen = getIntegerAttribute(tree,
910723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                                SMALL_SCREEN_ATTR, NULL, 1);
911723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                        normalScreen = getIntegerAttribute(tree,
912723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                                NORMAL_SCREEN_ATTR, NULL, 1);
913723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                        largeScreen = getIntegerAttribute(tree,
914723738cfaec3dd7b0fe152c872c41bebf94074c4Dianne Hackborn                                LARGE_SCREEN_ATTR, NULL, 1);
915f43489d7d5e1c0c5c9a7e54c22845fcb99765f81Dianne Hackborn                        xlargeScreen = getIntegerAttribute(tree,
916f43489d7d5e1c0c5c9a7e54c22845fcb99765f81Dianne Hackborn                                XLARGE_SCREEN_ATTR, NULL, 1);
917a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn                        anyDensity = getIntegerAttribute(tree,
918a0b46c9c441f017a2008ca8ee2c864987465996bDianne Hackborn                                ANY_DENSITY_ATTR, NULL, 1);
919e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        requiresSmallestWidthDp = getIntegerAttribute(tree,
920e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                REQUIRES_SMALLEST_WIDTH_DP_ATTR, NULL, 0);
921e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        compatibleWidthLimitDp = getIntegerAttribute(tree,
922e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                COMPATIBLE_WIDTH_LIMIT_DP_ATTR, NULL, 0);
923e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                        largestWidthLimitDp = getIntegerAttribute(tree,
924e289bff0ec2b1af4ba773e0c8d49d5fd46eb9921Dianne Hackborn                                LARGEST_WIDTH_LIMIT_DP_ATTR, NULL, 0);
925e5276a74746b5e8f09b05a50155e7aa0bbb4a747Dianne Hackborn                    } else if (tag == "uses-feature") {
926e5276a74746b5e8f09b05a50155e7aa0bbb4a747Dianne Hackborn                        String8 name = getAttribute(tree, NAME_ATTR, &error);
92740b94725dc1c62ea66db0b9502dd81b97dd3e35fSuchi Amalapurapu
92840b94725dc1c62ea66db0b9502dd81b97dd3e35fSuchi Amalapurapu                        if (name != "" && error == "") {
929e5276a74746b5e8f09b05a50155e7aa0bbb4a747Dianne Hackborn                            int req = getIntegerAttribute(tree,
930e5276a74746b5e8f09b05a50155e7aa0bbb4a747Dianne Hackborn                                    REQUIRED_ATTR, NULL, 1);
93189d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill
932e5276a74746b5e8f09b05a50155e7aa0bbb4a747Dianne Hackborn                            if (name == "android.hardware.camera") {
933e5276a74746b5e8f09b05a50155e7aa0bbb4a747Dianne Hackborn                                specCameraFeature = true;
93489d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                            } else if (name == "android.hardware.camera.autofocus") {
93589d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                // these have no corresponding permission to check for,
93689d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                // but should imply the foundational camera permission
93789d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                reqCameraAutofocusFeature = reqCameraAutofocusFeature || req;
93889d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                specCameraAutofocusFeature = true;
93989d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                            } else if (req && (name == "android.hardware.camera.flash")) {
94089d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                // these have no corresponding permission to check for,
94189d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                // but should imply the foundational camera permission
94289d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                reqCameraFlashFeature = true;
94389d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                            } else if (name == "android.hardware.location") {
94489d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                specLocationFeature = true;
94589d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                            } else if (name == "android.hardware.location.network") {
94689d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                specNetworkLocFeature = true;
94789d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                reqNetworkLocFeature = reqNetworkLocFeature || req;
948ef05e076ced1a32c5c0aaee28403779834adb2baDianne Hackborn                            } else if (name == "android.hardware.location.gps") {
949ef05e076ced1a32c5c0aaee28403779834adb2baDianne Hackborn                                specGpsFeature = true;
95089d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                reqGpsFeature = reqGpsFeature || req;
95189d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                            } else if (name == "android.hardware.bluetooth") {
95289d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                specBluetoothFeature = true;
95389d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                            } else if (name == "android.hardware.touchscreen") {
95489d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                                specTouchscreenFeature = true;
95589d97c1418c7a8b54fd57cc90daa1b1a98848bf6Dan Morrill                            } else if (name == "android.hardware.touchscreen.mult