Moved export to file code from Accounts activity into StorageExporter

This commit is contained in:
cketti 2011-03-28 08:04:43 +02:00
parent f5c153b405
commit 6510919eee
2 changed files with 52 additions and 64 deletions

View File

@ -1,7 +1,6 @@
package com.fsck.k9.activity;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
@ -21,7 +20,6 @@ import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.util.TypedValue;
@ -60,12 +58,12 @@ import com.fsck.k9.activity.setup.Prefs;
import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.controller.MessagingListener;
import com.fsck.k9.helper.SizeFormatter;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.internet.MimeUtility;
import com.fsck.k9.mail.store.StorageManager;
import com.fsck.k9.view.ColorChip;
import com.fsck.k9.preferences.StorageExporter;
import com.fsck.k9.preferences.StorageImportExportException;
public class Accounts extends K9ListActivity implements OnItemClickListener, OnClickListener {
@ -1164,17 +1162,9 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
@Override
protected Boolean doInBackground(Void... params) {
try {
//FIXME: this code doesn't belong here!
// Do not store with application files. Settings exports should *not* be
// deleted when the application is uninstalled.
File dir = new File(Environment.getExternalStorageDirectory() + File.separator
+ Accounts.this.getApplication().getPackageName());
dir.mkdirs();
File file = Utility.createUniqueFile(dir, "settings.k9s");
mFileName = file.getAbsolutePath();
StorageExporter.exportPreferences(Accounts.this, mIncludeGlobals,
mAccountUuids, mFileName, mEncryptionKey);
} catch (Exception e) {
mFileName = StorageExporter.exportToFile(Accounts.this, mIncludeGlobals,
mAccountUuids, mEncryptionKey);
} catch (StorageImportExportException e) {
Log.w(K9.LOG_TAG, "Exception during export", e);
return false;
}

View File

@ -2,74 +2,72 @@ package com.fsck.k9.preferences;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Set;
import android.app.Activity;
import android.util.Log;
import android.content.Context;
import android.os.Environment;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.K9;
public class StorageExporter {
private static void exportPreferences(Activity activity, boolean includeGlobals, Set<String> accountUuids, String fileName, OutputStream os, String encryptionKey) throws StorageImportExportException {
try {
IStorageExporter storageExporter = new StorageExporterEncryptedXml();
if (storageExporter.needsKey() && encryptionKey == null) {
throw new StorageImportExportException("Encryption key required, but none supplied");
} else {
finishExport(activity, storageExporter, includeGlobals, accountUuids, fileName, os, encryptionKey);
}
}
catch (Exception e) {
//FIXME: get this right
throw new StorageImportExportException();
}
}
private static final String EXPORT_FILENAME = "settings.k9s";
public static void exportPreferences(Activity activity, boolean includeGlobals, Set<String> accountUuids, String fileName, String encryptionKey) throws StorageImportExportException {
exportPreferences(activity, includeGlobals, accountUuids, fileName, null, encryptionKey);
}
private static void finishExport(Activity activity, IStorageExporter storageExporter, boolean includeGlobals, Set<String> accountUuids, String fileName, OutputStream os, String encryptionKey) throws StorageImportExportException {
boolean needToClose = false;
try {
// This needs to be after the password prompt. If the user cancels the password, we do not want
// to create the file needlessly
if (os == null && fileName != null) {
needToClose = true;
File outFile = new File(fileName);
os = new FileOutputStream(outFile);
}
if (os != null) {
public static String exportToFile(Context context, boolean includeGlobals,
Set<String> accountUuids, String encryptionKey)
throws StorageImportExportException {
OutputStreamWriter sw = new OutputStreamWriter(os);
PrintWriter pf = new PrintWriter(sw);
pf.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
OutputStream os = null;
try
{
File dir = new File(Environment.getExternalStorageDirectory() + File.separator
+ context.getPackageName());
dir.mkdirs();
File file = Utility.createUniqueFile(dir, EXPORT_FILENAME);
String fileName = file.getAbsolutePath();
os = new FileOutputStream(fileName);
exportPreferences(context, os, includeGlobals, accountUuids, encryptionKey);
pf.println("<k9settings version=\"" + 1 + "\">");
pf.flush();
storageExporter.exportPreferences(activity, includeGlobals, accountUuids, os, encryptionKey);
pf.println("</k9settings>");
pf.flush();
} else {
throw new StorageImportExportException("Internal error; no fileName or OutputStream", null);
}
// If all went well, we return the name of the file just written.
return fileName;
} catch (Exception e) {
throw new StorageImportExportException(e.getLocalizedMessage(), e);
throw new StorageImportExportException();
} finally {
if (needToClose && os != null) {
if (os != null) {
try {
os.close();
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Unable to close OutputStream", e);
}
} catch (IOException ioe) {}
}
}
}
public static void exportPreferences(Context context, OutputStream os, boolean includeGlobals,
Set<String> accountUuids, String encryptionKey) throws StorageImportExportException {
IStorageExporter storageExporter = new StorageExporterEncryptedXml();
if (storageExporter.needsKey() && encryptionKey == null) {
throw new StorageImportExportException("Encryption key required, but none supplied");
}
try {
OutputStreamWriter sw = new OutputStreamWriter(os);
PrintWriter pf = new PrintWriter(sw);
pf.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
pf.println("<k9settings version=\"" + 1 + "\">");
pf.flush();
storageExporter.exportPreferences(context, includeGlobals, accountUuids, os, encryptionKey);
pf.println("</k9settings>");
pf.flush();
} catch (Exception e) {
throw new StorageImportExportException(e.getLocalizedMessage(), e);
}
}
}