Code review fixes.

This commit is contained in:
Onuray Sahin 2022-04-14 16:23:43 +03:00
parent 023a00d160
commit f49e7d9619
4 changed files with 64 additions and 38 deletions

View File

@ -68,9 +68,16 @@ interface StateService {
/** /**
* Stops sharing live location in the room * Stops sharing live location in the room
* @param beaconInfoStateEvent Initial beacon info state event * @param userId user id
*/ */
suspend fun stopLiveLocation(beaconInfoStateEvent: Event) suspend fun stopLiveLocation(userId: String)
/**
* Returns beacon info state event of a user
* @param userId user id who is sharing location
* @param filterOnlyLive filters only ongoing live location sharing beacons if true else ended event is included
*/
suspend fun getLiveLocationBeaconInfo(userId: String, filterOnlyLive: Boolean): Event?
/** /**
* Send a state event to the room * Send a state event to the room

View File

@ -21,6 +21,7 @@ import androidx.lifecycle.LiveData
import dagger.assisted.Assisted import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.query.QueryStringValue import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.EventType
@ -190,24 +191,41 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
updateJoinRule(RoomJoinRules.RESTRICTED, null, allowEntries) updateJoinRule(RoomJoinRules.RESTRICTED, null, allowEntries)
} }
override suspend fun stopLiveLocation(beaconInfoStateEvent: Event) { override suspend fun stopLiveLocation(userId: String) {
beaconInfoStateEvent.getClearContent()?.toModel<LiveLocationBeaconContent>()?.let { content -> getLiveLocationBeaconInfo(userId, true)?.let { beaconInfoStateEvent ->
val beaconContent = LiveLocationBeaconContent( beaconInfoStateEvent.getClearContent()?.toModel<LiveLocationBeaconContent>()?.let { content ->
unstableBeaconInfo = BeaconInfo( val beaconContent = LiveLocationBeaconContent(
description = content.getBestBeaconInfo()?.description, unstableBeaconInfo = BeaconInfo(
timeout = content.getBestBeaconInfo()?.timeout, description = content.getBestBeaconInfo()?.description,
isLive = false, timeout = content.getBestBeaconInfo()?.timeout,
), isLive = false,
unstableTimestampAsMilliseconds = System.currentTimeMillis() ),
).toContent() unstableTimestampAsMilliseconds = System.currentTimeMillis()
).toContent()
beaconInfoStateEvent.stateKey?.let { beaconInfoStateEvent.stateKey?.let {
sendStateEvent( sendStateEvent(
eventType = EventType.STATE_ROOM_BEACON_INFO.first(), eventType = EventType.STATE_ROOM_BEACON_INFO.first(),
body = beaconContent, body = beaconContent,
stateKey = it stateKey = it
) )
}
} }
} }
} }
override suspend fun getLiveLocationBeaconInfo(userId: String, filterOnlyLive: Boolean): Event? {
return EventType.STATE_ROOM_BEACON_INFO
.mapNotNull {
stateEventDataSource.getStateEvent(
roomId = roomId,
eventType = it,
stateKey = QueryStringValue.Equals(userId)
)
}
.firstOrNull { beaconInfoEvent ->
!filterOnlyLive ||
beaconInfoEvent.getClearContent()?.toModel<LiveLocationBeaconContent>()?.getBestBeaconInfo()?.isLive.orFalse()
}
}
} }

View File

@ -28,7 +28,6 @@ import im.vector.app.features.notifications.NotificationUtils
import im.vector.app.features.session.coroutineScope import im.vector.app.features.session.coroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.toContent import org.matrix.android.sdk.api.session.events.model.toContent
@ -88,7 +87,7 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
.getSafeActiveSession() .getSafeActiveSession()
?.let { session -> ?.let { session ->
session.coroutineScope.launch(session.coroutineDispatchers.io) { session.coroutineScope.launch(session.coroutineDispatchers.io) {
sendBeaconInfo(session, roomArgs) sendLiveBeaconInfo(session, roomArgs)
} }
} }
} }
@ -96,7 +95,7 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
return START_STICKY return START_STICKY
} }
private suspend fun sendBeaconInfo(session: Session, roomArgs: RoomArgs) { private suspend fun sendLiveBeaconInfo(session: Session, roomArgs: RoomArgs) {
val beaconContent = LiveLocationBeaconContent( val beaconContent = LiveLocationBeaconContent(
unstableBeaconInfo = BeaconInfo( unstableBeaconInfo = BeaconInfo(
timeout = roomArgs.durationMillis, timeout = roomArgs.durationMillis,
@ -134,7 +133,7 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
Timber.i("### LocationSharingService.stopSharingLocation for $roomId") Timber.i("### LocationSharingService.stopSharingLocation for $roomId")
// Send a new beacon info state by setting live field as false // Send a new beacon info state by setting live field as false
updateStoppedBeaconInfo(roomId) sendStoppedBeaconInfo(roomId)
synchronized(roomArgsList) { synchronized(roomArgsList) {
roomArgsList.removeAll { it.roomId == roomId } roomArgsList.removeAll { it.roomId == roomId }
@ -145,21 +144,12 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
} }
} }
private fun updateStoppedBeaconInfo(roomId: String) { private fun sendStoppedBeaconInfo(roomId: String) {
activeSessionHolder activeSessionHolder
.getSafeActiveSession() .getSafeActiveSession()
?.let { session -> ?.let { session ->
session.coroutineScope.launch(session.coroutineDispatchers.io) { session.coroutineScope.launch(session.coroutineDispatchers.io) {
val room = session.getRoom(roomId) session.getRoom(roomId)?.stopLiveLocation(session.myUserId)
EventType
.STATE_ROOM_BEACON_INFO
.mapNotNull {
room?.getStateEvent(it, QueryStringValue.Equals(session.myUserId))
}
.firstOrNull()
?.let { beaconInfoEvent ->
room?.stopLiveLocation(beaconInfoEvent)
}
} }
} }
} }
@ -167,16 +157,26 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
override fun onLocationUpdate(locationData: LocationData) { override fun onLocationUpdate(locationData: LocationData) {
Timber.i("### LocationSharingService.onLocationUpdate. Uncertainty: ${locationData.uncertainty}") Timber.i("### LocationSharingService.onLocationUpdate. Uncertainty: ${locationData.uncertainty}")
val session = activeSessionHolder.getSafeActiveSession()
// Emit location update to all rooms in which live location sharing is active // Emit location update to all rooms in which live location sharing is active
roomArgsList.toList().forEach { roomArg -> session?.coroutineScope?.launch(session.coroutineDispatchers.io) {
sendLiveLocation(roomArg.roomId, locationData) roomArgsList.toList().forEach { roomArg ->
sendLiveLocation(roomArg.roomId, locationData)
}
} }
} }
private fun sendLiveLocation(roomId: String, locationData: LocationData) { private suspend fun sendLiveLocation(roomId: String, locationData: LocationData) {
val room = activeSessionHolder.getSafeActiveSession()?.getRoom(roomId) val session = activeSessionHolder.getSafeActiveSession()
val room = session?.getRoom(roomId)
val userId = session?.myUserId
if (room == null || userId == null) {
return
}
room room
?.getStateEvent(EventType.STATE_ROOM_BEACON_INFO.first()) .getLiveLocationBeaconInfo(userId, true)
?.eventId ?.eventId
?.let { ?.let {
room.sendLiveLocation( room.sendLiveLocation(

View File

@ -64,6 +64,7 @@ class LocationSharingServiceConnection @Inject constructor(
override fun onServiceDisconnected(className: ComponentName) { override fun onServiceDisconnected(className: ComponentName) {
isBound = false isBound = false
locationSharingService = null
callback?.onLocationServiceStopped() callback?.onLocationServiceStopped()
} }
} }