Introduce SessionState
to be able to check the Session state from several places.
This commit is contained in:
parent
d3784c8019
commit
1bad8f7741
@ -131,12 +131,11 @@ internal class DefaultSession @Inject constructor(
|
|||||||
private val toDeviceService: Lazy<ToDeviceService>,
|
private val toDeviceService: Lazy<ToDeviceService>,
|
||||||
private val eventStreamService: Lazy<EventStreamService>,
|
private val eventStreamService: Lazy<EventStreamService>,
|
||||||
@UnauthenticatedWithCertificate
|
@UnauthenticatedWithCertificate
|
||||||
private val unauthenticatedWithCertificateOkHttpClient: Lazy<OkHttpClient>
|
private val unauthenticatedWithCertificateOkHttpClient: Lazy<OkHttpClient>,
|
||||||
|
private val sessionState: SessionState,
|
||||||
) : Session,
|
) : Session,
|
||||||
GlobalErrorHandler.Listener {
|
GlobalErrorHandler.Listener {
|
||||||
|
|
||||||
private var isOpen = false
|
|
||||||
|
|
||||||
private val uiHandler = createUIHandler()
|
private val uiHandler = createUIHandler()
|
||||||
|
|
||||||
override val isOpenable: Boolean
|
override val isOpenable: Boolean
|
||||||
@ -144,8 +143,7 @@ internal class DefaultSession @Inject constructor(
|
|||||||
|
|
||||||
@MainThread
|
@MainThread
|
||||||
override fun open() {
|
override fun open() {
|
||||||
assert(!isOpen)
|
sessionState.setIsOpen(true)
|
||||||
isOpen = true
|
|
||||||
globalErrorHandler.listener = this
|
globalErrorHandler.listener = this
|
||||||
cryptoService.get().ensureDevice()
|
cryptoService.get().ensureDevice()
|
||||||
uiHandler.post {
|
uiHandler.post {
|
||||||
@ -159,7 +157,7 @@ internal class DefaultSession @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
assert(isOpen)
|
assert(sessionState.isOpen)
|
||||||
syncService.get().stopSync()
|
syncService.get().stopSync()
|
||||||
// timelineEventDecryptor.destroy()
|
// timelineEventDecryptor.destroy()
|
||||||
uiHandler.post {
|
uiHandler.post {
|
||||||
@ -170,7 +168,7 @@ internal class DefaultSession @Inject constructor(
|
|||||||
}
|
}
|
||||||
cryptoService.get().close()
|
cryptoService.get().close()
|
||||||
globalErrorHandler.listener = null
|
globalErrorHandler.listener = null
|
||||||
isOpen = false
|
sessionState.setIsOpen(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun clearCache() {
|
override suspend fun clearCache() {
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -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.api.session.sync.SyncService
|
||||||
import org.matrix.android.sdk.internal.di.SessionId
|
import org.matrix.android.sdk.internal.di.SessionId
|
||||||
import org.matrix.android.sdk.internal.di.WorkManagerProvider
|
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.SyncThread
|
||||||
import org.matrix.android.sdk.internal.session.sync.job.SyncWorker
|
import org.matrix.android.sdk.internal.session.sync.job.SyncWorker
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
@ -33,6 +34,7 @@ internal class DefaultSyncService @Inject constructor(
|
|||||||
private val syncThreadProvider: Provider<SyncThread>,
|
private val syncThreadProvider: Provider<SyncThread>,
|
||||||
private val syncTokenStore: SyncTokenStore,
|
private val syncTokenStore: SyncTokenStore,
|
||||||
private val syncRequestStateTracker: SyncRequestStateTracker,
|
private val syncRequestStateTracker: SyncRequestStateTracker,
|
||||||
|
private val sessionState: SessionState,
|
||||||
) : SyncService {
|
) : SyncService {
|
||||||
private var syncThread: SyncThread? = null
|
private var syncThread: SyncThread? = null
|
||||||
|
|
||||||
@ -50,8 +52,7 @@ internal class DefaultSyncService @Inject constructor(
|
|||||||
|
|
||||||
override fun startSync(fromForeground: Boolean) {
|
override fun startSync(fromForeground: Boolean) {
|
||||||
Timber.i("Starting sync thread")
|
Timber.i("Starting sync thread")
|
||||||
// TODO How to check that now?
|
assert(sessionState.isOpen)
|
||||||
// assert(isOpen)
|
|
||||||
val localSyncThread = getSyncThread()
|
val localSyncThread = getSyncThread()
|
||||||
localSyncThread.setInitialForeground(fromForeground)
|
localSyncThread.setInitialForeground(fromForeground)
|
||||||
if (!localSyncThread.isAlive) {
|
if (!localSyncThread.isAlive) {
|
||||||
@ -63,8 +64,7 @@ internal class DefaultSyncService @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun stopSync() {
|
override fun stopSync() {
|
||||||
// TODO How to check that now?
|
assert(sessionState.isOpen)
|
||||||
// assert(isOpen)
|
|
||||||
syncThread?.kill()
|
syncThread?.kill()
|
||||||
syncThread = null
|
syncThread = null
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user