2011-02-26 12:31:56 -05:00
|
|
|
package com.fsck.k9.preferences;
|
|
|
|
|
|
|
|
import java.io.File;
|
2011-03-03 11:00:58 -05:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.OutputStream;
|
2011-03-25 01:28:14 -04:00
|
|
|
import java.util.HashSet;
|
2011-02-26 12:31:56 -05:00
|
|
|
|
2011-03-20 12:52:13 -04:00
|
|
|
import android.app.Activity;
|
2011-03-03 11:00:58 -05:00
|
|
|
import android.util.Log;
|
2011-02-26 12:31:56 -05:00
|
|
|
|
|
|
|
import com.fsck.k9.K9;
|
2011-03-20 12:52:13 -04:00
|
|
|
import com.fsck.k9.R;
|
|
|
|
import com.fsck.k9.activity.AsyncUIProcessor;
|
|
|
|
import com.fsck.k9.activity.ExportListener;
|
|
|
|
import com.fsck.k9.activity.PasswordEntryDialog;
|
2011-02-26 12:31:56 -05:00
|
|
|
|
2011-03-20 16:21:24 -04:00
|
|
|
public class StorageExporter {
|
2011-03-26 02:19:20 -04:00
|
|
|
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 {
|
2011-03-26 02:19:20 -04:00
|
|
|
IStorageExporter storageExporter = StorageFormat.createExporter(storageFormat);
|
2011-03-20 12:52:13 -04:00
|
|
|
if (storageExporter == null) {
|
2011-03-26 02:19:20 -04:00
|
|
|
throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, storageFormat), null);
|
2011-03-20 12:52:13 -04:00
|
|
|
}
|
|
|
|
if (storageExporter.needsKey() && encryptionKey == null) {
|
2011-03-25 01:28:14 -04:00
|
|
|
gatherPassword(activity, storageExporter, accountUuids, fileName, os, listener);
|
2011-03-20 16:21:24 -04:00
|
|
|
} else {
|
2011-03-25 01:28:14 -04:00
|
|
|
finishExport(activity, storageExporter, accountUuids, fileName, os, encryptionKey, listener);
|
2011-03-20 12:52:13 -04:00
|
|
|
}
|
2011-03-03 11:00:58 -05:00
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
|
|
|
|
catch (Exception e) {
|
2011-03-20 12:52:13 -04:00
|
|
|
if (listener != null) {
|
|
|
|
listener.failure(e.getLocalizedMessage(), e);
|
2011-03-03 11:00:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
|
2011-03-26 02:19:20 -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 12:52:13 -04:00
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
|
2011-03-26 02:19:20 -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 12:52:13 -04:00
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
|
2011-03-25 01:28:14 -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) {
|
2011-03-20 12:52:13 -04:00
|
|
|
activity.runOnUiThread(new Runnable() {
|
2011-03-03 11:00:58 -05:00
|
|
|
|
2011-03-20 12:52:13 -04:00
|
|
|
@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 {
|
2011-03-25 01:28:14 -04:00
|
|
|
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 12:52:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel() {
|
|
|
|
if (listener != null) {
|
|
|
|
listener.canceled();
|
2011-03-20 12:52:13 -04:00
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
dialog.show();
|
2011-02-26 19:39:06 -05:00
|
|
|
}
|
2011-03-20 12:52:13 -04:00
|
|
|
});
|
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
|
|
|
|
|
2011-03-25 01:28:14 -04:00
|
|
|
private static void finishExport(Activity activity, IStorageExporter storageExporter, HashSet<String> accountUuids, String fileName, OutputStream os, String encryptionKey, ExportListener listener) throws StorageImportExportException {
|
2011-03-20 12:52:13 -04:00
|
|
|
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
|
2011-03-20 12:52:13 -04:00
|
|
|
// to create the file needlessly
|
|
|
|
if (os == null && fileName != null) {
|
|
|
|
needToClose = true;
|
|
|
|
File outFile = new File(fileName);
|
|
|
|
os = new FileOutputStream(outFile);
|
|
|
|
}
|
|
|
|
if (os != null) {
|
2011-03-25 01:28:14 -04:00
|
|
|
storageExporter.exportPreferences(activity, accountUuids, os, encryptionKey);
|
2011-03-20 12:52:13 -04:00
|
|
|
if (listener != null) {
|
|
|
|
if (fileName != null) {
|
|
|
|
listener.success(fileName);
|
2011-03-20 16:21:24 -04:00
|
|
|
} else {
|
2011-03-20 12:52:13 -04:00
|
|
|
listener.success();
|
2011-02-26 19:39:06 -05:00
|
|
|
}
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
} else {
|
2011-03-20 12:52:13 -04:00
|
|
|
throw new StorageImportExportException("Internal error; no fileName or OutputStream", null);
|
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
} catch (Exception e) {
|
2011-03-20 12:52:13 -04:00
|
|
|
throw new StorageImportExportException(e.getLocalizedMessage(), e);
|
2011-03-20 16:21:24 -04:00
|
|
|
} finally {
|
2011-03-20 12:52:13 -04:00
|
|
|
if (needToClose && os != null) {
|
|
|
|
try {
|
|
|
|
os.close();
|
2011-03-20 16:21:24 -04:00
|
|
|
} catch (Exception e) {
|
2011-03-20 12:52:13 -04:00
|
|
|
Log.w(K9.LOG_TAG, "Unable to close OutputStream", e);
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|