From 755d743b063d0773026b68ebe1846d9d10bc1035 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 9 Jun 2022 09:54:49 +0200 Subject: [PATCH] Encapsulate callbacks calls into try/catch block --- .../app/features/location/LocationTracker.kt | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/location/LocationTracker.kt b/vector/src/main/java/im/vector/app/features/location/LocationTracker.kt index 3e0d60f2c8..66005e71c6 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationTracker.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationTracker.kt @@ -71,16 +71,16 @@ class LocationTracker @Inject constructor( Timber.d("start()") if (locationManager == null) { - callbacks.forEach { it.onNoLocationProviderAvailable() } Timber.v("LocationManager is not available") + onNoLocationProviderAvailable() return } val providers = locationManager.allProviders if (providers.isEmpty()) { - callbacks.forEach { it.onNoLocationProviderAvailable() } Timber.v("There is no location provider available") + onNoLocationProviderAvailable() } else { // Take GPS first providers.sortedByDescending { getProviderPriority(it) } @@ -133,7 +133,7 @@ class LocationTracker @Inject constructor( * Please ensure adding a callback to receive the value. */ fun requestLastKnownLocation() { - lastLocation?.let { location -> callbacks.forEach { it.onLocationUpdate(location) } } + lastLocation?.let { locationData -> onLocationUpdate(locationData) } } fun addCallback(callback: Callback) { @@ -190,7 +190,7 @@ class LocationTracker @Inject constructor( val locationData = location.toLocationData() lastLocation = locationData - callbacks.forEach { it.onLocationUpdate(location.toLocationData()) } + onLocationUpdate(locationData) } override fun onProviderDisabled(provider: String) { @@ -203,10 +203,31 @@ class LocationTracker @Inject constructor( locationManager?.allProviders ?.takeIf { it.isEmpty() } ?.let { - callbacks.forEach { it.onNoLocationProviderAvailable() } + Timber.e("all providers have been disabled") + onNoLocationProviderAvailable() } } + private fun onNoLocationProviderAvailable() { + callbacks.forEach { + try { + it.onNoLocationProviderAvailable() + } catch (error: Exception) { + Timber.e(error, "error in onNoLocationProviderAvailable callback $it") + } + } + } + + private fun onLocationUpdate(locationData: LocationData) { + callbacks.forEach { + try { + it.onLocationUpdate(locationData) + } catch (error: Exception) { + Timber.e(error, "error in onLocationUpdate callback $it") + } + } + } + private fun Location.toLocationData(): LocationData { return LocationData(latitude, longitude, accuracy.toDouble()) }