Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added proper error handling on getResult of location settings #1557

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions geolocator_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum ErrorCodes {
activityMissing,
errorWhileAcquiringPosition,
locationServicesDisabled,
locationServicesFailed,
permissionDefinitionsNotFound,
permissionDenied,
permissionRequestInProgress;
Expand All @@ -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:
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -167,19 +168,27 @@ public void isLocationServiceEnabled(LocationServiceListener listener) {
(response) -> {
if (!response.isSuccessful()) {
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 {
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")
Expand Down
Loading