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.
This commit is contained in:
danapple 2011-03-27 11:37:43 -05:00
parent 0bded12843
commit e5ef068c15
3 changed files with 20 additions and 12 deletions

View file

@ -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<String> accountUuids, OutputStream os, String encryptionKey) throws StorageImportExportException;
}

View file

@ -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<String> 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<String> 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<String> accountUuids, String fileName, OutputStream os, String encryptionKey, ExportListener listener) throws StorageImportExportException {
private static void finishExport(Activity activity, String storageFormat, IStorageExporter storageExporter, boolean includeGlobals, Set<String> 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("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
pf.println("<k9settings version=\"" + storageFormat + "\">");
pf.flush();
storageExporter.exportPreferences(activity, includeGlobals, accountUuids, os, encryptionKey);
pf.println("</k9settings>");
pf.flush();
if (listener != null) {
if (fileName != null) {
listener.success(fileName);

View file

@ -24,11 +24,7 @@ public class StorageExporterEncryptedXml implements IStorageExporter {
PrintWriter pf = new PrintWriter(sw);
long keysEvaluated = 0;
long keysExported = 0;
pf.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
pf.print("<k9settings version=\"1\"");
pf.println(">");
Preferences preferences = Preferences.getPreferences(context);
SharedPreferences storage = preferences.getPreferences();
@ -66,7 +62,6 @@ public class StorageExporterEncryptedXml implements IStorageExporter {
}
pf.println("</k9settings>");
pf.flush();
Log.i(K9.LOG_TAG, "Exported " + keysExported + " of " + keysEvaluated + " settings.");