diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/crypto/CryptoService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/crypto/CryptoService.kt index 90f524748c..a3ece1ab1c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/crypto/CryptoService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/crypto/CryptoService.kt @@ -85,6 +85,8 @@ interface CryptoService { fun addRoomKeysRequestListener(listener: RoomKeysRequestListener) + fun removeRoomKeysRequestListener(listener: RoomKeysRequestListener) + fun getDevicesList(callback: MatrixCallback) fun inboundGroupSessionsCount(onlyBackedUp: Boolean): Int 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 d666bab3d2..b99b2e5a55 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 @@ -1023,7 +1023,7 @@ internal class CryptoManager @Inject constructor( * * @param listener listener */ - fun removeRoomKeysRequestListener(listener: RoomKeysRequestListener) { + override fun removeRoomKeysRequestListener(listener: RoomKeysRequestListener) { incomingRoomKeyRequestManager.removeRoomKeysRequestListener(listener) } diff --git a/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt b/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt index 517284d571..5f29a76f10 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt @@ -17,24 +17,31 @@ package im.vector.riotx.core.di import im.vector.matrix.android.api.auth.Authenticator -import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.api.session.Session -import java.lang.IllegalStateException +import im.vector.riotx.features.crypto.keysrequest.KeyRequestHandler +import im.vector.riotx.features.crypto.verification.IncomingVerificationRequestHandler import java.util.concurrent.atomic.AtomicReference import javax.inject.Inject import javax.inject.Singleton @Singleton -class ActiveSessionHolder @Inject constructor(private val authenticator: Authenticator) { +class ActiveSessionHolder @Inject constructor(private val authenticator: Authenticator, + private val keyRequestHandler: KeyRequestHandler, + private val incomingVerificationRequestHandler: IncomingVerificationRequestHandler +) { private var activeSession: AtomicReference = AtomicReference() - fun setActiveSession(session: Session){ + fun setActiveSession(session: Session) { activeSession.set(session) + keyRequestHandler.start(session) + incomingVerificationRequestHandler.start(session) } - fun clearActiveSession(){ + fun clearActiveSession() { activeSession.set(null) + keyRequestHandler.stop() + incomingVerificationRequestHandler.stop() } fun hasActiveSession(): Boolean { @@ -46,13 +53,14 @@ class ActiveSessionHolder @Inject constructor(private val authenticator: Authent } fun getActiveSession(): Session { - return activeSession.get() ?: throw IllegalStateException("You should authenticate before using this") + return activeSession.get() + ?: throw IllegalStateException("You should authenticate before using this") } //TODO: Stop sync ? - fun switchToSession(sessionParams: SessionParams) { - val newActiveSession = authenticator.getSession(sessionParams) - activeSession.set(newActiveSession) - } +// fun switchToSession(sessionParams: SessionParams) { +// val newActiveSession = authenticator.getSession(sessionParams) +// activeSession.set(newActiveSession) +// } } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotx/features/crypto/keysrequest/KeyRequestHandler.kt b/vector/src/main/java/im/vector/riotx/features/crypto/keysrequest/KeyRequestHandler.kt index 1bbd8b38bf..88f78244ee 100644 --- a/vector/src/main/java/im/vector/riotx/features/crypto/keysrequest/KeyRequestHandler.kt +++ b/vector/src/main/java/im/vector/riotx/features/crypto/keysrequest/KeyRequestHandler.kt @@ -54,18 +54,27 @@ import kotlin.collections.HashMap */ @Singleton -class KeyRequestHandler @Inject constructor(val context: Context, - val session: Session) +class KeyRequestHandler @Inject constructor(val context: Context) : RoomKeysRequestListener, - SasVerificationService.SasVerificationListener { + SasVerificationService.SasVerificationListener { private val alertsToRequests = HashMap>() - init { + var session: Session? = null + + + fun start(session: Session) { + this.session = session session.getSasVerificationService().addListener(this) session.addRoomKeysRequestListener(this) } + fun stop() { + session?.getSasVerificationService()?.removeListener(this) + session?.removeRoomKeysRequestListener(this) + session = null + } + /** * Handle incoming key request. * @@ -92,7 +101,7 @@ class KeyRequestHandler @Inject constructor(val context: Context, alertsToRequests[mappingKey] = ArrayList().apply { this.add(request) } //Add a notification for every incoming request - session.downloadKeys(Arrays.asList(userId), false, object : MatrixCallback> { + session?.downloadKeys(Arrays.asList(userId), false, object : MatrixCallback> { override fun onSuccess(data: MXUsersDevicesMap) { val deviceInfo = data.getObject(deviceId, userId) @@ -103,12 +112,12 @@ class KeyRequestHandler @Inject constructor(val context: Context, } if (deviceInfo.isUnknown) { - session.setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_UNVERIFIED, deviceId, userId) + session?.setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_UNVERIFIED, deviceId, userId) deviceInfo.verified = MXDeviceInfo.DEVICE_VERIFICATION_UNVERIFIED //can we get more info on this device? - session.getDevicesList(object : MatrixCallback { + session?.getDevicesList(object : MatrixCallback { override fun onSuccess(data: DevicesListResponse) { data.devices?.find { it.deviceId == deviceId }?.let { postAlert(context, userId, deviceId, true, deviceInfo, it) @@ -195,8 +204,8 @@ class KeyRequestHandler @Inject constructor(val context: Context, Runnable { alert.weakCurrentActivity?.get()?.let { val intent = SASVerificationActivity.outgoingIntent(it, - session.sessionParams.credentials.userId, - userId, deviceId) + session?.sessionParams?.credentials?.userId ?: "", + userId, deviceId) it.startActivity(intent) } }, @@ -247,8 +256,8 @@ class KeyRequestHandler @Inject constructor(val context: Context, val alertMgrUniqueKey = alertManagerId(deviceId!!, userId!!) alertsToRequests[alertMgrUniqueKey]?.removeAll { it.deviceId == request.deviceId - && it.userId == request.userId - && it.requestId == request.requestId + && it.userId == request.userId + && it.requestId == request.requestId } if (alertsToRequests[alertMgrUniqueKey]?.isEmpty() == true) { PopupAlertManager.cancelAlert(alertMgrUniqueKey) diff --git a/vector/src/main/java/im/vector/riotx/features/crypto/verification/IncomingVerificationRequestHandler.kt b/vector/src/main/java/im/vector/riotx/features/crypto/verification/IncomingVerificationRequestHandler.kt index 7f7c4741a2..e138eb9ea4 100644 --- a/vector/src/main/java/im/vector/riotx/features/crypto/verification/IncomingVerificationRequestHandler.kt +++ b/vector/src/main/java/im/vector/riotx/features/crypto/verification/IncomingVerificationRequestHandler.kt @@ -16,11 +16,11 @@ package im.vector.riotx.features.crypto.verification import android.content.Context +import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.crypto.sas.SasVerificationService import im.vector.matrix.android.api.session.crypto.sas.SasVerificationTransaction import im.vector.matrix.android.api.session.crypto.sas.SasVerificationTxState import im.vector.riotx.R -import im.vector.riotx.core.di.ActiveSessionHolder import im.vector.riotx.features.popup.PopupAlertManager import javax.inject.Inject import javax.inject.Singleton @@ -29,12 +29,18 @@ import javax.inject.Singleton * Listens to the VerificationManager and add a new notification when an incoming request is detected. */ @Singleton -class IncomingVerificationRequestHandler @Inject constructor(val context: Context, - private val activeSessionHolder: ActiveSessionHolder -) : SasVerificationService.SasVerificationListener { +class IncomingVerificationRequestHandler @Inject constructor(val context: Context) : SasVerificationService.SasVerificationListener { - init { - activeSessionHolder.getActiveSession().getSasVerificationService().addListener(this) + private var session: Session? = null + + fun start(session: Session) { + this.session = session + session.getSasVerificationService().addListener(this) + } + + fun stop() { + session?.getSasVerificationService()?.removeListener(this) + this.session = null } override fun transactionCreated(tx: SasVerificationTransaction) {} @@ -43,9 +49,8 @@ class IncomingVerificationRequestHandler @Inject constructor(val context: Contex when (tx.state) { SasVerificationTxState.OnStarted -> { //Add a notification for every incoming request - val session = activeSessionHolder.getActiveSession() - val name = session.getUser(tx.otherUserId)?.displayName - ?: tx.otherUserId + val name = session?.getUser(tx.otherUserId)?.displayName + ?: tx.otherUserId val alert = PopupAlertManager.VectorAlert( "kvr_${tx.transactionId}", @@ -55,9 +60,9 @@ class IncomingVerificationRequestHandler @Inject constructor(val context: Contex .apply { contentAction = Runnable { val intent = SASVerificationActivity.incomingIntent(context, - session.sessionParams.credentials.userId, - tx.otherUserId, - tx.transactionId) + session?.sessionParams?.credentials?.userId ?: "", + tx.otherUserId, + tx.transactionId) weakCurrentActivity?.get()?.startActivity(intent) } dismissedAction = Runnable { @@ -73,9 +78,9 @@ class IncomingVerificationRequestHandler @Inject constructor(val context: Contex context.getString(R.string.action_open), Runnable { val intent = SASVerificationActivity.incomingIntent(context, - session.sessionParams.credentials.userId, - tx.otherUserId, - tx.transactionId) + session?.sessionParams?.credentials?.userId ?: "", + tx.otherUserId, + tx.transactionId) weakCurrentActivity?.get()?.startActivity(intent) } ) diff --git a/vector/src/main/java/im/vector/riotx/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/riotx/features/home/HomeActivity.kt index 11d1248c74..21ca495ab9 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/HomeActivity.kt @@ -39,8 +39,6 @@ import im.vector.riotx.core.platform.OnBackPressed import im.vector.riotx.core.platform.ToolbarConfigurable import im.vector.riotx.core.platform.VectorBaseActivity import im.vector.riotx.core.pushers.PushersManager -import im.vector.riotx.features.crypto.keysrequest.KeyRequestHandler -import im.vector.riotx.features.crypto.verification.IncomingVerificationRequestHandler import im.vector.riotx.features.disclaimer.showDisclaimerDialog import im.vector.riotx.features.notifications.NotificationDrawerManager import im.vector.riotx.features.rageshake.VectorUncaughtExceptionHandler @@ -64,10 +62,6 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { @Inject lateinit var homeActivityViewModelFactory: HomeActivityViewModel.Factory @Inject lateinit var homeNavigator: HomeNavigator @Inject lateinit var vectorUncaughtExceptionHandler: VectorUncaughtExceptionHandler - // TODO Move this elsewhere - @Inject lateinit var incomingVerificationRequestHandler: IncomingVerificationRequestHandler - // TODO Move this elsewhere - @Inject lateinit var keyRequestHandler: KeyRequestHandler @Inject lateinit var pushManager: PushersManager @Inject lateinit var notificationDrawerManager: NotificationDrawerManager