BayernMessenger/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt

193 lines
8.1 KiB
Kotlin
Raw Normal View History

2019-01-18 10:12:08 +00:00
/*
* Copyright 2019 New Vector Ltd
2019-01-18 10:12:08 +00:00
*
* 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
2019-01-18 10:12:08 +00:00
*
* 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.
2019-01-18 10:12:08 +00:00
*/
package im.vector.matrix.android.internal.session
2019-05-16 08:23:57 +00:00
import android.content.Context
import android.os.Looper
import androidx.annotation.MainThread
import androidx.lifecycle.LiveData
2019-07-10 17:01:15 +00:00
import dagger.Lazy
2019-04-03 14:36:45 +00:00
import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.auth.data.SessionParams
2019-06-19 08:46:59 +00:00
import im.vector.matrix.android.api.pushrules.PushRuleService
2019-07-04 08:53:46 +00:00
import im.vector.matrix.android.api.session.InitialSyncProgressService
import im.vector.matrix.android.api.session.Session
2019-04-17 13:48:59 +00:00
import im.vector.matrix.android.api.session.cache.CacheService
import im.vector.matrix.android.api.session.content.ContentUploadStateTracker
import im.vector.matrix.android.api.session.content.ContentUrlResolver
import im.vector.matrix.android.api.session.crypto.CryptoService
2019-07-08 17:06:17 +00:00
import im.vector.matrix.android.api.session.file.FileService
2018-11-05 16:39:07 +00:00
import im.vector.matrix.android.api.session.group.GroupService
2019-06-19 08:46:59 +00:00
import im.vector.matrix.android.api.session.pushers.PushersService
2019-05-24 09:35:46 +00:00
import im.vector.matrix.android.api.session.room.RoomDirectoryService
2018-10-17 16:21:09 +00:00
import im.vector.matrix.android.api.session.room.RoomService
2019-04-03 14:36:45 +00:00
import im.vector.matrix.android.api.session.signout.SignOutService
2019-04-12 13:51:20 +00:00
import im.vector.matrix.android.api.session.sync.FilterService
import im.vector.matrix.android.api.session.sync.SyncState
import im.vector.matrix.android.api.session.user.UserService
2019-04-17 13:48:59 +00:00
import im.vector.matrix.android.api.util.MatrixCallbackDelegate
2019-05-21 13:42:09 +00:00
import im.vector.matrix.android.internal.crypto.CryptoManager
2018-11-07 19:36:19 +00:00
import im.vector.matrix.android.internal.database.LiveEntityObserver
2018-10-17 16:21:09 +00:00
import im.vector.matrix.android.internal.session.sync.job.SyncThread
2019-06-20 13:22:40 +00:00
import im.vector.matrix.android.internal.session.sync.job.SyncWorker
import im.vector.matrix.android.internal.worker.WorkManagerUtil
2019-06-13 08:58:45 +00:00
import timber.log.Timber
2019-06-17 16:17:37 +00:00
import javax.inject.Inject
@SessionScope
2019-06-17 16:17:37 +00:00
internal class DefaultSession @Inject constructor(override val sessionParams: SessionParams,
private val context: Context,
private val liveEntityObservers: Set<@JvmSuppressWildcards LiveEntityObserver>,
2019-06-17 16:17:37 +00:00
private val sessionListeners: SessionListeners,
2019-07-10 17:01:15 +00:00
private val roomService: Lazy<RoomService>,
private val roomDirectoryService: Lazy<RoomDirectoryService>,
private val groupService: Lazy<GroupService>,
private val userService: Lazy<UserService>,
private val filterService: Lazy<FilterService>,
private val cacheService: Lazy<CacheService>,
private val signOutService: Lazy<SignOutService>,
private val pushRuleService: Lazy<PushRuleService>,
private val pushersService: Lazy<PushersService>,
private val cryptoService: Lazy<CryptoManager>,
private val fileService: Lazy<FileService>,
2019-06-17 16:17:37 +00:00
private val syncThread: SyncThread,
private val contentUrlResolver: ContentUrlResolver,
2019-07-04 08:53:46 +00:00
private val contentUploadProgressTracker: ContentUploadStateTracker,
2019-07-10 17:01:15 +00:00
private val initialSyncProgressService: Lazy<InitialSyncProgressService>)
: Session,
2019-07-10 17:01:15 +00:00
RoomService by roomService.get(),
RoomDirectoryService by roomDirectoryService.get(),
GroupService by groupService.get(),
UserService by userService.get(),
CryptoService by cryptoService.get(),
CacheService by cacheService.get(),
SignOutService by signOutService.get(),
FilterService by filterService.get(),
PushRuleService by pushRuleService.get(),
PushersService by pushersService.get(),
FileService by fileService.get(),
InitialSyncProgressService by initialSyncProgressService.get() {
private var isOpen = false
2019-06-19 08:46:59 +00:00
@MainThread
override fun open() {
assertMainThread()
assert(!isOpen)
isOpen = true
liveEntityObservers.forEach { it.start() }
2019-04-12 13:51:20 +00:00
}
2019-06-20 13:22:40 +00:00
override fun requireBackgroundSync() {
SyncWorker.requireBackgroundSync(context, sessionParams.credentials.userId)
2019-06-20 13:22:40 +00:00
}
override fun startAutomaticBackgroundSync(repeatDelay: Long) {
SyncWorker.automaticallyBackgroundSync(context, sessionParams.credentials.userId, 0, repeatDelay)
2019-06-20 13:22:40 +00:00
}
override fun stopAnyBackgroundSync() {
SyncWorker.stopAnyBackgroundSync(context)
2019-06-20 13:22:40 +00:00
}
2019-06-19 08:46:59 +00:00
2019-04-16 13:36:29 +00:00
override fun startSync() {
2019-04-12 13:51:20 +00:00
assert(isOpen)
2019-06-20 13:22:40 +00:00
if (!syncThread.isAlive) {
syncThread.start()
} else {
syncThread.restart()
Timber.w("Attempt to start an already started thread")
}
}
2019-04-16 13:36:29 +00:00
override fun stopSync() {
2019-04-12 13:51:20 +00:00
assert(isOpen)
syncThread.kill()
}
2018-10-15 17:42:13 +00:00
override fun close() {
assert(isOpen)
2019-07-01 16:55:40 +00:00
stopSync()
liveEntityObservers.forEach { it.dispose() }
2019-07-10 17:01:15 +00:00
cryptoService.get().close()
isOpen = false
}
override fun syncState(): LiveData<SyncState> {
return syncThread.liveState()
}
2019-04-03 14:36:45 +00:00
@MainThread
override fun signOut(callback: MatrixCallback<Unit>) {
2019-06-13 08:58:45 +00:00
Timber.w("SIGN_OUT: start")
2019-04-03 14:36:45 +00:00
assert(isOpen)
2019-06-19 08:46:59 +00:00
//Timber.w("SIGN_OUT: kill sync thread")
//syncThread.kill()
2019-04-17 13:48:59 +00:00
2019-06-13 08:58:45 +00:00
Timber.w("SIGN_OUT: call webservice")
2019-07-10 17:01:15 +00:00
return signOutService.get().signOut(object : MatrixCallback<Unit> {
2019-04-03 14:36:45 +00:00
override fun onSuccess(data: Unit) {
2019-06-13 08:58:45 +00:00
Timber.w("SIGN_OUT: call webservice -> SUCCESS: clear cache")
2019-04-17 13:48:59 +00:00
// Clear the cache
2019-07-10 17:01:15 +00:00
cacheService.get().clearCache(object : MatrixCallback<Unit> {
2019-06-13 08:58:45 +00:00
override fun onSuccess(data: Unit) {
Timber.w("SIGN_OUT: clear cache -> SUCCESS: clear crypto cache")
2019-07-10 17:01:15 +00:00
cryptoService.get().clearCryptoCache(MatrixCallbackDelegate(callback))
2019-07-01 18:12:05 +00:00
WorkManagerUtil.cancelAllWorks(context)
2019-06-13 08:58:45 +00:00
}
override fun onFailure(failure: Throwable) {
// ignore error
Timber.e("SIGN_OUT: clear cache -> ERROR: ignoring")
onSuccess(Unit)
}
})
2019-04-03 14:36:45 +00:00
}
override fun onFailure(failure: Throwable) {
2019-04-17 13:48:59 +00:00
// Ignore failure
2019-06-13 08:58:45 +00:00
Timber.e("SIGN_OUT: call webservice -> ERROR: ignoring")
2019-04-17 13:48:59 +00:00
onSuccess(Unit)
2019-04-03 14:36:45 +00:00
}
})
}
override fun contentUrlResolver() = contentUrlResolver
override fun contentUploadProgressTracker() = contentUploadProgressTracker
override fun addListener(listener: Session.Listener) {
sessionListeners.addListener(listener)
}
override fun removeListener(listener: Session.Listener) {
sessionListeners.removeListener(listener)
}
2018-10-17 11:59:21 +00:00
// Private methods *****************************************************************************
private fun assertMainThread() {
if (Looper.getMainLooper().thread !== Thread.currentThread()) {
throw IllegalStateException("This method can only be called on the main thread!")
}
}
}