From 11c8c8c2bd8c46d008e5465135912e63d35ec12b Mon Sep 17 00:00:00 2001 From: ganfra Date: Wed, 15 Jan 2020 19:16:50 +0100 Subject: [PATCH] Fix fdroid background sync --- .../internal/session/sync/job/SyncService.kt | 69 +++++++++++++------ .../OnApplicationUpgradeOrRebootReceiver.kt | 2 +- .../im/vector/riotx/push/fcm/FcmHelper.kt | 2 +- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncService.kt index 724b0e1360..df050d7f30 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncService.kt @@ -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) diff --git a/vector/src/fdroid/java/im/vector/riotx/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt b/vector/src/fdroid/java/im/vector/riotx/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt index 207776b530..031c2bc5e7 100644 --- a/vector/src/fdroid/java/im/vector/riotx/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt +++ b/vector/src/fdroid/java/im/vector/riotx/fdroid/receiver/OnApplicationUpgradeOrRebootReceiver.kt @@ -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) } } } diff --git a/vector/src/fdroid/java/im/vector/riotx/push/fcm/FcmHelper.kt b/vector/src/fdroid/java/im/vector/riotx/push/fcm/FcmHelper.kt index bfd8850d00..9b32c8be98 100755 --- a/vector/src/fdroid/java/im/vector/riotx/push/fcm/FcmHelper.kt +++ b/vector/src/fdroid/java/im/vector/riotx/push/fcm/FcmHelper.kt @@ -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") } }