Skip to content

Commit

Permalink
Stale out old locations so they are not set on records and implement …
Browse files Browse the repository at this point in the history
…a location update rate ceiling
  • Loading branch information
christianrowlands committed Nov 15, 2024
1 parent ffb572d commit e8d04c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
Expand All @@ -22,6 +23,7 @@
*/
public class GpsListener implements LocationListener
{
public static final long LOCATION_AGE_THRESHOLD_MS = 60_000L;
private final Set<LocationListener> listeners = new CopyOnWriteArraySet<>();

private Location latestLocation;
Expand Down Expand Up @@ -136,7 +138,21 @@ public void onProviderDisabled(@NonNull String provider)

public Location getLatestLocation()
{
return latestLocation;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
{
long locationTimeAge = latestLocation.getElapsedRealtimeAgeMillis();
if (locationTimeAge > LOCATION_AGE_THRESHOLD_MS)
{
// Don't return stale locations as it will mess up the data
return null;
} else
{
return latestLocation;
}
} else
{
return latestLocation;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,9 @@ public void updateLocationListener()
// Use the smallest scan rate set by the user for the active scanning types
if (smallestScanRate > 20_000) smallestScanRate = smallestScanRate / 2;

long scanRateCeiling = GpsListener.LOCATION_AGE_THRESHOLD_MS - 20_000;
if (smallestScanRate > scanRateCeiling) smallestScanRate = (int) scanRateCeiling;

if (smallestScanRate < 8_000) smallestScanRate = 8_000;

Timber.d("Setting the location update rate to %d", smallestScanRate);
Expand Down

0 comments on commit e8d04c0

Please sign in to comment.