From 500af3d29481beb39664a7d22722c5d3b8cd2478 Mon Sep 17 00:00:00 2001 From: Tenten Ponce Date: Sat, 17 Aug 2024 13:51:23 +0800 Subject: [PATCH 1/3] Added proper error handling on getResult of location settings --- .../geolocator/errors/ErrorCodes.java | 5 ++++ .../location/FusedLocationClient.java | 28 ++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/geolocator_android/android/src/main/java/com/baseflow/geolocator/errors/ErrorCodes.java b/geolocator_android/android/src/main/java/com/baseflow/geolocator/errors/ErrorCodes.java index cea4141e1..a8151c58f 100644 --- a/geolocator_android/android/src/main/java/com/baseflow/geolocator/errors/ErrorCodes.java +++ b/geolocator_android/android/src/main/java/com/baseflow/geolocator/errors/ErrorCodes.java @@ -4,6 +4,7 @@ public enum ErrorCodes { activityMissing, errorWhileAcquiringPosition, locationServicesDisabled, + locationServicesFailed, permissionDefinitionsNotFound, permissionDenied, permissionRequestInProgress; @@ -16,6 +17,8 @@ public String toString() { return "ERROR_WHILE_ACQUIRING_POSITION"; case locationServicesDisabled: return "LOCATION_SERVICES_DISABLED"; + case locationServicesFailed: + return "LOCATION_SERVICES_FAILED"; case permissionDefinitionsNotFound: return "PERMISSION_DEFINITIONS_NOT_FOUND"; case permissionDenied: @@ -35,6 +38,8 @@ public String toDescription() { return "An unexpected error occurred while trying to acquire the device's position."; case locationServicesDisabled: return "Location services are disabled. To receive location updates the location services should be enabled."; + case locationServicesFailed: + return "Location services failed."; case permissionDefinitionsNotFound: return "No location permissions are defined in the manifest. Make sure at least ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION are defined in the manifest."; case permissionDenied: diff --git a/geolocator_android/android/src/main/java/com/baseflow/geolocator/location/FusedLocationClient.java b/geolocator_android/android/src/main/java/com/baseflow/geolocator/location/FusedLocationClient.java index a5e3f2b13..16f498f2e 100644 --- a/geolocator_android/android/src/main/java/com/baseflow/geolocator/location/FusedLocationClient.java +++ b/geolocator_android/android/src/main/java/com/baseflow/geolocator/location/FusedLocationClient.java @@ -29,6 +29,7 @@ import com.google.android.gms.location.LocationSettingsStatusCodes; import com.google.android.gms.location.Priority; import com.google.android.gms.location.SettingsClient; +import com.google.android.gms.tasks.RuntimeExecutionException; import java.security.SecureRandom; @@ -169,17 +170,24 @@ public void isLocationServiceEnabled(LocationServiceListener listener) { listener.onLocationServiceError(ErrorCodes.locationServicesDisabled); } - LocationSettingsResponse lsr = response.getResult(); - if (lsr != null) { - LocationSettingsStates settingsStates = lsr.getLocationSettingsStates(); - boolean isGpsUsable = settingsStates != null && settingsStates.isGpsUsable(); - boolean isNetworkUsable = - settingsStates != null && settingsStates.isNetworkLocationUsable(); - listener.onLocationServiceResult(isGpsUsable || isNetworkUsable); - } else { - listener.onLocationServiceError(ErrorCodes.locationServicesDisabled); + try { + LocationSettingsResponse lsr = response.getResult(); + if (lsr != null) { + LocationSettingsStates settingsStates = lsr.getLocationSettingsStates(); + boolean isGpsUsable = settingsStates != null && settingsStates.isGpsUsable(); + boolean isNetworkUsable = + settingsStates != null && settingsStates.isNetworkLocationUsable(); + listener.onLocationServiceResult(isGpsUsable || isNetworkUsable); + } else { + listener.onLocationServiceError(ErrorCodes.locationServicesDisabled); + } + } catch (RuntimeExecutionException e) { + listener.onLocationServiceError(ErrorCodes.locationServicesFailed); } - }); + }) + .addOnFailureListener((e) -> { + listener.onLocationServiceError(ErrorCodes.locationServicesFailed); + }); } @SuppressLint("MissingPermission") From 12cb0cc9b761a512b2c5650ff1001a10a680faa0 Mon Sep 17 00:00:00 2001 From: Tenten Ponce Date: Sat, 17 Aug 2024 14:08:18 +0800 Subject: [PATCH 2/3] Updated changelog --- geolocator_android/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/geolocator_android/CHANGELOG.md b/geolocator_android/CHANGELOG.md index b0e5ab352..a713ed3e9 100644 --- a/geolocator_android/CHANGELOG.md +++ b/geolocator_android/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.6.2 + +* Fixes crash when `RuntimeExecutionException` is encountered in `getResult` in `checkLocationSettings`. + ## 4.6.1 * Fixes a bug where the plugin throws an exception while requesting locations updates with coarse location permission only. From 44bd0e3617e5b157c71c8df698a6ec2c5d8dd26a Mon Sep 17 00:00:00 2001 From: Tenten Ponce Date: Mon, 26 Aug 2024 00:57:45 +0800 Subject: [PATCH 3/3] Added return listener returns an error already --- .../com/baseflow/geolocator/location/FusedLocationClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/geolocator_android/android/src/main/java/com/baseflow/geolocator/location/FusedLocationClient.java b/geolocator_android/android/src/main/java/com/baseflow/geolocator/location/FusedLocationClient.java index 16f498f2e..c80572aa1 100644 --- a/geolocator_android/android/src/main/java/com/baseflow/geolocator/location/FusedLocationClient.java +++ b/geolocator_android/android/src/main/java/com/baseflow/geolocator/location/FusedLocationClient.java @@ -168,6 +168,7 @@ public void isLocationServiceEnabled(LocationServiceListener listener) { (response) -> { if (!response.isSuccessful()) { listener.onLocationServiceError(ErrorCodes.locationServicesDisabled); + return; } try {