1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-08-13 17:03:48 -04:00

Moved password input dialog for export to Accounts activity.

This commit is contained in:
cketti 2011-03-28 01:55:46 +02:00
parent a559a35249
commit 1d146278b0
2 changed files with 23 additions and 58 deletions

View File

@ -1120,28 +1120,38 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
public void onExport(final boolean includeGlobals, final Account account) {
// TODO, prompt to allow a user to choose which accounts to export
HashSet<String> accountUuids;
accountUuids = new HashSet<String>();
final Set<String> accountUuids = new HashSet<String>();
if (account != null) {
accountUuids.add(account.getUuid());
}
// Once there are more file formats, build a UI to select which one to use. For now, use the encrypted/encoded format:
String storageFormat = StorageFormat.ENCRYPTED_XML_FILE;
new ExportAsyncTask(storageFormat, includeGlobals, accountUuids).execute();
// Prompt the user for a password
new PasswordEntryDialog(this,
getString(R.string.settings_export_encryption_password_prompt),
new PasswordEntryDialog.PasswordEntryListener() {
public void passwordChosen(final String chosenPassword) {
// Got the password. Now run export task in the background.
new ExportAsyncTask(includeGlobals, accountUuids, chosenPassword).execute();
}
public void cancel() {
// User cancelled the export. Nothing more to do.
}
})
.show();
}
private class ExportAsyncTask extends AsyncTask<Void, Void, Boolean> {
private boolean mIncludeGlobals;
private Set<String> mAccountUuids;
private String mStorageFormat;
private String mEncryptionKey;
private String mFileName;
private ExportAsyncTask(String storageFormat, boolean includeGlobals,
Set<String> accountUuids) {
mStorageFormat = storageFormat;
private ExportAsyncTask(boolean includeGlobals, Set<String> accountUuids,
String encryptionKey) {
mIncludeGlobals = includeGlobals;
mAccountUuids = accountUuids;
mEncryptionKey = encryptionKey;
}
@Override
@ -1163,8 +1173,8 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
dir.mkdirs();
File file = Utility.createUniqueFile(dir, "settings.k9s");
mFileName = file.getAbsolutePath();
StorageExporter.exportPreferences(Accounts.this, mStorageFormat, mIncludeGlobals,
mAccountUuids, mFileName, null, null);
StorageExporter.exportPreferences(Accounts.this, StorageFormat.ENCRYPTED_XML_FILE,
mIncludeGlobals, mAccountUuids, mFileName, mEncryptionKey, null);
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception during export", e);
return false;
@ -1185,5 +1195,4 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
}
}
}
}

View File

@ -12,9 +12,7 @@ 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;
public class StorageExporter {
private static void exportPreferences(Activity activity, String storageFormat, boolean includeGlobals, Set<String> accountUuids, String fileName, OutputStream os, String encryptionKey, final ExportListener listener) {
@ -24,7 +22,7 @@ public class StorageExporter {
throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, storageFormat), null);
}
if (storageExporter.needsKey() && encryptionKey == null) {
gatherPassword(activity, storageFormat, storageExporter, includeGlobals, accountUuids, fileName, os, listener);
throw new StorageImportExportException("Encryption key required, but none supplied");
} else {
finishExport(activity, storageFormat, storageExporter, includeGlobals, accountUuids, fileName, os, encryptionKey, listener);
}
@ -41,48 +39,6 @@ public class StorageExporter {
exportPreferences(activity, storageFormat, includeGlobals, accountUuids, fileName, null, encryptionKey, listener);
}
public static void exportPrefererences(Activity activity, String storageFormat, boolean includeGlobals, Set<String> accountUuids, OutputStream os, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, storageFormat, includeGlobals, accountUuids, null, os, encryptionKey, 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
public void run() {
PasswordEntryDialog dialog = new PasswordEntryDialog(activity, activity.getString(R.string.settings_export_encryption_password_prompt),
new PasswordEntryDialog.PasswordEntryListener() {
public void passwordChosen(final String chosenPassword) {
AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() {
@Override
public void run() {
try {
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) {
listener.failure(e.getLocalizedMessage(), e);
}
}
}
});
}
public void cancel() {
if (listener != null) {
listener.canceled();
}
}
});
dialog.show();
}
});
}
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) {
@ -101,7 +57,7 @@ public class StorageExporter {
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();