Using Debouncer to debounce location updates

This commit is contained in:
Maxime NATUREL 2022-06-03 11:14:34 +02:00
parent 5e6422b64c
commit a8c36e5e70
1 changed files with 16 additions and 10 deletions

View File

@ -24,10 +24,15 @@ import androidx.annotation.RequiresPermission
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.location.LocationListenerCompat import androidx.core.location.LocationListenerCompat
import im.vector.app.BuildConfig import im.vector.app.BuildConfig
import im.vector.app.core.utils.Debouncer
import im.vector.app.core.utils.createBackgroundHandler
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
private const val BKG_HANDLER_NAME = "LocationTracker.BKG_HANDLER_NAME"
private const val LOCATION_DEBOUNCE_ID = "LocationTracker.LOCATION_DEBOUNCE_ID"
@Singleton @Singleton
class LocationTracker @Inject constructor( class LocationTracker @Inject constructor(
context: Context context: Context
@ -44,14 +49,13 @@ 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
private val debouncer = Debouncer(createBackgroundHandler(BKG_HANDLER_NAME))
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION]) @RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
fun start() { fun start() {
Timber.d("## LocationTracker. start()") Timber.d("## LocationTracker. start()")
hasGpsProviderLiveLocation = false
if (locationManager == null) { if (locationManager == null) {
callbacks.forEach { it.onLocationProviderIsNotAvailable() } callbacks.forEach { it.onLocationProviderIsNotAvailable() }
@ -96,6 +100,8 @@ class LocationTracker @Inject constructor(
Timber.d("## LocationTracker. stop()") Timber.d("## LocationTracker. stop()")
locationManager?.removeUpdates(this) locationManager?.removeUpdates(this)
callbacks.clear() callbacks.clear()
debouncer.cancelAll()
hasGpsProviderLiveLocation = false
} }
/** /**
@ -139,18 +145,18 @@ class LocationTracker @Inject constructor(
} }
} }
val nowMillis = System.currentTimeMillis() debouncer.debounce(LOCATION_DEBOUNCE_ID, MIN_TIME_TO_UPDATE_LOCATION_MILLIS) {
val elapsedMillis = nowMillis - (lastLocationUpdateMillis ?: 0)
if (elapsedMillis >= MIN_TIME_TO_UPDATE_LOCATION_MILLIS) {
lastLocationUpdateMillis = nowMillis
notifyLocation(location) notifyLocation(location)
} else {
Timber.d("## LocationTracker. ignoring location: update is too fast")
} }
} }
private fun notifyLocation(location: Location) { private fun notifyLocation(location: Location) {
Timber.d("## LocationTracker. notify location") if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) {
Timber.d("## LocationTracker. notify location: $location")
} else {
Timber.d("## LocationTracker. notify location: ${location.provider}")
}
val locationData = location.toLocationData() val locationData = location.toLocationData()
lastLocation = locationData lastLocation = locationData
callbacks.forEach { it.onLocationUpdate(location.toLocationData()) } callbacks.forEach { it.onLocationUpdate(location.toLocationData()) }