Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip known-unreasonable network location updates #2367

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class LocationManager(private val context: Context, override val lifecycle: Life
private var coarsePendingIntent: PendingIntent? = null
private val postProcessor by lazy { LocationPostProcessor() }
private val lastLocationCapsule by lazy { LastLocationCapsule(context) }
private var lastGpsLocation: Location? = null
val database by lazy { LocationAppsDatabase(context) }
private val requestManager by lazy { LocationRequestManager(context, lifecycle, postProcessor, database) { onRequestManagerUpdated() } }
private val gpsLocationListener by lazy { LocationListenerCompat { updateGpsLocation(it) } }
Expand Down Expand Up @@ -224,12 +225,29 @@ class LocationManager(private val context: Context, override val lifecycle: Life
}
}

private fun Location.isUnreasonablyFarFromRecentGpsLocation(): Boolean {
val gpsLocation = lastGpsLocation ?: return false

// Ignore GPS location that is too old compared to this location
if (gpsLocation.elapsedMillis < elapsedMillis + MAX_FINE_UPDATE_INTERVAL) return false

val distance = distanceTo(gpsLocation)
if (distance > max(2 * gpsLocation.accuracy, MIN_UNREASONABLE_DISTANCE_FROM_GPS)) {
Log.d(TAG, String.format("Unreasonable location %s vs gps (accuracy %.0f): distance %.0f",
this.provider, gpsLocation.accuracy, distance))
return true
}
return false
}

fun updateNetworkLocation(location: Location) {
val lastLocation = lastLocationCapsule.getLocation(GRANULARITY_FINE, Long.MAX_VALUE)

// Ignore outdated location
if (lastLocation != null && location.elapsedMillis + UPDATE_CLIFF_MS < lastLocation.elapsedMillis) return

if (location.isUnreasonablyFarFromRecentGpsLocation()) return

if (lastLocation == null ||
lastLocation.accuracy > location.accuracy ||
lastLocation.elapsedMillis + min(requestManager.intervalMillis * 2, UPDATE_CLIFF_MS) < location.elapsedMillis ||
Expand All @@ -242,6 +260,7 @@ class LocationManager(private val context: Context, override val lifecycle: Life

fun updateGpsLocation(location: Location) {
lastLocationCapsule.updateFineLocation(location)
lastGpsLocation = location
sendNewLocation()
}

Expand Down Expand Up @@ -324,5 +343,6 @@ class LocationManager(private val context: Context, override val lifecycle: Life
const val MAX_FINE_UPDATE_INTERVAL = 10_000L
const val EXTENSION_CLIFF_MS = 10_000L
const val UPDATE_CLIFF_MS = 30_000L
const val MIN_UNREASONABLE_DISTANCE_FROM_GPS = 1_000f // 1 kilometer
}
}