From e5ef068c1535712b32c5c05cf67d1677537cfd18 Mon Sep 17 00:00:00 2001 From: danapple Date: Sun, 27 Mar 2011 11:37:43 -0500 Subject: [PATCH] Move the generation of the XML envelope to a common place since it would be used in all storageFormats. StorageImporter uses the header to figure out which specific import implementation to use. --- .../fsck/k9/preferences/IStorageExporter.java | 2 +- .../fsck/k9/preferences/StorageExporter.java | 23 +++++++++++++++---- .../StorageExporterEncryptedXml.java | 7 +----- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/com/fsck/k9/preferences/IStorageExporter.java b/src/com/fsck/k9/preferences/IStorageExporter.java index 095db3f36..76bdce369 100644 --- a/src/com/fsck/k9/preferences/IStorageExporter.java +++ b/src/com/fsck/k9/preferences/IStorageExporter.java @@ -1,12 +1,12 @@ package com.fsck.k9.preferences; import java.io.OutputStream; -import java.util.HashSet; import java.util.Set; import android.content.Context; public interface IStorageExporter { public boolean needsKey(); + // exportPreferences must be sure to flush all data to the OutputStream before returning public void exportPreferences(Context context, boolean includeGlobals, Set accountUuids, OutputStream os, String encryptionKey) throws StorageImportExportException; } diff --git a/src/com/fsck/k9/preferences/StorageExporter.java b/src/com/fsck/k9/preferences/StorageExporter.java index 0419cc862..50a918e68 100644 --- a/src/com/fsck/k9/preferences/StorageExporter.java +++ b/src/com/fsck/k9/preferences/StorageExporter.java @@ -3,6 +3,8 @@ package com.fsck.k9.preferences; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; import java.util.Set; import android.app.Activity; @@ -22,9 +24,9 @@ public class StorageExporter { throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, storageFormat), null); } if (storageExporter.needsKey() && encryptionKey == null) { - gatherPassword(activity, storageExporter, includeGlobals, accountUuids, fileName, os, listener); + gatherPassword(activity, storageFormat, storageExporter, includeGlobals, accountUuids, fileName, os, listener); } else { - finishExport(activity, storageExporter, includeGlobals, accountUuids, fileName, os, encryptionKey, listener); + finishExport(activity, storageFormat, storageExporter, includeGlobals, accountUuids, fileName, os, encryptionKey, listener); } } @@ -43,7 +45,7 @@ public class StorageExporter { exportPreferences(activity, storageFormat, includeGlobals, accountUuids, null, os, encryptionKey, listener); } - private static void gatherPassword(final Activity activity, final IStorageExporter storageExporter, final boolean includeGlobals, final Set accountUuids, final String fileName, final OutputStream os, final ExportListener listener) { + private static void gatherPassword(final Activity activity, final String storageFormat, final IStorageExporter storageExporter, final boolean includeGlobals, final Set accountUuids, final String fileName, final OutputStream os, final ExportListener listener) { activity.runOnUiThread(new Runnable() { @Override @@ -57,7 +59,7 @@ public class StorageExporter { @Override public void run() { try { - finishExport(activity, storageExporter, includeGlobals, accountUuids, fileName, os, chosenPassword, listener); + finishExport(activity, storageFormat, storageExporter, includeGlobals, accountUuids, fileName, os, chosenPassword, listener); } catch (Exception e) { Log.w(K9.LOG_TAG, "Exception while finishing export", e); if (listener != null) { @@ -81,7 +83,7 @@ public class StorageExporter { } - private static void finishExport(Activity activity, IStorageExporter storageExporter, boolean includeGlobals, Set accountUuids, String fileName, OutputStream os, String encryptionKey, ExportListener listener) throws StorageImportExportException { + private static void finishExport(Activity activity, String storageFormat, IStorageExporter storageExporter, boolean includeGlobals, Set accountUuids, String fileName, OutputStream os, String encryptionKey, ExportListener listener) throws StorageImportExportException { boolean needToClose = false; if (listener != null) { listener.started(); @@ -95,7 +97,18 @@ public class StorageExporter { os = new FileOutputStream(outFile); } if (os != null) { + + OutputStreamWriter sw = new OutputStreamWriter(os); + PrintWriter pf = new PrintWriter(sw); + pf.println(""); + + pf.println(""); + pf.flush(); + storageExporter.exportPreferences(activity, includeGlobals, accountUuids, os, encryptionKey); + + pf.println(""); + pf.flush(); if (listener != null) { if (fileName != null) { listener.success(fileName); diff --git a/src/com/fsck/k9/preferences/StorageExporterEncryptedXml.java b/src/com/fsck/k9/preferences/StorageExporterEncryptedXml.java index b406834fa..8aa48da86 100644 --- a/src/com/fsck/k9/preferences/StorageExporterEncryptedXml.java +++ b/src/com/fsck/k9/preferences/StorageExporterEncryptedXml.java @@ -24,11 +24,7 @@ public class StorageExporterEncryptedXml implements IStorageExporter { PrintWriter pf = new PrintWriter(sw); long keysEvaluated = 0; long keysExported = 0; - pf.println(""); - - pf.print(""); - + Preferences preferences = Preferences.getPreferences(context); SharedPreferences storage = preferences.getPreferences(); @@ -66,7 +62,6 @@ public class StorageExporterEncryptedXml implements IStorageExporter { } - pf.println(""); pf.flush(); Log.i(K9.LOG_TAG, "Exported " + keysExported + " of " + keysEvaluated + " settings.");