From fc79c4319c54cbcd5dbbb7221dfdd03d0515805b Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Mon, 29 Nov 2021 17:58:41 +0100 Subject: [PATCH] feat(geolocation): Error if Google Play Services are not available (#709) --- .../plugins/geolocation/Geolocation.java | 69 ++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/Geolocation.java b/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/Geolocation.java index 498a4fc8c..5823d8d73 100644 --- a/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/Geolocation.java +++ b/geolocation/android/src/main/java/com/capacitorjs/plugins/geolocation/Geolocation.java @@ -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; @@ -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"); } }