k-9/src/com/fsck/k9/preferences/StorageExporter.java

124 lines
5.3 KiB
Java
Raw Normal View History

package com.fsck.k9.preferences;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashSet;
import android.app.Activity;
import android.util.Log;
import com.fsck.k9.K9;
import com.fsck.k9.R;
import com.fsck.k9.activity.AsyncUIProcessor;
import com.fsck.k9.activity.ExportListener;
import com.fsck.k9.activity.PasswordEntryDialog;
2011-03-20 16:21:24 -04:00
public class StorageExporter {
private static void exportPreferences(Activity activity, String storageFormat, HashSet<String> accountUuids, String fileName, OutputStream os, String encryptionKey, final ExportListener listener) {
2011-02-26 19:39:06 -05:00
try {
IStorageExporter storageExporter = StorageFormat.createExporter(storageFormat);
if (storageExporter == null) {
throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, storageFormat), null);
}
if (storageExporter.needsKey() && encryptionKey == null) {
gatherPassword(activity, storageExporter, accountUuids, fileName, os, listener);
2011-03-20 16:21:24 -04:00
} else {
finishExport(activity, storageExporter, accountUuids, fileName, os, encryptionKey, listener);
}
}
2011-03-20 16:21:24 -04:00
catch (Exception e) {
if (listener != null) {
listener.failure(e.getLocalizedMessage(), e);
}
}
}
2011-03-20 16:21:24 -04:00
public static void exportPreferences(Activity activity, String storageFormat, HashSet<String> accountUuids, String fileName, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, storageFormat, accountUuids, fileName, null, encryptionKey, listener);
}
2011-03-20 16:21:24 -04:00
public static void exportPrefererences(Activity activity, String storageFormat, HashSet<String> accountUuids, OutputStream os, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, storageFormat, accountUuids, null, os, encryptionKey, listener);
}
2011-03-20 16:21:24 -04:00
private static void gatherPassword(final Activity activity, final IStorageExporter storageExporter, final HashSet<String> accountUuids, final String fileName, final OutputStream os, final ExportListener listener) {
activity.runOnUiThread(new Runnable() {
@Override
2011-03-20 16:21:24 -04:00
public void run() {
2011-03-25 02:15:46 -04:00
PasswordEntryDialog dialog = new PasswordEntryDialog(activity, activity.getString(R.string.settings_export_encryption_password_prompt),
2011-03-20 16:21:24 -04:00
new PasswordEntryDialog.PasswordEntryListener() {
public void passwordChosen(final String chosenPassword) {
AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() {
@Override
public void run() {
try {
finishExport(activity, storageExporter, accountUuids, fileName, os, chosenPassword, listener);
2011-03-20 16:21:24 -04:00
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception while finishing export", e);
if (listener != null) {
listener.failure(e.getLocalizedMessage(), e);
}
}
}
2011-03-20 16:21:24 -04:00
});
}
public void cancel() {
if (listener != null) {
listener.canceled();
}
2011-03-20 16:21:24 -04:00
}
});
dialog.show();
2011-02-26 19:39:06 -05:00
}
});
}
2011-03-20 16:21:24 -04:00
private static void finishExport(Activity activity, IStorageExporter storageExporter, HashSet<String> accountUuids, String fileName, OutputStream os, String encryptionKey, ExportListener listener) throws StorageImportExportException {
boolean needToClose = false;
if (listener != null) {
listener.started();
}
try {
2011-03-20 16:21:24 -04:00
// 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) {
storageExporter.exportPreferences(activity, accountUuids, os, encryptionKey);
if (listener != null) {
if (fileName != null) {
listener.success(fileName);
2011-03-20 16:21:24 -04:00
} else {
listener.success();
2011-02-26 19:39:06 -05:00
}
}
2011-03-20 16:21:24 -04:00
} else {
throw new StorageImportExportException("Internal error; no fileName or OutputStream", null);
}
2011-03-20 16:21:24 -04:00
} catch (Exception e) {
throw new StorageImportExportException(e.getLocalizedMessage(), e);
2011-03-20 16:21:24 -04:00
} finally {
if (needToClose && os != null) {
try {
os.close();
2011-03-20 16:21:24 -04:00
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Unable to close OutputStream", e);
}
}
}
2011-03-20 16:21:24 -04:00
}
2011-03-20 16:21:24 -04:00
}