Skip to content

Commit

Permalink
feat(geolocation): Error if Google Play Services are not available (#709
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jcesarmobile authored Nov 29, 2021
1 parent 3beb75a commit fc79c43
Showing 1 changed file with 38 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.location.LocationManager;
import android.os.SystemClock;
import androidx.core.location.LocationManagerCompat;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
Expand Down Expand Up @@ -38,44 +40,49 @@ public void requestLocationUpdates(
final boolean getCurrentPosition,
final LocationResultCallback resultCallback
) {
clearLocationUpdates();
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
if (resultCode == ConnectionResult.SUCCESS) {
clearLocationUpdates();
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);

LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (LocationManagerCompat.isLocationEnabled(lm)) {
boolean networkEnabled = false;
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
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();
}
Location lastLocation = locationResult.getLastLocation();
if (lastLocation == null) {
resultCallback.error("location unavailable");
} else {
resultCallback.success(lastLocation);
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);
}
}
}
};
};

fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
} else {
resultCallback.error("location disabled");
}
} else {
resultCallback.error("location disabled");
resultCallback.error("Google Play Services not available");
}
}

Expand Down

0 comments on commit fc79c43

Please sign in to comment.