Search in sources :

Example 1 with XmlSerializer

use of org.xmlpull.v1.XmlSerializer in project k-9 by k9mail.

the class SettingsExporter method exportPreferences.

static void exportPreferences(Context context, OutputStream os, boolean includeGlobals, Set<String> accountUuids) throws SettingsImportExportException {
    try {
        XmlSerializer serializer = Xml.newSerializer();
        serializer.setOutput(os, "UTF-8");
        serializer.startDocument(null, Boolean.TRUE);
        // Output with indentation
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, ROOT_ELEMENT);
        serializer.attribute(null, VERSION_ATTRIBUTE, Integer.toString(Settings.VERSION));
        serializer.attribute(null, FILE_FORMAT_ATTRIBUTE, Integer.toString(FILE_FORMAT_VERSION));
        Timber.i("Exporting preferences");
        Preferences preferences = Preferences.getPreferences(context);
        Storage storage = preferences.getStorage();
        Set<String> exportAccounts;
        if (accountUuids == null) {
            List<Account> accounts = preferences.getAccounts();
            exportAccounts = new HashSet<>();
            for (Account account : accounts) {
                exportAccounts.add(account.getUuid());
            }
        } else {
            exportAccounts = accountUuids;
        }
        Map<String, Object> prefs = new TreeMap<String, Object>(storage.getAll());
        if (includeGlobals) {
            serializer.startTag(null, GLOBAL_ELEMENT);
            writeSettings(serializer, prefs);
            serializer.endTag(null, GLOBAL_ELEMENT);
        }
        serializer.startTag(null, ACCOUNTS_ELEMENT);
        for (String accountUuid : exportAccounts) {
            Account account = preferences.getAccount(accountUuid);
            writeAccount(serializer, account, prefs);
        }
        serializer.endTag(null, ACCOUNTS_ELEMENT);
        serializer.endTag(null, ROOT_ELEMENT);
        serializer.endDocument();
        serializer.flush();
    } catch (Exception e) {
        throw new SettingsImportExportException(e.getLocalizedMessage(), e);
    }
}
Also used : Account(com.fsck.k9.Account) TreeMap(java.util.TreeMap) InvalidSettingValueException(com.fsck.k9.preferences.Settings.InvalidSettingValueException) IOException(java.io.IOException) Preferences(com.fsck.k9.Preferences) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 2 with XmlSerializer

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

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, "utf-8");
            out.startDocument(null, true);
            out.startTag(null, "app-ops");
            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()));
                    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.MODE_ALLOWED) {
                            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));
                        }
                        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) FileOutputStream(java.io.FileOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 3 with XmlSerializer

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

the class AppWidgetServiceImpl method writeStateToFileLocked.

boolean writeStateToFileLocked(FileOutputStream stream) {
    int N;
    try {
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(stream, "utf-8");
        out.startDocument(null, true);
        out.startTag(null, "gs");
        int providerIndex = 0;
        N = mInstalledProviders.size();
        for (int i = 0; i < N; i++) {
            Provider p = mInstalledProviders.get(i);
            if (p.instances.size() > 0) {
                out.startTag(null, "p");
                out.attribute(null, "pkg", p.info.provider.getPackageName());
                out.attribute(null, "cl", p.info.provider.getClassName());
                out.endTag(null, "p");
                p.tag = providerIndex;
                providerIndex++;
            }
        }
        N = mHosts.size();
        for (int i = 0; i < N; i++) {
            Host host = mHosts.get(i);
            out.startTag(null, "h");
            out.attribute(null, "pkg", host.packageName);
            out.attribute(null, "id", Integer.toHexString(host.hostId));
            out.endTag(null, "h");
            host.tag = i;
        }
        N = mAppWidgetIds.size();
        for (int i = 0; i < N; i++) {
            AppWidgetId id = mAppWidgetIds.get(i);
            out.startTag(null, "g");
            out.attribute(null, "id", Integer.toHexString(id.appWidgetId));
            out.attribute(null, "h", Integer.toHexString(id.host.tag));
            if (id.provider != null) {
                out.attribute(null, "p", Integer.toHexString(id.provider.tag));
            }
            if (id.options != null) {
                out.attribute(null, "min_width", Integer.toHexString(id.options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)));
                out.attribute(null, "min_height", Integer.toHexString(id.options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)));
                out.attribute(null, "max_width", Integer.toHexString(id.options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)));
                out.attribute(null, "max_height", Integer.toHexString(id.options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)));
                out.attribute(null, "host_category", Integer.toHexString(id.options.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY)));
            }
            out.endTag(null, "g");
        }
        Iterator<String> it = mPackagesWithBindWidgetPermission.iterator();
        while (it.hasNext()) {
            out.startTag(null, "b");
            out.attribute(null, "packageName", it.next());
            out.endTag(null, "b");
        }
        out.endTag(null, "gs");
        out.endDocument();
        return true;
    } catch (IOException e) {
        Slog.w(TAG, "Failed to write state: " + e);
        return false;
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) IAppWidgetHost(com.android.internal.appwidget.IAppWidgetHost) IOException(java.io.IOException) Point(android.graphics.Point) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 4 with XmlSerializer

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

the class WallpaperManagerService method saveSettingsLocked.

private void saveSettingsLocked(WallpaperData wallpaper) {
    JournaledFile journal = makeJournaledFile(wallpaper.userId);
    FileOutputStream stream = null;
    try {
        stream = new FileOutputStream(journal.chooseForWrite(), false);
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(stream, "utf-8");
        out.startDocument(null, true);
        out.startTag(null, "wp");
        out.attribute(null, "width", Integer.toString(wallpaper.width));
        out.attribute(null, "height", Integer.toString(wallpaper.height));
        out.attribute(null, "name", wallpaper.name);
        if (wallpaper.wallpaperComponent != null && !wallpaper.wallpaperComponent.equals(IMAGE_WALLPAPER)) {
            out.attribute(null, "component", wallpaper.wallpaperComponent.flattenToShortString());
        }
        out.endTag(null, "wp");
        out.endDocument();
        stream.close();
        journal.commit();
    } catch (IOException e) {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException ex) {
        // Ignore
        }
        journal.rollback();
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) JournaledFile(com.android.internal.util.JournaledFile) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 5 with XmlSerializer

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

the class CompatModePackages method saveCompatModes.

void saveCompatModes() {
    HashMap<String, Integer> pkgs;
    synchronized (mService) {
        pkgs = new HashMap<String, Integer>(mPackages);
    }
    FileOutputStream fos = null;
    try {
        fos = mFile.startWrite();
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(fos, "utf-8");
        out.startDocument(null, true);
        out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        out.startTag(null, "compat-packages");
        final IPackageManager pm = AppGlobals.getPackageManager();
        final int screenLayout = mService.mConfiguration.screenLayout;
        final int smallestScreenWidthDp = mService.mConfiguration.smallestScreenWidthDp;
        final Iterator<Map.Entry<String, Integer>> it = pkgs.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Integer> entry = it.next();
            String pkg = entry.getKey();
            int mode = entry.getValue();
            if (mode == 0) {
                continue;
            }
            ApplicationInfo ai = null;
            try {
                ai = pm.getApplicationInfo(pkg, 0, 0);
            } catch (RemoteException e) {
            }
            if (ai == null) {
                continue;
            }
            CompatibilityInfo info = new CompatibilityInfo(ai, screenLayout, smallestScreenWidthDp, false);
            if (info.alwaysSupportsScreen()) {
                continue;
            }
            if (info.neverSupportsScreen()) {
                continue;
            }
            out.startTag(null, "pkg");
            out.attribute(null, "name", pkg);
            out.attribute(null, "mode", Integer.toString(mode));
            out.endTag(null, "pkg");
        }
        out.endTag(null, "compat-packages");
        out.endDocument();
        mFile.finishWrite(fos);
    } catch (java.io.IOException e1) {
        Slog.w(TAG, "Error writing compat packages", e1);
        if (fos != null) {
            mFile.failWrite(fos);
        }
    }
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) CompatibilityInfo(android.content.res.CompatibilityInfo) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) IPackageManager(android.content.pm.IPackageManager) FileOutputStream(java.io.FileOutputStream) RemoteException(android.os.RemoteException) HashMap(java.util.HashMap) Map(java.util.Map) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Aggregations

XmlSerializer (org.xmlpull.v1.XmlSerializer)307 IOException (java.io.IOException)171 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)157 FileOutputStream (java.io.FileOutputStream)156 BufferedOutputStream (java.io.BufferedOutputStream)61 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)56 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 AtomicFile (android.util.AtomicFile)39 File (java.io.File)32 FileNotFoundException (java.io.FileNotFoundException)32 StringWriter (java.io.StringWriter)30 RemoteException (android.os.RemoteException)29 Map (java.util.Map)26 ErrnoException (android.system.ErrnoException)20 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)15 JournaledFile (com.android.internal.util.JournaledFile)15 HashMap (java.util.HashMap)14 FileWriter (java.io.FileWriter)12 UserInfo (android.content.pm.UserInfo)9 SendIntentException (android.content.IntentSender.SendIntentException)8