was requesting location updates longer than it needed
This commit is contained in:
parent
9591f9f8c0
commit
ded9d77213
4 changed files with 65 additions and 40 deletions
|
@ -115,10 +115,10 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
|||
/* Overrides onResume from Fragment */
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
// show hide the location error snackbar
|
||||
layout.toggleLocationErrorBar(gpsProviderActive, networkProviderActive)
|
||||
// set map center
|
||||
layout.centerMap(currentBestLocation)
|
||||
// if (bound) {
|
||||
// trackerService.addGpsLocationListener()
|
||||
// trackerService.addNetworkLocationListener()
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
|
@ -126,6 +126,10 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
|||
override fun onPause() {
|
||||
super.onPause()
|
||||
layout.saveState(currentBestLocation)
|
||||
if (bound && trackingState != Keys.STATE_TRACKING_ACTIVE) {
|
||||
trackerService.removeGpsLocationListener()
|
||||
trackerService.removeNetworkLocationListener()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -256,10 +260,13 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
|||
*/
|
||||
private val connection = object : ServiceConnection {
|
||||
override fun onServiceConnected(className: ComponentName, service: IBinder) {
|
||||
// We've bound to LocalService, cast the IBinder and get LocalService instance
|
||||
bound = true
|
||||
// get reference to tracker service
|
||||
val binder = service as TrackerService.LocalBinder
|
||||
trackerService = binder.service
|
||||
bound = true
|
||||
// get state of tracking and update button if necessary
|
||||
trackingState = trackerService.trackingState
|
||||
layout.updateRecordingButton(trackingState)
|
||||
// register listener for changes in shared preferences
|
||||
PreferenceManager.getDefaultSharedPreferences(activity as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||
// start listening for location updates
|
||||
|
@ -284,7 +291,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
|||
*/
|
||||
private val periodicLocationRequestRunnable: Runnable = object : Runnable {
|
||||
override fun run() {
|
||||
// pull values from service
|
||||
// pull current state from service
|
||||
currentBestLocation = trackerService.currentBestLocation
|
||||
track = trackerService.track
|
||||
gpsProviderActive = trackerService.gpsProviderActive
|
||||
|
|
|
@ -64,6 +64,8 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
var currentBestLocation: Location = LocationHelper.getDefaultLocation()
|
||||
var stepCountOffset: Float = 0f
|
||||
var track: Track = Track()
|
||||
var gpsLocationListenerRegistered: Boolean = false
|
||||
var networkLocationListenerRegistered: Boolean = false
|
||||
private val binder = LocalBinder()
|
||||
private val handler: Handler = Handler()
|
||||
private lateinit var locationManager: LocationManager
|
||||
|
@ -108,21 +110,30 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
if (intent == null) {
|
||||
if (trackingState == Keys.STATE_TRACKING_ACTIVE) {
|
||||
LogHelper.w(TAG, "Trackbook has been killed by the operating system. Trying to resume recording.")
|
||||
addGpsLocationListener()
|
||||
addNetworkLocationListener()
|
||||
resumeTracking()
|
||||
}
|
||||
// ACTION STOP
|
||||
} else if (Keys.ACTION_STOP == intent.action) {
|
||||
// TODO - do not remove when app is in foreground
|
||||
removeGpsLocationListener()
|
||||
removeNetworkLocationListener()
|
||||
stopTracking()
|
||||
// ACTION START
|
||||
} else if (Keys.ACTION_START == intent.action) {
|
||||
addGpsLocationListener()
|
||||
addNetworkLocationListener()
|
||||
startTracking()
|
||||
// ACTION RESUME
|
||||
} else if (Keys.ACTION_RESUME == intent.action) {
|
||||
addGpsLocationListener()
|
||||
addNetworkLocationListener()
|
||||
resumeTracking()
|
||||
}
|
||||
|
||||
// START_STICKY is used for services that are explicitly started and stopped as needed
|
||||
return Service.START_STICKY
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
|
||||
|
@ -270,10 +281,11 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
|
||||
|
||||
/* Adds a GPS location listener to location manager */
|
||||
private fun addGpsLocationListener() {
|
||||
fun addGpsLocationListener() {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
if (gpsProviderActive) {
|
||||
if (gpsProviderActive && !gpsLocationListenerRegistered) {
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f,gpsLocationListener)
|
||||
gpsLocationListenerRegistered = true
|
||||
LogHelper.v(TAG, "Added GPS location listener.")
|
||||
} else {
|
||||
LogHelper.w(TAG, "Unable to add GPS location listener.")
|
||||
|
@ -285,10 +297,11 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
|
||||
|
||||
/* Adds a Network location listener to location manager */
|
||||
private fun addNetworkLocationListener() {
|
||||
fun addNetworkLocationListener() {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
if (networkProviderActive && !gpsOnly) {
|
||||
if (networkProviderActive && !gpsOnly &&!networkLocationListenerRegistered) {
|
||||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f,networkLocationListener)
|
||||
networkLocationListenerRegistered = true
|
||||
LogHelper.v(TAG, "Added Network location listener.")
|
||||
} else {
|
||||
LogHelper.w(TAG, "Unable to add Network location listener.")
|
||||
|
@ -300,9 +313,10 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
|
||||
|
||||
/* Adds location listeners to location manager */
|
||||
private fun removeGpsLocationListener() {
|
||||
fun removeGpsLocationListener() {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
locationManager.removeUpdates(gpsLocationListener)
|
||||
gpsLocationListenerRegistered = false
|
||||
LogHelper.v(TAG, "Removed GPS location listener.")
|
||||
} else {
|
||||
LogHelper.w(TAG, "Unable to remove GPS location listener. Location permission is needed.")
|
||||
|
@ -311,9 +325,10 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
|
||||
|
||||
/* Adds location listeners to location manager */
|
||||
private fun removeNetworkLocationListener() {
|
||||
fun removeNetworkLocationListener() {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
locationManager.removeUpdates(gpsLocationListener)
|
||||
networkLocationListenerRegistered = false
|
||||
LogHelper.v(TAG, "Removed Network location listener.")
|
||||
} else {
|
||||
LogHelper.w(TAG, "Unable to remove Network location listener. Location permission is needed.")
|
||||
|
|
|
@ -58,10 +58,14 @@ class TrackingToggleTileService(): TileService() {
|
|||
}
|
||||
|
||||
|
||||
/* Overrides onStartListening from TileService */
|
||||
/* Overrides onStartListening from TileService (tile becomes visible) */
|
||||
override fun onStartListening() {
|
||||
super.onStartListening()
|
||||
// tile becomes visible - register listener for changes in shared preferences
|
||||
// get saved tracking state
|
||||
trackingState = PreferencesHelper.loadTrackingState(this)
|
||||
// set up tile
|
||||
updateTile()
|
||||
// register listener for changes in shared preferences
|
||||
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||
}
|
||||
|
||||
|
@ -76,31 +80,10 @@ class TrackingToggleTileService(): TileService() {
|
|||
}
|
||||
|
||||
|
||||
/* Start tracking */
|
||||
private fun startTracking() {
|
||||
val intent = Intent(application, TrackerService::class.java)
|
||||
intent.action = Keys.ACTION_START
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
// ... start service in foreground to prevent it being killed on Oreo
|
||||
application.startForegroundService(intent)
|
||||
} else {
|
||||
application.startService(intent)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Stop tracking */
|
||||
private fun stopTracking() {
|
||||
val intent = Intent(application, TrackerService::class.java)
|
||||
intent.action = Keys.ACTION_STOP
|
||||
application.startService(intent)
|
||||
}
|
||||
|
||||
|
||||
/* Overrides onStopListening from TileService */
|
||||
/* Overrides onStopListening from TileService (tile no longer visible) */
|
||||
override fun onStopListening() {
|
||||
super.onStopListening()
|
||||
// tile no longer visible - unregister listener for changes in shared preferences
|
||||
// unregister listener for changes in shared preferences
|
||||
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||
}
|
||||
|
||||
|
@ -131,6 +114,27 @@ class TrackingToggleTileService(): TileService() {
|
|||
}
|
||||
|
||||
|
||||
/* Start tracking */
|
||||
private fun startTracking() {
|
||||
val intent = Intent(application, TrackerService::class.java)
|
||||
intent.action = Keys.ACTION_START
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
// ... start service in foreground to prevent it being killed on Oreo
|
||||
application.startForegroundService(intent)
|
||||
} else {
|
||||
application.startService(intent)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Stop tracking */
|
||||
private fun stopTracking() {
|
||||
val intent = Intent(application, TrackerService::class.java)
|
||||
intent.action = Keys.ACTION_STOP
|
||||
application.startService(intent)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Defines the listener for changes in shared preferences
|
||||
*/
|
||||
|
|
|
@ -168,7 +168,6 @@ object LocationHelper {
|
|||
} else {
|
||||
distanceThreshold = Keys.DEFAULT_THRESHOLD_DISTANCE
|
||||
}
|
||||
LogHelper.e(TAG, "distanceThreshold -> $distanceThreshold") // todo remove
|
||||
// location is different when far enough away from previous location
|
||||
return calculateDistance(previousLocation, location) > distanceThreshold
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue