Send static location API

This commit is contained in:
Maxime NATUREL 2022-06-09 17:10:33 +02:00
parent 632064ffde
commit 9b61c1aead
7 changed files with 89 additions and 22 deletions

View File

@ -18,11 +18,21 @@ package org.matrix.android.sdk.api.session.room.location
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary
import org.matrix.android.sdk.api.util.Cancelable
/** /**
* Manage all location sharing related features. * Manage all location sharing related features.
*/ */
interface LocationSharingService { interface LocationSharingService {
/**
* Send a static location event to the room.
* @param latitude required latitude of the location
* @param longitude required longitude of the location
* @param uncertainty Accuracy of the location in meters
* @param isUserLocation indicates whether the location data corresponds to the user location or not (pinned location)
*/
suspend fun sendStaticLocation(latitude: Double, longitude: Double, uncertainty: Double?, isUserLocation: Boolean): Cancelable
/** /**
* Starts sharing live location in the room. * Starts sharing live location in the room.
* @param timeoutMillis timeout of the live in milliseconds * @param timeoutMillis timeout of the live in milliseconds

View File

@ -142,15 +142,6 @@ interface SendService {
*/ */
fun resendMediaMessage(localEcho: TimelineEvent): Cancelable fun resendMediaMessage(localEcho: TimelineEvent): Cancelable
/**
* Send a location event to the room.
* @param latitude required latitude of the location
* @param longitude required longitude of the location
* @param uncertainty Accuracy of the location in meters
* @param isUserLocation indicates whether the location data corresponds to the user location or not
*/
fun sendLocation(latitude: Double, longitude: Double, uncertainty: Double?, isUserLocation: Boolean): Cancelable
/** /**
* Send a live location event to the room. beacon_info state event has to be sent before sending live location updates. * Send a live location event to the room. beacon_info state event has to be sent before sending live location updates.
* @param beaconInfoEventId event id of the initial beacon info state event * @param beaconInfoEventId event id of the initial beacon info state event

View File

@ -51,8 +51,10 @@ import org.matrix.android.sdk.internal.session.room.directory.DefaultSetRoomDire
import org.matrix.android.sdk.internal.session.room.directory.GetPublicRoomTask import org.matrix.android.sdk.internal.session.room.directory.GetPublicRoomTask
import org.matrix.android.sdk.internal.session.room.directory.GetRoomDirectoryVisibilityTask import org.matrix.android.sdk.internal.session.room.directory.GetRoomDirectoryVisibilityTask
import org.matrix.android.sdk.internal.session.room.directory.SetRoomDirectoryVisibilityTask import org.matrix.android.sdk.internal.session.room.directory.SetRoomDirectoryVisibilityTask
import org.matrix.android.sdk.internal.session.room.location.DefaultSendStaticLocationTask
import org.matrix.android.sdk.internal.session.room.location.DefaultStartLiveLocationShareTask import org.matrix.android.sdk.internal.session.room.location.DefaultStartLiveLocationShareTask
import org.matrix.android.sdk.internal.session.room.location.DefaultStopLiveLocationShareTask import org.matrix.android.sdk.internal.session.room.location.DefaultStopLiveLocationShareTask
import org.matrix.android.sdk.internal.session.room.location.SendStaticLocationTask
import org.matrix.android.sdk.internal.session.room.location.StartLiveLocationShareTask import org.matrix.android.sdk.internal.session.room.location.StartLiveLocationShareTask
import org.matrix.android.sdk.internal.session.room.location.StopLiveLocationShareTask import org.matrix.android.sdk.internal.session.room.location.StopLiveLocationShareTask
import org.matrix.android.sdk.internal.session.room.membership.DefaultLoadRoomMembersTask import org.matrix.android.sdk.internal.session.room.membership.DefaultLoadRoomMembersTask
@ -309,4 +311,7 @@ internal abstract class RoomModule {
@Binds @Binds
abstract fun bindStopLiveLocationShareTask(task: DefaultStopLiveLocationShareTask): StopLiveLocationShareTask abstract fun bindStopLiveLocationShareTask(task: DefaultStopLiveLocationShareTask): StopLiveLocationShareTask
@Binds
abstract fun bindSendStaticLocationTask(task: DefaultSendStaticLocationTask): SendStaticLocationTask
} }

View File

@ -23,6 +23,7 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import org.matrix.android.sdk.api.session.room.location.LocationSharingService import org.matrix.android.sdk.api.session.room.location.LocationSharingService
import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.database.mapper.LiveLocationShareAggregatedSummaryMapper import org.matrix.android.sdk.internal.database.mapper.LiveLocationShareAggregatedSummaryMapper
import org.matrix.android.sdk.internal.database.model.livelocation.LiveLocationShareAggregatedSummaryEntity import org.matrix.android.sdk.internal.database.model.livelocation.LiveLocationShareAggregatedSummaryEntity
import org.matrix.android.sdk.internal.database.query.findRunningLiveInRoom import org.matrix.android.sdk.internal.database.query.findRunningLiveInRoom
@ -32,6 +33,7 @@ import org.matrix.android.sdk.internal.di.SessionDatabase
internal class DefaultLocationSharingService @AssistedInject constructor( internal class DefaultLocationSharingService @AssistedInject constructor(
@Assisted private val roomId: String, @Assisted private val roomId: String,
@SessionDatabase private val monarchy: Monarchy, @SessionDatabase private val monarchy: Monarchy,
private val sendStaticLocationTask: SendStaticLocationTask,
private val startLiveLocationShareTask: StartLiveLocationShareTask, private val startLiveLocationShareTask: StartLiveLocationShareTask,
private val stopLiveLocationShareTask: StopLiveLocationShareTask, private val stopLiveLocationShareTask: StopLiveLocationShareTask,
private val liveLocationShareAggregatedSummaryMapper: LiveLocationShareAggregatedSummaryMapper, private val liveLocationShareAggregatedSummaryMapper: LiveLocationShareAggregatedSummaryMapper,
@ -42,6 +44,17 @@ internal class DefaultLocationSharingService @AssistedInject constructor(
fun create(roomId: String): DefaultLocationSharingService fun create(roomId: String): DefaultLocationSharingService
} }
override suspend fun sendStaticLocation(latitude: Double, longitude: Double, uncertainty: Double?, isUserLocation: Boolean): Cancelable {
val params = SendStaticLocationTask.Params(
roomId = roomId,
latitude = latitude,
longitude = longitude,
uncertainty = uncertainty,
isUserLocation = isUserLocation
)
return sendStaticLocationTask.execute(params)
}
override suspend fun startLiveLocationShare(timeoutMillis: Long): String { override suspend fun startLiveLocationShare(timeoutMillis: Long): String {
val params = StartLiveLocationShareTask.Params( val params = StartLiveLocationShareTask.Params(
roomId = roomId, roomId = roomId,

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
*
* 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
*
* 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.
*/
package org.matrix.android.sdk.internal.session.room.location
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.session.room.send.LocalEchoEventFactory
import org.matrix.android.sdk.internal.session.room.send.queue.EventSenderProcessor
import org.matrix.android.sdk.internal.task.Task
import javax.inject.Inject
internal interface SendStaticLocationTask : Task<SendStaticLocationTask.Params, Cancelable> {
data class Params(
val roomId: String,
val latitude: Double,
val longitude: Double,
val uncertainty: Double?,
val isUserLocation: Boolean
)
}
// TODO add unit tests
internal class DefaultSendStaticLocationTask @Inject constructor(
private val localEchoEventFactory: LocalEchoEventFactory,
private val eventSenderProcessor: EventSenderProcessor,
) : SendStaticLocationTask {
override suspend fun execute(params: SendStaticLocationTask.Params): Cancelable {
val event = localEchoEventFactory.createLocationEvent(
roomId = params.roomId,
latitude = params.latitude,
longitude = params.longitude,
uncertainty = params.uncertainty,
isUserLocation = params.isUserLocation
)
localEchoEventFactory.createLocalEcho(event)
return eventSenderProcessor.postEvent(event)
}
}

View File

@ -129,12 +129,6 @@ internal class DefaultSendService @AssistedInject constructor(
.let { sendEvent(it) } .let { sendEvent(it) }
} }
override fun sendLocation(latitude: Double, longitude: Double, uncertainty: Double?, isUserLocation: Boolean): Cancelable {
return localEchoEventFactory.createLocationEvent(roomId, latitude, longitude, uncertainty, isUserLocation)
.also { createLocalEcho(it) }
.let { sendEvent(it) }
}
override fun sendLiveLocation(beaconInfoEventId: String, latitude: Double, longitude: Double, uncertainty: Double?): Cancelable { override fun sendLiveLocation(beaconInfoEventId: String, latitude: Double, longitude: Double, uncertainty: Double?): Cancelable {
return localEchoEventFactory.createLiveLocationEvent(beaconInfoEventId, roomId, latitude, longitude, uncertainty) return localEchoEventFactory.createLiveLocationEvent(beaconInfoEventId, roomId, latitude, longitude, uncertainty)
.also { createLocalEcho(it) } .also { createLocalEcho(it) }

View File

@ -136,13 +136,15 @@ class LocationSharingViewModel @AssistedInject constructor(
private fun shareLocation(locationData: LocationData?, isUserLocation: Boolean) { private fun shareLocation(locationData: LocationData?, isUserLocation: Boolean) {
locationData?.let { location -> locationData?.let { location ->
room.sendService().sendLocation( viewModelScope.launch {
room.locationSharingService().sendStaticLocation(
latitude = location.latitude, latitude = location.latitude,
longitude = location.longitude, longitude = location.longitude,
uncertainty = location.uncertainty, uncertainty = location.uncertainty,
isUserLocation = isUserLocation isUserLocation = isUserLocation
) )
_viewEvents.post(LocationSharingViewEvents.Close) _viewEvents.post(LocationSharingViewEvents.Close)
}
} ?: run { } ?: run {
_viewEvents.post(LocationSharingViewEvents.LocationNotAvailableError) _viewEvents.post(LocationSharingViewEvents.LocationNotAvailableError)
} }