2011-02-26 12:31:56 -05:00
|
|
|
package com.fsck.k9.preferences;
|
|
|
|
|
|
|
|
import java.io.File;
|
2011-03-03 11:00:58 -05:00
|
|
|
import java.io.FileOutputStream;
|
2011-03-28 02:04:43 -04:00
|
|
|
import java.io.IOException;
|
2011-03-03 11:00:58 -05:00
|
|
|
import java.io.OutputStream;
|
2011-03-29 23:08:49 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
2011-03-28 22:27:41 -04:00
|
|
|
import java.util.HashSet;
|
2011-03-29 23:08:49 -04:00
|
|
|
import java.util.List;
|
2011-03-28 22:27:41 -04:00
|
|
|
import java.util.Map;
|
2011-03-26 23:37:37 -04:00
|
|
|
import java.util.Set;
|
2011-03-30 00:44:16 -04:00
|
|
|
import java.util.TreeMap;
|
2011-06-06 22:25:17 -04:00
|
|
|
import java.util.Map.Entry;
|
2011-03-29 23:31:48 -04:00
|
|
|
import javax.crypto.CipherOutputStream;
|
2011-03-28 23:27:18 -04:00
|
|
|
import org.xmlpull.v1.XmlSerializer;
|
2011-02-26 12:31:56 -05:00
|
|
|
|
2011-03-28 02:04:43 -04:00
|
|
|
import android.content.Context;
|
2011-03-28 22:27:41 -04:00
|
|
|
import android.content.SharedPreferences;
|
2011-03-28 02:04:43 -04:00
|
|
|
import android.os.Environment;
|
2011-03-28 22:27:41 -04:00
|
|
|
import android.util.Log;
|
2011-03-28 23:27:18 -04:00
|
|
|
import android.util.Xml;
|
2011-03-28 02:04:43 -04:00
|
|
|
|
2011-03-28 22:27:41 -04:00
|
|
|
import com.fsck.k9.Account;
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.Preferences;
|
2011-03-28 02:04:43 -04:00
|
|
|
import com.fsck.k9.helper.Utility;
|
2011-06-06 22:25:17 -04:00
|
|
|
import com.fsck.k9.mail.Store;
|
2011-06-07 10:09:15 -04:00
|
|
|
import com.fsck.k9.mail.ServerSettings;
|
2011-06-07 17:09:40 -04:00
|
|
|
import com.fsck.k9.mail.Transport;
|
2011-03-29 22:57:05 -04:00
|
|
|
import com.fsck.k9.mail.store.LocalStore;
|
2011-02-26 12:31:56 -05:00
|
|
|
|
|
|
|
|
2011-03-20 16:21:24 -04:00
|
|
|
public class StorageExporter {
|
2011-03-28 02:04:43 -04:00
|
|
|
private static final String EXPORT_FILENAME = "settings.k9s";
|
|
|
|
|
2011-03-30 14:56:26 -04:00
|
|
|
public static final String ROOT_ELEMENT = "k9settings";
|
|
|
|
public static final String VERSION_ATTRIBUTE = "version";
|
2011-06-06 13:54:54 -04:00
|
|
|
public static final String FILE_FORMAT_ATTRIBUTE = "format";
|
2011-03-30 14:56:26 -04:00
|
|
|
public static final String GLOBAL_ELEMENT = "global";
|
|
|
|
public static final String SETTINGS_ELEMENT = "settings";
|
|
|
|
public static final String ACCOUNTS_ELEMENT = "accounts";
|
|
|
|
public static final String ACCOUNT_ELEMENT = "account";
|
|
|
|
public static final String UUID_ATTRIBUTE = "uuid";
|
2011-06-06 22:25:17 -04:00
|
|
|
public static final String INCOMING_SERVER_ELEMENT = "incoming-server";
|
2011-06-07 17:09:40 -04:00
|
|
|
public static final String OUTGOING_SERVER_ELEMENT = "outgoing-server";
|
2011-06-06 22:25:17 -04:00
|
|
|
public static final String TYPE_ATTRIBUTE = "type";
|
|
|
|
public static final String HOST_ELEMENT = "host";
|
|
|
|
public static final String PORT_ELEMENT = "port";
|
|
|
|
public static final String CONNECTION_SECURITY_ELEMENT = "connection-security";
|
|
|
|
public static final String AUTHENTICATION_TYPE_ELEMENT = "authentication-type";
|
|
|
|
public static final String USERNAME_ELEMENT = "username";
|
|
|
|
public static final String PASSWORD_ELEMENT = "password";
|
|
|
|
public static final String EXTRA_ELEMENT = "extra";
|
2011-03-30 14:56:26 -04:00
|
|
|
public static final String IDENTITIES_ELEMENT = "identities";
|
|
|
|
public static final String IDENTITY_ELEMENT = "identity";
|
|
|
|
public static final String FOLDERS_ELEMENT = "folders";
|
|
|
|
public static final String FOLDER_ELEMENT = "folder";
|
|
|
|
public static final String NAME_ATTRIBUTE = "name";
|
|
|
|
public static final String VALUE_ELEMENT = "value";
|
|
|
|
public static final String KEY_ATTRIBUTE = "key";
|
|
|
|
public static final String NAME_ELEMENT = "name";
|
|
|
|
public static final String EMAIL_ELEMENT = "email";
|
|
|
|
public static final String DESCRIPTION_ELEMENT = "description";
|
2011-03-29 18:21:15 -04:00
|
|
|
|
2011-03-28 02:04:43 -04:00
|
|
|
|
|
|
|
public static String exportToFile(Context context, boolean includeGlobals,
|
|
|
|
Set<String> accountUuids, String encryptionKey)
|
|
|
|
throws StorageImportExportException {
|
|
|
|
|
|
|
|
OutputStream os = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
File dir = new File(Environment.getExternalStorageDirectory() + File.separator
|
|
|
|
+ context.getPackageName());
|
|
|
|
dir.mkdirs();
|
|
|
|
File file = Utility.createUniqueFile(dir, EXPORT_FILENAME);
|
|
|
|
String fileName = file.getAbsolutePath();
|
|
|
|
os = new FileOutputStream(fileName);
|
2011-03-29 23:31:48 -04:00
|
|
|
|
|
|
|
if (encryptionKey == null) {
|
|
|
|
exportPreferences(context, os, includeGlobals, accountUuids);
|
|
|
|
} else {
|
|
|
|
exportPreferencesEncrypted(context, os, includeGlobals, accountUuids,
|
|
|
|
encryptionKey);
|
|
|
|
}
|
2011-03-28 02:04:43 -04:00
|
|
|
|
|
|
|
// If all went well, we return the name of the file just written.
|
|
|
|
return fileName;
|
|
|
|
} catch (Exception e) {
|
2011-06-06 22:25:17 -04:00
|
|
|
throw new StorageImportExportException(e);
|
2011-03-28 02:04:43 -04:00
|
|
|
} finally {
|
|
|
|
if (os != null) {
|
|
|
|
try {
|
|
|
|
os.close();
|
|
|
|
} catch (IOException ioe) {}
|
|
|
|
}
|
2011-03-03 11:00:58 -05:00
|
|
|
}
|
2011-03-29 23:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void exportPreferencesEncrypted(Context context, OutputStream os, boolean includeGlobals,
|
|
|
|
Set<String> accountUuids, String encryptionKey) throws StorageImportExportException {
|
|
|
|
|
|
|
|
try {
|
|
|
|
K9Krypto k = new K9Krypto(encryptionKey, K9Krypto.MODE.ENCRYPT);
|
|
|
|
CipherOutputStream cos = new CipherOutputStream(os, k.mCipher);
|
|
|
|
|
|
|
|
exportPreferences(context, cos, includeGlobals, accountUuids);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new StorageImportExportException();
|
|
|
|
}
|
2011-03-03 11:00:58 -05:00
|
|
|
}
|
2011-03-20 16:21:24 -04:00
|
|
|
|
2011-03-28 02:04:43 -04:00
|
|
|
public static void exportPreferences(Context context, OutputStream os, boolean includeGlobals,
|
2011-03-29 00:15:58 -04:00
|
|
|
Set<String> accountUuids) throws StorageImportExportException {
|
2011-03-27 12:37:43 -04:00
|
|
|
|
2011-03-28 02:04:43 -04:00
|
|
|
try {
|
2011-03-28 23:27:18 -04:00
|
|
|
XmlSerializer serializer = Xml.newSerializer();
|
|
|
|
serializer.setOutput(os, "UTF-8");
|
2011-03-27 19:55:46 -04:00
|
|
|
|
2011-03-28 23:27:18 -04:00
|
|
|
serializer.startDocument(null, Boolean.valueOf(true));
|
|
|
|
|
|
|
|
// Output with indentation
|
|
|
|
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
|
|
|
|
|
2011-03-29 18:21:15 -04:00
|
|
|
serializer.startTag(null, ROOT_ELEMENT);
|
2011-06-06 13:54:54 -04:00
|
|
|
//TODO: write content version number here
|
2011-03-29 18:21:15 -04:00
|
|
|
serializer.attribute(null, VERSION_ATTRIBUTE, "x");
|
2011-06-06 13:54:54 -04:00
|
|
|
//TODO: set file format version to "1" once the feature is stable and about to be merged into master
|
|
|
|
serializer.attribute(null, FILE_FORMAT_ATTRIBUTE, "y");
|
2011-03-27 12:37:43 -04:00
|
|
|
|
2011-03-28 22:27:41 -04:00
|
|
|
Log.i(K9.LOG_TAG, "Exporting preferences");
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-30 00:44:16 -04:00
|
|
|
Map<String, Object> prefs = new TreeMap<String, Object>(storage.getAll());
|
2011-03-28 22:27:41 -04:00
|
|
|
|
2011-03-29 01:08:29 -04:00
|
|
|
if (includeGlobals) {
|
2011-03-29 22:23:54 -04:00
|
|
|
serializer.startTag(null, GLOBAL_ELEMENT);
|
2011-03-29 01:08:29 -04:00
|
|
|
writeSettings(serializer, prefs);
|
2011-03-29 22:23:54 -04:00
|
|
|
serializer.endTag(null, GLOBAL_ELEMENT);
|
2011-03-28 22:27:41 -04:00
|
|
|
}
|
|
|
|
|
2011-03-29 18:21:15 -04:00
|
|
|
serializer.startTag(null, ACCOUNTS_ELEMENT);
|
2011-03-29 01:08:29 -04:00
|
|
|
for (String accountUuid : accountUuids) {
|
2011-06-06 22:25:17 -04:00
|
|
|
Account account = preferences.getAccount(accountUuid);
|
|
|
|
writeAccount(serializer, account, prefs);
|
2011-03-29 01:08:29 -04:00
|
|
|
}
|
2011-03-29 18:21:15 -04:00
|
|
|
serializer.endTag(null, ACCOUNTS_ELEMENT);
|
2011-03-27 12:37:43 -04:00
|
|
|
|
2011-03-29 18:21:15 -04:00
|
|
|
serializer.endTag(null, ROOT_ELEMENT);
|
2011-03-28 23:27:18 -04:00
|
|
|
serializer.endDocument();
|
|
|
|
serializer.flush();
|
|
|
|
|
2011-03-20 16:21:24 -04:00
|
|
|
} catch (Exception e) {
|
2011-03-20 12:52:13 -04:00
|
|
|
throw new StorageImportExportException(e.getLocalizedMessage(), e);
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|
|
|
|
}
|
2011-03-29 01:08:29 -04:00
|
|
|
|
|
|
|
private static void writeSettings(XmlSerializer serializer,
|
2011-03-30 00:44:16 -04:00
|
|
|
Map<String, Object> prefs) throws IOException {
|
2011-03-29 01:08:29 -04:00
|
|
|
|
2011-03-30 00:44:16 -04:00
|
|
|
for (Map.Entry<String, Object> entry : prefs.entrySet()) {
|
2011-03-29 01:08:29 -04:00
|
|
|
String key = entry.getKey();
|
|
|
|
String value = entry.getValue().toString();
|
|
|
|
if (key.indexOf('.') != -1) {
|
|
|
|
// Skip account entries
|
|
|
|
continue;
|
|
|
|
}
|
2011-06-06 22:25:17 -04:00
|
|
|
writeKeyValue(serializer, key, value);
|
2011-03-29 01:08:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-06 22:25:17 -04:00
|
|
|
private static void writeAccount(XmlSerializer serializer, Account account,
|
2011-03-30 00:44:16 -04:00
|
|
|
Map<String, Object> prefs) throws IOException {
|
2011-03-29 01:08:29 -04:00
|
|
|
|
2011-03-29 23:08:49 -04:00
|
|
|
Set<Integer> identities = new HashSet<Integer>();
|
2011-03-29 22:57:05 -04:00
|
|
|
Set<String> folders = new HashSet<String>();
|
2011-06-06 22:25:17 -04:00
|
|
|
String accountUuid = account.getUuid();
|
2011-03-29 21:55:14 -04:00
|
|
|
|
2011-03-29 18:21:15 -04:00
|
|
|
serializer.startTag(null, ACCOUNT_ELEMENT);
|
|
|
|
serializer.attribute(null, UUID_ATTRIBUTE, accountUuid);
|
2011-03-29 21:55:14 -04:00
|
|
|
|
2011-03-30 00:37:10 -04:00
|
|
|
String name = (String) prefs.get(accountUuid + "." + Account.ACCOUNT_DESCRIPTION_KEY);
|
|
|
|
if (name != null) {
|
|
|
|
serializer.startTag(null, NAME_ELEMENT);
|
|
|
|
serializer.text(name);
|
|
|
|
serializer.endTag(null, NAME_ELEMENT);
|
|
|
|
}
|
|
|
|
|
2011-06-06 22:25:17 -04:00
|
|
|
|
|
|
|
// Write incoming server settings
|
2011-06-07 10:09:15 -04:00
|
|
|
ServerSettings incoming = Store.decodeStoreUri(account.getStoreUri());
|
2011-06-06 22:25:17 -04:00
|
|
|
serializer.startTag(null, INCOMING_SERVER_ELEMENT);
|
|
|
|
serializer.attribute(null, TYPE_ATTRIBUTE, incoming.type);
|
|
|
|
|
|
|
|
writeElement(serializer, HOST_ELEMENT, incoming.host);
|
2011-06-07 17:09:40 -04:00
|
|
|
if (incoming.port != -1) {
|
|
|
|
writeElement(serializer, PORT_ELEMENT, Integer.toString(incoming.port));
|
|
|
|
}
|
2011-06-06 22:25:17 -04:00
|
|
|
writeElement(serializer, CONNECTION_SECURITY_ELEMENT, incoming.connectionSecurity.name());
|
|
|
|
writeElement(serializer, AUTHENTICATION_TYPE_ELEMENT, incoming.authenticationType);
|
|
|
|
writeElement(serializer, USERNAME_ELEMENT, incoming.username);
|
|
|
|
//TODO: make saving the password optional
|
|
|
|
writeElement(serializer, PASSWORD_ELEMENT, incoming.password);
|
|
|
|
|
|
|
|
Map<String, String> extras = incoming.getExtra();
|
|
|
|
if (extras != null && extras.size() > 0) {
|
|
|
|
serializer.startTag(null, EXTRA_ELEMENT);
|
|
|
|
for (Entry<String, String> extra : extras.entrySet()) {
|
|
|
|
writeKeyValue(serializer, extra.getKey(), extra.getValue());
|
|
|
|
}
|
|
|
|
serializer.endTag(null, EXTRA_ELEMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
serializer.endTag(null, INCOMING_SERVER_ELEMENT);
|
|
|
|
|
|
|
|
|
2011-06-07 17:09:40 -04:00
|
|
|
// Write outgoing server settings
|
|
|
|
ServerSettings outgoing = Transport.decodeTransportUri(account.getTransportUri());
|
|
|
|
serializer.startTag(null, OUTGOING_SERVER_ELEMENT);
|
|
|
|
serializer.attribute(null, TYPE_ATTRIBUTE, outgoing.type);
|
|
|
|
|
|
|
|
writeElement(serializer, HOST_ELEMENT, outgoing.host);
|
|
|
|
if (outgoing.port != -1) {
|
|
|
|
writeElement(serializer, PORT_ELEMENT, Integer.toString(outgoing.port));
|
|
|
|
}
|
|
|
|
writeElement(serializer, CONNECTION_SECURITY_ELEMENT, outgoing.connectionSecurity.name());
|
|
|
|
writeElement(serializer, AUTHENTICATION_TYPE_ELEMENT, outgoing.authenticationType);
|
|
|
|
writeElement(serializer, USERNAME_ELEMENT, outgoing.username);
|
|
|
|
//TODO: make saving the password optional
|
|
|
|
writeElement(serializer, PASSWORD_ELEMENT, outgoing.password);
|
|
|
|
|
|
|
|
extras = outgoing.getExtra();
|
|
|
|
if (extras != null && extras.size() > 0) {
|
|
|
|
serializer.startTag(null, EXTRA_ELEMENT);
|
|
|
|
for (Entry<String, String> extra : extras.entrySet()) {
|
|
|
|
writeKeyValue(serializer, extra.getKey(), extra.getValue());
|
|
|
|
}
|
|
|
|
serializer.endTag(null, EXTRA_ELEMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
serializer.endTag(null, OUTGOING_SERVER_ELEMENT);
|
2011-06-06 22:25:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
// Write account settings
|
2011-03-29 21:55:14 -04:00
|
|
|
serializer.startTag(null, SETTINGS_ELEMENT);
|
2011-03-30 00:44:16 -04:00
|
|
|
for (Map.Entry<String, Object> entry : prefs.entrySet()) {
|
2011-03-29 01:08:29 -04:00
|
|
|
String key = entry.getKey();
|
|
|
|
String value = entry.getValue().toString();
|
|
|
|
String[] comps = key.split("\\.");
|
2011-03-29 21:55:14 -04:00
|
|
|
if (comps.length >= 2) {
|
2011-03-29 01:08:29 -04:00
|
|
|
String keyUuid = comps[0];
|
2011-03-30 00:37:10 -04:00
|
|
|
String secondPart = comps[1];
|
|
|
|
|
|
|
|
if (!keyUuid.equals(accountUuid)
|
2011-06-06 22:25:17 -04:00
|
|
|
|| Account.ACCOUNT_DESCRIPTION_KEY.equals(secondPart)
|
2011-06-07 17:09:40 -04:00
|
|
|
|| "storeUri".equals(secondPart)
|
|
|
|
|| "transportUri".equals(secondPart)) {
|
2011-03-29 01:08:29 -04:00
|
|
|
continue;
|
|
|
|
}
|
2011-03-29 21:55:14 -04:00
|
|
|
if (comps.length == 3) {
|
2011-03-29 22:57:05 -04:00
|
|
|
String thirdPart = comps[2];
|
2011-03-29 21:55:14 -04:00
|
|
|
|
2011-03-29 22:57:05 -04:00
|
|
|
if (Account.IDENTITY_KEYS.contains(secondPart)) {
|
2011-03-29 21:55:14 -04:00
|
|
|
// This is an identity key. Save identity index for later...
|
2011-03-29 23:08:49 -04:00
|
|
|
try {
|
|
|
|
identities.add(Integer.parseInt(thirdPart));
|
|
|
|
} catch (NumberFormatException e) { /* ignore */ }
|
2011-03-29 22:57:05 -04:00
|
|
|
// ... but don't write it now.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LocalStore.FOLDER_SETTINGS_KEYS.contains(thirdPart)) {
|
|
|
|
// This is a folder key. Save folder name for later...
|
|
|
|
folders.add(secondPart);
|
2011-03-29 21:55:14 -04:00
|
|
|
// ... but don't write it now.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2011-03-29 01:08:29 -04:00
|
|
|
} else {
|
2011-03-29 21:55:14 -04:00
|
|
|
// Skip global config entries and identity entries
|
2011-03-29 01:08:29 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip account UUID from key
|
|
|
|
String keyPart = key.substring(comps[0].length() + 1);
|
|
|
|
|
2011-06-06 22:25:17 -04:00
|
|
|
writeKeyValue(serializer, keyPart, value);
|
2011-03-29 01:08:29 -04:00
|
|
|
}
|
2011-03-29 21:55:14 -04:00
|
|
|
serializer.endTag(null, SETTINGS_ELEMENT);
|
|
|
|
|
|
|
|
if (identities.size() > 0) {
|
|
|
|
serializer.startTag(null, IDENTITIES_ELEMENT);
|
2011-03-29 23:08:49 -04:00
|
|
|
|
|
|
|
// Sort identity indices (that's why we store them as Integers)
|
|
|
|
List<Integer> sortedIdentities = new ArrayList<Integer>(identities);
|
|
|
|
Collections.sort(sortedIdentities);
|
|
|
|
|
|
|
|
for (Integer identityIndex : sortedIdentities) {
|
|
|
|
writeIdentity(serializer, accountUuid, identityIndex.toString(), prefs);
|
2011-03-29 21:55:14 -04:00
|
|
|
}
|
|
|
|
serializer.endTag(null, IDENTITIES_ELEMENT);
|
|
|
|
}
|
|
|
|
|
2011-03-29 22:57:05 -04:00
|
|
|
if (folders.size() > 0) {
|
|
|
|
serializer.startTag(null, FOLDERS_ELEMENT);
|
|
|
|
for (String folder : folders) {
|
|
|
|
writeFolder(serializer, accountUuid, folder, prefs);
|
|
|
|
}
|
|
|
|
serializer.endTag(null, FOLDERS_ELEMENT);
|
|
|
|
}
|
|
|
|
|
2011-03-29 18:21:15 -04:00
|
|
|
serializer.endTag(null, ACCOUNT_ELEMENT);
|
2011-03-29 01:08:29 -04:00
|
|
|
}
|
2011-03-29 21:55:14 -04:00
|
|
|
|
|
|
|
private static void writeIdentity(XmlSerializer serializer, String accountUuid,
|
2011-03-30 00:44:16 -04:00
|
|
|
String identity, Map<String, Object> prefs) throws IOException {
|
2011-03-29 21:55:14 -04:00
|
|
|
|
|
|
|
serializer.startTag(null, IDENTITY_ELEMENT);
|
2011-03-30 00:37:10 -04:00
|
|
|
|
|
|
|
String name = (String) prefs.get(accountUuid + "." + Account.IDENTITY_NAME_KEY +
|
|
|
|
"." + identity);
|
|
|
|
serializer.startTag(null, NAME_ELEMENT);
|
|
|
|
serializer.text(name);
|
|
|
|
serializer.endTag(null, NAME_ELEMENT);
|
|
|
|
|
|
|
|
String email = (String) prefs.get(accountUuid + "." + Account.IDENTITY_EMAIL_KEY +
|
|
|
|
"." + identity);
|
|
|
|
serializer.startTag(null, EMAIL_ELEMENT);
|
|
|
|
serializer.text(email);
|
|
|
|
serializer.endTag(null, EMAIL_ELEMENT);
|
|
|
|
|
|
|
|
String description = (String) prefs.get(accountUuid + "." +
|
|
|
|
Account.IDENTITY_DESCRIPTION_KEY + "." + identity);
|
|
|
|
if (description != null) {
|
|
|
|
serializer.startTag(null, DESCRIPTION_ELEMENT);
|
|
|
|
serializer.text(description);
|
|
|
|
serializer.endTag(null, DESCRIPTION_ELEMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
serializer.startTag(null, SETTINGS_ELEMENT);
|
2011-03-30 00:44:16 -04:00
|
|
|
for (Map.Entry<String, Object> entry : prefs.entrySet()) {
|
2011-03-29 21:55:14 -04:00
|
|
|
String key = entry.getKey();
|
|
|
|
String value = entry.getValue().toString();
|
|
|
|
String[] comps = key.split("\\.");
|
|
|
|
if (comps.length >= 3) {
|
|
|
|
String keyUuid = comps[0];
|
|
|
|
String identityKey = comps[1];
|
|
|
|
String identityIndex = comps[2];
|
|
|
|
if (!keyUuid.equals(accountUuid) || !identityIndex.equals(identity)
|
2011-03-30 00:37:10 -04:00
|
|
|
|| !Account.IDENTITY_KEYS.contains(identityKey)
|
|
|
|
|| Account.IDENTITY_NAME_KEY.equals(identityKey)
|
|
|
|
|| Account.IDENTITY_EMAIL_KEY.equals(identityKey)
|
|
|
|
|| Account.IDENTITY_DESCRIPTION_KEY.equals(identityKey)) {
|
2011-03-29 21:55:14 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Skip non-identity config entries
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-06-06 22:25:17 -04:00
|
|
|
writeKeyValue(serializer, comps[1], value);
|
2011-03-29 21:55:14 -04:00
|
|
|
}
|
2011-03-30 00:37:10 -04:00
|
|
|
serializer.endTag(null, SETTINGS_ELEMENT);
|
|
|
|
|
2011-03-29 21:55:14 -04:00
|
|
|
serializer.endTag(null, IDENTITY_ELEMENT);
|
|
|
|
}
|
2011-03-29 22:57:05 -04:00
|
|
|
|
|
|
|
private static void writeFolder(XmlSerializer serializer, String accountUuid,
|
2011-03-30 00:44:16 -04:00
|
|
|
String folder, Map<String, Object> prefs) throws IOException {
|
2011-03-29 22:57:05 -04:00
|
|
|
|
|
|
|
serializer.startTag(null, FOLDER_ELEMENT);
|
|
|
|
serializer.attribute(null, NAME_ATTRIBUTE, folder);
|
2011-03-30 00:44:16 -04:00
|
|
|
for (Map.Entry<String, Object> entry : prefs.entrySet()) {
|
2011-03-29 22:57:05 -04:00
|
|
|
String key = entry.getKey();
|
|
|
|
String value = entry.getValue().toString();
|
|
|
|
String[] comps = key.split("\\.");
|
|
|
|
if (comps.length >= 3) {
|
|
|
|
String keyUuid = comps[0];
|
|
|
|
String folderName = comps[1];
|
|
|
|
String folderKey = comps[2];
|
|
|
|
if (!keyUuid.equals(accountUuid) || !folderName.equals(folder)
|
|
|
|
|| !LocalStore.FOLDER_SETTINGS_KEYS.contains(folderKey)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Skip non-folder config entries
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-06-06 22:25:17 -04:00
|
|
|
writeKeyValue(serializer, comps[2], value);
|
2011-03-29 22:57:05 -04:00
|
|
|
}
|
|
|
|
serializer.endTag(null, FOLDER_ELEMENT);
|
|
|
|
}
|
2011-06-06 22:25:17 -04:00
|
|
|
|
|
|
|
private static void writeElement(XmlSerializer serializer, String elementName, String value)
|
|
|
|
throws IllegalArgumentException, IllegalStateException, IOException {
|
|
|
|
if (value != null) {
|
|
|
|
serializer.startTag(null, elementName);
|
|
|
|
serializer.text(value);
|
|
|
|
serializer.endTag(null, elementName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void writeKeyValue(XmlSerializer serializer, String key, String value)
|
|
|
|
throws IllegalArgumentException, IllegalStateException, IOException {
|
|
|
|
serializer.startTag(null, VALUE_ELEMENT);
|
|
|
|
serializer.attribute(null, KEY_ATTRIBUTE, key);
|
|
|
|
if (value != null) {
|
|
|
|
serializer.text(value);
|
|
|
|
}
|
|
|
|
serializer.endTag(null, VALUE_ELEMENT);
|
|
|
|
}
|
2011-02-26 12:31:56 -05:00
|
|
|
}
|