Skip to content

Fixed handling of disabled location service. #1698

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

Open
wants to merge 1 commit 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
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 @@ -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")
Expand Down