1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-25 00:58:50 -05:00

Merge StorageExporterEncryptedXml into StorageExporter

This commit is contained in:
cketti 2011-03-29 04:27:41 +02:00
parent 84f4331766
commit 9a78145e22
3 changed files with 53 additions and 92 deletions

View File

@ -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<String> accountUuids, OutputStream os, String encryptionKey) throws StorageImportExportException;
}

View File

@ -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<String> 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("<k9settings version=\"" + 1 + "\">");
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<String>();
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("</k9settings>");
pf.flush();

View File

@ -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<String> 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<String>();
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;
}
}