mirror of
https://github.com/element-hq/element-android
synced 2024-11-25 02:45:37 +03:00
Track location in foreground service.
This commit is contained in:
parent
c63fc3d6c2
commit
334368083e
4 changed files with 49 additions and 14 deletions
|
@ -25,9 +25,10 @@ import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class LocationSharingService : VectorService() {
|
class LocationSharingService : VectorService(), LocationTracker.Callback {
|
||||||
|
|
||||||
@Inject lateinit var notificationUtils: NotificationUtils
|
@Inject lateinit var notificationUtils: NotificationUtils
|
||||||
|
@Inject lateinit var locationTracker: LocationTracker
|
||||||
|
|
||||||
private var sessionId: String? = null
|
private var sessionId: String? = null
|
||||||
private var roomId: String? = null
|
private var roomId: String? = null
|
||||||
|
@ -36,19 +37,27 @@ class LocationSharingService : VectorService() {
|
||||||
sessionId = intent?.getStringExtra(EXTRA_SESSION_ID)
|
sessionId = intent?.getStringExtra(EXTRA_SESSION_ID)
|
||||||
roomId = intent?.getStringExtra(EXTRA_ROOM_ID)
|
roomId = intent?.getStringExtra(EXTRA_ROOM_ID)
|
||||||
|
|
||||||
Timber.d("LocationSharingService $sessionId - $roomId")
|
|
||||||
|
|
||||||
if (sessionId == null || roomId == null) {
|
if (sessionId == null || roomId == null) {
|
||||||
stopForeground(true)
|
stopForeground(true)
|
||||||
stopSelf()
|
stopSelf()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Show a sticky notification
|
||||||
val notification = notificationUtils.buildLiveLocationSharingNotification()
|
val notification = notificationUtils.buildLiveLocationSharingNotification()
|
||||||
startForeground(roomId!!.hashCode(), notification)
|
startForeground(roomId!!.hashCode(), notification)
|
||||||
|
|
||||||
|
// Start tracking location
|
||||||
|
locationTracker.addCallback(this)
|
||||||
|
locationTracker.start()
|
||||||
|
|
||||||
return START_STICKY
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
locationTracker.removeCallback(this)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onBind(intent: Intent?): IBinder? {
|
override fun onBind(intent: Intent?): IBinder? {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
@ -57,4 +66,13 @@ class LocationSharingService : VectorService() {
|
||||||
const val EXTRA_SESSION_ID = "EXTRA_SESSION_ID"
|
const val EXTRA_SESSION_ID = "EXTRA_SESSION_ID"
|
||||||
const val EXTRA_ROOM_ID = "EXTRA_ROOM_ID"
|
const val EXTRA_ROOM_ID = "EXTRA_ROOM_ID"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onLocationUpdate(locationData: LocationData) {
|
||||||
|
Timber.d("### LocationSharingService.onLocationUpdate: ${locationData.latitude} - ${locationData.longitude}")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLocationProviderIsNotAvailable() {
|
||||||
|
stopForeground(true)
|
||||||
|
stopSelf()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +65,8 @@ class LocationSharingViewModel @AssistedInject constructor(
|
||||||
companion object : MavericksViewModelFactory<LocationSharingViewModel, LocationSharingViewState> by hiltMavericksViewModelFactory()
|
companion object : MavericksViewModelFactory<LocationSharingViewModel, LocationSharingViewState> by hiltMavericksViewModelFactory()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
locationTracker.start(this)
|
locationTracker.addCallback(this)
|
||||||
|
locationTracker.start()
|
||||||
setUserItem()
|
setUserItem()
|
||||||
updatePin()
|
updatePin()
|
||||||
compareTargetAndUserLocation()
|
compareTargetAndUserLocation()
|
||||||
|
@ -112,7 +113,7 @@ class LocationSharingViewModel @AssistedInject constructor(
|
||||||
|
|
||||||
override fun onCleared() {
|
override fun onCleared() {
|
||||||
super.onCleared()
|
super.onCleared()
|
||||||
locationTracker.stop()
|
locationTracker.removeCallback(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun handle(action: LocationSharingAction) {
|
override fun handle(action: LocationSharingAction) {
|
||||||
|
|
|
@ -40,18 +40,17 @@ class LocationTracker @Inject constructor(
|
||||||
fun onLocationProviderIsNotAvailable()
|
fun onLocationProviderIsNotAvailable()
|
||||||
}
|
}
|
||||||
|
|
||||||
private var callback: Callback? = null
|
private var callbacks = mutableListOf<Callback>()
|
||||||
|
|
||||||
private var hasGpsProviderLiveLocation = false
|
private var hasGpsProviderLiveLocation = false
|
||||||
|
|
||||||
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
|
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
|
||||||
fun start(callback: Callback?) {
|
fun start() {
|
||||||
Timber.d("## LocationTracker. start()")
|
Timber.d("## LocationTracker. start()")
|
||||||
hasGpsProviderLiveLocation = false
|
hasGpsProviderLiveLocation = false
|
||||||
this.callback = callback
|
|
||||||
|
|
||||||
if (locationManager == null) {
|
if (locationManager == null) {
|
||||||
callback?.onLocationProviderIsNotAvailable()
|
callbacks.forEach { it.onLocationProviderIsNotAvailable() }
|
||||||
Timber.v("## LocationTracker. LocationManager is not available")
|
Timber.v("## LocationTracker. LocationManager is not available")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -81,7 +80,7 @@ class LocationTracker @Inject constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
?: run {
|
?: run {
|
||||||
callback?.onLocationProviderIsNotAvailable()
|
callbacks.forEach { it.onLocationProviderIsNotAvailable() }
|
||||||
Timber.v("## LocationTracker. There is no location provider available")
|
Timber.v("## LocationTracker. There is no location provider available")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,7 +89,24 @@ class LocationTracker @Inject constructor(
|
||||||
fun stop() {
|
fun stop() {
|
||||||
Timber.d("## LocationTracker. stop()")
|
Timber.d("## LocationTracker. stop()")
|
||||||
locationManager?.removeUpdates(this)
|
locationManager?.removeUpdates(this)
|
||||||
callback = null
|
callbacks.clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addCallback(callback: Callback) {
|
||||||
|
synchronized(callbacks) {
|
||||||
|
if (!callbacks.contains(callback)) {
|
||||||
|
callbacks.add(callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeCallback(callback: Callback) {
|
||||||
|
synchronized(callbacks) {
|
||||||
|
callbacks.remove(callback)
|
||||||
|
if (callbacks.size == 0) {
|
||||||
|
stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onLocationChanged(location: Location) {
|
override fun onLocationChanged(location: Location) {
|
||||||
|
@ -115,12 +131,12 @@ class LocationTracker @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
callback?.onLocationUpdate(location.toLocationData())
|
callbacks.forEach { it.onLocationUpdate(location.toLocationData()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onProviderDisabled(provider: String) {
|
override fun onProviderDisabled(provider: String) {
|
||||||
Timber.d("## LocationTracker. onProviderDisabled: $provider")
|
Timber.d("## LocationTracker. onProviderDisabled: $provider")
|
||||||
callback?.onLocationProviderIsNotAvailable()
|
callbacks.forEach { it.onLocationProviderIsNotAvailable() }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Location.toLocationData(): LocationData {
|
private fun Location.toLocationData(): LocationData {
|
||||||
|
|
|
@ -528,7 +528,7 @@ class NotificationUtils @Inject constructor(private val context: Context,
|
||||||
return NotificationCompat.Builder(context, SILENT_NOTIFICATION_CHANNEL_ID)
|
return NotificationCompat.Builder(context, SILENT_NOTIFICATION_CHANNEL_ID)
|
||||||
.setContentTitle(stringProvider.getString(R.string.live_location_sharing_notification_title))
|
.setContentTitle(stringProvider.getString(R.string.live_location_sharing_notification_title))
|
||||||
.setContentText(stringProvider.getString(R.string.live_location_sharing_notification_description))
|
.setContentText(stringProvider.getString(R.string.live_location_sharing_notification_description))
|
||||||
.setSmallIcon(R.drawable.ic_location_pin)
|
.setSmallIcon(R.drawable.ic_attachment_location_live_white)
|
||||||
.setCategory(NotificationCompat.CATEGORY_LOCATION_SHARING)
|
.setCategory(NotificationCompat.CATEGORY_LOCATION_SHARING)
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue