diff --git a/vector-app/src/fdroid/java/im/vector/app/push/fcm/FdroidFcmHelper.kt b/vector-app/src/fdroid/java/im/vector/app/push/fcm/FdroidFcmHelper.kt index 44fd92953e..ef50f13a2d 100755 --- a/vector-app/src/fdroid/java/im/vector/app/push/fcm/FdroidFcmHelper.kt +++ b/vector-app/src/fdroid/java/im/vector/app/push/fcm/FdroidFcmHelper.kt @@ -49,7 +49,9 @@ class FdroidFcmHelper @Inject constructor( override fun onEnterForeground(activeSessionHolder: ActiveSessionHolder) { // try to stop all regardless of background mode - activeSessionHolder.getSafeActiveSession()?.syncService()?.stopAnyBackgroundSync() + activeSessionHolder.getSafeActiveSessionAsync { + it?.syncService()?.stopAnyBackgroundSync() + } AlarmSyncBroadcastReceiver.cancelAlarm(context) } diff --git a/vector-app/src/main/java/im/vector/app/VectorApplication.kt b/vector-app/src/main/java/im/vector/app/VectorApplication.kt index 1dbbc17403..0108f381d7 100644 --- a/vector-app/src/main/java/im/vector/app/VectorApplication.kt +++ b/vector-app/src/main/java/im/vector/app/VectorApplication.kt @@ -190,9 +190,12 @@ class VectorApplication : override fun onResume(owner: LifecycleOwner) { Timber.i("App entered foreground") fcmHelper.onEnterForeground(activeSessionHolder) - activeSessionHolder.getSafeActiveSession()?.also { - it.syncService().stopAnyBackgroundSync() + activeSessionHolder.getSafeActiveSessionAsync { + it?.syncService()?.stopAnyBackgroundSync() } +// activeSessionHolder.getSafeActiveSession()?.also { +// it.syncService().stopAnyBackgroundSync() +// } } override fun onPause(owner: LifecycleOwner) { diff --git a/vector/src/main/java/im/vector/app/core/di/ActiveSessionHolder.kt b/vector/src/main/java/im/vector/app/core/di/ActiveSessionHolder.kt index e698f65bf4..4a212e5a91 100644 --- a/vector/src/main/java/im/vector/app/core/di/ActiveSessionHolder.kt +++ b/vector/src/main/java/im/vector/app/core/di/ActiveSessionHolder.kt @@ -18,6 +18,7 @@ package im.vector.app.core.di import android.content.Context import im.vector.app.ActiveSessionDataSource +import im.vector.app.core.dispatchers.CoroutineDispatchers import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase import im.vector.app.core.services.GuardServiceStarter import im.vector.app.core.session.ConfigureAndStartSessionUseCase @@ -26,7 +27,10 @@ import im.vector.app.features.crypto.keysrequest.KeyRequestHandler import im.vector.app.features.crypto.verification.IncomingVerificationRequestHandler import im.vector.app.features.notifications.PushRuleTriggerListener import im.vector.app.features.session.SessionListener +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext import org.matrix.android.sdk.api.auth.AuthenticationService import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.util.Optional @@ -51,6 +55,8 @@ class ActiveSessionHolder @Inject constructor( private val authenticationService: AuthenticationService, private val configureAndStartSessionUseCase: ConfigureAndStartSessionUseCase, private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase, + private val applicationCoroutineScope: CoroutineScope, + private val coroutineDispatchers: CoroutineDispatchers, ) { private var activeSessionReference: AtomicReference = AtomicReference() @@ -96,6 +102,13 @@ class ActiveSessionHolder @Inject constructor( return runBlocking { getOrInitializeSession() } } + fun getSafeActiveSessionAsync(withSession: ((Session?) -> Unit)) { + applicationCoroutineScope.launch(coroutineDispatchers.io) { + val session = getOrInitializeSession() + withSession(session) + } + } + fun getActiveSession(): Session { return getSafeActiveSession() ?: throw IllegalStateException("You should authenticate before using this")