Debouncing location updates
This commit is contained in:
parent
7860173fa2
commit
c61412520d
|
@ -44,6 +44,8 @@ class LocationTracker @Inject constructor(
|
||||||
|
|
||||||
private var hasGpsProviderLiveLocation = false
|
private var hasGpsProviderLiveLocation = false
|
||||||
|
|
||||||
|
private var lastLocationUpdateMillis: Long? = null
|
||||||
|
|
||||||
private var lastLocation: LocationData? = null
|
private var lastLocation: LocationData? = null
|
||||||
|
|
||||||
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
|
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
|
||||||
|
@ -84,7 +86,7 @@ class LocationTracker @Inject constructor(
|
||||||
} else {
|
} else {
|
||||||
Timber.d("## LocationTracker. lastKnownLocation: ${latestKnownLocation.provider}")
|
Timber.d("## LocationTracker. lastKnownLocation: ${latestKnownLocation.provider}")
|
||||||
}
|
}
|
||||||
notifyLocation(latestKnownLocation, isLive = false)
|
notifyLocation(latestKnownLocation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,14 +125,10 @@ class LocationTracker @Inject constructor(
|
||||||
} else {
|
} else {
|
||||||
Timber.d("## LocationTracker. onLocationChanged: ${location.provider}")
|
Timber.d("## LocationTracker. onLocationChanged: ${location.provider}")
|
||||||
}
|
}
|
||||||
notifyLocation(location, isLive = true)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun notifyLocation(location: Location, isLive: Boolean) {
|
|
||||||
Timber.d("## LocationTracker. notify location")
|
|
||||||
when (location.provider) {
|
when (location.provider) {
|
||||||
LocationManager.GPS_PROVIDER -> {
|
LocationManager.GPS_PROVIDER -> {
|
||||||
hasGpsProviderLiveLocation = isLive
|
hasGpsProviderLiveLocation = true
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
if (hasGpsProviderLiveLocation) {
|
if (hasGpsProviderLiveLocation) {
|
||||||
|
@ -140,9 +138,22 @@ class LocationTracker @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val nowMillis = System.currentTimeMillis()
|
||||||
|
val elapsedMillis = nowMillis - (lastLocationUpdateMillis ?: 0)
|
||||||
|
if (elapsedMillis >= MIN_TIME_TO_UPDATE_LOCATION_MILLIS) {
|
||||||
|
lastLocationUpdateMillis = nowMillis
|
||||||
|
notifyLocation(location)
|
||||||
|
} else {
|
||||||
|
Timber.d("## LocationTracker. ignoring location: update is too fast")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun notifyLocation(location: Location) {
|
||||||
|
Timber.d("## LocationTracker. notify location")
|
||||||
val locationData = location.toLocationData()
|
val locationData = location.toLocationData()
|
||||||
lastLocation = locationData
|
lastLocation = locationData
|
||||||
callbacks.forEach { it.onLocationUpdate(locationData) }
|
callbacks.forEach { it.onLocationUpdate(location.toLocationData()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onProviderDisabled(provider: String) {
|
override fun onProviderDisabled(provider: String) {
|
||||||
|
|
Loading…
Reference in New Issue