Skip to content

Commit

Permalink
don't scan wifis if gps accuracy insufficient
Browse files Browse the repository at this point in the history
  • Loading branch information
Helium314 committed May 17, 2023
1 parent 06b221f commit 000291e
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions app/src/main/java/org/fitchfamily/android/dejavu/BackendService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ class BackendService : LocationBackendService() {
* @param update The current GPS reported location
*/
@Synchronized
private fun onGpsChanged(update: Location) {
private fun onLocationChanged(update: Location) {
if (permissionsOkay && notNullIsland(update)) {
if (DEBUG) Log.d(TAG, "onGpsChanged() - accuracy ${update.accuracy}")
if (DEBUG) Log.d(TAG, "onLocationChanged() - ${update.provider}, accuracy ${update.accuracy}")
lastGpsOfThisPeriod = update
if (useKalman)
kalmanGpsLocation?.update(update) ?: run { kalmanGpsLocation = Kalman(update, GPS_COORDINATE_NOISE) }
Expand All @@ -339,17 +339,18 @@ class BackendService : LocationBackendService() {
if (DEBUG) Log.d(TAG, "onGpsChanged() - updating old emitters")
updateEmitters(oldEmitters.mapNotNull { emitterCache?.get(it) }, location)
}
scanAllSensors()
scanAllSensors(update)
} else
Log.d(TAG, "onGpsChanged() - Permissions not granted or location invalid, soft fail.")
}

/**
* Kick off new scans for all the sensor types we know about. Typically scans
* should occur asynchronously so we don't hang up our caller's thread.
* if a [location] is provided, WiFi scan is only started if accucacy is sufficient
*/
@Synchronized
private fun scanAllSensors() {
private fun scanAllSensors(location: Location? = null) {
if (emitterCache == null) {
if (instance == null) { // this should not be necessary, but better make sure
if (DEBUG) Log.d(TAG, "scanAllSensors() - instance is null")
Expand All @@ -360,7 +361,7 @@ class BackendService : LocationBackendService() {
}

if (DEBUG) Log.d(TAG, "scanAllSensors() - starting scans")
val wifiScanStarted = startWiFiScan()
val wifiScanStarted = (location?.accuracy ?: 0f) < shortRangeMinAccuracy && startWiFiScan()
val mobileScanStarted = startMobileScan()
if (wifiScanStarted || mobileScanStarted)
startProcessingPeriodIfNecessary() // only try starting new period if a scan was started
Expand Down Expand Up @@ -1012,7 +1013,7 @@ class BackendService : LocationBackendService() {
*/
fun instanceGpsLocationUpdated(locReport: Location) {
//Log.d(TAG, "instanceGpsLocationUpdated() entry.");
instance?.onGpsChanged(locReport)
instance?.onLocationChanged(locReport)
}

/**
Expand All @@ -1021,12 +1022,12 @@ class BackendService : LocationBackendService() {
*/
fun geoUriLocationProvided(latitude: Double, longitude: Double) {
Log.d(TAG, "handleExternalLocation() - accepting location provided via geoUri intent")
val loc = Location("external")
val loc = Location("geoUri")
loc.latitude = latitude
loc.longitude = longitude
loc.accuracy = 0f
loc.elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos()
instance?.onGpsChanged(loc)
instance?.onLocationChanged(loc)
}

/** Clears the emitter cache. Necessary if changes to database were made no through cache */
Expand Down Expand Up @@ -1115,3 +1116,5 @@ private val wifiBroadcastFilter = IntentFilter(WifiManager.SCAN_RESULTS_AVAILABL

// shorter name because it's checked often
private const val intMax = Int.MAX_VALUE

private val shortRangeMinAccuracy = shortRangeEmitterTypes.maxOf { it.getRfCharacteristics().requiredGpsAccuracy }

0 comments on commit 000291e

Please sign in to comment.