From f56c315207bb9c8ab058d2cdc321fcb70ec71c21 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Tue, 26 Jul 2022 14:33:08 +0200 Subject: [PATCH 1/3] Dedicated notification builder and opening map on tap of notification --- vector/src/main/AndroidManifest.xml | 2 +- .../location/LocationSharingFragment.kt | 1 + .../LocationSharingServiceConnection.kt | 1 + .../live/map/LocationLiveMapViewActivity.kt | 10 ++- .../LiveLocationNotificationBuilder.kt | 83 +++++++++++++++++++ .../LocationSharingAndroidService.kt | 9 +- .../notifications/NotificationUtils.kt | 18 +--- 7 files changed, 101 insertions(+), 23 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/location/live/tracking/LiveLocationNotificationBuilder.kt rename vector/src/main/java/im/vector/app/features/location/{ => live/tracking}/LocationSharingAndroidService.kt (96%) diff --git a/vector/src/main/AndroidManifest.xml b/vector/src/main/AndroidManifest.xml index fa89013707..63c713d9e6 100644 --- a/vector/src/main/AndroidManifest.xml +++ b/vector/src/main/AndroidManifest.xml @@ -377,7 +377,7 @@ diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt index 17e53e63d1..ca524a0126 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt @@ -42,6 +42,7 @@ import im.vector.app.features.home.AvatarRenderer import im.vector.app.features.home.room.detail.timeline.helper.MatrixItemColorProvider import im.vector.app.features.location.live.LiveLocationLabsFlagPromotionBottomSheet import im.vector.app.features.location.live.duration.ChooseLiveDurationBottomSheet +import im.vector.app.features.location.live.tracking.LocationSharingAndroidService import im.vector.app.features.location.option.LocationSharingOption import im.vector.app.features.settings.VectorPreferences import org.matrix.android.sdk.api.util.MatrixItem diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt index 3be73e9fd4..9e905060d9 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt @@ -22,6 +22,7 @@ import android.content.Intent import android.content.ServiceConnection import android.os.IBinder import im.vector.app.core.di.ActiveSessionHolder +import im.vector.app.features.location.live.tracking.LocationSharingAndroidService import im.vector.app.features.session.coroutineScope import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.launchIn diff --git a/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewActivity.kt b/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewActivity.kt index c0f07dba57..fabdda49d8 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewActivity.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewActivity.kt @@ -24,6 +24,7 @@ import im.vector.app.R import im.vector.app.core.extensions.addFragment import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.databinding.ActivityLocationSharingBinding +import im.vector.app.features.MainActivity import kotlinx.parcelize.Parcelize @Parcelize @@ -59,10 +60,15 @@ class LocationLiveMapViewActivity : VectorBaseActivity= Build.VERSION_CODES.O) @@ -540,20 +540,6 @@ class NotificationUtils @Inject constructor( return builder.build() } - /** - * Creates a notification that indicates the application is retrieving location even if it is in background or killed. - */ - fun buildLiveLocationSharingNotification(): Notification { - return NotificationCompat.Builder(context, SILENT_NOTIFICATION_CHANNEL_ID) - .setContentTitle(stringProvider.getString(R.string.live_location_sharing_notification_title)) - .setContentText(stringProvider.getString(R.string.live_location_sharing_notification_description)) - .setSmallIcon(R.drawable.ic_attachment_location_live_white) - .setColor(ThemeUtils.getColor(context, android.R.attr.colorPrimary)) - .setCategory(NotificationCompat.CATEGORY_LOCATION_SHARING) - .setContentIntent(buildOpenHomePendingIntentForSummary()) - .build() - } - /** * Creates a notification that indicates the application is capturing the screen. */ From f3ad9e38c3e811271f4d7f07494bd645f0d3c989 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Tue, 26 Jul 2022 15:28:27 +0200 Subject: [PATCH 2/3] Update the notification when starting/stopping new live in room --- .../tracking/LocationSharingAndroidService.kt | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt b/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt index 90bbc7e515..6babc94385 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt @@ -20,6 +20,7 @@ import android.content.Intent import android.os.Binder import android.os.IBinder import android.os.Parcelable +import androidx.core.app.NotificationManagerCompat import dagger.hilt.android.AndroidEntryPoint import im.vector.app.R import im.vector.app.core.di.ActiveSessionHolder @@ -63,13 +64,11 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca private val binder = LocalBinder() - /** - * Keep track of a map between beacon event Id starting the live and RoomArgs. - */ - private val roomArgsMap = mutableMapOf() + private val liveInfoSet = linkedSetOf() var callback: Callback? = null private val jobs = mutableListOf() private var startInProgress = false + private var foregroundModeStarted = false private val _roomIdsOfActiveLives = MutableSharedFlow>(replay = 1) val roomIdsOfActiveLives = _roomIdsOfActiveLives.asSharedFlow() @@ -77,7 +76,6 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca override fun onCreate() { super.onCreate() Timber.i("onCreate") - initLocationTracking() } @@ -104,7 +102,12 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca if (roomArgs != null) { // Show a sticky notification val notification = liveLocationNotificationBuilder.buildLiveLocationSharingNotification(roomArgs.roomId) - startForeground(roomArgs.roomId.hashCode(), notification) + if (foregroundModeStarted) { + NotificationManagerCompat.from(this).notify(FOREGROUND_SERVICE_NOTIFICATION_ID, notification) + } else { + startForeground(FOREGROUND_SERVICE_NOTIFICATION_ID, notification) + foregroundModeStarted = true + } // Send beacon info state event launchWithActiveSession { session -> @@ -149,15 +152,24 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca private fun stopSharingLocation(beaconEventId: String) { Timber.i("stopSharingLocation for beacon $beaconEventId") removeRoomArgs(beaconEventId) + updateNotification() tryToDestroyMe() } + private fun updateNotification() { + if (liveInfoSet.isNotEmpty()) { + val roomId = liveInfoSet.last().roomArgs.roomId + val notification = liveLocationNotificationBuilder.buildLiveLocationSharingNotification(roomId) + NotificationManagerCompat.from(this).notify(FOREGROUND_SERVICE_NOTIFICATION_ID, notification) + } + } + private fun onLocationUpdate(locationData: LocationData) { Timber.i("onLocationUpdate. Uncertainty: ${locationData.uncertainty}") // Emit location update to all rooms in which live location sharing is active - roomArgsMap.toMap().forEach { item -> - sendLiveLocation(item.value.roomId, item.key, locationData) + liveInfoSet.toSet().forEach { liveInfo -> + sendLiveLocation(liveInfo.roomArgs.roomId, liveInfo.beaconEventId, locationData) } } @@ -184,7 +196,7 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca } private fun tryToDestroyMe() { - if (startInProgress.not() && roomArgsMap.isEmpty()) { + if (startInProgress.not() && liveInfoSet.isEmpty()) { Timber.i("Destroying self, time is up for all rooms") stopSelf() } @@ -200,13 +212,14 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca private fun addRoomArgs(beaconEventId: String, roomArgs: RoomArgs) { Timber.i("adding roomArgs for beaconEventId: $beaconEventId") - roomArgsMap[beaconEventId] = roomArgs + liveInfoSet.removeAll { it.beaconEventId == beaconEventId } + liveInfoSet.add(LiveInfo(beaconEventId, roomArgs)) launchWithActiveSession { _roomIdsOfActiveLives.emit(getRoomIdsOfActiveLives()) } } private fun removeRoomArgs(beaconEventId: String) { Timber.i("removing roomArgs for beaconEventId: $beaconEventId") - roomArgsMap.remove(beaconEventId) + liveInfoSet.removeAll { it.beaconEventId == beaconEventId } launchWithActiveSession { _roomIdsOfActiveLives.emit(getRoomIdsOfActiveLives()) } } @@ -235,7 +248,7 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca } fun getRoomIdsOfActiveLives(): Set { - return roomArgsMap.map { it.value.roomId }.toSet() + return liveInfoSet.map { it.roomArgs.roomId }.toSet() } override fun onBind(intent: Intent?): IBinder { @@ -252,5 +265,11 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca companion object { const val EXTRA_ROOM_ARGS = "EXTRA_ROOM_ARGS" + private const val FOREGROUND_SERVICE_NOTIFICATION_ID = 300 } + + private data class LiveInfo( + val beaconEventId: String, + val roomArgs: RoomArgs + ) } From fb82b1d1ce9793d3a7d61aaad8e9363488ec5f23 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Tue, 26 Jul 2022 15:37:08 +0200 Subject: [PATCH 3/3] Adding changelog entry --- changelog.d/6642.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/6642.misc diff --git a/changelog.d/6642.misc b/changelog.d/6642.misc new file mode 100644 index 0000000000..a32b20716a --- /dev/null +++ b/changelog.d/6642.misc @@ -0,0 +1 @@ +[Location Share] Open maximized map on tapping on live sharing notification