Add synchronized annotations to protect from concurrent access to callbacks

This commit is contained in:
Maxime NATUREL 2022-06-13 12:02:21 +02:00
parent 162fd0a840
commit dee5dfd187

View File

@ -122,7 +122,9 @@ class LocationTracker @Inject constructor(
fun stop() { fun stop() {
Timber.d("stop()") Timber.d("stop()")
locationManager?.removeUpdates(this) locationManager?.removeUpdates(this)
callbacks.clear() synchronized(this) {
callbacks.clear()
}
debouncer.cancelAll() debouncer.cancelAll()
hasLocationFromGPSProvider = false hasLocationFromGPSProvider = false
hasLocationFromFusedProvider = false hasLocationFromFusedProvider = false
@ -136,12 +138,14 @@ class LocationTracker @Inject constructor(
lastLocation?.let { locationData -> onLocationUpdate(locationData) } lastLocation?.let { locationData -> onLocationUpdate(locationData) }
} }
@Synchronized
fun addCallback(callback: Callback) { fun addCallback(callback: Callback) {
if (!callbacks.contains(callback)) { if (!callbacks.contains(callback)) {
callbacks.add(callback) callbacks.add(callback)
} }
} }
@Synchronized
fun removeCallback(callback: Callback) { fun removeCallback(callback: Callback) {
callbacks.remove(callback) callbacks.remove(callback)
if (callbacks.size == 0) { if (callbacks.size == 0) {
@ -211,6 +215,7 @@ class LocationTracker @Inject constructor(
} }
} }
@Synchronized
private fun onNoLocationProviderAvailable() { private fun onNoLocationProviderAvailable() {
callbacks.forEach { callbacks.forEach {
try { try {
@ -221,6 +226,7 @@ class LocationTracker @Inject constructor(
} }
} }
@Synchronized
private fun onLocationUpdate(locationData: LocationData) { private fun onLocationUpdate(locationData: LocationData) {
callbacks.forEach { callbacks.forEach {
try { try {