diff --git a/app/src/main/java/net/osmtracker/OSMTracker.java b/app/src/main/java/net/osmtracker/OSMTracker.java
index 3accb77c..484b1660 100644
--- a/app/src/main/java/net/osmtracker/OSMTracker.java
+++ b/app/src/main/java/net/osmtracker/OSMTracker.java
@@ -23,6 +23,7 @@ public static final class Preferences {
public final static String KEY_GPS_IGNORE_CLOCK = "gps.ignoreclock";
public final static String KEY_GPS_LOGGING_INTERVAL = "gps.logging.interval";
public final static String KEY_GPS_LOGGING_MIN_DISTANCE = "gps.logging.min_distance";
+ public final static String KEY_GPS_LOGGING_DIST_GT_ACCURACY = "gps.logging.dist_gt_accuracy";
public final static String KEY_USE_BAROMETER = "gpx.use_barometer";
public final static String KEY_USE_NOTES = "gpx.notes";
public final static String KEY_OUTPUT_FILENAME = "gpx.filename";
@@ -61,6 +62,7 @@ public static final class Preferences {
public final static boolean VAL_GPS_IGNORE_CLOCK = false;
public final static String VAL_GPS_LOGGING_INTERVAL = "0";
public final static String VAL_GPS_LOGGING_MIN_DISTANCE = "0";
+ public final static boolean VAL_GPS_LOGGING_DIST_GT_ACCURACY = false;
public final static boolean VAL_USE_BAROMETER = false;
public final static String VAL_USE_NOTES = "both";
diff --git a/app/src/main/java/net/osmtracker/service/gps/GPSLogger.java b/app/src/main/java/net/osmtracker/service/gps/GPSLogger.java
index 901cc5e6..da1a7957 100644
--- a/app/src/main/java/net/osmtracker/service/gps/GPSLogger.java
+++ b/app/src/main/java/net/osmtracker/service/gps/GPSLogger.java
@@ -98,6 +98,7 @@ public class GPSLogger extends Service implements LocationListener {
*/
private long gpsLoggingInterval;
private long gpsLoggingMinDistance;
+ private boolean gpsLoggingDistGtAccuracy;
/**
* sensors for magnetic orientation
@@ -254,6 +255,8 @@ public void onCreate() {
OSMTracker.Preferences.KEY_GPS_LOGGING_MIN_DISTANCE, OSMTracker.Preferences.VAL_GPS_LOGGING_MIN_DISTANCE));
use_barometer = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).getBoolean(
OSMTracker.Preferences.KEY_USE_BAROMETER, OSMTracker.Preferences.VAL_USE_BAROMETER);
+ gpsLoggingDistGtAccuracy = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()).getBoolean(
+ OSMTracker.Preferences.KEY_GPS_LOGGING_DIST_GT_ACCURACY, OSMTracker.Preferences.VAL_GPS_LOGGING_DIST_GT_ACCURACY);
// Register our broadcast receiver
IntentFilter filter = new IntentFilter();
@@ -348,6 +351,14 @@ public void onLocationChanged(Location location) {
// first of all we check if the time from the last used fix to the current fix is greater than the logging interval
if((lastGPSTimestamp + gpsLoggingInterval) < System.currentTimeMillis()){
+ // Check whether distance is greater than accuracy
+ if(gpsLoggingDistGtAccuracy &&
+ lastLocation != null &&
+ location.distanceTo(lastLocation) <= location.getAccuracy()) {
+ Log.d(TAG, "Location ignored because distance is less than accuracy");
+ return;
+ }
+
lastGPSTimestamp = System.currentTimeMillis(); // save the time of this fix
lastLocation = location;
diff --git a/app/src/main/res/values/strings-preferences.xml b/app/src/main/res/values/strings-preferences.xml
index 446f1b50..a34452dd 100644
--- a/app/src/main/res/values/strings-preferences.xml
+++ b/app/src/main/res/values/strings-preferences.xml
@@ -26,6 +26,8 @@
Min. distance between track points in meters, use 0 for the shortest possible
meters
Min. distance between track points cannot be empty
+ GPS Logging only track points farther than GPS accuracy
+ Skip a track point if it is closer to the previous one than its reported GPS accuracy
User interface
Default photo source
Take photos from camera or gallery?
diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml
index fb05bec8..a1266aa0 100644
--- a/app/src/main/res/xml/preferences.xml
+++ b/app/src/main/res/xml/preferences.xml
@@ -67,6 +67,11 @@
app:key="gps.logging.min_distance"
app:summary="@string/prefs_gps_logging_min_distance_summary"
app:title="@string/prefs_gps_logging_min_distance" />
+
diff --git a/app/src/test/java/net/osmtracker/gps/GPSLoggerTest.java b/app/src/test/java/net/osmtracker/gps/GPSLoggerTest.java
new file mode 100644
index 00000000..64570d32
--- /dev/null
+++ b/app/src/test/java/net/osmtracker/gps/GPSLoggerTest.java
@@ -0,0 +1,97 @@
+package net.osmtracker.gps;
+
+import java.lang.reflect.Field;
+import android.content.Context;
+import android.location.Location;
+import android.util.Log;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.annotation.Config;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+import androidx.preference.PreferenceManager;
+
+import net.osmtracker.OSMTracker;
+import net.osmtracker.db.DataHelper;
+import net.osmtracker.service.gps.GPSLogger;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.anyFloat;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(sdk = 25)
+public class GPSLoggerTest {
+
+ private GPSLogger gpsLogger;
+ private DataHelper mockDataHelper;
+ private Context context;
+
+ @Before
+ public void setUp() throws NoSuchFieldException, IllegalAccessException {
+ context = RuntimeEnvironment.getApplication();
+ // Set up default preferences
+ PreferenceManager.getDefaultSharedPreferences(context)
+ .edit()
+ .putString(OSMTracker.Preferences.KEY_GPS_LOGGING_INTERVAL, "0")
+ .putString(OSMTracker.Preferences.KEY_GPS_LOGGING_MIN_DISTANCE, "0")
+ .putBoolean(OSMTracker.Preferences.KEY_OUTPUT_ACCURACY, true)
+ .commit();
+
+ gpsLogger = new GPSLogger();
+ mockDataHelper = mock(DataHelper.class);
+ setInternalState(gpsLogger, "dataHelper", mockDataHelper);
+ setInternalState(gpsLogger, "isTracking", true);
+ setInternalState(gpsLogger, "gpsLoggingDistGtAccuracy", true);
+ }
+
+ @Test
+ public void shouldLogWhenDistanceGreaterThanAccuracy() {
+ Log.i("GPSLoggerTest", "shouldLogWhenDistanceGreaterThanAccuracy");
+ gpsLogger.onLocationChanged(createLocation(0.0, 0.0, 5.0f));
+ verify(mockDataHelper, times(1)).track(anyLong(), any(Location.class), anyFloat(), anyInt(), anyFloat(), anyLong());
+
+ // Second location: distance (~10m) > accuracy (5m), should log
+ gpsLogger.onLocationChanged(createLocation(0.00009, 0.0, 5.0f));
+
+ verify(mockDataHelper, times(2)).track(anyLong(), any(Location.class), anyFloat(), anyInt(), anyFloat(), anyLong());
+ }
+
+ @Test
+ public void shouldNotLogWhenDistanceLessThanAccuracy() {
+ Log.i("GPSLoggerTest", "shouldNotLogWhenDistanceGreaterThanAccuracy");
+ gpsLogger.onLocationChanged(createLocation(0.0, 0.0, 15.0f));
+ verify(mockDataHelper, times(1)).track(anyLong(), any(Location.class), anyFloat(), anyInt(), anyFloat(), anyLong());
+
+ // Second location: distance (~5m) < accuracy (15m), should NOT log
+ gpsLogger.onLocationChanged(createLocation(0.000045, 0.0, 15.0f));
+ verify(mockDataHelper, times(1)).track(anyLong(), any(Location.class), anyFloat(), anyInt(), anyFloat(), anyLong()); // there should be no additional invocation
+ }
+
+ private Location createLocation(double latitude, double longitude, float accuracy) {
+ Location location = new Location("test");
+ location.setLatitude(latitude);
+ location.setLongitude(longitude);
+ location.setAccuracy(accuracy);
+ location.setTime(System.currentTimeMillis());
+ return location;
+ }
+
+ private void setInternalState(Object target, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
+ Field field;
+ if (target instanceof Class) {
+ field = ((Class>) target).getDeclaredField(fieldName);
+ } else {
+ field = target.getClass().getDeclaredField(fieldName);
+ }
+ field.setAccessible(true);
+ field.set(target instanceof Class ? null : target, value);
+ }
+}