diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index fc76ebd519..32269c9afd 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -131,12 +131,11 @@ internal class DefaultSession @Inject constructor( private val toDeviceService: Lazy, private val eventStreamService: Lazy, @UnauthenticatedWithCertificate - private val unauthenticatedWithCertificateOkHttpClient: Lazy + private val unauthenticatedWithCertificateOkHttpClient: Lazy, + private val sessionState: SessionState, ) : Session, GlobalErrorHandler.Listener { - private var isOpen = false - private val uiHandler = createUIHandler() override val isOpenable: Boolean @@ -144,8 +143,7 @@ internal class DefaultSession @Inject constructor( @MainThread override fun open() { - assert(!isOpen) - isOpen = true + sessionState.setIsOpen(true) globalErrorHandler.listener = this cryptoService.get().ensureDevice() uiHandler.post { @@ -159,7 +157,7 @@ internal class DefaultSession @Inject constructor( } override fun close() { - assert(isOpen) + assert(sessionState.isOpen) syncService.get().stopSync() // timelineEventDecryptor.destroy() uiHandler.post { @@ -170,7 +168,7 @@ internal class DefaultSession @Inject constructor( } cryptoService.get().close() globalErrorHandler.listener = null - isOpen = false + sessionState.setIsOpen(false) } override suspend fun clearCache() { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionState.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionState.kt new file mode 100644 index 0000000000..b109a9aa60 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionState.kt @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 The Matrix.org Foundation C.I.C. + * + * 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 org.matrix.android.sdk.internal.session + +import javax.inject.Inject + +@SessionScope +internal class SessionState @Inject constructor() { + var isOpen = false + private set + + /** + * Set the new state. Throw if already in the new state. + */ + fun setIsOpen(newState: Boolean) { + assert(newState != isOpen) + isOpen = newState + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/DefaultSyncService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/DefaultSyncService.kt index a97caa10c7..37869b88f9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/DefaultSyncService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/DefaultSyncService.kt @@ -21,6 +21,7 @@ import org.matrix.android.sdk.api.session.sync.SyncRequestState import org.matrix.android.sdk.api.session.sync.SyncService import org.matrix.android.sdk.internal.di.SessionId import org.matrix.android.sdk.internal.di.WorkManagerProvider +import org.matrix.android.sdk.internal.session.SessionState import org.matrix.android.sdk.internal.session.sync.job.SyncThread import org.matrix.android.sdk.internal.session.sync.job.SyncWorker import timber.log.Timber @@ -33,6 +34,7 @@ internal class DefaultSyncService @Inject constructor( private val syncThreadProvider: Provider, private val syncTokenStore: SyncTokenStore, private val syncRequestStateTracker: SyncRequestStateTracker, + private val sessionState: SessionState, ) : SyncService { private var syncThread: SyncThread? = null @@ -50,8 +52,7 @@ internal class DefaultSyncService @Inject constructor( override fun startSync(fromForeground: Boolean) { Timber.i("Starting sync thread") - // TODO How to check that now? - // assert(isOpen) + assert(sessionState.isOpen) val localSyncThread = getSyncThread() localSyncThread.setInitialForeground(fromForeground) if (!localSyncThread.isAlive) { @@ -63,8 +64,7 @@ internal class DefaultSyncService @Inject constructor( } override fun stopSync() { - // TODO How to check that now? - // assert(isOpen) + assert(sessionState.isOpen) syncThread?.kill() syncThread = null }