use of com.fsck.k9.Preferences 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 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;
}
use of com.fsck.k9.Preferences in project k-9 by k9mail.
the class MailService method reschedulePoll.
private void reschedulePoll(final boolean hasConnectivity, final boolean doBackground, boolean considerLastCheckEnd) {
if (!(hasConnectivity && doBackground)) {
Timber.i("No connectivity, canceling check for %s", getApplication().getPackageName());
nextCheck = -1;
cancel();
return;
}
Preferences prefs = Preferences.getPreferences(MailService.this);
Storage storage = prefs.getStorage();
int previousInterval = storage.getInt(PREVIOUS_INTERVAL, -1);
long lastCheckEnd = storage.getLong(LAST_CHECK_END, -1);
long now = System.currentTimeMillis();
if (lastCheckEnd > now) {
Timber.i("The database claims that the last time mail was checked was in the future (%tc). To try to get " + "things back to normal, the last check time has been reset to: %tc", lastCheckEnd, now);
lastCheckEnd = now;
}
int shortestInterval = -1;
for (Account account : prefs.getAvailableAccounts()) {
if (account.getAutomaticCheckIntervalMinutes() != -1 && account.getFolderSyncMode() != FolderMode.NONE && (account.getAutomaticCheckIntervalMinutes() < shortestInterval || shortestInterval == -1)) {
shortestInterval = account.getAutomaticCheckIntervalMinutes();
}
}
StorageEditor editor = storage.edit();
editor.putInt(PREVIOUS_INTERVAL, shortestInterval);
editor.commit();
if (shortestInterval == -1) {
Timber.i("No next check scheduled for package %s", getApplication().getPackageName());
nextCheck = -1;
pollingRequested = false;
cancel();
} else {
long delay = (shortestInterval * (60 * 1000));
long base = (previousInterval == -1 || lastCheckEnd == -1 || !considerLastCheckEnd ? System.currentTimeMillis() : lastCheckEnd);
long nextTime = base + delay;
Timber.i("previousInterval = %d, shortestInterval = %d, lastCheckEnd = %tc, considerLastCheckEnd = %b", previousInterval, shortestInterval, lastCheckEnd, considerLastCheckEnd);
nextCheck = nextTime;
pollingRequested = true;
try {
Timber.i("Next check for package %s scheduled for %tc", getApplication().getPackageName(), nextTime);
} catch (Exception e) {
// I once got a NullPointerException deep in new Date();
Timber.e(e, "Exception while logging");
}
Intent i = new Intent(this, MailService.class);
i.setAction(ACTION_CHECK_MAIL);
BootReceiver.scheduleIntent(MailService.this, nextTime, i);
}
}
use of com.fsck.k9.Preferences in project k-9 by k9mail.
the class MessageList method decodeExtras.
private boolean decodeExtras(Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action) && intent.getData() != null) {
Uri uri = intent.getData();
List<String> segmentList = uri.getPathSegments();
String accountId = segmentList.get(0);
Collection<Account> accounts = Preferences.getPreferences(this).getAvailableAccounts();
for (Account account : accounts) {
if (String.valueOf(account.getAccountNumber()).equals(accountId)) {
String folderName = segmentList.get(1);
String messageUid = segmentList.get(2);
mMessageReference = new MessageReference(account.getUuid(), folderName, messageUid, null);
break;
}
}
} else if (ACTION_SHORTCUT.equals(action)) {
// Handle shortcut intents
String specialFolder = intent.getStringExtra(EXTRA_SPECIAL_FOLDER);
if (SearchAccount.UNIFIED_INBOX.equals(specialFolder)) {
mSearch = SearchAccount.createUnifiedInboxAccount(this).getRelatedSearch();
} else if (SearchAccount.ALL_MESSAGES.equals(specialFolder)) {
mSearch = SearchAccount.createAllMessagesAccount(this).getRelatedSearch();
}
} else if (intent.getStringExtra(SearchManager.QUERY) != null) {
// check if this intent comes from the system search ( remote )
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
//Query was received from Search Dialog
String query = intent.getStringExtra(SearchManager.QUERY).trim();
mSearch = new LocalSearch(getString(R.string.search_results));
mSearch.setManualSearch(true);
mNoThreading = true;
mSearch.or(new SearchCondition(SearchField.SENDER, Attribute.CONTAINS, query));
mSearch.or(new SearchCondition(SearchField.SUBJECT, Attribute.CONTAINS, query));
mSearch.or(new SearchCondition(SearchField.MESSAGE_CONTENTS, Attribute.CONTAINS, query));
Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
if (appData != null) {
mSearch.addAccountUuid(appData.getString(EXTRA_SEARCH_ACCOUNT));
// searches started from a folder list activity will provide an account, but no folder
if (appData.getString(EXTRA_SEARCH_FOLDER) != null) {
mSearch.addAllowedFolder(appData.getString(EXTRA_SEARCH_FOLDER));
}
} else {
mSearch.addAccountUuid(LocalSearch.ALL_ACCOUNTS);
}
}
} else if (intent.hasExtra(EXTRA_SEARCH_OLD)) {
mSearch = intent.getParcelableExtra(EXTRA_SEARCH_OLD);
mNoThreading = intent.getBooleanExtra(EXTRA_NO_THREADING, false);
} else {
// regular LocalSearch object was passed
mSearch = intent.hasExtra(EXTRA_SEARCH) ? ParcelableUtil.unmarshall(intent.getByteArrayExtra(EXTRA_SEARCH), LocalSearch.CREATOR) : null;
mNoThreading = intent.getBooleanExtra(EXTRA_NO_THREADING, false);
}
if (mMessageReference == null) {
String messageReferenceString = intent.getStringExtra(EXTRA_MESSAGE_REFERENCE);
mMessageReference = MessageReference.parse(messageReferenceString);
}
if (mMessageReference != null) {
mSearch = new LocalSearch();
mSearch.addAccountUuid(mMessageReference.getAccountUuid());
mSearch.addAllowedFolder(mMessageReference.getFolderName());
}
if (mSearch == null) {
// We've most likely been started by an old unread widget
String accountUuid = intent.getStringExtra("account");
String folderName = intent.getStringExtra("folder");
mSearch = new LocalSearch(folderName);
mSearch.addAccountUuid((accountUuid == null) ? "invalid" : accountUuid);
if (folderName != null) {
mSearch.addAllowedFolder(folderName);
}
}
Preferences prefs = Preferences.getPreferences(getApplicationContext());
String[] accountUuids = mSearch.getAccountUuids();
if (mSearch.searchAllAccounts()) {
List<Account> accounts = prefs.getAccounts();
mSingleAccountMode = (accounts.size() == 1);
if (mSingleAccountMode) {
mAccount = accounts.get(0);
}
} else {
mSingleAccountMode = (accountUuids.length == 1);
if (mSingleAccountMode) {
mAccount = prefs.getAccount(accountUuids[0]);
}
}
mSingleFolderMode = mSingleAccountMode && (mSearch.getFolderNames().size() == 1);
if (mSingleAccountMode && (mAccount == null || !mAccount.isAvailable(this))) {
Timber.i("not opening MessageList of unavailable account");
onAccountUnavailable();
return false;
}
if (mSingleFolderMode) {
mFolderName = mSearch.getFolderNames().get(0);
}
// now we know if we are in single account mode and need a subtitle
mActionBarSubTitle.setVisibility((!mSingleFolderMode) ? View.GONE : View.VISIBLE);
return true;
}
use of com.fsck.k9.Preferences in project k-9 by k9mail.
the class MessageList method openMessage.
@Override
public void openMessage(MessageReference messageReference) {
Preferences prefs = Preferences.getPreferences(getApplicationContext());
Account account = prefs.getAccount(messageReference.getAccountUuid());
String folderName = messageReference.getFolderName();
if (folderName.equals(account.getDraftsFolderName())) {
MessageActions.actionEditDraft(this, messageReference);
} else {
mMessageViewContainer.removeView(mMessageViewPlaceHolder);
if (mMessageListFragment != null) {
mMessageListFragment.setActiveMessage(messageReference);
}
MessageViewFragment fragment = MessageViewFragment.newInstance(messageReference);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.message_view_container, fragment);
mMessageViewFragment = fragment;
ft.commit();
if (mDisplayMode != DisplayMode.SPLIT_VIEW) {
showMessageView();
}
}
}
Aggregations