Search in sources :

Example 46 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class AppOpsService method writeState.

void writeState() {
    synchronized (mFile) {
        FileOutputStream stream;
        try {
            stream = mFile.startWrite();
        } catch (IOException e) {
            Slog.w(TAG, "Failed to write state: " + e);
            return;
        }
        SparseArray<UidState> outUidStates = null;
        synchronized (this) {
            final int uidStateCount = mUidStates.size();
            for (int i = 0; i < uidStateCount; i++) {
                UidState uidState = mUidStates.valueAt(i);
                SparseIntArray opModes = uidState.opModes;
                if (opModes != null && opModes.size() > 0) {
                    UidState outUidState = new UidState(uidState.uid);
                    outUidState.opModes = opModes.clone();
                    if (outUidStates == null) {
                        outUidStates = new SparseArray<>();
                    }
                    outUidStates.put(mUidStates.keyAt(i), outUidState);
                }
            }
        }
        List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
        try {
            XmlSerializer out = new FastXmlSerializer();
            out.setOutput(stream, StandardCharsets.UTF_8.name());
            out.startDocument(null, true);
            out.startTag(null, "app-ops");
            final int uidStateCount = outUidStates != null ? outUidStates.size() : 0;
            for (int i = 0; i < uidStateCount; i++) {
                UidState uidState = outUidStates.valueAt(i);
                if (uidState.opModes != null && uidState.opModes.size() > 0) {
                    out.startTag(null, "uid");
                    out.attribute(null, "n", Integer.toString(uidState.uid));
                    SparseIntArray uidOpModes = uidState.opModes;
                    final int opCount = uidOpModes.size();
                    for (int j = 0; j < opCount; j++) {
                        final int op = uidOpModes.keyAt(j);
                        final int mode = uidOpModes.valueAt(j);
                        out.startTag(null, "op");
                        out.attribute(null, "n", Integer.toString(op));
                        out.attribute(null, "m", Integer.toString(mode));
                        out.endTag(null, "op");
                    }
                    out.endTag(null, "uid");
                }
            }
            if (allOps != null) {
                String lastPkg = null;
                for (int i = 0; i < allOps.size(); i++) {
                    AppOpsManager.PackageOps pkg = allOps.get(i);
                    if (!pkg.getPackageName().equals(lastPkg)) {
                        if (lastPkg != null) {
                            out.endTag(null, "pkg");
                        }
                        lastPkg = pkg.getPackageName();
                        out.startTag(null, "pkg");
                        out.attribute(null, "n", lastPkg);
                    }
                    out.startTag(null, "uid");
                    out.attribute(null, "n", Integer.toString(pkg.getUid()));
                    synchronized (this) {
                        Ops ops = getOpsRawLocked(pkg.getUid(), pkg.getPackageName(), false);
                        // from Ops.
                        if (ops != null) {
                            out.attribute(null, "p", Boolean.toString(ops.isPrivileged));
                        } else {
                            out.attribute(null, "p", Boolean.toString(false));
                        }
                    }
                    List<AppOpsManager.OpEntry> ops = pkg.getOps();
                    for (int j = 0; j < ops.size(); j++) {
                        AppOpsManager.OpEntry op = ops.get(j);
                        out.startTag(null, "op");
                        out.attribute(null, "n", Integer.toString(op.getOp()));
                        out.attribute(null, "ns", AppOpsManager.opToName(op.getOp()));
                        int defaultMode = getDefaultMode(op.getOp(), pkg.getUid(), pkg.getPackageName());
                        if (op.getMode() != defaultMode) {
                            out.attribute(null, "m", Integer.toString(op.getMode()));
                        } else {
                            out.attribute(null, "dm", Integer.toString(defaultMode));
                        }
                        long time = op.getTime();
                        if (time != 0) {
                            out.attribute(null, "t", Long.toString(time));
                        }
                        time = op.getRejectTime();
                        if (time != 0) {
                            out.attribute(null, "r", Long.toString(time));
                        }
                        int dur = op.getDuration();
                        if (dur != 0) {
                            out.attribute(null, "d", Integer.toString(dur));
                        }
                        int proxyUid = op.getProxyUid();
                        if (proxyUid != -1) {
                            out.attribute(null, "pu", Integer.toString(proxyUid));
                        }
                        String proxyPackageName = op.getProxyPackageName();
                        if (proxyPackageName != null) {
                            out.attribute(null, "pp", proxyPackageName);
                        }
                        int allowed = op.getAllowedCount();
                        if (allowed != 0) {
                            out.attribute(null, "ac", Integer.toString(allowed));
                        }
                        int ignored = op.getIgnoredCount();
                        if (ignored != 0) {
                            out.attribute(null, "ic", Integer.toString(ignored));
                        }
                        out.endTag(null, "op");
                    }
                    out.endTag(null, "uid");
                }
                if (lastPkg != null) {
                    out.endTag(null, "pkg");
                }
            }
            out.endTag(null, "app-ops");
            out.endDocument();
            mFile.finishWrite(stream);
        } catch (IOException e) {
            Slog.w(TAG, "Failed to write state, restoring backup.", e);
            mFile.failWrite(stream);
        }
    }
}
Also used : IOException(java.io.IOException) AppOpsManager(android.app.AppOpsManager) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) SparseIntArray(android.util.SparseIntArray) FileOutputStream(java.io.FileOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 47 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class TaskPersister method saveToXml.

private StringWriter saveToXml(TaskRecord task) throws IOException, XmlPullParserException {
    if (DEBUG)
        Slog.d(TAG, "saveToXml: task=" + task);
    final XmlSerializer xmlSerializer = new FastXmlSerializer();
    StringWriter stringWriter = new StringWriter();
    xmlSerializer.setOutput(stringWriter);
    if (DEBUG)
        xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
    // save task
    xmlSerializer.startDocument(null, true);
    xmlSerializer.startTag(null, TAG_TASK);
    task.saveToXml(xmlSerializer);
    xmlSerializer.endTag(null, TAG_TASK);
    xmlSerializer.endDocument();
    xmlSerializer.flush();
    return stringWriter;
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) StringWriter(java.io.StringWriter) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 48 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class FingerprintsUserState method doWriteState.

private void doWriteState() {
    AtomicFile destination = new AtomicFile(mFile);
    ArrayList<Fingerprint> fingerprints;
    synchronized (this) {
        fingerprints = getCopy(mFingerprints);
    }
    FileOutputStream out = null;
    try {
        out = destination.startWrite();
        XmlSerializer serializer = Xml.newSerializer();
        serializer.setOutput(out, "utf-8");
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startDocument(null, true);
        serializer.startTag(null, TAG_FINGERPRINTS);
        final int count = fingerprints.size();
        for (int i = 0; i < count; i++) {
            Fingerprint fp = fingerprints.get(i);
            serializer.startTag(null, TAG_FINGERPRINT);
            serializer.attribute(null, ATTR_FINGER_ID, Integer.toString(fp.getFingerId()));
            serializer.attribute(null, ATTR_NAME, fp.getName().toString());
            serializer.attribute(null, ATTR_GROUP_ID, Integer.toString(fp.getGroupId()));
            serializer.attribute(null, ATTR_DEVICE_ID, Long.toString(fp.getDeviceId()));
            serializer.endTag(null, TAG_FINGERPRINT);
        }
        serializer.endTag(null, TAG_FINGERPRINTS);
        serializer.endDocument();
        destination.finishWrite(out);
    // Any error while writing is fatal.
    } catch (Throwable t) {
        Slog.wtf(TAG, "Failed to write settings, restoring backup", t);
        destination.failWrite(out);
        throw new IllegalStateException("Failed to write fingerprints", t);
    } finally {
        IoUtils.closeQuietly(out);
    }
}
Also used : Fingerprint(android.hardware.fingerprint.Fingerprint) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) Fingerprint(android.hardware.fingerprint.Fingerprint) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 49 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class EphemeralApplicationRegistry method writeUninstalledEphemeralAppMetadata.

private void writeUninstalledEphemeralAppMetadata(EphemeralApplicationInfo ephemeralApp, int userId) {
    File appDir = getEphemeralApplicationDir(ephemeralApp.getPackageName(), userId);
    if (!appDir.exists() && !appDir.mkdirs()) {
        return;
    }
    File metadataFile = new File(appDir, EPHEMERAL_APP_METADATA_FILE);
    AtomicFile destination = new AtomicFile(metadataFile);
    FileOutputStream out = null;
    try {
        out = destination.startWrite();
        XmlSerializer serializer = Xml.newSerializer();
        serializer.setOutput(out, StandardCharsets.UTF_8.name());
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startDocument(null, true);
        serializer.startTag(null, TAG_PACKAGE);
        serializer.attribute(null, ATTR_LABEL, ephemeralApp.loadLabel(mService.mContext.getPackageManager()).toString());
        serializer.startTag(null, TAG_PERMS);
        for (String permission : ephemeralApp.getRequestedPermissions()) {
            serializer.startTag(null, TAG_PERM);
            serializer.attribute(null, ATTR_NAME, permission);
            if (ArrayUtils.contains(ephemeralApp.getGrantedPermissions(), permission)) {
                serializer.attribute(null, ATTR_GRANTED, String.valueOf(true));
            }
            serializer.endTag(null, TAG_PERM);
        }
        serializer.endTag(null, TAG_PERMS);
        serializer.endTag(null, TAG_PACKAGE);
        serializer.endDocument();
        destination.finishWrite(out);
    } catch (Throwable t) {
        Slog.wtf(LOG_TAG, "Failed to write ephemeral state, restoring backup", t);
        destination.failWrite(out);
    } finally {
        IoUtils.closeQuietly(out);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) AtomicFile(android.util.AtomicFile) File(java.io.File) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 50 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project android_frameworks_base by ResurrectionRemix.

the class Settings method writeLPr.

void writeLPr() {
    // been successfully written.
    if (mSettingsFilename.exists()) {
        // might have been corrupted.
        if (!mBackupSettingsFilename.exists()) {
            if (!mSettingsFilename.renameTo(mBackupSettingsFilename)) {
                Slog.wtf(PackageManagerService.TAG, "Unable to backup package manager settings, " + " current changes will be lost at reboot");
                return;
            }
        } else {
            mSettingsFilename.delete();
            Slog.w(PackageManagerService.TAG, "Preserving older settings backup");
        }
    }
    mPastSignatures.clear();
    try {
        FileOutputStream fstr = new FileOutputStream(mSettingsFilename);
        BufferedOutputStream str = new BufferedOutputStream(fstr);
        //XmlSerializer serializer = XmlUtils.serializerInstance();
        XmlSerializer serializer = new FastXmlSerializer();
        serializer.setOutput(str, StandardCharsets.UTF_8.name());
        serializer.startDocument(null, true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, "packages");
        for (int i = 0; i < mVersion.size(); i++) {
            final String volumeUuid = mVersion.keyAt(i);
            final VersionInfo ver = mVersion.valueAt(i);
            serializer.startTag(null, TAG_VERSION);
            XmlUtils.writeStringAttribute(serializer, ATTR_VOLUME_UUID, volumeUuid);
            XmlUtils.writeIntAttribute(serializer, ATTR_SDK_VERSION, ver.sdkVersion);
            XmlUtils.writeIntAttribute(serializer, ATTR_DATABASE_VERSION, ver.databaseVersion);
            XmlUtils.writeStringAttribute(serializer, ATTR_FINGERPRINT, ver.fingerprint);
            serializer.endTag(null, TAG_VERSION);
        }
        if (mVerifierDeviceIdentity != null) {
            serializer.startTag(null, "verifier");
            serializer.attribute(null, "device", mVerifierDeviceIdentity.toString());
            serializer.endTag(null, "verifier");
        }
        if (mReadExternalStorageEnforced != null) {
            serializer.startTag(null, TAG_READ_EXTERNAL_STORAGE);
            serializer.attribute(null, ATTR_ENFORCEMENT, mReadExternalStorageEnforced ? "1" : "0");
            serializer.endTag(null, TAG_READ_EXTERNAL_STORAGE);
        }
        serializer.startTag(null, "permission-trees");
        for (BasePermission bp : mPermissionTrees.values()) {
            writePermissionLPr(serializer, bp);
        }
        serializer.endTag(null, "permission-trees");
        serializer.startTag(null, "permissions");
        for (BasePermission bp : mPermissions.values()) {
            writePermissionLPr(serializer, bp);
        }
        serializer.endTag(null, "permissions");
        for (final PackageSetting pkg : mPackages.values()) {
            writePackageLPr(serializer, pkg);
        }
        for (final PackageSetting pkg : mDisabledSysPackages.values()) {
            writeDisabledSysPackageLPr(serializer, pkg);
        }
        for (final SharedUserSetting usr : mSharedUsers.values()) {
            serializer.startTag(null, "shared-user");
            serializer.attribute(null, ATTR_NAME, usr.name);
            serializer.attribute(null, "userId", Integer.toString(usr.userId));
            usr.signatures.writeXml(serializer, "sigs", mPastSignatures);
            writePermissionsLPr(serializer, usr.getPermissionsState().getInstallPermissionStates());
            serializer.endTag(null, "shared-user");
        }
        if (mPackagesToBeCleaned.size() > 0) {
            for (PackageCleanItem item : mPackagesToBeCleaned) {
                final String userStr = Integer.toString(item.userId);
                serializer.startTag(null, "cleaning-package");
                serializer.attribute(null, ATTR_NAME, item.packageName);
                serializer.attribute(null, ATTR_CODE, item.andCode ? "true" : "false");
                serializer.attribute(null, ATTR_USER, userStr);
                serializer.endTag(null, "cleaning-package");
            }
        }
        if (mRenamedPackages.size() > 0) {
            for (Map.Entry<String, String> e : mRenamedPackages.entrySet()) {
                serializer.startTag(null, "renamed-package");
                serializer.attribute(null, "new", e.getKey());
                serializer.attribute(null, "old", e.getValue());
                serializer.endTag(null, "renamed-package");
            }
        }
        final int numIVIs = mRestoredIntentFilterVerifications.size();
        if (numIVIs > 0) {
            if (DEBUG_DOMAIN_VERIFICATION) {
                Slog.i(TAG, "Writing restored-ivi entries to packages.xml");
            }
            serializer.startTag(null, "restored-ivi");
            for (int i = 0; i < numIVIs; i++) {
                IntentFilterVerificationInfo ivi = mRestoredIntentFilterVerifications.valueAt(i);
                writeDomainVerificationsLPr(serializer, ivi);
            }
            serializer.endTag(null, "restored-ivi");
        } else {
            if (DEBUG_DOMAIN_VERIFICATION) {
                Slog.i(TAG, "  no restored IVI entries to write");
            }
        }
        mKeySetManagerService.writeKeySetManagerServiceLPr(serializer);
        serializer.endTag(null, "packages");
        serializer.endDocument();
        str.flush();
        FileUtils.sync(fstr);
        str.close();
        // New settings successfully written, old ones are no longer
        // needed.
        mBackupSettingsFilename.delete();
        FileUtils.setPermissions(mSettingsFilename.toString(), FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP | FileUtils.S_IWGRP, -1, -1);
        writeKernelMappingLPr();
        writePackageListLPr();
        writeAllUsersPackageRestrictionsLPr();
        writeAllRuntimePermissionsLPr();
        return;
    } catch (XmlPullParserException e) {
        Slog.wtf(PackageManagerService.TAG, "Unable to write package manager settings, " + "current changes will be lost at reboot", e);
    } catch (java.io.IOException e) {
        Slog.wtf(PackageManagerService.TAG, "Unable to write package manager settings, " + "current changes will be lost at reboot", e);
    }
    // Clean up partially written files
    if (mSettingsFilename.exists()) {
        if (!mSettingsFilename.delete()) {
            Slog.wtf(PackageManagerService.TAG, "Failed to clean up mangled file: " + mSettingsFilename);
        }
    }
//Debug.stopMethodTracing();
}
Also used : IOException(java.io.IOException) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) PackageCleanItem(android.content.pm.PackageCleanItem) FileOutputStream(java.io.FileOutputStream) IntentFilterVerificationInfo(android.content.pm.IntentFilterVerificationInfo) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) BufferedOutputStream(java.io.BufferedOutputStream) Map(java.util.Map) ArrayMap(android.util.ArrayMap) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Aggregations

XmlSerializer (org.xmlpull.v1.XmlSerializer)268 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)156 IOException (java.io.IOException)151 FileOutputStream (java.io.FileOutputStream)144 BufferedOutputStream (java.io.BufferedOutputStream)57 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 AtomicFile (android.util.AtomicFile)39 RemoteException (android.os.RemoteException)29 File (java.io.File)26 FileNotFoundException (java.io.FileNotFoundException)26 StringWriter (java.io.StringWriter)26 Map (java.util.Map)23 ErrnoException (android.system.ErrnoException)20 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)15 JournaledFile (com.android.internal.util.JournaledFile)15 UserInfo (android.content.pm.UserInfo)9 HashMap (java.util.HashMap)9 SendIntentException (android.content.IntentSender.SendIntentException)8 PackageParserException (android.content.pm.PackageParser.PackageParserException)8