mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-02 08:35:08 -04:00
Reworked MailService to cut overhead when executing background tasks
Since MailService uses the auto shutdown mode of CoreService we can't use CoreService.execute() with a startId parameter that is null. So this change should also fix the problem some users reported where the pushers weren't set up correctly. See issue 2777
This commit is contained in:
parent
74f7abaec2
commit
2d1f9f9c84
@ -259,7 +259,7 @@ public class K9 extends Application {
|
||||
|
||||
public static final int PUSH_WAKE_LOCK_TIMEOUT = 60000;
|
||||
|
||||
public static final int MAIL_SERVICE_WAKE_LOCK_TIMEOUT = 30000;
|
||||
public static final int MAIL_SERVICE_WAKE_LOCK_TIMEOUT = 60000;
|
||||
|
||||
public static final int BOOT_RECEIVER_WAKE_LOCK_TIMEOUT = 60000;
|
||||
|
||||
|
@ -138,7 +138,7 @@ public class MailService extends CoreService {
|
||||
if (hasConnectivity && doBackground) {
|
||||
PollService.startService(this);
|
||||
}
|
||||
reschedulePoll(hasConnectivity, doBackground, startId, false);
|
||||
reschedulePollInBackground(hasConnectivity, doBackground, startId, false);
|
||||
} else if (ACTION_CANCEL.equals(intent.getAction())) {
|
||||
if (K9.DEBUG)
|
||||
Log.v(K9.LOG_TAG, "***** MailService *****: cancel");
|
||||
@ -146,26 +146,25 @@ public class MailService extends CoreService {
|
||||
} else if (ACTION_RESET.equals(intent.getAction())) {
|
||||
if (K9.DEBUG)
|
||||
Log.v(K9.LOG_TAG, "***** MailService *****: reschedule");
|
||||
rescheduleAll(hasConnectivity, doBackground, startId);
|
||||
rescheduleAllInBackground(hasConnectivity, doBackground, startId);
|
||||
} else if (ACTION_RESTART_PUSHERS.equals(intent.getAction())) {
|
||||
if (K9.DEBUG)
|
||||
Log.v(K9.LOG_TAG, "***** MailService *****: restarting pushers");
|
||||
reschedulePushers(hasConnectivity, doBackground, startId);
|
||||
reschedulePushersInBackground(hasConnectivity, doBackground, startId);
|
||||
} else if (ACTION_RESCHEDULE_POLL.equals(intent.getAction())) {
|
||||
if (K9.DEBUG)
|
||||
Log.v(K9.LOG_TAG, "***** MailService *****: rescheduling poll");
|
||||
reschedulePoll(hasConnectivity, doBackground, startId, true);
|
||||
reschedulePollInBackground(hasConnectivity, doBackground, startId, true);
|
||||
} else if (ACTION_REFRESH_PUSHERS.equals(intent.getAction())) {
|
||||
if (hasConnectivity && doBackground) {
|
||||
refreshPushers(null);
|
||||
schedulePushers(startId);
|
||||
}
|
||||
refreshPushersInBackground(hasConnectivity, doBackground, startId);
|
||||
} else if (CONNECTIVITY_CHANGE.equals(intent.getAction())) {
|
||||
rescheduleAll(hasConnectivity, doBackground, startId);
|
||||
rescheduleAllInBackground(hasConnectivity, doBackground, startId);
|
||||
if (K9.DEBUG)
|
||||
Log.i(K9.LOG_TAG, "Got connectivity action with hasConnectivity = " + hasConnectivity + ", doBackground = " + doBackground);
|
||||
} else if (CANCEL_CONNECTIVITY_NOTICE.equals(intent.getAction())) {
|
||||
/* do nothing */
|
||||
}
|
||||
|
||||
if (isSyncDisabled() != oldIsSyncDisabled) {
|
||||
MessagingController.getInstance(getApplication()).systemStatusChanged();
|
||||
}
|
||||
@ -176,12 +175,6 @@ public class MailService extends CoreService {
|
||||
Log.i(K9.LOG_TAG, "MailService.onStart took " + (System.currentTimeMillis() - startTime) + "ms");
|
||||
}
|
||||
|
||||
private void rescheduleAll(final boolean hasConnectivity, final boolean doBackground, final Integer startId) {
|
||||
reschedulePoll(hasConnectivity, doBackground, null, true);
|
||||
reschedulePushers(hasConnectivity, doBackground, startId);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if (K9.DEBUG)
|
||||
@ -211,11 +204,65 @@ public class MailService extends CoreService {
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
private void reschedulePoll(final boolean hasConnectivity, final boolean doBackground, Integer startId, final boolean considerLastCheckEnd) {
|
||||
private void rescheduleAllInBackground(final boolean hasConnectivity,
|
||||
final boolean doBackground, Integer startId) {
|
||||
|
||||
execute(getApplication(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
reschedulePoll(hasConnectivity, doBackground, true);
|
||||
reschedulePushers(hasConnectivity, doBackground);
|
||||
}
|
||||
}, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||
}
|
||||
|
||||
private void reschedulePollInBackground(final boolean hasConnectivity,
|
||||
final boolean doBackground, Integer startId, final boolean considerLastCheckEnd) {
|
||||
|
||||
execute(getApplication(), new Runnable() {
|
||||
public void run() {
|
||||
reschedulePoll(hasConnectivity, doBackground, considerLastCheckEnd);
|
||||
}
|
||||
}, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||
}
|
||||
|
||||
private void reschedulePushersInBackground(final boolean hasConnectivity,
|
||||
final boolean doBackground, Integer startId) {
|
||||
|
||||
execute(getApplication(), new Runnable() {
|
||||
public void run() {
|
||||
reschedulePushers(hasConnectivity, doBackground);
|
||||
}
|
||||
}, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||
}
|
||||
|
||||
private void refreshPushersInBackground(boolean hasConnectivity, boolean doBackground,
|
||||
Integer startId) {
|
||||
|
||||
if (hasConnectivity && doBackground) {
|
||||
execute(getApplication(), new Runnable() {
|
||||
public void run() {
|
||||
int shortestInterval = -1;
|
||||
refreshPushers();
|
||||
schedulePushers();
|
||||
}
|
||||
}, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||
}
|
||||
}
|
||||
|
||||
private void reschedulePoll(final boolean hasConnectivity, final boolean doBackground,
|
||||
boolean considerLastCheckEnd) {
|
||||
|
||||
if (!(hasConnectivity && doBackground)) {
|
||||
if (K9.DEBUG) {
|
||||
Log.i(K9.LOG_TAG, "No connectivity, canceling check for " +
|
||||
getApplication().getPackageName());
|
||||
}
|
||||
|
||||
nextCheck = -1;
|
||||
cancel();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Preferences prefs = Preferences.getPreferences(MailService.this);
|
||||
SharedPreferences sPrefs = prefs.getPreferences();
|
||||
@ -223,15 +270,18 @@ public class MailService extends CoreService {
|
||||
long lastCheckEnd = sPrefs.getLong(LAST_CHECK_END, -1);
|
||||
|
||||
if (lastCheckEnd > System.currentTimeMillis()) {
|
||||
Log.i(K9.LOG_TAG, "The database claims that the last time mail was checked was in the future. ("+lastCheckEnd+"). To try to get things back to normal, the last check time has been reset to "+System.currentTimeMillis());
|
||||
Log.i(K9.LOG_TAG, "The database claims that the last time mail was checked was in " +
|
||||
"the future (" + lastCheckEnd + "). To try to get things back to normal, " +
|
||||
"the last check time has been reset to: " + System.currentTimeMillis());
|
||||
lastCheckEnd = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
int shortestInterval = -1;
|
||||
for (Account account : prefs.getAccounts()) {
|
||||
if (account.getAutomaticCheckIntervalMinutes() != -1
|
||||
&& account.getFolderSyncMode() != FolderMode.NONE
|
||||
&& (account.getAutomaticCheckIntervalMinutes() < shortestInterval || shortestInterval == -1)) {
|
||||
if (account.getAutomaticCheckIntervalMinutes() != -1 &&
|
||||
account.getFolderSyncMode() != FolderMode.NONE &&
|
||||
(account.getAutomaticCheckIntervalMinutes() < shortestInterval ||
|
||||
shortestInterval == -1)) {
|
||||
shortestInterval = account.getAutomaticCheckIntervalMinutes();
|
||||
}
|
||||
}
|
||||
@ -240,26 +290,36 @@ public class MailService extends CoreService {
|
||||
editor.commit();
|
||||
|
||||
if (shortestInterval == -1) {
|
||||
if (K9.DEBUG) {
|
||||
Log.i(K9.LOG_TAG, "No next check scheduled for package " +
|
||||
getApplication().getPackageName());
|
||||
}
|
||||
|
||||
nextCheck = -1;
|
||||
pollingRequested = false;
|
||||
if (K9.DEBUG)
|
||||
Log.i(K9.LOG_TAG, "No next check scheduled for package " + getApplication().getPackageName());
|
||||
cancel();
|
||||
} else {
|
||||
long delay = (shortestInterval * (60 * 1000));
|
||||
long base = (previousInterval == -1 || lastCheckEnd == -1 || !considerLastCheckEnd ? System.currentTimeMillis() : lastCheckEnd);
|
||||
long base = (previousInterval == -1 || lastCheckEnd == -1 ||
|
||||
!considerLastCheckEnd ? System.currentTimeMillis() : lastCheckEnd);
|
||||
long nextTime = base + delay;
|
||||
if (K9.DEBUG)
|
||||
Log.i(K9.LOG_TAG,
|
||||
"previousInterval = " + previousInterval
|
||||
+ ", shortestInterval = " + shortestInterval
|
||||
+ ", lastCheckEnd = " + new Date(lastCheckEnd)
|
||||
+ ", considerLastCheckEnd = " + considerLastCheckEnd);
|
||||
|
||||
if (K9.DEBUG) {
|
||||
Log.i(K9.LOG_TAG, "previousInterval = " + previousInterval +
|
||||
", shortestInterval = " + shortestInterval +
|
||||
", lastCheckEnd = " + new Date(lastCheckEnd) +
|
||||
", considerLastCheckEnd = " + considerLastCheckEnd);
|
||||
}
|
||||
|
||||
nextCheck = nextTime;
|
||||
pollingRequested = true;
|
||||
|
||||
try {
|
||||
if (K9.DEBUG)
|
||||
Log.i(K9.LOG_TAG, "Next check for package " + getApplication().getPackageName() + " scheduled for " + new Date(nextTime));
|
||||
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);
|
||||
@ -269,16 +329,6 @@ public class MailService extends CoreService {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -286,39 +336,32 @@ public class MailService extends CoreService {
|
||||
return syncBlocked || (!pollingRequested && !pushingRequested);
|
||||
}
|
||||
|
||||
private void stopPushers(final Integer startId) {
|
||||
execute(getApplication(), new Runnable() {
|
||||
public void run() {
|
||||
private void stopPushers() {
|
||||
MessagingController.getInstance(getApplication()).stopAllPushing();
|
||||
PushService.stopService(MailService.this);
|
||||
}
|
||||
}
|
||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||
}
|
||||
|
||||
private void reschedulePushers(final boolean hasConnectivity, final boolean doBackground, final Integer startId) {
|
||||
execute(getApplication(), new Runnable() {
|
||||
public void run() {
|
||||
if (K9.DEBUG)
|
||||
Log.i(K9.LOG_TAG, "Rescheduling pushers");
|
||||
stopPushers(null);
|
||||
if (hasConnectivity && doBackground) {
|
||||
setupPushers(null);
|
||||
schedulePushers(startId);
|
||||
} else {
|
||||
private void reschedulePushers(boolean hasConnectivity, boolean doBackground) {
|
||||
if (K9.DEBUG) {
|
||||
Log.i(K9.LOG_TAG, "Not scheduling pushers: connectivity? " + hasConnectivity + " -- doBackground? " + doBackground);
|
||||
}
|
||||
Log.i(K9.LOG_TAG, "Rescheduling pushers");
|
||||
}
|
||||
|
||||
stopPushers();
|
||||
|
||||
if (!(hasConnectivity && doBackground)) {
|
||||
if (K9.DEBUG) {
|
||||
Log.i(K9.LOG_TAG, "Not scheduling pushers: connectivity? " + hasConnectivity +
|
||||
" -- doBackground? " + doBackground);
|
||||
}
|
||||
}
|
||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, null);
|
||||
return;
|
||||
}
|
||||
|
||||
private void setupPushers(final Integer startId) {
|
||||
execute(getApplication(), new Runnable() {
|
||||
public void run() {
|
||||
setupPushers();
|
||||
schedulePushers();
|
||||
}
|
||||
|
||||
|
||||
private void setupPushers() {
|
||||
boolean pushing = false;
|
||||
for (Account account : Preferences.getPreferences(MailService.this).getAccounts()) {
|
||||
if (K9.DEBUG)
|
||||
@ -334,13 +377,8 @@ public class MailService extends CoreService {
|
||||
}
|
||||
pushingRequested = pushing;
|
||||
}
|
||||
}
|
||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||
}
|
||||
|
||||
private void refreshPushers(final Integer startId) {
|
||||
execute(getApplication(), new Runnable() {
|
||||
public void run() {
|
||||
private void refreshPushers() {
|
||||
try {
|
||||
long nowTime = System.currentTimeMillis();
|
||||
if (K9.DEBUG)
|
||||
@ -375,13 +413,8 @@ public class MailService extends CoreService {
|
||||
Log.e(K9.LOG_TAG, "Exception while refreshing pushers", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||
}
|
||||
|
||||
private void schedulePushers(final Integer startId) {
|
||||
execute(getApplication(), new Runnable() {
|
||||
public void run() {
|
||||
private void schedulePushers() {
|
||||
int minInterval = -1;
|
||||
|
||||
Collection<Pusher> pushers = MessagingController.getInstance(getApplication()).getPushers();
|
||||
@ -404,13 +437,11 @@ public class MailService extends CoreService {
|
||||
BootReceiver.scheduleIntent(MailService.this, nextTime, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IBinder onBind(@SuppressWarnings("unused") Intent intent) {
|
||||
public IBinder onBind(Intent intent) {
|
||||
// Unused
|
||||
return null;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user