Lift the choice of which accounts to export all the way up to

Accounts.java in advance of adding a dialog
This commit is contained in:
Jesse Vincent 2011-03-25 16:28:14 +11:00
parent b8949abaca
commit b4a43893a3
6 changed files with 43 additions and 44 deletions

View file

@ -1113,7 +1113,15 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
}
public void onExport(final Account account) {
ExportHelper.exportSettings(this, account, new ExportListener() {
// TODO, prompt to allow a user to choose which accounts to export
HashSet<String> accountUuids;
accountUuids = new HashSet<String>();
if (account != null) {
accountUuids.add(account.getUuid());
}
ExportHelper.exportSettings(this, accountUuids, new ExportListener() {
@Override
public void canceled() {

View file

@ -4,6 +4,7 @@ import java.io.File;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.HashSet;
import android.app.Activity;
import android.app.Application;
@ -40,7 +41,7 @@ public class AsyncUIProcessor {
public void execute(Runnable runnable) {
threadPool.execute(runnable);
}
public void exportSettings(final Activity activity, final String version, final String uuid, final ExportListener listener) {
public void exportSettings(final Activity activity, final String version, final HashSet<String> accountUuids, final ExportListener listener) {
threadPool.execute(new Runnable() {
@Override
@ -53,7 +54,7 @@ public class AsyncUIProcessor {
dir.mkdirs();
File file = Utility.createUniqueFile(dir, "settings.k9s");
String fileName = file.getAbsolutePath();
StorageExporter.exportPreferences(activity, version, uuid, fileName, null, listener);
StorageExporter.exportPreferences(activity, version, accountUuids, fileName, null, listener);
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception during export", e);
listener.failure(e.getLocalizedMessage(), e);

View file

@ -1,5 +1,6 @@
package com.fsck.k9.activity;
import java.util.HashSet;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
@ -10,14 +11,10 @@ import com.fsck.k9.R;
import com.fsck.k9.preferences.StorageVersioning;
public class ExportHelper {
public static void exportSettings(final Activity activity, final Account account, final ExportListener listener) {
public static void exportSettings(final Activity activity, final HashSet<String> accountUuids, final ExportListener listener) {
// Once there are more versions, build a UI to select which one to use. For now, use the encrypted/encoded version:
String version = StorageVersioning.STORAGE_VERSION.VERSION1.getVersionString();
String uuid = null;
if (account != null) {
uuid = account.getUuid();
}
AsyncUIProcessor.getInstance(activity.getApplication()).exportSettings(activity, version, uuid, new ExportListener() {
AsyncUIProcessor.getInstance(activity.getApplication()).exportSettings(activity, version, accountUuids, new ExportListener() {
@Override
public void canceled() {

View file

@ -1,10 +1,11 @@
package com.fsck.k9.preferences;
import java.io.OutputStream;
import java.util.HashSet;
import android.content.Context;
public interface IStorageExporter {
public boolean needsKey();
public void exportPreferences(Context context, String uuid, OutputStream os, String encryptionKey) throws StorageImportExportException;
}
public void exportPreferences(Context context, HashSet<String> accountUuids, OutputStream os, String encryptionKey) throws StorageImportExportException;
}

View file

@ -3,6 +3,7 @@ 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;
@ -14,16 +15,16 @@ import com.fsck.k9.activity.ExportListener;
import com.fsck.k9.activity.PasswordEntryDialog;
public class StorageExporter {
private static void exportPreferences(Activity activity, String version, String uuid, String fileName, OutputStream os, String encryptionKey, final ExportListener listener) {
private static void exportPreferences(Activity activity, String version, HashSet<String> accountUuids, String fileName, OutputStream os, String encryptionKey, final ExportListener listener) {
try {
IStorageExporter storageExporter = StorageVersioning.createExporter(version);
if (storageExporter == null) {
throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, version), null);
}
if (storageExporter.needsKey() && encryptionKey == null) {
gatherPassword(activity, storageExporter, uuid, fileName, os, listener);
gatherPassword(activity, storageExporter, accountUuids, fileName, os, listener);
} else {
finishExport(activity, storageExporter, uuid, fileName, os, encryptionKey, listener);
finishExport(activity, storageExporter, accountUuids, fileName, os, encryptionKey, listener);
}
}
@ -34,15 +35,15 @@ public class StorageExporter {
}
}
public static void exportPreferences(Activity activity, String version, String uuid, String fileName, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, version, uuid, fileName, null, encryptionKey, listener);
public static void exportPreferences(Activity activity, String version, HashSet<String> accountUuids, String fileName, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, version, accountUuids, fileName, null, encryptionKey, listener);
}
public static void exportPrefererences(Activity activity, String version, String uuid, OutputStream os, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, version, uuid, null, os, encryptionKey, listener);
public static void exportPrefererences(Activity activity, String version, HashSet<String> accountUuids, OutputStream os, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, version, accountUuids, null, os, encryptionKey, listener);
}
private static void gatherPassword(final Activity activity, final IStorageExporter storageExporter, final String uuid, final String fileName, final OutputStream os, final ExportListener listener) {
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
@ -56,7 +57,7 @@ public class StorageExporter {
@Override
public void run() {
try {
finishExport(activity, storageExporter, uuid, fileName, os, chosenPassword, listener);
finishExport(activity, storageExporter, accountUuids, fileName, os, chosenPassword, listener);
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception while finishing export", e);
if (listener != null) {
@ -80,7 +81,7 @@ public class StorageExporter {
}
private static void finishExport(Activity activity, IStorageExporter storageExporter, String uuid, String fileName, OutputStream os, String encryptionKey, ExportListener listener) throws StorageImportExportException {
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();
@ -94,7 +95,7 @@ public class StorageExporter {
os = new FileOutputStream(outFile);
}
if (os != null) {
storageExporter.exportPreferences(activity, uuid, os, encryptionKey);
storageExporter.exportPreferences(activity, accountUuids, os, encryptionKey);
if (listener != null) {
if (fileName != null) {
listener.success(fileName);

View file

@ -19,9 +19,9 @@ import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
public class StorageExporterVersion1 implements IStorageExporter {
public void exportPreferences(Context context, String uuid, OutputStream os, String encryptionKey) throws StorageImportExportException {
public void exportPreferences(Context context, HashSet<String> accountUuids, OutputStream os, String encryptionKey) throws StorageImportExportException {
try {
Log.i(K9.LOG_TAG, "Exporting preferences for account " + uuid + " to OutputStream");
Log.i(K9.LOG_TAG, "Exporting preferences");
K9Krypto krypto = new K9Krypto(encryptionKey, K9Krypto.MODE.ENCRYPT);
OutputStreamWriter sw = new OutputStreamWriter(os);
PrintWriter pf = new PrintWriter(sw);
@ -35,10 +35,12 @@ public class StorageExporterVersion1 implements IStorageExporter {
Preferences preferences = Preferences.getPreferences(context);
SharedPreferences storage = preferences.getPreferences();
Account[] accounts = preferences.getAccounts();
Set<String> accountUuids = new HashSet<String>();
for (Account account : accounts) {
accountUuids.add(account.getUuid());
if (accountUuids == null) {
Account[] accounts = preferences.getAccounts();
accountUuids = new HashSet<String>();
for (Account account : accounts) {
accountUuids.add(account.getUuid());
}
}
Map < String, ? extends Object > prefs = storage.getAll();
@ -47,23 +49,13 @@ public class StorageExporterVersion1 implements IStorageExporter {
String value = entry.getValue().toString();
//Log.i(K9.LOG_TAG, "Evaluating key " + key);
keysEvaluated++;
if (uuid != null) {
String[] comps = key.split("\\.");
String[] comps = key.split("\\.");
if (comps.length > 1) {
String keyUuid = comps[0];
//Log.i(K9.LOG_TAG, "Got key uuid " + keyUuid);
if (uuid.equals(keyUuid) == false) {
//Log.i(K9.LOG_TAG, "Skipping key " + key + " which is for another account or global");
if (accountUuids.contains(keyUuid) == false) {
//Log.i(K9.LOG_TAG, "Skipping key " + key + " which is not for any current account");
continue;
}
} else {
String[] comps = key.split("\\.");
if (comps.length > 1) {
String keyUuid = comps[0];
if (accountUuids.contains(keyUuid) == false) {
//Log.i(K9.LOG_TAG, "Skipping key " + key + " which is not for any current account");
continue;
}
}
}
String keyEnc = krypto.encrypt(key);
String valueEnc = krypto.encrypt(value);
@ -77,8 +69,7 @@ public class StorageExporterVersion1 implements IStorageExporter {
pf.println("</k9settings>");
pf.flush();
Log.i(K9.LOG_TAG, "Exported " + keysExported + " settings of " + keysEvaluated
+ " total for preferences for account " + uuid);
Log.i(K9.LOG_TAG, "Exported " + keysExported + " of " + keysEvaluated + " settings.");
} catch (Exception e) {
throw new StorageImportExportException("Unable to encrypt settings", e);
}