2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +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
|
|
|
*
|
2019-01-25 13:04:59 +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
|
2018-10-10 17:46:43 +00:00
|
|
|
|
2018-11-06 15:35:06 +00:00
|
|
|
import arrow.core.Try
|
2019-07-04 08:53:46 +00:00
|
|
|
import im.vector.matrix.android.R
|
2019-09-05 14:14:34 +00:00
|
|
|
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
|
2018-10-10 17:46:43 +00:00
|
|
|
import timber.log.Timber
|
2019-06-14 14:32:23 +00:00
|
|
|
import javax.inject.Inject
|
2019-03-28 11:00:45 +00:00
|
|
|
import kotlin.system.measureTimeMillis
|
2018-10-12 17:26:22 +00:00
|
|
|
|
2019-06-14 14:32:23 +00:00
|
|
|
internal class SyncResponseHandler @Inject constructor(private val roomSyncHandler: RoomSyncHandler,
|
|
|
|
private val userAccountDataSyncHandler: UserAccountDataSyncHandler,
|
|
|
|
private val groupSyncHandler: GroupSyncHandler,
|
|
|
|
private val cryptoSyncHandler: CryptoSyncHandler,
|
2019-09-05 14:14:34 +00:00
|
|
|
private val cryptoService: DefaultCryptoService,
|
2019-07-04 08:53:46 +00:00
|
|
|
private val initialSyncProgressService: DefaultInitialSyncProgressService) {
|
2018-10-10 17:46:43 +00:00
|
|
|
|
2018-11-06 15:35:06 +00:00
|
|
|
fun handleResponse(syncResponse: SyncResponse, fromToken: String?, isCatchingUp: Boolean): Try<SyncResponse> {
|
|
|
|
return Try {
|
2019-07-03 15:17:58 +00:00
|
|
|
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 {
|
2019-09-05 14:14:34 +00:00
|
|
|
if (!cryptoService.isStarted()) {
|
|
|
|
Timber.v("Should start cryptoService")
|
|
|
|
cryptoService.start(isInitialSync)
|
2019-07-09 15:42:53 +00:00
|
|
|
}
|
|
|
|
}.also {
|
2019-09-05 14:14:34 +00:00
|
|
|
Timber.v("Finish handling start cryptoService in $it ms")
|
2019-07-03 15:17:58 +00:00
|
|
|
}
|
2019-03-28 11:00:45 +00:00
|
|
|
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) {
|
2019-08-08 13:03:36 +00:00
|
|
|
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-03-28 11:00:45 +00:00
|
|
|
}
|
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-03-28 11:00:45 +00:00
|
|
|
}
|
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")
|
2019-07-30 15:32:31 +00:00
|
|
|
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-03-28 11:00:45 +00:00
|
|
|
}
|
2019-07-04 08:53:46 +00:00
|
|
|
|
2019-06-06 17:10:04 +00:00
|
|
|
Timber.v("On sync completed")
|
2019-06-07 14:01:24 +00:00
|
|
|
cryptoSyncHandler.onSyncCompleted(syncResponse)
|
2018-11-06 15:35:06 +00:00
|
|
|
}
|
2019-03-28 11:00:45 +00:00
|
|
|
Timber.v("Finish handling sync in $measure ms")
|
2018-11-06 15:35:06 +00:00
|
|
|
syncResponse
|
2018-10-12 17:26:22 +00:00
|
|
|
}
|
2018-10-10 17:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|