Skip to content

Commit

Permalink
Send locations updates not faster than every 30 seconds - workaround …
Browse files Browse the repository at this point in the history
…for Google Play Services bug (or feature) sending location updates every second when GPS is in use
  • Loading branch information
anyuta1166 committed Mar 11, 2022
1 parent 7ee1441 commit a6c9e6d
Showing 1 changed file with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
)
requestSingleAccurateLocation()
} else {
sendLocationUpdate(geofencingEvent.triggeringLocation)
sendLocationUpdate(geofencingEvent.triggeringLocation, true)
}
}

private fun sendLocationUpdate(location: Location) {
private fun sendLocationUpdate(location: Location, geofenceUpdate: Boolean = false) {
Log.d(
TAG, "Last Location: " +
"\nCoords:(${location.latitude}, ${location.longitude})" +
Expand All @@ -230,18 +230,50 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {

val now = System.currentTimeMillis()

if (lastUpdateLocation == updateLocation.gps.contentToString()) {
if (now < lastLocationSend + 9000) {
Log.d(TAG, "Duplicate location received, not sending to HA")
return
Log.d(TAG, "Begin evaluating if location update should be skipped")
if (now + 5000 < location.time) {
Log.d(TAG, "Skipping location update that came from the future. ${now + 5000} should always be greater than ${location.time}")
return
}

if (location.time < lastLocationSend) {
Log.d(
TAG,
"Skipping old location update since time is before the last one we sent, received: ${location.time} last sent: $lastLocationSend"
)
return
}

if (now - location.time < 300000) {
Log.d(
TAG,
"Received location that is ${now - location.time} milliseconds old, ${location.time} compared to $now with source ${location.provider}"
)
if (lastUpdateLocation == updateLocation.gps.contentToString()) {
if (now < lastLocationSend + 29000) {
Log.d(TAG, "Duplicate location received, not sending to HA")
return
}
} else {
if (now < lastLocationSend + 29000 && !geofenceUpdate) {
Log.d(
TAG,
"New location update not possible within 30 seconds, not sending to HA"
)
return
}
}
} else {
Log.d(TAG, "Skipping location update due to old timestamp ${location.time} compared to $now")
return
}
lastLocationSend = now
lastUpdateLocation = updateLocation.gps.contentToString()

ioScope.launch {
try {
integrationUseCase.updateLocation(updateLocation)
Log.d(TAG, "Location update sent successfully")
} catch (e: Exception) {
Log.e(TAG, "Could not update location.", e)
}
Expand Down Expand Up @@ -305,12 +337,12 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
PowerManager.PARTIAL_WAKE_LOCK,
"HomeAssistant::AccurateLocation"
)?.apply { acquire(10 * 60 * 1000L /*10 minutes*/) }
override fun onLocationResult(locationResult: LocationResult?) {
override fun onLocationResult(locationResult: LocationResult) {
Log.d(
TAG,
"Got single accurate location update: ${locationResult?.lastLocation}"
"Got single accurate location update: ${locationResult.lastLocation}"
)
if (locationResult == null) {
if (locationResult.equals(null)) {
Log.w(TAG, "No location provided.")
return
}
Expand Down

0 comments on commit a6c9e6d

Please sign in to comment.