diff --git a/matrix-sdk-android/build.gradle b/matrix-sdk-android/build.gradle index ba29aad2fa..4119c03c70 100644 --- a/matrix-sdk-android/build.gradle +++ b/matrix-sdk-android/build.gradle @@ -91,6 +91,7 @@ dependencies { def lifecycle_version = '2.0.0' def coroutines_version = "1.0.1" def markwon_version = '3.0.0' + def daggerVersion = '2.23.1' implementation fileTree(dir: 'libs', include: ['*.aar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" @@ -132,8 +133,10 @@ dependencies { implementation 'org.matrix.gitlab.matrix-org:olm:3.1.2' // DI - implementation "org.koin:koin-core:$koin_version" - implementation "org.koin:koin-core-ext:$koin_version" + implementation "com.google.dagger:dagger:$daggerVersion" + kapt "com.google.dagger:dagger-compiler:$daggerVersion" + compileOnly 'com.squareup.inject:assisted-inject-annotations-dagger2:0.4.0' + kapt 'com.squareup.inject:assisted-inject-processor-dagger2:0.4.0' // Logging implementation 'com.jakewharton.timber:timber:4.7.1' diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/FakeGetContextOfEventTask.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/FakeGetContextOfEventTask.kt index c2d325496a..afcb846fcd 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/FakeGetContextOfEventTask.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/FakeGetContextOfEventTask.kt @@ -23,7 +23,7 @@ import im.vector.matrix.android.internal.session.room.timeline.TokenChunkEvent import im.vector.matrix.android.internal.session.room.timeline.TokenChunkEventPersistor import kotlin.random.Random -internal class FakeGetContextOfEventTask(private val tokenChunkEventPersistor: TokenChunkEventPersistor) : GetContextOfEventTask { +internal class FakeGetContextOfEventTask @Inject constructor(private val tokenChunkEventPersistor: TokenChunkEventPersistor) : GetContextOfEventTask { override suspend fun execute(params: GetContextOfEventTask.Params): Try { val fakeEvents = RoomDataHelper.createFakeListOfEvents(30) diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/FakePaginationTask.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/FakePaginationTask.kt index a0bc1f7415..e4082b3543 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/FakePaginationTask.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/FakePaginationTask.kt @@ -21,7 +21,7 @@ import im.vector.matrix.android.internal.session.room.timeline.PaginationTask import im.vector.matrix.android.internal.session.room.timeline.TokenChunkEventPersistor import kotlin.random.Random -internal class FakePaginationTask(private val tokenChunkEventPersistor: TokenChunkEventPersistor) : PaginationTask { +internal class FakePaginationTask @Inject constructor(private val tokenChunkEventPersistor: TokenChunkEventPersistor) : PaginationTask { override suspend fun execute(params: PaginationTask.Params): Try { val fakeEvents = RoomDataHelper.createFakeListOfEvents(30) diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineTest.kt index 221887c7bf..5a470ce75c 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/session/room/timeline/TimelineTest.kt @@ -59,8 +59,8 @@ internal class TimelineTest : InstrumentedTest { private fun createTimeline(initialEventId: String? = null): Timeline { val taskExecutor = TaskExecutor(testCoroutineDispatchers) val tokenChunkEventPersistor = TokenChunkEventPersistor(monarchy) - val paginationTask = FakePaginationTask(tokenChunkEventPersistor) - val getContextOfEventTask = FakeGetContextOfEventTask(tokenChunkEventPersistor) + val paginationTask = FakePaginationTask @Inject constructor(tokenChunkEventPersistor) + val getContextOfEventTask = FakeGetContextOfEventTask @Inject constructor(tokenChunkEventPersistor) val roomMemberExtractor = SenderRoomMemberExtractor(ROOM_ID) val timelineEventFactory = TimelineEventFactory(roomMemberExtractor, EventRelationExtractor()) return DefaultTimeline( diff --git a/matrix-sdk-android/src/debug/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt b/matrix-sdk-android/src/debug/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt index edca01562a..33f460bd56 100644 --- a/matrix-sdk-android/src/debug/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt +++ b/matrix-sdk-android/src/debug/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt @@ -17,12 +17,14 @@ package im.vector.matrix.android.internal.network.interceptors +import im.vector.matrix.android.internal.di.MatrixScope import okhttp3.Interceptor import okhttp3.Response import okhttp3.logging.HttpLoggingInterceptor import okio.Buffer import java.io.IOException import java.nio.charset.Charset +import javax.inject.Inject /** * An OkHttp interceptor that logs requests as curl shell commands. They can then @@ -33,7 +35,8 @@ import java.nio.charset.Charset * information. It should only be used in a controlled manner or in a * non-production environment. */ -internal class CurlLoggingInterceptor(private val logger: HttpLoggingInterceptor.Logger = HttpLoggingInterceptor.Logger.DEFAULT) +@MatrixScope +internal class CurlLoggingInterceptor @Inject constructor(private val logger: HttpLoggingInterceptor.Logger = HttpLoggingInterceptor.Logger.DEFAULT) : Interceptor { /** diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt index b3b98847ed..9f99088904 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/Matrix.kt @@ -22,34 +22,25 @@ import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.auth.Authenticator import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.sync.FilterService -import im.vector.matrix.android.internal.auth.AuthModule -import im.vector.matrix.android.internal.di.MatrixKoinComponent -import im.vector.matrix.android.internal.di.MatrixKoinHolder -import im.vector.matrix.android.internal.di.MatrixModule -import im.vector.matrix.android.internal.di.NetworkModule import im.vector.matrix.android.internal.network.UserAgentHolder import im.vector.matrix.android.internal.util.BackgroundDetectionObserver -import org.koin.standalone.inject import java.util.concurrent.atomic.AtomicBoolean +import javax.inject.Inject /** * This is the main entry point to the matrix sdk. * This class is automatically init by a provider. * To get the singleton instance, use getInstance static method. */ -class Matrix private constructor(context: Context) : MatrixKoinComponent { +class Matrix private constructor(context: Context) { - private val authenticator by inject() - private val userAgentHolder by inject() - private val backgroundDetectionObserver by inject() + @Inject internal lateinit var authenticator: Authenticator + @Inject internal lateinit var userAgentHolder: UserAgentHolder + @Inject internal lateinit var backgroundDetectionObserver: BackgroundDetectionObserver var currentSession: Session? = null init { Monarchy.init(context) - val matrixModule = MatrixModule(context).definition - val networkModule = NetworkModule().definition - val authModule = AuthModule().definition - MatrixKoinHolder.instance.loadModules(listOf(matrixModule, networkModule, authModule)) ProcessLifecycleOwner.get().lifecycle.addObserver(backgroundDetectionObserver) authenticator.getLastActiveSession()?.also { currentSession = it diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/AuthModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/AuthModule.kt index 5a49c5124b..3653f7df2f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/AuthModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/AuthModule.kt @@ -17,38 +17,27 @@ package im.vector.matrix.android.internal.auth import android.content.Context -import im.vector.matrix.android.api.auth.Authenticator +import dagger.Module +import dagger.Provides import im.vector.matrix.android.internal.auth.db.AuthRealmModule -import im.vector.matrix.android.internal.auth.db.RealmSessionParamsStore -import im.vector.matrix.android.internal.auth.db.SessionParamsMapper +import im.vector.matrix.android.internal.di.MatrixScope import io.realm.RealmConfiguration -import org.koin.dsl.module.module import java.io.File -class AuthModule { +@Module +internal class AuthModule { - val definition = module { - - single { - DefaultAuthenticator(get(), get(), get()) as Authenticator + @Provides + @MatrixScope + fun providesRealmConfiguration(context: Context): RealmConfiguration { + val old = File(context.filesDir, "matrix-sdk-auth") + if (old.exists()) { + old.renameTo(File(context.filesDir, "matrix-sdk-auth.realm")) } - - single { - val context: Context = get() - val old = File(context.filesDir, "matrix-sdk-auth") - - if (old.exists()) { - old.renameTo(File(context.filesDir, "matrix-sdk-auth.realm")) - } - - val mapper = SessionParamsMapper((get())) - val realmConfiguration = RealmConfiguration.Builder() - .name("matrix-sdk-auth.realm") - .modules(AuthRealmModule()) - .deleteRealmIfMigrationNeeded() - .build() - RealmSessionParamsStore(mapper, realmConfiguration) as SessionParamsStore - } - + return RealmConfiguration.Builder() + .name("matrix-sdk-auth.realm") + .modules(AuthRealmModule()) + .deleteRealmIfMigrationNeeded() + .build() } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/DefaultAuthenticator.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/DefaultAuthenticator.kt index d2474f3750..8d6f8d293e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/DefaultAuthenticator.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/DefaultAuthenticator.kt @@ -19,25 +19,32 @@ package im.vector.matrix.android.internal.auth import android.util.Patterns import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.auth.Authenticator +import im.vector.matrix.android.api.auth.data.Credentials import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig +import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.util.Cancelable -import im.vector.matrix.android.api.auth.data.Credentials import im.vector.matrix.android.internal.auth.data.PasswordLoginParams -import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.internal.auth.data.ThreePidMedium +import im.vector.matrix.android.internal.di.MatrixScope import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.session.DefaultSession +import im.vector.matrix.android.internal.session.SessionFactory import im.vector.matrix.android.internal.util.CancelableCoroutine import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import retrofit2.Retrofit +import javax.inject.Inject +import kotlin.random.Random -internal class DefaultAuthenticator(private val retrofitBuilder: Retrofit.Builder, - private val coroutineDispatchers: MatrixCoroutineDispatchers, - private val sessionParamsStore: SessionParamsStore) : Authenticator { +@MatrixScope +internal class DefaultAuthenticator @Inject constructor(private val retrofitBuilder: Retrofit.Builder, + private val coroutineDispatchers: MatrixCoroutineDispatchers, + private val sessionParamsStore: SessionParamsStore, + private val sessionFactory: DefaultSession.Factory +) : Authenticator { override fun hasActiveSessions(): Boolean { return sessionParamsStore.get() != null @@ -46,7 +53,7 @@ internal class DefaultAuthenticator(private val retrofitBuilder: Retrofit.Builde override fun getLastActiveSession(): Session? { val sessionParams = sessionParamsStore.get() return sessionParams?.let { - DefaultSession(it) + sessionFactory.create(it) } } @@ -80,7 +87,7 @@ internal class DefaultAuthenticator(private val retrofitBuilder: Retrofit.Builde sessionParamsStore.save(sessionParams) sessionParams }.map { - DefaultSession(it) + sessionFactory.create(it) } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/db/RealmSessionParamsStore.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/db/RealmSessionParamsStore.kt index 084495869c..ecb442ca69 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/db/RealmSessionParamsStore.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/auth/db/RealmSessionParamsStore.kt @@ -19,11 +19,14 @@ package im.vector.matrix.android.internal.auth.db import arrow.core.Try import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.internal.auth.SessionParamsStore +import im.vector.matrix.android.internal.di.MatrixScope import io.realm.Realm import io.realm.RealmConfiguration +import javax.inject.Inject -internal class RealmSessionParamsStore(private val mapper: SessionParamsMapper, - private val realmConfiguration: RealmConfiguration) : SessionParamsStore { +@MatrixScope +internal class RealmSessionParamsStore @Inject constructor(private val mapper: SessionParamsMapper, + private val realmConfiguration: RealmConfiguration) : SessionParamsStore { override fun save(sessionParams: SessionParams): Try { return Try { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt index 8b29287d70..8b60b5c049 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt @@ -63,6 +63,7 @@ import im.vector.matrix.android.internal.crypto.verification.DefaultSasVerificat import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.di.MoshiProvider +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.cache.ClearCacheTask import im.vector.matrix.android.internal.session.room.membership.LoadRoomMembersTask import im.vector.matrix.android.internal.session.room.membership.RoomMembers @@ -76,6 +77,7 @@ import org.matrix.olm.OlmManager import timber.log.Timber import java.util.* import java.util.concurrent.atomic.AtomicBoolean +import javax.inject.Inject import kotlin.coroutines.EmptyCoroutineContext /** @@ -88,7 +90,8 @@ import kotlin.coroutines.EmptyCoroutineContext * CryptoService maintains all necessary keys and their sharing with other devices required for the crypto. * Specially, it tracks all room membership changes events in order to do keys updates. */ -internal class CryptoManager( +@SessionScope +internal class CryptoManager @Inject constructor( // The credentials, private val credentials: Credentials, private val myDeviceInfoHolder: MyDeviceInfoHolder, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt index 84029ba64b..775eaad454 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt @@ -17,316 +17,62 @@ package im.vector.matrix.android.internal.crypto import android.content.Context +import dagger.Module +import dagger.Provides import im.vector.matrix.android.api.auth.data.Credentials -import im.vector.matrix.android.api.session.crypto.CryptoService -import im.vector.matrix.android.internal.crypto.actions.* -import im.vector.matrix.android.internal.crypto.algorithms.megolm.MXMegolmDecryptionFactory -import im.vector.matrix.android.internal.crypto.algorithms.megolm.MXMegolmEncryptionFactory -import im.vector.matrix.android.internal.crypto.algorithms.olm.MXOlmDecryptionFactory -import im.vector.matrix.android.internal.crypto.algorithms.olm.MXOlmEncryptionFactory import im.vector.matrix.android.internal.crypto.api.CryptoApi -import im.vector.matrix.android.internal.crypto.keysbackup.KeysBackup import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi -import im.vector.matrix.android.internal.crypto.keysbackup.tasks.* -import im.vector.matrix.android.internal.crypto.repository.WarnOnUnknownDeviceRepository import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore import im.vector.matrix.android.internal.crypto.store.db.RealmCryptoStore import im.vector.matrix.android.internal.crypto.store.db.RealmCryptoStoreMigration import im.vector.matrix.android.internal.crypto.store.db.RealmCryptoStoreModule import im.vector.matrix.android.internal.crypto.store.db.hash -import im.vector.matrix.android.internal.crypto.tasks.* -import im.vector.matrix.android.internal.crypto.verification.DefaultSasVerificationService -import im.vector.matrix.android.internal.session.DefaultSession -import im.vector.matrix.android.internal.session.cache.ClearCacheTask -import im.vector.matrix.android.internal.session.cache.RealmClearCacheTask +import im.vector.matrix.android.internal.session.SessionScope import io.realm.RealmConfiguration -import org.koin.dsl.module.module -import org.matrix.olm.OlmManager import retrofit2.Retrofit import java.io.File +@Module internal class CryptoModule { - val definition = module(override = true) { - - /* ========================================================================================== - * Crypto Main - * ========================================================================================== */ - - // Realm configuration, named to avoid clash with main cache realm configuration - scope(DefaultSession.SCOPE, name = "CryptoRealmConfiguration") { - val context: Context = get() - - val credentials: Credentials = get() - - RealmConfiguration.Builder() - .directory(File(context.filesDir, credentials.userId.hash())) - .name("crypto_store.realm") - .modules(RealmCryptoStoreModule()) - .schemaVersion(RealmCryptoStoreMigration.CRYPTO_STORE_SCHEMA_VERSION) - .migration(RealmCryptoStoreMigration) - .build() - } - - // CryptoStore - scope(DefaultSession.SCOPE) { - RealmCryptoStore(false /* TODO*/, - get("CryptoRealmConfiguration"), - get()) as IMXCryptoStore - } - - scope(DefaultSession.SCOPE) { - val retrofit: Retrofit = get() - retrofit.create(CryptoApi::class.java) - } - - // CryptoService - scope(DefaultSession.SCOPE) { - get() as CryptoService - } - - // - scope(DefaultSession.SCOPE) { - OutgoingRoomKeyRequestManager(get(), get(), get()) - } - - scope(DefaultSession.SCOPE) { - IncomingRoomKeyRequestManager(get(), get(), get()) - } - - scope(DefaultSession.SCOPE) { - RoomDecryptorProvider(get(), get()) - } - - scope(DefaultSession.SCOPE) { - // Ensure OlmManager is loaded first - get() - MXOlmDevice(get()) - } - - // ObjectSigner - scope(DefaultSession.SCOPE) { - ObjectSigner(get(), get()) - } - - // OneTimeKeysUploader - scope(DefaultSession.SCOPE) { - OneTimeKeysUploader(get(), get(), get(), get()) - } - - // Actions - scope(DefaultSession.SCOPE) { - SetDeviceVerificationAction(get(), get(), get()) - } - - // Device info - scope(DefaultSession.SCOPE) { - MyDeviceInfoHolder(get(), get(), get()) - } - - scope(DefaultSession.SCOPE) { - EnsureOlmSessionsForDevicesAction(get(), get()) - } - - scope(DefaultSession.SCOPE) { - EnsureOlmSessionsForUsersAction(get(), get(), get()) - } - - scope(DefaultSession.SCOPE) { - MegolmSessionDataImporter(get(), get(), get(), get()) - } - - scope(DefaultSession.SCOPE) { - MessageEncrypter(get(), get()) - } - - - scope(DefaultSession.SCOPE) { - WarnOnUnknownDeviceRepository() - } - - // Factories - scope(DefaultSession.SCOPE) { - MXMegolmDecryptionFactory( - get(), get(), get(), get(), get(), get(), get(), get(), get() - ) - } - - scope(DefaultSession.SCOPE) { - MXMegolmEncryptionFactory( - get(), get(), get(), get(), get(), get(), get(), get(), get(), get() - ) - } - - scope(DefaultSession.SCOPE) { - MXOlmDecryptionFactory( - get(), get() - ) - } - - scope(DefaultSession.SCOPE) { - MXOlmEncryptionFactory( - get(), get(), get(), get(), get(), get() - ) - } - - // CryptoManager - scope(DefaultSession.SCOPE) { - CryptoManager( - credentials = get(), - myDeviceInfoHolder = get(), - cryptoStore = get(), - olmDevice = get(), - cryptoConfig = get(), - deviceListManager = get(), - keysBackup = get(), - objectSigner = get(), - oneTimeKeysUploader = get(), - roomDecryptorProvider = get(), - sasVerificationService = get(), - incomingRoomKeyRequestManager = get(), - outgoingRoomKeyRequestManager = get(), - olmManager = get(), - setDeviceVerificationAction = get(), - megolmSessionDataImporter = get(), - warnOnUnknownDevicesRepository = get(), - megolmEncryptionFactory = get(), - olmEncryptionFactory = get(), - deleteDeviceTask = get(), - // Tasks - getDevicesTask = get(), - setDeviceNameTask = get(), - uploadKeysTask = get(), - loadRoomMembersTask = get(), - clearCryptoDataTask = get("ClearTaskCryptoCache"), - monarchy = get(), - coroutineDispatchers = get(), - taskExecutor = get() - ) - } - - // Olm manager - single { - // load the crypto libs. - OlmManager() - } - - - // Crypto config - scope(DefaultSession.SCOPE) { - MXCryptoConfig() - } - - // Device list - scope(DefaultSession.SCOPE) { - DeviceListManager(get(), get(), get(), get(), get()) - } - - // Crypto tasks - scope(DefaultSession.SCOPE) { - DefaultClaimOneTimeKeysForUsersDevice(get()) as ClaimOneTimeKeysForUsersDeviceTask - } - scope(DefaultSession.SCOPE) { - DefaultDeleteDeviceTask(get()) as DeleteDeviceTask - } - scope(DefaultSession.SCOPE) { - DefaultDownloadKeysForUsers(get()) as DownloadKeysForUsersTask - } - scope(DefaultSession.SCOPE) { - DefaultGetDevicesTask(get()) as GetDevicesTask - } - scope(DefaultSession.SCOPE) { - DefaultGetKeyChangesTask(get()) as GetKeyChangesTask - } - scope(DefaultSession.SCOPE) { - DefaultSendToDeviceTask(get()) as SendToDeviceTask - } - scope(DefaultSession.SCOPE) { - DefaultSetDeviceNameTask(get()) as SetDeviceNameTask - } - scope(DefaultSession.SCOPE) { - DefaultUploadKeysTask(get()) as UploadKeysTask - } - - scope(DefaultSession.SCOPE, name = "ClearTaskCryptoCache") { - RealmClearCacheTask(get("CryptoRealmConfiguration")) as ClearCacheTask - } - - /* ========================================================================================== - * Keys backup - * ========================================================================================== */ - - scope(DefaultSession.SCOPE) { - val retrofit: Retrofit = get() - retrofit.create(RoomKeysApi::class.java) - } - - scope(DefaultSession.SCOPE) { - KeysBackup( - // Credentials - get(), - // CryptoStore - get(), - get(), - get(), - get(), - // Task - get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), - // Task executor - get()) - } - - // Key backup tasks - scope(DefaultSession.SCOPE) { - DefaultCreateKeysBackupVersionTask(get()) as CreateKeysBackupVersionTask - } - scope(DefaultSession.SCOPE) { - DefaultDeleteBackupTask(get()) as DeleteBackupTask - } - scope(DefaultSession.SCOPE) { - DefaultDeleteRoomSessionDataTask(get()) as DeleteRoomSessionDataTask - } - scope(DefaultSession.SCOPE) { - DefaultDeleteRoomSessionsDataTask(get()) as DeleteRoomSessionsDataTask - } - scope(DefaultSession.SCOPE) { - DefaultDeleteSessionsDataTask(get()) as DeleteSessionsDataTask - } - scope(DefaultSession.SCOPE) { - DefaultGetKeysBackupLastVersionTask(get()) as GetKeysBackupLastVersionTask - } - scope(DefaultSession.SCOPE) { - DefaultGetKeysBackupVersionTask(get()) as GetKeysBackupVersionTask - } - scope(DefaultSession.SCOPE) { - DefaultGetRoomSessionDataTask(get()) as GetRoomSessionDataTask - } - scope(DefaultSession.SCOPE) { - DefaultGetRoomSessionsDataTask(get()) as GetRoomSessionsDataTask - } - scope(DefaultSession.SCOPE) { - DefaultGetSessionsDataTask(get()) as GetSessionsDataTask - } - scope(DefaultSession.SCOPE) { - DefaultStoreRoomSessionDataTask(get()) as StoreRoomSessionDataTask - } - scope(DefaultSession.SCOPE) { - DefaultStoreRoomSessionsDataTask(get()) as StoreRoomSessionsDataTask - } - scope(DefaultSession.SCOPE) { - DefaultStoreSessionsDataTask(get()) as StoreSessionsDataTask - } - scope(DefaultSession.SCOPE) { - DefaultUpdateKeysBackupVersionTask(get()) as UpdateKeysBackupVersionTask - } - - /* ========================================================================================== - * SAS Verification - * ========================================================================================== */ - - scope(DefaultSession.SCOPE) { - DefaultSasVerificationService(get(), get(), get(), get(), get(), get(), get(), get()) - } - + // Realm configuration, named to avoid clash with main cache realm configuration + @Provides + @SessionScope + fun providesRealmConfiguration(context: Context, credentials: Credentials): RealmConfiguration { + return RealmConfiguration.Builder() + .directory(File(context.filesDir, credentials.userId.hash())) + .name("crypto_store.realm") + .modules(RealmCryptoStoreModule()) + .schemaVersion(RealmCryptoStoreMigration.CRYPTO_STORE_SCHEMA_VERSION) + .migration(RealmCryptoStoreMigration) + .build() } + + @Provides + @SessionScope + fun providesCryptoStore(realmConfiguration: RealmConfiguration, credentials: Credentials): IMXCryptoStore { + return RealmCryptoStore(false /* TODO*/, + realmConfiguration, + credentials) + } + + @Provides + @SessionScope + fun providesCryptoAPI(retrofit: Retrofit): CryptoApi { + return retrofit.create(CryptoApi::class.java) + } + + @Provides + @SessionScope + fun providesRoomKeysAPI(retrofit: Retrofit): RoomKeysApi { + return retrofit.create(RoomKeysApi::class.java) + } + + @Provides + @SessionScope + fun providesCryptoConfig(): MXCryptoConfig { + return MXCryptoConfig() + } + + } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt index 2217a79616..f58901d18b 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt @@ -26,16 +26,20 @@ import im.vector.matrix.android.internal.crypto.model.MXDeviceInfo import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore import im.vector.matrix.android.internal.crypto.tasks.DownloadKeysForUsersTask +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.sync.SyncTokenStore import timber.log.Timber import java.util.* +import javax.inject.Inject // Legacy name: MXDeviceList -internal class DeviceListManager(private val cryptoStore: IMXCryptoStore, - private val olmDevice: MXOlmDevice, - private val syncTokenStore: SyncTokenStore, - private val credentials: Credentials, - private val downloadKeysForUsersTask: DownloadKeysForUsersTask) { + +@SessionScope +internal class DeviceListManager @Inject constructor(private val cryptoStore: IMXCryptoStore, + private val olmDevice: MXOlmDevice, + private val syncTokenStore: SyncTokenStore, + private val credentials: Credentials, + private val downloadKeysForUsersTask: DownloadKeysForUsersTask) { // HS not ready for retry private val notReadyToRetryHS = HashSet() @@ -410,7 +414,7 @@ internal class DeviceListManager(private val cryptoStore: IMXCryptoStore, if (!isVerified) { Timber.e("## validateDeviceKeys() : Unable to verify signature on device " + userId + ":" - + deviceKeys.deviceId + " with error " + errorMessage) + + deviceKeys.deviceId + " with error " + errorMessage) return false } @@ -421,8 +425,8 @@ internal class DeviceListManager(private val cryptoStore: IMXCryptoStore, // // Should we warn the user about it somehow? Timber.e("## validateDeviceKeys() : WARNING:Ed25519 key for device " + userId + ":" - + deviceKeys.deviceId + " has changed : " - + previouslyStoredDeviceKeys.fingerprint() + " -> " + signKey) + + deviceKeys.deviceId + " has changed : " + + previouslyStoredDeviceKeys.fingerprint() + " -> " + signKey) Timber.e("## validateDeviceKeys() : $previouslyStoredDeviceKeys -> $deviceKeys") Timber.e("## validateDeviceKeys() : " + previouslyStoredDeviceKeys.keys + " -> " + deviceKeys.keys) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/IncomingRoomKeyRequestManager.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/IncomingRoomKeyRequestManager.kt index 37d76340cb..7901ca1a7e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/IncomingRoomKeyRequestManager.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/IncomingRoomKeyRequestManager.kt @@ -23,11 +23,14 @@ import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyShare import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore +import im.vector.matrix.android.internal.session.SessionScope import timber.log.Timber import java.util.* +import javax.inject.Inject import kotlin.collections.ArrayList -internal class IncomingRoomKeyRequestManager( +@SessionScope +internal class IncomingRoomKeyRequestManager @Inject constructor( private val credentials: Credentials, private val cryptoStore: IMXCryptoStore, private val roomDecryptorProvider: RoomDecryptorProvider) { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/MXOlmDevice.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/MXOlmDevice.kt index 12381a5b7e..243ddea66e 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/MXOlmDevice.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/MXOlmDevice.kt @@ -26,15 +26,23 @@ import im.vector.matrix.android.internal.crypto.model.OlmInboundGroupSessionWrap import im.vector.matrix.android.internal.crypto.model.OlmSessionWrapper import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore import im.vector.matrix.android.internal.di.MoshiProvider +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.util.convertFromUTF8 import im.vector.matrix.android.internal.util.convertToUTF8 -import org.matrix.olm.* +import org.matrix.olm.OlmAccount +import org.matrix.olm.OlmInboundGroupSession +import org.matrix.olm.OlmMessage +import org.matrix.olm.OlmOutboundGroupSession +import org.matrix.olm.OlmSession +import org.matrix.olm.OlmUtility import timber.log.Timber import java.net.URLEncoder import java.util.* +import javax.inject.Inject // The libolm wrapper. -internal class MXOlmDevice( +@SessionScope +internal class MXOlmDevice @Inject constructor( /** * The store where crypto data is saved. */ @@ -670,7 +678,7 @@ internal class MXOlmDevice( val reason = String.format(MXCryptoError.DUPLICATE_MESSAGE_INDEX_REASON, decryptResult.mIndex) Timber.e("## decryptGroupMessage() : $reason") throw MXDecryptionException(MXCryptoError(MXCryptoError.DUPLICATED_MESSAGE_INDEX_ERROR_CODE, - MXCryptoError.UNABLE_TO_DECRYPT, reason)) + MXCryptoError.UNABLE_TO_DECRYPT, reason)) } inboundGroupSessionMessageIndexes[timeline]!!.put(messageIndexKey, true) @@ -703,7 +711,7 @@ internal class MXOlmDevice( val reason = String.format(MXCryptoError.INBOUND_SESSION_MISMATCH_ROOM_ID_REASON, roomId, session.roomId) Timber.e("## decryptGroupMessage() : $reason") throw MXDecryptionException(MXCryptoError(MXCryptoError.INBOUND_SESSION_MISMATCH_ROOM_ID_ERROR_CODE, - MXCryptoError.UNABLE_TO_DECRYPT, reason)) + MXCryptoError.UNABLE_TO_DECRYPT, reason)) } } else { Timber.e("## decryptGroupMessage() : Cannot retrieve inbound group session $sessionId") diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/MyDeviceInfoHolder.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/MyDeviceInfoHolder.kt index 80e03f5d09..4223983298 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/MyDeviceInfoHolder.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/MyDeviceInfoHolder.kt @@ -20,9 +20,12 @@ import android.text.TextUtils import im.vector.matrix.android.api.auth.data.Credentials import im.vector.matrix.android.internal.crypto.model.MXDeviceInfo import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore +import im.vector.matrix.android.internal.session.SessionScope import java.util.* +import javax.inject.Inject -internal class MyDeviceInfoHolder( +@SessionScope +internal class MyDeviceInfoHolder @Inject constructor( // The credentials, credentials: Credentials, // the crypto store diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/ObjectSigner.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/ObjectSigner.kt index 78359abc41..2e91cb7a7f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/ObjectSigner.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/ObjectSigner.kt @@ -17,9 +17,12 @@ package im.vector.matrix.android.internal.crypto import im.vector.matrix.android.api.auth.data.Credentials +import im.vector.matrix.android.internal.session.SessionScope import java.util.* +import javax.inject.Inject -internal class ObjectSigner(private val credentials: Credentials, +@SessionScope +internal class ObjectSigner @Inject constructor(private val credentials: Credentials, private val olmDevice: MXOlmDevice) { /** diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OneTimeKeysUploader.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OneTimeKeysUploader.kt index 20c6800eaf..27bd7e2a6d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OneTimeKeysUploader.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OneTimeKeysUploader.kt @@ -23,11 +23,14 @@ import im.vector.matrix.android.internal.crypto.model.MXKey import im.vector.matrix.android.internal.crypto.model.rest.KeysUploadResponse import im.vector.matrix.android.internal.crypto.tasks.UploadKeysTask import im.vector.matrix.android.internal.di.MoshiProvider +import im.vector.matrix.android.internal.session.SessionScope import org.matrix.olm.OlmAccount import timber.log.Timber import java.util.* +import javax.inject.Inject -internal class OneTimeKeysUploader( +@SessionScope +internal class OneTimeKeysUploader @Inject constructor( private val credentials: Credentials, private val olmDevice: MXOlmDevice, private val objectSigner: ObjectSigner, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OutgoingRoomKeyRequestManager.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OutgoingRoomKeyRequestManager.kt index 4c68e905da..a07b136c97 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OutgoingRoomKeyRequestManager.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OutgoingRoomKeyRequestManager.kt @@ -26,12 +26,15 @@ import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyShareCancellat import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyShareRequest import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith import timber.log.Timber import java.util.* +import javax.inject.Inject -internal class OutgoingRoomKeyRequestManager( +@SessionScope +internal class OutgoingRoomKeyRequestManager @Inject constructor( private val cryptoStore: IMXCryptoStore, private val sendToDeviceTask: SendToDeviceTask, private val taskExecutor: TaskExecutor) { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/RoomDecryptorProvider.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/RoomDecryptorProvider.kt index e75885a3cb..6e30484d8c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/RoomDecryptorProvider.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/RoomDecryptorProvider.kt @@ -20,10 +20,13 @@ import android.text.TextUtils import im.vector.matrix.android.internal.crypto.algorithms.IMXDecrypting import im.vector.matrix.android.internal.crypto.algorithms.megolm.MXMegolmDecryptionFactory import im.vector.matrix.android.internal.crypto.algorithms.olm.MXOlmDecryptionFactory +import im.vector.matrix.android.internal.session.SessionScope import timber.log.Timber import java.util.* +import javax.inject.Inject -internal class RoomDecryptorProvider( +@SessionScope +internal class RoomDecryptorProvider @Inject constructor( private val olmDecryptionFactory: MXOlmDecryptionFactory, private val megolmDecryptionFactory: MXMegolmDecryptionFactory ) { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/EnsureOlmSessionsForDevicesAction.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/EnsureOlmSessionsForDevicesAction.kt index da357c6a2d..6ea04594dd 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/EnsureOlmSessionsForDevicesAction.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/EnsureOlmSessionsForDevicesAction.kt @@ -24,11 +24,14 @@ import im.vector.matrix.android.internal.crypto.model.MXKey import im.vector.matrix.android.internal.crypto.model.MXOlmSessionResult import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap import im.vector.matrix.android.internal.crypto.tasks.ClaimOneTimeKeysForUsersDeviceTask +import im.vector.matrix.android.internal.session.SessionScope import timber.log.Timber import java.util.* +import javax.inject.Inject -internal class EnsureOlmSessionsForDevicesAction(private val olmDevice: MXOlmDevice, - private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask) { +@SessionScope +internal class EnsureOlmSessionsForDevicesAction @Inject constructor(private val olmDevice: MXOlmDevice, + private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask) { suspend fun handle(devicesByUser: Map>): Try> { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/EnsureOlmSessionsForUsersAction.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/EnsureOlmSessionsForUsersAction.kt index 6b7e28be1b..0f784b6942 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/EnsureOlmSessionsForUsersAction.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/EnsureOlmSessionsForUsersAction.kt @@ -24,10 +24,13 @@ import im.vector.matrix.android.internal.crypto.model.MXDeviceInfo import im.vector.matrix.android.internal.crypto.model.MXOlmSessionResult import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore +import im.vector.matrix.android.internal.session.SessionScope import timber.log.Timber import java.util.* +import javax.inject.Inject -internal class EnsureOlmSessionsForUsersAction(private val olmDevice: MXOlmDevice, +@SessionScope +internal class EnsureOlmSessionsForUsersAction @Inject constructor(private val olmDevice: MXOlmDevice, private val cryptoStore: IMXCryptoStore, private val ensureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction) { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/MegolmSessionDataImporter.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/MegolmSessionDataImporter.kt index 07c8b465f4..a311ee0a49 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/MegolmSessionDataImporter.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/MegolmSessionDataImporter.kt @@ -26,12 +26,15 @@ import im.vector.matrix.android.internal.crypto.RoomDecryptorProvider import im.vector.matrix.android.internal.crypto.model.ImportRoomKeysResult import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyRequestBody import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore +import im.vector.matrix.android.internal.session.SessionScope import timber.log.Timber +import javax.inject.Inject -internal class MegolmSessionDataImporter(private val olmDevice: MXOlmDevice, - private val roomDecryptorProvider: RoomDecryptorProvider, - private val outgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager, - private val cryptoStore: IMXCryptoStore) { +@SessionScope +internal class MegolmSessionDataImporter @Inject constructor(private val olmDevice: MXOlmDevice, + private val roomDecryptorProvider: RoomDecryptorProvider, + private val outgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager, + private val cryptoStore: IMXCryptoStore) { /** * Import a list of megolm session keys. diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/MessageEncrypter.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/MessageEncrypter.kt index bbd23dad25..fd063df9b3 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/MessageEncrypter.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/MessageEncrypter.kt @@ -23,12 +23,15 @@ import im.vector.matrix.android.internal.crypto.MXOlmDevice import im.vector.matrix.android.internal.crypto.model.MXDeviceInfo import im.vector.matrix.android.internal.crypto.model.rest.EncryptedMessage import im.vector.matrix.android.internal.di.MoshiProvider +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.util.convertToUTF8 import timber.log.Timber import java.util.* +import javax.inject.Inject -internal class MessageEncrypter(private val credentials: Credentials, - private val olmDevice: MXOlmDevice) { +@SessionScope +internal class MessageEncrypter @Inject constructor(private val credentials: Credentials, + private val olmDevice: MXOlmDevice) { /** * Encrypt an event payload for a list of devices. diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/SetDeviceVerificationAction.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/SetDeviceVerificationAction.kt index b76f905bb9..d545007774 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/SetDeviceVerificationAction.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/actions/SetDeviceVerificationAction.kt @@ -19,9 +19,12 @@ package im.vector.matrix.android.internal.crypto.actions import im.vector.matrix.android.api.auth.data.Credentials import im.vector.matrix.android.internal.crypto.keysbackup.KeysBackup import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore +import im.vector.matrix.android.internal.session.SessionScope import timber.log.Timber +import javax.inject.Inject -internal class SetDeviceVerificationAction(private val cryptoStore: IMXCryptoStore, +@SessionScope +internal class SetDeviceVerificationAction @Inject constructor(private val cryptoStore: IMXCryptoStore, private val credentials: Credentials, private val keysBackup: KeysBackup) { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryptionFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryptionFactory.kt index 1f2a3758a7..2f9a61cfbb 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryptionFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmDecryptionFactory.kt @@ -24,9 +24,12 @@ import im.vector.matrix.android.internal.crypto.actions.EnsureOlmSessionsForDevi import im.vector.matrix.android.internal.crypto.actions.MessageEncrypter import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers +import javax.inject.Inject -internal class MXMegolmDecryptionFactory(private val credentials: Credentials, +@SessionScope +internal class MXMegolmDecryptionFactory @Inject constructor(private val credentials: Credentials, private val olmDevice: MXOlmDevice, private val deviceListManager: DeviceListManager, private val outgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryptionFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryptionFactory.kt index e7daf9adce..0fad79a696 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryptionFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryptionFactory.kt @@ -25,9 +25,12 @@ import im.vector.matrix.android.internal.crypto.keysbackup.KeysBackup import im.vector.matrix.android.internal.crypto.repository.WarnOnUnknownDeviceRepository import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.TaskExecutor +import javax.inject.Inject -internal class MXMegolmEncryptionFactory( +@SessionScope +internal class MXMegolmEncryptionFactory @Inject constructor( private val olmDevice: MXOlmDevice, private val keysBackup: KeysBackup, private val cryptoStore: IMXCryptoStore, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/olm/MXOlmDecryptionFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/olm/MXOlmDecryptionFactory.kt index 5f18fe3ec4..4fbf88bf0c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/olm/MXOlmDecryptionFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/olm/MXOlmDecryptionFactory.kt @@ -18,9 +18,12 @@ package im.vector.matrix.android.internal.crypto.algorithms.olm import im.vector.matrix.android.api.auth.data.Credentials import im.vector.matrix.android.internal.crypto.MXOlmDevice +import im.vector.matrix.android.internal.session.SessionScope +import javax.inject.Inject -internal class MXOlmDecryptionFactory(private val olmDevice: MXOlmDevice, - private val credentials: Credentials) { +@SessionScope +internal class MXOlmDecryptionFactory @Inject constructor(private val olmDevice: MXOlmDevice, + private val credentials: Credentials) { fun create(): MXOlmDecryption { return MXOlmDecryption( diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/olm/MXOlmEncryptionFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/olm/MXOlmEncryptionFactory.kt index 30c66a3e10..ef047d6c6b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/olm/MXOlmEncryptionFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/olm/MXOlmEncryptionFactory.kt @@ -21,14 +21,17 @@ import im.vector.matrix.android.internal.crypto.MXOlmDevice import im.vector.matrix.android.internal.crypto.actions.EnsureOlmSessionsForUsersAction import im.vector.matrix.android.internal.crypto.actions.MessageEncrypter import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers +import javax.inject.Inject -internal class MXOlmEncryptionFactory(private val olmDevice: MXOlmDevice, - private val cryptoStore: IMXCryptoStore, - private val messageEncrypter: MessageEncrypter, - private val deviceListManager: DeviceListManager, - private val coroutineDispatchers: MatrixCoroutineDispatchers, - private val ensureOlmSessionsForUsersAction: EnsureOlmSessionsForUsersAction) { +@SessionScope +internal class MXOlmEncryptionFactory @Inject constructor(private val olmDevice: MXOlmDevice, + private val cryptoStore: IMXCryptoStore, + private val messageEncrypter: MessageEncrypter, + private val deviceListManager: DeviceListManager, + private val coroutineDispatchers: MatrixCoroutineDispatchers, + private val ensureOlmSessionsForUsersAction: EnsureOlmSessionsForUsersAction) { fun create(roomId: String): MXOlmEncryption { return MXOlmEncryption( diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackup.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackup.kt index b80b063fbc..673fd6dbb2 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackup.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackup.kt @@ -47,6 +47,7 @@ import im.vector.matrix.android.internal.crypto.model.OlmInboundGroupSessionWrap import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore import im.vector.matrix.android.internal.crypto.store.db.model.KeysBackupDataEntity import im.vector.matrix.android.internal.di.MoshiProvider +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.TaskThread @@ -58,13 +59,16 @@ import org.matrix.olm.OlmPkMessage import timber.log.Timber import java.security.InvalidParameterException import java.util.* +import javax.inject.Inject import kotlin.collections.HashMap /** * A KeysBackup class instance manage incremental backup of e2e keys (megolm keys) * to the user's homeserver. */ -internal class KeysBackup( + +@SessionScope +internal class KeysBackup @Inject constructor( private val credentials: Credentials, private val cryptoStore: IMXCryptoStore, private val olmDevice: MXOlmDevice, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/CreateKeysBackupVersionTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/CreateKeysBackupVersionTask.kt index e63201bd4e..feb4a189ac 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/CreateKeysBackupVersionTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/CreateKeysBackupVersionTask.kt @@ -21,12 +21,14 @@ import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.CreateKeysBackupVersionBody import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.KeysVersion import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface CreateKeysBackupVersionTask : Task - -internal class DefaultCreateKeysBackupVersionTask(private val roomKeysApi: RoomKeysApi) +@SessionScope +internal class DefaultCreateKeysBackupVersionTask @Inject constructor(private val roomKeysApi: RoomKeysApi) : CreateKeysBackupVersionTask { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteBackupTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteBackupTask.kt index 34d183a17a..6d85763666 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteBackupTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteBackupTask.kt @@ -19,7 +19,9 @@ package im.vector.matrix.android.internal.crypto.keysbackup.tasks import arrow.core.Try import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface DeleteBackupTask : Task { @@ -28,8 +30,8 @@ internal interface DeleteBackupTask : Task { ) } - -internal class DefaultDeleteBackupTask(private val roomKeysApi: RoomKeysApi) +@SessionScope +internal class DefaultDeleteBackupTask @Inject constructor(private val roomKeysApi: RoomKeysApi) : DeleteBackupTask { override suspend fun execute(params: DeleteBackupTask.Params): Try { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteRoomSessionDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteRoomSessionDataTask.kt index e80e160bdf..3edd90a873 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteRoomSessionDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteRoomSessionDataTask.kt @@ -19,7 +19,9 @@ package im.vector.matrix.android.internal.crypto.keysbackup.tasks import arrow.core.Try import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface DeleteRoomSessionDataTask : Task { data class Params( @@ -29,8 +31,8 @@ internal interface DeleteRoomSessionDataTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteRoomSessionsDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteRoomSessionsDataTask.kt index 25e930f427..71dd780144 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteRoomSessionsDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteRoomSessionsDataTask.kt @@ -19,7 +19,9 @@ package im.vector.matrix.android.internal.crypto.keysbackup.tasks import arrow.core.Try import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface DeleteRoomSessionsDataTask : Task { data class Params( @@ -28,8 +30,8 @@ internal interface DeleteRoomSessionsDataTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteSessionsDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteSessionsDataTask.kt index 1580849ade..dfdf3afa3b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteSessionsDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/DeleteSessionsDataTask.kt @@ -19,7 +19,9 @@ package im.vector.matrix.android.internal.crypto.keysbackup.tasks import arrow.core.Try import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface DeleteSessionsDataTask : Task { data class Params( @@ -27,8 +29,8 @@ internal interface DeleteSessionsDataTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetKeysBackupLastVersionTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetKeysBackupLastVersionTask.kt index eb5d8899ba..ec6029bc14 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetKeysBackupLastVersionTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetKeysBackupLastVersionTask.kt @@ -20,12 +20,14 @@ import arrow.core.Try import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.KeysVersionResult import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface GetKeysBackupLastVersionTask : Task - -internal class DefaultGetKeysBackupLastVersionTask(private val roomKeysApi: RoomKeysApi) +@SessionScope +internal class DefaultGetKeysBackupLastVersionTask @Inject constructor(private val roomKeysApi: RoomKeysApi) : GetKeysBackupLastVersionTask { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetKeysBackupVersionTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetKeysBackupVersionTask.kt index 209c8136b9..0016a01b29 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetKeysBackupVersionTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetKeysBackupVersionTask.kt @@ -20,12 +20,14 @@ import arrow.core.Try import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.KeysVersionResult import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface GetKeysBackupVersionTask : Task - -internal class DefaultGetKeysBackupVersionTask(private val roomKeysApi: RoomKeysApi) +@SessionScope +internal class DefaultGetKeysBackupVersionTask @Inject constructor(private val roomKeysApi: RoomKeysApi) : GetKeysBackupVersionTask { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetRoomSessionDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetRoomSessionDataTask.kt index 849e37ebcf..05aee672d3 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetRoomSessionDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetRoomSessionDataTask.kt @@ -20,7 +20,9 @@ import arrow.core.Try import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.KeyBackupData import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface GetRoomSessionDataTask : Task { data class Params( @@ -30,8 +32,8 @@ internal interface GetRoomSessionDataTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetRoomSessionsDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetRoomSessionsDataTask.kt index e67e2bc3f7..fa466815d3 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetRoomSessionsDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetRoomSessionsDataTask.kt @@ -20,7 +20,9 @@ import arrow.core.Try import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.RoomKeysBackupData import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface GetRoomSessionsDataTask : Task { @@ -30,8 +32,8 @@ internal interface GetRoomSessionsDataTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetSessionsDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetSessionsDataTask.kt index b2f3005bd6..f541df78d3 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetSessionsDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/GetSessionsDataTask.kt @@ -20,7 +20,9 @@ import arrow.core.Try import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.KeysBackupData import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface GetSessionsDataTask : Task { data class Params( @@ -28,8 +30,8 @@ internal interface GetSessionsDataTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreRoomSessionDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreRoomSessionDataTask.kt index e7452b0e15..c100580181 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreRoomSessionDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreRoomSessionDataTask.kt @@ -21,7 +21,9 @@ import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.KeyBackupData import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.BackupKeysResult import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface StoreRoomSessionDataTask : Task { data class Params( @@ -32,8 +34,8 @@ internal interface StoreRoomSessionDataTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreRoomSessionsDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreRoomSessionsDataTask.kt index 29ab818ddf..edaa666c00 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreRoomSessionsDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreRoomSessionsDataTask.kt @@ -21,7 +21,9 @@ import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.RoomKeysBackupData import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.BackupKeysResult import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface StoreRoomSessionsDataTask : Task { data class Params( @@ -31,8 +33,8 @@ internal interface StoreRoomSessionsDataTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreSessionsDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreSessionsDataTask.kt index c5b39d2dae..252dc906f3 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreSessionsDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/StoreSessionsDataTask.kt @@ -21,7 +21,9 @@ import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.KeysBackupData import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.BackupKeysResult import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface StoreSessionsDataTask : Task { data class Params( @@ -30,8 +32,8 @@ internal interface StoreSessionsDataTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/UpdateKeysBackupVersionTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/UpdateKeysBackupVersionTask.kt index e2605bfbaf..7f060e68da 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/UpdateKeysBackupVersionTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/tasks/UpdateKeysBackupVersionTask.kt @@ -20,7 +20,9 @@ import arrow.core.Try import im.vector.matrix.android.internal.crypto.keysbackup.api.RoomKeysApi import im.vector.matrix.android.internal.crypto.keysbackup.model.rest.UpdateKeysBackupVersionBody import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface UpdateKeysBackupVersionTask : Task { data class Params( @@ -29,8 +31,8 @@ internal interface UpdateKeysBackupVersionTask : Task> { data class Params( @@ -34,7 +36,8 @@ internal interface ClaimOneTimeKeysForUsersDeviceTask : Task> { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/DeleteDeviceTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/DeleteDeviceTask.kt index 0fb9ad0f15..acd84b4125 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/DeleteDeviceTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/DeleteDeviceTask.kt @@ -20,7 +20,9 @@ import arrow.core.Try import im.vector.matrix.android.internal.crypto.api.CryptoApi import im.vector.matrix.android.internal.crypto.model.rest.DeleteDeviceParams import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface DeleteDeviceTask : Task { data class Params( @@ -29,7 +31,8 @@ internal interface DeleteDeviceTask : Task { ) } -internal class DefaultDeleteDeviceTask(private val cryptoApi: CryptoApi) +@SessionScope +internal class DefaultDeleteDeviceTask @Inject constructor(private val cryptoApi: CryptoApi) : DeleteDeviceTask { override suspend fun execute(params: DeleteDeviceTask.Params): Try { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/DownloadKeysForUsersTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/DownloadKeysForUsersTask.kt index a8da9eab5f..09539ade6f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/DownloadKeysForUsersTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/DownloadKeysForUsersTask.kt @@ -22,8 +22,10 @@ import im.vector.matrix.android.internal.crypto.api.CryptoApi import im.vector.matrix.android.internal.crypto.model.rest.KeysQueryBody import im.vector.matrix.android.internal.crypto.model.rest.KeysQueryResponse import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task import java.util.* +import javax.inject.Inject internal interface DownloadKeysForUsersTask : Task { data class Params( @@ -33,7 +35,8 @@ internal interface DownloadKeysForUsersTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/GetDevicesTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/GetDevicesTask.kt index 7d634ab5d0..d0296a3980 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/GetDevicesTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/GetDevicesTask.kt @@ -20,11 +20,14 @@ import arrow.core.Try import im.vector.matrix.android.internal.crypto.api.CryptoApi import im.vector.matrix.android.internal.crypto.model.rest.DevicesListResponse import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface GetDevicesTask : Task -internal class DefaultGetDevicesTask(private val cryptoApi: CryptoApi) +@SessionScope +internal class DefaultGetDevicesTask @Inject constructor(private val cryptoApi: CryptoApi) : GetDevicesTask { override suspend fun execute(params: Unit): Try { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/GetKeyChangesTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/GetKeyChangesTask.kt index 937607d95d..a89dabea57 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/GetKeyChangesTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/GetKeyChangesTask.kt @@ -20,7 +20,9 @@ import arrow.core.Try import im.vector.matrix.android.internal.crypto.api.CryptoApi import im.vector.matrix.android.internal.crypto.model.rest.KeyChangesResponse import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface GetKeyChangesTask : Task { data class Params( @@ -31,7 +33,8 @@ internal interface GetKeyChangesTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/SendToDeviceTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/SendToDeviceTask.kt index 89417b087c..6e6fcff327 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/SendToDeviceTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/SendToDeviceTask.kt @@ -21,8 +21,10 @@ import im.vector.matrix.android.internal.crypto.api.CryptoApi import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap import im.vector.matrix.android.internal.crypto.model.rest.SendToDeviceBody import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task import java.util.* +import javax.inject.Inject internal interface SendToDeviceTask : Task { data class Params( @@ -35,7 +37,8 @@ internal interface SendToDeviceTask : Task { ) } -internal class DefaultSendToDeviceTask(private val cryptoApi: CryptoApi) +@SessionScope +internal class DefaultSendToDeviceTask @Inject constructor(private val cryptoApi: CryptoApi) : SendToDeviceTask { override suspend fun execute(params: SendToDeviceTask.Params): Try { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/SetDeviceNameTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/SetDeviceNameTask.kt index 22daf51775..0997a4b2bb 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/SetDeviceNameTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/SetDeviceNameTask.kt @@ -21,7 +21,9 @@ import arrow.core.Try import im.vector.matrix.android.internal.crypto.api.CryptoApi import im.vector.matrix.android.internal.crypto.model.rest.UpdateDeviceInfoBody import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface SetDeviceNameTask : Task { data class Params( @@ -32,7 +34,8 @@ internal interface SetDeviceNameTask : Task { ) } -internal class DefaultSetDeviceNameTask(private val cryptoApi: CryptoApi) +@SessionScope +internal class DefaultSetDeviceNameTask @Inject constructor(private val cryptoApi: CryptoApi) : SetDeviceNameTask { override suspend fun execute(params: SetDeviceNameTask.Params): Try { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/UploadKeysTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/UploadKeysTask.kt index 067f8ff789..434803c271 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/UploadKeysTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/tasks/UploadKeysTask.kt @@ -23,8 +23,10 @@ import im.vector.matrix.android.internal.crypto.model.rest.KeysUploadResponse import im.vector.matrix.android.internal.crypto.model.rest.DeviceKeys import im.vector.matrix.android.internal.crypto.model.rest.KeysUploadBody import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.util.convertToUTF8 +import javax.inject.Inject internal interface UploadKeysTask : Task { data class Params( @@ -36,7 +38,8 @@ internal interface UploadKeysTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt index 81556052a7..803bcb98da 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt @@ -40,6 +40,7 @@ import im.vector.matrix.android.internal.crypto.model.rest.KeyVerificationMac import im.vector.matrix.android.internal.crypto.model.rest.KeyVerificationStart import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore import im.vector.matrix.android.internal.crypto.tasks.SendToDeviceTask +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers @@ -47,6 +48,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import timber.log.Timber import java.util.* +import javax.inject.Inject import kotlin.collections.HashMap /** @@ -54,14 +56,16 @@ import kotlin.collections.HashMap * Short codes interactive verification is a more user friendly way of verifying devices * that is still maintaining a good level of security (alternative to the 43-character strings compare method). */ -internal class DefaultSasVerificationService(private val credentials: Credentials, - private val cryptoStore: IMXCryptoStore, - private val myDeviceInfoHolder: MyDeviceInfoHolder, - private val deviceListManager: DeviceListManager, - private val setDeviceVerificationAction: SetDeviceVerificationAction, - private val sendToDeviceTask: SendToDeviceTask, - private val coroutineDispatchers: MatrixCoroutineDispatchers, - private val taskExecutor: TaskExecutor) + +@SessionScope +internal class DefaultSasVerificationService @Inject constructor(private val credentials: Credentials, + private val cryptoStore: IMXCryptoStore, + private val myDeviceInfoHolder: MyDeviceInfoHolder, + private val deviceListManager: DeviceListManager, + private val setDeviceVerificationAction: SetDeviceVerificationAction, + private val sendToDeviceTask: SendToDeviceTask, + private val coroutineDispatchers: MatrixCoroutineDispatchers, + private val taskExecutor: TaskExecutor) : VerificationTransaction.Listener, SasVerificationService { private val uiHandler = Handler(Looper.getMainLooper()) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixKoinHolder.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/ChildWorkerFactory.kt similarity index 64% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixKoinHolder.kt rename to matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/ChildWorkerFactory.kt index e3d2047f30..169bcb6fc8 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixKoinHolder.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/ChildWorkerFactory.kt @@ -16,22 +16,12 @@ package im.vector.matrix.android.internal.di -import org.koin.core.Koin -import org.koin.core.KoinContext -import org.koin.standalone.KoinComponent +import android.content.Context +import androidx.work.ListenableWorker +import androidx.work.WorkerParameters -internal object MatrixKoinHolder { +interface ChildWorkerFactory { - val instance: Koin by lazy { - Koin.create() - } - -} - -internal interface MatrixKoinComponent : KoinComponent { - - override fun getKoin(): KoinContext { - return MatrixKoinHolder.instance.koinContext - } + fun create(appContext: Context, params: WorkerParameters): ListenableWorker } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixComponent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixComponent.kt new file mode 100644 index 0000000000..444475d58b --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixComponent.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2019 New Vector Ltd + * + * 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 im.vector.matrix.android.internal.di + +import dagger.Component + +@Component +@MatrixScope +interface MatrixComponent { + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixModule.kt index 8f9447c6c0..795ec83c0f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixModule.kt @@ -17,43 +17,31 @@ package im.vector.matrix.android.internal.di import android.content.Context +import dagger.Module +import dagger.Provides import im.vector.matrix.android.internal.crypto.CryptoAsyncHelper -import im.vector.matrix.android.internal.task.TaskExecutor -import im.vector.matrix.android.internal.util.BackgroundDetectionObserver import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers -import im.vector.matrix.android.internal.util.StringProvider import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.android.asCoroutineDispatcher -import org.koin.dsl.module.module +@Module +internal class MatrixModule(private val context: Context) { -class MatrixModule(private val context: Context) { - - val definition = module { - - single { - context + @Provides + @MatrixScope + fun providesContext(): Context { + return context } - single { + @Provides + @MatrixScope + fun providesMatrixCoroutineDispatchers(): MatrixCoroutineDispatchers { val cryptoHandler = CryptoAsyncHelper.getDecryptBackgroundHandler() - MatrixCoroutineDispatchers(io = Dispatchers.IO, + return MatrixCoroutineDispatchers(io = Dispatchers.IO, computation = Dispatchers.IO, main = Dispatchers.Main, crypto = cryptoHandler.asCoroutineDispatcher("crypto") ) } - single { - TaskExecutor(get()) - } - single { - StringProvider(context.resources) - } - - single { - BackgroundDetectionObserver() - } - - } } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixScope.java similarity index 63% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserModule.kt rename to matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixScope.java index a82fddc8de..30c9138e16 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/MatrixScope.java @@ -14,18 +14,16 @@ * limitations under the License. */ -package im.vector.matrix.android.internal.session.user +package im.vector.matrix.android.internal.di; -import im.vector.matrix.android.internal.session.DefaultSession -import org.koin.dsl.module.module +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; -internal class UserModule { +import javax.inject.Scope; - val definition = module(override = true) { +import static java.lang.annotation.RetentionPolicy.RUNTIME; - scope(DefaultSession.SCOPE) { - DefaultUpdateUserTask(get()) as UpdateUserTask - } - - } -} +@Scope +@Documented +@Retention(RUNTIME) +public @interface MatrixScope {} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt index d41f2cf8d3..b11e5a1ec7 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt @@ -17,85 +17,84 @@ package im.vector.matrix.android.internal.di import com.facebook.stetho.okhttp3.StethoInterceptor +import com.squareup.moshi.Moshi +import dagger.Module +import dagger.Provides import im.vector.matrix.android.BuildConfig -import im.vector.matrix.android.internal.network.* +import im.vector.matrix.android.internal.network.AccessTokenInterceptor +import im.vector.matrix.android.internal.network.UnitConverterFactory +import im.vector.matrix.android.internal.network.UserAgentInterceptor import im.vector.matrix.android.internal.network.interceptors.CurlLoggingInterceptor import im.vector.matrix.android.internal.network.interceptors.FormattedJsonHttpLogger import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import okreplay.OkReplayInterceptor -import org.koin.dsl.module.module import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory import java.util.concurrent.TimeUnit -class NetworkModule { - - val definition = module { - - single { - UserAgentHolder(get()) - } - - single { - UserAgentInterceptor(get()) - } - - single { - AccessTokenInterceptor(get()) - } - - single { - val logger = FormattedJsonHttpLogger() - val interceptor = HttpLoggingInterceptor(logger) - interceptor.level = BuildConfig.OKHTTP_LOGGING_LEVEL - interceptor - } - - single { - CurlLoggingInterceptor() - } - - single { - OkReplayInterceptor() - } - - single { - StethoInterceptor() - } - - single { - OkHttpClient.Builder() - .connectTimeout(1, TimeUnit.MINUTES) - .readTimeout(30, TimeUnit.SECONDS) - .writeTimeout(30, TimeUnit.SECONDS) - .addNetworkInterceptor(get()) - .addInterceptor(get()) - .addInterceptor(get()) - .addInterceptor(get()) - .apply { - if (BuildConfig.LOG_PRIVATE_DATA) { - addInterceptor(get()) - } - } - .addInterceptor(get()) - .build() - } - - single { - MoshiProvider.providesMoshi() - } - - single { - NetworkConnectivityChecker(get()) - } - - factory { - Retrofit.Builder() - .client(get()) - .addConverterFactory(UnitConverterFactory) - .addConverterFactory(MoshiConverterFactory.create(get())) - } +@Module +internal class NetworkModule { + @MatrixScope + @Provides + fun providesHttpLogingInterceptor(): HttpLoggingInterceptor { + val logger = FormattedJsonHttpLogger() + val interceptor = HttpLoggingInterceptor(logger) + interceptor.level = BuildConfig.OKHTTP_LOGGING_LEVEL + return interceptor } + + @MatrixScope + @Provides + fun providesOkReplayInterceptor(): OkReplayInterceptor { + return OkReplayInterceptor() + } + + @MatrixScope + @Provides + fun providesStethoInterceptor(): StethoInterceptor { + return StethoInterceptor() + } + + @MatrixScope + @Provides + fun providesOkHttpClient(stethoInterceptor: StethoInterceptor, + userAgentInterceptor: UserAgentInterceptor, + accessTokenInterceptor: AccessTokenInterceptor, + httpLoggingInterceptor: HttpLoggingInterceptor, + curlLoggingInterceptor: CurlLoggingInterceptor, + okReplayInterceptor: OkReplayInterceptor): OkHttpClient { + return OkHttpClient.Builder() + .connectTimeout(1, TimeUnit.MINUTES) + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .addNetworkInterceptor(stethoInterceptor) + .addInterceptor(userAgentInterceptor) + .addInterceptor(accessTokenInterceptor) + .addInterceptor(httpLoggingInterceptor) + .apply { + if (BuildConfig.LOG_PRIVATE_DATA) { + addInterceptor(curlLoggingInterceptor) + } + } + .addInterceptor(okReplayInterceptor) + .build() + } + + @MatrixScope + @Provides + fun providesMoshi(): Moshi { + return MoshiProvider.providesMoshi() + } + + @Provides + fun providesRetrofitBuilder(okHttpClient: OkHttpClient, + moshi: Moshi): Retrofit.Builder { + return Retrofit.Builder() + .client(okHttpClient) + .addConverterFactory(UnitConverterFactory) + .addConverterFactory(MoshiConverterFactory.create(moshi)) + } + } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/AccessTokenInterceptor.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/AccessTokenInterceptor.kt index 77d2ec926a..62030490ce 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/AccessTokenInterceptor.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/AccessTokenInterceptor.kt @@ -17,10 +17,13 @@ package im.vector.matrix.android.internal.network import im.vector.matrix.android.internal.auth.SessionParamsStore +import im.vector.matrix.android.internal.di.MatrixScope import okhttp3.Interceptor import okhttp3.Response +import javax.inject.Inject -internal class AccessTokenInterceptor(private val sessionParamsStore: SessionParamsStore) : Interceptor { +@MatrixScope +internal class AccessTokenInterceptor @Inject constructor(private val sessionParamsStore: SessionParamsStore) : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { var request = chain.request() diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/NetworkConnectivityChecker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/NetworkConnectivityChecker.kt index ef83e47e3b..9502cb28d9 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/NetworkConnectivityChecker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/NetworkConnectivityChecker.kt @@ -20,8 +20,11 @@ import android.content.Context import com.novoda.merlin.Merlin import com.novoda.merlin.MerlinsBeard import com.novoda.merlin.registerable.connection.Connectable +import im.vector.matrix.android.internal.di.MatrixScope +import javax.inject.Inject -internal class NetworkConnectivityChecker(context: Context) { +@MatrixScope +internal class NetworkConnectivityChecker @Inject constructor(context: Context) { private val merlin = Merlin.Builder().withConnectableCallbacks().build(context) private val merlinsBeard = MerlinsBeard.from(context) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/UserAgentHolder.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/UserAgentHolder.kt index 7917f960b6..306b8a91e6 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/UserAgentHolder.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/UserAgentHolder.kt @@ -19,9 +19,12 @@ package im.vector.matrix.android.internal.network import android.content.Context import android.text.TextUtils import im.vector.matrix.android.BuildConfig +import im.vector.matrix.android.internal.di.MatrixScope import timber.log.Timber +import javax.inject.Inject -internal class UserAgentHolder(val context: Context) { +@MatrixScope +internal class UserAgentHolder @Inject constructor(val context: Context) { var userAgent: String = "" private set diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/UserAgentInterceptor.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/UserAgentInterceptor.kt index 151ba0c361..30759923d4 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/UserAgentInterceptor.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/network/UserAgentInterceptor.kt @@ -16,10 +16,13 @@ package im.vector.matrix.android.internal.network +import im.vector.matrix.android.internal.di.MatrixScope import okhttp3.Interceptor import okhttp3.Response +import javax.inject.Inject -internal class UserAgentInterceptor(private val userAgentHolder: UserAgentHolder) : Interceptor { +@MatrixScope +internal class UserAgentInterceptor @Inject constructor(private val userAgentHolder: UserAgentHolder) : Interceptor { override fun intercept(chain: Interceptor.Chain): Response { var request = chain.request() diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt index 747ee9e4e6..80f60b8c3d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt @@ -16,86 +16,63 @@ package im.vector.matrix.android.internal.session -import android.content.Context import android.os.Looper import androidx.annotation.MainThread import androidx.lifecycle.LiveData +import com.squareup.inject.assisted.Assisted +import com.squareup.inject.assisted.AssistedInject import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.auth.data.SessionParams -import im.vector.matrix.android.api.listeners.ProgressListener import im.vector.matrix.android.api.session.Session 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.keysbackup.KeysBackupService -import im.vector.matrix.android.api.session.crypto.keyshare.RoomKeysRequestListener -import im.vector.matrix.android.api.session.crypto.sas.SasVerificationService -import im.vector.matrix.android.api.session.events.model.Content -import im.vector.matrix.android.api.session.events.model.Event -import im.vector.matrix.android.api.session.group.Group +import im.vector.matrix.android.api.session.crypto.CryptoService import im.vector.matrix.android.api.session.group.GroupService -import im.vector.matrix.android.api.session.group.model.GroupSummary -import im.vector.matrix.android.api.session.room.Room import im.vector.matrix.android.api.session.room.RoomDirectoryService import im.vector.matrix.android.api.session.room.RoomService -import im.vector.matrix.android.api.session.room.model.RoomSummary -import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams -import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoomsParams -import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoomsResponse -import im.vector.matrix.android.api.session.room.model.thirdparty.ThirdPartyProtocol import im.vector.matrix.android.api.session.signout.SignOutService 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 -import im.vector.matrix.android.api.session.user.model.User -import im.vector.matrix.android.api.util.Cancelable import im.vector.matrix.android.api.util.MatrixCallbackDelegate import im.vector.matrix.android.internal.crypto.CryptoManager -import im.vector.matrix.android.internal.crypto.CryptoModule -import im.vector.matrix.android.internal.crypto.MXEventDecryptionResult -import im.vector.matrix.android.internal.crypto.model.ImportRoomKeysResult -import im.vector.matrix.android.internal.crypto.model.MXDeviceInfo -import im.vector.matrix.android.internal.crypto.model.MXEncryptEventContentResult -import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap -import im.vector.matrix.android.internal.crypto.model.rest.DevicesListResponse -import im.vector.matrix.android.internal.crypto.model.rest.RoomKeyRequestBody import im.vector.matrix.android.internal.database.LiveEntityObserver -import im.vector.matrix.android.internal.di.MatrixKoinComponent -import im.vector.matrix.android.internal.di.MatrixKoinHolder -import im.vector.matrix.android.internal.session.content.ContentModule -import im.vector.matrix.android.internal.session.group.GroupModule -import im.vector.matrix.android.internal.session.room.RoomModule -import im.vector.matrix.android.internal.session.signout.SignOutModule -import im.vector.matrix.android.internal.session.sync.SyncModule import im.vector.matrix.android.internal.session.sync.job.SyncThread -import im.vector.matrix.android.internal.session.user.UserModule -import org.koin.core.scope.Scope -import org.koin.standalone.inject import timber.log.Timber -internal class DefaultSession(override val sessionParams: SessionParams) : Session, MatrixKoinComponent { - companion object { - const val SCOPE: String = "session" +internal class DefaultSession @AssistedInject constructor(@Assisted override val sessionParams: SessionParams, + private val monarchy: Monarchy, + private val liveEntityUpdaters: List, + private val sessionListeners: SessionListeners, + private val roomService: RoomService, + private val roomDirectoryService: RoomDirectoryService, + private val groupService: GroupService, + private val userService: UserService, + private val filterService: FilterService, + private val cacheService: CacheService, + private val signOutService: SignOutService, + private val cryptoService: CryptoManager, + private val syncThread: SyncThread, + private val contentUrlResolver: ContentUrlResolver, + private val contentUploadProgressTracker: ContentUploadStateTracker) + : Session, + RoomService by roomService, + RoomDirectoryService by roomDirectoryService, + GroupService by groupService, + UserService by userService, + CryptoService by cryptoService, + CacheService by cacheService, + SignOutService by signOutService, + FilterService by filterService { + + @AssistedInject.Factory + interface Factory { + fun create(sessionParams: SessionParams): DefaultSession } - private lateinit var scope: Scope - - private val monarchy by inject() - private val liveEntityUpdaters by inject>() - private val sessionListeners by inject() - private val roomService by inject() - private val roomDirectoryService by inject() - private val groupService by inject() - private val userService by inject() - private val filterService by inject() - private val cacheService by inject() - private val signOutService by inject() - private val cryptoService by inject() - private val syncThread by inject() - private val contentUrlResolver by inject() - private val contentUploadProgressTracker by inject() private var isOpen = false @MainThread @@ -103,23 +80,6 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi assertMainThread() assert(!isOpen) isOpen = true - val sessionModule = SessionModule(sessionParams).definition - val syncModule = SyncModule().definition - val roomModule = RoomModule().definition - val groupModule = GroupModule().definition - val signOutModule = SignOutModule().definition - val userModule = UserModule().definition - val contentModule = ContentModule().definition - val cryptoModule = CryptoModule().definition - MatrixKoinHolder.instance.loadModules(listOf(sessionModule, - syncModule, - roomModule, - groupModule, - userModule, - signOutModule, - contentModule, - cryptoModule)) - scope = getKoin().getOrCreateScope(SCOPE) if (!monarchy.isMonarchyThreadOpen) { monarchy.openManually() } @@ -147,7 +107,6 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi if (monarchy.isMonarchyThreadOpen) { monarchy.closeManually() } - scope.close() isOpen = false } @@ -207,221 +166,6 @@ internal class DefaultSession(override val sessionParams: SessionParams) : Sessi sessionListeners.removeListener(listener) } - // ROOM SERVICE - - override fun createRoom(createRoomParams: CreateRoomParams, callback: MatrixCallback) { - assert(isOpen) - return roomService.createRoom(createRoomParams, callback) - } - - override fun getRoom(roomId: String): Room? { - assert(isOpen) - return roomService.getRoom(roomId) - } - - - override fun liveRoomSummaries(): LiveData> { - assert(isOpen) - return roomService.liveRoomSummaries() - } - - // ROOM DIRECTORY SERVICE - - override fun getPublicRooms(server: String?, publicRoomsParams: PublicRoomsParams, callback: MatrixCallback): Cancelable { - assert(isOpen) - return roomDirectoryService.getPublicRooms(server, publicRoomsParams, callback) - } - - override fun joinRoom(roomId: String, callback: MatrixCallback) { - assert(isOpen) - return roomDirectoryService.joinRoom(roomId, callback) - } - - override fun getThirdPartyProtocol(callback: MatrixCallback>) { - assert(isOpen) - return roomDirectoryService.getThirdPartyProtocol(callback) - } - - // GROUP SERVICE - - override fun getGroup(groupId: String): Group? { - assert(isOpen) - return groupService.getGroup(groupId) - } - - override fun liveGroupSummaries(): LiveData> { - assert(isOpen) - return groupService.liveGroupSummaries() - } - - override fun setFilter(filterPreset: FilterService.FilterPreset) { - assert(isOpen) - return filterService.setFilter(filterPreset) - } - - override fun clearCache(callback: MatrixCallback) { - assert(isOpen) - syncThread.pause() - cacheService.clearCache(object : MatrixCallbackDelegate(callback) { - override fun onSuccess(data: Unit) { - // Restart the sync - syncThread.restart() - - super.onSuccess(data) - } - }) - } - - // USER SERVICE - - override fun getUser(userId: String): User? { - assert(isOpen) - return userService.getUser(userId) - } - - override fun observeUser(userId: String): LiveData { - assert(isOpen) - return userService.observeUser(userId) - } - - // CRYPTO SERVICE - - override fun setDeviceName(deviceId: String, deviceName: String, callback: MatrixCallback) { - cryptoService.setDeviceName(deviceId, deviceName, callback) - } - - override fun deleteDevice(deviceId: String, accountPassword: String, callback: MatrixCallback) { - cryptoService.deleteDevice(deviceId, accountPassword, callback) - } - - override fun getCryptoVersion(context: Context, longFormat: Boolean): String { - return cryptoService.getCryptoVersion(context, longFormat) - } - - override fun isCryptoEnabled(): Boolean { - return cryptoService.isCryptoEnabled() - } - - override fun getSasVerificationService(): SasVerificationService { - return cryptoService.getSasVerificationService() - } - - override fun getKeysBackupService(): KeysBackupService { - return cryptoService.getKeysBackupService() - } - - override fun isRoomBlacklistUnverifiedDevices(roomId: String?): Boolean { - return cryptoService.isRoomBlacklistUnverifiedDevices(roomId) - } - - override fun setWarnOnUnknownDevices(warn: Boolean) { - cryptoService.setWarnOnUnknownDevices(warn) - } - - override fun setDeviceVerification(verificationStatus: Int, deviceId: String, userId: String) { - cryptoService.setDeviceVerification(verificationStatus, deviceId, userId) - } - - override fun getUserDevices(userId: String): MutableList { - return cryptoService.getUserDevices(userId) - } - - override fun setDevicesKnown(devices: List, callback: MatrixCallback?) { - cryptoService.setDevicesKnown(devices, callback) - } - - override fun deviceWithIdentityKey(senderKey: String, algorithm: String): MXDeviceInfo? { - return cryptoService.deviceWithIdentityKey(senderKey, algorithm) - } - - override fun getMyDevice(): MXDeviceInfo { - return cryptoService.getMyDevice() - } - - override fun getDevicesList(callback: MatrixCallback) { - cryptoService.getDevicesList(callback) - } - - override fun inboundGroupSessionsCount(onlyBackedUp: Boolean): Int { - return cryptoService.inboundGroupSessionsCount(onlyBackedUp) - } - - override fun getGlobalBlacklistUnverifiedDevices(): Boolean { - return cryptoService.getGlobalBlacklistUnverifiedDevices() - } - - override fun setGlobalBlacklistUnverifiedDevices(block: Boolean) { - cryptoService.setGlobalBlacklistUnverifiedDevices(block) - } - - override fun setRoomUnBlacklistUnverifiedDevices(roomId: String) { - cryptoService.setRoomUnBlacklistUnverifiedDevices(roomId) - } - - override fun getDeviceTrackingStatus(userId: String): Int { - return cryptoService.getDeviceTrackingStatus(userId) - } - - override fun importRoomKeys(roomKeysAsArray: ByteArray, password: String, progressListener: ProgressListener?, callback: MatrixCallback) { - cryptoService.importRoomKeys(roomKeysAsArray, password, progressListener, callback) - } - - override fun exportRoomKeys(password: String, callback: MatrixCallback) { - cryptoService.exportRoomKeys(password, callback) - } - - override fun setRoomBlacklistUnverifiedDevices(roomId: String) { - cryptoService.setRoomBlacklistUnverifiedDevices(roomId) - } - - override fun isRoomEncrypted(roomId: String): Boolean { - return cryptoService.isRoomEncrypted(roomId) - } - - override fun encryptEventContent(eventContent: Content, eventType: String, roomId: String, callback: MatrixCallback) { - cryptoService.encryptEventContent(eventContent, eventType, roomId, callback) - } - - override fun getDeviceInfo(userId: String, deviceId: String?): MXDeviceInfo? { - return cryptoService.getDeviceInfo(userId, deviceId) - } - - override fun reRequestRoomKeyForEvent(event: Event) { - cryptoService.reRequestRoomKeyForEvent(event) - } - - override fun cancelRoomKeyRequest(requestBody: RoomKeyRequestBody) { - cryptoService.cancelRoomKeyRequest(requestBody) - } - - override fun addRoomKeysRequestListener(listener: RoomKeysRequestListener) { - cryptoService.addRoomKeysRequestListener(listener) - } - - override fun decryptEvent(event: Event, timeline: String): MXEventDecryptionResult? { - return cryptoService.decryptEvent(event, timeline) - } - - override fun decryptEventAsync(event: Event, timeline: String, callback: MatrixCallback) { - return cryptoService.decryptEventAsync(event, timeline, callback) - } - - override fun getEncryptionAlgorithm(roomId: String): String? { - return cryptoService.getEncryptionAlgorithm(roomId) - } - - override fun shouldEncryptForInvitedMembers(roomId: String): Boolean { - return cryptoService.shouldEncryptForInvitedMembers(roomId) - } - - override fun downloadKeys(userIds: List, forceDownload: Boolean, callback: MatrixCallback>) { - cryptoService.downloadKeys(userIds, forceDownload, callback) - } - - override fun clearCryptoCache(callback: MatrixCallback) { - cryptoService.clearCryptoCache(callback) - } - // Private methods ***************************************************************************** private fun assertMainThread() { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionComponent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionComponent.kt new file mode 100644 index 0000000000..74eb73b4df --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionComponent.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2019 New Vector Ltd + * + * 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 im.vector.matrix.android.internal.session + +import dagger.Component +import im.vector.matrix.android.internal.di.MatrixComponent + +@Component(dependencies = [MatrixComponent::class]) +@SessionScope +interface SessionComponent { + + @Component.Builder + interface Builder { + fun matrixComponent(matrixComponent: MatrixComponent): Builder + } + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionFactory.kt new file mode 100644 index 0000000000..ef5a0e4ccd --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionFactory.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2019 New Vector Ltd + * + * 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 im.vector.matrix.android.internal.session + +import com.zhuinden.monarchy.Monarchy +import im.vector.matrix.android.api.auth.data.SessionParams +import im.vector.matrix.android.api.session.Session +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.group.GroupService +import im.vector.matrix.android.api.session.room.RoomDirectoryService +import im.vector.matrix.android.api.session.room.RoomService +import im.vector.matrix.android.api.session.signout.SignOutService +import im.vector.matrix.android.api.session.sync.FilterService +import im.vector.matrix.android.api.session.user.UserService +import im.vector.matrix.android.internal.crypto.CryptoManager +import im.vector.matrix.android.internal.database.LiveEntityObserver +import im.vector.matrix.android.internal.di.MatrixScope +import im.vector.matrix.android.internal.session.sync.job.SyncThread +import javax.inject.Inject + +@MatrixScope +internal class SessionFactory @Inject constructor( + private val monarchy: Monarchy, + private val liveEntityUpdaters: List, + private val sessionListeners: SessionListeners, + private val roomService: RoomService, + private val roomDirectoryService: RoomDirectoryService, + private val groupService: GroupService, + private val userService: UserService, + private val filterService: FilterService, + private val cacheService: CacheService, + private val signOutService: SignOutService, + private val cryptoService: CryptoManager, + private val syncThread: SyncThread, + private val contentUrlResolver: ContentUrlResolver, + private val contentUploadProgressTracker: ContentUploadStateTracker) { + + + fun create(sessionParams: SessionParams): Session { + return DefaultSession( + sessionParams, + monarchy, + liveEntityUpdaters, + sessionListeners, + roomService, + roomDirectoryService, + groupService, + userService, + filterService, + cacheService, + signOutService, + cryptoService, + syncThread, + contentUrlResolver, + contentUploadProgressTracker + ) + } + +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionListeners.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionListeners.kt index 4fcb16e8b1..51b7354785 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionListeners.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionListeners.kt @@ -17,8 +17,9 @@ package im.vector.matrix.android.internal.session import im.vector.matrix.android.api.session.Session +import javax.inject.Inject -internal class SessionListeners { +internal class SessionListeners @Inject constructor(){ private val listeners = ArrayList() diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt index 4a5d3b4e53..aea08a69ea 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt @@ -18,161 +18,68 @@ package im.vector.matrix.android.internal.session import android.content.Context import com.zhuinden.monarchy.Monarchy +import dagger.Module +import dagger.Provides +import im.vector.matrix.android.api.auth.data.Credentials import im.vector.matrix.android.api.auth.data.SessionParams -import im.vector.matrix.android.api.session.cache.CacheService -import im.vector.matrix.android.api.session.group.GroupService -import im.vector.matrix.android.api.session.room.RoomDirectoryService -import im.vector.matrix.android.api.session.room.RoomService -import im.vector.matrix.android.api.session.signout.SignOutService -import im.vector.matrix.android.api.session.sync.FilterService -import im.vector.matrix.android.api.session.user.UserService import im.vector.matrix.android.internal.database.LiveEntityObserver import im.vector.matrix.android.internal.database.model.SessionRealmModule -import im.vector.matrix.android.internal.session.cache.ClearCacheTask -import im.vector.matrix.android.internal.session.cache.RealmCacheService -import im.vector.matrix.android.internal.session.cache.RealmClearCacheTask -import im.vector.matrix.android.internal.session.filter.* -import im.vector.matrix.android.internal.session.group.DefaultGroupService +import im.vector.matrix.android.internal.session.filter.FilterApi import im.vector.matrix.android.internal.session.group.GroupSummaryUpdater -import im.vector.matrix.android.internal.session.room.* -import im.vector.matrix.android.internal.session.room.directory.DefaultGetPublicRoomTask -import im.vector.matrix.android.internal.session.room.directory.DefaultGetThirdPartyProtocolsTask -import im.vector.matrix.android.internal.session.room.directory.GetPublicRoomTask -import im.vector.matrix.android.internal.session.room.directory.GetThirdPartyProtocolsTask -import im.vector.matrix.android.internal.session.room.membership.RoomDisplayNameResolver -import im.vector.matrix.android.internal.session.room.membership.RoomMemberDisplayNameResolver +import im.vector.matrix.android.internal.session.room.EventRelationsAggregationUpdater import im.vector.matrix.android.internal.session.room.prune.EventsPruner -import im.vector.matrix.android.internal.session.signout.DefaultSignOutService -import im.vector.matrix.android.internal.session.user.DefaultUserService import im.vector.matrix.android.internal.session.user.UserEntityUpdater import im.vector.matrix.android.internal.util.md5 import io.realm.RealmConfiguration -import org.koin.dsl.module.module import retrofit2.Retrofit import java.io.File +@Module internal class SessionModule(private val sessionParams: SessionParams) { - val definition = module(override = true) { + @Provides + fun providesCredentials(): Credentials { + return sessionParams.credentials + } - scope(DefaultSession.SCOPE) { - sessionParams - } + @Provides + fun providesRealmConfiguration(context: Context): RealmConfiguration { + val childPath = sessionParams.credentials.userId.md5() + val directory = File(context.filesDir, childPath) - scope(DefaultSession.SCOPE) { - sessionParams.credentials - } + return RealmConfiguration.Builder() + .directory(directory) + .name("disk_store.realm") + .modules(SessionRealmModule()) + .deleteRealmIfMigrationNeeded() + .build() + } - scope(DefaultSession.SCOPE, name = "SessionRealmConfiguration") { - val context = get() - val childPath = sessionParams.credentials.userId.md5() - val directory = File(context.filesDir, childPath) + @Provides + fun providesMonarchy(realmConfiguration: RealmConfiguration): Monarchy { + return Monarchy.Builder() + .setRealmConfiguration(realmConfiguration) + .build() + } - RealmConfiguration.Builder() - .directory(directory) - .name("disk_store.realm") - .modules(SessionRealmModule()) - .deleteRealmIfMigrationNeeded() - .build() - } - - scope(DefaultSession.SCOPE) { - Monarchy.Builder() - .setRealmConfiguration(get("SessionRealmConfiguration")) - .build() - } - - scope(DefaultSession.SCOPE) { - val retrofitBuilder = get() - retrofitBuilder - .baseUrl(sessionParams.homeServerConnectionConfig.homeServerUri.toString()) - .build() - } - - scope(DefaultSession.SCOPE) { - RoomMemberDisplayNameResolver() - } - - scope(DefaultSession.SCOPE) { - RoomDisplayNameResolver(get(), get(), get(), sessionParams.credentials) - } - - scope(DefaultSession.SCOPE) { - RoomAvatarResolver(get(), get()) - } - - scope(DefaultSession.SCOPE) { - RoomSummaryUpdater(get(), get(), get()) - } - - scope(DefaultSession.SCOPE) { - DefaultRoomService(get(), get(), get(), get()) as RoomService - } - - scope(DefaultSession.SCOPE) { - DefaultGetPublicRoomTask(get()) as GetPublicRoomTask - } - - scope(DefaultSession.SCOPE) { - DefaultGetThirdPartyProtocolsTask(get()) as GetThirdPartyProtocolsTask - } - - scope(DefaultSession.SCOPE) { - DefaultRoomDirectoryService(get(), get(), get(), get()) as RoomDirectoryService - } - - scope(DefaultSession.SCOPE) { - DefaultGroupService(get()) as GroupService - } - - scope(DefaultSession.SCOPE) { - DefaultSignOutService(get(), get()) as SignOutService - } - - scope(DefaultSession.SCOPE) { - RealmCacheService(get("ClearTaskMainCache"), get()) as CacheService - } - - // Give a name, because we have a clear task for crypto store as well - scope(DefaultSession.SCOPE, name = "ClearTaskMainCache") { - RealmClearCacheTask(get("SessionRealmConfiguration")) as ClearCacheTask - } - - scope(DefaultSession.SCOPE) { - DefaultUserService(get()) as UserService - } - - scope(DefaultSession.SCOPE) { - SessionListeners() - } - - scope(DefaultSession.SCOPE) { - DefaultFilterRepository(get("SessionRealmConfiguration")) as FilterRepository - } - - scope(DefaultSession.SCOPE) { - DefaultSaveFilterTask(get(), get(), get()) as SaveFilterTask - } - - scope(DefaultSession.SCOPE) { - DefaultFilterService(get(), get(), get()) as FilterService - } - - scope(DefaultSession.SCOPE) { - val retrofit: Retrofit = get() - retrofit.create(FilterApi::class.java) - } - - scope(DefaultSession.SCOPE) { - val groupSummaryUpdater = GroupSummaryUpdater(get()) - val userEntityUpdater = UserEntityUpdater(get(), get(), get()) - val aggregationUpdater = EventRelationsAggregationUpdater(get(), get(), get(), get()) - //Event pruner must be the last one, because it will clear contents - val eventsPruner = EventsPruner(get(), get(), get(), get()) - listOf(groupSummaryUpdater, userEntityUpdater, aggregationUpdater, eventsPruner) - } + @Provides + fun providesRetrofit(retrofitBuilder: Retrofit.Builder): Retrofit { + return retrofitBuilder + .baseUrl(sessionParams.homeServerConnectionConfig.homeServerUri.toString()) + .build() + } + @Provides + fun providesFilterAPI(retrofit: Retrofit): FilterApi { + return retrofit.create(FilterApi::class.java) + } + @Provides + fun providesLiveEntityObservers(groupSummaryUpdater: GroupSummaryUpdater, + userEntityUpdater: UserEntityUpdater, + aggregationUpdater: EventRelationsAggregationUpdater, + eventsPruner: EventsPruner): List { + return listOf(groupSummaryUpdater, userEntityUpdater, aggregationUpdater, eventsPruner) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionScope.java b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionScope.java new file mode 100644 index 0000000000..5284264178 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionScope.java @@ -0,0 +1,29 @@ +/* + * Copyright 2019 New Vector Ltd + * + * 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 im.vector.matrix.android.internal.session; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; + +import javax.inject.Scope; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Scope +@Documented +@Retention(RUNTIME) +public @interface SessionScope {} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/cache/RealmCacheService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/cache/RealmCacheService.kt index 7af01b7ef0..588ec63ae2 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/cache/RealmCacheService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/cache/RealmCacheService.kt @@ -18,11 +18,14 @@ package im.vector.matrix.android.internal.session.cache import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.session.cache.CacheService +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith +import javax.inject.Inject -internal class RealmCacheService(private val clearCacheTask: ClearCacheTask, - private val taskExecutor: TaskExecutor) : CacheService { +@SessionScope +internal class RealmCacheService @Inject constructor(private val clearCacheTask: ClearCacheTask, + private val taskExecutor: TaskExecutor) : CacheService { override fun clearCache(callback: MatrixCallback) { clearCacheTask diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/cache/RealmClearCacheTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/cache/RealmClearCacheTask.kt index f1e927f029..67ed54dc29 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/cache/RealmClearCacheTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/cache/RealmClearCacheTask.kt @@ -17,13 +17,16 @@ package im.vector.matrix.android.internal.session.cache import arrow.core.Try +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task import io.realm.Realm import io.realm.RealmConfiguration +import javax.inject.Inject internal interface ClearCacheTask : Task -internal class RealmClearCacheTask(val realmConfiguration: RealmConfiguration) : ClearCacheTask { +@SessionScope +internal class RealmClearCacheTask @Inject constructor(val realmConfiguration: RealmConfiguration) : ClearCacheTask { override suspend fun execute(params: Unit): Try { return Try { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/ContentModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/ContentModule.kt index 7fb9b75768..9f431862ff 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/ContentModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/ContentModule.kt @@ -16,34 +16,10 @@ package im.vector.matrix.android.internal.session.content -import im.vector.matrix.android.api.auth.data.SessionParams -import im.vector.matrix.android.api.session.content.ContentUploadStateTracker -import im.vector.matrix.android.api.session.content.ContentUrlResolver -import im.vector.matrix.android.internal.session.DefaultSession -import org.koin.dsl.module.module +import dagger.Module +@Module internal class ContentModule { - val definition = module(override = true) { - - scope(DefaultSession.SCOPE) { - DefaultContentUploadStateTracker() - } - - scope(DefaultSession.SCOPE) { - get() as ContentUploadStateTracker - } - - scope(DefaultSession.SCOPE) { - FileUploader(get(), get()) - } - - scope(DefaultSession.SCOPE) { - val sessionParams = get() - DefaultContentUrlResolver(sessionParams.homeServerConnectionConfig) as ContentUrlResolver - } - - } - } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/DefaultContentUploadStateTracker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/DefaultContentUploadStateTracker.kt index a5dd16fbdd..fbd7983d96 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/DefaultContentUploadStateTracker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/DefaultContentUploadStateTracker.kt @@ -19,8 +19,11 @@ package im.vector.matrix.android.internal.session.content import android.os.Handler import android.os.Looper import im.vector.matrix.android.api.session.content.ContentUploadStateTracker +import im.vector.matrix.android.internal.session.SessionScope +import javax.inject.Inject -internal class DefaultContentUploadStateTracker : ContentUploadStateTracker { +@SessionScope +internal class DefaultContentUploadStateTracker @Inject constructor() : ContentUploadStateTracker { private val mainHandler = Handler(Looper.getMainLooper()) private val states = mutableMapOf() diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/DefaultContentUrlResolver.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/DefaultContentUrlResolver.kt index 0fad40d082..bfba5bb796 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/DefaultContentUrlResolver.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/DefaultContentUrlResolver.kt @@ -18,12 +18,15 @@ package im.vector.matrix.android.internal.session.content import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig import im.vector.matrix.android.api.session.content.ContentUrlResolver +import im.vector.matrix.android.internal.session.SessionScope +import javax.inject.Inject private const val MATRIX_CONTENT_URI_SCHEME = "mxc://" private const val URI_PREFIX_CONTENT_API = "_matrix/media/v1/" -internal class DefaultContentUrlResolver(private val homeServerConnectionConfig: HomeServerConnectionConfig) : ContentUrlResolver { +@SessionScope +internal class DefaultContentUrlResolver @Inject constructor(private val homeServerConnectionConfig: HomeServerConnectionConfig) : ContentUrlResolver { companion object { fun getUploadUrl(homeServerConnectionConfig: HomeServerConnectionConfig): String { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt index e8133070d0..67d79305ea 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt @@ -21,12 +21,15 @@ import arrow.core.Try.Companion.raise import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.internal.di.MoshiProvider import im.vector.matrix.android.internal.network.ProgressRequestBody +import im.vector.matrix.android.internal.session.SessionScope import okhttp3.* import java.io.File import java.io.IOException +import javax.inject.Inject -internal class FileUploader(private val okHttpClient: OkHttpClient, +@SessionScope +internal class FileUploader @Inject constructor(private val okHttpClient: OkHttpClient, sessionParams: SessionParams) { private val uploadUrl = DefaultContentUrlResolver.getUploadUrl(sessionParams.homeServerConnectionConfig) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt index bdfd9395a1..58516088d5 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/UploadContentWorker.kt @@ -19,26 +19,32 @@ package im.vector.matrix.android.internal.session.content import android.content.Context import androidx.work.CoroutineWorker import androidx.work.WorkerParameters +import com.squareup.inject.assisted.Assisted +import com.squareup.inject.assisted.AssistedInject import com.squareup.moshi.JsonClass import im.vector.matrix.android.api.session.content.ContentAttachmentData import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.toContent import im.vector.matrix.android.api.session.events.model.toModel -import im.vector.matrix.android.api.session.room.model.message.* -import im.vector.matrix.android.internal.di.MatrixKoinComponent +import im.vector.matrix.android.api.session.room.model.message.MessageAudioContent +import im.vector.matrix.android.api.session.room.model.message.MessageContent +import im.vector.matrix.android.api.session.room.model.message.MessageFileContent +import im.vector.matrix.android.api.session.room.model.message.MessageImageContent +import im.vector.matrix.android.api.session.room.model.message.MessageVideoContent +import im.vector.matrix.android.internal.di.ChildWorkerFactory import im.vector.matrix.android.internal.network.ProgressRequestBody import im.vector.matrix.android.internal.session.room.send.SendEventWorker import im.vector.matrix.android.internal.util.WorkerParamsFactory -import org.koin.standalone.inject import timber.log.Timber import java.io.File -internal class UploadContentWorker(context: Context, params: WorkerParameters) - : CoroutineWorker(context, params), MatrixKoinComponent { - - private val fileUploader by inject() - private val contentUploadProgressTracker by inject() +internal class UploadContentWorker @AssistedInject constructor( + @Assisted context: Context, + @Assisted params: WorkerParameters, + private val fileUploader: FileUploader, + private val contentUploadStateTracker: DefaultContentUploadStateTracker) + : CoroutineWorker(context, params) { @JsonClass(generateAdapter = true) internal data class Params( @@ -69,7 +75,7 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) val progressListener = object : ProgressRequestBody.Listener { override fun onProgress(current: Long, total: Long) { - contentUploadProgressTracker.setProgress(eventId, current, total) + contentUploadStateTracker.setProgress(eventId, current, total) } } return fileUploader @@ -90,14 +96,14 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) } private fun handleFailure(params: Params): Result { - contentUploadProgressTracker.setFailure(params.event.eventId!!) + contentUploadStateTracker.setFailure(params.event.eventId!!) return Result.success() } private fun handleSuccess(params: Params, attachmentUrl: String, thumbnailUrl: String?): Result { - contentUploadProgressTracker.setFailure(params.event.eventId!!) + contentUploadStateTracker.setSuccess(params.event.eventId!!) val event = updateEvent(params.event, attachmentUrl, thumbnailUrl) val sendParams = SendEventWorker.Params(params.roomId, event) return Result.success(WorkerParamsFactory.toData(sendParams)) @@ -131,6 +137,8 @@ internal class UploadContentWorker(context: Context, params: WorkerParameters) return copy(url = url) } + @AssistedInject.Factory + interface Factory : ChildWorkerFactory } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultFilterRepository.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultFilterRepository.kt index 32d9541ae5..690db907be 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultFilterRepository.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultFilterRepository.kt @@ -19,11 +19,14 @@ package im.vector.matrix.android.internal.session.filter import im.vector.matrix.android.internal.database.model.FilterEntity import im.vector.matrix.android.internal.database.model.FilterEntityFields import im.vector.matrix.android.internal.database.query.getFilter +import im.vector.matrix.android.internal.session.SessionScope import io.realm.Realm import io.realm.RealmConfiguration import io.realm.kotlin.where +import javax.inject.Inject -internal class DefaultFilterRepository(val realmConfiguration: RealmConfiguration) : FilterRepository { +@SessionScope +internal class DefaultFilterRepository @Inject constructor(val realmConfiguration: RealmConfiguration) : FilterRepository { override fun storeFilter(filterBody: FilterBody, roomEventFilter: RoomEventFilter): Boolean { val result: Boolean diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultFilterService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultFilterService.kt index 94c3f1cf37..033838ee87 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultFilterService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultFilterService.kt @@ -17,12 +17,15 @@ package im.vector.matrix.android.internal.session.filter import im.vector.matrix.android.api.session.sync.FilterService +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith +import javax.inject.Inject -internal class DefaultFilterService(private val filterRepository: FilterRepository, - private val saveFilterTask: SaveFilterTask, - private val taskExecutor: TaskExecutor) : FilterService { +@SessionScope +internal class DefaultFilterService @Inject constructor(private val filterRepository: FilterRepository, + private val saveFilterTask: SaveFilterTask, + private val taskExecutor: TaskExecutor) : FilterService { // TODO Pass a list of support events instead override fun setFilter(filterPreset: FilterService.FilterPreset) { @@ -30,7 +33,7 @@ internal class DefaultFilterService(private val filterRepository: FilterReposito FilterService.FilterPreset.RiotFilter -> { FilterFactory.createRiotFilterBody() } - FilterService.FilterPreset.NoFilter -> { + FilterService.FilterPreset.NoFilter -> { FilterFactory.createDefaultFilterBody() } } @@ -39,7 +42,7 @@ internal class DefaultFilterService(private val filterRepository: FilterReposito FilterService.FilterPreset.RiotFilter -> { FilterFactory.createRiotRoomFilter() } - FilterService.FilterPreset.NoFilter -> { + FilterService.FilterPreset.NoFilter -> { FilterFactory.createDefaultRoomFilter() } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultSaveFilterTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultSaveFilterTask.kt index ab826805c3..a3c7fea8eb 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultSaveFilterTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/filter/DefaultSaveFilterTask.kt @@ -19,7 +19,9 @@ package im.vector.matrix.android.internal.session.filter import arrow.core.Try import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject /** @@ -33,9 +35,10 @@ internal interface SaveFilterTask : Task { } -internal class DefaultSaveFilterTask(private val sessionParams: SessionParams, - private val filterAPI: FilterApi, - private val filterRepository: FilterRepository +@SessionScope +internal class DefaultSaveFilterTask @Inject constructor(private val sessionParams: SessionParams, + private val filterAPI: FilterApi, + private val filterRepository: FilterRepository ) : SaveFilterTask { override suspend fun execute(params: SaveFilterTask.Params): Try { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/DefaultGetGroupDataTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/DefaultGetGroupDataTask.kt index 0785c33472..15d4d55f2f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/DefaultGetGroupDataTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/DefaultGetGroupDataTask.kt @@ -25,11 +25,13 @@ import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.database.model.GroupSummaryEntity import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.group.model.GroupRooms import im.vector.matrix.android.internal.session.group.model.GroupSummaryResponse import im.vector.matrix.android.internal.session.group.model.GroupUsers import im.vector.matrix.android.internal.util.tryTransactionSync import io.realm.kotlin.createObject +import javax.inject.Inject internal interface GetGroupDataTask : Task { @@ -37,8 +39,8 @@ internal interface GetGroupDataTask : Task { } - -internal class DefaultGetGroupDataTask( +@SessionScope +internal class DefaultGetGroupDataTask @Inject constructor( private val groupAPI: GroupAPI, private val monarchy: Monarchy ) : GetGroupDataTask { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/DefaultGroupService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/DefaultGroupService.kt index 5c0f88de2f..a574df7c8f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/DefaultGroupService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/DefaultGroupService.kt @@ -25,8 +25,9 @@ import im.vector.matrix.android.internal.database.mapper.asDomain import im.vector.matrix.android.internal.database.model.GroupSummaryEntity import im.vector.matrix.android.internal.database.model.GroupSummaryEntityFields import im.vector.matrix.android.internal.database.query.where +import javax.inject.Inject -internal class DefaultGroupService(private val monarchy: Monarchy) : GroupService { +internal class DefaultGroupService @Inject constructor(private val monarchy: Monarchy) : GroupService { override fun getGroup(groupId: String): Group? { return null diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GetGroupDataWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GetGroupDataWorker.kt index 34960493f6..7e979c72a8 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GetGroupDataWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GetGroupDataWorker.kt @@ -18,25 +18,25 @@ package im.vector.matrix.android.internal.session.group import android.content.Context import androidx.work.CoroutineWorker -import androidx.work.Worker import androidx.work.WorkerParameters import arrow.core.Try +import com.squareup.inject.assisted.Assisted +import com.squareup.inject.assisted.AssistedInject import com.squareup.moshi.JsonClass -import im.vector.matrix.android.internal.di.MatrixKoinComponent +import im.vector.matrix.android.internal.di.ChildWorkerFactory import im.vector.matrix.android.internal.util.WorkerParamsFactory -import org.koin.standalone.inject -internal class GetGroupDataWorker(context: Context, - workerParameters: WorkerParameters -) : CoroutineWorker(context, workerParameters), MatrixKoinComponent { +internal class GetGroupDataWorker @AssistedInject constructor( + @Assisted context: Context, + @Assisted workerParameters: WorkerParameters, + private val getGroupDataTask: GetGroupDataTask +) : CoroutineWorker(context, workerParameters) { @JsonClass(generateAdapter = true) internal data class Params( val groupIds: List ) - private val getGroupDataTask by inject() - override suspend fun doWork(): Result { val params = WorkerParamsFactory.fromData(inputData) ?: return Result.failure() @@ -52,4 +52,7 @@ internal class GetGroupDataWorker(context: Context, return getGroupDataTask.execute(GetGroupDataTask.Params(groupId)) } + @AssistedInject.Factory + interface Factory : ChildWorkerFactory + } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupModule.kt index fe63ef9096..5d7ac1f21d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupModule.kt @@ -16,22 +16,15 @@ package im.vector.matrix.android.internal.session.group -import im.vector.matrix.android.internal.session.DefaultSession -import org.koin.dsl.module.module +import dagger.Module +import im.vector.matrix.android.internal.session.SessionScope import retrofit2.Retrofit -class GroupModule { - - val definition = module(override = true) { - - scope(DefaultSession.SCOPE) { - val retrofit: Retrofit = get() - retrofit.create(GroupAPI::class.java) - } - - scope(DefaultSession.SCOPE) { - DefaultGetGroupDataTask(get(), get()) as GetGroupDataTask - } +@Module +internal class GroupModule { + @SessionScope + fun providesGroupAPI(retrofit: Retrofit): GroupAPI { + return retrofit.create(GroupAPI::class.java) } } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupSummaryUpdater.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupSummaryUpdater.kt index 826c53fcdd..f1306b76af 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupSummaryUpdater.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupSummaryUpdater.kt @@ -25,12 +25,14 @@ import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.internal.database.RealmLiveEntityObserver import im.vector.matrix.android.internal.database.model.GroupEntity import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.util.WorkerParamsFactory +import javax.inject.Inject private const val GET_GROUP_DATA_WORKER = "GET_GROUP_DATA_WORKER" -internal class GroupSummaryUpdater(monarchy: Monarchy -) : RealmLiveEntityObserver(monarchy) { +@SessionScope +internal class GroupSummaryUpdater @Inject constructor(monarchy: Monarchy) : RealmLiveEntityObserver(monarchy) { override val query = Monarchy.Query { GroupEntity.where(it) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomDirectoryService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomDirectoryService.kt index 1a434dd638..764d481662 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomDirectoryService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomDirectoryService.kt @@ -27,11 +27,12 @@ import im.vector.matrix.android.internal.session.room.directory.GetThirdPartyPro import im.vector.matrix.android.internal.session.room.membership.joining.JoinRoomTask import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith +import javax.inject.Inject -internal class DefaultRoomDirectoryService(private val getPublicRoomTask: GetPublicRoomTask, - private val joinRoomTask: JoinRoomTask, - private val getThirdPartyProtocolsTask: GetThirdPartyProtocolsTask, - private val taskExecutor: TaskExecutor) : RoomDirectoryService { +internal class DefaultRoomDirectoryService @Inject constructor(private val getPublicRoomTask: GetPublicRoomTask, + private val joinRoomTask: JoinRoomTask, + private val getThirdPartyProtocolsTask: GetThirdPartyProtocolsTask, + private val taskExecutor: TaskExecutor) : RoomDirectoryService { override fun getPublicRooms(server: String?, publicRoomsParams: PublicRoomsParams, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt index 18de69c4e5..448d316cde 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt @@ -28,15 +28,18 @@ import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.model.RoomSummaryEntity import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.create.CreateRoomTask import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith import im.vector.matrix.android.internal.util.fetchManaged +import javax.inject.Inject -internal class DefaultRoomService(private val monarchy: Monarchy, - private val createRoomTask: CreateRoomTask, - private val roomFactory: RoomFactory, - private val taskExecutor: TaskExecutor) : RoomService { +@SessionScope +internal class DefaultRoomService @Inject constructor(private val monarchy: Monarchy, + private val createRoomTask: CreateRoomTask, + private val roomFactory: RoomFactory, + private val taskExecutor: TaskExecutor) : RoomService { override fun createRoom(createRoomParams: CreateRoomParams, callback: MatrixCallback) { createRoomTask diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationExtractor.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationExtractor.kt index e52f5e0988..78e24b2da4 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationExtractor.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationExtractor.kt @@ -20,12 +20,16 @@ import im.vector.matrix.android.internal.database.mapper.asDomain import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryEntity import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import io.realm.Realm +import javax.inject.Inject /** * Fetches annotations (reactions, edits...) associated to a given eventEntity from the data layer. */ -internal class EventRelationExtractor { + +@SessionScope +internal class EventRelationExtractor @Inject constructor() { fun extractFrom(event: EventEntity, realm: Realm = event.realm): EventAnnotationsSummary? { return EventAnnotationsSummaryEntity.where(realm, event.eventId).findFirst()?.asDomain() diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationsAggregationTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationsAggregationTask.kt index e12d60cac2..18d0896e4c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationsAggregationTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/EventRelationsAggregationTask.kt @@ -26,10 +26,12 @@ import im.vector.matrix.android.internal.database.mapper.EventMapper import im.vector.matrix.android.internal.database.model.* import im.vector.matrix.android.internal.database.query.create import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.util.tryTransactionSync import io.realm.Realm import timber.log.Timber +import javax.inject.Inject internal interface EventRelationsAggregationTask : Task { @@ -42,7 +44,8 @@ internal interface EventRelationsAggregationTask : Task(monarchy) { override val query = Monarchy.Query { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAvatarResolver.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAvatarResolver.kt index c26f059646..68373428bb 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAvatarResolver.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAvatarResolver.kt @@ -27,10 +27,13 @@ import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.query.prev import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.membership.RoomMembers +import javax.inject.Inject -internal class RoomAvatarResolver(private val monarchy: Monarchy, - private val credentials: Credentials) { +@SessionScope +internal class RoomAvatarResolver @Inject constructor(private val monarchy: Monarchy, + private val credentials: Credentials) { /** * Compute the room avatar url diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomFactory.kt index 4f4b7e73f2..c3c58e5af4 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomFactory.kt @@ -19,6 +19,7 @@ package im.vector.matrix.android.internal.session.room import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.session.crypto.CryptoService import im.vector.matrix.android.api.session.room.Room +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.membership.DefaultMembershipService import im.vector.matrix.android.internal.session.room.membership.LoadRoomMembersTask import im.vector.matrix.android.internal.session.room.membership.SenderRoomMemberExtractor @@ -39,21 +40,23 @@ import im.vector.matrix.android.internal.session.room.timeline.GetContextOfEvent import im.vector.matrix.android.internal.session.room.timeline.PaginationTask import im.vector.matrix.android.internal.session.room.timeline.TimelineEventFactory import im.vector.matrix.android.internal.task.TaskExecutor +import javax.inject.Inject -internal class RoomFactory(private val monarchy: Monarchy, - private val eventFactory: LocalEchoEventFactory, - private val taskExecutor: TaskExecutor, - private val loadRoomMembersTask: LoadRoomMembersTask, - private val inviteTask: InviteTask, - private val sendStateTask: SendStateTask, - private val paginationTask: PaginationTask, - private val contextOfEventTask: GetContextOfEventTask, - private val setReadMarkersTask: SetReadMarkersTask, - private val cryptoService: CryptoService, - private val findReactionEventForUndoTask: FindReactionEventForUndoTask, - private val updateQuickReactionTask: UpdateQuickReactionTask, - private val joinRoomTask: JoinRoomTask, - private val leaveRoomTask: LeaveRoomTask) { +@SessionScope +internal class RoomFactory @Inject constructor(private val monarchy: Monarchy, + private val eventFactory: LocalEchoEventFactory, + private val taskExecutor: TaskExecutor, + private val loadRoomMembersTask: LoadRoomMembersTask, + private val inviteTask: InviteTask, + private val sendStateTask: SendStateTask, + private val paginationTask: PaginationTask, + private val contextOfEventTask: GetContextOfEventTask, + private val setReadMarkersTask: SetReadMarkersTask, + private val cryptoService: CryptoService, + private val findReactionEventForUndoTask: FindReactionEventForUndoTask, + private val updateQuickReactionTask: UpdateQuickReactionTask, + private val joinRoomTask: JoinRoomTask, + private val leaveRoomTask: LeaveRoomTask) { fun instantiate(roomId: String): Room { val roomMemberExtractor = SenderRoomMemberExtractor(roomId) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt index 671a947f7b..64ac991c88 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomModule.kt @@ -16,7 +16,9 @@ package im.vector.matrix.android.internal.session.room -import im.vector.matrix.android.internal.session.DefaultSession +import dagger.Module +import dagger.Provides +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.create.CreateRoomTask import im.vector.matrix.android.internal.session.room.create.DefaultCreateRoomTask import im.vector.matrix.android.internal.session.room.membership.DefaultLoadRoomMembersTask @@ -39,87 +41,20 @@ import im.vector.matrix.android.internal.session.room.send.LocalEchoEventFactory import im.vector.matrix.android.internal.session.room.send.LocalEchoUpdater import im.vector.matrix.android.internal.session.room.state.DefaultSendStateTask import im.vector.matrix.android.internal.session.room.state.SendStateTask -import im.vector.matrix.android.internal.session.room.timeline.* -import org.koin.dsl.module.module +import im.vector.matrix.android.internal.session.room.timeline.DefaultGetContextOfEventTask +import im.vector.matrix.android.internal.session.room.timeline.DefaultPaginationTask +import im.vector.matrix.android.internal.session.room.timeline.GetContextOfEventTask +import im.vector.matrix.android.internal.session.room.timeline.PaginationTask +import im.vector.matrix.android.internal.session.room.timeline.TokenChunkEventPersistor import retrofit2.Retrofit +@Module +internal class RoomModule { -class RoomModule { - - val definition = module(override = true) { - - scope(DefaultSession.SCOPE) { - val retrofit: Retrofit = get() - retrofit.create(RoomAPI::class.java) - } - - scope(DefaultSession.SCOPE) { - DefaultLoadRoomMembersTask(get(), get(), get(), get()) as LoadRoomMembersTask - } - - scope(DefaultSession.SCOPE) { - TokenChunkEventPersistor(get()) - } - - scope(DefaultSession.SCOPE) { - DefaultPaginationTask(get(), get(), get()) as PaginationTask - } - - scope(DefaultSession.SCOPE) { - DefaultGetContextOfEventTask(get(), get(), get()) as GetContextOfEventTask - } - - scope(DefaultSession.SCOPE) { - DefaultSetReadMarkersTask(get(), get(), get()) as SetReadMarkersTask - } - - scope(DefaultSession.SCOPE) { - LocalEchoEventFactory(get(), get()) - } - - scope(DefaultSession.SCOPE) { - LocalEchoUpdater(get()) - } - - scope(DefaultSession.SCOPE) { - RoomFactory(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) - } - - scope(DefaultSession.SCOPE) { - DefaultCreateRoomTask(get(), get("SessionRealmConfiguration")) as CreateRoomTask - } - - scope(DefaultSession.SCOPE) { - DefaultInviteTask(get()) as InviteTask - } - - scope(DefaultSession.SCOPE) { - DefaultJoinRoomTask(get()) as JoinRoomTask - } - - scope(DefaultSession.SCOPE) { - DefaultLeaveRoomTask(get()) as LeaveRoomTask - } - - scope(DefaultSession.SCOPE) { - DefaultSendStateTask(get()) as SendStateTask - } - - scope(DefaultSession.SCOPE) { - DefaultFindReactionEventForUndoTask(get()) as FindReactionEventForUndoTask - } - - scope(DefaultSession.SCOPE) { - DefaultUpdateQuickReactionTask(get()) as UpdateQuickReactionTask - } - - scope(DefaultSession.SCOPE) { - DefaultPruneEventTask(get()) as PruneEventTask - } - - scope(DefaultSession.SCOPE) { - DefaultEventRelationsAggregationTask(get()) as EventRelationsAggregationTask - } - + @SessionScope + @Provides + fun providesRoomAPI(retrofit: Retrofit): RoomAPI { + return retrofit.create(RoomAPI::class.java) } + } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomSummaryUpdater.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomSummaryUpdater.kt index 7bf08b70f7..609e55f153 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomSummaryUpdater.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomSummaryUpdater.kt @@ -29,16 +29,19 @@ import im.vector.matrix.android.internal.database.model.RoomSummaryEntity import im.vector.matrix.android.internal.database.query.latestEvent import im.vector.matrix.android.internal.database.query.prev import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.membership.RoomDisplayNameResolver import im.vector.matrix.android.internal.session.room.membership.RoomMembers import im.vector.matrix.android.internal.session.sync.model.RoomSyncSummary import im.vector.matrix.android.internal.session.sync.model.RoomSyncUnreadNotifications import io.realm.Realm import io.realm.kotlin.createObject +import javax.inject.Inject -internal class RoomSummaryUpdater(private val credentials: Credentials, - private val roomDisplayNameResolver: RoomDisplayNameResolver, - private val roomAvatarResolver: RoomAvatarResolver) { +@SessionScope +internal class RoomSummaryUpdater @Inject constructor(private val credentials: Credentials, + private val roomDisplayNameResolver: RoomDisplayNameResolver, + private val roomAvatarResolver: RoomAvatarResolver) { fun update(realm: Realm, roomId: String, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt index 9ff5f51149..37088f7479 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt @@ -23,14 +23,16 @@ import im.vector.matrix.android.internal.database.RealmQueryLatch import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.model.RoomEntityFields import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.task.Task import io.realm.RealmConfiguration +import javax.inject.Inject internal interface CreateRoomTask : Task - -internal class DefaultCreateRoomTask(private val roomAPI: RoomAPI, +@SessionScope +internal class DefaultCreateRoomTask @Inject constructor(private val roomAPI: RoomAPI, private val realmConfiguration: RealmConfiguration) : CreateRoomTask { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/directory/GetPublicRoomTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/directory/GetPublicRoomTask.kt index 4b71b8712b..e3fbed1ece 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/directory/GetPublicRoomTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/directory/GetPublicRoomTask.kt @@ -20,8 +20,10 @@ import arrow.core.Try import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoomsParams import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoomsResponse import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface GetPublicRoomTask : Task { data class Params( @@ -30,7 +32,8 @@ internal interface GetPublicRoomTask : Task { return executeRequest { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/directory/GetThirdPartyProtocolsTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/directory/GetThirdPartyProtocolsTask.kt index a50ec36756..d72c3b5a70 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/directory/GetThirdPartyProtocolsTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/directory/GetThirdPartyProtocolsTask.kt @@ -19,12 +19,15 @@ package im.vector.matrix.android.internal.session.room.directory import arrow.core.Try import im.vector.matrix.android.api.session.room.model.thirdparty.ThirdPartyProtocol import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface GetThirdPartyProtocolsTask : Task> -internal class DefaultGetThirdPartyProtocolsTask(private val roomAPI: RoomAPI) : GetThirdPartyProtocolsTask { +@SessionScope +internal class DefaultGetThirdPartyProtocolsTask @Inject constructor (private val roomAPI: RoomAPI) : GetThirdPartyProtocolsTask { override suspend fun execute(params: Unit): Try> { return executeRequest { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/DefaultMembershipService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/DefaultMembershipService.kt index a9b48ea69a..9f51ade001 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/DefaultMembershipService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/DefaultMembershipService.kt @@ -27,20 +27,23 @@ import im.vector.matrix.android.api.session.room.model.Membership import im.vector.matrix.android.api.session.room.model.RoomMember import im.vector.matrix.android.api.util.Cancelable import im.vector.matrix.android.internal.database.mapper.asDomain +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.membership.joining.InviteTask import im.vector.matrix.android.internal.session.room.membership.joining.JoinRoomTask import im.vector.matrix.android.internal.session.room.membership.leaving.LeaveRoomTask import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith import im.vector.matrix.android.internal.util.fetchCopied +import javax.inject.Inject -internal class DefaultMembershipService(private val roomId: String, - private val monarchy: Monarchy, - private val taskExecutor: TaskExecutor, - private val loadRoomMembersTask: LoadRoomMembersTask, - private val inviteTask: InviteTask, - private val joinTask: JoinRoomTask, - private val leaveRoomTask: LeaveRoomTask +@SessionScope +internal class DefaultMembershipService @Inject constructor(private val roomId: String, + private val monarchy: Monarchy, + private val taskExecutor: TaskExecutor, + private val loadRoomMembersTask: LoadRoomMembersTask, + private val inviteTask: InviteTask, + private val joinTask: JoinRoomTask, + private val leaveRoomTask: LeaveRoomTask ) : MembershipService { override fun loadRoomMembersIfNeeded(): Cancelable { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/LoadRoomMembersTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/LoadRoomMembersTask.kt index 44a050db47..d49448a048 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/LoadRoomMembersTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/LoadRoomMembersTask.kt @@ -23,12 +23,14 @@ import im.vector.matrix.android.internal.database.helper.addStateEvents import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.session.room.RoomSummaryUpdater import im.vector.matrix.android.internal.session.sync.SyncTokenStore import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.util.tryTransactionSync import io.realm.kotlin.createObject +import javax.inject.Inject internal interface LoadRoomMembersTask : Task { @@ -38,7 +40,8 @@ internal interface LoadRoomMembersTask : Task): String? { val currentMember = members[userId] diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/InviteTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/InviteTask.kt index 3610cbe023..ec35b44387 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/InviteTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/InviteTask.kt @@ -18,8 +18,10 @@ package im.vector.matrix.android.internal.session.room.membership.joining import arrow.core.Try import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface InviteTask : Task { @@ -29,7 +31,8 @@ internal interface InviteTask : Task { ) } -internal class DefaultInviteTask(private val roomAPI: RoomAPI) : InviteTask { +@SessionScope +internal class DefaultInviteTask @Inject constructor(private val roomAPI: RoomAPI) : InviteTask { override suspend fun execute(params: InviteTask.Params): Try { return executeRequest { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/JoinRoomTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/JoinRoomTask.kt index 3fe2d13996..37f4a2fa02 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/JoinRoomTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/JoinRoomTask.kt @@ -18,8 +18,10 @@ package im.vector.matrix.android.internal.session.room.membership.joining import arrow.core.Try import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface JoinRoomTask : Task { data class Params( @@ -27,7 +29,8 @@ internal interface JoinRoomTask : Task { ) } -internal class DefaultJoinRoomTask(private val roomAPI: RoomAPI) : JoinRoomTask { +@SessionScope +internal class DefaultJoinRoomTask @Inject constructor(private val roomAPI: RoomAPI) : JoinRoomTask { override suspend fun execute(params: JoinRoomTask.Params): Try { return executeRequest { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/leaving/LeaveRoomTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/leaving/LeaveRoomTask.kt index 9eb1728f7e..30c73035ad 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/leaving/LeaveRoomTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/leaving/LeaveRoomTask.kt @@ -18,8 +18,10 @@ package im.vector.matrix.android.internal.session.room.membership.leaving import arrow.core.Try import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface LeaveRoomTask : Task { data class Params( @@ -27,7 +29,8 @@ internal interface LeaveRoomTask : Task { ) } -internal class DefaultLeaveRoomTask(private val roomAPI: RoomAPI) : LeaveRoomTask { +@SessionScope +internal class DefaultLeaveRoomTask @Inject constructor(private val roomAPI: RoomAPI) : LeaveRoomTask { override suspend fun execute(params: LeaveRoomTask.Params): Try { return executeRequest { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt index 4d3bc6fd93..4b663d5a26 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/EventsPruner.kt @@ -23,18 +23,21 @@ import im.vector.matrix.android.internal.database.RealmLiveEntityObserver import im.vector.matrix.android.internal.database.mapper.asDomain import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith import timber.log.Timber +import javax.inject.Inject /** * Listens to the database for the insertion of any redaction event. * As it will actually delete the content, it should be called last in the list of listener. */ -internal class EventsPruner(monarchy: Monarchy, - private val credentials: Credentials, - private val pruneEventTask: PruneEventTask, - private val taskExecutor: TaskExecutor) : +@SessionScope +internal class EventsPruner @Inject constructor(monarchy: Monarchy, + private val credentials: Credentials, + private val pruneEventTask: PruneEventTask, + private val taskExecutor: TaskExecutor) : RealmLiveEntityObserver(monarchy) { override val query = Monarchy.Query { EventEntity.where(it, type = EventType.REDACTION) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/PruneEventTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/PruneEventTask.kt index ef8557aec2..08d52e249d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/PruneEventTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/prune/PruneEventTask.kt @@ -26,10 +26,12 @@ import im.vector.matrix.android.internal.database.mapper.EventMapper import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.di.MoshiProvider +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.util.tryTransactionSync import io.realm.Realm import timber.log.Timber +import javax.inject.Inject internal interface PruneEventTask : Task { @@ -41,8 +43,8 @@ internal interface PruneEventTask : Task { } -internal class DefaultPruneEventTask( - private val monarchy: Monarchy) : PruneEventTask { +@SessionScope +internal class DefaultPruneEventTask @Inject constructor(private val monarchy: Monarchy) : PruneEventTask { override suspend fun execute(params: PruneEventTask.Params): Try { return monarchy.tryTransactionSync { realm -> diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/DefaultReadService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/DefaultReadService.kt index 30bbe30c28..ad3ad3b60a 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/DefaultReadService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/DefaultReadService.kt @@ -21,11 +21,14 @@ import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.session.room.read.ReadService import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.query.latestEvent +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith import im.vector.matrix.android.internal.util.fetchCopied +import javax.inject.Inject -internal class DefaultReadService(private val roomId: String, +@SessionScope +internal class DefaultReadService @Inject constructor(private val roomId: String, private val monarchy: Monarchy, private val taskExecutor: TaskExecutor, private val setReadMarkersTask: SetReadMarkersTask) : ReadService { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/SetReadMarkersTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/SetReadMarkersTask.kt index 0592077eaf..aef52f5f3e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/SetReadMarkersTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/read/SetReadMarkersTask.kt @@ -29,9 +29,11 @@ import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoo import im.vector.matrix.android.internal.database.query.latestEvent import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.util.tryTransactionAsync +import javax.inject.Inject internal interface SetReadMarkersTask : Task { @@ -45,7 +47,8 @@ internal interface SetReadMarkersTask : Task { private const val READ_MARKER = "m.fully_read" private const val READ_RECEIPT = "m.read" -internal class DefaultSetReadMarkersTask(private val roomAPI: RoomAPI, +@SessionScope +internal class DefaultSetReadMarkersTask @Inject constructor(private val roomAPI: RoomAPI, private val credentials: Credentials, private val monarchy: Monarchy ) : SetReadMarkersTask { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt index 264909e956..a48a0ad60b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/DefaultRelationService.kt @@ -31,6 +31,7 @@ import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryE import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.send.LocalEchoEventFactory import im.vector.matrix.android.internal.session.room.send.RedactEventWorker import im.vector.matrix.android.internal.session.room.send.SendEventWorker @@ -41,14 +42,14 @@ import im.vector.matrix.android.internal.util.CancelableWork import im.vector.matrix.android.internal.util.WorkerParamsFactory import im.vector.matrix.android.internal.util.tryTransactionAsync import timber.log.Timber +import javax.inject.Inject - -internal class DefaultRelationService(private val roomId: String, - private val eventFactory: LocalEchoEventFactory, - private val findReactionEventForUndoTask: FindReactionEventForUndoTask, - private val updateQuickReactionTask: UpdateQuickReactionTask, - private val monarchy: Monarchy, - private val taskExecutor: TaskExecutor) +internal class DefaultRelationService constructor(private val roomId: String, + private val eventFactory: LocalEchoEventFactory, + private val findReactionEventForUndoTask: FindReactionEventForUndoTask, + private val updateQuickReactionTask: UpdateQuickReactionTask, + private val monarchy: Monarchy, + private val taskExecutor: TaskExecutor) : RelationService { @@ -139,7 +140,7 @@ internal class DefaultRelationService(private val roomId: String, private fun createRedactEventWork(localEvent: Event, eventId: String, reason: String?): OneTimeWorkRequest { val sendContentWorkerParams = RedactEventWorker.Params(localEvent.eventId!!, - roomId, eventId, reason) + roomId, eventId, reason) val redactWorkData = WorkerParamsFactory.toData(sendContentWorkerParams) return TimelineSendEventWorkCommon.createWork(redactWorkData) } @@ -194,9 +195,9 @@ internal class DefaultRelationService(private val roomId: String, private fun saveLocalEcho(event: Event) { monarchy.tryTransactionAsync { realm -> val roomEntity = RoomEntity.where(realm, roomId = roomId).findFirst() - ?: return@tryTransactionAsync + ?: return@tryTransactionAsync val liveChunk = ChunkEntity.findLastLiveChunkFromRoom(realm, roomId = roomId) - ?: return@tryTransactionAsync + ?: return@tryTransactionAsync roomEntity.addSendingEvent(event, liveChunk.forwardsStateIndex ?: 0) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/FindReactionEventForUndoTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/FindReactionEventForUndoTask.kt index 07b48bd013..7a7abdbe71 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/FindReactionEventForUndoTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/FindReactionEventForUndoTask.kt @@ -21,8 +21,10 @@ import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryE import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.model.ReactionAggregatedSummaryEntityFields import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task import io.realm.Realm +import javax.inject.Inject internal interface FindReactionEventForUndoTask : Task { @@ -40,7 +42,8 @@ internal interface FindReactionEventForUndoTask : Task { return Try { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/SendRelationWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/SendRelationWorker.kt index 41184aaf14..c41ac5df35 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/SendRelationWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/SendRelationWorker.kt @@ -18,21 +18,25 @@ package im.vector.matrix.android.internal.session.room.relation import android.content.Context import androidx.work.Worker import androidx.work.WorkerParameters +import com.squareup.inject.assisted.Assisted +import com.squareup.inject.assisted.AssistedInject import com.squareup.moshi.JsonClass import im.vector.matrix.android.api.failure.Failure import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.api.session.room.model.relation.ReactionContent import im.vector.matrix.android.api.session.room.model.relation.ReactionInfo -import im.vector.matrix.android.internal.di.MatrixKoinComponent +import im.vector.matrix.android.internal.di.ChildWorkerFactory import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.session.room.send.SendResponse import im.vector.matrix.android.internal.util.WorkerParamsFactory -import org.koin.standalone.inject -class SendRelationWorker(context: Context, params: WorkerParameters) - : Worker(context, params), MatrixKoinComponent { +internal class SendRelationWorker @AssistedInject constructor( + @Assisted context: Context, + @Assisted params: WorkerParameters, + private val roomAPI: RoomAPI) + : Worker(context, params) { @JsonClass(generateAdapter = true) @@ -42,21 +46,19 @@ class SendRelationWorker(context: Context, params: WorkerParameters) val relationType: String? = null ) - private val roomAPI by inject() - override fun doWork(): Result { val params = WorkerParamsFactory.fromData(inputData) - ?: return Result.failure() + ?: return Result.failure() val localEvent = params.event if (localEvent.eventId == null) { return Result.failure() } val relationContent = localEvent.content.toModel() - ?: return Result.failure() + ?: return Result.failure() val relatedEventId = relationContent.relatesTo?.eventId ?: return Result.failure() val relationType = (relationContent.relatesTo as? ReactionInfo)?.type ?: params.relationType - ?: return Result.failure() + ?: return Result.failure() val result = executeRequest { apiCall = roomAPI.sendRelation( @@ -68,14 +70,17 @@ class SendRelationWorker(context: Context, params: WorkerParameters) ) } return result.fold({ - when (it) { - is Failure.NetworkConnection -> Result.retry() - else -> { - //TODO mark as failed to send? - //always return success, or the chain will be stuck for ever! - Result.success() - } - } - }, { Result.success() }) + when (it) { + is Failure.NetworkConnection -> Result.retry() + else -> { + //TODO mark as failed to send? + //always return success, or the chain will be stuck for ever! + Result.success() + } + } + }, { Result.success() }) } + + @AssistedInject.Factory + interface Factory : ChildWorkerFactory } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/UpdateQuickReactionTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/UpdateQuickReactionTask.kt index bc54464fdf..2169adebc1 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/UpdateQuickReactionTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/relation/UpdateQuickReactionTask.kt @@ -21,8 +21,10 @@ import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryE import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.model.ReactionAggregatedSummaryEntityFields import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.Task import io.realm.Realm +import javax.inject.Inject internal interface UpdateQuickReactionTask : Task { @@ -41,7 +43,8 @@ internal interface UpdateQuickReactionTask : Task { return Try { var res: Pair?>? = null diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt index e54af1cae8..585b925a94 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt @@ -33,12 +33,14 @@ import im.vector.matrix.android.internal.database.model.ChunkEntity import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.content.ThumbnailExtractor import im.vector.matrix.android.internal.util.StringProvider import im.vector.matrix.android.internal.util.tryTransactionAsync import org.commonmark.parser.Parser import org.commonmark.renderer.html.HtmlRenderer import java.util.* +import javax.inject.Inject /** * Creates local echo of events for room events. @@ -49,7 +51,9 @@ import java.util.* * * The transactionID is used as loc */ -internal class LocalEchoEventFactory(private val credentials: Credentials, private val stringProvider: StringProvider) { + +@SessionScope +internal class LocalEchoEventFactory @Inject constructor(private val credentials: Credentials, private val stringProvider: StringProvider) { fun createTextEvent(roomId: String, msgType: String, text: String, autoMarkdown: Boolean): Event { if (autoMarkdown && msgType == MessageType.MSGTYPE_TEXT) { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoUpdater.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoUpdater.kt index 7dc69536f9..689d2b484c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoUpdater.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoUpdater.kt @@ -22,9 +22,12 @@ import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.session.room.send.SendState import im.vector.matrix.android.internal.database.model.EventEntity import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.util.tryTransactionAsync +import javax.inject.Inject -internal class LocalEchoUpdater(private val monarchy: Monarchy) { +@SessionScope +internal class LocalEchoUpdater @Inject constructor(private val monarchy: Monarchy) { fun updateSendState(eventId: String, sendState: SendState) { monarchy.tryTransactionAsync { realm -> diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/SendEventWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/SendEventWorker.kt index 32239b53a8..598352c50e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/SendEventWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/SendEventWorker.kt @@ -19,18 +19,23 @@ package im.vector.matrix.android.internal.session.room.send import android.content.Context import androidx.work.Worker import androidx.work.WorkerParameters +import com.squareup.inject.assisted.Assisted +import com.squareup.inject.assisted.AssistedInject import com.squareup.moshi.JsonClass import im.vector.matrix.android.api.failure.Failure import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.room.send.SendState -import im.vector.matrix.android.internal.di.MatrixKoinComponent +import im.vector.matrix.android.internal.di.ChildWorkerFactory import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.util.WorkerParamsFactory -import org.koin.standalone.inject -internal class SendEventWorker(context: Context, params: WorkerParameters) - : Worker(context, params), MatrixKoinComponent { +internal class SendEventWorker @AssistedInject constructor( + @Assisted context: Context, + @Assisted params: WorkerParameters, + private val roomAPI: RoomAPI, + private val localEchoUpdater: LocalEchoUpdater) + : Worker(context, params) { @JsonClass(generateAdapter = true) @@ -39,13 +44,10 @@ internal class SendEventWorker(context: Context, params: WorkerParameters) val event: Event ) - private val roomAPI by inject() - private val localEchoUpdater by inject() - override fun doWork(): Result { val params = WorkerParamsFactory.fromData(inputData) - ?: return Result.success() + ?: return Result.success() val event = params.event if (event.eventId == null) { @@ -62,14 +64,18 @@ internal class SendEventWorker(context: Context, params: WorkerParameters) ) } return result.fold({ - when (it) { - is Failure.NetworkConnection -> Result.retry() - else -> { - localEchoUpdater.updateSendState(event.eventId, SendState.UNDELIVERED) - //always return success, or the chain will be stuck for ever! - Result.success() - } - } - }, { Result.success() }) + when (it) { + is Failure.NetworkConnection -> Result.retry() + else -> { + localEchoUpdater.updateSendState(event.eventId, SendState.UNDELIVERED) + //always return success, or the chain will be stuck for ever! + Result.success() + } + } + }, { Result.success() }) } + + @AssistedInject.Factory + interface Factory : ChildWorkerFactory + } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/state/DefaultStateService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/state/DefaultStateService.kt index f1ad712e4a..6988b6f8a5 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/state/DefaultStateService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/state/DefaultStateService.kt @@ -19,10 +19,13 @@ package im.vector.matrix.android.internal.session.room.state import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.room.state.StateService +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.task.TaskExecutor import im.vector.matrix.android.internal.task.configureWith +import javax.inject.Inject -internal class DefaultStateService(private val roomId: String, +@SessionScope +internal class DefaultStateService @Inject constructor(private val roomId: String, private val taskExecutor: TaskExecutor, private val sendStateTask: SendStateTask) : StateService { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/state/SendStateTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/state/SendStateTask.kt index b6d3a74868..45e714e120 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/state/SendStateTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/state/SendStateTask.kt @@ -18,8 +18,10 @@ package im.vector.matrix.android.internal.session.room.state import arrow.core.Try import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface SendStateTask : Task { data class Params( @@ -29,7 +31,8 @@ internal interface SendStateTask : Task { ) } -internal class DefaultSendStateTask(private val roomAPI: RoomAPI) : SendStateTask { +@SessionScope +internal class DefaultSendStateTask @Inject constructor(private val roomAPI: RoomAPI) : SendStateTask { override suspend fun execute(params: SendStateTask.Params): Try { return executeRequest { apiCall = roomAPI.sendStateEvent(params.roomId, params.eventType, params.body) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultGetContextOfEventTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultGetContextOfEventTask.kt index 5d72ac3344..49c69a00bb 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultGetContextOfEventTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultGetContextOfEventTask.kt @@ -18,9 +18,11 @@ package im.vector.matrix.android.internal.session.room.timeline import arrow.core.Try import im.vector.matrix.android.internal.network.executeRequest +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.session.filter.FilterRepository import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.task.Task +import javax.inject.Inject internal interface GetContextOfEventTask : Task { @@ -31,7 +33,8 @@ internal interface GetContextOfEventTask : Task { @@ -34,9 +36,10 @@ internal interface PaginationTask : Task { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimeline.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimeline.kt index 6743d1cb0b..c276c26f90 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimeline.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/DefaultTimeline.kt @@ -274,7 +274,7 @@ internal class DefaultTimeline( val newRequestedCount = count - builtCount updatePaginationState(direction) { it.copy(requestedCount = newRequestedCount) } val fetchingCount = Math.max(MIN_FETCHING_COUNT, newRequestedCount) - executePaginationTask(direction, fetchingCount) + executePaginationTask @Inject constructor(direction, fetchingCount) } else { updatePaginationState(direction) { it.copy(isPaginating = false, requestedCount = 0) } } @@ -350,7 +350,7 @@ internal class DefaultTimeline( /** * This has to be called on TimelineThread as it access realm live results */ - private fun executePaginationTask(direction: Timeline.Direction, limit: Int) { + private fun executePaginationTask @Inject constructor(direction: Timeline.Direction, limit: Int) { val token = getTokenLive(direction) ?: return val params = PaginationTask.Params(roomId = roomId, from = token, @@ -367,7 +367,7 @@ internal class DefaultTimeline( } else { // Database won't be updated, so we force pagination request backgroundHandler.get()?.post { - executePaginationTask(direction, limit) + executePaginationTask @Inject constructor(direction, limit) } } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/GetEventTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/GetEventTask.kt index ae575d68c0..9ac5fce35d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/GetEventTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/GetEventTask.kt @@ -22,7 +22,7 @@ import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.session.room.RoomAPI -internal class GetEventTask(private val roomAPI: RoomAPI +internal class GetEventTask @Inject constructor(private val roomAPI: RoomAPI ) : Task { internal data class Params( diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventFactory.kt index 7c4cfff940..fa602500f7 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TimelineEventFactory.kt @@ -30,12 +30,13 @@ import im.vector.matrix.android.internal.session.room.membership.SenderRoomMembe import io.realm.Realm import timber.log.Timber import java.util.* +import javax.inject.Inject /** * This class is responsible for building [TimelineEvent] returned by a [Timeline] through [TimelineService] * It handles decryption, extracting additional data around an event as sender data and relation. */ -internal class TimelineEventFactory( +internal class TimelineEventFactory @Inject constructor( private val roomMemberExtractor: SenderRoomMemberExtractor, private val relationExtractor: EventRelationExtractor, private val cryptoService: CryptoService) { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEventPersistor.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEventPersistor.kt index 9c40b4aa66..3aa07b00f3 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEventPersistor.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/timeline/TokenChunkEventPersistor.kt @@ -25,14 +25,17 @@ import im.vector.matrix.android.internal.database.query.create import im.vector.matrix.android.internal.database.query.find import im.vector.matrix.android.internal.database.query.findAllIncludingEvents import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.SessionScope import im.vector.matrix.android.internal.util.tryTransactionSync import io.realm.kotlin.createObject import timber.log.Timber +import javax.inject.Inject /** * Insert Chunk in DB, and eventually merge with existing chunk event */ -internal class TokenChunkEventPersistor(private val monarchy: Monarchy) { +@SessionScope +internal class TokenChunkEventPersistor @Inject constructor(private val monarchy: Monarchy) { /** *
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/DefaultSignOutService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/DefaultSignOutService.kt
index ff39990017..3589df3d37 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/DefaultSignOutService.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/DefaultSignOutService.kt
@@ -18,11 +18,14 @@ package im.vector.matrix.android.internal.session.signout
 
 import im.vector.matrix.android.api.MatrixCallback
 import im.vector.matrix.android.api.session.signout.SignOutService
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.task.TaskExecutor
 import im.vector.matrix.android.internal.task.configureWith
+import javax.inject.Inject
 
-internal class DefaultSignOutService(private val signOutTask: SignOutTask,
-                                     private val taskExecutor: TaskExecutor) : SignOutService {
+@SessionScope
+internal class DefaultSignOutService @Inject constructor(private val signOutTask: SignOutTask,
+                                                         private val taskExecutor: TaskExecutor) : SignOutService {
 
     override fun signOut(callback: MatrixCallback) {
         signOutTask
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/SignOutModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/SignOutModule.kt
index 98098b1af5..7e2fbc850f 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/SignOutModule.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/SignOutModule.kt
@@ -16,22 +16,16 @@
 
 package im.vector.matrix.android.internal.session.signout
 
-import im.vector.matrix.android.internal.session.DefaultSession
-import org.koin.dsl.module.module
+import dagger.Module
+import im.vector.matrix.android.internal.session.SessionScope
 import retrofit2.Retrofit
 
-class SignOutModule {
-
-    val definition = module(override = true) {
-
-        scope(DefaultSession.SCOPE) {
-            val retrofit: Retrofit = get()
-            retrofit.create(SignOutAPI::class.java)
-        }
-
-        scope(DefaultSession.SCOPE) {
-            DefaultSignOutTask(get(), get()) as SignOutTask
-        }
+@Module
+internal class SignOutModule {
 
+    @SessionScope
+    fun providesSignOutAPI(retrofit: Retrofit): SignOutAPI {
+        return retrofit.create(SignOutAPI::class.java)
     }
+
 }
\ No newline at end of file
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/SignOutTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/SignOutTask.kt
index 5fa09b9aa7..ab34e0f806 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/SignOutTask.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/signout/SignOutTask.kt
@@ -19,13 +19,15 @@ package im.vector.matrix.android.internal.session.signout
 import arrow.core.Try
 import im.vector.matrix.android.internal.auth.SessionParamsStore
 import im.vector.matrix.android.internal.network.executeRequest
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.task.Task
+import javax.inject.Inject
 
 internal interface SignOutTask : Task
 
-
-internal class DefaultSignOutTask(private val signOutAPI: SignOutAPI,
-                                  private val sessionParamsStore: SessionParamsStore) : SignOutTask {
+@SessionScope
+internal class DefaultSignOutTask @Inject constructor(private val signOutAPI: SignOutAPI,
+                                                      private val sessionParamsStore: SessionParamsStore) : SignOutTask {
 
     override suspend fun execute(params: Unit): Try {
         return executeRequest {
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt
index 6dd2763fa0..5930d86145 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt
@@ -25,20 +25,23 @@ import im.vector.matrix.android.internal.crypto.CryptoManager
 import im.vector.matrix.android.internal.crypto.MXDecryptionException
 import im.vector.matrix.android.internal.crypto.MXEventDecryptionResult
 import im.vector.matrix.android.internal.crypto.verification.DefaultSasVerificationService
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.session.sync.model.SyncResponse
 import im.vector.matrix.android.internal.session.sync.model.ToDeviceSyncResponse
 import timber.log.Timber
+import javax.inject.Inject
 
 
-internal class CryptoSyncHandler(private val cryptoManager: CryptoManager,
-                                 private val sasVerificationService: DefaultSasVerificationService) {
+@SessionScope
+internal class CryptoSyncHandler @Inject constructor(private val cryptoManager: CryptoManager,
+                                                     private val sasVerificationService: DefaultSasVerificationService) {
 
     fun handleToDevice(toDevice: ToDeviceSyncResponse) {
         toDevice.events?.forEach { event ->
             // Decrypt event if necessary
             decryptEvent(event, null)
             if (TextUtils.equals(event.getClearType(), EventType.MESSAGE)
-                    && event.mClearEvent?.content?.toModel()?.type == "m.bad.encrypted") {
+                && event.mClearEvent?.content?.toModel()?.type == "m.bad.encrypted") {
                 Timber.e("## handleToDeviceEvent() : Warning: Unable to decrypt to-device event : " + event.content)
             } else {
                 sasVerificationService.onToDeviceEvent(event)
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/GroupSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/GroupSyncHandler.kt
index 25bea0968d..7c9248c938 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/GroupSyncHandler.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/GroupSyncHandler.kt
@@ -20,12 +20,14 @@ import com.zhuinden.monarchy.Monarchy
 import im.vector.matrix.android.api.session.room.model.Membership
 import im.vector.matrix.android.internal.database.model.GroupEntity
 import im.vector.matrix.android.internal.database.query.where
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.session.sync.model.GroupsSyncResponse
 import im.vector.matrix.android.internal.session.sync.model.InvitedGroupSync
 import io.realm.Realm
+import javax.inject.Inject
 
-
-internal class GroupSyncHandler(private val monarchy: Monarchy) {
+@SessionScope
+internal class GroupSyncHandler @Inject constructor(private val monarchy: Monarchy) {
 
     sealed class HandlingStrategy {
         data class JOINED(val data: Map) : HandlingStrategy()
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt
index 7557f71a61..a7af1e3478 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/ReadReceiptHandler.kt
@@ -17,6 +17,7 @@
 package im.vector.matrix.android.internal.session.sync
 
 import im.vector.matrix.android.internal.database.model.ReadReceiptEntity
+import im.vector.matrix.android.internal.session.SessionScope
 import io.realm.Realm
 import timber.log.Timber
 
@@ -28,7 +29,8 @@ import timber.log.Timber
 //                    dict value ts value
 typealias ReadReceiptContent = Map>>>
 
-internal class ReadReceiptHandler {
+@SessionScope
+internal class ReadReceiptHandler internal constructor() {
 
     fun handle(realm: Realm, roomId: String, content: ReadReceiptContent?) {
         if (content == null) {
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt
index e6d777759b..f8fb285da6 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt
@@ -32,18 +32,21 @@ import im.vector.matrix.android.internal.database.model.RoomEntity
 import im.vector.matrix.android.internal.database.query.find
 import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom
 import im.vector.matrix.android.internal.database.query.where
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.session.room.RoomSummaryUpdater
 import im.vector.matrix.android.internal.session.room.timeline.PaginationDirection
 import im.vector.matrix.android.internal.session.sync.model.*
 import io.realm.Realm
 import io.realm.kotlin.createObject
 import timber.log.Timber
+import javax.inject.Inject
 
-internal class RoomSyncHandler(private val monarchy: Monarchy,
-                               private val readReceiptHandler: ReadReceiptHandler,
-                               private val roomSummaryUpdater: RoomSummaryUpdater,
-                               private val roomTagHandler: RoomTagHandler,
-                               private val cryptoManager: CryptoManager) {
+@SessionScope
+internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarchy,
+                                                   private val readReceiptHandler: ReadReceiptHandler,
+                                                   private val roomSummaryUpdater: RoomSummaryUpdater,
+                                                   private val roomTagHandler: RoomTagHandler,
+                                                   private val cryptoManager: CryptoManager) {
 
     sealed class HandlingStrategy {
         data class JOINED(val data: Map) : HandlingStrategy()
@@ -63,9 +66,9 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
 
     private fun handleRoomSync(realm: Realm, handlingStrategy: HandlingStrategy) {
         val rooms = when (handlingStrategy) {
-            is HandlingStrategy.JOINED -> handlingStrategy.data.map { handleJoinedRoom(realm, it.key, it.value) }
+            is HandlingStrategy.JOINED  -> handlingStrategy.data.map { handleJoinedRoom(realm, it.key, it.value) }
             is HandlingStrategy.INVITED -> handlingStrategy.data.map { handleInvitedRoom(realm, it.key, it.value) }
-            is HandlingStrategy.LEFT -> handlingStrategy.data.map { handleLeftRoom(realm, it.key, it.value) }
+            is HandlingStrategy.LEFT    -> handlingStrategy.data.map { handleLeftRoom(realm, it.key, it.value) }
         }
         realm.insertOrUpdate(rooms)
     }
@@ -77,7 +80,7 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
         Timber.v("Handle join sync for room $roomId")
 
         val roomEntity = RoomEntity.where(realm, roomId).findFirst()
-                ?: realm.createObject(roomId)
+                         ?: realm.createObject(roomId)
 
         if (roomEntity.membership == Membership.INVITE) {
             roomEntity.chunks.deleteAllFromRealm()
@@ -148,7 +151,7 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
                                   InvitedRoomSync): RoomEntity {
         Timber.v("Handle invited sync for room $roomId")
         val roomEntity = RoomEntity.where(realm, roomId).findFirst()
-                ?: realm.createObject(roomId)
+                         ?: realm.createObject(roomId)
         roomEntity.membership = Membership.INVITE
         if (roomSync.inviteState != null && roomSync.inviteState.events.isNotEmpty()) {
             val chunkEntity = handleTimelineEvents(realm, roomId, roomSync.inviteState.events)
@@ -162,7 +165,7 @@ internal class RoomSyncHandler(private val monarchy: Monarchy,
                                roomId: String,
                                roomSync: RoomSync): RoomEntity {
         val roomEntity = RoomEntity.where(realm, roomId).findFirst()
-                ?: realm.createObject(roomId)
+                         ?: realm.createObject(roomId)
 
         roomEntity.membership = Membership.LEAVE
         roomEntity.chunks.deleteAllFromRealm()
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomTagHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomTagHandler.kt
index 90915b42f2..9bec48dd14 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomTagHandler.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomTagHandler.kt
@@ -20,10 +20,13 @@ import im.vector.matrix.android.api.session.room.model.tag.RoomTagContent
 import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
 import im.vector.matrix.android.internal.database.model.RoomTagEntity
 import im.vector.matrix.android.internal.database.query.where
+import im.vector.matrix.android.internal.session.SessionScope
 import io.realm.Realm
 import java.util.*
+import javax.inject.Inject
 
-internal class RoomTagHandler {
+@SessionScope
+internal class RoomTagHandler @Inject constructor() {
 
     fun handle(realm: Realm, roomId: String, content: RoomTagContent?) {
         if (content == null) {
@@ -41,7 +44,7 @@ internal class RoomTagHandler {
             tags.add(tag)
         }
         val roomSummaryEntity = RoomSummaryEntity.where(realm, roomId).findFirst()
-                ?: RoomSummaryEntity(roomId)
+                                ?: RoomSummaryEntity(roomId)
 
         roomSummaryEntity.tags.clear()
         roomSummaryEntity.tags.addAll(tags)
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncModule.kt
index 5fd7f6108e..4e12ccd8fd 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncModule.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncModule.kt
@@ -16,60 +16,16 @@
 
 package im.vector.matrix.android.internal.session.sync
 
-import im.vector.matrix.android.internal.session.DefaultSession
-import im.vector.matrix.android.internal.session.sync.job.SyncThread
-import org.koin.dsl.module.module
+import dagger.Module
+import im.vector.matrix.android.internal.session.SessionScope
 import retrofit2.Retrofit
 
-
+@Module
 internal class SyncModule {
 
-    val definition = module(override = true) {
-
-        scope(DefaultSession.SCOPE) {
-            val retrofit: Retrofit = get()
-            retrofit.create(SyncAPI::class.java)
-        }
-
-        scope(DefaultSession.SCOPE) {
-            ReadReceiptHandler()
-        }
-
-        scope(DefaultSession.SCOPE) {
-            RoomTagHandler()
-        }
-
-        scope(DefaultSession.SCOPE) {
-            RoomSyncHandler(get(), get(), get(), get(), get())
-        }
-
-        scope(DefaultSession.SCOPE) {
-            GroupSyncHandler(get())
-        }
-
-        scope(DefaultSession.SCOPE) {
-            CryptoSyncHandler(get(), get())
-        }
-
-        scope(DefaultSession.SCOPE) {
-            UserAccountDataSyncHandler(get())
-        }
-
-        scope(DefaultSession.SCOPE) {
-            SyncResponseHandler(get(), get(), get(), get(), get())
-        }
-
-        scope(DefaultSession.SCOPE) {
-            DefaultSyncTask(get(), get(), get(), get()) as SyncTask
-        }
-
-        scope(DefaultSession.SCOPE) {
-            SyncTokenStore(get("SessionRealmConfiguration"))
-        }
-
-        scope(DefaultSession.SCOPE) {
-            SyncThread(get(), get(), get(), get(), get())
-        }
-
+    @SessionScope
+    fun providesSyncAPI(retrofit: Retrofit): SyncAPI {
+        return retrofit.create(SyncAPI::class.java)
     }
+
 }
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncResponseHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncResponseHandler.kt
index c6305bd98f..174f2de9c2 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncResponseHandler.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncResponseHandler.kt
@@ -18,15 +18,18 @@ package im.vector.matrix.android.internal.session.sync
 
 import arrow.core.Try
 import im.vector.matrix.android.internal.crypto.CryptoManager
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.session.sync.model.SyncResponse
 import timber.log.Timber
+import javax.inject.Inject
 import kotlin.system.measureTimeMillis
 
-internal class SyncResponseHandler(private val roomSyncHandler: RoomSyncHandler,
-                                   private val userAccountDataSyncHandler: UserAccountDataSyncHandler,
-                                   private val groupSyncHandler: GroupSyncHandler,
-                                   private val cryptoSyncHandler: CryptoSyncHandler,
-                                   private val cryptoManager: CryptoManager) {
+@SessionScope
+internal class SyncResponseHandler @Inject constructor(private val roomSyncHandler: RoomSyncHandler,
+                                                       private val userAccountDataSyncHandler: UserAccountDataSyncHandler,
+                                                       private val groupSyncHandler: GroupSyncHandler,
+                                                       private val cryptoSyncHandler: CryptoSyncHandler,
+                                                       private val cryptoManager: CryptoManager) {
 
     fun handleResponse(syncResponse: SyncResponse, fromToken: String?, isCatchingUp: Boolean): Try {
         return Try {
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTask.kt
index 1f09f4c611..920fb3897c 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTask.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTask.kt
@@ -23,9 +23,11 @@ import im.vector.matrix.android.api.failure.Failure
 import im.vector.matrix.android.api.failure.MatrixError
 import im.vector.matrix.android.internal.auth.SessionParamsStore
 import im.vector.matrix.android.internal.network.executeRequest
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.session.filter.FilterRepository
 import im.vector.matrix.android.internal.session.sync.model.SyncResponse
 import im.vector.matrix.android.internal.task.Task
+import javax.inject.Inject
 
 internal interface SyncTask : Task {
 
@@ -33,10 +35,11 @@ internal interface SyncTask : Task {
 
 }
 
-internal class DefaultSyncTask(private val syncAPI: SyncAPI,
-                               private val filterRepository: FilterRepository,
-                               private val syncResponseHandler: SyncResponseHandler,
-                               private val sessionParamsStore: SessionParamsStore
+@SessionScope
+internal class DefaultSyncTask @Inject constructor(private val syncAPI: SyncAPI,
+                                                   private val filterRepository: FilterRepository,
+                                                   private val syncResponseHandler: SyncResponseHandler,
+                                                   private val sessionParamsStore: SessionParamsStore
 ) : SyncTask {
 
 
@@ -55,7 +58,7 @@ internal class DefaultSyncTask(private val syncAPI: SyncAPI,
         }.recoverWith { throwable ->
             // Intercept 401
             if (throwable is Failure.ServerError
-                    && throwable.error.code == MatrixError.UNKNOWN_TOKEN) {
+                && throwable.error.code == MatrixError.UNKNOWN_TOKEN) {
                 sessionParamsStore.delete()
             }
 
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTokenStore.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTokenStore.kt
index d487417818..d9a00636d1 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTokenStore.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncTokenStore.kt
@@ -17,10 +17,13 @@
 package im.vector.matrix.android.internal.session.sync
 
 import im.vector.matrix.android.internal.database.model.SyncEntity
+import im.vector.matrix.android.internal.session.SessionScope
 import io.realm.Realm
 import io.realm.RealmConfiguration
+import javax.inject.Inject
 
-internal class SyncTokenStore(private val realmConfiguration: RealmConfiguration) {
+@SessionScope
+internal class SyncTokenStore @Inject constructor(private val realmConfiguration: RealmConfiguration) {
 
     fun getLastToken(): String? {
         val realm = Realm.getInstance(realmConfiguration)
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt
index 65a19afc92..db37759909 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt
@@ -20,10 +20,13 @@ import com.zhuinden.monarchy.Monarchy
 import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
 import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields
 import im.vector.matrix.android.internal.database.query.where
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.session.sync.model.UserAccountDataDirectMessages
 import im.vector.matrix.android.internal.session.sync.model.UserAccountDataSync
+import javax.inject.Inject
 
-internal class UserAccountDataSyncHandler(private val monarchy: Monarchy) {
+@SessionScope
+internal class UserAccountDataSyncHandler @Inject constructor(private val monarchy: Monarchy) {
 
     fun handle(accountData: UserAccountDataSync) {
         accountData.list.forEach {
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt
index 7bad5e64d9..db79d39278 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt
@@ -25,6 +25,7 @@ import im.vector.matrix.android.api.failure.MatrixError
 import im.vector.matrix.android.api.session.sync.SyncState
 import im.vector.matrix.android.api.util.Cancelable
 import im.vector.matrix.android.internal.network.NetworkConnectivityChecker
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.session.sync.SyncTask
 import im.vector.matrix.android.internal.session.sync.SyncTokenStore
 import im.vector.matrix.android.internal.session.sync.model.SyncResponse
@@ -35,14 +36,16 @@ import im.vector.matrix.android.internal.util.BackgroundDetectionObserver
 import timber.log.Timber
 import java.net.SocketTimeoutException
 import java.util.concurrent.CountDownLatch
+import javax.inject.Inject
 
 private const val RETRY_WAIT_TIME_MS = 10_000L
 
-internal class SyncThread(private val syncTask: SyncTask,
-                          private val networkConnectivityChecker: NetworkConnectivityChecker,
-                          private val syncTokenStore: SyncTokenStore,
-                          private val backgroundDetectionObserver: BackgroundDetectionObserver,
-                          private val taskExecutor: TaskExecutor
+@SessionScope
+internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
+                                              private val networkConnectivityChecker: NetworkConnectivityChecker,
+                                              private val syncTokenStore: SyncTokenStore,
+                                              private val backgroundDetectionObserver: BackgroundDetectionObserver,
+                                              private val taskExecutor: TaskExecutor
 ) : Thread(), NetworkConnectivityChecker.Listener, BackgroundDetectionObserver.Listener {
 
     private var state: SyncState = SyncState.IDLE
@@ -111,7 +114,7 @@ internal class SyncThread(private val syncTask: SyncTask,
 
                             override fun onFailure(failure: Throwable) {
                                 if (failure is Failure.NetworkConnection
-                                        && failure.cause is SocketTimeoutException) {
+                                    && failure.cause is SocketTimeoutException) {
                                     // Timeout are not critical
                                     Timber.v("Timeout")
                                 } else {
@@ -119,13 +122,13 @@ internal class SyncThread(private val syncTask: SyncTask,
                                 }
 
                                 if (failure !is Failure.NetworkConnection
-                                        || failure.cause is JsonEncodingException) {
+                                    || failure.cause is JsonEncodingException) {
                                     // Wait 10s before retrying
                                     sleep(RETRY_WAIT_TIME_MS)
                                 }
 
                                 if (failure is Failure.ServerError
-                                        && (failure.error.code == MatrixError.UNKNOWN_TOKEN || failure.error.code == MatrixError.MISSING_TOKEN)) {
+                                    && (failure.error.code == MatrixError.UNKNOWN_TOKEN || failure.error.code == MatrixError.MISSING_TOKEN)) {
                                     // No token or invalid token, stop the thread
                                     updateStateTo(SyncState.KILLING)
                                 }
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/DefaultUserService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/DefaultUserService.kt
index e3f68f06ee..4f79a9f159 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/DefaultUserService.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/DefaultUserService.kt
@@ -27,9 +27,12 @@ import im.vector.matrix.android.internal.database.RealmLiveData
 import im.vector.matrix.android.internal.database.mapper.asDomain
 import im.vector.matrix.android.internal.database.model.UserEntity
 import im.vector.matrix.android.internal.database.query.where
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.util.fetchCopied
+import javax.inject.Inject
 
-internal class DefaultUserService(private val monarchy: Monarchy) : UserService {
+@SessionScope
+internal class DefaultUserService @Inject constructor(private val monarchy: Monarchy) : UserService {
 
     override fun getUser(userId: String): User? {
         val userEntity = monarchy.fetchCopied { UserEntity.where(it, userId).findFirst() }
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UpdateUserTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UpdateUserTask.kt
index 3230ecf058..b653e48fe2 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UpdateUserTask.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UpdateUserTask.kt
@@ -23,9 +23,11 @@ import im.vector.matrix.android.internal.database.mapper.asDomain
 import im.vector.matrix.android.internal.database.model.EventEntity
 import im.vector.matrix.android.internal.database.model.UserEntity
 import im.vector.matrix.android.internal.database.query.where
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.session.room.membership.RoomMembers
 import im.vector.matrix.android.internal.task.Task
 import im.vector.matrix.android.internal.util.tryTransactionSync
+import javax.inject.Inject
 
 internal interface UpdateUserTask : Task {
 
@@ -33,7 +35,8 @@ internal interface UpdateUserTask : Task {
 
 }
 
-internal class DefaultUpdateUserTask(private val monarchy: Monarchy) : UpdateUserTask {
+@SessionScope
+internal class DefaultUpdateUserTask @Inject constructor(private val monarchy: Monarchy) : UpdateUserTask {
 
     override suspend fun execute(params: UpdateUserTask.Params): Try {
         return monarchy.tryTransactionSync { realm ->
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityUpdater.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityUpdater.kt
index b09fa9116a..40998d0401 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityUpdater.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/UserEntityUpdater.kt
@@ -24,14 +24,17 @@ import im.vector.matrix.android.internal.database.RealmLiveEntityObserver
 import im.vector.matrix.android.internal.database.model.EventEntity
 import im.vector.matrix.android.internal.database.model.EventEntityFields
 import im.vector.matrix.android.internal.database.query.where
+import im.vector.matrix.android.internal.session.SessionScope
 import im.vector.matrix.android.internal.task.TaskExecutor
 import im.vector.matrix.android.internal.task.TaskThread
 import im.vector.matrix.android.internal.task.configureWith
 import io.realm.Sort
+import javax.inject.Inject
 
-internal class UserEntityUpdater(monarchy: Monarchy,
-                                 private val updateUserTask: UpdateUserTask,
-                                 private val taskExecutor: TaskExecutor)
+@SessionScope
+internal class UserEntityUpdater @Inject constructor(monarchy: Monarchy,
+                                                     private val updateUserTask: UpdateUserTask,
+                                                     private val taskExecutor: TaskExecutor)
     : RealmLiveEntityObserver(monarchy) {
 
     override val query = Monarchy.Query {
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/task/ConfigurableTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/task/ConfigurableTask.kt
index 153bd97c43..02ce07b08d 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/task/ConfigurableTask.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/task/ConfigurableTask.kt
@@ -21,7 +21,7 @@ import im.vector.matrix.android.api.MatrixCallback
 import im.vector.matrix.android.api.util.Cancelable
 
 internal fun  Task.configureWith(params: PARAMS): ConfigurableTask {
-    return ConfigurableTask(this, params)
+    return ConfigurableTask (this, params)
 }
 
 internal data class ConfigurableTask(
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/task/TaskExecutor.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/task/TaskExecutor.kt
index 986fe81bee..228e83baa9 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/task/TaskExecutor.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/task/TaskExecutor.kt
@@ -25,9 +25,12 @@ import kotlinx.coroutines.delay
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 import timber.log.Timber
+import javax.inject.Inject
+import javax.inject.Singleton
 import kotlin.coroutines.EmptyCoroutineContext
 
-internal class TaskExecutor(private val coroutineDispatchers: MatrixCoroutineDispatchers) {
+@Singleton
+internal class TaskExecutor @Inject constructor(private val coroutineDispatchers: MatrixCoroutineDispatchers) {
 
     fun  execute(task: ConfigurableTask): Cancelable {
 
diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/StringProvider.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/StringProvider.kt
index 02d90c2fba..14f2a825f5 100644
--- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/StringProvider.kt
+++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/StringProvider.kt
@@ -19,8 +19,12 @@ package im.vector.matrix.android.internal.util
 import android.content.res.Resources
 import androidx.annotation.NonNull
 import androidx.annotation.StringRes
+import im.vector.matrix.android.internal.di.MatrixScope
+import javax.inject.Inject
+import javax.inject.Singleton
 
-internal class StringProvider(private val resources: Resources) {
+@MatrixScope
+internal class StringProvider @Inject constructor(private val resources: Resources) {
 
     /**
      * Returns a localized string from the application's package's
diff --git a/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragment.kt
index 69bc82fc29..e30f9bb71e 100755
--- a/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragment.kt
+++ b/vector/src/main/java/im/vector/riotredesign/features/settings/VectorSettingsPreferencesFragment.kt
@@ -710,7 +710,7 @@ class VectorSettingsPreferencesFragment : VectorPreferenceFragment(), SharedPref
                 /* TODO
                 displayLoadingView()
 
-                val task = ClearMediaCacheAsyncTask(
+                val task = ClearMediaCacheAsyncTask @Inject constructor(
                         backgroundTask = {
                             session.mediaCache.clear()
                             activity?.let { it -> Glide.get(it).clearDiskCache() }