Search in sources :

Example 76 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by DirtyUnicorns.

the class PackageParser method parseBaseApk.

private Package parseBaseApk(File apkFile, AssetManager assets, int flags) throws PackageParserException {
    final String apkPath = apkFile.getAbsolutePath();
    String volumeUuid = null;
    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.setVolumeUuid(volumeUuid);
        pkg.setApplicationVolumeUuid(volumeUuid);
        pkg.setBaseCodePath(apkPath);
        pkg.setSignatures(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);
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) Resources(android.content.res.Resources) GeneralSecurityException(java.security.GeneralSecurityException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException)

Example 77 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by DirtyUnicorns.

the class PackageParser method parseApkLite.

/**
     * 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
     * @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;
        final Certificate[][] certificates;
        if ((flags & PARSE_COLLECT_CERTIFICATES) != 0) {
            // TODO: factor signature related items out of Package object
            final Package tempPkg = new Package(null);
            Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "collectCertificates");
            try {
                collectCertificates(tempPkg, apkFile, 0);
            } finally {
                Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
            }
            signatures = tempPkg.mSignatures;
            certificates = tempPkg.mCertificates;
        } else {
            signatures = null;
            certificates = null;
        }
        final AttributeSet attrs = parser;
        return parseApkLite(apkPath, res, parser, attrs, flags, signatures, certificates);
    } 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);
    }
}
Also used : AssetManager(android.content.res.AssetManager) XmlResourceParser(android.content.res.XmlResourceParser) IOException(java.io.IOException) DisplayMetrics(android.util.DisplayMetrics) AttributeSet(android.util.AttributeSet) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources)

Example 78 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by DirtyUnicorns.

the class XmlConfigSource method parseDebugOverridesResource.

private NetworkSecurityConfig.Builder parseDebugOverridesResource() throws IOException, XmlPullParserException, ParserException {
    Resources resources = mContext.getResources();
    String packageName = resources.getResourcePackageName(mResourceId);
    String entryName = resources.getResourceEntryName(mResourceId);
    int resId = resources.getIdentifier(entryName + "_debug", "xml", packageName);
    // No debug-overrides resource was found, nothing to parse.
    if (resId == 0) {
        return null;
    }
    NetworkSecurityConfig.Builder debugConfigBuilder = null;
    // Parse debug-overrides out of the _debug resource.
    try (XmlResourceParser parser = resources.getXml(resId)) {
        XmlUtils.beginDocument(parser, "network-security-config");
        int outerDepth = parser.getDepth();
        boolean seenDebugOverrides = false;
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if ("debug-overrides".equals(parser.getName())) {
                if (seenDebugOverrides) {
                    throw new ParserException(parser, "Only one debug-overrides allowed");
                }
                if (mDebugBuild) {
                    debugConfigBuilder = parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
                } else {
                    XmlUtils.skipCurrentTag(parser);
                }
                seenDebugOverrides = true;
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }
    }
    return debugConfigBuilder;
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlResourceParser(android.content.res.XmlResourceParser) Resources(android.content.res.Resources)

Example 79 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by DirtyUnicorns.

the class XmlConfigSource method ensureInitialized.

private void ensureInitialized() {
    synchronized (mLock) {
        if (mInitialized) {
            return;
        }
        try (XmlResourceParser parser = mContext.getResources().getXml(mResourceId)) {
            parseNetworkSecurityConfig(parser);
            mContext = null;
            mInitialized = true;
        } catch (Resources.NotFoundException | XmlPullParserException | IOException | ParserException e) {
            throw new RuntimeException("Failed to parse XML configuration from " + mContext.getResources().getResourceEntryName(mResourceId), e);
        }
    }
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 80 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by DirtyUnicorns.

the class AutoText method init.

private void init(Resources r) {
    XmlResourceParser parser = r.getXml(com.android.internal.R.xml.autotext);
    StringBuilder right = new StringBuilder(RIGHT);
    mTrie = new char[DEFAULT];
    mTrie[TRIE_ROOT] = TRIE_NULL;
    mTrieUsed = TRIE_ROOT + 1;
    try {
        XmlUtils.beginDocument(parser, "words");
        String odest = "";
        char ooff = 0;
        while (true) {
            XmlUtils.nextElement(parser);
            String element = parser.getName();
            if (element == null || !(element.equals("word"))) {
                break;
            }
            String src = parser.getAttributeValue(null, "src");
            if (parser.next() == XmlPullParser.TEXT) {
                String dest = parser.getText();
                char off;
                if (dest.equals(odest)) {
                    off = ooff;
                } else {
                    off = (char) right.length();
                    right.append((char) dest.length());
                    right.append(dest);
                }
                add(src, off);
            }
        }
        // Don't let Resources cache a copy of all these strings.
        r.flushLayoutCache();
    } catch (XmlPullParserException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        parser.close();
    }
    mText = right.toString();
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

XmlResourceParser (android.content.res.XmlResourceParser)331 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)210 IOException (java.io.IOException)202 Resources (android.content.res.Resources)114 AttributeSet (android.util.AttributeSet)113 TypedArray (android.content.res.TypedArray)80 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)64 PackageManager (android.content.pm.PackageManager)45 ActivityInfo (android.content.pm.ActivityInfo)43 Bundle (android.os.Bundle)42 ComponentName (android.content.ComponentName)41 ArrayList (java.util.ArrayList)30 TypedValue (android.util.TypedValue)29 AssetManager (android.content.res.AssetManager)28 RemoteException (android.os.RemoteException)28 Intent (android.content.Intent)22 ApplicationInfo (android.content.pm.ApplicationInfo)18 InflateException (android.view.InflateException)18 FileNotFoundException (java.io.FileNotFoundException)18 NotFoundException (android.content.res.Resources.NotFoundException)17