Fix fdroid background sync

This commit is contained in:
ganfra 2020-01-15 19:16:50 +01:00
parent b5fead18fe
commit 11c8c8c2bd
3 changed files with 49 additions and 24 deletions

View File

@ -18,6 +18,7 @@ package im.vector.matrix.android.internal.session.sync.job
import android.app.Service
import android.content.Intent
import android.os.IBinder
import im.vector.matrix.android.BuildConfig
import im.vector.matrix.android.api.Matrix
import im.vector.matrix.android.api.failure.isTokenError
import im.vector.matrix.android.api.session.Session
@ -56,29 +57,15 @@ abstract class SyncService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Timber.i("onStartCommand $intent")
intent?.let {
val matrix = Matrix.getInstance(applicationContext)
val safeSessionId = it.getStringExtra(EXTRA_SESSION_ID) ?: return@let
val sessionComponent = matrix.sessionManager.getSessionComponent(safeSessionId)
?: return@let
session = sessionComponent.session()
sessionId = safeSessionId
syncTask = sessionComponent.syncTask()
isInitialSync = !session.hasAlreadySynced()
networkConnectivityChecker = sessionComponent.networkConnectivityChecker()
taskExecutor = sessionComponent.taskExecutor()
coroutineDispatchers = sessionComponent.coroutineDispatchers()
backgroundDetectionObserver = matrix.backgroundDetectionObserver
if (isRunning.get()) {
Timber.i("Received a start while was already syncing... ignore")
} else {
isRunning.set(true)
serviceScope.launch(coroutineDispatchers.io) {
doSync()
}
}
val isInit = initialize(intent)
if (isInit) {
onStart(isInitialSync)
doSyncIfNotAlreadyRunning()
} else {
// We should start and stop as we have to ensure to call Service.startForeground()
onStart(isInitialSync)
stopMe()
}
onStart(isInitialSync)
// No intent just start the service, an alarm will should call with intent
return START_STICKY
}
@ -98,6 +85,17 @@ abstract class SyncService : Service() {
stopSelf()
}
private fun doSyncIfNotAlreadyRunning() {
if (isRunning.get()) {
Timber.i("Received a start while was already syncing... ignore")
} else {
isRunning.set(true)
serviceScope.launch(coroutineDispatchers.io) {
doSync()
}
}
}
private suspend fun doSync() {
if (!networkConnectivityChecker.hasInternetAccess()) {
Timber.v("No network reschedule to avoid wasting resources")
@ -129,6 +127,33 @@ abstract class SyncService : Service() {
}
}
private fun initialize(intent: Intent?): Boolean {
if (intent == null) {
return false
}
val matrix = Matrix.getInstance(applicationContext)
val safeSessionId = intent.getStringExtra(EXTRA_SESSION_ID) ?: return false
try {
val sessionComponent = matrix.sessionManager.getSessionComponent(safeSessionId)
?: throw IllegalStateException("You should have a session to make it work")
session = sessionComponent.session()
sessionId = safeSessionId
syncTask = sessionComponent.syncTask()
isInitialSync = !session.hasAlreadySynced()
networkConnectivityChecker = sessionComponent.networkConnectivityChecker()
taskExecutor = sessionComponent.taskExecutor()
coroutineDispatchers = sessionComponent.coroutineDispatchers()
backgroundDetectionObserver = matrix.backgroundDetectionObserver
return true
} catch (exception: Exception) {
if (BuildConfig.DEBUG) {
throw exception
}
Timber.e(exception, "An exception occurred during initialisation")
return false
}
}
abstract fun onStart(isInitialSync: Boolean)
abstract fun onRescheduleAsked(sessionId: String, isInitialSync: Boolean, delay: Long)

View File

@ -31,7 +31,7 @@ class OnApplicationUpgradeOrRebootReceiver : BroadcastReceiver() {
if (appContext is HasVectorInjector) {
val activeSession = appContext.injector().activeSessionHolder().getSafeActiveSession()
if (activeSession != null) {
AlarmSyncBroadcastReceiver.scheduleAlarm(context, activeSession.myUserId, 10)
AlarmSyncBroadcastReceiver.scheduleAlarm(context, activeSession.sessionId, 10)
}
}
}

View File

@ -69,7 +69,7 @@ object FcmHelper {
// We need to use alarm in this mode
if (vectorPreferences.areNotificationEnabledForDevice() && activeSessionHolder.hasActiveSession()) {
val currentSession = activeSessionHolder.getActiveSession()
AlarmSyncBroadcastReceiver.scheduleAlarm(context, currentSession.myUserId, 4_000L)
AlarmSyncBroadcastReceiver.scheduleAlarm(context, currentSession.sessionId, 4_000L)
Timber.i("Alarm scheduled to restart service")
}
}