From d80c15f52fe4b769d73a209ec7499ce7bd1b4907 Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 6 Feb 2020 10:31:21 +0100 Subject: [PATCH] Dispatch init on crypto thread to avoid blocking cold start --- .../internal/crypto/DeviceListManager.kt | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) 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 faf9e7e3c5..cd46cdbe36 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 @@ -27,6 +27,9 @@ 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 im.vector.matrix.android.internal.task.TaskExecutor +import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers +import kotlinx.coroutines.launch import timber.log.Timber import javax.inject.Inject @@ -36,7 +39,9 @@ internal class DeviceListManager @Inject constructor(private val cryptoStore: IM private val olmDevice: MXOlmDevice, private val syncTokenStore: SyncTokenStore, private val credentials: Credentials, - private val downloadKeysForUsersTask: DownloadKeysForUsersTask) { + private val downloadKeysForUsersTask: DownloadKeysForUsersTask, + coroutineDispatchers: MatrixCoroutineDispatchers, + taskExecutor: TaskExecutor) { interface UserDevicesUpdateListener { fun onUsersDeviceUpdate(users: List) @@ -72,17 +77,19 @@ internal class DeviceListManager @Inject constructor(private val cryptoStore: IM private val notReadyToRetryHS = mutableSetOf() init { - var isUpdated = false - val deviceTrackingStatuses = cryptoStore.getDeviceTrackingStatuses().toMutableMap() - for ((userId, status) in deviceTrackingStatuses) { - if (TRACKING_STATUS_DOWNLOAD_IN_PROGRESS == status || TRACKING_STATUS_UNREACHABLE_SERVER == status) { - // if a download was in progress when we got shut down, it isn't any more. - deviceTrackingStatuses[userId] = TRACKING_STATUS_PENDING_DOWNLOAD - isUpdated = true + taskExecutor.executorScope.launch(coroutineDispatchers.crypto) { + var isUpdated = false + val deviceTrackingStatuses = cryptoStore.getDeviceTrackingStatuses().toMutableMap() + for ((userId, status) in deviceTrackingStatuses) { + if (TRACKING_STATUS_DOWNLOAD_IN_PROGRESS == status || TRACKING_STATUS_UNREACHABLE_SERVER == status) { + // if a download was in progress when we got shut down, it isn't any more. + deviceTrackingStatuses[userId] = TRACKING_STATUS_PENDING_DOWNLOAD + isUpdated = true + } + } + if (isUpdated) { + cryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) } - } - if (isUpdated) { - cryptoStore.saveDeviceTrackingStatuses(deviceTrackingStatuses) } }