Example usage for android.content.res AssetManager AssetManager

List of usage examples for android.content.res AssetManager AssetManager

Introduction

In this page you can find the example usage for android.content.res AssetManager AssetManager.

Prototype

@UnsupportedAppUsage
public AssetManager() 

Source Link

Document

Create a new AssetManager containing only the basic system assets.

Usage

From source file:android.content.pm.PackageParser.java

/**
 * Parse all APKs contained in the given directory, treating them as a
 * single package. This also performs sanity checking, such as requiring
 * identical package name and version codes, a single base APK, and unique
 * split names.//from www  .j  a  va 2s  .co m
 * <p>
 * Note that this <em>does not</em> perform signature verification; that
 * must be done separately in {@link #collectCertificates(Package, int)}.
 */
private Package parseClusterPackage(File packageDir, int flags) throws PackageParserException {
    final PackageLite lite = parseClusterPackageLite(packageDir, 0);

    if (mOnlyCoreApps && !lite.coreApp) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
                "Not a coreApp: " + packageDir);
    }

    final AssetManager assets = new AssetManager();
    try {
        // Load the base and all splits into the AssetManager
        // so that resources can be overriden when parsing the manifests.
        loadApkIntoAssetManager(assets, lite.baseCodePath, flags);

        if (!ArrayUtils.isEmpty(lite.splitCodePaths)) {
            for (String path : lite.splitCodePaths) {
                loadApkIntoAssetManager(assets, path, flags);
            }
        }

        final File baseApk = new File(lite.baseCodePath);
        final Package pkg = parseBaseApk(baseApk, assets, flags);
        if (pkg == null) {
            throw new PackageParserException(INSTALL_PARSE_FAILED_NOT_APK,
                    "Failed to parse base APK: " + baseApk);
        }

        if (!ArrayUtils.isEmpty(lite.splitNames)) {
            final int num = lite.splitNames.length;
            pkg.splitNames = lite.splitNames;
            pkg.splitCodePaths = lite.splitCodePaths;
            pkg.splitRevisionCodes = lite.splitRevisionCodes;
            pkg.splitFlags = new int[num];
            pkg.splitPrivateFlags = new int[num];

            for (int i = 0; i < num; i++) {
                parseSplitApk(pkg, i, assets, flags);
            }
        }

        pkg.codePath = packageDir.getAbsolutePath();
        return pkg;
    } finally {
        IoUtils.closeQuietly(assets);
    }
}

From source file:android.content.pm.PackageParser.java

/**
 * Parse the given APK file, treating it as as a single monolithic package.
 * <p>//  ww w  .  j av a 2  s . co  m
 * Note that this <em>does not</em> perform signature verification; that
 * must be done separately in {@link #collectCertificates(Package, int)}.
 *
 * @deprecated external callers should move to
 *             {@link #parsePackage(File, int)}. Eventually this method will
 *             be marked private.
 */
@Deprecated
public Package parseMonolithicPackage(File apkFile, int flags) throws PackageParserException {
    if (mOnlyCoreApps) {
        final PackageLite lite = parseMonolithicPackageLite(apkFile, flags);
        if (!lite.coreApp) {
            throw new PackageParserException(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
                    "Not a coreApp: " + apkFile);
        }
    }

    final AssetManager assets = new AssetManager();
    try {
        final Package pkg = parseBaseApk(apkFile, assets, flags);
        pkg.codePath = apkFile.getAbsolutePath();
        return pkg;
    } finally {
        IoUtils.closeQuietly(assets);
    }
}

From source file:android.content.pm.PackageParser.java

/**
 * Utility method that retrieves lightweight details about a single APK
 * file, including package name, split name, and install location.
 *
 * @param apkFile path to a single APK/*  w  w  w . jav a  2s .  com*/
 * @param flags optional parse flags, such as
 *            {@link #PARSE_COLLECT_CERTIFICATES}
 */
public static ApkLite parseApkLite(File apkFile, int flags) throws PackageParserException {
    final String apkPath = apkFile.getAbsolutePath();

    AssetManager assets = null;
    XmlResourceParser parser = null;
    try {
        assets = new AssetManager();
        assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                Build.VERSION.RESOURCES_SDK_INT);

        int cookie = assets.addAssetPath(apkPath);
        if (cookie == 0) {
            throw new PackageParserException(INSTALL_PARSE_FAILED_NOT_APK, "Failed to parse " + apkPath);
        }

        final DisplayMetrics metrics = new DisplayMetrics();
        metrics.setToDefaults();

        final Resources res = new Resources(assets, metrics, null);
        parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);

        final Signature[] signatures;
        if ((flags & PARSE_COLLECT_CERTIFICATES) != 0) {
            // TODO: factor signature related items out of Package object
            final Package tempPkg = new Package(null);
            collectCertificates(tempPkg, apkFile, 0);
            signatures = tempPkg.mSignatures;
        } else {
            signatures = null;
        }

        final AttributeSet attrs = parser;
        return parseApkLite(apkPath, res, parser, attrs, flags, signatures);

    } catch (XmlPullParserException | IOException | RuntimeException e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
                "Failed to parse " + apkPath, e);
    } finally {
        IoUtils.closeQuietly(parser);
        IoUtils.closeQuietly(assets);
    }
}