Skip to content

Commit

Permalink
feat(geolocation): Throw error if location is disabled (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Sep 1, 2021
1 parent 27b2381 commit 14724c5
Showing 1 changed file with 31 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import androidx.core.location.LocationManagerCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationAvailability;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
Expand Down Expand Up @@ -41,45 +41,41 @@ public void requestLocationUpdates(
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);

LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean networkEnabled = false;
if (LocationManagerCompat.isLocationEnabled(lm)) {
boolean networkEnabled = false;

try {
networkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {}
try {
networkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {}

LocationRequest locationRequest = new LocationRequest();
locationRequest.setMaxWaitTime(timeout);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
int lowPriority = networkEnabled ? LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY : LocationRequest.PRIORITY_LOW_POWER;
int priority = enableHighAccuracy ? LocationRequest.PRIORITY_HIGH_ACCURACY : lowPriority;
locationRequest.setPriority(priority);
LocationRequest locationRequest = new LocationRequest();
locationRequest.setMaxWaitTime(timeout);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
int lowPriority = networkEnabled ? LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY : LocationRequest.PRIORITY_LOW_POWER;
int priority = enableHighAccuracy ? LocationRequest.PRIORITY_HIGH_ACCURACY : lowPriority;
locationRequest.setPriority(priority);

locationCallback =
new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (getCurrentPosition) {
clearLocationUpdates();
locationCallback =
new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (getCurrentPosition) {
clearLocationUpdates();
}
Location lastLocation = locationResult.getLastLocation();
if (lastLocation == null) {
resultCallback.error("location unavailable");
} else {
resultCallback.success(lastLocation);
}
}
Location lastLocation = locationResult.getLastLocation();
if (lastLocation == null) {
resultCallback.error("location unavailable");
} else {
resultCallback.success(lastLocation);
}
}

@Override
public void onLocationAvailability(LocationAvailability availability) {
if (!availability.isLocationAvailable()) {
resultCallback.error("location unavailable");
clearLocationUpdates();
}
}
};
};

fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
} else {
resultCallback.error("location disabled");
}
}

public void clearLocationUpdates() {
Expand Down

0 comments on commit 14724c5

Please sign in to comment.