Search in sources :

Example 36 with XmlResourceParser

use of android.content.res.XmlResourceParser in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class SettingsInjector method parseServiceInfo.

/**
     * Returns the settings parsed from the attributes of the
     * {@link SettingInjectorService#META_DATA_NAME} tag, or null.
     *
     * Duplicates some code from {@link android.content.pm.RegisteredServicesCache}.
     */
private InjectedSetting parseServiceInfo(ResolveInfo service, UserHandle userHandle, PackageManager pm) throws XmlPullParserException, IOException {
    ServiceInfo si = service.serviceInfo;
    ApplicationInfo ai = si.applicationInfo;
    if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
        if (Log.isLoggable(TAG, Log.WARN)) {
            Log.w(TAG, "Ignoring attempt to inject setting from app not in system image: " + service);
            return null;
        }
    } else if (!DimmableIZatIconPreference.showIzat(mContext, si.packageName)) {
        return null;
    }
    XmlResourceParser parser = null;
    try {
        parser = si.loadXmlMetaData(pm, SettingInjectorService.META_DATA_NAME);
        if (parser == null) {
            throw new XmlPullParserException("No " + SettingInjectorService.META_DATA_NAME + " meta-data for " + service + ": " + si);
        }
        AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        }
        String nodeName = parser.getName();
        if (!SettingInjectorService.ATTRIBUTES_NAME.equals(nodeName)) {
            throw new XmlPullParserException("Meta-data does not start with " + SettingInjectorService.ATTRIBUTES_NAME + " tag");
        }
        Resources res = pm.getResourcesForApplicationAsUser(si.packageName, userHandle.getIdentifier());
        return parseAttributes(si.packageName, si.name, userHandle, res, attrs);
    } catch (PackageManager.NameNotFoundException e) {
        throw new XmlPullParserException("Unable to load resources for package " + si.packageName);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
Also used : ServiceInfo(android.content.pm.ServiceInfo) XmlResourceParser(android.content.res.XmlResourceParser) PackageManager(android.content.pm.PackageManager) AttributeSet(android.util.AttributeSet) ApplicationInfo(android.content.pm.ApplicationInfo) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources)

Example 37 with XmlResourceParser

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

the class KeyphraseEnrollmentInfo method getKeyphraseMetadataFromApplicationInfo.

private KeyphraseMetadata getKeyphraseMetadataFromApplicationInfo(PackageManager pm, ApplicationInfo ai, List<String> parseErrors) {
    XmlResourceParser parser = null;
    String packageName = ai.packageName;
    KeyphraseMetadata keyphraseMetadata = null;
    try {
        parser = ai.loadXmlMetaData(pm, VOICE_KEYPHRASE_META_DATA);
        if (parser == null) {
            String error = "No " + VOICE_KEYPHRASE_META_DATA + " meta-data for " + packageName;
            parseErrors.add(error);
            Slog.w(TAG, error);
            return null;
        }
        Resources res = pm.getResourcesForApplication(ai);
        AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        }
        String nodeName = parser.getName();
        if (!"voice-enrollment-application".equals(nodeName)) {
            String error = "Meta-data does not start with voice-enrollment-application tag for " + packageName;
            parseErrors.add(error);
            Slog.w(TAG, error);
            return null;
        }
        TypedArray array = res.obtainAttributes(attrs, com.android.internal.R.styleable.VoiceEnrollmentApplication);
        keyphraseMetadata = getKeyphraseFromTypedArray(array, packageName, parseErrors);
        array.recycle();
    } catch (XmlPullParserException e) {
        String error = "Error parsing keyphrase enrollment meta-data for " + packageName;
        parseErrors.add(error + ": " + e);
        Slog.w(TAG, error, e);
    } catch (IOException e) {
        String error = "Error parsing keyphrase enrollment meta-data for " + packageName;
        parseErrors.add(error + ": " + e);
        Slog.w(TAG, error, e);
    } catch (PackageManager.NameNotFoundException e) {
        String error = "Error parsing keyphrase enrollment meta-data for " + packageName;
        parseErrors.add(error + ": " + e);
        Slog.w(TAG, error, e);
    } finally {
        if (parser != null)
            parser.close();
    }
    return keyphraseMetadata;
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) PackageManager(android.content.pm.PackageManager) AttributeSet(android.util.AttributeSet) TypedArray(android.content.res.TypedArray) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) IOException(java.io.IOException)

Example 38 with XmlResourceParser

use of android.content.res.XmlResourceParser in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class VoiceInputHelper method buildUi.

public void buildUi() {
    // Get the currently selected interactor from the secure setting.
    String currentSetting = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.VOICE_INTERACTION_SERVICE);
    if (currentSetting != null && !currentSetting.isEmpty()) {
        mCurrentVoiceInteraction = ComponentName.unflattenFromString(currentSetting);
    } else {
        mCurrentVoiceInteraction = null;
    }
    ArraySet<ComponentName> interactorRecognizers = new ArraySet<>();
    // Iterate through all the available interactors and load up their info to show
    // in the preference.
    int size = mAvailableVoiceInteractions.size();
    for (int i = 0; i < size; i++) {
        ResolveInfo resolveInfo = mAvailableVoiceInteractions.get(i);
        VoiceInteractionServiceInfo info = new VoiceInteractionServiceInfo(mContext.getPackageManager(), resolveInfo.serviceInfo);
        if (info.getParseError() != null) {
            Log.w("VoiceInteractionService", "Error in VoiceInteractionService " + resolveInfo.serviceInfo.packageName + "/" + resolveInfo.serviceInfo.name + ": " + info.getParseError());
            continue;
        }
        mAvailableInteractionInfos.add(new InteractionInfo(mContext.getPackageManager(), info));
        interactorRecognizers.add(new ComponentName(resolveInfo.serviceInfo.packageName, info.getRecognitionService()));
    }
    Collections.sort(mAvailableInteractionInfos);
    // Get the currently selected recognizer from the secure setting.
    currentSetting = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.VOICE_RECOGNITION_SERVICE);
    if (currentSetting != null && !currentSetting.isEmpty()) {
        mCurrentRecognizer = ComponentName.unflattenFromString(currentSetting);
    } else {
        mCurrentRecognizer = null;
    }
    // Iterate through all the available recognizers and load up their info to show
    // in the preference.
    size = mAvailableRecognition.size();
    for (int i = 0; i < size; i++) {
        ResolveInfo resolveInfo = mAvailableRecognition.get(i);
        ComponentName comp = new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
        if (interactorRecognizers.contains(comp)) {
        //continue;
        }
        ServiceInfo si = resolveInfo.serviceInfo;
        XmlResourceParser parser = null;
        String settingsActivity = null;
        try {
            parser = si.loadXmlMetaData(mContext.getPackageManager(), RecognitionService.SERVICE_META_DATA);
            if (parser == null) {
                throw new XmlPullParserException("No " + RecognitionService.SERVICE_META_DATA + " meta-data for " + si.packageName);
            }
            Resources res = mContext.getPackageManager().getResourcesForApplication(si.applicationInfo);
            AttributeSet attrs = Xml.asAttributeSet(parser);
            int type;
            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
            }
            String nodeName = parser.getName();
            if (!"recognition-service".equals(nodeName)) {
                throw new XmlPullParserException("Meta-data does not start with recognition-service tag");
            }
            TypedArray array = res.obtainAttributes(attrs, com.android.internal.R.styleable.RecognitionService);
            settingsActivity = array.getString(com.android.internal.R.styleable.RecognitionService_settingsActivity);
            array.recycle();
        } catch (XmlPullParserException e) {
            Log.e(TAG, "error parsing recognition service meta-data", e);
        } catch (IOException e) {
            Log.e(TAG, "error parsing recognition service meta-data", e);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "error parsing recognition service meta-data", e);
        } finally {
            if (parser != null)
                parser.close();
        }
        mAvailableRecognizerInfos.add(new RecognizerInfo(mContext.getPackageManager(), resolveInfo.serviceInfo, settingsActivity));
    }
    Collections.sort(mAvailableRecognizerInfos);
}
Also used : ArraySet(android.util.ArraySet) XmlResourceParser(android.content.res.XmlResourceParser) IOException(java.io.IOException) ResolveInfo(android.content.pm.ResolveInfo) VoiceInteractionServiceInfo(android.service.voice.VoiceInteractionServiceInfo) ServiceInfo(android.content.pm.ServiceInfo) PackageManager(android.content.pm.PackageManager) AttributeSet(android.util.AttributeSet) VoiceInteractionServiceInfo(android.service.voice.VoiceInteractionServiceInfo) TypedArray(android.content.res.TypedArray) ComponentName(android.content.ComponentName) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources)

Example 39 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 40 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)

Aggregations

XmlResourceParser (android.content.res.XmlResourceParser)508 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)267 IOException (java.io.IOException)247 AttributeSet (android.util.AttributeSet)241 Resources (android.content.res.Resources)146 TypedArray (android.content.res.TypedArray)97 Test (org.junit.Test)82 PackageManager (android.content.pm.PackageManager)68 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)68 ArrayList (java.util.ArrayList)52 ComponentName (android.content.ComponentName)49 Bundle (android.os.Bundle)44 ActivityInfo (android.content.pm.ActivityInfo)43 AssetManager (android.content.res.AssetManager)32 RemoteException (android.os.RemoteException)31 TypedValue (android.util.TypedValue)29 ResolveInfo (android.content.pm.ResolveInfo)27 Intent (android.content.Intent)25 ApplicationInfo (android.content.pm.ApplicationInfo)25 Context (android.content.Context)22