2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +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
|
|
|
*
|
2019-01-25 13:04:59 +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
|
|
|
*/
|
|
|
|
|
2018-10-08 15:52:45 +00:00
|
|
|
package im.vector.matrix.android.internal.session
|
|
|
|
|
2019-05-16 08:23:57 +00:00
|
|
|
import android.content.Context
|
2018-10-16 13:52:30 +00:00
|
|
|
import android.os.Looper
|
2019-01-16 18:25:43 +00:00
|
|
|
import androidx.annotation.MainThread
|
2019-01-23 15:25:24 +00:00
|
|
|
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
|
2018-12-12 20:15:01 +00:00
|
|
|
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
|
2018-10-09 10:30:01 +00:00
|
|
|
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
|
2019-04-05 16:21:45 +00:00
|
|
|
import im.vector.matrix.android.api.session.content.ContentUploadStateTracker
|
2019-01-23 15:25:24 +00:00
|
|
|
import im.vector.matrix.android.api.session.content.ContentUrlResolver
|
2019-06-14 14:32:23 +00:00
|
|
|
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
|
2019-06-11 14:54:44 +00:00
|
|
|
import im.vector.matrix.android.api.session.sync.SyncState
|
2019-02-25 20:55:33 +00:00
|
|
|
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
|
2019-07-09 15:08:24 +00:00
|
|
|
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
|
2018-10-08 15:52:45 +00:00
|
|
|
|
2019-06-16 15:00:47 +00:00
|
|
|
@SessionScope
|
2019-06-17 16:17:37 +00:00
|
|
|
internal class DefaultSession @Inject constructor(override val sessionParams: SessionParams,
|
2019-06-26 18:58:46 +00:00
|
|
|
private val context: Context,
|
2019-06-21 08:36:02 +00:00
|
|
|
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>)
|
2019-06-14 14:32:23 +00:00
|
|
|
: 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() {
|
2019-06-14 14:32:23 +00:00
|
|
|
|
2018-10-16 13:52:30 +00:00
|
|
|
private var isOpen = false
|
2018-10-08 15:52:45 +00:00
|
|
|
|
2019-06-19 08:46:59 +00:00
|
|
|
|
2018-10-16 13:52:30 +00:00
|
|
|
@MainThread
|
|
|
|
override fun open() {
|
2018-12-12 20:15:01 +00:00
|
|
|
assertMainThread()
|
2018-10-16 13:52:30 +00:00
|
|
|
assert(!isOpen)
|
|
|
|
isOpen = true
|
2019-06-21 08:36:02 +00:00
|
|
|
liveEntityObservers.forEach { it.start() }
|
2019-04-12 13:51:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 13:22:40 +00:00
|
|
|
override fun requireBackgroundSync() {
|
2019-06-26 18:58:46 +00:00
|
|
|
SyncWorker.requireBackgroundSync(context, sessionParams.credentials.userId)
|
2019-06-20 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun startAutomaticBackgroundSync(repeatDelay: Long) {
|
2019-06-26 18:58:46 +00:00
|
|
|
SyncWorker.automaticallyBackgroundSync(context, sessionParams.credentials.userId, 0, repeatDelay)
|
2019-06-20 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun stopAnyBackgroundSync() {
|
2019-06-26 18:58:46 +00:00
|
|
|
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")
|
|
|
|
}
|
2018-10-08 15:52:45 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-10-08 15:52:45 +00:00
|
|
|
override fun close() {
|
2018-10-16 13:52:30 +00:00
|
|
|
assert(isOpen)
|
2019-07-01 16:55:40 +00:00
|
|
|
stopSync()
|
2019-06-21 08:36:02 +00:00
|
|
|
liveEntityObservers.forEach { it.dispose() }
|
2019-07-10 17:01:15 +00:00
|
|
|
cryptoService.get().close()
|
2018-10-16 13:52:30 +00:00
|
|
|
isOpen = false
|
2018-10-08 15:52:45 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:54:44 +00:00
|
|
|
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
|
|
|
|
2019-07-09 15:08:24 +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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-20 17:26:59 +00:00
|
|
|
override fun contentUrlResolver() = contentUrlResolver
|
2019-01-23 15:25:24 +00:00
|
|
|
|
2019-06-20 17:26:59 +00:00
|
|
|
override fun contentUploadProgressTracker() = contentUploadProgressTracker
|
2019-04-05 16:21:45 +00:00
|
|
|
|
2019-01-14 15:18:39 +00:00
|
|
|
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 *****************************************************************************
|
|
|
|
|
2018-12-12 20:15:01 +00:00
|
|
|
private fun assertMainThread() {
|
|
|
|
if (Looper.getMainLooper().thread !== Thread.currentThread()) {
|
|
|
|
throw IllegalStateException("This method can only be called on the main thread!")
|
2018-10-16 13:52:30 +00:00
|
|
|
}
|
2018-10-08 15:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|