Search in sources :

Example 56 with FastXmlSerializer

use of com.android.internal.util.FastXmlSerializer in project android_frameworks_base by DirtyUnicorns.

the class AccountManagerBackupHelper method backupAccountAccessPermissions.

public byte[] backupAccountAccessPermissions(int userId) {
    final AccountManagerService.UserAccounts accounts = mAccountManagerService.getUserAccounts(userId);
    synchronized (accounts.cacheLock) {
        SQLiteDatabase db = accounts.openHelper.getReadableDatabase();
        try (Cursor cursor = db.rawQuery(ACCOUNT_ACCESS_GRANTS, null)) {
            if (cursor == null || !cursor.moveToFirst()) {
                return null;
            }
            final int nameColumnIdx = cursor.getColumnIndex(AccountManagerService.ACCOUNTS_NAME);
            final int uidColumnIdx = cursor.getColumnIndex(AccountManagerService.GRANTS_GRANTEE_UID);
            ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            try {
                final XmlSerializer serializer = new FastXmlSerializer();
                serializer.setOutput(dataStream, StandardCharsets.UTF_8.name());
                serializer.startDocument(null, true);
                serializer.startTag(null, TAG_PERMISSIONS);
                PackageManager packageManager = mAccountManagerService.mContext.getPackageManager();
                do {
                    final String accountName = cursor.getString(nameColumnIdx);
                    final int uid = cursor.getInt(uidColumnIdx);
                    final String[] packageNames = packageManager.getPackagesForUid(uid);
                    if (packageNames == null) {
                        continue;
                    }
                    for (String packageName : packageNames) {
                        String digest = PackageUtils.computePackageCertSha256Digest(packageManager, packageName, userId);
                        if (digest != null) {
                            serializer.startTag(null, TAG_PERMISSION);
                            serializer.attribute(null, ATTR_ACCOUNT_SHA_256, PackageUtils.computeSha256Digest(accountName.getBytes()));
                            serializer.attribute(null, ATTR_PACKAGE, packageName);
                            serializer.attribute(null, ATTR_DIGEST, digest);
                            serializer.endTag(null, TAG_PERMISSION);
                        }
                    }
                } while (cursor.moveToNext());
                serializer.endTag(null, TAG_PERMISSIONS);
                serializer.endDocument();
                serializer.flush();
            } catch (IOException e) {
                Log.e(TAG, "Error backing up account access grants", e);
                return null;
            }
            return dataStream.toByteArray();
        }
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) PackageManager(android.content.pm.PackageManager) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Cursor(android.database.Cursor) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 57 with FastXmlSerializer

use of com.android.internal.util.FastXmlSerializer in project android_frameworks_base by DirtyUnicorns.

the class ActivityManagerService method writeGrantedUriPermissions.

private void writeGrantedUriPermissions() {
    if (DEBUG_URI_PERMISSION)
        Slog.v(TAG_URI_PERMISSION, "writeGrantedUriPermissions()");
    // Snapshot permissions so we can persist without lock
    ArrayList<UriPermission.Snapshot> persist = Lists.newArrayList();
    synchronized (this) {
        final int size = mGrantedUriPermissions.size();
        for (int i = 0; i < size; i++) {
            final ArrayMap<GrantUri, UriPermission> perms = mGrantedUriPermissions.valueAt(i);
            for (UriPermission perm : perms.values()) {
                if (perm.persistedModeFlags != 0) {
                    persist.add(perm.snapshot());
                }
            }
        }
    }
    FileOutputStream fos = null;
    try {
        fos = mGrantFile.startWrite();
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(fos, StandardCharsets.UTF_8.name());
        out.startDocument(null, true);
        out.startTag(null, TAG_URI_GRANTS);
        for (UriPermission.Snapshot perm : persist) {
            out.startTag(null, TAG_URI_GRANT);
            writeIntAttribute(out, ATTR_SOURCE_USER_ID, perm.uri.sourceUserId);
            writeIntAttribute(out, ATTR_TARGET_USER_ID, perm.targetUserId);
            out.attribute(null, ATTR_SOURCE_PKG, perm.sourcePkg);
            out.attribute(null, ATTR_TARGET_PKG, perm.targetPkg);
            out.attribute(null, ATTR_URI, String.valueOf(perm.uri.uri));
            writeBooleanAttribute(out, ATTR_PREFIX, perm.uri.prefix);
            writeIntAttribute(out, ATTR_MODE_FLAGS, perm.persistedModeFlags);
            writeLongAttribute(out, ATTR_CREATED_TIME, perm.persistedCreateTime);
            out.endTag(null, TAG_URI_GRANT);
        }
        out.endTag(null, TAG_URI_GRANTS);
        out.endDocument();
        mGrantFile.finishWrite(fos);
    } catch (IOException e) {
        if (fos != null) {
            mGrantFile.failWrite(fos);
        }
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Point(android.graphics.Point) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 58 with FastXmlSerializer

use of com.android.internal.util.FastXmlSerializer in project android_frameworks_base by AOSPA.

the class PersistentDataStore method save.

private void save() {
    final FileOutputStream os;
    try {
        os = mAtomicFile.startWrite();
        boolean success = false;
        try {
            XmlSerializer serializer = new FastXmlSerializer();
            serializer.setOutput(new BufferedOutputStream(os), StandardCharsets.UTF_8.name());
            saveToXml(serializer);
            serializer.flush();
            success = true;
        } finally {
            if (success) {
                mAtomicFile.finishWrite(os);
            } else {
                mAtomicFile.failWrite(os);
            }
        }
    } catch (IOException ex) {
        Slog.w(InputManagerService.TAG, "Failed to save input manager persistent store data.", ex);
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 59 with FastXmlSerializer

use of com.android.internal.util.FastXmlSerializer in project android_frameworks_base by AOSPA.

the class AppOpsService method writeState.

void writeState() {
    synchronized (mFile) {
        List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
        FileOutputStream stream;
        try {
            stream = mFile.startWrite();
        } catch (IOException e) {
            Slog.w(TAG, "Failed to write state: " + e);
            return;
        }
        try {
            XmlSerializer out = new FastXmlSerializer();
            out.setOutput(stream, StandardCharsets.UTF_8.name());
            out.startDocument(null, true);
            out.startTag(null, "app-ops");
            final int uidStateCount = mUidStates.size();
            for (int i = 0; i < uidStateCount; i++) {
                UidState uidState = mUidStates.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()));
                        if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
                            out.attribute(null, "m", Integer.toString(op.getMode()));
                        }
                        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);
                        }
                        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 60 with FastXmlSerializer

use of com.android.internal.util.FastXmlSerializer in project android_frameworks_base by AOSPA.

the class MountService method writeSettingsLocked.

private void writeSettingsLocked() {
    FileOutputStream fos = null;
    try {
        fos = mSettingsFile.startWrite();
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(fos, StandardCharsets.UTF_8.name());
        out.startDocument(null, true);
        out.startTag(null, TAG_VOLUMES);
        writeIntAttribute(out, ATTR_VERSION, VERSION_FIX_PRIMARY);
        writeStringAttribute(out, ATTR_PRIMARY_STORAGE_UUID, mPrimaryStorageUuid);
        writeBooleanAttribute(out, ATTR_FORCE_ADOPTABLE, mForceAdoptable);
        final int size = mRecords.size();
        for (int i = 0; i < size; i++) {
            final VolumeRecord rec = mRecords.valueAt(i);
            writeVolumeRecord(out, rec);
        }
        out.endTag(null, TAG_VOLUMES);
        out.endDocument();
        mSettingsFile.finishWrite(fos);
    } catch (IOException e) {
        if (fos != null) {
            mSettingsFile.failWrite(fos);
        }
    }
}
Also used : VolumeRecord(android.os.storage.VolumeRecord) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Aggregations

FastXmlSerializer (com.android.internal.util.FastXmlSerializer)169 XmlSerializer (org.xmlpull.v1.XmlSerializer)156 IOException (java.io.IOException)138 FileOutputStream (java.io.FileOutputStream)132 BufferedOutputStream (java.io.BufferedOutputStream)56 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)35 RemoteException (android.os.RemoteException)29 AtomicFile (android.util.AtomicFile)28 FileNotFoundException (java.io.FileNotFoundException)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 Map (java.util.Map)22 ErrnoException (android.system.ErrnoException)20 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)15 JournaledFile (com.android.internal.util.JournaledFile)15 File (java.io.File)13 HashMap (java.util.HashMap)11 UserInfo (android.content.pm.UserInfo)9 SendIntentException (android.content.IntentSender.SendIntentException)8 PackageParserException (android.content.pm.PackageParser.PackageParserException)8 Point (android.graphics.Point)8