ActiveSessionHolder is not supposed to start syncing. Instead, the MainActivity does it, if necessary.

Fixes a race condition when clearing cache.
This commit is contained in:
Benoit Marty 2022-12-20 13:12:54 +01:00
parent 789746c276
commit 95a29b83fe
4 changed files with 15 additions and 13 deletions

View File

@ -18,7 +18,6 @@ package im.vector.app.core.di
import android.content.Context
import im.vector.app.ActiveSessionDataSource
import im.vector.app.core.extensions.startSyncing
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
import im.vector.app.core.services.GuardServiceStarter
import im.vector.app.core.session.ConfigureAndStartSessionUseCase
@ -72,7 +71,7 @@ class ActiveSessionHolder @Inject constructor(
suspend fun clearActiveSession() {
// Do some cleanup first
getSafeActiveSession(startSync = false)?.let {
getSafeActiveSession()?.let {
Timber.w("clearActiveSession of ${it.myUserId}")
it.callSignalingService().removeCallListener(callManager)
it.removeListener(sessionListener)
@ -93,8 +92,8 @@ class ActiveSessionHolder @Inject constructor(
return activeSessionReference.get() != null || authenticationService.hasAuthenticatedSessions()
}
fun getSafeActiveSession(startSync: Boolean = true): Session? {
return runBlocking { getOrInitializeSession(startSync = startSync) }
fun getSafeActiveSession(): Session? {
return runBlocking { getOrInitializeSession() }
}
fun getActiveSession(): Session {
@ -102,16 +101,11 @@ class ActiveSessionHolder @Inject constructor(
?: throw IllegalStateException("You should authenticate before using this")
}
suspend fun getOrInitializeSession(startSync: Boolean): Session? {
suspend fun getOrInitializeSession(): Session? {
return activeSessionReference.get()
?.also {
if (startSync && !it.syncService().isSyncThreadAlive()) {
it.startSyncing(applicationContext)
}
}
?: sessionInitializer.tryInitialize(readCurrentSession = { activeSessionReference.get() }) { session ->
setActiveSession(session)
configureAndStartSessionUseCase.execute(session, startSyncing = startSync)
configureAndStartSessionUseCase.execute(session, startSyncing = false)
}
}

View File

@ -118,7 +118,7 @@ class VectorPushHandler @Inject constructor(
Timber.tag(loggerTag.value).d("## handleInternal()")
}
val session = activeSessionHolder.getOrInitializeSession(startSync = false)
val session = activeSessionHolder.getOrInitializeSession()
if (session == null) {
Timber.tag(loggerTag.value).w("## Can't sync from push, no current session")

View File

@ -174,12 +174,15 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
private fun handleAppStarted() {
if (intent.hasExtra(EXTRA_NEXT_INTENT)) {
// Start the next Activity
startSyncing()
val nextIntent = intent.getParcelableExtraCompat<Intent>(EXTRA_NEXT_INTENT)
startIntentAndFinish(nextIntent)
} else if (intent.hasExtra(EXTRA_INIT_SESSION)) {
startSyncing()
setResult(RESULT_OK)
finish()
} else if (intent.action == ACTION_ROOM_DETAILS_FROM_SHORTCUT) {
startSyncing()
val roomId = intent.getStringExtra(EXTRA_ROOM_ID)
if (roomId?.isNotEmpty() == true) {
navigator.openRoom(this, roomId, trigger = ViewRoom.Trigger.Shortcut)
@ -194,11 +197,16 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
if (args.clearCache || args.clearCredentials) {
doCleanUp()
} else {
startSyncing()
startNextActivityAndFinish()
}
}
}
private fun startSyncing() {
activeSessionHolder.getSafeActiveSession()?.startSyncing(this)
}
private fun clearNotifications() {
// Dismiss all notifications
notificationDrawerManager.clearAllEvents()

View File

@ -63,7 +63,7 @@ class StartAppViewModel @AssistedInject constructor(
}
private suspend fun eagerlyInitializeSession() {
sessionHolder.getOrInitializeSession(startSync = true)
sessionHolder.getOrInitializeSession()
}
private fun handleLongProcessing() {