From b4a43893a35eac7becfb21be8a0328aa82662012 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Fri, 25 Mar 2011 16:28:14 +1100 Subject: [PATCH] Lift the choice of which accounts to export all the way up to Accounts.java in advance of adding a dialog --- src/com/fsck/k9/activity/Accounts.java | 10 +++++- .../fsck/k9/activity/AsyncUIProcessor.java | 5 +-- src/com/fsck/k9/activity/ExportHelper.java | 9 ++--- .../fsck/k9/preferences/IStorageExporter.java | 5 +-- .../fsck/k9/preferences/StorageExporter.java | 23 ++++++------ .../preferences/StorageExporterVersion1.java | 35 +++++++------------ 6 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/com/fsck/k9/activity/Accounts.java b/src/com/fsck/k9/activity/Accounts.java index 3662fd748..435b50d1c 100644 --- a/src/com/fsck/k9/activity/Accounts.java +++ b/src/com/fsck/k9/activity/Accounts.java @@ -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 accountUuids; + accountUuids = new HashSet(); + if (account != null) { + accountUuids.add(account.getUuid()); + } + + ExportHelper.exportSettings(this, accountUuids, new ExportListener() { @Override public void canceled() { diff --git a/src/com/fsck/k9/activity/AsyncUIProcessor.java b/src/com/fsck/k9/activity/AsyncUIProcessor.java index 9ac776aae..52d047296 100644 --- a/src/com/fsck/k9/activity/AsyncUIProcessor.java +++ b/src/com/fsck/k9/activity/AsyncUIProcessor.java @@ -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 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); diff --git a/src/com/fsck/k9/activity/ExportHelper.java b/src/com/fsck/k9/activity/ExportHelper.java index b019175b0..e9899fc19 100644 --- a/src/com/fsck/k9/activity/ExportHelper.java +++ b/src/com/fsck/k9/activity/ExportHelper.java @@ -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 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() { diff --git a/src/com/fsck/k9/preferences/IStorageExporter.java b/src/com/fsck/k9/preferences/IStorageExporter.java index ae682e2a9..5cf94afad 100644 --- a/src/com/fsck/k9/preferences/IStorageExporter.java +++ b/src/com/fsck/k9/preferences/IStorageExporter.java @@ -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; -} \ No newline at end of file + public void exportPreferences(Context context, HashSet accountUuids, OutputStream os, String encryptionKey) throws StorageImportExportException; +} diff --git a/src/com/fsck/k9/preferences/StorageExporter.java b/src/com/fsck/k9/preferences/StorageExporter.java index 62bb61f8f..4375bed1c 100644 --- a/src/com/fsck/k9/preferences/StorageExporter.java +++ b/src/com/fsck/k9/preferences/StorageExporter.java @@ -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 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 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 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 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 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); diff --git a/src/com/fsck/k9/preferences/StorageExporterVersion1.java b/src/com/fsck/k9/preferences/StorageExporterVersion1.java index 5959c2d46..9c4cf8aa1 100644 --- a/src/com/fsck/k9/preferences/StorageExporterVersion1.java +++ b/src/com/fsck/k9/preferences/StorageExporterVersion1.java @@ -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 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 accountUuids = new HashSet(); - for (Account account : accounts) { - accountUuids.add(account.getUuid()); + if (accountUuids == null) { + Account[] accounts = preferences.getAccounts(); + accountUuids = new HashSet(); + 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(""); 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); }