Periodic sync to re-kick-off rapid sync
This commit is contained in:
parent
d13e30b3be
commit
d261dd705d
|
@ -121,10 +121,9 @@ interface Session :
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Launches infinite periodic background syncs
|
* Launches infinite periodic background syncs
|
||||||
* This does not work in doze mode :/
|
* Doze mode will probably interrupt rapid sync, but it should start up again the next time we run a periodic sync
|
||||||
* If battery optimization is on it can work in app standby but that's all :/
|
|
||||||
*/
|
*/
|
||||||
fun startAutomaticBackgroundSync(timeOutInSeconds: Long, repeatDelayInSeconds: Long)
|
fun startAutomaticBackgroundSync(timeOutInSeconds: Long, rapidSync: Boolean, repeatDelayInSeconds: Long)
|
||||||
|
|
||||||
fun stopAnyBackgroundSync()
|
fun stopAnyBackgroundSync()
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,16 @@ 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
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -184,8 +184,8 @@ internal class DefaultSession @Inject constructor(
|
||||||
SyncWorker.requireBackgroundSync(workManagerProvider, sessionId)
|
SyncWorker.requireBackgroundSync(workManagerProvider, sessionId)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun startAutomaticBackgroundSync(timeOutInSeconds: Long, repeatDelayInSeconds: Long) {
|
override fun startAutomaticBackgroundSync(timeOutInSeconds: Long, rapidSync: Boolean, repeatDelayInSeconds: Long) {
|
||||||
SyncWorker.automaticallyBackgroundSync(workManagerProvider, sessionId, timeOutInSeconds, repeatDelayInSeconds)
|
SyncWorker.automaticallyPeriodicBackgroundSync(workManagerProvider, sessionId, timeOutInSeconds, rapidSync, repeatDelayInSeconds)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun stopAnyBackgroundSync() {
|
override fun stopAnyBackgroundSync() {
|
||||||
|
|
|
@ -17,7 +17,9 @@ 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
|
||||||
|
@ -73,7 +75,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
|
||||||
automaticallyBackgroundSync(workManagerProvider, params.sessionId, params.timeout, params.delay)
|
automaticallyRapidBackgroundSync(workManagerProvider, params.sessionId, params.timeout, params.delay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -101,6 +103,8 @@ 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))
|
||||||
|
@ -109,11 +113,12 @@ 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.APPEND_OR_REPLACE, workRequest)
|
.enqueueUniqueWork(BG_SYNC_WORK_NAME, ExistingWorkPolicy.KEEP, workRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun automaticallyBackgroundSync(workManagerProvider: WorkManagerProvider, sessionId: String, serverTimeout: Long = 0, delayInSeconds: Long = 30) {
|
fun automaticallyRapidBackgroundSync(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)
|
||||||
|
@ -121,14 +126,30 @@ internal class SyncWorker(context: Context,
|
||||||
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
|
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
|
||||||
.setInitialDelay(delayInSeconds, TimeUnit.SECONDS)
|
.setInitialDelay(delayInSeconds, TimeUnit.SECONDS)
|
||||||
.build()
|
.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
|
workManagerProvider.workManager
|
||||||
.enqueueUniqueWork(BG_SYNC_WORK_NAME, ExistingWorkPolicy.APPEND_OR_REPLACE, workRequest)
|
.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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ 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()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,6 +108,11 @@ object FcmHelper {
|
||||||
|
|
||||||
@Suppress("UNUSED_PARAMETER")
|
@Suppress("UNUSED_PARAMETER")
|
||||||
fun onEnterBackground(context: Context, vectorPreferences: VectorPreferences, activeSessionHolder: ActiveSessionHolder) {
|
fun onEnterBackground(context: Context, vectorPreferences: VectorPreferences, activeSessionHolder: ActiveSessionHolder) {
|
||||||
// TODO FCM fallback
|
val activeSession = activeSessionHolder.getSafeActiveSession() ?: return
|
||||||
|
activeSession.startAutomaticBackgroundSync(
|
||||||
|
vectorPreferences.backgroundSyncTimeOut().toLong(),
|
||||||
|
false,
|
||||||
|
vectorPreferences.backgroundSyncDelay().toLong()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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, 0)
|
currentSession?.startAutomaticBackgroundSync(30, true, 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?
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue