use of com.fsck.k9.preferences.Storage in project k-9 by k9mail.
the class SettingsExporter method writeSettings.
private static void writeSettings(XmlSerializer serializer, Map<String, Object> prefs) throws IOException {
for (Entry<String, TreeMap<Integer, SettingsDescription>> versionedSetting : GlobalSettings.SETTINGS.entrySet()) {
String key = versionedSetting.getKey();
String valueString = (String) prefs.get(key);
TreeMap<Integer, SettingsDescription> versions = versionedSetting.getValue();
Integer highestVersion = versions.lastKey();
SettingsDescription setting = versions.get(highestVersion);
if (setting == null) {
// Setting was removed.
continue;
}
if (valueString != null) {
try {
writeKeyAndPrettyValueFromSetting(serializer, key, setting, valueString);
} catch (InvalidSettingValueException e) {
Timber.w("Global setting \"%s\" has invalid value \"%s\" in preference storage. " + "This shouldn't happen!", key, valueString);
}
} else {
Timber.d("Couldn't find key \"%s\" in preference storage. Using default value.", key);
writeKeyAndDefaultValueFromSetting(serializer, key, setting);
}
}
}
use of com.fsck.k9.preferences.Storage in project k-9 by k9mail.
the class SettingsExporter method writeAccount.
private static void writeAccount(XmlSerializer serializer, Account account, Map<String, Object> prefs) throws IOException {
Set<Integer> identities = new HashSet<>();
Set<String> folders = new HashSet<>();
String accountUuid = account.getUuid();
serializer.startTag(null, ACCOUNT_ELEMENT);
serializer.attribute(null, UUID_ATTRIBUTE, accountUuid);
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);
}
// Write incoming server settings
ServerSettings incoming = RemoteStore.decodeStoreUri(account.getStoreUri());
serializer.startTag(null, INCOMING_SERVER_ELEMENT);
serializer.attribute(null, TYPE_ATTRIBUTE, incoming.type.name());
writeElement(serializer, HOST_ELEMENT, incoming.host);
if (incoming.port != -1) {
writeElement(serializer, PORT_ELEMENT, Integer.toString(incoming.port));
}
if (incoming.connectionSecurity != null) {
writeElement(serializer, CONNECTION_SECURITY_ELEMENT, incoming.connectionSecurity.name());
}
if (incoming.authenticationType != null) {
writeElement(serializer, AUTHENTICATION_TYPE_ELEMENT, incoming.authenticationType.name());
}
writeElement(serializer, USERNAME_ELEMENT, incoming.username);
writeElement(serializer, CLIENT_CERTIFICATE_ALIAS_ELEMENT, incoming.clientCertificateAlias);
// XXX For now we don't export the password
//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()) {
writeKeyAndPrettyValueFromSetting(serializer, extra.getKey(), extra.getValue());
}
serializer.endTag(null, EXTRA_ELEMENT);
}
serializer.endTag(null, INCOMING_SERVER_ELEMENT);
// Write outgoing server settings
ServerSettings outgoing = Transport.decodeTransportUri(account.getTransportUri());
serializer.startTag(null, OUTGOING_SERVER_ELEMENT);
serializer.attribute(null, TYPE_ATTRIBUTE, outgoing.type.name());
writeElement(serializer, HOST_ELEMENT, outgoing.host);
if (outgoing.port != -1) {
writeElement(serializer, PORT_ELEMENT, Integer.toString(outgoing.port));
}
if (outgoing.connectionSecurity != null) {
writeElement(serializer, CONNECTION_SECURITY_ELEMENT, outgoing.connectionSecurity.name());
}
if (outgoing.authenticationType != null) {
writeElement(serializer, AUTHENTICATION_TYPE_ELEMENT, outgoing.authenticationType.name());
}
writeElement(serializer, USERNAME_ELEMENT, outgoing.username);
writeElement(serializer, CLIENT_CERTIFICATE_ALIAS_ELEMENT, outgoing.clientCertificateAlias);
// XXX For now we don't export the password
//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()) {
writeKeyAndPrettyValueFromSetting(serializer, extra.getKey(), extra.getValue());
}
serializer.endTag(null, EXTRA_ELEMENT);
}
serializer.endTag(null, OUTGOING_SERVER_ELEMENT);
// Write account settings
serializer.startTag(null, SETTINGS_ELEMENT);
for (Map.Entry<String, Object> entry : prefs.entrySet()) {
String key = entry.getKey();
String valueString = entry.getValue().toString();
String[] comps = key.split("\\.", 2);
if (comps.length < 2) {
// Skip global settings
continue;
}
String keyUuid = comps[0];
String keyPart = comps[1];
if (!keyUuid.equals(accountUuid)) {
// Setting doesn't belong to the account we're currently writing.
continue;
}
int indexOfLastDot = keyPart.lastIndexOf(".");
boolean hasThirdPart = indexOfLastDot != -1 && indexOfLastDot < keyPart.length() - 1;
if (hasThirdPart) {
String secondPart = keyPart.substring(0, indexOfLastDot);
String thirdPart = keyPart.substring(indexOfLastDot + 1);
if (Account.IDENTITY_DESCRIPTION_KEY.equals(secondPart)) {
// This is an identity key. Save identity index for later...
try {
identities.add(Integer.parseInt(thirdPart));
} catch (NumberFormatException e) {
/* ignore */
}
// ... but don't write it now.
continue;
}
if (FolderSettings.SETTINGS.containsKey(thirdPart)) {
// This is a folder key. Save folder name for later...
folders.add(secondPart);
// ... but don't write it now.
continue;
}
}
TreeMap<Integer, SettingsDescription> versionedSetting = AccountSettings.SETTINGS.get(keyPart);
if (versionedSetting != null) {
Integer highestVersion = versionedSetting.lastKey();
SettingsDescription setting = versionedSetting.get(highestVersion);
if (setting != null) {
// Only export account settings that can be found in AccountSettings.SETTINGS
try {
writeKeyAndPrettyValueFromSetting(serializer, keyPart, setting, valueString);
} catch (InvalidSettingValueException e) {
Timber.w("Account setting \"%s\" (%s) has invalid value \"%s\" in preference storage. " + "This shouldn't happen!", keyPart, account.getDescription(), valueString);
}
}
}
}
serializer.endTag(null, SETTINGS_ELEMENT);
if (identities.size() > 0) {
serializer.startTag(null, IDENTITIES_ELEMENT);
// Sort identity indices (that's why we store them as Integers)
List<Integer> sortedIdentities = new ArrayList<>(identities);
Collections.sort(sortedIdentities);
for (Integer identityIndex : sortedIdentities) {
writeIdentity(serializer, accountUuid, identityIndex.toString(), prefs);
}
serializer.endTag(null, IDENTITIES_ELEMENT);
}
if (folders.size() > 0) {
serializer.startTag(null, FOLDERS_ELEMENT);
for (String folder : folders) {
writeFolder(serializer, accountUuid, folder, prefs);
}
serializer.endTag(null, FOLDERS_ELEMENT);
}
serializer.endTag(null, ACCOUNT_ELEMENT);
}
use of com.fsck.k9.preferences.Storage in project k-9 by k9mail.
the class SettingsExporter method writeIdentity.
private static void writeIdentity(XmlSerializer serializer, String accountUuid, String identity, Map<String, Object> prefs) throws IOException {
serializer.startTag(null, IDENTITY_ELEMENT);
String prefix = accountUuid + ".";
String suffix = "." + identity;
// Write name belonging to the identity
String name = (String) prefs.get(prefix + Account.IDENTITY_NAME_KEY + suffix);
serializer.startTag(null, NAME_ELEMENT);
serializer.text(name);
serializer.endTag(null, NAME_ELEMENT);
// Write email address belonging to the identity
String email = (String) prefs.get(prefix + Account.IDENTITY_EMAIL_KEY + suffix);
serializer.startTag(null, EMAIL_ELEMENT);
serializer.text(email);
serializer.endTag(null, EMAIL_ELEMENT);
// Write identity description
String description = (String) prefs.get(prefix + Account.IDENTITY_DESCRIPTION_KEY + suffix);
if (description != null) {
serializer.startTag(null, DESCRIPTION_ELEMENT);
serializer.text(description);
serializer.endTag(null, DESCRIPTION_ELEMENT);
}
// Write identity settings
serializer.startTag(null, SETTINGS_ELEMENT);
for (Map.Entry<String, Object> entry : prefs.entrySet()) {
String key = entry.getKey();
String valueString = entry.getValue().toString();
String[] comps = key.split("\\.");
if (comps.length < 3) {
// Skip non-identity config entries
continue;
}
String keyUuid = comps[0];
String identityKey = comps[1];
String identityIndex = comps[2];
if (!keyUuid.equals(accountUuid) || !identityIndex.equals(identity)) {
// Skip entries that belong to another identity
continue;
}
TreeMap<Integer, SettingsDescription> versionedSetting = IdentitySettings.SETTINGS.get(identityKey);
if (versionedSetting != null) {
Integer highestVersion = versionedSetting.lastKey();
SettingsDescription setting = versionedSetting.get(highestVersion);
if (setting != null) {
// Only write settings that have an entry in IdentitySettings.SETTINGS
try {
writeKeyAndPrettyValueFromSetting(serializer, identityKey, setting, valueString);
} catch (InvalidSettingValueException e) {
Timber.w("Identity setting \"%s\" has invalid value \"%s\" in preference storage. " + "This shouldn't happen!", identityKey, valueString);
}
}
}
}
serializer.endTag(null, SETTINGS_ELEMENT);
serializer.endTag(null, IDENTITY_ELEMENT);
}
use of com.fsck.k9.preferences.Storage in project k-9 by k9mail.
the class SettingsExporter method exportPreferences.
static void exportPreferences(Context context, OutputStream os, boolean includeGlobals, Set<String> accountUuids) throws SettingsImportExportException {
try {
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(os, "UTF-8");
serializer.startDocument(null, Boolean.TRUE);
// Output with indentation
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, ROOT_ELEMENT);
serializer.attribute(null, VERSION_ATTRIBUTE, Integer.toString(Settings.VERSION));
serializer.attribute(null, FILE_FORMAT_ATTRIBUTE, Integer.toString(FILE_FORMAT_VERSION));
Timber.i("Exporting preferences");
Preferences preferences = Preferences.getPreferences(context);
Storage storage = preferences.getStorage();
Set<String> exportAccounts;
if (accountUuids == null) {
List<Account> accounts = preferences.getAccounts();
exportAccounts = new HashSet<>();
for (Account account : accounts) {
exportAccounts.add(account.getUuid());
}
} else {
exportAccounts = accountUuids;
}
Map<String, Object> prefs = new TreeMap<String, Object>(storage.getAll());
if (includeGlobals) {
serializer.startTag(null, GLOBAL_ELEMENT);
writeSettings(serializer, prefs);
serializer.endTag(null, GLOBAL_ELEMENT);
}
serializer.startTag(null, ACCOUNTS_ELEMENT);
for (String accountUuid : exportAccounts) {
Account account = preferences.getAccount(accountUuid);
writeAccount(serializer, account, prefs);
}
serializer.endTag(null, ACCOUNTS_ELEMENT);
serializer.endTag(null, ROOT_ELEMENT);
serializer.endDocument();
serializer.flush();
} catch (Exception e) {
throw new SettingsImportExportException(e.getLocalizedMessage(), e);
}
}
use of com.fsck.k9.preferences.Storage in project k-9 by k9mail.
the class RemoteControlService method startService.
@Override
public int startService(final Intent intent, final int startId) {
Timber.i("RemoteControlService started with startId = %d", startId);
final Preferences preferences = Preferences.getPreferences(this);
if (RESCHEDULE_ACTION.equals(intent.getAction())) {
Timber.i("RemoteControlService requesting MailService poll reschedule");
MailService.actionReschedulePoll(this, null);
}
if (PUSH_RESTART_ACTION.equals(intent.getAction())) {
Timber.i("RemoteControlService requesting MailService push restart");
MailService.actionRestartPushers(this, null);
} else if (RemoteControlService.SET_ACTION.equals(intent.getAction())) {
Timber.i("RemoteControlService got request to change settings");
execute(getApplication(), new Runnable() {
public void run() {
try {
boolean needsReschedule = false;
boolean needsPushRestart = false;
String uuid = intent.getStringExtra(K9_ACCOUNT_UUID);
boolean allAccounts = intent.getBooleanExtra(K9_ALL_ACCOUNTS, false);
if (allAccounts) {
Timber.i("RemoteControlService changing settings for all accounts");
} else {
Timber.i("RemoteControlService changing settings for account with UUID %s", uuid);
}
List<Account> accounts = preferences.getAccounts();
for (Account account : accounts) {
//warning: account may not be isAvailable()
if (allAccounts || account.getUuid().equals(uuid)) {
Timber.i("RemoteControlService changing settings for account %s", account.getDescription());
String notificationEnabled = intent.getStringExtra(K9_NOTIFICATION_ENABLED);
String ringEnabled = intent.getStringExtra(K9_RING_ENABLED);
String vibrateEnabled = intent.getStringExtra(K9_VIBRATE_ENABLED);
String pushClasses = intent.getStringExtra(K9_PUSH_CLASSES);
String pollClasses = intent.getStringExtra(K9_POLL_CLASSES);
String pollFrequency = intent.getStringExtra(K9_POLL_FREQUENCY);
if (notificationEnabled != null) {
account.setNotifyNewMail(Boolean.parseBoolean(notificationEnabled));
}
if (ringEnabled != null) {
account.getNotificationSetting().setRing(Boolean.parseBoolean(ringEnabled));
}
if (vibrateEnabled != null) {
account.getNotificationSetting().setVibrate(Boolean.parseBoolean(vibrateEnabled));
}
if (pushClasses != null) {
needsPushRestart |= account.setFolderPushMode(FolderMode.valueOf(pushClasses));
}
if (pollClasses != null) {
needsReschedule |= account.setFolderSyncMode(FolderMode.valueOf(pollClasses));
}
if (pollFrequency != null) {
String[] allowedFrequencies = getResources().getStringArray(R.array.account_settings_check_frequency_values);
for (String allowedFrequency : allowedFrequencies) {
if (allowedFrequency.equals(pollFrequency)) {
Integer newInterval = Integer.parseInt(allowedFrequency);
needsReschedule |= account.setAutomaticCheckIntervalMinutes(newInterval);
}
}
}
account.save(Preferences.getPreferences(RemoteControlService.this));
}
}
Timber.i("RemoteControlService changing global settings");
String backgroundOps = intent.getStringExtra(K9_BACKGROUND_OPERATIONS);
if (K9RemoteControl.K9_BACKGROUND_OPERATIONS_ALWAYS.equals(backgroundOps) || K9RemoteControl.K9_BACKGROUND_OPERATIONS_NEVER.equals(backgroundOps) || K9RemoteControl.K9_BACKGROUND_OPERATIONS_WHEN_CHECKED_AUTO_SYNC.equals(backgroundOps)) {
BACKGROUND_OPS newBackgroundOps = BACKGROUND_OPS.valueOf(backgroundOps);
boolean needsReset = K9.setBackgroundOps(newBackgroundOps);
needsPushRestart |= needsReset;
needsReschedule |= needsReset;
}
String theme = intent.getStringExtra(K9_THEME);
if (theme != null) {
K9.setK9Theme(K9RemoteControl.K9_THEME_DARK.equals(theme) ? K9.Theme.DARK : K9.Theme.LIGHT);
}
Storage storage = preferences.getStorage();
StorageEditor editor = storage.edit();
K9.save(editor);
editor.commit();
if (needsReschedule) {
Intent i = new Intent(RemoteControlService.this, RemoteControlService.class);
i.setAction(RESCHEDULE_ACTION);
long nextTime = System.currentTimeMillis() + 10000;
BootReceiver.scheduleIntent(RemoteControlService.this, nextTime, i);
}
if (needsPushRestart) {
Intent i = new Intent(RemoteControlService.this, RemoteControlService.class);
i.setAction(PUSH_RESTART_ACTION);
long nextTime = System.currentTimeMillis() + 10000;
BootReceiver.scheduleIntent(RemoteControlService.this, nextTime, i);
}
} catch (Exception e) {
Timber.e(e, "Could not handle K9_SET");
Toast toast = Toast.makeText(RemoteControlService.this, e.getMessage(), Toast.LENGTH_LONG);
toast.show();
}
}
}, RemoteControlService.REMOTE_CONTROL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
}
return START_NOT_STICKY;
}
Aggregations