Example usage for android.content.res AssetManager openXmlResourceParser

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

Introduction

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

Prototype

public @NonNull XmlResourceParser openXmlResourceParser(int cookie, @NonNull String fileName)
        throws IOException 

Source Link

Document

Retrieve a parser for a compiled XML file.

Usage

From source file:Main.java

public static String getMetaData(Context app, String name) {
    Bundle bundle = app.getApplicationInfo().metaData;
    if (bundle != null) {
        return bundle.getString(name);
    } else {/*from  w ww .  jav  a 2s  . co  m*/
        XmlResourceParser parser = null;
        AssetManager assmgr = null;

        try {
            assmgr = (AssetManager) AssetManager.class.newInstance();
            Method e = AssetManager.class.getDeclaredMethod("addAssetPath", new Class[] { String.class });
            e.setAccessible(true);
            int cookie = ((Integer) e.invoke(assmgr, new Object[] { app.getApplicationInfo().sourceDir }))
                    .intValue();
            if (cookie != 0) {
                String ANDROID_RESOURCES = "http://schemas.android.com/apk/res/android";
                parser = assmgr.openXmlResourceParser(cookie, "AndroidManifest.xml");
                boolean findAppMetadata = false;
                int event = parser.getEventType();
                while (event != 1) {
                    switch (event) {
                    case 2:
                        String nodeName = parser.getName();
                        String metadataName;
                        if ("meta-data".equals(nodeName)) {
                            findAppMetadata = true;
                            metadataName = parser.getAttributeValue(ANDROID_RESOURCES, "name");
                            if (metadataName.equals(name)) {
                                String var12 = parser.getAttributeValue(ANDROID_RESOURCES, "value");
                                return var12;
                            }
                        } else if (findAppMetadata) {
                            metadataName = null;
                            return metadataName;
                        }
                    default:
                        event = parser.next();
                    }
                }
            }
        } catch (Throwable var16) {
            var16.printStackTrace();
        } finally {
            if (parser != null) {
                parser.close();
            }

            if (assmgr != null) {
                assmgr.close();
            }

        }

        return null;
    }
}

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/* ww  w  .  j  a va2s . c om*/
 * @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);
    }
}

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

private void parseSplitApk(Package pkg, int splitIndex, AssetManager assets, int flags)
        throws PackageParserException {
    final String apkPath = pkg.splitCodePaths[splitIndex];
    final File apkFile = new File(apkPath);

    mParseError = PackageManager.INSTALL_SUCCEEDED;
    mArchiveSourcePath = apkPath;//w  ww  . j a v  a 2 s  .  c  om

    if (DEBUG_JAR)
        Slog.d(TAG, "Scanning split APK: " + apkPath);

    final int cookie = loadApkIntoAssetManager(assets, apkPath, flags);

    Resources res = null;
    XmlResourceParser parser = null;
    try {
        res = new Resources(assets, mMetrics, null);
        assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                Build.VERSION.RESOURCES_SDK_INT);
        parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);

        final String[] outError = new String[1];
        pkg = parseSplitApk(pkg, res, parser, flags, splitIndex, outError);
        if (pkg == null) {
            throw new PackageParserException(mParseError,
                    apkPath + " (at " + parser.getPositionDescription() + "): " + outError[0]);
        }

    } catch (PackageParserException e) {
        throw e;
    } catch (Exception e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
                "Failed to read manifest from " + apkPath, e);
    } finally {
        IoUtils.closeQuietly(parser);
    }
}

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

private Package parseBaseApk(File apkFile, AssetManager assets, int flags) throws PackageParserException {
    final String apkPath = apkFile.getAbsolutePath();

    String volumeUuid = null;/*from w  w  w  .  j  a va  2s  . c o m*/
    if (apkPath.startsWith(MNT_EXPAND)) {
        final int end = apkPath.indexOf('/', MNT_EXPAND.length());
        volumeUuid = apkPath.substring(MNT_EXPAND.length(), end);
    }

    mParseError = PackageManager.INSTALL_SUCCEEDED;
    mArchiveSourcePath = apkFile.getAbsolutePath();

    if (DEBUG_JAR)
        Slog.d(TAG, "Scanning base APK: " + apkPath);

    final int cookie = loadApkIntoAssetManager(assets, apkPath, flags);

    Resources res = null;
    XmlResourceParser parser = null;
    try {
        res = new Resources(assets, mMetrics, null);
        assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                Build.VERSION.RESOURCES_SDK_INT);
        parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);

        final String[] outError = new String[1];
        final Package pkg = parseBaseApk(res, parser, flags, outError);
        if (pkg == null) {
            throw new PackageParserException(mParseError,
                    apkPath + " (at " + parser.getPositionDescription() + "): " + outError[0]);
        }

        pkg.volumeUuid = volumeUuid;
        pkg.applicationInfo.volumeUuid = volumeUuid;
        pkg.baseCodePath = apkPath;
        pkg.mSignatures = null;

        return pkg;

    } catch (PackageParserException e) {
        throw e;
    } catch (Exception e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
                "Failed to read manifest from " + apkPath, e);
    } finally {
        IoUtils.closeQuietly(parser);
    }
}