diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DefaultCryptoService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DefaultCryptoService.kt index 61a072ece6..e37af45a73 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DefaultCryptoService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DefaultCryptoService.kt @@ -122,7 +122,8 @@ internal class DefaultCryptoService @Inject constructor( private val myDeviceInfoHolder: Lazy, // the crypto store private val cryptoStore: IMXCryptoStore, - + // Room encryptors store + private val roomEncryptorsStore: RoomEncryptorsStore, // Olm device private val olmDevice: MXOlmDevice, // Set of parameters used to configure/customize the end-to-end crypto. @@ -172,8 +173,6 @@ internal class DefaultCryptoService @Inject constructor( private val uiHandler = Handler(Looper.getMainLooper()) - // MXEncrypting instance for each room. - private val roomEncryptors: MutableMap = HashMap() private val isStarting = AtomicBoolean(false) private val isStarted = AtomicBoolean(false) @@ -512,9 +511,7 @@ internal class DefaultCryptoService @Inject constructor( else -> olmEncryptionFactory.create(roomId) } - synchronized(roomEncryptors) { - roomEncryptors.put(roomId, alg) - } + roomEncryptorsStore.put(roomId, alg) // if encryption was not previously enabled in this room, we will have been // ignoring new device events for these users so far. We may well have @@ -596,16 +593,12 @@ internal class DefaultCryptoService @Inject constructor( internalStart(false) } val userIds = getRoomUserIds(roomId) - var alg = synchronized(roomEncryptors) { - roomEncryptors[roomId] - } + var alg = roomEncryptorsStore.get(roomId) if (alg == null) { val algorithm = getEncryptionAlgorithm(roomId) if (algorithm != null) { if (setEncryptionInRoom(roomId, algorithm, false, userIds)) { - synchronized(roomEncryptors) { - alg = roomEncryptors[roomId] - } + alg = roomEncryptorsStore.get(roomId) } } } @@ -836,16 +829,8 @@ internal class DefaultCryptoService @Inject constructor( * @param event the membership event causing the change */ private fun onRoomMembershipEvent(roomId: String, event: Event) { - val alg: IMXEncrypting? + roomEncryptorsStore.get(roomId) ?: /* No encrypting in this room */ return - synchronized(roomEncryptors) { - alg = roomEncryptors[roomId] - } - - if (null == alg) { - // No encrypting in this room - return - } event.stateKey?.let { userId -> val roomMember: RoomMemberSummary? = event.content.toModel() val membership = roomMember?.membership diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/RoomEncryptorsStore.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/RoomEncryptorsStore.kt new file mode 100644 index 0000000000..3b74e62167 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/RoomEncryptorsStore.kt @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020 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.crypto + +import im.vector.matrix.android.internal.crypto.algorithms.IMXEncrypting +import javax.inject.Inject + +internal class RoomEncryptorsStore @Inject constructor() { + + // MXEncrypting instance for each room. + private val roomEncryptors = mutableMapOf() + + fun put(roomId: String, alg: IMXEncrypting) { + synchronized(roomEncryptors) { + roomEncryptors.put(roomId, alg) + } + } + + fun get(roomId: String): IMXEncrypting? { + return synchronized(roomEncryptors) { + roomEncryptors[roomId] + } + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt index a2d21c4f89..0ce2269bde 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/algorithms/megolm/MXMegolmEncryption.kt @@ -40,7 +40,7 @@ import timber.log.Timber internal class MXMegolmEncryption( // The id of the room we will be sending to. - private var roomId: String, + private val roomId: String, private val olmDevice: MXOlmDevice, private val defaultKeysBackupService: DefaultKeysBackupService, private val cryptoStore: IMXCryptoStore,