Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/main/java/net/osmtracker/OSMTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/net/osmtracker/service/gps/GPSLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public class GPSLogger extends Service implements LocationListener {
*/
private long gpsLoggingInterval;
private long gpsLoggingMinDistance;
private boolean gpsLoggingDistGtAccuracy;

/**
* sensors for magnetic orientation
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings-preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<string name="prefs_gps_logging_min_distance_summary">Min. distance between track points in meters, use 0 for the shortest possible</string>
<string name="prefs_gps_logging_min_distance_meters">meters</string>
<string name="prefs_gps_logging_min_distance_empty">Min. distance between track points cannot be empty</string>
<string name="prefs_gps_logging_distance_gt_accuracy">GPS Logging only track points farther than GPS accuracy</string>
<string name="prefs_gps_logging_distance_gt_accuracy_summary">Skip a track point if it is closer to the previous one than its reported GPS accuracy</string>
<string name="prefs_ui">User interface</string>
<string name="prefs_ui_picture_source">Default photo source</string>
<string name="prefs_ui_picture_source_summary">Take photos from camera or gallery?</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
<CheckBoxPreference
app:defaultValue="false"
app:key="gps.logging.dist_gt_accuracy"
app:summary="@string/prefs_gps_logging_distance_gt_accuracy_summary"
app:title="@string/prefs_gps_logging_distance_gt_accuracy" />
</PreferenceCategory>

<PreferenceCategory app:title="@string/prefs_output">
Expand Down
97 changes: 97 additions & 0 deletions app/src/test/java/net/osmtracker/gps/GPSLoggerTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading