When network is back, do an immediate sync

This commit is contained in:
Benoit Marty 2021-01-12 13:56:06 +01:00 committed by Benoit Marty
parent 609ceb7fa4
commit b20bbc1295
1 changed files with 16 additions and 10 deletions

View File

@ -159,16 +159,22 @@ private fun Context.rescheduleSyncService(sessionId: String,
isNetworkBack: Boolean) {
Timber.d("## Sync: rescheduleSyncService")
val periodicIntent = VectorSyncService.newPeriodicIntent(this, sessionId, timeout, delay, isNetworkBack)
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PendingIntent.getForegroundService(this, 0, periodicIntent, 0)
if (isNetworkBack || delay == 0) {
// Do not wait, do the sync now (more reactivity if network back is due to user action)
startService(periodicIntent)
} else {
PendingIntent.getService(this, 0, periodicIntent, 0)
}
val firstMillis = System.currentTimeMillis() + delay * 1000L
val alarmMgr = getSystemService<AlarmManager>()!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pendingIntent)
} else {
alarmMgr.set(AlarmManager.RTC_WAKEUP, firstMillis, pendingIntent)
val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PendingIntent.getForegroundService(this, 0, periodicIntent, 0)
} else {
PendingIntent.getService(this, 0, periodicIntent, 0)
}
val firstMillis = System.currentTimeMillis() + delay * 1000L
val alarmMgr = getSystemService<AlarmManager>()!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pendingIntent)
} else {
alarmMgr.set(AlarmManager.RTC_WAKEUP, firstMillis, pendingIntent)
}
}
}