From ed39b02924748201bc92ad92de61985a5c151b00 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 5 Sep 2019 15:36:20 +0200 Subject: [PATCH 1/6] Avoid using keyword for variable names --- .../api/pushrules/RoomMemberCountCondition.kt | 6 +++--- .../vector/riotx/features/rageshake/BugReporter.kt | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RoomMemberCountCondition.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RoomMemberCountCondition.kt index 1c02d63d..d0b76fba 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RoomMemberCountCondition.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RoomMemberCountCondition.kt @@ -21,14 +21,14 @@ import timber.log.Timber private val regex = Regex("^(==|<=|>=|<|>)?(\\d*)$") -class RoomMemberCountCondition(val `is`: String) : Condition(Kind.room_member_count) { +class RoomMemberCountCondition(val iz: String) : Condition(Kind.room_member_count) { override fun isSatisfied(conditionResolver: ConditionResolver): Boolean { return conditionResolver.resolveRoomMemberCountCondition(this) } override fun technicalDescription(): String { - return "Room member count is $`is`" + return "Room member count is $iz" } fun isSatisfied(event: Event, session: RoomService?): Boolean { @@ -55,7 +55,7 @@ class RoomMemberCountCondition(val `is`: String) : Condition(Kind.room_member_co */ private fun parseIsField(): Pair? { try { - val match = regex.find(`is`) ?: return null + val match = regex.find(iz) ?: return null val (prefix, count) = match.destructured return prefix to count.toInt() } catch (t: Throwable) { diff --git a/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt b/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt index c678f47d..39a57b87 100755 --- a/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt +++ b/vector/src/main/java/im/vector/riotx/features/rageshake/BugReporter.kt @@ -349,20 +349,20 @@ class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSes } else if (null == response || null == response.body()) { serverError = "Failed with error $responseCode" } else { - var `is`: InputStream? = null + var inputStream: InputStream? = null try { - `is` = response.body()!!.byteStream() + inputStream = response.body()!!.byteStream() - if (null != `is`) { - var ch = `is`.read() + if (null != inputStream) { + var ch = inputStream.read() val b = StringBuilder() while (ch != -1) { b.append(ch.toChar()) - ch = `is`.read() + ch = inputStream.read() } serverError = b.toString() - `is`.close() + inputStream.close() // check if the error message try { @@ -381,7 +381,7 @@ class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSes Timber.e(e, "## sendBugReport() : failed to parse error " + e.message) } finally { try { - `is`?.close() + inputStream?.close() } catch (e: Exception) { Timber.e(e, "## sendBugReport() : failed to close the error stream " + e.message) } From a23be05cbf2f9fea52470f843f1ab4e0d57bceeb Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 5 Sep 2019 15:38:14 +0200 Subject: [PATCH 2/6] Better type --- .../android/internal/crypto/keysbackup/KeysBackup.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackup.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackup.kt index edd2e190..786923da 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackup.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/keysbackup/KeysBackup.kt @@ -67,7 +67,6 @@ import org.matrix.olm.OlmPkMessage import timber.log.Timber import java.security.InvalidParameterException import javax.inject.Inject -import kotlin.collections.HashMap import kotlin.random.Random /** @@ -846,7 +845,7 @@ internal class KeysBackup @Inject constructor( // Wait between 0 and 10 seconds, to avoid backup requests from // different clients hitting the server all at the same time when a // new key is sent - val delayInMs = Random.nextInt(KEY_BACKUP_WAITING_TIME_TO_SEND_KEY_BACKUP_MILLIS).toLong() + val delayInMs = Random.nextLong(KEY_BACKUP_WAITING_TIME_TO_SEND_KEY_BACKUP_MILLIS) uiHandler.postDelayed({ backupKeys() }, delayInMs) } @@ -1305,7 +1304,7 @@ internal class KeysBackup @Inject constructor( // Make the request storeSessionDataTask - .configureWith(StoreSessionsDataTask.Params(keysBackupVersion!!.version!!, keysBackupData)){ + .configureWith(StoreSessionsDataTask.Params(keysBackupVersion!!.version!!, keysBackupData)) { this.callback = sendingRequestCallback } .executeBy(taskExecutor) @@ -1403,7 +1402,7 @@ internal class KeysBackup @Inject constructor( companion object { // Maximum delay in ms in {@link maybeBackupKeys} - private const val KEY_BACKUP_WAITING_TIME_TO_SEND_KEY_BACKUP_MILLIS = 10000 + private const val KEY_BACKUP_WAITING_TIME_TO_SEND_KEY_BACKUP_MILLIS = 10_000L // Maximum number of keys to send at a time to the homeserver. private const val KEY_BACKUP_SEND_KEYS_MAX_COUNT = 100 From 242e60fcaaf0b1c9343987090369da5bfb6e1eb4 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 5 Sep 2019 16:14:34 +0200 Subject: [PATCH 3/6] Rename CryptoManager to DefaultCryptoService --- .../matrix/android/internal/crypto/CryptoModule.kt | 2 +- .../{CryptoManager.kt => DefaultCryptoService.kt} | 4 ++-- .../android/internal/session/DefaultSession.kt | 4 ++-- .../internal/session/sync/CryptoSyncHandler.kt | 10 +++++----- .../android/internal/session/sync/RoomSyncHandler.kt | 8 ++++---- .../internal/session/sync/SyncResponseHandler.kt | 12 ++++++------ 6 files changed, 20 insertions(+), 20 deletions(-) rename matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/{CryptoManager.kt => DefaultCryptoService.kt} (99%) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt index 5ed0c0e2..4272dbd3 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoModule.kt @@ -105,7 +105,7 @@ internal abstract class CryptoModule { } @Binds - abstract fun bindCryptoService(cryptoManager: CryptoManager): CryptoService + abstract fun bindCryptoService(cryptoService: DefaultCryptoService): CryptoService @Binds abstract fun bindDeleteDeviceTask(deleteDeviceTask: DefaultDeleteDeviceTask): DeleteDeviceTask 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/DefaultCryptoService.kt similarity index 99% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/CryptoManager.kt rename to matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DefaultCryptoService.kt index 96d337bd..a3786d48 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/DefaultCryptoService.kt @@ -93,7 +93,7 @@ import kotlin.math.max * Specially, it tracks all room membership changes events in order to do keys updates. */ @SessionScope -internal class CryptoManager @Inject constructor( +internal class DefaultCryptoService @Inject constructor( // Olm Manager private val olmManager: OlmManager, // The credentials, @@ -1067,6 +1067,6 @@ internal class CryptoManager @Inject constructor( * ========================================================================================== */ override fun toString(): String { - return "CryptoManager of " + credentials.userId + " (" + credentials.deviceId + ")" + return "DefaultCryptoService of " + credentials.userId + " (" + credentials.deviceId + ")" } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt index 2922b7d6..a18c339b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt @@ -40,7 +40,7 @@ import im.vector.matrix.android.api.session.sync.FilterService import im.vector.matrix.android.api.session.sync.SyncState import im.vector.matrix.android.api.session.user.UserService import im.vector.matrix.android.api.util.MatrixCallbackDelegate -import im.vector.matrix.android.internal.crypto.CryptoManager +import im.vector.matrix.android.internal.crypto.DefaultCryptoService import im.vector.matrix.android.internal.database.LiveEntityObserver import im.vector.matrix.android.internal.session.sync.job.SyncThread import im.vector.matrix.android.internal.session.sync.job.SyncWorker @@ -63,7 +63,7 @@ internal class DefaultSession @Inject constructor(override val sessionParams: Se private val signOutService: Lazy, private val pushRuleService: Lazy, private val pushersService: Lazy, - private val cryptoService: Lazy, + private val cryptoService: Lazy, private val fileService: Lazy, private val syncThreadProvider: Provider, private val contentUrlResolver: ContentUrlResolver, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt index 013fc3ca..945f3440 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/CryptoSyncHandler.kt @@ -22,7 +22,7 @@ import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.api.session.room.model.message.MessageContent -import im.vector.matrix.android.internal.crypto.CryptoManager +import im.vector.matrix.android.internal.crypto.DefaultCryptoService import im.vector.matrix.android.internal.crypto.MXEventDecryptionResult import im.vector.matrix.android.internal.crypto.algorithms.olm.OlmDecryptionResult import im.vector.matrix.android.internal.crypto.verification.DefaultSasVerificationService @@ -33,7 +33,7 @@ import timber.log.Timber import javax.inject.Inject -internal class CryptoSyncHandler @Inject constructor(private val cryptoManager: CryptoManager, +internal class CryptoSyncHandler @Inject constructor(private val cryptoService: DefaultCryptoService, private val sasVerificationService: DefaultSasVerificationService) { fun handleToDevice(toDevice: ToDeviceSyncResponse, initialSyncProgressService: DefaultInitialSyncProgressService? = null) { @@ -47,13 +47,13 @@ internal class CryptoSyncHandler @Inject constructor(private val cryptoManager: Timber.e("## handleToDeviceEvent() : Warning: Unable to decrypt to-device event : " + event.content) } else { sasVerificationService.onToDeviceEvent(event) - cryptoManager.onToDeviceEvent(event) + cryptoService.onToDeviceEvent(event) } } } fun onSyncCompleted(syncResponse: SyncResponse) { - cryptoManager.onSyncCompleted(syncResponse) + cryptoService.onSyncCompleted(syncResponse) } @@ -68,7 +68,7 @@ internal class CryptoSyncHandler @Inject constructor(private val cryptoManager: if (event.getClearType() == EventType.ENCRYPTED) { var result: MXEventDecryptionResult? = null try { - result = cryptoManager.decryptEvent(event, timelineId ?: "") + result = cryptoService.decryptEvent(event, timelineId ?: "") } catch (exception: MXCryptoError) { event.mCryptoError = (exception as? MXCryptoError.Base)?.errorType //setCryptoError(exception.cryptoError) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt index 74b56e77..0ebca5e1 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt @@ -23,7 +23,7 @@ import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.api.session.room.model.Membership import im.vector.matrix.android.api.session.room.model.tag.RoomTagContent -import im.vector.matrix.android.internal.crypto.CryptoManager +import im.vector.matrix.android.internal.crypto.DefaultCryptoService import im.vector.matrix.android.internal.database.helper.* import im.vector.matrix.android.internal.database.model.ChunkEntity import im.vector.matrix.android.internal.database.model.EventEntityFields @@ -50,7 +50,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch private val readReceiptHandler: ReadReceiptHandler, private val roomSummaryUpdater: RoomSummaryUpdater, private val roomTagHandler: RoomTagHandler, - private val cryptoManager: CryptoManager, + private val cryptoService: DefaultCryptoService, private val tokenStore: SyncTokenStore, private val pushRuleService: DefaultPushRuleService, private val processForPushTask: ProcessEventForPushTask, @@ -141,7 +141,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch roomSync.state.events.forEach { event -> roomEntity.addStateEvent(event, filterDuplicates = true, stateIndex = untimelinedStateIndex) // Give info to crypto module - cryptoManager.onStateEvent(roomId, event) + cryptoService.onStateEvent(roomId, event) UserEntityFactory.createOrNull(event)?.also { realm.insertOrUpdate(it) } @@ -214,7 +214,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch event.eventId?.also { eventIds.add(it) } chunkEntity.add(roomEntity.roomId, event, PaginationDirection.FORWARDS, stateIndexOffset) // Give info to crypto module - cryptoManager.onLiveEvent(roomEntity.roomId, event) + cryptoService.onLiveEvent(roomEntity.roomId, event) // Try to remove local echo event.unsignedData?.transactionId?.also { val sendingEventEntity = roomEntity.sendingTimelineEvents.find(it) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncResponseHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncResponseHandler.kt index 991e5a9a..584e84c2 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncResponseHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncResponseHandler.kt @@ -18,7 +18,7 @@ package im.vector.matrix.android.internal.session.sync import arrow.core.Try import im.vector.matrix.android.R -import im.vector.matrix.android.internal.crypto.CryptoManager +import im.vector.matrix.android.internal.crypto.DefaultCryptoService import im.vector.matrix.android.internal.session.DefaultInitialSyncProgressService import im.vector.matrix.android.internal.session.reportSubtask import im.vector.matrix.android.internal.session.sync.model.SyncResponse @@ -30,7 +30,7 @@ internal class SyncResponseHandler @Inject constructor(private val roomSyncHandl private val userAccountDataSyncHandler: UserAccountDataSyncHandler, private val groupSyncHandler: GroupSyncHandler, private val cryptoSyncHandler: CryptoSyncHandler, - private val cryptoManager: CryptoManager, + private val cryptoService: DefaultCryptoService, private val initialSyncProgressService: DefaultInitialSyncProgressService) { fun handleResponse(syncResponse: SyncResponse, fromToken: String?, isCatchingUp: Boolean): Try { @@ -40,12 +40,12 @@ internal class SyncResponseHandler @Inject constructor(private val roomSyncHandl val reporter = initialSyncProgressService.takeIf { isInitialSync } measureTimeMillis { - if (!cryptoManager.isStarted()) { - Timber.v("Should start cryptoManager") - cryptoManager.start(isInitialSync) + if (!cryptoService.isStarted()) { + Timber.v("Should start cryptoService") + cryptoService.start(isInitialSync) } }.also { - Timber.v("Finish handling start cryptoManager in $it ms") + Timber.v("Finish handling start cryptoService in $it ms") } val measure = measureTimeMillis { // Handle the to device events before the room ones From 38da4b9ee502ff070e2dd00e3debdc9b6f19b9b7 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 5 Sep 2019 17:02:03 +0200 Subject: [PATCH 4/6] Cleanup and document DefaultInitialSyncProgressService --- .../DefaultInitialSyncProgressService.kt | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt index f1f8a6e3..69f608d0 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt @@ -15,6 +15,7 @@ */ package im.vector.matrix.android.internal.session +import androidx.annotation.StringRes import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import im.vector.matrix.android.api.session.InitialSyncProgressService @@ -25,31 +26,33 @@ import javax.inject.Inject @SessionScope class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgressService { - var status = MutableLiveData() + private var status = MutableLiveData() - var rootTask: TaskInfo? = null + private var rootTask: TaskInfo? = null override fun getLiveStatus(): LiveData { return status } - - fun startTask(nameRes: Int, totalProgress: Int, parentWeight: Float = 1f) { + fun startTask(@StringRes nameRes: Int, totalProgress: Int, parentWeight: Float = 1f) { + // Create a rootTask, or add a child to the leaf if (rootTask == null) { rootTask = TaskInfo(nameRes, totalProgress) } else { val currentLeaf = rootTask!!.leaf() - val newTask = TaskInfo(nameRes, totalProgress) - newTask.parent = currentLeaf - newTask.offset = currentLeaf.currentProgress + + val newTask = TaskInfo(nameRes, + totalProgress, + currentLeaf, + parentWeight) + currentLeaf.child = newTask - newTask.parentWeight = parentWeight } reportProgress(0) } fun reportProgress(progress: Int) { - rootTask?.leaf()?.incrementProgress(progress) + rootTask?.leaf()?.setProgress(progress) } fun endTask(nameRes: Int) { @@ -58,7 +61,7 @@ class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgr //close it val parent = endedTask.parent parent?.child = null - parent?.incrementProgress(endedTask.offset + (endedTask.totalProgress * endedTask.parentWeight).toInt()) + parent?.setProgress(endedTask.offset + (endedTask.totalProgress * endedTask.parentWeight).toInt()) } if (endedTask?.parent == null) { status.postValue(null) @@ -71,14 +74,17 @@ class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgr } - inner class TaskInfo(var nameRes: Int, - var totalProgress: Int) { - var parent: TaskInfo? = null + private inner class TaskInfo(@StringRes var nameRes: Int, + var totalProgress: Int, + var parent: TaskInfo? = null, + var parentWeight: Float = 1f, + var offset: Int = parent?.currentProgress ?: 0) { var child: TaskInfo? = null - var parentWeight: Float = 1f var currentProgress: Int = 0 - var offset: Int = 0 + /** + * Get the further child + */ fun leaf(): TaskInfo { var last = this while (last.child != null) { @@ -87,26 +93,27 @@ class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgr return last } - fun incrementProgress(progress: Int) { + /** + * Set progress of the parent if any (which will post value), or post the value + */ + fun setProgress(progress: Int) { currentProgress = progress // val newProgress = Math.min(currentProgress + progress, totalProgress) parent?.let { val parentProgress = (currentProgress * parentWeight).toInt() - it.incrementProgress(offset + parentProgress) - } - if (parent == null) { - Timber.e("--- ${leaf().nameRes}: ${currentProgress}") + it.setProgress(offset + parentProgress) + } ?: run { + Timber.e("--- ${leaf().nameRes}: $currentProgress") status.postValue( InitialSyncProgressService.Status(leaf().nameRes, currentProgress) ) } } } - } inline fun reportSubtask(reporter: DefaultInitialSyncProgressService?, - nameRes: Int, + @StringRes nameRes: Int, totalProgress: Int, parentWeight: Float = 1f, block: () -> T): T { From a5975a099efe833f0050e49d3986224fefc08f1c Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 5 Sep 2019 17:23:09 +0200 Subject: [PATCH 5/6] Cleanup and document DefaultInitialSyncProgressService --- .../api/session/InitialSyncProgressService.kt | 4 ++-- .../session/DefaultInitialSyncProgressService.kt | 2 +- .../im/vector/riotx/features/home/HomeActivity.kt | 15 +++++---------- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/InitialSyncProgressService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/InitialSyncProgressService.kt index 3bb567ae..a4c9d81b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/InitialSyncProgressService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/InitialSyncProgressService.kt @@ -20,10 +20,10 @@ import androidx.lifecycle.LiveData interface InitialSyncProgressService { - fun getLiveStatus() : LiveData + fun getInitialSyncProgressStatus() : LiveData data class Status( - @StringRes val statusText: Int?, + @StringRes val statusText: Int, val percentProgress: Int = 0 ) } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt index 69f608d0..1d3ce233 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt @@ -30,7 +30,7 @@ class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgr private var rootTask: TaskInfo? = null - override fun getLiveStatus(): LiveData { + override fun getInitialSyncProgressStatus(): LiveData { return status } 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 64a86b90..247c0c19 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 @@ -26,7 +26,6 @@ import androidx.appcompat.widget.Toolbar import androidx.core.view.GravityCompat import androidx.core.view.isVisible import androidx.drawerlayout.widget.DrawerLayout -import androidx.fragment.app.FragmentManager import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders import com.airbnb.mvrx.viewModel @@ -36,12 +35,10 @@ import im.vector.riotx.core.di.ScreenComponent import im.vector.riotx.core.extensions.hideKeyboard import im.vector.riotx.core.extensions.observeEvent import im.vector.riotx.core.extensions.replaceFragment -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.disclaimer.showDisclaimerDialog -import im.vector.riotx.features.navigation.Navigator import im.vector.riotx.features.notifications.NotificationDrawerManager import im.vector.riotx.features.rageshake.VectorUncaughtExceptionHandler import im.vector.riotx.features.workers.signout.SignOutViewModel @@ -119,22 +116,22 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { intent.removeExtra(EXTRA_CLEAR_EXISTING_NOTIFICATION) } - activeSessionHolder.getSafeActiveSession()?.getLiveStatus()?.observe(this, Observer { sprogress -> - Timber.e("${sprogress?.statusText?.let { getString(it) }} ${sprogress?.percentProgress}") - if (sprogress == null) { + activeSessionHolder.getSafeActiveSession()?.getInitialSyncProgressStatus()?.observe(this, Observer { status -> + if (status == null) { waiting_view.isVisible = false } else { + Timber.e("${getString(status.statusText)} ${status.percentProgress}") waiting_view.setOnClickListener { //block interactions } waiting_view_status_horizontal_progress.apply { isIndeterminate = false max = 100 - progress = sprogress.percentProgress + progress = status.percentProgress isVisible = true } waiting_view_status_text.apply { - text = sprogress.statusText?.let { getString(it) } + text = getString(status.statusText) isVisible = true } waiting_view.isVisible = true @@ -213,8 +210,6 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable { } - - companion object { private const val EXTRA_CLEAR_EXISTING_NOTIFICATION = "EXTRA_CLEAR_EXISTING_NOTIFICATION" From 012840abbae87b92b424534dd2f45c3ea48b9ce2 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 5 Sep 2019 18:14:05 +0200 Subject: [PATCH 6/6] Progress in initial sync dialog is decreasing for a step and should not (#532) --- CHANGES.md | 1 + .../session/DefaultInitialSyncProgressService.kt | 6 +++--- .../internal/session/sync/RoomSyncHandler.kt | 16 ++++++---------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index df1379d4..7655c08d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ Other changes: Bugfix: - Fix crash due to missing informationData (#535) + - Progress in initial sync dialog is decreasing for a step and should not (#532) Translations: - diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt index 1d3ce233..6eb1ec35 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt @@ -128,11 +128,11 @@ inline fun Map.mapWithProgress(reporter: DefaultInitialSyncP taskId: Int, weight: Float, transform: (Map.Entry) -> R): List { - val total = count() + val total = count().toFloat() var current = 0 reporter?.startTask(taskId, 100, weight) - return this.map { - reporter?.reportProgress((current / total.toFloat() * 100).toInt()) + return map { + reporter?.reportProgress((current / total * 100).toInt()) current++ transform.invoke(it) }.also { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt index 0ebca5e1..fccaa9b9 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt @@ -97,12 +97,12 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch handleJoinedRoom(realm, it.key, it.value, isInitialSync) } is HandlingStrategy.INVITED -> - handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_invited_rooms, 0.4f) { + handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_invited_rooms, 0.1f) { handleInvitedRoom(realm, it.key, it.value) } is HandlingStrategy.LEFT -> { - handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_left_rooms, 0.2f) { + handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_left_rooms, 0.3f) { handleLeftRoom(realm, it.key, it.value) } } @@ -125,8 +125,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch handleRoomAccountDataEvents(realm, roomId, roomSync.accountData) } - val roomEntity = RoomEntity.where(realm, roomId).findFirst() - ?: realm.createObject(roomId) + val roomEntity = RoomEntity.where(realm, roomId).findFirst() ?: realm.createObject(roomId) if (roomEntity.membership == Membership.INVITE) { roomEntity.chunks.deleteAllFromRealm() @@ -135,8 +134,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch // State event if (roomSync.state != null && roomSync.state.events.isNotEmpty()) { - val minStateIndex = roomEntity.untimelinedStateEvents.where().min(EventEntityFields.STATE_INDEX)?.toInt() - ?: Int.MIN_VALUE + val minStateIndex = roomEntity.untimelinedStateEvents.where().min(EventEntityFields.STATE_INDEX)?.toInt() ?: Int.MIN_VALUE val untimelinedStateIndex = minStateIndex + 1 roomSync.state.events.forEach { event -> roomEntity.addStateEvent(event, filterDuplicates = true, stateIndex = untimelinedStateIndex) @@ -167,8 +165,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch roomSync: InvitedRoomSync): RoomEntity { Timber.v("Handle invited sync for room $roomId") - val roomEntity = RoomEntity.where(realm, roomId).findFirst() - ?: realm.createObject(roomId) + val roomEntity = RoomEntity.where(realm, roomId).findFirst() ?: realm.createObject(roomId) roomEntity.membership = Membership.INVITE if (roomSync.inviteState != null && roomSync.inviteState.events.isNotEmpty()) { val chunkEntity = handleTimelineEvents(realm, roomEntity, roomSync.inviteState.events) @@ -181,8 +178,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch private fun handleLeftRoom(realm: Realm, roomId: String, roomSync: RoomSync): RoomEntity { - val roomEntity = RoomEntity.where(realm, roomId).findFirst() - ?: realm.createObject(roomId) + val roomEntity = RoomEntity.where(realm, roomId).findFirst() ?: realm.createObject(roomId) roomEntity.membership = Membership.LEAVE roomEntity.chunks.deleteAllFromRealm()