mirror of https://github.com/M66B/FairEmail.git
Reconnect backup takes precedence
This commit is contained in:
parent
66ad968672
commit
4daba377a8
|
@ -818,7 +818,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
if (ago < RECONNECT_BACKOFF)
|
if (ago < RECONNECT_BACKOFF)
|
||||||
try {
|
try {
|
||||||
long backoff = RECONNECT_BACKOFF - ago;
|
long backoff = RECONNECT_BACKOFF - ago;
|
||||||
EntityLog.log(ServiceSynchronize.this, account.name + " backoff=" + (backoff / 1000));
|
EntityLog.log(ServiceSynchronize.this, account.name + " reconnect backoff=" + (backoff / 1000));
|
||||||
state.acquire(backoff);
|
state.acquire(backoff);
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
Log.w(account.name + " backoff " + ex.toString());
|
Log.w(account.name + " backoff " + ex.toString());
|
||||||
|
@ -1523,61 +1523,72 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
|
|
||||||
if (state.isRunning()) {
|
if (state.isRunning()) {
|
||||||
int backoff = state.getBackoff();
|
int backoff = state.getBackoff();
|
||||||
EntityLog.log(this, account.name + " backoff=" + backoff);
|
long cbackoff = RECONNECT_BACKOFF - (new Date().getTime() - lastLost);
|
||||||
if (backoff <= CONNECT_BACKOFF_MAX) {
|
if (cbackoff > backoff) {
|
||||||
// Short back-off period, keep device awake
|
|
||||||
try {
|
try {
|
||||||
state.acquire(backoff * 1000L);
|
EntityLog.log(this, account.name + " reconnect backoff=" + cbackoff);
|
||||||
|
state.acquire(cbackoff * 1000L);
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
Log.w(account.name + " backoff " + ex.toString());
|
Log.w(account.name + " cbackoff " + ex.toString());
|
||||||
}
|
}
|
||||||
|
state.setBackoff(CONNECT_BACKOFF_START);
|
||||||
} else {
|
} else {
|
||||||
// Cancel transient sync operations
|
EntityLog.log(this, account.name + " backoff=" + backoff);
|
||||||
boolean enabled = prefs.getBoolean("enabled", true);
|
if (backoff <= CONNECT_BACKOFF_MAX) {
|
||||||
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
|
// Short back-off period, keep device awake
|
||||||
if (!enabled || account.ondemand || (pollInterval > 0 && !account.poll_exempted)) {
|
|
||||||
List<EntityOperation> syncs = db.operation().getOperations(account.id, EntityOperation.SYNC);
|
|
||||||
if (syncs != null) {
|
|
||||||
for (EntityOperation op : syncs) {
|
|
||||||
db.folder().setFolderSyncState(op.folder, null);
|
|
||||||
db.operation().deleteOperation(op.id);
|
|
||||||
}
|
|
||||||
Log.i(account.name + " cancelled syncs=" + syncs.size());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Long back-off period, let device sleep
|
|
||||||
Intent intent = new Intent(ServiceSynchronize.this, ServiceSynchronize.class);
|
|
||||||
intent.setAction("backoff:" + account.id);
|
|
||||||
PendingIntent pi = PendingIntentCompat.getForegroundService(
|
|
||||||
this, PI_BACKOFF, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
||||||
|
|
||||||
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
|
||||||
try {
|
|
||||||
long trigger = System.currentTimeMillis() + backoff * 1000L;
|
|
||||||
EntityLog.log(this, "### " + account.name + " backoff until=" + new Date(trigger));
|
|
||||||
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, pi);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
wlAccount.release();
|
state.acquire(backoff * 1000L);
|
||||||
state.acquire(2 * backoff * 1000L);
|
|
||||||
Log.i("### " + account.name + " backoff done");
|
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
Log.w(account.name + " backoff " + ex.toString());
|
Log.w(account.name + " backoff " + ex.toString());
|
||||||
} finally {
|
|
||||||
wlAccount.acquire();
|
|
||||||
}
|
}
|
||||||
} finally {
|
} else {
|
||||||
am.cancel(pi);
|
// Cancel transient sync operations
|
||||||
}
|
boolean enabled = prefs.getBoolean("enabled", true);
|
||||||
}
|
int pollInterval = prefs.getInt("poll_interval", DEFAULT_POLL_INTERVAL);
|
||||||
|
if (!enabled || account.ondemand || (pollInterval > 0 && !account.poll_exempted)) {
|
||||||
|
List<EntityOperation> syncs = db.operation().getOperations(account.id, EntityOperation.SYNC);
|
||||||
|
if (syncs != null) {
|
||||||
|
for (EntityOperation op : syncs) {
|
||||||
|
db.folder().setFolderSyncState(op.folder, null);
|
||||||
|
db.operation().deleteOperation(op.id);
|
||||||
|
}
|
||||||
|
Log.i(account.name + " cancelled syncs=" + syncs.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (backoff < CONNECT_BACKOFF_MAX)
|
// Long back-off period, let device sleep
|
||||||
state.setBackoff(backoff * 2);
|
Intent intent = new Intent(ServiceSynchronize.this, ServiceSynchronize.class);
|
||||||
else if (backoff == CONNECT_BACKOFF_MAX)
|
intent.setAction("backoff:" + account.id);
|
||||||
state.setBackoff(CONNECT_BACKOFF_AlARM_START * 60);
|
PendingIntent pi = PendingIntentCompat.getForegroundService(
|
||||||
else if (backoff < CONNECT_BACKOFF_AlARM_MAX * 60)
|
this, PI_BACKOFF, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
state.setBackoff(backoff * 2);
|
|
||||||
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||||
|
try {
|
||||||
|
long trigger = System.currentTimeMillis() + backoff * 1000L;
|
||||||
|
EntityLog.log(this, "### " + account.name + " backoff until=" + new Date(trigger));
|
||||||
|
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, pi);
|
||||||
|
|
||||||
|
try {
|
||||||
|
wlAccount.release();
|
||||||
|
state.acquire(2 * backoff * 1000L);
|
||||||
|
Log.i("### " + account.name + " backoff done");
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
Log.w(account.name + " backoff " + ex.toString());
|
||||||
|
} finally {
|
||||||
|
wlAccount.acquire();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
am.cancel(pi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (backoff < CONNECT_BACKOFF_MAX)
|
||||||
|
state.setBackoff(backoff * 2);
|
||||||
|
else if (backoff == CONNECT_BACKOFF_MAX)
|
||||||
|
state.setBackoff(CONNECT_BACKOFF_AlARM_START * 60);
|
||||||
|
else if (backoff < CONNECT_BACKOFF_AlARM_MAX * 60)
|
||||||
|
state.setBackoff(backoff * 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
currentThread = Thread.currentThread().getId();
|
currentThread = Thread.currentThread().getId();
|
||||||
|
|
Loading…
Reference in New Issue