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 */
|
/* Overrides onResume from Fragment */
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
// show hide the location error snackbar
|
// if (bound) {
|
||||||
layout.toggleLocationErrorBar(gpsProviderActive, networkProviderActive)
|
// trackerService.addGpsLocationListener()
|
||||||
// set map center
|
// trackerService.addNetworkLocationListener()
|
||||||
layout.centerMap(currentBestLocation)
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -126,6 +126,10 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
super.onPause()
|
||||||
layout.saveState(currentBestLocation)
|
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 {
|
private val connection = object : ServiceConnection {
|
||||||
override fun onServiceConnected(className: ComponentName, service: IBinder) {
|
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
|
val binder = service as TrackerService.LocalBinder
|
||||||
trackerService = binder.service
|
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
|
// register listener for changes in shared preferences
|
||||||
PreferenceManager.getDefaultSharedPreferences(activity as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
PreferenceManager.getDefaultSharedPreferences(activity as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||||
// start listening for location updates
|
// start listening for location updates
|
||||||
|
@ -284,7 +291,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
||||||
*/
|
*/
|
||||||
private val periodicLocationRequestRunnable: Runnable = object : Runnable {
|
private val periodicLocationRequestRunnable: Runnable = object : Runnable {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
// pull values from service
|
// pull current state from service
|
||||||
currentBestLocation = trackerService.currentBestLocation
|
currentBestLocation = trackerService.currentBestLocation
|
||||||
track = trackerService.track
|
track = trackerService.track
|
||||||
gpsProviderActive = trackerService.gpsProviderActive
|
gpsProviderActive = trackerService.gpsProviderActive
|
||||||
|
|
|
@ -64,6 +64,8 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
||||||
var currentBestLocation: Location = LocationHelper.getDefaultLocation()
|
var currentBestLocation: Location = LocationHelper.getDefaultLocation()
|
||||||
var stepCountOffset: Float = 0f
|
var stepCountOffset: Float = 0f
|
||||||
var track: Track = Track()
|
var track: Track = Track()
|
||||||
|
var gpsLocationListenerRegistered: Boolean = false
|
||||||
|
var networkLocationListenerRegistered: Boolean = false
|
||||||
private val binder = LocalBinder()
|
private val binder = LocalBinder()
|
||||||
private val handler: Handler = Handler()
|
private val handler: Handler = Handler()
|
||||||
private lateinit var locationManager: LocationManager
|
private lateinit var locationManager: LocationManager
|
||||||
|
@ -108,21 +110,30 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
||||||
if (intent == null) {
|
if (intent == null) {
|
||||||
if (trackingState == Keys.STATE_TRACKING_ACTIVE) {
|
if (trackingState == Keys.STATE_TRACKING_ACTIVE) {
|
||||||
LogHelper.w(TAG, "Trackbook has been killed by the operating system. Trying to resume recording.")
|
LogHelper.w(TAG, "Trackbook has been killed by the operating system. Trying to resume recording.")
|
||||||
|
addGpsLocationListener()
|
||||||
|
addNetworkLocationListener()
|
||||||
resumeTracking()
|
resumeTracking()
|
||||||
}
|
}
|
||||||
// ACTION STOP
|
// ACTION STOP
|
||||||
} else if (Keys.ACTION_STOP == intent.action) {
|
} else if (Keys.ACTION_STOP == intent.action) {
|
||||||
|
// TODO - do not remove when app is in foreground
|
||||||
|
removeGpsLocationListener()
|
||||||
|
removeNetworkLocationListener()
|
||||||
stopTracking()
|
stopTracking()
|
||||||
// ACTION START
|
// ACTION START
|
||||||
} else if (Keys.ACTION_START == intent.action) {
|
} else if (Keys.ACTION_START == intent.action) {
|
||||||
|
addGpsLocationListener()
|
||||||
|
addNetworkLocationListener()
|
||||||
startTracking()
|
startTracking()
|
||||||
// ACTION RESUME
|
// ACTION RESUME
|
||||||
} else if (Keys.ACTION_RESUME == intent.action) {
|
} else if (Keys.ACTION_RESUME == intent.action) {
|
||||||
|
addGpsLocationListener()
|
||||||
|
addNetworkLocationListener()
|
||||||
resumeTracking()
|
resumeTracking()
|
||||||
}
|
}
|
||||||
|
|
||||||
// START_STICKY is used for services that are explicitly started and stopped as needed
|
// 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 */
|
/* 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 (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||||
if (gpsProviderActive) {
|
if (gpsProviderActive && !gpsLocationListenerRegistered) {
|
||||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f,gpsLocationListener)
|
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f,gpsLocationListener)
|
||||||
|
gpsLocationListenerRegistered = true
|
||||||
LogHelper.v(TAG, "Added GPS location listener.")
|
LogHelper.v(TAG, "Added GPS location listener.")
|
||||||
} else {
|
} else {
|
||||||
LogHelper.w(TAG, "Unable to add GPS location listener.")
|
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 */
|
/* 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 (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)
|
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f,networkLocationListener)
|
||||||
|
networkLocationListenerRegistered = true
|
||||||
LogHelper.v(TAG, "Added Network location listener.")
|
LogHelper.v(TAG, "Added Network location listener.")
|
||||||
} else {
|
} else {
|
||||||
LogHelper.w(TAG, "Unable to add Network location listener.")
|
LogHelper.w(TAG, "Unable to add Network location listener.")
|
||||||
|
@ -300,9 +313,10 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
||||||
|
|
||||||
|
|
||||||
/* Adds location listeners to location manager */
|
/* Adds location listeners to location manager */
|
||||||
private fun removeGpsLocationListener() {
|
fun removeGpsLocationListener() {
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||||
locationManager.removeUpdates(gpsLocationListener)
|
locationManager.removeUpdates(gpsLocationListener)
|
||||||
|
gpsLocationListenerRegistered = false
|
||||||
LogHelper.v(TAG, "Removed GPS location listener.")
|
LogHelper.v(TAG, "Removed GPS location listener.")
|
||||||
} else {
|
} else {
|
||||||
LogHelper.w(TAG, "Unable to remove GPS location listener. Location permission is needed.")
|
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 */
|
/* Adds location listeners to location manager */
|
||||||
private fun removeNetworkLocationListener() {
|
fun removeNetworkLocationListener() {
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||||
locationManager.removeUpdates(gpsLocationListener)
|
locationManager.removeUpdates(gpsLocationListener)
|
||||||
|
networkLocationListenerRegistered = false
|
||||||
LogHelper.v(TAG, "Removed Network location listener.")
|
LogHelper.v(TAG, "Removed Network location listener.")
|
||||||
} else {
|
} else {
|
||||||
LogHelper.w(TAG, "Unable to remove Network location listener. Location permission is needed.")
|
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() {
|
override fun onStartListening() {
|
||||||
super.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)
|
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,31 +80,10 @@ class TrackingToggleTileService(): TileService() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Start tracking */
|
/* Overrides onStopListening from TileService (tile no longer visible) */
|
||||||
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 */
|
|
||||||
override fun onStopListening() {
|
override fun onStopListening() {
|
||||||
super.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)
|
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
|
* Defines the listener for changes in shared preferences
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -168,7 +168,6 @@ object LocationHelper {
|
||||||
} else {
|
} else {
|
||||||
distanceThreshold = Keys.DEFAULT_THRESHOLD_DISTANCE
|
distanceThreshold = Keys.DEFAULT_THRESHOLD_DISTANCE
|
||||||
}
|
}
|
||||||
LogHelper.e(TAG, "distanceThreshold -> $distanceThreshold") // todo remove
|
|
||||||
// location is different when far enough away from previous location
|
// location is different when far enough away from previous location
|
||||||
return calculateDistance(previousLocation, location) > distanceThreshold
|
return calculateDistance(previousLocation, location) > distanceThreshold
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue