Search in sources :

Example 51 with XmlSerializer

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

the class ShortcutService method saveBaseStateLocked.

@VisibleForTesting
void saveBaseStateLocked() {
    final AtomicFile file = getBaseStateFile();
    if (DEBUG) {
        Slog.d(TAG, "Saving to " + file.getBaseFile());
    }
    FileOutputStream outs = null;
    try {
        outs = file.startWrite();
        // Write to XML
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(outs, StandardCharsets.UTF_8.name());
        out.startDocument(null, true);
        out.startTag(null, TAG_ROOT);
        // Body.
        writeTagValue(out, TAG_LAST_RESET_TIME, mRawLastResetTime);
        // Epilogue.
        out.endTag(null, TAG_ROOT);
        out.endDocument();
        // Close.
        file.finishWrite(outs);
    } catch (IOException e) {
        Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e);
        file.failWrite(outs);
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 52 with XmlSerializer

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

the class UserManagerService method writeUserListLP.

/*
     * Writes the user list file in this format:
     *
     * <users nextSerialNumber="3">
     *   <user id="0"></user>
     *   <user id="2"></user>
     * </users>
     */
private void writeUserListLP() {
    if (DBG) {
        debug("writeUserList");
    }
    FileOutputStream fos = null;
    AtomicFile userListFile = new AtomicFile(mUserListFile);
    try {
        fos = userListFile.startWrite();
        final BufferedOutputStream bos = new BufferedOutputStream(fos);
        // XmlSerializer serializer = XmlUtils.serializerInstance();
        final XmlSerializer serializer = new FastXmlSerializer();
        serializer.setOutput(bos, StandardCharsets.UTF_8.name());
        serializer.startDocument(null, true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, TAG_USERS);
        serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
        serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
        serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
        synchronized (mGuestRestrictions) {
            UserRestrictionsUtils.writeRestrictions(serializer, mGuestRestrictions, TAG_RESTRICTIONS);
        }
        serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
        synchronized (mRestrictionsLock) {
            UserRestrictionsUtils.writeRestrictions(serializer, mDevicePolicyGlobalUserRestrictions, TAG_DEVICE_POLICY_RESTRICTIONS);
        }
        serializer.startTag(null, TAG_GLOBAL_RESTRICTION_OWNER_ID);
        serializer.attribute(null, ATTR_ID, Integer.toString(mGlobalRestrictionOwnerUserId));
        serializer.endTag(null, TAG_GLOBAL_RESTRICTION_OWNER_ID);
        int[] userIdsToWrite;
        synchronized (mUsersLock) {
            userIdsToWrite = new int[mUsers.size()];
            for (int i = 0; i < userIdsToWrite.length; i++) {
                UserInfo user = mUsers.valueAt(i).info;
                userIdsToWrite[i] = user.id;
            }
        }
        for (int id : userIdsToWrite) {
            serializer.startTag(null, TAG_USER);
            serializer.attribute(null, ATTR_ID, Integer.toString(id));
            serializer.endTag(null, TAG_USER);
        }
        serializer.endTag(null, TAG_USERS);
        serializer.endDocument();
        userListFile.finishWrite(fos);
    } catch (Exception e) {
        userListFile.failWrite(fos);
        Slog.e(LOG_TAG, "Error writing user list");
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) UserInfo(android.content.pm.UserInfo) BufferedOutputStream(java.io.BufferedOutputStream) ErrnoException(android.system.ErrnoException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 53 with XmlSerializer

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

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);
                broadcastChangesIfNeeded();
            } else {
                mAtomicFile.failWrite(os);
            }
        }
    } catch (IOException ex) {
        Slog.w(TAG, "Failed to save tv 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 54 with XmlSerializer

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

the class WallpaperManagerService method saveSettingsLocked.

private void saveSettingsLocked(int userId) {
    JournaledFile journal = makeJournaledFile(userId);
    FileOutputStream fstream = null;
    BufferedOutputStream stream = null;
    try {
        XmlSerializer out = new FastXmlSerializer();
        fstream = new FileOutputStream(journal.chooseForWrite(), false);
        stream = new BufferedOutputStream(fstream);
        out.setOutput(stream, StandardCharsets.UTF_8.name());
        out.startDocument(null, true);
        WallpaperData wallpaper;
        wallpaper = mWallpaperMap.get(userId);
        if (wallpaper != null) {
            writeWallpaperAttributes(out, "wp", wallpaper);
        }
        wallpaper = mLockWallpaperMap.get(userId);
        if (wallpaper != null) {
            writeWallpaperAttributes(out, "kwp", wallpaper);
        }
        out.endDocument();
        // also flushes fstream
        stream.flush();
        FileUtils.sync(fstream);
        // also closes fstream
        stream.close();
        journal.commit();
    } catch (IOException e) {
        IoUtils.closeQuietly(stream);
        journal.rollback();
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) JournaledFile(com.android.internal.util.JournaledFile) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 55 with XmlSerializer

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

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)

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