creating a dedicated threadsafe Session instance initializer in order to attempt to restore session when they're not yet created in memory

This commit is contained in:
Adam Brown 2022-08-01 13:11:19 +01:00
parent a812b77e7d
commit fedbe048ba
5 changed files with 91 additions and 58 deletions

View file

@ -16,8 +16,10 @@
package im.vector.app.core.di
import android.content.Context
import arrow.core.Option
import im.vector.app.ActiveSessionDataSource
import im.vector.app.core.extensions.configureAndStart
import im.vector.app.core.pushers.UnifiedPushHelper
import im.vector.app.core.services.GuardServiceStarter
import im.vector.app.features.call.webrtc.WebRtcCallManager
@ -25,6 +27,12 @@ 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.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.newSingleThreadContext
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 timber.log.Timber
import java.util.concurrent.atomic.AtomicReference
@ -41,7 +49,10 @@ class ActiveSessionHolder @Inject constructor(
private val sessionListener: SessionListener,
private val imageManager: ImageManager,
private val unifiedPushHelper: UnifiedPushHelper,
private val guardServiceStarter: GuardServiceStarter
private val guardServiceStarter: GuardServiceStarter,
private val sessionInitializer: SessionInitializer,
private val applicationContext: Context,
private val authenticationService: AuthenticationService,
) {
private var activeSessionReference: AtomicReference<Session?> = AtomicReference()
@ -80,18 +91,29 @@ class ActiveSessionHolder @Inject constructor(
}
fun hasActiveSession(): Boolean {
return activeSessionReference.get() != null
return activeSessionReference.get() != null || authenticationService.hasAuthenticatedSessions()
}
fun getSafeActiveSession(): Session? {
return activeSessionReference.get()
return runBlocking { getOrInitializeSession(startSync = true) }
}
fun getActiveSession(): Session {
return activeSessionReference.get()
return getSafeActiveSession()
?: throw IllegalStateException("You should authenticate before using this")
}
suspend fun getOrInitializeSession(startSync: Boolean): Session? {
return activeSessionReference.get() ?: sessionInitializer.tryInitialize(readCurrentSession = { activeSessionReference.get() }) { session ->
setActiveSession(session)
withContext(Dispatchers.Main) {
session.configureAndStart(applicationContext, startSyncing = startSync)
}
}
}
fun isWaitingForSessionInitialization() = activeSessionReference.get() == null && authenticationService.hasAuthenticatedSessions()
// TODO Stop sync ?
// fun switchToSession(sessionParams: SessionParams) {
// val newActiveSession = authenticationService.getSession(sessionParams)

View file

@ -1,40 +0,0 @@
/*
* Copyright 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.di
import android.content.Context
import im.vector.app.core.extensions.configureAndStart
import org.matrix.android.sdk.api.auth.AuthenticationService
import javax.inject.Inject
class ActiveSessionSetter @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
private val authenticationService: AuthenticationService,
private val applicationContext: Context,
) {
fun shouldSetActionSession(): Boolean {
return authenticationService.hasAuthenticatedSessions() && !activeSessionHolder.hasActiveSession()
}
fun tryToSetActiveSession(startSync: Boolean) {
if (shouldSetActionSession()) {
val lastAuthenticatedSession = authenticationService.getLastAuthenticatedSession()!!
activeSessionHolder.setActiveSession(lastAuthenticatedSession)
lastAuthenticatedSession.configureAndStart(applicationContext, startSyncing = startSync)
}
}
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.core.di
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.withContext
import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.session.Session
import javax.inject.Inject
@OptIn(DelicateCoroutinesApi::class)
private val INITIALIZER_CONTEXT = newSingleThreadContext("session-initializer")
class SessionInitializer @Inject constructor(
private val authenticationService: AuthenticationService,
) {
/**
* A thread safe way to initialize the last authenticated Session instance.
*
* @param readCurrentSession expects an in-memory Session to be provided or null if not yet set.
* @param initializer callback to allow additional initialization on the Session, such as setting the in-memory Session instance.
* @return the initialized Session or null when no authenticated sessions are available.
*/
suspend fun tryInitialize(readCurrentSession: () -> Session?, initializer: suspend (Session) -> Unit): Session? {
return withContext(INITIALIZER_CONTEXT) {
val currentInMemorySession = readCurrentSession()
when {
currentInMemorySession != null -> currentInMemorySession
authenticationService.hasAuthenticatedSessions() -> {
val lastAuthenticatedSession = authenticationService.getLastAuthenticatedSession()!!
lastAuthenticatedSession.also { initializer(lastAuthenticatedSession) }
}
else -> null
}
}
}
}

View file

@ -27,7 +27,6 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.BuildConfig
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.ActiveSessionSetter
import im.vector.app.core.network.WifiDetector
import im.vector.app.core.pushers.model.PushData
import im.vector.app.core.services.GuardServiceStarter
@ -38,6 +37,7 @@ import im.vector.app.features.settings.BackgroundSyncMode
import im.vector.app.features.settings.VectorDataStore
import im.vector.app.features.settings.VectorPreferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
@ -60,7 +60,6 @@ class VectorMessagingReceiver : MessagingReceiver() {
@Inject lateinit var notificationDrawerManager: NotificationDrawerManager
@Inject lateinit var notifiableEventResolver: NotifiableEventResolver
@Inject lateinit var pushersManager: PushersManager
@Inject lateinit var activeSessionSetter: ActiveSessionSetter
@Inject lateinit var activeSessionHolder: ActiveSessionHolder
@Inject lateinit var vectorPreferences: VectorPreferences
@Inject lateinit var vectorDataStore: VectorDataStore
@ -116,7 +115,7 @@ class VectorMessagingReceiver : MessagingReceiver() {
// we are in foreground, let the sync do the things?
Timber.tag(loggerTag.value).d("PUSH received in a foreground state, ignore")
} else {
onMessageReceivedInternal(pushData)
coroutineScope.launch(Dispatchers.IO) { onMessageReceivedInternal(pushData) }
}
}
}
@ -170,7 +169,7 @@ class VectorMessagingReceiver : MessagingReceiver() {
*
* @param pushData Object containing message data.
*/
private fun onMessageReceivedInternal(pushData: PushData) {
private suspend fun onMessageReceivedInternal(pushData: PushData) {
try {
if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) {
Timber.tag(loggerTag.value).d("## onMessageReceivedInternal() : $pushData")
@ -178,12 +177,7 @@ class VectorMessagingReceiver : MessagingReceiver() {
Timber.tag(loggerTag.value).d("## onMessageReceivedInternal()")
}
val session = activeSessionHolder.getSafeActiveSession()
?: run {
// Active session may not exist yet, if MainActivity has not been launched
activeSessionSetter.tryToSetActiveSession(startSync = false)
activeSessionHolder.getSafeActiveSession()
}
val session = activeSessionHolder.getOrInitializeSession(startSync = false)
if (session == null) {
Timber.tag(loggerTag.value).w("## Can't sync from push, no current session")

View file

@ -20,7 +20,7 @@ import com.airbnb.mvrx.MavericksViewModelFactory
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import im.vector.app.core.di.ActiveSessionSetter
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.di.MavericksAssistedViewModelFactory
import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.platform.VectorViewModel
@ -31,7 +31,7 @@ import kotlin.time.Duration.Companion.seconds
class StartAppViewModel @AssistedInject constructor(
@Assisted val initialState: StartAppViewState,
private val activeSessionSetter: ActiveSessionSetter,
private val sessionHolder: ActiveSessionHolder,
) : VectorViewModel<StartAppViewState, StartAppAction, StartAppViewEvent>(initialState) {
@AssistedFactory
@ -42,7 +42,7 @@ class StartAppViewModel @AssistedInject constructor(
companion object : MavericksViewModelFactory<StartAppViewModel, StartAppViewState> by hiltMavericksViewModelFactory()
fun shouldStartApp(): Boolean {
return activeSessionSetter.shouldSetActionSession()
return sessionHolder.isWaitingForSessionInitialization()
}
override fun handle(action: StartAppAction) {
@ -55,11 +55,15 @@ class StartAppViewModel @AssistedInject constructor(
handleLongProcessing()
viewModelScope.launch(Dispatchers.IO) {
// This can take time because of DB migration(s), so do it in a background task.
activeSessionSetter.tryToSetActiveSession(startSync = true)
eagerlyInitializeSession()
_viewEvents.post(StartAppViewEvent.AppStarted)
}
}
private suspend fun eagerlyInitializeSession() {
sessionHolder.getOrInitializeSession(startSync = true)
}
private fun handleLongProcessing() {
viewModelScope.launch(Dispatchers.Default) {
delay(1.seconds.inWholeMilliseconds)