Skip to content

Added proper error handling on getResult of location settings #1557

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

Closed
Closed
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
Original file line number Diff line number Diff line change
@@ -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:
Original file line number Diff line number Diff line change
@@ -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;

@@ -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")