From 8b48a4bb06a0e7a66ea75cddabc963f0f2cecf22 Mon Sep 17 00:00:00 2001 From: Alain Knaff Date: Sat, 16 May 2026 14:11:55 +0200 Subject: [PATCH 1/3] Only log point if new point is further away from previous than its accuracy --- app/src/main/java/net/osmtracker/OSMTracker.java | 3 +++ .../main/java/net/osmtracker/service/gps/GPSLogger.java | 9 +++++++++ app/src/main/res/values/strings-preferences.xml | 2 ++ app/src/main/res/xml/preferences.xml | 7 ++++++- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/net/osmtracker/OSMTracker.java b/app/src/main/java/net/osmtracker/OSMTracker.java index 3accb77c..8a81ac22 100644 --- a/app/src/main/java/net/osmtracker/OSMTracker.java +++ b/app/src/main/java/net/osmtracker/OSMTracker.java @@ -23,6 +23,8 @@ 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 +63,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..f865cc3d 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,12 @@ 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()){ + // next check whether distance is greater than accurracy + if(gpsLoggingDistGtAccuracy && + lastLocation != null && + location.distanceTo(lastLocation) <= location.getAccuracy()) + 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..3d22d6a6 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 + Log only pts where dist. > accur. + Log only points which are further away from previous point than 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..07d2b719 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" /> + @@ -159,4 +164,4 @@ app:title="@string/prefs_ui_orientation" /> - \ No newline at end of file + From fec2c9dfb3dde69f1cd4b11495e0bf9fce37eaf7 Mon Sep 17 00:00:00 2001 From: Alain Knaff Date: Thu, 22 Oct 2026 22:50:26 +0200 Subject: [PATCH 2/3] Testcase for distance > accuracy --- .../net/osmtracker/gps/GPSLoggerTest.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 app/src/test/java/net/osmtracker/gps/GPSLoggerTest.java 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); + } +} From 6eeb0de9554a9ceac1d976d142ae4688bcdec6f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Guti=C3=A9rrez=20Alfaro?= Date: Mon, 2 Nov 2026 12:28:59 -0600 Subject: [PATCH 3/3] refactor: GPS accuracy filter from #703 --- app/src/main/java/net/osmtracker/OSMTracker.java | 1 - app/src/main/java/net/osmtracker/service/gps/GPSLogger.java | 6 ++++-- app/src/main/res/values/strings-preferences.xml | 4 ++-- app/src/main/res/xml/preferences.xml | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/net/osmtracker/OSMTracker.java b/app/src/main/java/net/osmtracker/OSMTracker.java index 8a81ac22..484b1660 100644 --- a/app/src/main/java/net/osmtracker/OSMTracker.java +++ b/app/src/main/java/net/osmtracker/OSMTracker.java @@ -24,7 +24,6 @@ public static final class Preferences { 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"; 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 f865cc3d..da1a7957 100644 --- a/app/src/main/java/net/osmtracker/service/gps/GPSLogger.java +++ b/app/src/main/java/net/osmtracker/service/gps/GPSLogger.java @@ -351,11 +351,13 @@ 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()){ - // next check whether distance is greater than accurracy + // Check whether distance is greater than accuracy if(gpsLoggingDistGtAccuracy && lastLocation != null && - location.distanceTo(lastLocation) <= location.getAccuracy()) + 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 diff --git a/app/src/main/res/values/strings-preferences.xml b/app/src/main/res/values/strings-preferences.xml index 3d22d6a6..a34452dd 100644 --- a/app/src/main/res/values/strings-preferences.xml +++ b/app/src/main/res/values/strings-preferences.xml @@ -26,8 +26,8 @@ Min. distance between track points in meters, use 0 for the shortest possible meters Min. distance between track points cannot be empty - Log only pts where dist. > accur. - Log only points which are further away from previous point than GPS accuracy + 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 07d2b719..a1266aa0 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -164,4 +164,4 @@ app:title="@string/prefs_ui_orientation" /> - + \ No newline at end of file