From b870a8b791c040afdca121cb16e6487dad74a08d Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 9 Feb 2021 10:30:11 +0100 Subject: [PATCH] Create ProgressReporter interface --- .../DefaultInitialSyncProgressService.kt | 28 +++++++++++++------ .../session/sync/CryptoSyncHandler.kt | 8 +++--- .../internal/session/sync/GroupSyncHandler.kt | 14 ++++------ .../internal/session/sync/RoomSyncHandler.kt | 16 +++++------ .../session/sync/SyncResponseHandler.kt | 6 ++-- 5 files changed, 41 insertions(+), 31 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultInitialSyncProgressService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultInitialSyncProgressService.kt index fb7ba7c58f..e74d905436 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultInitialSyncProgressService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultInitialSyncProgressService.kt @@ -22,8 +22,20 @@ import org.matrix.android.sdk.api.session.InitialSyncProgressService import timber.log.Timber import javax.inject.Inject +internal interface ProgressReporter { + fun startTask(@StringRes nameRes: Int, + totalProgress: Int, + parentWeight: Float) + + fun reportProgress(progress: Float) + + fun endTask() +} + @SessionScope -internal class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgressService { +internal class DefaultInitialSyncProgressService @Inject constructor() + : InitialSyncProgressService, + ProgressReporter { private val status = MutableLiveData() @@ -33,9 +45,9 @@ internal class DefaultInitialSyncProgressService @Inject constructor() : Initial return status } - fun startTask(@StringRes nameRes: Int, - totalProgress: Int, - parentWeight: Float) { + override fun startTask(@StringRes nameRes: Int, + totalProgress: Int, + parentWeight: Float) { // Create a rootTask, or add a child to the leaf if (rootTask == null) { rootTask = TaskInfo(nameRes, totalProgress, null, 1F) @@ -51,7 +63,7 @@ internal class DefaultInitialSyncProgressService @Inject constructor() : Initial reportProgress(0F) } - fun reportProgress(progress: Float) { + override fun reportProgress(progress: Float) { rootTask?.let { root -> root.leaf().let { leaf -> // Update the progress of the leaf and all its parents @@ -62,7 +74,7 @@ internal class DefaultInitialSyncProgressService @Inject constructor() : Initial } } - fun endTask() { + override fun endTask() { rootTask?.leaf()?.let { endedTask -> // Ensure the task progress is complete reportProgress(endedTask.totalProgress.toFloat()) @@ -117,7 +129,7 @@ private class TaskInfo(@StringRes val nameRes: Int, } } -internal inline fun reportSubtask(reporter: DefaultInitialSyncProgressService?, +internal inline fun reportSubtask(reporter: ProgressReporter?, @StringRes nameRes: Int, totalProgress: Int, parentWeight: Float, @@ -128,7 +140,7 @@ internal inline fun reportSubtask(reporter: DefaultInitialSyncProgressServic } } -internal inline fun Map.mapWithProgress(reporter: DefaultInitialSyncProgressService?, +internal inline fun Map.mapWithProgress(reporter: ProgressReporter?, @StringRes nameRes: Int, parentWeight: Float, transform: (Map.Entry) -> R): List { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/CryptoSyncHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/CryptoSyncHandler.kt index 42477e1dd8..a244e7a273 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/CryptoSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/CryptoSyncHandler.kt @@ -26,7 +26,7 @@ import org.matrix.android.sdk.internal.crypto.MXEventDecryptionResult import org.matrix.android.sdk.internal.crypto.algorithms.olm.OlmDecryptionResult import org.matrix.android.sdk.internal.crypto.model.event.OlmEventContent import org.matrix.android.sdk.internal.crypto.verification.DefaultVerificationService -import org.matrix.android.sdk.internal.session.DefaultInitialSyncProgressService +import org.matrix.android.sdk.internal.session.ProgressReporter import org.matrix.android.sdk.internal.session.sync.model.SyncResponse import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse import timber.log.Timber @@ -35,10 +35,10 @@ import javax.inject.Inject internal class CryptoSyncHandler @Inject constructor(private val cryptoService: DefaultCryptoService, private val verificationService: DefaultVerificationService) { - fun handleToDevice(toDevice: ToDeviceSyncResponse, initialSyncProgressService: DefaultInitialSyncProgressService? = null) { + fun handleToDevice(toDevice: ToDeviceSyncResponse, progressReporter: ProgressReporter? = null) { val total = toDevice.events?.size ?: 0 toDevice.events?.forEachIndexed { index, event -> - initialSyncProgressService?.reportProgress(index * 100F / total) + progressReporter?.reportProgress(index * 100F / total) // Decrypt event if necessary Timber.i("## CRYPTO | To device event from ${event.senderId} of type:${event.type}") decryptToDeviceEvent(event, null) @@ -75,7 +75,7 @@ internal class CryptoSyncHandler @Inject constructor(private val cryptoService: // try to find device id to ease log reading val deviceId = cryptoService.getCryptoDeviceInfo(event.senderId!!).firstOrNull { it.identityKey() == senderKey - }?.deviceId ?: senderKey + }?.deviceId ?: senderKey Timber.e("## CRYPTO | Failed to decrypt to device event from ${event.senderId}|$deviceId reason:<${event.mCryptoError ?: exception}>") } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/GroupSyncHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/GroupSyncHandler.kt index 135f711a6c..03ad76e19a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/GroupSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/GroupSyncHandler.kt @@ -16,17 +16,17 @@ package org.matrix.android.sdk.internal.session.sync +import io.realm.Realm import org.matrix.android.sdk.R import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.internal.database.model.GroupEntity import org.matrix.android.sdk.internal.database.model.GroupSummaryEntity import org.matrix.android.sdk.internal.database.query.getOrCreate import org.matrix.android.sdk.internal.database.query.where -import org.matrix.android.sdk.internal.session.DefaultInitialSyncProgressService +import org.matrix.android.sdk.internal.session.ProgressReporter import org.matrix.android.sdk.internal.session.mapWithProgress import org.matrix.android.sdk.internal.session.sync.model.GroupsSyncResponse import org.matrix.android.sdk.internal.session.sync.model.InvitedGroupSync -import io.realm.Realm import javax.inject.Inject internal class GroupSyncHandler @Inject constructor() { @@ -37,11 +37,9 @@ internal class GroupSyncHandler @Inject constructor() { data class LEFT(val data: Map) : HandlingStrategy() } - fun handle( - realm: Realm, - roomsSyncResponse: GroupsSyncResponse, - reporter: DefaultInitialSyncProgressService? = null - ) { + fun handle(realm: Realm, + roomsSyncResponse: GroupsSyncResponse, + reporter: ProgressReporter? = null) { handleGroupSync(realm, HandlingStrategy.JOINED(roomsSyncResponse.join), reporter) handleGroupSync(realm, HandlingStrategy.INVITED(roomsSyncResponse.invite), reporter) handleGroupSync(realm, HandlingStrategy.LEFT(roomsSyncResponse.leave), reporter) @@ -49,7 +47,7 @@ internal class GroupSyncHandler @Inject constructor() { // PRIVATE METHODS ***************************************************************************** - private fun handleGroupSync(realm: Realm, handlingStrategy: HandlingStrategy, reporter: DefaultInitialSyncProgressService?) { + private fun handleGroupSync(realm: Realm, handlingStrategy: HandlingStrategy, reporter: ProgressReporter?) { val groups = when (handlingStrategy) { is HandlingStrategy.JOINED -> handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_groups, 0.6f) { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt index ef2212cdab..7d8966579e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/RoomSyncHandler.kt @@ -49,7 +49,7 @@ import org.matrix.android.sdk.internal.database.query.where import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.di.UserId import org.matrix.android.sdk.internal.extensions.clearWith -import org.matrix.android.sdk.internal.session.DefaultInitialSyncProgressService +import org.matrix.android.sdk.internal.session.ProgressReporter import org.matrix.android.sdk.internal.session.mapWithProgress import org.matrix.android.sdk.internal.session.reportSubtask import org.matrix.android.sdk.internal.session.room.membership.RoomChangeMembershipStateDataSource @@ -86,12 +86,10 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle data class LEFT(val data: Map) : HandlingStrategy() } - fun handle( - realm: Realm, - roomsSyncResponse: RoomsSyncResponse, - isInitialSync: Boolean, - reporter: DefaultInitialSyncProgressService? = null - ) { + fun handle(realm: Realm, + roomsSyncResponse: RoomsSyncResponse, + isInitialSync: Boolean, + reporter: ProgressReporter? = null) { Timber.v("Execute transaction from $this") handleRoomSync(realm, HandlingStrategy.JOINED(roomsSyncResponse.join), isInitialSync, reporter) handleRoomSync(realm, HandlingStrategy.INVITED(roomsSyncResponse.invite), isInitialSync, reporter) @@ -100,7 +98,7 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle // PRIVATE METHODS ***************************************************************************** - private fun handleRoomSync(realm: Realm, handlingStrategy: HandlingStrategy, isInitialSync: Boolean, reporter: DefaultInitialSyncProgressService?) { + private fun handleRoomSync(realm: Realm, handlingStrategy: HandlingStrategy, isInitialSync: Boolean, reporter: ProgressReporter?) { val insertType = if (isInitialSync) { EventInsertType.INITIAL_SYNC } else { @@ -137,7 +135,7 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle handlingStrategy: HandlingStrategy.JOINED, insertType: EventInsertType, syncLocalTimeStampMillis: Long, - reporter: DefaultInitialSyncProgressService?) { + reporter: ProgressReporter?) { val maxSize = (initialSyncStrategy as? InitialSyncStrategy.Optimized)?.maxRoomsToInsert ?: Int.MAX_VALUE val listSize = handlingStrategy.data.keys.size val numberOfChunks = ceil(listSize / maxSize.toDouble()).toInt() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/SyncResponseHandler.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/SyncResponseHandler.kt index c9376eb9f2..a80b733dcc 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/SyncResponseHandler.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/sync/SyncResponseHandler.kt @@ -25,7 +25,7 @@ import org.matrix.android.sdk.internal.crypto.DefaultCryptoService import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.di.SessionId import org.matrix.android.sdk.internal.di.WorkManagerProvider -import org.matrix.android.sdk.internal.session.DefaultInitialSyncProgressService +import org.matrix.android.sdk.internal.session.ProgressReporter import org.matrix.android.sdk.internal.session.group.GetGroupDataWorker import org.matrix.android.sdk.internal.session.notification.ProcessEventForPushTask import org.matrix.android.sdk.internal.session.reportSubtask @@ -53,7 +53,9 @@ internal class SyncResponseHandler @Inject constructor(@SessionDatabase private private val processEventForPushTask: ProcessEventForPushTask, private val pushRuleService: PushRuleService) { - suspend fun handleResponse(syncResponse: SyncResponse, fromToken: String?, reporter: DefaultInitialSyncProgressService?) { + suspend fun handleResponse(syncResponse: SyncResponse, + fromToken: String?, + reporter: ProgressReporter?) { val isInitialSync = fromToken == null Timber.v("Start handling sync, is InitialSync: $isInitialSync")