Simplification

pull/169/head
M66B 6 years ago
parent 8a15321e83
commit a5ec0f9f0f

@ -1230,62 +1230,66 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
idlers.clear(); idlers.clear();
} }
if (state.isRunning()) if (state.isRunning()) {
try { if (backoff <= CONNECT_BACKOFF_MAX) {
if (backoff <= CONNECT_BACKOFF_MAX) { // Short back-off period, keep device awake
// Short back-off period, keep device awake EntityLog.log(this, account.name + " backoff=" + backoff);
EntityLog.log(this, account.name + " backoff=" + backoff); try {
state.acquire(backoff * 1000L); state.acquire(backoff * 1000L);
} else { } catch (InterruptedException ex) {
// Long back-off period, let device sleep Log.w(account.name + " backoff " + ex.toString());
EntityLog.log(this, account.name + " backoff alarm=" + CONNECT_BACKOFF_AlARM); }
} else {
// Long back-off period, let device sleep
EntityLog.log(this, account.name + " backoff alarm=" + CONNECT_BACKOFF_AlARM);
BroadcastReceiver alarm = new BroadcastReceiver() { BroadcastReceiver alarm = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
state.release(); state.release();
} }
}; };
String id = BuildConfig.APPLICATION_ID + ".BACKOFF." + account.id + "." + new Random().nextInt(); String id = BuildConfig.APPLICATION_ID + ".BACKOFF." + account.id;
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(id), 0); PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(id), 0);
registerReceiver(alarm, new IntentFilter(id)); registerReceiver(alarm, new IntentFilter(id));
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
try { try {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
am.set( am.set(
AlarmManager.RTC_WAKEUP, AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + CONNECT_BACKOFF_AlARM * 60 * 1000L, System.currentTimeMillis() + CONNECT_BACKOFF_AlARM * 60 * 1000L,
pi); pi);
else else
am.setAndAllowWhileIdle( am.setAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP, AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + CONNECT_BACKOFF_AlARM * 60 * 1000L, System.currentTimeMillis() + CONNECT_BACKOFF_AlARM * 60 * 1000L,
pi); pi);
try { try {
wlAccount.release(); wlAccount.release();
state.acquire(2 * CONNECT_BACKOFF_AlARM * 60 * 1000L); state.acquire(2 * CONNECT_BACKOFF_AlARM * 60 * 1000L);
} finally { } catch (InterruptedException ex) {
wlAccount.acquire(); Log.w(account.name + " backoff " + ex.toString());
}
} finally { } finally {
// Cleanup wlAccount.acquire();
am.cancel(pi); }
try { } finally {
unregisterReceiver(alarm); // Cleanup
} catch (IllegalArgumentException ex) { am.cancel(pi);
Log.e(ex); try {
} unregisterReceiver(alarm);
} catch (IllegalArgumentException ex) {
// Should not happen, but does happen
Log.e(ex);
} }
} }
if (backoff <= CONNECT_BACKOFF_MAX)
backoff *= 2;
} catch (InterruptedException ex) {
Log.w(account.name + " backoff " + ex.toString());
} }
if (backoff <= CONNECT_BACKOFF_MAX)
backoff *= 2;
}
} }
} finally { } finally {
EntityLog.log(this, account.name + " stopped"); EntityLog.log(this, account.name + " stopped");

Loading…
Cancel
Save