From f02addae4cfe58be1cf320abd105e8e62ce65123 Mon Sep 17 00:00:00 2001 From: y20k Date: Wed, 28 Sep 2016 14:57:06 +0200 Subject: [PATCH] trying to get rid of implausible locations delivered by the network provider --- app/build.gradle | 2 +- .../java/org/y20k/trackbook/MainActivity.java | 2 ++ .../trackbook/MainActivityMapFragment.java | 3 +- .../org/y20k/trackbook/TrackerService.java | 12 +++++++- .../trackbook/helpers/LocationHelper.java | 28 ++++++++++++++----- .../y20k/trackbook/helpers/TrackbookKeys.java | 2 +- 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 49eeccf..1bd011c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,6 +25,6 @@ dependencies { testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.2.1' compile 'com.android.support:design:24.2.1' - compile 'org.osmdroid:osmdroid-android:5.2@aar' + compile 'org.osmdroid:osmdroid-android:5.4.1:release@aar' compile 'com.google.code.gson:gson:2.7' } diff --git a/app/src/main/java/org/y20k/trackbook/MainActivity.java b/app/src/main/java/org/y20k/trackbook/MainActivity.java index c3e3ad1..325e049 100644 --- a/app/src/main/java/org/y20k/trackbook/MainActivity.java +++ b/app/src/main/java/org/y20k/trackbook/MainActivity.java @@ -372,6 +372,8 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys, Di startService(intent); } else { + // TODO ask if user wants to save the last track before starting a new recording + // TODO alternatively only ask if last track was very short // show snackbar Snackbar.make(view, R.string.snackbar_message_tracking_started, Snackbar.LENGTH_SHORT).setAction("Action", null).show(); diff --git a/app/src/main/java/org/y20k/trackbook/MainActivityMapFragment.java b/app/src/main/java/org/y20k/trackbook/MainActivityMapFragment.java index 6ceaa4b..f17c9ca 100644 --- a/app/src/main/java/org/y20k/trackbook/MainActivityMapFragment.java +++ b/app/src/main/java/org/y20k/trackbook/MainActivityMapFragment.java @@ -396,6 +396,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys { // save track object SaveTrackAsyncHelper saveTrackAsyncHelper = new SaveTrackAsyncHelper(); saveTrackAsyncHelper.execute(); + // TODO add toast indicating track save } @@ -587,7 +588,7 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys { /** - * Inner class: Saves track to external storage using an AsyncTask + * Inner class: Saves track to external storage using AsyncTask */ private class SaveTrackAsyncHelper extends AsyncTask { diff --git a/app/src/main/java/org/y20k/trackbook/TrackerService.java b/app/src/main/java/org/y20k/trackbook/TrackerService.java index ae94b07..3f328ec 100644 --- a/app/src/main/java/org/y20k/trackbook/TrackerService.java +++ b/app/src/main/java/org/y20k/trackbook/TrackerService.java @@ -250,7 +250,17 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven } else { // get last WayPoint and compare it to current location Location lastWayPoint = mTrack.getWayPointLocation(trackSize - 1); - if (LocationHelper.isNewWayPoint(lastWayPoint, mCurrentBestLocation)) { + + // compute average speed + float averageSpeed = 0f; + if (trackSize > 1) { + Location firstWayPoint = mTrack.getWayPointLocation(0); + float distance = firstWayPoint.distanceTo(lastWayPoint); + long timeDifference = lastWayPoint.getElapsedRealtimeNanos() - firstWayPoint.getElapsedRealtimeNanos(); + averageSpeed = distance / ((float)timeDifference / ONE_NANOSECOND); + } + + if (LocationHelper.isNewWayPoint(lastWayPoint, mCurrentBestLocation, averageSpeed)) { // if new, add current best location to track newWayPoint = mTrack.addWayPoint(mCurrentBestLocation); } diff --git a/app/src/main/java/org/y20k/trackbook/helpers/LocationHelper.java b/app/src/main/java/org/y20k/trackbook/helpers/LocationHelper.java index c7832ef..160be62 100644 --- a/app/src/main/java/org/y20k/trackbook/helpers/LocationHelper.java +++ b/app/src/main/java/org/y20k/trackbook/helpers/LocationHelper.java @@ -136,19 +136,33 @@ public final class LocationHelper implements TrackbookKeys { /* Checks if given location is a new WayPoint */ - public static boolean isNewWayPoint(Location lastLocation, Location newLocation) { + public static boolean isNewWayPoint(Location lastLocation, Location newLocation, float averageSpeed) { float distance = newLocation.distanceTo(lastLocation); long timeDifference = newLocation.getElapsedRealtimeNanos() - lastLocation.getElapsedRealtimeNanos(); - // TODO check for sudden location jump errors + if (newLocation.getProvider().equals(LocationManager.NETWORK_PROVIDER)) { + // SPECIAL CASE network: plausibility check for network provider. looking for sudden location jump errors + float speedDifference; + float currentSpeed = distance / ((float)timeDifference / ONE_NANOSECOND); + if (currentSpeed > averageSpeed) { + speedDifference = currentSpeed / averageSpeed; + } else { + speedDifference = averageSpeed / currentSpeed; + } + // check if speed is high (10 m/s == 36km/h) and has doubled + if (averageSpeed != 0f && currentSpeed > 10f && speedDifference > 2f) { + // implausible location + return false; + } + + // DEFAULT network: distance is bigger than 50 meters and time difference bigger than 12 seconds + return distance > 50 && timeDifference >= 12 * ONE_NANOSECOND; - if (newLocation.getProvider().equals(LocationManager.GPS_PROVIDER)) { - // CASE GPS: distance is bigger than 10 meters and time difference bigger than 12 seconds - return distance > 10 && timeDifference >= TWELVE_SECONDS_IN_NANOSECONDS; } else { - // CASE network: distance is bigger than 50 meters and time difference bigger than 12 seconds - return distance > 50 && timeDifference >= TWELVE_SECONDS_IN_NANOSECONDS; + // DEFAULT GPS: distance is bigger than 10 meters and time difference bigger than 12 seconds + return distance > 10 && timeDifference >= 12 * ONE_NANOSECOND; } + } diff --git a/app/src/main/java/org/y20k/trackbook/helpers/TrackbookKeys.java b/app/src/main/java/org/y20k/trackbook/helpers/TrackbookKeys.java index 817e59b..9809708 100644 --- a/app/src/main/java/org/y20k/trackbook/helpers/TrackbookKeys.java +++ b/app/src/main/java/org/y20k/trackbook/helpers/TrackbookKeys.java @@ -67,11 +67,11 @@ public interface TrackbookKeys { /* RESULTS */ /* CONSTANTS */ + long ONE_NANOSECOND = 1000000000L; long EIGHT_HOURS_IN_MILLISECONDS = 43200000; // maximum tracking duration long FIFTEEN_SECONDS_IN_MILLISECONDS = 15000; // timer interval for tracking long FIVE_MINUTES_IN_NANOSECONDS = 5L * 60000000000L; // determines a stop over long TWO_MINUTES_IN_NANOSECONDS = 2L * 60000000000L; // defines an old location - long TWELVE_SECONDS_IN_NANOSECONDS = 12000000000L; // defines a new location /* MISC */