Skip to content

Commit f49e7d9

Browse files
author
Onuray Sahin
committed
Code review fixes.
1 parent 023a00d commit f49e7d9

File tree

4 files changed

+65
-39
lines changed

4 files changed

+65
-39
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/state/StateService.kt

+9-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,16 @@ interface StateService {
6868

6969
/**
7070
* Stops sharing live location in the room
71-
* @param beaconInfoStateEvent Initial beacon info state event
71+
* @param userId user id
7272
*/
73-
suspend fun stopLiveLocation(beaconInfoStateEvent: Event)
73+
suspend fun stopLiveLocation(userId: String)
74+
75+
/**
76+
* Returns beacon info state event of a user
77+
* @param userId user id who is sharing location
78+
* @param filterOnlyLive filters only ongoing live location sharing beacons if true else ended event is included
79+
*/
80+
suspend fun getLiveLocationBeaconInfo(userId: String, filterOnlyLive: Boolean): Event?
7481

7582
/**
7683
* Send a state event to the room

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/state/DefaultStateService.kt

+35-17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import androidx.lifecycle.LiveData
2121
import dagger.assisted.Assisted
2222
import dagger.assisted.AssistedFactory
2323
import dagger.assisted.AssistedInject
24+
import org.matrix.android.sdk.api.extensions.orFalse
2425
import org.matrix.android.sdk.api.query.QueryStringValue
2526
import org.matrix.android.sdk.api.session.events.model.Event
2627
import org.matrix.android.sdk.api.session.events.model.EventType
@@ -190,24 +191,41 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
190191
updateJoinRule(RoomJoinRules.RESTRICTED, null, allowEntries)
191192
}
192193

193-
override suspend fun stopLiveLocation(beaconInfoStateEvent: Event) {
194-
beaconInfoStateEvent.getClearContent()?.toModel<LiveLocationBeaconContent>()?.let { content ->
195-
val beaconContent = LiveLocationBeaconContent(
196-
unstableBeaconInfo = BeaconInfo(
197-
description = content.getBestBeaconInfo()?.description,
198-
timeout = content.getBestBeaconInfo()?.timeout,
199-
isLive = false,
200-
),
201-
unstableTimestampAsMilliseconds = System.currentTimeMillis()
202-
).toContent()
203-
204-
beaconInfoStateEvent.stateKey?.let {
205-
sendStateEvent(
206-
eventType = EventType.STATE_ROOM_BEACON_INFO.first(),
207-
body = beaconContent,
208-
stateKey = it
209-
)
194+
override suspend fun stopLiveLocation(userId: String) {
195+
getLiveLocationBeaconInfo(userId, true)?.let { beaconInfoStateEvent ->
196+
beaconInfoStateEvent.getClearContent()?.toModel<LiveLocationBeaconContent>()?.let { content ->
197+
val beaconContent = LiveLocationBeaconContent(
198+
unstableBeaconInfo = BeaconInfo(
199+
description = content.getBestBeaconInfo()?.description,
200+
timeout = content.getBestBeaconInfo()?.timeout,
201+
isLive = false,
202+
),
203+
unstableTimestampAsMilliseconds = System.currentTimeMillis()
204+
).toContent()
205+
206+
beaconInfoStateEvent.stateKey?.let {
207+
sendStateEvent(
208+
eventType = EventType.STATE_ROOM_BEACON_INFO.first(),
209+
body = beaconContent,
210+
stateKey = it
211+
)
212+
}
210213
}
211214
}
212215
}
216+
217+
override suspend fun getLiveLocationBeaconInfo(userId: String, filterOnlyLive: Boolean): Event? {
218+
return EventType.STATE_ROOM_BEACON_INFO
219+
.mapNotNull {
220+
stateEventDataSource.getStateEvent(
221+
roomId = roomId,
222+
eventType = it,
223+
stateKey = QueryStringValue.Equals(userId)
224+
)
225+
}
226+
.firstOrNull { beaconInfoEvent ->
227+
!filterOnlyLive ||
228+
beaconInfoEvent.getClearContent()?.toModel<LiveLocationBeaconContent>()?.getBestBeaconInfo()?.isLive.orFalse()
229+
}
230+
}
213231
}

vector/src/main/java/im/vector/app/features/location/LocationSharingService.kt

+20-20
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import im.vector.app.features.notifications.NotificationUtils
2828
import im.vector.app.features.session.coroutineScope
2929
import kotlinx.coroutines.launch
3030
import kotlinx.parcelize.Parcelize
31-
import org.matrix.android.sdk.api.query.QueryStringValue
3231
import org.matrix.android.sdk.api.session.Session
3332
import org.matrix.android.sdk.api.session.events.model.EventType
3433
import org.matrix.android.sdk.api.session.events.model.toContent
@@ -88,15 +87,15 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
8887
.getSafeActiveSession()
8988
?.let { session ->
9089
session.coroutineScope.launch(session.coroutineDispatchers.io) {
91-
sendBeaconInfo(session, roomArgs)
90+
sendLiveBeaconInfo(session, roomArgs)
9291
}
9392
}
9493
}
9594

9695
return START_STICKY
9796
}
9897

99-
private suspend fun sendBeaconInfo(session: Session, roomArgs: RoomArgs) {
98+
private suspend fun sendLiveBeaconInfo(session: Session, roomArgs: RoomArgs) {
10099
val beaconContent = LiveLocationBeaconContent(
101100
unstableBeaconInfo = BeaconInfo(
102101
timeout = roomArgs.durationMillis,
@@ -134,7 +133,7 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
134133
Timber.i("### LocationSharingService.stopSharingLocation for $roomId")
135134

136135
// Send a new beacon info state by setting live field as false
137-
updateStoppedBeaconInfo(roomId)
136+
sendStoppedBeaconInfo(roomId)
138137

139138
synchronized(roomArgsList) {
140139
roomArgsList.removeAll { it.roomId == roomId }
@@ -145,38 +144,39 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
145144
}
146145
}
147146

148-
private fun updateStoppedBeaconInfo(roomId: String) {
147+
private fun sendStoppedBeaconInfo(roomId: String) {
149148
activeSessionHolder
150149
.getSafeActiveSession()
151150
?.let { session ->
152151
session.coroutineScope.launch(session.coroutineDispatchers.io) {
153-
val room = session.getRoom(roomId)
154-
EventType
155-
.STATE_ROOM_BEACON_INFO
156-
.mapNotNull {
157-
room?.getStateEvent(it, QueryStringValue.Equals(session.myUserId))
158-
}
159-
.firstOrNull()
160-
?.let { beaconInfoEvent ->
161-
room?.stopLiveLocation(beaconInfoEvent)
162-
}
152+
session.getRoom(roomId)?.stopLiveLocation(session.myUserId)
163153
}
164154
}
165155
}
166156

167157
override fun onLocationUpdate(locationData: LocationData) {
168158
Timber.i("### LocationSharingService.onLocationUpdate. Uncertainty: ${locationData.uncertainty}")
169159

160+
val session = activeSessionHolder.getSafeActiveSession()
170161
// Emit location update to all rooms in which live location sharing is active
171-
roomArgsList.toList().forEach { roomArg ->
172-
sendLiveLocation(roomArg.roomId, locationData)
162+
session?.coroutineScope?.launch(session.coroutineDispatchers.io) {
163+
roomArgsList.toList().forEach { roomArg ->
164+
sendLiveLocation(roomArg.roomId, locationData)
165+
}
173166
}
174167
}
175168

176-
private fun sendLiveLocation(roomId: String, locationData: LocationData) {
177-
val room = activeSessionHolder.getSafeActiveSession()?.getRoom(roomId)
169+
private suspend fun sendLiveLocation(roomId: String, locationData: LocationData) {
170+
val session = activeSessionHolder.getSafeActiveSession()
171+
val room = session?.getRoom(roomId)
172+
val userId = session?.myUserId
173+
174+
if (room == null || userId == null) {
175+
return
176+
}
177+
178178
room
179-
?.getStateEvent(EventType.STATE_ROOM_BEACON_INFO.first())
179+
.getLiveLocationBeaconInfo(userId, true)
180180
?.eventId
181181
?.let {
182182
room.sendLiveLocation(

vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class LocationSharingServiceConnection @Inject constructor(
6464

6565
override fun onServiceDisconnected(className: ComponentName) {
6666
isBound = false
67+
locationSharingService = null
6768
callback?.onLocationServiceStopped()
6869
}
6970
}

0 commit comments

Comments
 (0)