Skip to content

Commit ced1af3

Browse files
committed
Skip known-unreasonable network location updates
If we have a recent GPS location of a given accuracy, and our proposed network location is more than twice as far away as the GPS accuracy (but at least 1 kilometer away from the GPS location), this is considered unreasonable. Stop sending that network location. Change-Id: I1893c63c2d716a5e41011cbf39f069491e6b5da6
1 parent 3fcc78a commit ced1af3

File tree

1 file changed

+20
-0
lines changed
  • play-services-location/core/src/main/kotlin/org/microg/gms/location/manager

1 file changed

+20
-0
lines changed

play-services-location/core/src/main/kotlin/org/microg/gms/location/manager/LocationManager.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class LocationManager(private val context: Context, override val lifecycle: Life
4646
private var coarsePendingIntent: PendingIntent? = null
4747
private val postProcessor by lazy { LocationPostProcessor() }
4848
private val lastLocationCapsule by lazy { LastLocationCapsule(context) }
49+
private var lastGpsLocation: Location? = null
4950
val database by lazy { LocationAppsDatabase(context) }
5051
private val requestManager by lazy { LocationRequestManager(context, lifecycle, postProcessor, database) { onRequestManagerUpdated() } }
5152
private val gpsLocationListener by lazy { LocationListenerCompat { updateGpsLocation(it) } }
@@ -224,12 +225,29 @@ class LocationManager(private val context: Context, override val lifecycle: Life
224225
}
225226
}
226227

228+
private fun Location.isUnreasonablyFarFromRecentGpsLocation(): Boolean {
229+
val gpsLocation = lastGpsLocation ?: return false
230+
231+
// Ignore GPS location that is too old compared to this location
232+
if (gpsLocation.elapsedMillis < elapsedMillis + MAX_FINE_UPDATE_INTERVAL) return false
233+
234+
val distance = distanceTo(gpsLocation)
235+
if (distance > min(2 * gpsLocation.accuracy, MIN_UNREASONABLE_DISTANCE_FROM_GPS)) {
236+
Log.d(TAG, String.format("Unreasonable location %s vs gps (accuracy %.0f): distance %.0f",
237+
this.provider, gpsLocation.accuracy, distance))
238+
return true
239+
}
240+
return false
241+
}
242+
227243
fun updateNetworkLocation(location: Location) {
228244
val lastLocation = lastLocationCapsule.getLocation(GRANULARITY_FINE, Long.MAX_VALUE)
229245

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

249+
if (location.isUnreasonablyFarFromRecentGpsLocation()) return
250+
233251
if (lastLocation == null ||
234252
lastLocation.accuracy > location.accuracy ||
235253
lastLocation.elapsedMillis + min(requestManager.intervalMillis * 2, UPDATE_CLIFF_MS) < location.elapsedMillis ||
@@ -242,6 +260,7 @@ class LocationManager(private val context: Context, override val lifecycle: Life
242260

243261
fun updateGpsLocation(location: Location) {
244262
lastLocationCapsule.updateFineLocation(location)
263+
lastGpsLocation = location
245264
sendNewLocation()
246265
}
247266

@@ -324,5 +343,6 @@ class LocationManager(private val context: Context, override val lifecycle: Life
324343
const val MAX_FINE_UPDATE_INTERVAL = 10_000L
325344
const val EXTENSION_CLIFF_MS = 10_000L
326345
const val UPDATE_CLIFF_MS = 30_000L
346+
const val MIN_UNREASONABLE_DISTANCE_FROM_GPS = 1_000f // 1 kilometer
327347
}
328348
}

0 commit comments

Comments
 (0)