From 790ae2e25c91139c19f6cfc019e66e97e9aad13f Mon Sep 17 00:00:00 2001 From: Daniel Applebaum Date: Tue, 9 Feb 2010 01:14:55 +0000 Subject: [PATCH] Fixes Issue 1194 Do both a poll and push reset when connectivity or background data status changes. --- src/com/fsck/k9/activity/setup/Prefs.java | 2 +- .../k9/remotecontrol/K9RemoteControl.java | 2 +- src/com/fsck/k9/service/BootReceiver.java | 2 +- src/com/fsck/k9/service/MailService.java | 98 +++++++++++-------- .../fsck/k9/service/RemoteControlService.java | 4 +- 5 files changed, 63 insertions(+), 45 deletions(-) diff --git a/src/com/fsck/k9/activity/setup/Prefs.java b/src/com/fsck/k9/activity/setup/Prefs.java index d8f9c813a..b1525fa59 100644 --- a/src/com/fsck/k9/activity/setup/Prefs.java +++ b/src/com/fsck/k9/activity/setup/Prefs.java @@ -162,7 +162,7 @@ public class Prefs extends K9PreferenceActivity editor.commit(); if (needsRefresh) { - MailService.actionRestartPushers(this, null); + MailService.actionReset(this, null); } } diff --git a/src/com/fsck/k9/remotecontrol/K9RemoteControl.java b/src/com/fsck/k9/remotecontrol/K9RemoteControl.java index ce236b15e..26afc68e0 100644 --- a/src/com/fsck/k9/remotecontrol/K9RemoteControl.java +++ b/src/com/fsck/k9/remotecontrol/K9RemoteControl.java @@ -74,7 +74,7 @@ public class K9RemoteControl * Key for the {@link Intent} Extra for controlling whether K-9 will activate the vibrator for new unread mail. * Acceptable values are K9_ENABLED and K9_DISABLED */ - public final static String K9_VIBRATE_ENABLED = "com.fsck.k9.K9RemoteControl.notificationEnabled"; + public final static String K9_VIBRATE_ENABLED = "com.fsck.k9.K9RemoteControl.vibrateEnabled"; public final static String K9_FOLDERS_NONE = "NONE"; public final static String K9_FOLDERS_ALL = "ALL"; diff --git a/src/com/fsck/k9/service/BootReceiver.java b/src/com/fsck/k9/service/BootReceiver.java index f3513cc0d..7a9dd8a4e 100644 --- a/src/com/fsck/k9/service/BootReceiver.java +++ b/src/com/fsck/k9/service/BootReceiver.java @@ -51,7 +51,7 @@ public class BootReceiver extends CoreReceiver } else if (ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED.equals(intent.getAction())) { - MailService.actionRestartPushers(context, tmpWakeLockId); + MailService.actionReset(context, tmpWakeLockId); tmpWakeLockId = null; } else if (FIRE_INTENT.equals(intent.getAction())) diff --git a/src/com/fsck/k9/service/MailService.java b/src/com/fsck/k9/service/MailService.java index 822cf58fc..e2ce83125 100644 --- a/src/com/fsck/k9/service/MailService.java +++ b/src/com/fsck/k9/service/MailService.java @@ -37,6 +37,8 @@ public class MailService extends CoreService private static final String CANCEL_CONNECTIVITY_NOTICE = "com.fsck.k9.intent.action.MAIL_SERVICE_CANCEL_CONNECTIVITY_NOTICE"; private static final String HAS_CONNECTIVITY = "com.fsck.k9.intent.action.MAIL_SERVICE_HAS_CONNECTIVITY"; + + private static long nextCheck = -1; public static void actionReset(Context context, Integer wakeLockId) { @@ -146,7 +148,7 @@ public class MailService extends CoreService PollService.startService(this); } - reschedule(startIdObj); + reschedule(hasConnectivity, doBackground, startIdObj); startIdObj = null; } else if (ACTION_CANCEL.equals(intent.getAction())) @@ -177,7 +179,7 @@ public class MailService extends CoreService { if (K9.DEBUG) Log.v(K9.LOG_TAG, "***** MailService *****: rescheduling poll"); - reschedule(startIdObj); + reschedule(hasConnectivity, doBackground, startIdObj); startIdObj = null; } @@ -216,10 +218,7 @@ public class MailService extends CoreService private void rescheduleAll(final boolean hasConnectivity, final boolean doBackground, final Integer startId) { - if (hasConnectivity && doBackground) - { - reschedule(null); - } + reschedule(hasConnectivity, doBackground, null); reschedulePushers(hasConnectivity, doBackground, startId); } @@ -272,55 +271,67 @@ public class MailService extends CoreService BootReceiver.cancelIntent(this, i); } - private void reschedule(Integer startId) + private void reschedule(final boolean hasConnectivity, final boolean doBackground, Integer startId) { - execute(getApplication(), new Runnable() + if (hasConnectivity && doBackground) { - public void run() + execute(getApplication(), new Runnable() { - int shortestInterval = -1; - - for (Account account : Preferences.getPreferences(MailService.this).getAccounts()) + public void run() { - if (account.getAutomaticCheckIntervalMinutes() != -1 - && account.getFolderSyncMode() != FolderMode.NONE - && (account.getAutomaticCheckIntervalMinutes() < shortestInterval || shortestInterval == -1)) + int shortestInterval = -1; + + for (Account account : Preferences.getPreferences(MailService.this).getAccounts()) { - shortestInterval = account.getAutomaticCheckIntervalMinutes(); + if (account.getAutomaticCheckIntervalMinutes() != -1 + && account.getFolderSyncMode() != FolderMode.NONE + && (account.getAutomaticCheckIntervalMinutes() < shortestInterval || shortestInterval == -1)) + { + shortestInterval = account.getAutomaticCheckIntervalMinutes(); + } } - } - - if (shortestInterval == -1) - { - if (K9.DEBUG) - Log.i(K9.LOG_TAG, "No next check scheduled for package " + getApplication().getPackageName()); - cancel(); - } - else - { - long delay = (shortestInterval * (60 * 1000)); - - long nextTime = System.currentTimeMillis() + delay; - try + + if (shortestInterval == -1) { + nextCheck = -1; if (K9.DEBUG) - Log.i(K9.LOG_TAG, "Next check for package " + getApplication().getPackageName() + " scheduled for " + new Date(nextTime)); + Log.i(K9.LOG_TAG, "No next check scheduled for package " + getApplication().getPackageName()); + cancel(); } - catch (Exception e) + else { - // I once got a NullPointerException deep in new Date(); - Log.e(K9.LOG_TAG, "Exception while logging", e); + long delay = (shortestInterval * (60 * 1000)); + + long nextTime = System.currentTimeMillis() + delay; + nextCheck = nextTime; + try + { + if (K9.DEBUG) + Log.i(K9.LOG_TAG, "Next check for package " + getApplication().getPackageName() + " scheduled for " + new Date(nextTime)); + } + catch (Exception e) + { + // I once got a NullPointerException deep in new Date(); + Log.e(K9.LOG_TAG, "Exception while logging", e); + } + + Intent i = new Intent(); + i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService"); + i.setAction(ACTION_CHECK_MAIL); + BootReceiver.scheduleIntent(MailService.this, nextTime, i); + } - - Intent i = new Intent(); - i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService"); - i.setAction(ACTION_CHECK_MAIL); - BootReceiver.scheduleIntent(MailService.this, nextTime, i); - } } + , K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId); + } + else + { + nextCheck = -1; + if (K9.DEBUG) + Log.i(K9.LOG_TAG, "No connectivity, canceling check for " + getApplication().getPackageName()); + cancel(); } - , K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId); } private void stopPushers(final Integer startId) @@ -446,5 +457,10 @@ public class MailService extends CoreService return null; } + public static long getNextPollTime() + { + return nextCheck; + } + } diff --git a/src/com/fsck/k9/service/RemoteControlService.java b/src/com/fsck/k9/service/RemoteControlService.java index 8b44dc7ef..a553a1205 100644 --- a/src/com/fsck/k9/service/RemoteControlService.java +++ b/src/com/fsck/k9/service/RemoteControlService.java @@ -143,7 +143,9 @@ public class RemoteControlService extends CoreService || K9RemoteControl.K9_BACKGROUND_OPERATIONS_WHEN_CHECKED.equals(backgroundOps)) { BACKGROUND_OPS newBackgroundOps = BACKGROUND_OPS.valueOf(backgroundOps); - needsPushRestart |= K9.setBackgroundOps(newBackgroundOps); + boolean needsReset = K9.setBackgroundOps(newBackgroundOps); + needsPushRestart |= needsReset; + needsReschedule |= needsReset; } String theme = intent.getStringExtra(K9_THEME);