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.") }
val sessionId = intent.getStringExtra(SyncService.EXTRA_SESSION_ID) ?: return
VectorSyncService.newPeriodicIntent(context, sessionId, vectorPreferences.backgroundSyncTimeOut(), vectorPreferences.backgroundSyncDelay()).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)
}
}
VectorSyncService.newPeriodicIntent(
context = context,
sessionId = sessionId,
syncTimeoutSeconds = vectorPreferences.backgroundSyncTimeOut(),
syncDelaySeconds = vectorPreferences.backgroundSyncDelay(),
isNetworkBack = false
)
.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 {

View file

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

View file

@ -41,27 +41,27 @@ class VectorSyncService : SyncService() {
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 {
it.putExtra(EXTRA_SESSION_ID, sessionId)
it.putExtra(EXTRA_TIMEOUT_SECONDS, timeoutSeconds)
it.putExtra(EXTRA_TIMEOUT_SECONDS, syncTimeoutSeconds)
it.putExtra(EXTRA_PERIODIC, false)
}
}
fun newPeriodicIntent(
context: Context,
sessionId: String,
timeoutSeconds: Int,
delayInSeconds: Int,
networkBack: Boolean = false
): Intent {
fun newPeriodicIntent(context: Context,
sessionId: String,
syncTimeoutSeconds: Int,
syncDelaySeconds: Int,
isNetworkBack: Boolean): Intent {
return Intent(context, VectorSyncService::class.java).also {
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_DELAY_SECONDS, delayInSeconds)
it.putExtra(EXTRA_NETWORK_BACK_RESTART, networkBack)
it.putExtra(EXTRA_DELAY_SECONDS, syncDelaySeconds)
it.putExtra(EXTRA_NETWORK_BACK_RESTART, isNetworkBack)
}
}
@ -154,13 +154,19 @@ class VectorSyncService : SyncService() {
}
private fun Context.rescheduleSyncService(sessionId: String,
timeout: Int,
delay: Int,
syncTimeoutSeconds: Int,
syncDelaySeconds: Int,
isNetworkBack: Boolean) {
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)
startService(periodicIntent)
} else {
@ -169,7 +175,7 @@ private fun Context.rescheduleSyncService(sessionId: String,
} else {
PendingIntent.getService(this, 0, periodicIntent, 0)
}
val firstMillis = System.currentTimeMillis() + delay * 1000L
val firstMillis = System.currentTimeMillis() + syncDelaySeconds * 1000L
val alarmMgr = getSystemService<AlarmManager>()!!
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pendingIntent)