Skip to content

Commit

Permalink
Introduce SessionState to be able to check the Session state from s…
Browse files Browse the repository at this point in the history
…everal places.
  • Loading branch information
bmarty committed May 30, 2022
1 parent 974b2c8 commit a33f7d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,19 @@ internal class DefaultSession @Inject constructor(
private val toDeviceService: Lazy<ToDeviceService>,
private val eventStreamService: Lazy<EventStreamService>,
@UnauthenticatedWithCertificate
private val unauthenticatedWithCertificateOkHttpClient: Lazy<OkHttpClient>
private val unauthenticatedWithCertificateOkHttpClient: Lazy<OkHttpClient>,
private val sessionState: SessionState,
) : Session,
GlobalErrorHandler.Listener {

private var isOpen = false

private val uiHandler = createUIHandler()

override val isOpenable: Boolean
get() = sessionParamsStore.get(sessionId)?.isTokenValid ?: false

@MainThread
override fun open() {
assert(!isOpen)
isOpen = true
sessionState.setIsOpen(true)
globalErrorHandler.listener = this
cryptoService.get().ensureDevice()
uiHandler.post {
Expand All @@ -159,7 +157,7 @@ internal class DefaultSession @Inject constructor(
}

override fun close() {
assert(isOpen)
assert(sessionState.isOpen)
syncService.get().stopSync()
// timelineEventDecryptor.destroy()
uiHandler.post {
Expand All @@ -170,7 +168,7 @@ internal class DefaultSession @Inject constructor(
}
cryptoService.get().close()
globalErrorHandler.listener = null
isOpen = false
sessionState.setIsOpen(false)
}

override suspend fun clearCache() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +34,7 @@ internal class DefaultSyncService @Inject constructor(
private val syncThreadProvider: Provider<SyncThread>,
private val syncTokenStore: SyncTokenStore,
private val syncRequestStateTracker: SyncRequestStateTracker,
private val sessionState: SessionState,
) : SyncService {
private var syncThread: SyncThread? = null

Expand All @@ -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) {
Expand All @@ -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
}
Expand Down

0 comments on commit a33f7d0

Please sign in to comment.