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..f346ed33e 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; @@ -161,25 +162,38 @@ private void requestPositionUpdates(LocationOptions locationOptions) { @Override public void isLocationServiceEnabled(LocationServiceListener listener) { + Log.d(TAG, "Checking location services"); LocationServices.getSettingsClient(context) .checkLocationSettings(new LocationSettingsRequest.Builder().build()) .addOnCompleteListener( (response) -> { if (!response.isSuccessful()) { - listener.onLocationServiceError(ErrorCodes.locationServicesDisabled); + Log.d(TAG, "OnComplete: Location settings response failed"); + // listener.onLocationServiceError(ErrorCodes.locationServicesDisabled); + return; } - 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 { + Log.d(TAG, "Getting location settings response"); + 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) { + Log.e(TAG, "RuntimeExecutionException: " + e.getMessage()); + listener.onLocationServiceError(ErrorCodes.locationServicesFailed); } - }); + }) + .addOnFailureListener((e) -> { + Log.d(TAG, "OnFailure: Location settings response failed"); + listener.onLocationServiceError(ErrorCodes.locationServicesFailed); + }); } @SuppressLint("MissingPermission")