diff --git a/src/com/fsck/k9/preferences/IStorageExporter.java b/src/com/fsck/k9/preferences/IStorageExporter.java deleted file mode 100644 index 76bdce369..000000000 --- a/src/com/fsck/k9/preferences/IStorageExporter.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.fsck.k9.preferences; - -import java.io.OutputStream; -import java.util.Set; - -import android.content.Context; - -public interface IStorageExporter { - public boolean needsKey(); - // exportPreferences must be sure to flush all data to the OutputStream before returning - public void exportPreferences(Context context, boolean includeGlobals, Set 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 20bc0bc5f..49d5d2c17 100644 --- a/src/com/fsck/k9/preferences/StorageExporter.java +++ b/src/com/fsck/k9/preferences/StorageExporter.java @@ -6,11 +6,18 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.util.HashSet; +import java.util.Map; import java.util.Set; import android.content.Context; +import android.content.SharedPreferences; import android.os.Environment; +import android.util.Log; +import com.fsck.k9.Account; +import com.fsck.k9.K9; +import com.fsck.k9.Preferences; import com.fsck.k9.helper.Utility; @@ -49,8 +56,7 @@ public class StorageExporter { public static void exportPreferences(Context context, OutputStream os, boolean includeGlobals, Set accountUuids, String encryptionKey) throws StorageImportExportException { - IStorageExporter storageExporter = new StorageExporterEncryptedXml(); - if (storageExporter.needsKey() && encryptionKey == null) { + if (encryptionKey == null) { throw new StorageImportExportException("Encryption key required, but none supplied"); } @@ -62,7 +68,51 @@ public class StorageExporter { pf.println(""); pf.flush(); - storageExporter.exportPreferences(context, includeGlobals, accountUuids, os, encryptionKey); + Log.i(K9.LOG_TAG, "Exporting preferences"); + K9Krypto krypto = new K9Krypto(encryptionKey, K9Krypto.MODE.ENCRYPT); + long keysEvaluated = 0; + long keysExported = 0; + + Preferences preferences = Preferences.getPreferences(context); + SharedPreferences storage = preferences.getPreferences(); + + 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(); + for (Map.Entry < String, ? extends Object > entry : prefs.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue().toString(); + //Log.i(K9.LOG_TAG, "Evaluating key " + key); + keysEvaluated++; + 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; + } + } else if (!includeGlobals) { + // Skip global config entries if the user didn't request them + continue; + } + String keyEnc = krypto.encrypt(key); + String valueEnc = krypto.encrypt(value); + String output = keyEnc + ":" + valueEnc; + //Log.i(K9.LOG_TAG, "For key " + key + ", output is " + output); + pf.println(output); + keysExported++; + + } + + pf.flush(); + + Log.i(K9.LOG_TAG, "Exported " + keysExported + " of " + keysEvaluated + " settings."); pf.println(""); pf.flush(); diff --git a/src/com/fsck/k9/preferences/StorageExporterEncryptedXml.java b/src/com/fsck/k9/preferences/StorageExporterEncryptedXml.java deleted file mode 100644 index 8aa48da86..000000000 --- a/src/com/fsck/k9/preferences/StorageExporterEncryptedXml.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.fsck.k9.preferences; - -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import android.content.Context; -import android.content.SharedPreferences; -import android.util.Log; - -import com.fsck.k9.Account; -import com.fsck.k9.K9; -import com.fsck.k9.Preferences; - -public class StorageExporterEncryptedXml implements IStorageExporter { - public void exportPreferences(Context context, boolean includeGlobals, Set accountUuids, OutputStream os, String encryptionKey) throws StorageImportExportException { - try { - 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); - long keysEvaluated = 0; - long keysExported = 0; - - Preferences preferences = Preferences.getPreferences(context); - SharedPreferences storage = preferences.getPreferences(); - - 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(); - for (Map.Entry < String, ? extends Object > entry : prefs.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue().toString(); - //Log.i(K9.LOG_TAG, "Evaluating key " + key); - keysEvaluated++; - 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; - } - } else if (!includeGlobals) { - // Skip global config entries if the user didn't request them - continue; - } - String keyEnc = krypto.encrypt(key); - String valueEnc = krypto.encrypt(value); - String output = keyEnc + ":" + valueEnc; - //Log.i(K9.LOG_TAG, "For key " + key + ", output is " + output); - pf.println(output); - keysExported++; - - } - - pf.flush(); - - Log.i(K9.LOG_TAG, "Exported " + keysExported + " of " + keysEvaluated + " settings."); - } catch (Exception e) { - throw new StorageImportExportException("Unable to encrypt settings", e); - } - } - - @Override - public boolean needsKey() { - return true; - } -}