diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt index 7af7451901..894eb04a11 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt @@ -410,15 +410,19 @@ internal class DefaultCryptoService @Inject constructor( } override fun getCryptoDeviceInfo(userId: String): List { - return cryptoStore.getUserDeviceList(userId).orEmpty() + return runBlocking { + olmMachine!!.getUserDevices(userId) + } } override fun getLiveCryptoDeviceInfo(userId: String): LiveData> { - return cryptoStore.getLiveDeviceList(userId) + return getLiveCryptoDeviceInfo(listOf(userId)) } override fun getLiveCryptoDeviceInfo(userIds: List): LiveData> { - return cryptoStore.getLiveDeviceList(userIds) + return runBlocking { + olmMachine!!.getLiveDevices(userIds) + } } /** diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt index 76f47ecc4f..973b8977d5 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt @@ -16,6 +16,8 @@ package org.matrix.android.sdk.internal +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.LiveData import java.io.File import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -61,6 +63,26 @@ private class CryptoProgressListener(listener: ProgressListener?) : RustProgress } } +internal class LiveDevice( + userIds: List, + machine: OlmMachine +) : MutableLiveData>() { + var userIds: List = userIds + private var machine: OlmMachine = machine + + private val listener = { devices: List -> + value = devices + } + + override fun onActive() { + machine.addDeviceUpdateListener(this) + } + + override fun onInactive() { + machine.removeDeviceUpdateListener(this) + } +} + fun setRustLogger() { setLogger(CryptoLogger() as Logger) } @@ -108,6 +130,7 @@ class Device(inner: InnerDevice, machine: InnerMachine) { internal class OlmMachine(user_id: String, device_id: String, path: File) { private val inner: InnerMachine = InnerMachine(user_id, device_id, path.toString()) + private val deviceUpdateListeners = HashMap>() fun userId(): String { return this.inner.userId() @@ -136,6 +159,21 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) { ) } + fun addDeviceUpdateListener(device: LiveDevice) { + deviceUpdateListeners.set(device, device.userIds) + } + + fun removeDeviceUpdateListener(device: LiveDevice) { + deviceUpdateListeners.remove(device) + } + + suspend fun updateLiveDevices() { + for ((liveDevice, users) in deviceUpdateListeners) { + val devices = getUserDevices(users) + liveDevice.postValue(devices) + } + } + suspend fun outgoingRequests(): List = withContext(Dispatchers.IO) { inner.outgoingRequests() } @@ -183,6 +221,10 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) { response_body: String ) = withContext(Dispatchers.IO) { inner.markRequestAsSent(request_id, request_type, response_body) + + if (request_type == RequestType.KEYS_QUERY) { + updateLiveDevices() + } } suspend fun getDevice(user_id: String, device_id: String): Device? = withContext(Dispatchers.IO) { @@ -192,6 +234,29 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) { } } + suspend fun getUserDevices(userId: String): List { + return inner.getUserDevices(userId).map { Device(it, inner).toCryptoDeviceInfo() } + } + + suspend fun getUserDevices(userIds: List): List { + val plainDevices: ArrayList = arrayListOf() + + for (user in userIds) { + val devices = getUserDevices(user) + plainDevices.addAll(devices) + } + + return plainDevices + } + + suspend fun getLiveDevices(userIds: List): LiveData> { + val plainDevices = getUserDevices(userIds) + val devices = LiveDevice(userIds, this) + devices.setValue(plainDevices) + + return devices + } + @Throws(CryptoStoreErrorException::class) suspend fun exportKeys(passphrase: String, rounds: Int): ByteArray = withContext(Dispatchers.IO) { inner.exportKeys(passphrase, rounds).toByteArray()