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
This commit is contained in:
Adam Brown 2021-10-22 10:39:52 +01:00
parent 71b27bfd5d
commit 56d5a38e80
7 changed files with 11 additions and 47 deletions

View File

@ -120,10 +120,11 @@ interface Session :
fun requireBackgroundSync() fun requireBackgroundSync()
/** /**
* Launches infinite periodic background syncs * Launches infinite self rescheduling background syncs
* Doze mode will probably interrupt rapid sync, but it should start up again the next time we run a periodic sync * 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() fun stopAnyBackgroundSync()

View File

@ -50,16 +50,6 @@ internal class WorkManagerProvider @Inject constructor(
PeriodicWorkRequestBuilder<W>(repeatInterval, repeatIntervalTimeUnit) PeriodicWorkRequestBuilder<W>(repeatInterval, repeatIntervalTimeUnit)
.addTag(tag) .addTag(tag)
/**
* Create a PeriodicWorkRequestBuilder, with the Matrix SDK tag
*/
inline fun <reified W : ListenableWorker> matrixPeriodicWorkRequestBuilder(repeatInterval: Long,
repeatIntervalTimeUnit: TimeUnit,
flexTimeInterval: Long,
flexTimeIntervalUnit: TimeUnit) =
PeriodicWorkRequestBuilder<W>(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 * Cancel all works instantiated by the Matrix SDK for the current session, and not those from the SDK client, or for other sessions
*/ */

View File

@ -184,8 +184,8 @@ internal class DefaultSession @Inject constructor(
SyncWorker.requireBackgroundSync(workManagerProvider, sessionId) SyncWorker.requireBackgroundSync(workManagerProvider, sessionId)
} }
override fun startAutomaticBackgroundSync(timeOutInSeconds: Long, rapidSync: Boolean, repeatDelayInSeconds: Long) { override fun startAutomaticBackgroundSync(timeOutInSeconds: Long, repeatDelayInSeconds: Long) {
SyncWorker.automaticallyPeriodicBackgroundSync(workManagerProvider, sessionId, timeOutInSeconds, rapidSync, repeatDelayInSeconds) SyncWorker.automaticallyBackgroundSync(workManagerProvider, sessionId, timeOutInSeconds, repeatDelayInSeconds)
} }
override fun stopAnyBackgroundSync() { override fun stopAnyBackgroundSync() {

View File

@ -17,9 +17,7 @@ package org.matrix.android.sdk.internal.session.sync.job
import android.content.Context import android.content.Context
import androidx.work.BackoffPolicy import androidx.work.BackoffPolicy
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.ExistingWorkPolicy import androidx.work.ExistingWorkPolicy
import androidx.work.WorkRequest
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import com.squareup.moshi.JsonClass import com.squareup.moshi.JsonClass
import org.matrix.android.sdk.api.failure.isTokenError import org.matrix.android.sdk.api.failure.isTokenError
@ -75,7 +73,7 @@ internal class SyncWorker(context: Context,
Result.success().also { Result.success().also {
if (params.periodic) { if (params.periodic) {
// we want to schedule another one after delay // 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 { companion object {
private const val BG_SYNC_WORK_NAME = "BG_SYNCP" 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) { fun requireBackgroundSync(workManagerProvider: WorkManagerProvider, sessionId: String, serverTimeout: Long = 0) {
val data = WorkerParamsFactory.toData(Params(sessionId, serverTimeout, 0L, false)) 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) .setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
.setInputData(data) .setInputData(data)
.build() .build()
// If we've already scheduled a sync that's not yet run, defer to the existing one
workManagerProvider.workManager 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 data = WorkerParamsFactory.toData(Params(sessionId, serverTimeout, delayInSeconds, true))
val workRequest = workManagerProvider.matrixOneTimeWorkRequestBuilder<SyncWorker>() val workRequest = workManagerProvider.matrixOneTimeWorkRequestBuilder<SyncWorker>()
.setConstraints(WorkManagerProvider.workConstraints) .setConstraints(WorkManagerProvider.workConstraints)
@ -128,28 +123,12 @@ internal class SyncWorker(context: Context,
.build() .build()
// Avoid risking multiple chains of syncs by replacing the existing chain // Avoid risking multiple chains of syncs by replacing the existing chain
workManagerProvider.workManager workManagerProvider.workManager
.enqueueUniqueWork(BG_RAPID_SYNC_WORK_NAME, ExistingWorkPolicy.REPLACE, workRequest) .enqueueUniqueWork(BG_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<SyncWorker>(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)
} }
fun stopAnyBackgroundSync(workManagerProvider: WorkManagerProvider) { fun stopAnyBackgroundSync(workManagerProvider: WorkManagerProvider) {
workManagerProvider.workManager workManagerProvider.workManager
.cancelUniqueWork(BG_SYNC_WORK_NAME) .cancelUniqueWork(BG_SYNC_WORK_NAME)
workManagerProvider.workManager
.cancelUniqueWork(BG_RAPID_SYNC_WORK_NAME)
workManagerProvider.workManager
.cancelUniqueWork(BG_PERIODIC_SYNC_WORK_NAME)
} }
} }
} }

View File

@ -19,13 +19,9 @@ package im.vector.app.fdroid
import android.content.Context import android.content.Context
import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.fdroid.receiver.AlarmSyncBroadcastReceiver 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.BackgroundSyncMode
import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.settings.VectorPreferences
import timber.log.Timber 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 { object BackgroundSyncStarter {
fun start(context: Context, vectorPreferences: VectorPreferences, activeSessionHolder: ActiveSessionHolder) { 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") Timber.i("## Sync: Work scheduled to periodically sync in ${vectorPreferences.backgroundSyncDelay()}s")
activeSession.startAutomaticBackgroundSync( activeSession.startAutomaticBackgroundSync(
vectorPreferences.backgroundSyncTimeOut().toLong(), vectorPreferences.backgroundSyncTimeOut().toLong(),
true,
vectorPreferences.backgroundSyncDelay().toLong() vectorPreferences.backgroundSyncDelay().toLong()
) )
} }

View File

@ -111,7 +111,6 @@ object FcmHelper {
val activeSession = activeSessionHolder.getSafeActiveSession() ?: return val activeSession = activeSessionHolder.getSafeActiveSession() ?: return
activeSession.startAutomaticBackgroundSync( activeSession.startAutomaticBackgroundSync(
vectorPreferences.backgroundSyncTimeOut().toLong(), vectorPreferences.backgroundSyncTimeOut().toLong(),
false,
vectorPreferences.backgroundSyncDelay().toLong() vectorPreferences.backgroundSyncDelay().toLong()
) )
} }

View File

@ -373,7 +373,7 @@ class WebRtcCallManager @Inject constructor(
if (isInBackground) { if (isInBackground) {
if (FcmHelper.isPushSupported()) { if (FcmHelper.isPushSupported()) {
// only for push version as fdroid version is already doing it? // only for push version as fdroid version is already doing it?
currentSession?.startAutomaticBackgroundSync(30, true, 0) currentSession?.startAutomaticBackgroundSync(30, 0)
} else { } else {
// Maybe increase sync freq? but how to set back to default values? // Maybe increase sync freq? but how to set back to default values?
} }