@@ -3,19 +3,19 @@ package mil.nga.giat.mage.location
33import android.app.NotificationChannel
44import android.app.NotificationManager
55import android.app.PendingIntent
6- import android.app.Service
76import android.content.Context
87import android.content.Intent
98import android.content.SharedPreferences
109import android.location.Location
1110import android.location.LocationManager
12- import android.os.Build
1311import android.util.Log
1412import androidx.core.app.NotificationCompat
1513import androidx.lifecycle.LifecycleService
1614import androidx.lifecycle.Observer
1715import androidx.lifecycle.lifecycleScope
1816import dagger.hilt.android.AndroidEntryPoint
17+ import kotlinx.coroutines.channels.Channel
18+ import kotlinx.coroutines.flow.receiveAsFlow
1919import kotlinx.coroutines.launch
2020import mil.nga.giat.mage.MageApplication
2121import mil.nga.giat.mage.R
@@ -34,6 +34,7 @@ open class LocationReportingService : LifecycleService(), Observer<Location>, Sh
3434 private var shouldReportLocation: Boolean = false
3535 private var locationPushFrequency: Long = 0
3636 private var oldestLocationTime: Long = 0
37+ private lateinit var locationChannel: Channel <Location >
3738
3839 companion object {
3940 private val LOG_NAME = LocationReportingService ::class .java.name
@@ -57,12 +58,10 @@ open class LocationReportingService : LifecycleService(), Observer<Location>, Sh
5758 locationPushFrequency = getLocationPushFrequency()
5859 shouldReportLocation = getShouldReportLocation()
5960
60- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
61- val notificationManager = getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
62- val channel = NotificationChannel (NOTIFICATION_CHANNEL_ID , " MAGE" , NotificationManager .IMPORTANCE_MIN )
63- channel.setShowBadge(true )
64- notificationManager.createNotificationChannel(channel)
65- }
61+ val notificationManager = getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
62+ val notificationChannel = NotificationChannel (NOTIFICATION_CHANNEL_ID , " MAGE" , NotificationManager .IMPORTANCE_MIN )
63+ notificationChannel.setShowBadge(true )
64+ notificationManager.createNotificationChannel(notificationChannel)
6665
6766 val intent = Intent (applicationContext, LoginActivity ::class .java)
6867 intent.putExtra(" LOGOUT" , true )
@@ -77,6 +76,13 @@ open class LocationReportingService : LifecycleService(), Observer<Location>, Sh
7776 .addAction(R .drawable.ic_power_settings_new_white_24dp, " Logout" , pendingIntent)
7877 .build()
7978
79+ locationChannel = Channel (Channel .CONFLATED )
80+ lifecycleScope.launch {
81+ locationChannel.receiveAsFlow().collect { location ->
82+ pushLocations(location)
83+ }
84+ }
85+
8086 startForeground(NOTIFICATION_ID , notification)
8187 }
8288
@@ -85,34 +91,37 @@ open class LocationReportingService : LifecycleService(), Observer<Location>, Sh
8591
8692 locationProvider.observe(this , this )
8793
88- return Service . START_STICKY
94+ return START_STICKY
8995 }
9096
9197 override fun onDestroy () {
9298 super .onDestroy()
9399
94100 locationProvider.removeObserver(this )
95-
101+ locationChannel.close()
96102 preferences.unregisterOnSharedPreferenceChangeListener(this )
97103 }
98104
99- override fun onChanged (location : Location ) {
100- if (shouldReportLocation && location? .provider == LocationManager .GPS_PROVIDER ) {
105+ override fun onChanged (value : Location ) {
106+ if (shouldReportLocation && value .provider == LocationManager .GPS_PROVIDER ) {
101107 Log .v(LOG_NAME , " GPS location changed" )
102108
103109 lifecycleScope.launch {
104- locationRepository.saveLocation(location)
105-
106- if (oldestLocationTime == 0L ) {
107- oldestLocationTime = location.time
108- }
109-
110- if (! locationAccess.isPreciseLocationGranted() || (location.time - oldestLocationTime > locationPushFrequency)) {
111- val success = locationRepository.pushLocations()
112- if (success) {
113- oldestLocationTime = 0
114- }
115- }
110+ locationRepository.saveLocation(value)
111+ locationChannel.send(value)
112+ }
113+ }
114+ }
115+
116+ private suspend fun pushLocations (location : Location ) {
117+ if (oldestLocationTime == 0L ) {
118+ oldestLocationTime = location.time
119+ }
120+
121+ if (! locationAccess.isPreciseLocationGranted() || (location.time - oldestLocationTime > locationPushFrequency)) {
122+ val success = locationRepository.pushLocations()
123+ if (success) {
124+ oldestLocationTime = 0
116125 }
117126 }
118127 }
0 commit comments