From 893afc768e97a0eb83a3bb378a17268047392fcb Mon Sep 17 00:00:00 2001 From: pinpong Date: Fri, 27 May 2022 10:00:26 +0200 Subject: [PATCH] added showLocationDialog function --- README.md | 3 +++ .../RNFusedLocation/FusedLocationProvider.java | 14 +++++++++----- .../RNFusedLocation/LocationManagerProvider.java | 5 +++++ .../agontuk/RNFusedLocation/LocationProvider.java | 2 ++ .../RNFusedLocation/RNFusedLocationModule.java | 7 +++++++ index.d.ts | 2 ++ js/Geolocation.js | 4 ++++ js/Geolocation.native.js | 8 ++++++++ 8 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 81c5a66..dc71b06 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,9 @@ When promise resolves, returns the status of the authorization. #### `stopObserving()` Stops observing for device location changes. In addition, it removes all listeners previously registered. +#### `showLocationDialog()` +Show location enable dialog (android only). + # Error Codes | Name | Code | Description | | --- | --- | --- | diff --git a/android/src/main/java/com/agontuk/RNFusedLocation/FusedLocationProvider.java b/android/src/main/java/com/agontuk/RNFusedLocation/FusedLocationProvider.java index e502919..949d55b 100644 --- a/android/src/main/java/com/agontuk/RNFusedLocation/FusedLocationProvider.java +++ b/android/src/main/java/com/agontuk/RNFusedLocation/FusedLocationProvider.java @@ -106,13 +106,13 @@ public void onSuccess(Location location) { return; } - checkLocationSettings(); + checkLocationSettings(locationOptions.isShowLocationDialog()); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { - checkLocationSettings(); + checkLocationSettings(locationOptions.isShowLocationDialog()); } }); } @@ -149,7 +149,7 @@ public void requestLocationUpdates(LocationOptions locationOptions) { this.isSingleUpdate = false; this.locationOptions = locationOptions; this.locationRequest = buildLocationRequest(locationOptions); - checkLocationSettings(); + checkLocationSettings(locationOptions.isShowLocationDialog()); } @Override @@ -169,7 +169,11 @@ private LocationRequest buildLocationRequest(LocationOptions options) { return locationRequest; } - private void checkLocationSettings() { + public void showLocationDialog() { + checkLocationSettings(true); + } + + private void checkLocationSettings(boolean showLocationDialog) { LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.addLocationRequest(locationRequest); LocationSettingsRequest locationSettingsRequest = builder.build(); @@ -192,7 +196,7 @@ public void onFailure(@NonNull Exception e) { boolean forceRequestLocation = locationOptions.isForceRequestLocation(); boolean locationEnabled = LocationUtils.isLocationEnabled(context); - if (!showLocationDialog) { + if (!locationOptions.isShowLocationDialog()) { if (forceRequestLocation && locationEnabled) { startLocationUpdates(); } else { diff --git a/android/src/main/java/com/agontuk/RNFusedLocation/LocationManagerProvider.java b/android/src/main/java/com/agontuk/RNFusedLocation/LocationManagerProvider.java index c05b8c1..604eb68 100644 --- a/android/src/main/java/com/agontuk/RNFusedLocation/LocationManagerProvider.java +++ b/android/src/main/java/com/agontuk/RNFusedLocation/LocationManagerProvider.java @@ -140,6 +140,11 @@ public void removeLocationUpdates() { locationManager.removeUpdates(locationListener); } + @Override + public void showLocationDialog() { + /// ignore + } + @Nullable private String getBestProvider(LocationAccuracy locationAccuracy) { Criteria criteria = getProviderCriteria(locationAccuracy); diff --git a/android/src/main/java/com/agontuk/RNFusedLocation/LocationProvider.java b/android/src/main/java/com/agontuk/RNFusedLocation/LocationProvider.java index 4e7b161..4dbbcff 100644 --- a/android/src/main/java/com/agontuk/RNFusedLocation/LocationProvider.java +++ b/android/src/main/java/com/agontuk/RNFusedLocation/LocationProvider.java @@ -8,4 +8,6 @@ public interface LocationProvider { void requestLocationUpdates(LocationOptions locationOptions); void removeLocationUpdates(); + + void showLocationDialog(); } diff --git a/android/src/main/java/com/agontuk/RNFusedLocation/RNFusedLocationModule.java b/android/src/main/java/com/agontuk/RNFusedLocation/RNFusedLocationModule.java index 414e2be..91d2322 100644 --- a/android/src/main/java/com/agontuk/RNFusedLocation/RNFusedLocationModule.java +++ b/android/src/main/java/com/agontuk/RNFusedLocation/RNFusedLocationModule.java @@ -141,6 +141,13 @@ public void stopObserving() { } } + @ReactMethod + public void showLocationDialog() { + if (continuousLocationProvider != null) { + continuousLocationProvider.showLocationDialog(); + } + } + @ReactMethod public void addListener(String eventName) { // Keep: Required for RN built in Event Emitter Calls. diff --git a/index.d.ts b/index.d.ts index 313049e..20f4cfc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -96,4 +96,6 @@ declare module "react-native-geolocation-service" { export function clearWatch(watchID: number): void; export function stopObserving(): void; + + export function showLocationDialog(): void; } diff --git a/js/Geolocation.js b/js/Geolocation.js index 3fc7eba..172c3f5 100644 --- a/js/Geolocation.js +++ b/js/Geolocation.js @@ -40,6 +40,10 @@ const Geolocation = { stopObserving: function () { throw new Error('Method not supported by browser'); + }, + + showLocationDialog: function () { + throw new Error('Method not supported by browser'); } }; diff --git a/js/Geolocation.native.js b/js/Geolocation.native.js index 89ccb4f..79cef42 100644 --- a/js/Geolocation.native.js +++ b/js/Geolocation.native.js @@ -108,6 +108,14 @@ const Geolocation = { subscriptions = []; } + }, + + showLocationDialog: () => { + if (Platform.OS === "ios") { + console.warn("ShowLocationDialog not supported on IOS"); + } else { + RNFusedLocation.showLocationDialog(); + } } };