From 59c736630f4ec193c88f285dc4c3843ac05e841a Mon Sep 17 00:00:00 2001 From: y20k Date: Wed, 1 Apr 2020 17:26:55 +0200 Subject: [PATCH] should fix crash described in issue #70 - adds check if file manager is present --- .../java/org/y20k/trackbook/TrackerService.kt | 2 +- .../org/y20k/trackbook/helpers/TrackHelper.kt | 32 +++++++++++++++---- .../trackbook/ui/TrackFragmentLayoutHolder.kt | 10 +++--- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/y20k/trackbook/TrackerService.kt b/app/src/main/java/org/y20k/trackbook/TrackerService.kt index 94727d3..80b9d06 100644 --- a/app/src/main/java/org/y20k/trackbook/TrackerService.kt +++ b/app/src/main/java/org/y20k/trackbook/TrackerService.kt @@ -457,7 +457,7 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener { private val periodicTrackUpdate: Runnable = object : Runnable { override fun run() { // add waypoint to track - step count is continuously updated in onSensorChanged - val result: Pair = TrackHelper.addWayPointToTrack(track, currentBestLocation, locationAccuracyThreshold, resumed) + val result: Pair = TrackHelper.addWayPointToTrack(this@TrackerService, track, currentBestLocation, locationAccuracyThreshold, resumed) // get track from result track = result.first // check if waypoint was successfully added (= result.second) diff --git a/app/src/main/java/org/y20k/trackbook/helpers/TrackHelper.kt b/app/src/main/java/org/y20k/trackbook/helpers/TrackHelper.kt index 6e28c9b..60884b2 100644 --- a/app/src/main/java/org/y20k/trackbook/helpers/TrackHelper.kt +++ b/app/src/main/java/org/y20k/trackbook/helpers/TrackHelper.kt @@ -17,7 +17,9 @@ package org.y20k.trackbook.helpers +import android.content.Context import android.location.Location +import android.widget.Toast import org.y20k.trackbook.core.Track import org.y20k.trackbook.core.TracklistElement import org.y20k.trackbook.core.WayPoint @@ -45,24 +47,27 @@ object TrackHelper { /* Adds given locatiom as waypoint to track */ - fun addWayPointToTrack(track: Track, location: Location, locationAccuracyThreshold: Int, resumed: Boolean): Pair { + fun addWayPointToTrack(context: Context, track: Track, location: Location, locationAccuracyThreshold: Int, resumed: Boolean): Pair { // get previous location val previousLocation: Location? var numberOfWayPoints: Int = track.wayPoints.size // CASE: First location if (numberOfWayPoints == 0) { + LogHelper.e(TAG, "First Location." ) // todo remove previousLocation = null } // CASE: Second location - check if first location was plausible & remove implausible location else if (numberOfWayPoints == 1 && !LocationHelper.isFirstLocationPlausible(location, track)) { + LogHelper.e(TAG, "Second Location not plausible.") // todo remove previousLocation = null numberOfWayPoints = 0 track.wayPoints.removeAt(0) } // CASE: Third location or second location (if first was plausible) else { - previousLocation = track.wayPoints.get(numberOfWayPoints - 1).toLocation() + LogHelper.e(TAG, "Yay.") // todo remove + previousLocation = track.wayPoints[numberOfWayPoints - 1].toLocation() } // update duration @@ -73,9 +78,24 @@ object TrackHelper { // add only if recent and accurate val shouldBeAdded: Boolean - shouldBeAdded = (LocationHelper.isRecentEnough(location) - && LocationHelper.isAccurateEnough(location, locationAccuracyThreshold) - && LocationHelper.isDifferentEnough(previousLocation, location)) + + // todo remove for production + val recentEnough: Boolean = LocationHelper.isRecentEnough(location) + val accurateEnough: Boolean = LocationHelper.isAccurateEnough(location, locationAccuracyThreshold) + val differentEnough: Boolean = LocationHelper.isDifferentEnough(previousLocation, location) + + if (!recentEnough && accurateEnough && differentEnough) { Toast.makeText(context, "Debug: Not recent enough", Toast.LENGTH_LONG).show() } + else if (!accurateEnough && recentEnough && differentEnough) { Toast.makeText(context, "Debug: Not accurate enough", Toast.LENGTH_LONG).show() } + else if (!differentEnough && recentEnough && accurateEnough) { Toast.makeText(context, "Debug: Not different enough", Toast.LENGTH_LONG).show() } + else if (!recentEnough && !accurateEnough && differentEnough) { Toast.makeText(context, "Debug: Not recent and accurate enough", Toast.LENGTH_LONG).show() } + else if (!recentEnough && !differentEnough && accurateEnough) { Toast.makeText(context, "Debug: Not recent and different enough", Toast.LENGTH_LONG).show() } + else if (!accurateEnough && !differentEnough && recentEnough) { Toast.makeText(context, "Debug: Not accurate and different enough", Toast.LENGTH_LONG).show() } + else { Toast.makeText(context, "Debug: bad location.", Toast.LENGTH_LONG).show() } + + shouldBeAdded = recentEnough && accurateEnough && differentEnough + //shouldBeAdded = (LocationHelper.isRecentEnough(location) && + // LocationHelper.isAccurateEnough(location, locationAccuracyThreshold) && + // LocationHelper.isDifferentEnough(previousLocation, location)) if (shouldBeAdded) { // update distance (do not update if resumed -> we do not want to add values calculated during a recording pause) @@ -124,7 +144,7 @@ object TrackHelper { track.longitude = location.longitude // add location as new waypoint - track.wayPoints.add(WayPoint(location.provider, location.latitude, location.longitude, location.altitude, location.accuracy, location.time, track.length, numberOfSatellites)) + track.wayPoints.add(WayPoint(provider = location.provider, latitude = location.latitude, longitude = location.longitude, altitude = location.altitude, accuracy = location.accuracy, time = location.time, distanceToStartingPoint = track.length, numberSatellites = numberOfSatellites)) } return Pair(track, shouldBeAdded) diff --git a/app/src/main/java/org/y20k/trackbook/ui/TrackFragmentLayoutHolder.kt b/app/src/main/java/org/y20k/trackbook/ui/TrackFragmentLayoutHolder.kt index ba0b92a..b9b9b71 100644 --- a/app/src/main/java/org/y20k/trackbook/ui/TrackFragmentLayoutHolder.kt +++ b/app/src/main/java/org/y20k/trackbook/ui/TrackFragmentLayoutHolder.kt @@ -162,10 +162,12 @@ data class TrackFragmentLayoutHolder(var context: Context, var inflater: LayoutI /* Saves zoom level and center of this map */ fun saveViewStateToTrack() { val center: IGeoPoint = mapView.mapCenter - track.latitude = center.latitude - track.longitude = center.longitude - track.zoomLevel = mapView.zoomLevelDouble - GlobalScope.launch { FileHelper.saveTrackSuspended(track, false) } + if (center.latitude != 0.0 && center.longitude != 0.0) { + track.latitude = center.latitude + track.longitude = center.longitude + track.zoomLevel = mapView.zoomLevelDouble + GlobalScope.launch { FileHelper.saveTrackSuspended(track, false) } + } }