From e771b21ea3c0578dfd3cfc06e597d7b98953800a Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 12 Jan 2021 15:21:16 +0100 Subject: [PATCH] Rename parameters for clarity --- .../receiver/AlarmSyncBroadcastReceiver.kt | 25 +++++++----- .../im/vector/app/core/extensions/Session.kt | 21 ++++++---- .../app/core/services/VectorSyncService.kt | 40 +++++++++++-------- 3 files changed, 52 insertions(+), 34 deletions(-) diff --git a/vector/src/fdroid/java/im/vector/app/fdroid/receiver/AlarmSyncBroadcastReceiver.kt b/vector/src/fdroid/java/im/vector/app/fdroid/receiver/AlarmSyncBroadcastReceiver.kt index 1b5c71d952..b94e99208b 100644 --- a/vector/src/fdroid/java/im/vector/app/fdroid/receiver/AlarmSyncBroadcastReceiver.kt +++ b/vector/src/fdroid/java/im/vector/app/fdroid/receiver/AlarmSyncBroadcastReceiver.kt @@ -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 { diff --git a/vector/src/main/java/im/vector/app/core/extensions/Session.kt b/vector/src/main/java/im/vector/app/core/extensions/Session.kt index cb87947612..b8cdd83324 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/Session.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/Session.kt @@ -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") diff --git a/vector/src/main/java/im/vector/app/core/services/VectorSyncService.kt b/vector/src/main/java/im/vector/app/core/services/VectorSyncService.kt index 2f21d53177..c9b72708f8 100644 --- a/vector/src/main/java/im/vector/app/core/services/VectorSyncService.kt +++ b/vector/src/main/java/im/vector/app/core/services/VectorSyncService.kt @@ -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()!! if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, firstMillis, pendingIntent)