2020-01-02 17:00:37 +00:00
|
|
|
/*
|
|
|
|
* TrackerService.kt
|
|
|
|
* Implements the app's movement tracker service
|
|
|
|
* The TrackerService keeps track of the current location
|
|
|
|
*
|
|
|
|
* This file is part of
|
|
|
|
* TRACKBOOK - Movement Recorder for Android
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-20 - Y20K.org
|
|
|
|
* Licensed under the MIT-License
|
|
|
|
* http://opensource.org/licenses/MIT
|
|
|
|
*
|
|
|
|
* Trackbook uses osmdroid - OpenStreetMap-Tools for Android
|
|
|
|
* https://github.com/osmdroid/osmdroid
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package org.y20k.trackbook
|
|
|
|
|
|
|
|
import android.Manifest
|
|
|
|
import android.app.Notification
|
|
|
|
import android.app.NotificationManager
|
|
|
|
import android.app.Service
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
import android.hardware.Sensor
|
|
|
|
import android.hardware.SensorEvent
|
|
|
|
import android.hardware.SensorEventListener
|
|
|
|
import android.hardware.SensorManager
|
|
|
|
import android.location.Location
|
|
|
|
import android.location.LocationListener
|
|
|
|
import android.location.LocationManager
|
|
|
|
import android.os.Binder
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Handler
|
|
|
|
import android.os.IBinder
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
import androidx.preference.PreferenceManager
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import org.y20k.trackbook.core.Track
|
|
|
|
import org.y20k.trackbook.helpers.*
|
|
|
|
import java.util.*
|
|
|
|
import kotlin.coroutines.CoroutineContext
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TrackerService class
|
|
|
|
*/
|
2020-11-18 17:19:52 +00:00
|
|
|
class TrackerService: Service(), CoroutineScope, SensorEventListener {
|
2020-01-02 17:00:37 +00:00
|
|
|
|
|
|
|
/* Define log tag */
|
|
|
|
private val TAG: String = LogHelper.makeLogTag(TrackerService::class.java)
|
|
|
|
|
|
|
|
|
|
|
|
/* Main class variables */
|
2020-01-28 16:39:45 +00:00
|
|
|
var trackingState: Int = Keys.STATE_TRACKING_NOT
|
2020-01-02 17:00:37 +00:00
|
|
|
var gpsProviderActive: Boolean = false
|
|
|
|
var networkProviderActive: Boolean = false
|
2020-08-03 09:39:05 +00:00
|
|
|
var useImperial: Boolean = false
|
|
|
|
var gpsOnly: Boolean = false
|
2021-05-02 15:29:13 +00:00
|
|
|
var accuracyMultiplier: Int = 1
|
2020-01-02 17:00:37 +00:00
|
|
|
var currentBestLocation: Location = LocationHelper.getDefaultLocation()
|
2021-03-15 10:47:25 +00:00
|
|
|
var lastSave: Date = Keys.DEFAULT_DATE
|
2020-08-03 09:39:05 +00:00
|
|
|
var stepCountOffset: Float = 0f
|
2020-03-02 15:45:12 +00:00
|
|
|
var resumed: Boolean = false
|
2020-01-02 17:00:37 +00:00
|
|
|
var track: Track = Track()
|
2020-08-03 09:39:05 +00:00
|
|
|
var gpsLocationListenerRegistered: Boolean = false
|
|
|
|
var networkLocationListenerRegistered: Boolean = false
|
|
|
|
var bound: Boolean = false
|
2020-01-02 17:00:37 +00:00
|
|
|
private val binder = LocalBinder()
|
|
|
|
private val handler: Handler = Handler()
|
2021-05-02 20:51:22 +00:00
|
|
|
private var altitudeValues: SimpleMovingAverageQueue = SimpleMovingAverageQueue(Keys.DEFAULT_ALTITUDE_SMOOTHING_VALUE)
|
2020-01-02 17:00:37 +00:00
|
|
|
private lateinit var locationManager: LocationManager
|
|
|
|
private lateinit var sensorManager: SensorManager
|
|
|
|
private lateinit var notificationManager: NotificationManager
|
|
|
|
private lateinit var notificationHelper: NotificationHelper
|
|
|
|
private lateinit var gpsLocationListener: LocationListener
|
|
|
|
private lateinit var networkLocationListener: LocationListener
|
|
|
|
private lateinit var backgroundJob: Job
|
|
|
|
|
|
|
|
|
|
|
|
/* Overrides coroutineContext variable */
|
|
|
|
override val coroutineContext: CoroutineContext get() = backgroundJob + Dispatchers.Main
|
|
|
|
|
|
|
|
|
|
|
|
/* Overrides onCreate from Service */
|
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
2020-01-09 12:23:26 +00:00
|
|
|
gpsOnly = PreferencesHelper.loadGpsOnly(this)
|
|
|
|
useImperial = PreferencesHelper.loadUseImperialUnits(this)
|
2021-05-02 15:29:13 +00:00
|
|
|
accuracyMultiplier = PreferencesHelper.loadAccuracyMultiplier(this)
|
2021-05-02 20:51:22 +00:00
|
|
|
|
2020-01-02 17:00:37 +00:00
|
|
|
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
|
|
sensorManager = this.getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
|
|
|
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
notificationHelper = NotificationHelper(this)
|
|
|
|
gpsProviderActive = LocationHelper.isGpsEnabled(locationManager)
|
|
|
|
networkProviderActive = LocationHelper.isNetworkEnabled(locationManager)
|
|
|
|
gpsLocationListener = createLocationListener()
|
|
|
|
networkLocationListener = createLocationListener()
|
|
|
|
trackingState = PreferencesHelper.loadTrackingState(this)
|
|
|
|
currentBestLocation = LocationHelper.getLastKnownLocation(this)
|
2020-08-03 09:39:05 +00:00
|
|
|
track = FileHelper.readTrack(this, FileHelper.getTempFileUri(this))
|
2020-01-02 17:00:37 +00:00
|
|
|
backgroundJob = Job()
|
2021-05-02 20:51:22 +00:00
|
|
|
altitudeValues.capacity = PreferencesHelper.loadAltitudeSmoothingValue(this)
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(
|
|
|
|
sharedPreferenceChangeListener
|
|
|
|
)
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Overrides onStartCommand from Service */
|
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
|
|
|
|
|
|
|
// SERVICE RESTART (via START_STICKY)
|
|
|
|
if (intent == null) {
|
|
|
|
if (trackingState == Keys.STATE_TRACKING_ACTIVE) {
|
2021-05-02 20:51:22 +00:00
|
|
|
LogHelper.w(
|
|
|
|
TAG,
|
|
|
|
"Trackbook has been killed by the operating system. Trying to resume recording."
|
|
|
|
)
|
2020-01-02 17:00:37 +00:00
|
|
|
resumeTracking()
|
|
|
|
}
|
2020-08-03 09:39:05 +00:00
|
|
|
// ACTION STOP
|
2020-01-02 17:00:37 +00:00
|
|
|
} else if (Keys.ACTION_STOP == intent.action) {
|
|
|
|
stopTracking()
|
2020-08-03 09:39:05 +00:00
|
|
|
// ACTION START
|
2020-01-02 17:00:37 +00:00
|
|
|
} else if (Keys.ACTION_START == intent.action) {
|
|
|
|
startTracking()
|
2020-08-03 09:39:05 +00:00
|
|
|
// ACTION RESUME
|
2020-01-02 17:00:37 +00:00
|
|
|
} else if (Keys.ACTION_RESUME == intent.action) {
|
|
|
|
resumeTracking()
|
|
|
|
}
|
|
|
|
|
|
|
|
// START_STICKY is used for services that are explicitly started and stopped as needed
|
2020-01-31 22:24:28 +00:00
|
|
|
return START_STICKY
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Overrides onBind from Service */
|
|
|
|
override fun onBind(p0: Intent?): IBinder? {
|
2020-02-06 14:55:15 +00:00
|
|
|
bound = true
|
|
|
|
// start receiving location updates
|
2020-01-09 12:23:26 +00:00
|
|
|
addGpsLocationListener()
|
|
|
|
addNetworkLocationListener()
|
2020-02-06 14:55:15 +00:00
|
|
|
// return reference to this service
|
2020-01-02 17:00:37 +00:00
|
|
|
return binder
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-06 14:55:15 +00:00
|
|
|
/* Overrides onRebind from Service */
|
|
|
|
override fun onRebind(intent: Intent?) {
|
|
|
|
bound = true
|
|
|
|
// start receiving location updates
|
|
|
|
addGpsLocationListener()
|
|
|
|
addNetworkLocationListener()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Overrides onUnbind from Service */
|
|
|
|
override fun onUnbind(intent: Intent?): Boolean {
|
|
|
|
bound = false
|
|
|
|
// stop receiving location updates - if not tracking
|
|
|
|
if (trackingState != Keys.STATE_TRACKING_ACTIVE) {
|
|
|
|
removeGpsLocationListener()
|
|
|
|
removeNetworkLocationListener()
|
|
|
|
}
|
|
|
|
// ensures onRebind is called
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-03-13 12:34:37 +00:00
|
|
|
|
2020-01-02 17:00:37 +00:00
|
|
|
/* Overrides onDestroy from Service */
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
LogHelper.i(TAG, "onDestroy called.")
|
2020-02-06 14:55:15 +00:00
|
|
|
// stop tracking
|
2020-01-02 17:00:37 +00:00
|
|
|
if (trackingState == Keys.STATE_TRACKING_ACTIVE) stopTracking()
|
2020-02-06 14:55:15 +00:00
|
|
|
// remove notification
|
2020-01-02 17:00:37 +00:00
|
|
|
stopForeground(true)
|
2020-02-06 14:55:15 +00:00
|
|
|
// stop listening for changes in shared preferences
|
2021-05-02 20:51:22 +00:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(
|
|
|
|
sharedPreferenceChangeListener
|
|
|
|
)
|
2020-02-06 14:55:15 +00:00
|
|
|
// stop receiving location updates
|
2020-01-09 12:23:26 +00:00
|
|
|
removeGpsLocationListener()
|
|
|
|
removeNetworkLocationListener()
|
2020-02-06 14:55:15 +00:00
|
|
|
// cancel background job
|
2020-01-02 17:00:37 +00:00
|
|
|
backgroundJob.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Overrides onAccuracyChanged from SensorEventListener */
|
|
|
|
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
|
|
|
|
LogHelper.v(TAG, "Accuracy changed: $accuracy")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Overrides onSensorChanged from SensorEventListener */
|
|
|
|
override fun onSensorChanged(sensorEvent: SensorEvent?) {
|
2020-08-03 09:39:05 +00:00
|
|
|
var steps: Float = 0f
|
2020-01-02 17:00:37 +00:00
|
|
|
if (sensorEvent != null) {
|
|
|
|
if (stepCountOffset == 0f) {
|
|
|
|
// store steps previously recorded by the system
|
2020-08-03 09:39:05 +00:00
|
|
|
stepCountOffset = (sensorEvent.values[0] - 1) - track.stepCount // subtract any steps recorded during this session in case the app was killed
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
|
|
|
// calculate step count - subtract steps previously recorded
|
|
|
|
steps = sensorEvent.values[0] - stepCountOffset
|
|
|
|
}
|
|
|
|
// update step count in track
|
|
|
|
track.stepCount = steps
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Resume tracking after stop/pause */
|
|
|
|
fun resumeTracking() {
|
|
|
|
// load temp track - returns an empty track if not available
|
2020-08-03 09:39:05 +00:00
|
|
|
track = FileHelper.readTrack(this, FileHelper.getTempFileUri(this))
|
2020-01-02 17:00:37 +00:00
|
|
|
// try to mark last waypoint as stopover
|
|
|
|
if (track.wayPoints.size > 0) {
|
|
|
|
val lastWayPointIndex = track.wayPoints.size - 1
|
2020-11-18 17:19:52 +00:00
|
|
|
track.wayPoints[lastWayPointIndex].isStopOver = true
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
2020-03-02 15:45:12 +00:00
|
|
|
// set resumed flag
|
|
|
|
resumed = true
|
2020-02-18 16:09:54 +00:00
|
|
|
// calculate length of recording break
|
2020-08-03 09:39:05 +00:00
|
|
|
track.recordingPaused = track.recordingPaused + TrackHelper.calculateDurationOfPause(track.recordingStop)
|
2020-01-02 17:00:37 +00:00
|
|
|
// start tracking
|
|
|
|
startTracking(newTrack = false)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Start tracking location */
|
|
|
|
fun startTracking(newTrack: Boolean = true) {
|
2020-02-06 14:55:15 +00:00
|
|
|
// start receiving location updates
|
|
|
|
addGpsLocationListener()
|
|
|
|
addNetworkLocationListener()
|
|
|
|
// set up new track
|
2020-01-02 17:00:37 +00:00
|
|
|
if (newTrack) {
|
2020-01-31 20:15:33 +00:00
|
|
|
track = Track()
|
2020-01-02 17:00:37 +00:00
|
|
|
track.recordingStart = GregorianCalendar.getInstance().time
|
|
|
|
track.recordingStop = track.recordingStart
|
|
|
|
track.name = DateTimeHelper.convertToReadableDate(track.recordingStart)
|
|
|
|
stepCountOffset = 0f
|
|
|
|
}
|
2020-02-06 14:55:15 +00:00
|
|
|
// set state
|
2020-01-02 17:00:37 +00:00
|
|
|
trackingState = Keys.STATE_TRACKING_ACTIVE
|
|
|
|
PreferencesHelper.saveTrackingState(this, trackingState)
|
2020-02-06 14:55:15 +00:00
|
|
|
// start recording steps and location fixes
|
2020-01-02 17:00:37 +00:00
|
|
|
startStepCounter()
|
|
|
|
handler.postDelayed(periodicTrackUpdate, 0)
|
2020-02-06 14:55:15 +00:00
|
|
|
// show notification
|
2020-01-02 17:00:37 +00:00
|
|
|
startForeground(Keys.TRACKER_SERVICE_NOTIFICATION_ID, displayNotification())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Stop tracking location */
|
|
|
|
fun stopTracking() {
|
2021-03-15 10:47:25 +00:00
|
|
|
// save temp track
|
2020-01-02 17:00:37 +00:00
|
|
|
track.recordingStop = GregorianCalendar.getInstance().time
|
2021-03-15 10:47:25 +00:00
|
|
|
GlobalScope.launch { FileHelper.saveTempTrackSuspended(this@TrackerService, track) }
|
|
|
|
// save state
|
2020-01-02 17:00:37 +00:00
|
|
|
trackingState = Keys.STATE_TRACKING_STOPPED
|
|
|
|
PreferencesHelper.saveTrackingState(this, trackingState)
|
2021-05-08 19:59:14 +00:00
|
|
|
// reset altitude values queue
|
|
|
|
altitudeValues.reset()
|
2020-02-06 14:55:15 +00:00
|
|
|
// stop recording steps and location fixes
|
2020-01-02 17:00:37 +00:00
|
|
|
sensorManager.unregisterListener(this)
|
|
|
|
handler.removeCallbacks(periodicTrackUpdate)
|
2020-02-06 14:55:15 +00:00
|
|
|
// update notification
|
2020-01-02 17:00:37 +00:00
|
|
|
displayNotification()
|
|
|
|
stopForeground(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Clear track recording */
|
|
|
|
fun clearTrack() {
|
|
|
|
track = Track()
|
|
|
|
FileHelper.deleteTempFile(this)
|
2020-01-28 16:39:45 +00:00
|
|
|
trackingState = Keys.STATE_TRACKING_NOT
|
2020-01-02 17:00:37 +00:00
|
|
|
PreferencesHelper.saveTrackingState(this, trackingState)
|
|
|
|
stopForeground(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-15 10:47:25 +00:00
|
|
|
// /* Saves track recording to storage */ // todo remove
|
|
|
|
// fun saveTrack() {
|
|
|
|
// // save track using "deferred await"
|
|
|
|
// launch {
|
|
|
|
// // step 1: create and store filenames for json and gpx files
|
|
|
|
// track.trackUriString = FileHelper.getTrackFileUri(this@TrackerService, track).toString()
|
|
|
|
// track.gpxUriString = FileHelper.getGpxFileUri(this@TrackerService, track).toString()
|
|
|
|
// // step 2: save track
|
|
|
|
// FileHelper.saveTrackSuspended(track, saveGpxToo = true)
|
|
|
|
// // step 3: save tracklist
|
|
|
|
// FileHelper.addTrackAndSaveTracklistSuspended(this@TrackerService, track)
|
|
|
|
// // step 3: clear track
|
|
|
|
// clearTrack()
|
|
|
|
// }
|
|
|
|
// }
|
2020-01-02 17:00:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Creates location listener */
|
|
|
|
private fun createLocationListener(): LocationListener {
|
|
|
|
return object : LocationListener {
|
|
|
|
override fun onLocationChanged(location: Location) {
|
|
|
|
// update currentBestLocation if a better location is available
|
|
|
|
if (LocationHelper.isBetterLocation(location, currentBestLocation)) {
|
|
|
|
currentBestLocation = location
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onProviderEnabled(provider: String) {
|
|
|
|
LogHelper.v(TAG, "onProviderEnabled $provider")
|
|
|
|
when (provider) {
|
2021-05-02 20:51:22 +00:00
|
|
|
LocationManager.GPS_PROVIDER -> gpsProviderActive = LocationHelper.isGpsEnabled(
|
|
|
|
locationManager
|
|
|
|
)
|
|
|
|
LocationManager.NETWORK_PROVIDER -> networkProviderActive =
|
|
|
|
LocationHelper.isNetworkEnabled(
|
|
|
|
locationManager
|
|
|
|
)
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onProviderDisabled(provider: String) {
|
|
|
|
LogHelper.v(TAG, "onProviderDisabled $provider")
|
|
|
|
when (provider) {
|
2021-05-02 20:51:22 +00:00
|
|
|
LocationManager.GPS_PROVIDER -> gpsProviderActive = LocationHelper.isGpsEnabled(
|
|
|
|
locationManager
|
|
|
|
)
|
|
|
|
LocationManager.NETWORK_PROVIDER -> networkProviderActive =
|
|
|
|
LocationHelper.isNetworkEnabled(
|
|
|
|
locationManager
|
|
|
|
)
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {
|
|
|
|
// deprecated method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-09 12:23:26 +00:00
|
|
|
/* Adds a GPS location listener to location manager */
|
2020-02-06 14:55:15 +00:00
|
|
|
private fun addGpsLocationListener() {
|
|
|
|
// check if already registered
|
|
|
|
if (!gpsLocationListenerRegistered) {
|
|
|
|
// check if Network provider is available
|
2020-09-02 14:00:08 +00:00
|
|
|
gpsProviderActive = LocationHelper.isGpsEnabled(locationManager)
|
2020-02-06 14:55:15 +00:00
|
|
|
if (gpsProviderActive) {
|
|
|
|
// check for location permission
|
2021-05-02 20:51:22 +00:00
|
|
|
if (ContextCompat.checkSelfPermission(
|
|
|
|
this,
|
|
|
|
Manifest.permission.ACCESS_FINE_LOCATION
|
|
|
|
) == PackageManager.PERMISSION_GRANTED) {
|
2020-02-06 14:55:15 +00:00
|
|
|
// adds GPS location listener
|
2021-05-02 20:51:22 +00:00
|
|
|
locationManager.requestLocationUpdates(
|
|
|
|
LocationManager.GPS_PROVIDER,
|
|
|
|
0,
|
|
|
|
0f,
|
|
|
|
gpsLocationListener
|
|
|
|
)
|
2020-02-06 14:55:15 +00:00
|
|
|
gpsLocationListenerRegistered = true
|
|
|
|
LogHelper.v(TAG, "Added GPS location listener.")
|
|
|
|
} else {
|
2021-05-02 20:51:22 +00:00
|
|
|
LogHelper.w(
|
|
|
|
TAG,
|
|
|
|
"Unable to add GPS location listener. Location permission is not granted."
|
|
|
|
)
|
2020-02-06 14:55:15 +00:00
|
|
|
}
|
2020-01-09 12:23:26 +00:00
|
|
|
} else {
|
|
|
|
LogHelper.w(TAG, "Unable to add GPS location listener.")
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
2020-01-09 12:23:26 +00:00
|
|
|
} else {
|
2020-02-06 14:55:15 +00:00
|
|
|
LogHelper.v(TAG, "Skipping registration. GPS location listener has already been added.")
|
2020-01-09 12:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Adds a Network location listener to location manager */
|
2020-02-06 14:55:15 +00:00
|
|
|
private fun addNetworkLocationListener() {
|
|
|
|
// check if already registered
|
|
|
|
if (!networkLocationListenerRegistered) {
|
|
|
|
// check if Network provider is available
|
2020-09-02 14:00:08 +00:00
|
|
|
networkProviderActive = LocationHelper.isNetworkEnabled(locationManager)
|
2020-02-06 14:55:15 +00:00
|
|
|
if (networkProviderActive && !gpsOnly) {
|
|
|
|
// check for location permission
|
2021-05-02 20:51:22 +00:00
|
|
|
if (ContextCompat.checkSelfPermission(
|
|
|
|
this,
|
|
|
|
Manifest.permission.ACCESS_FINE_LOCATION
|
|
|
|
) == PackageManager.PERMISSION_GRANTED) {
|
2020-02-06 14:55:15 +00:00
|
|
|
// adds Network location listener
|
2021-05-02 20:51:22 +00:00
|
|
|
locationManager.requestLocationUpdates(
|
|
|
|
LocationManager.NETWORK_PROVIDER,
|
|
|
|
0,
|
|
|
|
0f,
|
|
|
|
networkLocationListener
|
|
|
|
)
|
2020-02-06 14:55:15 +00:00
|
|
|
networkLocationListenerRegistered = true
|
|
|
|
LogHelper.v(TAG, "Added Network location listener.")
|
|
|
|
} else {
|
2021-05-02 20:51:22 +00:00
|
|
|
LogHelper.w(
|
|
|
|
TAG,
|
|
|
|
"Unable to add Network location listener. Location permission is not granted."
|
|
|
|
)
|
2020-02-06 14:55:15 +00:00
|
|
|
}
|
2020-01-09 12:23:26 +00:00
|
|
|
} else {
|
|
|
|
LogHelper.w(TAG, "Unable to add Network location listener.")
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-05-02 20:51:22 +00:00
|
|
|
LogHelper.v(
|
|
|
|
TAG,
|
|
|
|
"Skipping registration. Network location listener has already been added."
|
|
|
|
)
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-09 12:23:26 +00:00
|
|
|
/* Adds location listeners to location manager */
|
2020-01-31 22:24:28 +00:00
|
|
|
fun removeGpsLocationListener() {
|
2020-08-03 09:39:05 +00:00
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
2020-01-09 12:23:26 +00:00
|
|
|
locationManager.removeUpdates(gpsLocationListener)
|
2020-01-31 22:24:28 +00:00
|
|
|
gpsLocationListenerRegistered = false
|
2020-01-09 12:23:26 +00:00
|
|
|
LogHelper.v(TAG, "Removed GPS location listener.")
|
|
|
|
} else {
|
2021-05-02 20:51:22 +00:00
|
|
|
LogHelper.w(
|
|
|
|
TAG,
|
|
|
|
"Unable to remove GPS location listener. Location permission is needed."
|
|
|
|
)
|
2020-01-09 12:23:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Adds location listeners to location manager */
|
2020-01-31 22:24:28 +00:00
|
|
|
fun removeNetworkLocationListener() {
|
2020-08-03 09:39:05 +00:00
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
2021-04-08 04:45:42 +00:00
|
|
|
locationManager.removeUpdates(networkLocationListener)
|
2020-01-31 22:24:28 +00:00
|
|
|
networkLocationListenerRegistered = false
|
2020-01-09 12:23:26 +00:00
|
|
|
LogHelper.v(TAG, "Removed Network location listener.")
|
|
|
|
} else {
|
2021-05-02 20:51:22 +00:00
|
|
|
LogHelper.w(
|
|
|
|
TAG,
|
|
|
|
"Unable to remove Network location listener. Location permission is needed."
|
|
|
|
)
|
2020-01-09 12:23:26 +00:00
|
|
|
}
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Registers a step counter listener */
|
|
|
|
private fun startStepCounter() {
|
2021-05-17 13:44:11 +00:00
|
|
|
val stepCounterAvailable = sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER), SensorManager.SENSOR_DELAY_UI)
|
2020-01-02 17:00:37 +00:00
|
|
|
if (!stepCounterAvailable) {
|
|
|
|
LogHelper.w(TAG, "Pedometer sensor not available.")
|
|
|
|
track.stepCount = -1f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Displays / updates notification */
|
|
|
|
private fun displayNotification(): Notification {
|
2021-05-02 20:51:22 +00:00
|
|
|
val notification: Notification = notificationHelper.createNotification(
|
|
|
|
trackingState,
|
|
|
|
track.length,
|
|
|
|
track.duration,
|
|
|
|
useImperial
|
|
|
|
)
|
2020-01-02 17:00:37 +00:00
|
|
|
notificationManager.notify(Keys.TRACKER_SERVICE_NOTIFICATION_ID, notification)
|
|
|
|
return notification
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Defines the listener for changes in shared preferences
|
|
|
|
*/
|
2020-08-03 09:39:05 +00:00
|
|
|
private val sharedPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
2020-01-02 17:00:37 +00:00
|
|
|
when (key) {
|
2020-02-06 14:55:15 +00:00
|
|
|
// preference "Restrict to GPS"
|
2020-01-09 12:23:26 +00:00
|
|
|
Keys.PREF_GPS_ONLY -> {
|
|
|
|
gpsOnly = PreferencesHelper.loadGpsOnly(this@TrackerService)
|
|
|
|
when (gpsOnly) {
|
|
|
|
true -> removeNetworkLocationListener()
|
|
|
|
false -> addNetworkLocationListener()
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 14:55:15 +00:00
|
|
|
// preference "Use Imperial Measurements"
|
2020-01-09 12:23:26 +00:00
|
|
|
Keys.PREF_USE_IMPERIAL_UNITS -> {
|
|
|
|
useImperial = PreferencesHelper.loadUseImperialUnits(this@TrackerService)
|
|
|
|
}
|
2021-05-02 15:29:13 +00:00
|
|
|
// preference "Recording Accuracy"
|
|
|
|
Keys.PREF_RECORDING_ACCURACY_HIGH -> {
|
|
|
|
accuracyMultiplier = PreferencesHelper.loadAccuracyMultiplier(this@TrackerService)
|
2020-01-09 12:23:26 +00:00
|
|
|
}
|
2020-01-02 17:00:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* End of declaration
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inner class: Local Binder that returns this service
|
|
|
|
*/
|
|
|
|
inner class LocalBinder : Binder() {
|
|
|
|
val service: TrackerService = this@TrackerService
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* End of inner class
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Runnable: Periodically track updates (if recording active)
|
|
|
|
*/
|
|
|
|
private val periodicTrackUpdate: Runnable = object : Runnable {
|
|
|
|
override fun run() {
|
|
|
|
// add waypoint to track - step count is continuously updated in onSensorChanged
|
2021-05-02 20:51:22 +00:00
|
|
|
val result: Pair<Boolean, Track> = TrackHelper.addWayPointToTrack(track, currentBestLocation, accuracyMultiplier, resumed)
|
|
|
|
// get results
|
|
|
|
val successfullyAdded: Boolean = result.first
|
|
|
|
track = result.second
|
2021-03-15 10:47:25 +00:00
|
|
|
// check, if waypoint was added
|
2021-05-02 20:51:22 +00:00
|
|
|
if (successfullyAdded) {
|
2021-03-15 10:47:25 +00:00
|
|
|
// reset resumed flag, if necessary
|
|
|
|
if (resumed) {
|
|
|
|
resumed = false
|
|
|
|
}
|
2021-05-02 20:51:22 +00:00
|
|
|
|
|
|
|
// store previous smoothed altitude
|
2021-05-03 07:26:29 +00:00
|
|
|
val previousAltitude: Double = altitudeValues.getAverage()
|
2021-05-02 20:51:22 +00:00
|
|
|
// put current altitude into queue
|
2021-05-03 12:58:58 +00:00
|
|
|
val currentBestLocationAltitude: Double = currentBestLocation.altitude
|
|
|
|
if (currentBestLocationAltitude != Keys.DEFAULT_ALTITUDE) altitudeValues.add(currentBestLocationAltitude)
|
2021-05-03 10:49:15 +00:00
|
|
|
// TODO remove
|
|
|
|
// uncomment to use test altitude values - useful if testing wirth an emulator
|
2021-05-08 19:59:14 +00:00
|
|
|
//altitudeValues.add(getTestAltitude()) // TODO remove
|
2021-05-03 10:49:15 +00:00
|
|
|
// TODO remove
|
|
|
|
|
2021-05-08 19:59:14 +00:00
|
|
|
// only start calculating elevation differences, if enough data has been added to queue
|
|
|
|
if (altitudeValues.prepared) {
|
|
|
|
// get current smoothed altitude
|
|
|
|
val currentAltitude: Double = altitudeValues.getAverage()
|
|
|
|
// calculate and store elevation differences
|
|
|
|
track = LocationHelper.calculateElevationDifferences(currentAltitude, previousAltitude, track)
|
|
|
|
// TODO remove
|
|
|
|
LogHelper.d(TAG, "Elevation Calculation || prev = $previousAltitude | curr = $currentAltitude | pos = ${track.positiveElevation} | neg = ${track.negativeElevation}")
|
|
|
|
// TODO remove
|
|
|
|
}
|
2021-05-03 10:49:15 +00:00
|
|
|
|
2021-03-15 10:47:25 +00:00
|
|
|
// save a temp track
|
|
|
|
val now: Date = GregorianCalendar.getInstance().time
|
|
|
|
if (now.time - lastSave.time > Keys.SAVE_TEMP_TRACK_INTERVAL) {
|
|
|
|
lastSave = now
|
2021-05-02 20:51:22 +00:00
|
|
|
GlobalScope.launch { FileHelper.saveTempTrackSuspended(
|
|
|
|
this@TrackerService,
|
|
|
|
track
|
|
|
|
) }
|
2021-03-15 10:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-02 17:00:37 +00:00
|
|
|
// update notification
|
|
|
|
displayNotification()
|
2021-03-15 10:47:25 +00:00
|
|
|
// re-run this in set interval
|
2020-01-02 17:00:37 +00:00
|
|
|
handler.postDelayed(this, Keys.ADD_WAYPOINT_TO_TRACK_INTERVAL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* End of declaration
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-05-02 20:51:22 +00:00
|
|
|
/* Simple queue that evicts older elements and holds an average */
|
|
|
|
/* Credit: CircularQueue https://stackoverflow.com/a/51923797 */
|
|
|
|
class SimpleMovingAverageQueue(var capacity: Int) : LinkedList<Double>() {
|
2021-05-08 19:59:14 +00:00
|
|
|
var prepared: Boolean = false
|
2021-05-02 20:51:22 +00:00
|
|
|
private var sum: Double = 0.0
|
|
|
|
override fun add(element: Double): Boolean {
|
2021-05-08 19:59:14 +00:00
|
|
|
prepared = this.size + 1 >= Keys.MIN_NUMBER_OF_WAYPOINTS_FOR_ELEVATION_CALCULATION
|
2021-05-02 20:51:22 +00:00
|
|
|
if (this.size >= capacity) {
|
|
|
|
sum -= this.first
|
|
|
|
removeFirst()
|
|
|
|
}
|
2021-05-03 07:26:29 +00:00
|
|
|
sum += element
|
2021-05-02 20:51:22 +00:00
|
|
|
return super.add(element)
|
|
|
|
}
|
2021-05-03 10:49:15 +00:00
|
|
|
fun getAverage(): Double = sum / this.size
|
2021-05-08 19:59:14 +00:00
|
|
|
fun reset() {
|
|
|
|
this.clear()
|
|
|
|
prepared = false
|
|
|
|
sum = 0.0
|
|
|
|
}
|
2021-05-03 10:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO remove
|
|
|
|
val testAltitudes: Array<Double> = arrayOf(352.4349365234375, 358.883544921875, 358.6827392578125, 357.31396484375, 354.27459716796875, 354.573486328125, 354.388916015625, 354.6697998046875, 356.534912109375, 355.2772216796875, 356.21246337890625, 352.3499755859375, 350.37646484375, 351.2098388671875, 350.5213623046875, 350.5145263671875, 350.1728515625, 350.9075927734375, 351.5965576171875, 349.55767822265625, 351.548583984375, 357.1195068359375, 362.18634033203125, 366.3153076171875, 366.2218017578125, 362.1046142578125, 357.48291015625, 356.78570556640625, 353.7734375, 352.53936767578125, 351.8125, 353.1099853515625, 354.93035888671875, 355.4337158203125, 354.83270263671875, 352.9859619140625, 352.3006591796875, 351.63470458984375, 350.2501220703125, 351.75726318359375, 350.87664794921875, 350.4185791015625, 350.51568603515625, 349.5537109375, 345.2874755859375, 345.57196044921875, 349.99658203125, 353.3822021484375, 355.19061279296875, 359.1099853515625, 361.74365234375, 363.313232421875, 362.026611328125, 363.20703125, 363.2508544921875, 362.5870361328125, 362.521240234375)
|
|
|
|
var testCounter: Int = 0
|
|
|
|
fun getTestAltitude(): Double {
|
|
|
|
if (testCounter >= testAltitudes.size) testCounter = 0
|
|
|
|
val testAltitude: Double = testAltitudes[testCounter]
|
|
|
|
testCounter ++
|
|
|
|
return testAltitude
|
2021-05-02 20:51:22 +00:00
|
|
|
}
|
2021-05-03 10:49:15 +00:00
|
|
|
// TODO remove
|
2021-05-02 20:51:22 +00:00
|
|
|
|
|
|
|
|
2021-05-02 15:29:13 +00:00
|
|
|
}
|