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-11-08 18:08:14 +00:00
|
|
|
package im.vector.matrix.android.internal.session.room.send
|
|
|
|
|
2019-05-27 17:13:42 +00:00
|
|
|
import androidx.work.BackoffPolicy
|
|
|
|
import androidx.work.Constraints
|
|
|
|
import androidx.work.ExistingWorkPolicy
|
|
|
|
import androidx.work.NetworkType
|
|
|
|
import androidx.work.OneTimeWorkRequest
|
|
|
|
import androidx.work.OneTimeWorkRequestBuilder
|
|
|
|
import androidx.work.WorkManager
|
2018-11-25 15:17:47 +00:00
|
|
|
import com.zhuinden.monarchy.Monarchy
|
2019-04-04 17:55:58 +00:00
|
|
|
import im.vector.matrix.android.api.session.content.ContentAttachmentData
|
2019-05-21 13:42:09 +00:00
|
|
|
import im.vector.matrix.android.api.session.crypto.CryptoService
|
2018-11-25 15:17:47 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.Event
|
2019-01-18 15:26:17 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.send.SendService
|
2018-11-08 18:08:14 +00:00
|
|
|
import im.vector.matrix.android.api.util.Cancelable
|
2019-04-09 17:57:43 +00:00
|
|
|
import im.vector.matrix.android.api.util.CancelableBag
|
|
|
|
import im.vector.matrix.android.api.util.addTo
|
|
|
|
import im.vector.matrix.android.internal.database.helper.addSendingEvent
|
2018-11-25 15:17:47 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
2019-04-09 17:57:43 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.RoomEntity
|
2018-11-25 15:17:47 +00:00
|
|
|
import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom
|
2019-04-09 17:57:43 +00:00
|
|
|
import im.vector.matrix.android.internal.database.query.where
|
2019-04-04 17:55:58 +00:00
|
|
|
import im.vector.matrix.android.internal.session.content.UploadContentWorker
|
2018-11-08 18:08:14 +00:00
|
|
|
import im.vector.matrix.android.internal.util.CancelableWork
|
2018-11-09 13:06:23 +00:00
|
|
|
import im.vector.matrix.android.internal.util.WorkerParamsFactory
|
2018-11-25 15:17:47 +00:00
|
|
|
import im.vector.matrix.android.internal.util.tryTransactionAsync
|
2019-05-21 13:42:09 +00:00
|
|
|
import timber.log.Timber
|
2018-11-08 18:08:14 +00:00
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
2018-11-09 13:06:23 +00:00
|
|
|
private const val SEND_WORK = "SEND_WORK"
|
2019-04-09 17:57:43 +00:00
|
|
|
private const val UPLOAD_WORK = "UPLOAD_WORK"
|
2019-04-03 20:54:48 +00:00
|
|
|
private const val BACKOFF_DELAY = 10_000L
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2019-04-03 20:54:48 +00:00
|
|
|
private val WORK_CONSTRAINTS = Constraints.Builder()
|
|
|
|
.setRequiredNetworkType(NetworkType.CONNECTED)
|
|
|
|
.build()
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2018-11-25 15:17:47 +00:00
|
|
|
internal class DefaultSendService(private val roomId: String,
|
2019-04-04 17:55:58 +00:00
|
|
|
private val eventFactory: LocalEchoEventFactory,
|
2019-05-21 13:42:09 +00:00
|
|
|
private val cryptoService: CryptoService,
|
2019-04-09 17:57:43 +00:00
|
|
|
private val monarchy: Monarchy)
|
|
|
|
: SendService {
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2019-04-11 11:21:51 +00:00
|
|
|
override fun sendTextMessage(text: String, msgType: String): Cancelable {
|
|
|
|
val event = eventFactory.createTextEvent(roomId, msgType, text).also {
|
2019-04-09 17:57:43 +00:00
|
|
|
saveLocalEcho(it)
|
2018-11-25 15:17:47 +00:00
|
|
|
}
|
2019-05-21 13:42:09 +00:00
|
|
|
|
|
|
|
// Encrypted room handling
|
|
|
|
if (cryptoService.isRoomEncrypted(roomId)) {
|
|
|
|
Timber.v("Send event in encrypted room")
|
|
|
|
// Encrypt then send
|
|
|
|
|
|
|
|
val encryptWork = createEncryptEventWork(event)
|
|
|
|
|
|
|
|
val sendWork = OneTimeWorkRequestBuilder<SendEventWorker>()
|
|
|
|
.setConstraints(WORK_CONSTRAINTS)
|
|
|
|
.setBackoffCriteria(BackoffPolicy.LINEAR, BACKOFF_DELAY, TimeUnit.MILLISECONDS)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
WorkManager.getInstance()
|
|
|
|
// Encrypt
|
|
|
|
.beginUniqueWork(buildWorkIdentifier(SEND_WORK), ExistingWorkPolicy.APPEND, encryptWork)
|
|
|
|
// then send
|
|
|
|
.then(sendWork)
|
|
|
|
.enqueue()
|
|
|
|
|
|
|
|
return CancelableWork(encryptWork.id)
|
|
|
|
} else {
|
|
|
|
return sendEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun sendEvent(event: Event): Cancelable {
|
2019-04-03 20:54:48 +00:00
|
|
|
val sendWork = createSendEventWork(event)
|
|
|
|
WorkManager.getInstance()
|
2019-04-09 17:57:43 +00:00
|
|
|
.beginUniqueWork(buildWorkIdentifier(SEND_WORK), ExistingWorkPolicy.APPEND, sendWork)
|
2019-04-03 20:54:48 +00:00
|
|
|
.enqueue()
|
|
|
|
return CancelableWork(sendWork.id)
|
|
|
|
}
|
2018-11-25 15:17:47 +00:00
|
|
|
|
2019-04-09 17:57:43 +00:00
|
|
|
override fun sendMedias(attachments: List<ContentAttachmentData>): Cancelable {
|
|
|
|
val cancelableBag = CancelableBag()
|
|
|
|
attachments.forEach {
|
|
|
|
sendMedia(it).addTo(cancelableBag)
|
|
|
|
}
|
|
|
|
return cancelableBag
|
|
|
|
}
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2019-04-09 17:57:43 +00:00
|
|
|
override fun sendMedia(attachment: ContentAttachmentData): Cancelable {
|
2019-04-03 20:54:48 +00:00
|
|
|
// Create an event with the media file path
|
2019-04-04 17:55:58 +00:00
|
|
|
val event = eventFactory.createMediaEvent(roomId, attachment).also {
|
|
|
|
saveLocalEcho(it)
|
|
|
|
}
|
|
|
|
val uploadWork = createUploadMediaWork(event, attachment)
|
2019-04-03 20:54:48 +00:00
|
|
|
val sendWork = createSendEventWork(event)
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2018-12-13 10:00:50 +00:00
|
|
|
WorkManager.getInstance()
|
2019-04-09 17:57:43 +00:00
|
|
|
.beginUniqueWork(buildWorkIdentifier(UPLOAD_WORK), ExistingWorkPolicy.APPEND, uploadWork)
|
2019-04-03 20:54:48 +00:00
|
|
|
.then(sendWork)
|
2018-11-08 18:08:14 +00:00
|
|
|
.enqueue()
|
|
|
|
|
2018-12-13 10:00:50 +00:00
|
|
|
return CancelableWork(sendWork.id)
|
2019-04-03 20:54:48 +00:00
|
|
|
}
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2019-04-04 17:55:58 +00:00
|
|
|
private fun saveLocalEcho(event: Event) {
|
2018-11-25 15:17:47 +00:00
|
|
|
monarchy.tryTransactionAsync { realm ->
|
2019-04-09 17:57:43 +00:00
|
|
|
val roomEntity = RoomEntity.where(realm, roomId = roomId).findFirst()
|
2019-05-21 13:42:09 +00:00
|
|
|
?: return@tryTransactionAsync
|
2019-04-09 17:57:43 +00:00
|
|
|
val liveChunk = ChunkEntity.findLastLiveChunkFromRoom(realm, roomId = roomId)
|
2019-05-21 13:42:09 +00:00
|
|
|
?: return@tryTransactionAsync
|
2019-04-09 17:57:43 +00:00
|
|
|
|
|
|
|
roomEntity.addSendingEvent(event, liveChunk.forwardsStateIndex ?: 0)
|
2018-11-25 15:17:47 +00:00
|
|
|
}
|
2019-04-03 20:54:48 +00:00
|
|
|
}
|
2018-11-25 15:17:47 +00:00
|
|
|
|
2019-04-09 17:57:43 +00:00
|
|
|
private fun buildWorkIdentifier(identifier: String): String {
|
|
|
|
return "${roomId}_$identifier"
|
|
|
|
}
|
|
|
|
|
2019-05-21 13:42:09 +00:00
|
|
|
private fun createEncryptEventWork(event: Event): OneTimeWorkRequest {
|
|
|
|
// Same parameter
|
2019-05-26 17:21:45 +00:00
|
|
|
val params = EncryptEventWorker.Params(roomId, event)
|
|
|
|
val sendWorkData = WorkerParamsFactory.toData(params)
|
2019-05-21 13:42:09 +00:00
|
|
|
|
|
|
|
return OneTimeWorkRequestBuilder<EncryptEventWorker>()
|
|
|
|
.setConstraints(WORK_CONSTRAINTS)
|
|
|
|
.setInputData(sendWorkData)
|
|
|
|
.setBackoffCriteria(BackoffPolicy.LINEAR, BACKOFF_DELAY, TimeUnit.MILLISECONDS)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2019-04-03 20:54:48 +00:00
|
|
|
private fun createSendEventWork(event: Event): OneTimeWorkRequest {
|
2018-11-25 15:17:47 +00:00
|
|
|
val sendContentWorkerParams = SendEventWorker.Params(roomId, event)
|
2019-04-03 20:54:48 +00:00
|
|
|
val sendWorkData = WorkerParamsFactory.toData(sendContentWorkerParams)
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2019-04-03 20:54:48 +00:00
|
|
|
return OneTimeWorkRequestBuilder<SendEventWorker>()
|
|
|
|
.setConstraints(WORK_CONSTRAINTS)
|
|
|
|
.setInputData(sendWorkData)
|
|
|
|
.setBackoffCriteria(BackoffPolicy.LINEAR, BACKOFF_DELAY, TimeUnit.MILLISECONDS)
|
2018-11-08 18:08:14 +00:00
|
|
|
.build()
|
2019-04-03 20:54:48 +00:00
|
|
|
}
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2019-04-04 17:55:58 +00:00
|
|
|
private fun createUploadMediaWork(event: Event, attachment: ContentAttachmentData): OneTimeWorkRequest {
|
|
|
|
val uploadMediaWorkerParams = UploadContentWorker.Params(roomId, event, attachment)
|
2019-04-03 20:54:48 +00:00
|
|
|
val uploadWorkData = WorkerParamsFactory.toData(uploadMediaWorkerParams)
|
2018-11-08 18:08:14 +00:00
|
|
|
|
2019-04-04 17:55:58 +00:00
|
|
|
return OneTimeWorkRequestBuilder<UploadContentWorker>()
|
2019-04-03 20:54:48 +00:00
|
|
|
.setConstraints(WORK_CONSTRAINTS)
|
|
|
|
.setInputData(uploadWorkData)
|
|
|
|
.setBackoffCriteria(BackoffPolicy.LINEAR, BACKOFF_DELAY, TimeUnit.MILLISECONDS)
|
|
|
|
.build()
|
2018-11-08 18:08:14 +00:00
|
|
|
}
|
|
|
|
|
2018-11-25 15:17:47 +00:00
|
|
|
}
|