Stop the sync service when no pushes are running.
Change-Id: If2475f76df93d12a3b8c341a5c34c3bd5743d531
diff --git a/src/com/android/exchange/service/EasPingSyncHandler.java b/src/com/android/exchange/service/EasPingSyncHandler.java
index 1439cbe..baaf6ea 100644
--- a/src/com/android/exchange/service/EasPingSyncHandler.java
+++ b/src/com/android/exchange/service/EasPingSyncHandler.java
@@ -43,14 +43,14 @@
private static final String WHERE_ACCOUNT_KEY_AND_SERVER_ID =
MailboxColumns.ACCOUNT_KEY + "=? and " + MailboxColumns.SERVER_ID + "=?";
- private final EmailSyncAdapterService.SyncHandlerSychronizer mSyncHandlerMap;
+ private final EmailSyncAdapterService.SyncHandlerSynchronizer mSyncHandlerMap;
// TODO: The old code used to increase the heartbeat time after successful pings. Is there
// a good reason to not just start at the high value? If there's a problem, it'll just fail
// early anyway...
private static final long PING_HEARTBEAT = 8 * DateUtils.MINUTE_IN_MILLIS;
- private PingTask(final EmailSyncAdapterService.SyncHandlerSychronizer syncHandlerMap) {
+ private PingTask(final EmailSyncAdapterService.SyncHandlerSynchronizer syncHandlerMap) {
mSyncHandlerMap = syncHandlerMap;
}
@@ -286,7 +286,7 @@
}
public EasPingSyncHandler(final Context context, final Account account,
- final EmailSyncAdapterService.SyncHandlerSychronizer syncHandlerMap) {
+ final EmailSyncAdapterService.SyncHandlerSynchronizer syncHandlerMap) {
super(context, account, HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv));
mContentResolver = context.getContentResolver();
mPingTask = new PingTask(syncHandlerMap);
diff --git a/src/com/android/exchange/service/EmailSyncAdapterService.java b/src/com/android/exchange/service/EmailSyncAdapterService.java
index 4619d09..ae96c72 100644
--- a/src/com/android/exchange/service/EmailSyncAdapterService.java
+++ b/src/com/android/exchange/service/EmailSyncAdapterService.java
@@ -80,7 +80,7 @@
* - As an optimization, while a ping request is waiting to run, subsequent ping requests are
* ignored (the pending ping will pick up the latest ping parameters at the time it runs).
*/
- public class SyncHandlerSychronizer {
+ public class SyncHandlerSynchronizer {
/**
* Map of account id -> ping handler.
* For a given account id, there are three possible states:
@@ -126,6 +126,15 @@
return (mPingHandlers.containsKey(accountId) && mPingHandlers.get(accountId) == null);
}
+ private void stopServiceIfNoPings() {
+ for (final EasPingSyncHandler pingHandler : mPingHandlers.values()) {
+ if (pingHandler != null) {
+ return;
+ }
+ }
+ EmailSyncAdapterService.this.stopSelf();
+ }
+
/**
* Called prior to starting a sync to update our state.
* @param accountId The account on which we are running a sync.
@@ -165,7 +174,11 @@
// No ping or sync running. Figure out whether a ping is needed, and if so with
// what params.
final Account account = Account.restoreAccountWithId(context, accountId);
- if (account.mSyncInterval == Account.CHECK_INTERVAL_PUSH) {
+ if (account == null || account.mSyncInterval != Account.CHECK_INTERVAL_PUSH) {
+ // A ping that was running is no longer running, or something happened to the
+ // account.
+ stopServiceIfNoPings();
+ } else {
// Note: unlike startSync, we CANNOT allow the caller to do the actual work.
// If we return before the ping starts, there's a race condition where another
// ping or sync might start first. It only works for startSync because sync is
@@ -203,6 +216,9 @@
// block.
if (wasSync) {
modifyPing(accountId);
+ } else {
+ // A ping stopped, so check if we should stop the service.
+ stopServiceIfNoPings();
}
// Similarly, it's ok to notify after we restart the ping, because we know the ping
@@ -212,7 +228,7 @@
}
}
}
- private final SyncHandlerSychronizer mSyncHandlerMap = new SyncHandlerSychronizer();
+ private final SyncHandlerSynchronizer mSyncHandlerMap = new SyncHandlerSynchronizer();
/**
* The binder for IEmailService.