From 56d5a38e80d086821855c8d9759c8c6d6fb04136 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 22 Oct 2021 10:39:52 +0100 Subject: [PATCH] reverting parts of the rapid periodic sync, unfortunately it suffers from the same issue as the one shot workers - the system can ignore them if the application process is in the background --- .../matrix/android/sdk/api/session/Session.kt | 7 +++-- .../sdk/internal/di/WorkManagerProvider.kt | 10 ------- .../sdk/internal/session/DefaultSession.kt | 4 +-- .../internal/session/sync/job/SyncWorker.kt | 29 +++---------------- .../app/fdroid/BackgroundSyncStarter.kt | 5 ---- .../java/im/vector/app/push/fcm/FcmHelper.kt | 1 - .../features/call/webrtc/WebRtcCallManager.kt | 2 +- 7 files changed, 11 insertions(+), 47 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt index 74b3876177..fb425399d0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt @@ -120,10 +120,11 @@ interface Session : fun requireBackgroundSync() /** - * Launches infinite periodic background syncs - * Doze mode will probably interrupt rapid sync, but it should start up again the next time we run a periodic sync + * Launches infinite self rescheduling background syncs + * This does not work in doze mode :/ + * If battery optimization is on it can work in app standby but that's all :/ */ - fun startAutomaticBackgroundSync(timeOutInSeconds: Long, rapidSync: Boolean, repeatDelayInSeconds: Long) + fun startAutomaticBackgroundSync(timeOutInSeconds: Long, repeatDelayInSeconds: Long) fun stopAnyBackgroundSync() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/WorkManagerProvider.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/WorkManagerProvider.kt index 2fef48e17b..bafffdf852 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/WorkManagerProvider.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/WorkManagerProvider.kt @@ -50,16 +50,6 @@ internal class WorkManagerProvider @Inject constructor( PeriodicWorkRequestBuilder(repeatInterval, repeatIntervalTimeUnit) .addTag(tag) - /** - * Create a PeriodicWorkRequestBuilder, with the Matrix SDK tag - */ - inline fun matrixPeriodicWorkRequestBuilder(repeatInterval: Long, - repeatIntervalTimeUnit: TimeUnit, - flexTimeInterval: Long, - flexTimeIntervalUnit: TimeUnit) = - PeriodicWorkRequestBuilder(repeatInterval, repeatIntervalTimeUnit, flexTimeInterval, flexTimeIntervalUnit) - .addTag(tag) - /** * Cancel all works instantiated by the Matrix SDK for the current session, and not those from the SDK client, or for other sessions */ diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index 8b89ee04cc..c52462612a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -184,8 +184,8 @@ internal class DefaultSession @Inject constructor( SyncWorker.requireBackgroundSync(workManagerProvider, sessionId) } - override fun startAutomaticBackgroundSync(timeOutInSeconds: Long, rapidSync: Boolean, repeatDelayInSeconds: Long) { - SyncWorker.automaticallyPeriodicBackgroundSync(workManagerProvider, sessionId, timeOutInSeconds, rapidSync, repeatDelayInSeconds) + override fun startAutomaticBackgroundSync(timeOutInSeconds: Long, repeatDelayInSeconds: Long) { + SyncWorker.automaticallyBackgroundSync(workManagerProvider, sessionId, timeOutInSeconds, repeatDelayInSeconds) } override fun stopAnyBackgroundSync() { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/job/SyncWorker.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/job/SyncWorker.kt index bcc9f4d995..41bb1a44a6 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/job/SyncWorker.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/job/SyncWorker.kt @@ -17,9 +17,7 @@ package org.matrix.android.sdk.internal.session.sync.job import android.content.Context import androidx.work.BackoffPolicy -import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.ExistingWorkPolicy -import androidx.work.WorkRequest import androidx.work.WorkerParameters import com.squareup.moshi.JsonClass import org.matrix.android.sdk.api.failure.isTokenError @@ -75,7 +73,7 @@ internal class SyncWorker(context: Context, Result.success().also { if (params.periodic) { // we want to schedule another one after delay - automaticallyRapidBackgroundSync(workManagerProvider, params.sessionId, params.timeout, params.delay) + automaticallyBackgroundSync(workManagerProvider, params.sessionId, params.timeout, params.delay) } } }, @@ -103,8 +101,6 @@ internal class SyncWorker(context: Context, companion object { private const val BG_SYNC_WORK_NAME = "BG_SYNCP" - private const val BG_RAPID_SYNC_WORK_NAME = "BG_RAPID_SYNCP" - private const val BG_PERIODIC_SYNC_WORK_NAME = "BG_PERIODIC_SYNCP" fun requireBackgroundSync(workManagerProvider: WorkManagerProvider, sessionId: String, serverTimeout: Long = 0) { val data = WorkerParamsFactory.toData(Params(sessionId, serverTimeout, 0L, false)) @@ -113,12 +109,11 @@ internal class SyncWorker(context: Context, .setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS) .setInputData(data) .build() - // If we've already scheduled a sync that's not yet run, defer to the existing one workManagerProvider.workManager - .enqueueUniqueWork(BG_SYNC_WORK_NAME, ExistingWorkPolicy.KEEP, workRequest) + .enqueueUniqueWork(BG_SYNC_WORK_NAME, ExistingWorkPolicy.APPEND_OR_REPLACE, workRequest) } - fun automaticallyRapidBackgroundSync(workManagerProvider: WorkManagerProvider, sessionId: String, serverTimeout: Long = 0, delayInSeconds: Long = 30) { + fun automaticallyBackgroundSync(workManagerProvider: WorkManagerProvider, sessionId: String, serverTimeout: Long = 0, delayInSeconds: Long = 30) { val data = WorkerParamsFactory.toData(Params(sessionId, serverTimeout, delayInSeconds, true)) val workRequest = workManagerProvider.matrixOneTimeWorkRequestBuilder() .setConstraints(WorkManagerProvider.workConstraints) @@ -128,28 +123,12 @@ internal class SyncWorker(context: Context, .build() // Avoid risking multiple chains of syncs by replacing the existing chain workManagerProvider.workManager - .enqueueUniqueWork(BG_RAPID_SYNC_WORK_NAME, ExistingWorkPolicy.REPLACE, workRequest) - } - - fun automaticallyPeriodicBackgroundSync(workManagerProvider: WorkManagerProvider, sessionId: String, serverTimeout: Long = 0, restartRapidSync: Boolean = true, delayInSeconds: Long = 30) { - val data = WorkerParamsFactory.toData(Params(sessionId, serverTimeout, delayInSeconds, restartRapidSync)) - val workRequest = workManagerProvider.matrixPeriodicWorkRequestBuilder(1, TimeUnit.HOURS, 15, TimeUnit.MINUTES) - .setConstraints(WorkManagerProvider.workConstraints) - .setInputData(data) - .setBackoffCriteria(BackoffPolicy.LINEAR, WorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS) - .build() - - workManagerProvider.workManager - .enqueueUniquePeriodicWork(BG_PERIODIC_SYNC_WORK_NAME, ExistingPeriodicWorkPolicy.KEEP, workRequest) + .enqueueUniqueWork(BG_SYNC_WORK_NAME, ExistingWorkPolicy.REPLACE, workRequest) } fun stopAnyBackgroundSync(workManagerProvider: WorkManagerProvider) { workManagerProvider.workManager .cancelUniqueWork(BG_SYNC_WORK_NAME) - workManagerProvider.workManager - .cancelUniqueWork(BG_RAPID_SYNC_WORK_NAME) - workManagerProvider.workManager - .cancelUniqueWork(BG_PERIODIC_SYNC_WORK_NAME) } } } diff --git a/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt b/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt index f952fe6abe..0fe89e7fe5 100644 --- a/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt +++ b/vector/src/fdroid/java/im/vector/app/fdroid/BackgroundSyncStarter.kt @@ -19,13 +19,9 @@ package im.vector.app.fdroid import android.content.Context import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.fdroid.receiver.AlarmSyncBroadcastReceiver -import im.vector.app.fdroid.service.GuardService import im.vector.app.features.settings.BackgroundSyncMode import im.vector.app.features.settings.VectorPreferences import timber.log.Timber -import android.content.Intent -import androidx.core.content.ContextCompat -import org.matrix.android.sdk.internal.session.sync.job.SyncService object BackgroundSyncStarter { fun start(context: Context, vectorPreferences: VectorPreferences, activeSessionHolder: ActiveSessionHolder) { @@ -37,7 +33,6 @@ object BackgroundSyncStarter { Timber.i("## Sync: Work scheduled to periodically sync in ${vectorPreferences.backgroundSyncDelay()}s") activeSession.startAutomaticBackgroundSync( vectorPreferences.backgroundSyncTimeOut().toLong(), - true, vectorPreferences.backgroundSyncDelay().toLong() ) } diff --git a/vector/src/gplay/java/im/vector/app/push/fcm/FcmHelper.kt b/vector/src/gplay/java/im/vector/app/push/fcm/FcmHelper.kt index d4f2b0dd57..b11e86507d 100755 --- a/vector/src/gplay/java/im/vector/app/push/fcm/FcmHelper.kt +++ b/vector/src/gplay/java/im/vector/app/push/fcm/FcmHelper.kt @@ -111,7 +111,6 @@ object FcmHelper { val activeSession = activeSessionHolder.getSafeActiveSession() ?: return activeSession.startAutomaticBackgroundSync( vectorPreferences.backgroundSyncTimeOut().toLong(), - false, vectorPreferences.backgroundSyncDelay().toLong() ) } diff --git a/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt b/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt index 828dbc21e3..9e620174f3 100644 --- a/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt +++ b/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt @@ -373,7 +373,7 @@ class WebRtcCallManager @Inject constructor( if (isInBackground) { if (FcmHelper.isPushSupported()) { // only for push version as fdroid version is already doing it? - currentSession?.startAutomaticBackgroundSync(30, true, 0) + currentSession?.startAutomaticBackgroundSync(30, 0) } else { // Maybe increase sync freq? but how to set back to default values? }