1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 19:52:17 -05:00

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) { 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 @Override
public void canceled() { public void canceled() {

View File

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

View File

@ -1,5 +1,6 @@
package com.fsck.k9.activity; package com.fsck.k9.activity;
import java.util.HashSet;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -10,14 +11,10 @@ import com.fsck.k9.R;
import com.fsck.k9.preferences.StorageVersioning; import com.fsck.k9.preferences.StorageVersioning;
public class ExportHelper { 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: // 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 version = StorageVersioning.STORAGE_VERSION.VERSION1.getVersionString();
String uuid = null; AsyncUIProcessor.getInstance(activity.getApplication()).exportSettings(activity, version, accountUuids, new ExportListener() {
if (account != null) {
uuid = account.getUuid();
}
AsyncUIProcessor.getInstance(activity.getApplication()).exportSettings(activity, version, uuid, new ExportListener() {
@Override @Override
public void canceled() { public void canceled() {

View File

@ -1,10 +1,11 @@
package com.fsck.k9.preferences; package com.fsck.k9.preferences;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.HashSet;
import android.content.Context; import android.content.Context;
public interface IStorageExporter { public interface IStorageExporter {
public boolean needsKey(); 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.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.HashSet;
import android.app.Activity; import android.app.Activity;
import android.util.Log; import android.util.Log;
@ -14,16 +15,16 @@ import com.fsck.k9.activity.ExportListener;
import com.fsck.k9.activity.PasswordEntryDialog; import com.fsck.k9.activity.PasswordEntryDialog;
public class StorageExporter { 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 { try {
IStorageExporter storageExporter = StorageVersioning.createExporter(version); IStorageExporter storageExporter = StorageVersioning.createExporter(version);
if (storageExporter == null) { if (storageExporter == null) {
throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, version), null); throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, version), null);
} }
if (storageExporter.needsKey() && encryptionKey == null) { if (storageExporter.needsKey() && encryptionKey == null) {
gatherPassword(activity, storageExporter, uuid, fileName, os, listener); gatherPassword(activity, storageExporter, accountUuids, fileName, os, listener);
} else { } 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 { public static void exportPreferences(Activity activity, String version, HashSet<String> accountUuids, String fileName, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, version, uuid, fileName, null, encryptionKey, listener); 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 { public static void exportPrefererences(Activity activity, String version, HashSet<String> accountUuids, OutputStream os, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, version, uuid, null, os, encryptionKey, listener); 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() { activity.runOnUiThread(new Runnable() {
@Override @Override
@ -56,7 +57,7 @@ public class StorageExporter {
@Override @Override
public void run() { public void run() {
try { try {
finishExport(activity, storageExporter, uuid, fileName, os, chosenPassword, listener); finishExport(activity, storageExporter, accountUuids, fileName, os, chosenPassword, listener);
} catch (Exception e) { } catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception while finishing export", e); Log.w(K9.LOG_TAG, "Exception while finishing export", e);
if (listener != null) { 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; boolean needToClose = false;
if (listener != null) { if (listener != null) {
listener.started(); listener.started();
@ -94,7 +95,7 @@ public class StorageExporter {
os = new FileOutputStream(outFile); os = new FileOutputStream(outFile);
} }
if (os != null) { if (os != null) {
storageExporter.exportPreferences(activity, uuid, os, encryptionKey); storageExporter.exportPreferences(activity, accountUuids, os, encryptionKey);
if (listener != null) { if (listener != null) {
if (fileName != null) { if (fileName != null) {
listener.success(fileName); listener.success(fileName);

View File

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