BayernMessenger/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/SyncResponseHandler.kt

105 lines
4.7 KiB
Kotlin
Raw Normal View History

2019-01-18 10:12:08 +00:00
/*
* Copyright 2019 New Vector Ltd
2019-01-18 10:12:08 +00:00
*
* 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
2019-01-18 10:12:08 +00:00
*
* 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.
2019-01-18 10:12:08 +00:00
*/
2018-10-17 16:21:09 +00:00
package im.vector.matrix.android.internal.session.sync
import arrow.core.Try
2019-07-04 08:53:46 +00:00
import im.vector.matrix.android.R
import im.vector.matrix.android.internal.crypto.DefaultCryptoService
2019-07-04 08:53:46 +00:00
import im.vector.matrix.android.internal.session.DefaultInitialSyncProgressService
import im.vector.matrix.android.internal.session.reportSubtask
2018-10-17 16:21:09 +00:00
import im.vector.matrix.android.internal.session.sync.model.SyncResponse
import timber.log.Timber
import javax.inject.Inject
import kotlin.system.measureTimeMillis
2018-10-12 17:26:22 +00:00
internal class SyncResponseHandler @Inject constructor(private val roomSyncHandler: RoomSyncHandler,
private val userAccountDataSyncHandler: UserAccountDataSyncHandler,
private val groupSyncHandler: GroupSyncHandler,
private val cryptoSyncHandler: CryptoSyncHandler,
private val cryptoService: DefaultCryptoService,
2019-07-04 08:53:46 +00:00
private val initialSyncProgressService: DefaultInitialSyncProgressService) {
fun handleResponse(syncResponse: SyncResponse, fromToken: String?, isCatchingUp: Boolean): Try<SyncResponse> {
return Try {
val isInitialSync = fromToken == null
2019-07-09 14:36:46 +00:00
Timber.v("Start handling sync, is InitialSync: $isInitialSync")
2019-07-04 08:53:46 +00:00
val reporter = initialSyncProgressService.takeIf { isInitialSync }
2019-07-09 15:42:53 +00:00
measureTimeMillis {
if (!cryptoService.isStarted()) {
Timber.v("Should start cryptoService")
cryptoService.start(isInitialSync)
2019-07-09 15:42:53 +00:00
}
}.also {
Timber.v("Finish handling start cryptoService in $it ms")
}
val measure = measureTimeMillis {
2019-05-16 08:23:57 +00:00
// Handle the to device events before the room ones
// to ensure to decrypt them properly
2019-07-09 15:42:53 +00:00
measureTimeMillis {
Timber.v("Handle toDevice")
reportSubtask(reporter, R.string.initial_sync_start_importing_account_crypto, 100, 0.1f) {
if (syncResponse.toDevice != null) {
cryptoSyncHandler.handleToDevice(syncResponse.toDevice, reporter)
}
2019-07-04 08:53:46 +00:00
}
2019-07-09 15:42:53 +00:00
}.also {
Timber.v("Finish handling toDevice in $it ms")
2019-05-16 08:23:57 +00:00
}
2019-07-04 08:53:46 +00:00
2019-07-09 15:42:53 +00:00
measureTimeMillis {
Timber.v("Handle rooms")
reportSubtask(reporter, R.string.initial_sync_start_importing_account_rooms, 100, 0.7f) {
if (syncResponse.rooms != null) {
roomSyncHandler.handle(syncResponse.rooms, isInitialSync, reporter)
2019-07-09 15:42:53 +00:00
}
2019-07-04 08:53:46 +00:00
}
2019-07-09 15:42:53 +00:00
}.also {
Timber.v("Finish handling rooms in $it ms")
}
2019-07-04 08:53:46 +00:00
2019-07-09 15:42:53 +00:00
measureTimeMillis {
reportSubtask(reporter, R.string.initial_sync_start_importing_account_groups, 100, 0.1f) {
Timber.v("Handle groups")
if (syncResponse.groups != null) {
groupSyncHandler.handle(syncResponse.groups, reporter)
}
2019-07-04 08:53:46 +00:00
}
2019-07-09 15:42:53 +00:00
}.also {
Timber.v("Finish handling groups in $it ms")
}
2019-07-04 08:53:46 +00:00
2019-07-09 15:42:53 +00:00
measureTimeMillis {
reportSubtask(reporter, R.string.initial_sync_start_importing_account_data, 100, 0.1f) {
Timber.v("Handle accountData")
userAccountDataSyncHandler.handle(syncResponse.accountData, syncResponse.rooms?.invite)
2019-07-04 08:53:46 +00:00
}
2019-07-09 15:42:53 +00:00
}.also {
Timber.v("Finish handling accountData in $it ms")
}
2019-07-04 08:53:46 +00:00
Timber.v("On sync completed")
cryptoSyncHandler.onSyncCompleted(syncResponse)
}
Timber.v("Finish handling sync in $measure ms")
syncResponse
2018-10-12 17:26:22 +00:00
}
}
}