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()
/**
* 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()

View File

@ -50,16 +50,6 @@ internal class WorkManagerProvider @Inject constructor(
PeriodicWorkRequestBuilder<W>(repeatInterval, repeatIntervalTimeUnit)
.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
*/

View File

@ -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() {

View File

@ -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<SyncWorker>()
.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<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)
.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)
}
}
}

View File

@ -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()
)
}

View File

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

View File

@ -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?
}