save temp track only every 9+ seconds (see #96)

This commit is contained in:
y20k 2021-03-15 11:47:25 +01:00
parent dbad7bb1cb
commit c193e8a475
No known key found for this signature in database
GPG key ID: 824D4259F41FAFF6
2 changed files with 35 additions and 19 deletions

View file

@ -97,6 +97,7 @@ object Keys {
const val EMPTY_STRING_RESOURCE: Int = 0 const val EMPTY_STRING_RESOURCE: Int = 0
const val REQUEST_CURRENT_LOCATION_INTERVAL: Long = 1000L // 1 second in milliseconds const val REQUEST_CURRENT_LOCATION_INTERVAL: Long = 1000L // 1 second in milliseconds
const val ADD_WAYPOINT_TO_TRACK_INTERVAL: Long = 1000L // 1 second in milliseconds const val ADD_WAYPOINT_TO_TRACK_INTERVAL: Long = 1000L // 1 second in milliseconds
const val SAVE_TEMP_TRACK_INTERVAL: Long = 9000L // 9 seconds in milliseconds
const val SIGNIFICANT_TIME_DIFFERENCE: Long = 120000L // 2 minutes in milliseconds const val SIGNIFICANT_TIME_DIFFERENCE: Long = 120000L // 2 minutes in milliseconds
const val STOP_OVER_THRESHOLD: Long = 300000L // 5 minutes in milliseconds const val STOP_OVER_THRESHOLD: Long = 300000L // 5 minutes in milliseconds
const val IMPLAUSIBLE_TRACK_START_SPEED: Double = 250.0 // 250 km/h const val IMPLAUSIBLE_TRACK_START_SPEED: Double = 250.0 // 250 km/h

View file

@ -62,6 +62,7 @@ class TrackerService: Service(), CoroutineScope, SensorEventListener {
var gpsOnly: Boolean = false var gpsOnly: Boolean = false
var locationAccuracyThreshold: Int = Keys.DEFAULT_THRESHOLD_LOCATION_ACCURACY var locationAccuracyThreshold: Int = Keys.DEFAULT_THRESHOLD_LOCATION_ACCURACY
var currentBestLocation: Location = LocationHelper.getDefaultLocation() var currentBestLocation: Location = LocationHelper.getDefaultLocation()
var lastSave: Date = Keys.DEFAULT_DATE
var stepCountOffset: Float = 0f var stepCountOffset: Float = 0f
var resumed: Boolean = false var resumed: Boolean = false
var track: Track = Track() var track: Track = Track()
@ -247,8 +248,10 @@ class TrackerService: Service(), CoroutineScope, SensorEventListener {
/* Stop tracking location */ /* Stop tracking location */
fun stopTracking() { fun stopTracking() {
// save state // save temp track
track.recordingStop = GregorianCalendar.getInstance().time track.recordingStop = GregorianCalendar.getInstance().time
GlobalScope.launch { FileHelper.saveTempTrackSuspended(this@TrackerService, track) }
// save state
trackingState = Keys.STATE_TRACKING_STOPPED trackingState = Keys.STATE_TRACKING_STOPPED
PreferencesHelper.saveTrackingState(this, trackingState) PreferencesHelper.saveTrackingState(this, trackingState)
// stop recording steps and location fixes // stop recording steps and location fixes
@ -270,21 +273,21 @@ class TrackerService: Service(), CoroutineScope, SensorEventListener {
} }
/* Saves track recording to storage */ // /* Saves track recording to storage */ // todo remove
fun saveTrack() { // fun saveTrack() {
// save track using "deferred await" // // save track using "deferred await"
launch { // launch {
// step 1: create and store filenames for json and gpx files // // step 1: create and store filenames for json and gpx files
track.trackUriString = FileHelper.getTrackFileUri(this@TrackerService, track).toString() // track.trackUriString = FileHelper.getTrackFileUri(this@TrackerService, track).toString()
track.gpxUriString = FileHelper.getGpxFileUri(this@TrackerService, track).toString() // track.gpxUriString = FileHelper.getGpxFileUri(this@TrackerService, track).toString()
// step 2: save track // // step 2: save track
FileHelper.saveTrackSuspended(track, saveGpxToo = true) // FileHelper.saveTrackSuspended(track, saveGpxToo = true)
// step 3: save tracklist // // step 3: save tracklist
FileHelper.addTrackAndSaveTracklistSuspended(this@TrackerService, track) // FileHelper.addTrackAndSaveTracklistSuspended(this@TrackerService, track)
// step 3: clear track // // step 3: clear track
clearTrack() // clearTrack()
} // }
} // }
/* Creates location listener */ /* Creates location listener */
@ -462,11 +465,23 @@ class TrackerService: Service(), CoroutineScope, SensorEventListener {
// reset resumed flag, if necessary // reset resumed flag, if necessary
resumed = false resumed = false
} }
// check, if waypoint was added
if (result.second) {
// reset resumed flag, if necessary
if (resumed) {
resumed = false
}
// save a temp track
val now: Date = GregorianCalendar.getInstance().time
if (now.time - lastSave.time > Keys.SAVE_TEMP_TRACK_INTERVAL) {
lastSave = now
GlobalScope.launch { FileHelper.saveTempTrackSuspended(this@TrackerService, track) }
LogHelper.e(TAG, "TEMP SAVE: $lastSave")
}
}
// update notification // update notification
displayNotification() displayNotification()
// save temp track using GlobalScope.launch = fire & forget (no return value from save) // re-run this in set interval
GlobalScope.launch { FileHelper.saveTempTrackSuspended(this@TrackerService, track) }
// re-run this in 10 seconds
handler.postDelayed(this, Keys.ADD_WAYPOINT_TO_TRACK_INTERVAL) handler.postDelayed(this, Keys.ADD_WAYPOINT_TO_TRACK_INTERVAL)
} }
} }