Rename parameters for clarity

This commit is contained in:
Benoit Marty 2021-01-12 15:21:16 +01:00 committed by Benoit Marty
parent b20bbc1295
commit e771b21ea3
3 changed files with 52 additions and 34 deletions

View file

@ -40,15 +40,22 @@ class AlarmSyncBroadcastReceiver : BroadcastReceiver() {
?: return Unit.also { Timber.v("No active session, so don't launch sync service.") } ?: return Unit.also { Timber.v("No active session, so don't launch sync service.") }
val sessionId = intent.getStringExtra(SyncService.EXTRA_SESSION_ID) ?: return val sessionId = intent.getStringExtra(SyncService.EXTRA_SESSION_ID) ?: return
VectorSyncService.newPeriodicIntent(context, sessionId, vectorPreferences.backgroundSyncTimeOut(), vectorPreferences.backgroundSyncDelay()).let { VectorSyncService.newPeriodicIntent(
try { context = context,
ContextCompat.startForegroundService(context, it) sessionId = sessionId,
} catch (ex: Throwable) { syncTimeoutSeconds = vectorPreferences.backgroundSyncTimeOut(),
Timber.i("## Sync: Failed to start service, Alarm scheduled to restart service") syncDelaySeconds = vectorPreferences.backgroundSyncDelay(),
scheduleAlarm(context, sessionId, vectorPreferences.backgroundSyncDelay()) isNetworkBack = false
Timber.e(ex) )
} .let {
} try {
ContextCompat.startForegroundService(context, it)
} catch (ex: Throwable) {
Timber.i("## Sync: Failed to start service, Alarm scheduled to restart service")
scheduleAlarm(context, sessionId, vectorPreferences.backgroundSyncDelay())
Timber.e(ex)
}
}
} }
companion object { companion object {

View file

@ -38,14 +38,19 @@ fun Session.startSyncing(context: Context) {
val applicationContext = context.applicationContext val applicationContext = context.applicationContext
if (!hasAlreadySynced()) { if (!hasAlreadySynced()) {
// initial sync is done as a service so it can continue below app lifecycle // initial sync is done as a service so it can continue below app lifecycle
VectorSyncService.newOneShotIntent(applicationContext, sessionId, 0).also { VectorSyncService.newOneShotIntent(
try { context = applicationContext,
ContextCompat.startForegroundService(applicationContext, it) sessionId = sessionId,
} catch (ex: Throwable) { syncTimeoutSeconds = 0
// TODO )
Timber.e(ex) .let {
} try {
} ContextCompat.startForegroundService(applicationContext, it)
} catch (ex: Throwable) {
// TODO
Timber.e(ex)
}
}
} else { } else {
val isAtLeastStarted = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED) val isAtLeastStarted = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)
Timber.v("--> is at least started? $isAtLeastStarted") Timber.v("--> is at least started? $isAtLeastStarted")

View file

@ -41,27 +41,27 @@ class VectorSyncService : SyncService() {
companion object { companion object {
fun newOneShotIntent(context: Context, sessionId: String, timeoutSeconds: Int): Intent { fun newOneShotIntent(context: Context,
sessionId: String,
syncTimeoutSeconds: Int): Intent {
return Intent(context, VectorSyncService::class.java).also { return Intent(context, VectorSyncService::class.java).also {
it.putExtra(EXTRA_SESSION_ID, sessionId) it.putExtra(EXTRA_SESSION_ID, sessionId)
it.putExtra(EXTRA_TIMEOUT_SECONDS, timeoutSeconds) it.putExtra(EXTRA_TIMEOUT_SECONDS, syncTimeoutSeconds)
it.putExtra(EXTRA_PERIODIC, false) it.putExtra(EXTRA_PERIODIC, false)
} }
} }
fun newPeriodicIntent( fun newPeriodicIntent(context: Context,
context: Context, sessionId: String,
sessionId: String, syncTimeoutSeconds: Int,
timeoutSeconds: Int, syncDelaySeconds: Int,
delayInSeconds: Int, isNetworkBack: Boolean): Intent {
networkBack: Boolean = false
): Intent {
return Intent(context, VectorSyncService::class.java).also { return Intent(context, VectorSyncService::class.java).also {
it.putExtra(EXTRA_SESSION_ID, sessionId) it.putExtra(EXTRA_SESSION_ID, sessionId)
it.putExtra(EXTRA_TIMEOUT_SECONDS, timeoutSeconds) it.putExtra(EXTRA_TIMEOUT_SECONDS, syncTimeoutSeconds)
it.putExtra(EXTRA_PERIODIC, true) it.putExtra(EXTRA_PERIODIC, true)
it.putExtra(EXTRA_DELAY_SECONDS, delayInSeconds) it.putExtra(EXTRA_DELAY_SECONDS, syncDelaySeconds)
it.putExtra(EXTRA_NETWORK_BACK_RESTART, networkBack) it.putExtra(EXTRA_NETWORK_BACK_RESTART, isNetworkBack)
} }
} }
@ -154,13 +154,19 @@ class VectorSyncService : SyncService() {
} }
private fun Context.rescheduleSyncService(sessionId: String, private fun Context.rescheduleSyncService(sessionId: String,
timeout: Int, syncTimeoutSeconds: Int,
delay: Int, syncDelaySeconds: Int,
isNetworkBack: Boolean) { isNetworkBack: Boolean) {
Timber.d("## Sync: rescheduleSyncService") Timber.d("## Sync: rescheduleSyncService")
val periodicIntent = VectorSyncService.newPeriodicIntent(this, sessionId, timeout, delay, isNetworkBack) val periodicIntent = VectorSyncService.newPeriodicIntent(
context = this,
sessionId = sessionId,
syncTimeoutSeconds = syncTimeoutSeconds,
syncDelaySeconds = syncDelaySeconds,
isNetworkBack = isNetworkBack
)
if (isNetworkBack || delay == 0) { if (isNetworkBack || syncDelaySeconds == 0) {
// Do not wait, do the sync now (more reactivity if network back is due to user action) // Do not wait, do the sync now (more reactivity if network back is due to user action)
startService(periodicIntent) startService(periodicIntent)
} else { } else {
@ -169,7 +175,7 @@ private fun Context.rescheduleSyncService(sessionId: String,
} else { } else {
PendingIntent.getService(this, 0, periodicIntent, 0) PendingIntent.getService(this, 0, periodicIntent, 0)
} }
val firstMillis = System.currentTimeMillis() + delay * 1000L val firstMillis = System.currentTimeMillis() + syncDelaySeconds * 1000L
val alarmMgr = getSystemService<AlarmManager>()!! val alarmMgr = getSystemService<AlarmManager>()!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pendingIntent) alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pendingIntent)