|
| 1 | +diff --git a/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/AndroidLocationManager.java b/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/AndroidLocationManager.java |
| 2 | +index 55d2619..eb57964 100644 |
| 3 | +--- a/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/AndroidLocationManager.java |
| 4 | ++++ b/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/AndroidLocationManager.java |
| 5 | +@@ -10,6 +10,7 @@ import android.location.LocationManager; |
| 6 | + import android.location.LocationProvider; |
| 7 | + import android.os.Bundle; |
| 8 | + import android.os.Handler; |
| 9 | ++import android.os.Looper; |
| 10 | + |
| 11 | + import androidx.core.content.ContextCompat; |
| 12 | + |
| 13 | +diff --git a/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/GeolocationModule.java b/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/GeolocationModule.java |
| 14 | +index 5b42ba4..4972f38 100644 |
| 15 | +--- a/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/GeolocationModule.java |
| 16 | ++++ b/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/GeolocationModule.java |
| 17 | +@@ -36,6 +36,7 @@ public class GeolocationModule extends ReactContextBaseJavaModule { |
| 18 | + super(reactContext); |
| 19 | + mConfiguration = Configuration.getDefault(); |
| 20 | + mLocationManager = new AndroidLocationManager(reactContext); |
| 21 | ++ onConfigurationChange(mConfiguration); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | +@@ -50,13 +51,27 @@ public class GeolocationModule extends ReactContextBaseJavaModule { |
| 26 | + |
| 27 | + private void onConfigurationChange(Configuration config) { |
| 28 | + ReactApplicationContext reactContext = mLocationManager.mReactContext; |
| 29 | ++ GoogleApiAvailability availability = new GoogleApiAvailability(); |
| 30 | ++ boolean hasPlayServices = |
| 31 | ++ availability.isGooglePlayServicesAvailable(reactContext.getApplicationContext()) |
| 32 | ++ == ConnectionResult.SUCCESS; |
| 33 | ++ |
| 34 | + if (Objects.equals(config.locationProvider, "android") && mLocationManager instanceof PlayServicesLocationManager) { |
| 35 | + mLocationManager = new AndroidLocationManager(reactContext); |
| 36 | +- } else if (Objects.equals(config.locationProvider, "playServices") && mLocationManager instanceof AndroidLocationManager) { |
| 37 | +- GoogleApiAvailability availability = new GoogleApiAvailability(); |
| 38 | +- if (availability.isGooglePlayServicesAvailable(reactContext.getApplicationContext()) == ConnectionResult.SUCCESS) { |
| 39 | +- mLocationManager = new PlayServicesLocationManager(reactContext); |
| 40 | +- } |
| 41 | ++ return; |
| 42 | ++ } |
| 43 | ++ |
| 44 | ++ if ((Objects.equals(config.locationProvider, "playServices") || Objects.equals(config.locationProvider, "auto")) |
| 45 | ++ && hasPlayServices |
| 46 | ++ && mLocationManager instanceof AndroidLocationManager) { |
| 47 | ++ mLocationManager = new PlayServicesLocationManager(reactContext); |
| 48 | ++ return; |
| 49 | ++ } |
| 50 | ++ |
| 51 | ++ if (Objects.equals(config.locationProvider, "auto") |
| 52 | ++ && !hasPlayServices |
| 53 | ++ && mLocationManager instanceof PlayServicesLocationManager) { |
| 54 | ++ mLocationManager = new AndroidLocationManager(reactContext); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +diff --git a/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/PlayServicesLocationManager.java b/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/PlayServicesLocationManager.java |
| 59 | +index 423f7a5..cbde4be 100644 |
| 60 | +--- a/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/PlayServicesLocationManager.java |
| 61 | ++++ b/node_modules/@react-native-community/geolocation/android/src/main/java/com/reactnativecommunity/geolocation/PlayServicesLocationManager.java |
| 62 | +@@ -3,6 +3,7 @@ package com.reactnativecommunity.geolocation; |
| 63 | + import android.annotation.SuppressLint; |
| 64 | + import android.app.Activity; |
| 65 | + import android.location.Location; |
| 66 | ++import android.os.Handler; |
| 67 | + import android.os.Looper; |
| 68 | + import android.util.Log; |
| 69 | + import android.content.Context; |
| 70 | +@@ -46,8 +47,8 @@ public class PlayServicesLocationManager extends BaseLocationManager { |
| 71 | + Activity currentActivity = mReactContext.getCurrentActivity(); |
| 72 | + |
| 73 | + if (currentActivity == null) { |
| 74 | +- mSingleLocationCallback = createSingleLocationCallback(success, error); |
| 75 | +- checkLocationSettings(options, mSingleLocationCallback, error); |
| 76 | ++ mSingleLocationCallback = createSingleLocationCallback(success, error, locationOptions.maximumAge, locationOptions.timeout); |
| 77 | ++ checkLocationSettings(options, mSingleLocationCallback, error, true); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | +@@ -57,8 +58,8 @@ public class PlayServicesLocationManager extends BaseLocationManager { |
| 82 | + if (location != null && (SystemClock.currentTimeMillis() - location.getTime()) < locationOptions.maximumAge) { |
| 83 | + success.invoke(locationToMap(location)); |
| 84 | + } else { |
| 85 | +- mSingleLocationCallback = createSingleLocationCallback(success, error); |
| 86 | +- checkLocationSettings(options, mSingleLocationCallback, error); |
| 87 | ++ mSingleLocationCallback = createSingleLocationCallback(success, error, locationOptions.maximumAge, locationOptions.timeout); |
| 88 | ++ checkLocationSettings(options, mSingleLocationCallback, error, true); |
| 89 | + } |
| 90 | + }); |
| 91 | + } catch (SecurityException e) { |
| 92 | +@@ -88,7 +89,7 @@ public class PlayServicesLocationManager extends BaseLocationManager { |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | +- checkLocationSettings(options, mLocationCallback, null); |
| 97 | ++ checkLocationSettings(options, mLocationCallback, null, false); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | +@@ -99,17 +100,19 @@ public class PlayServicesLocationManager extends BaseLocationManager { |
| 102 | + mFusedLocationClient.removeLocationUpdates(mLocationCallback); |
| 103 | + } |
| 104 | + |
| 105 | +- private void checkLocationSettings(ReadableMap options, LocationCallback locationCallback, Callback error) { |
| 106 | ++ private void checkLocationSettings(ReadableMap options, LocationCallback locationCallback, Callback error, boolean isSingleRequest) { |
| 107 | + LocationOptions locationOptions = LocationOptions.fromReactMap(options); |
| 108 | +- LocationRequest.Builder requestBuilder = new LocationRequest.Builder(locationOptions.interval); |
| 109 | ++ LocationRequest.Builder requestBuilder = new LocationRequest.Builder(isSingleRequest ? 0 : locationOptions.interval); |
| 110 | + requestBuilder.setPriority(locationOptions.highAccuracy ? Priority.PRIORITY_HIGH_ACCURACY : Priority.PRIORITY_LOW_POWER); |
| 111 | + requestBuilder.setMaxUpdateAgeMillis((long) locationOptions.maximumAge); |
| 112 | + |
| 113 | +- if (locationOptions.fastestInterval >= 0) { |
| 114 | ++ if (!isSingleRequest && locationOptions.fastestInterval >= 0) { |
| 115 | + requestBuilder.setMinUpdateIntervalMillis(locationOptions.fastestInterval); |
| 116 | + } |
| 117 | + |
| 118 | +- if (locationOptions.distanceFilter >= 0) { |
| 119 | ++ if (isSingleRequest) { |
| 120 | ++ requestBuilder.setMinUpdateDistanceMeters(0f); |
| 121 | ++ } else if (locationOptions.distanceFilter >= 0) { |
| 122 | + requestBuilder.setMinUpdateDistanceMeters(locationOptions.distanceFilter); |
| 123 | + } |
| 124 | + LocationRequest locationRequest = requestBuilder.build(); |
| 125 | +@@ -152,8 +155,18 @@ public class PlayServicesLocationManager extends BaseLocationManager { |
| 126 | + return locationManager != null && (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)); |
| 127 | + } |
| 128 | + |
| 129 | +- private LocationCallback createSingleLocationCallback(Callback success, Callback error) { |
| 130 | ++ private LocationCallback createSingleLocationCallback(Callback success, Callback error, double maximumAge, long timeout) { |
| 131 | + final CallbackHolder callbackHolder = new CallbackHolder(success, error); |
| 132 | ++ final Handler timeoutHandler = new Handler(Looper.getMainLooper()); |
| 133 | ++ final Runnable timeoutRunnable = () -> { |
| 134 | ++ callbackHolder.error(PositionError.buildError(PositionError.TIMEOUT, "Location request timed out")); |
| 135 | ++ if (mSingleLocationCallback != null) { |
| 136 | ++ mFusedLocationClient.removeLocationUpdates(mSingleLocationCallback); |
| 137 | ++ mSingleLocationCallback = null; |
| 138 | ++ } |
| 139 | ++ }; |
| 140 | ++ |
| 141 | ++ timeoutHandler.postDelayed(timeoutRunnable, timeout); |
| 142 | + |
| 143 | + return new LocationCallback() { |
| 144 | + @Override |
| 145 | +@@ -161,10 +174,10 @@ public class PlayServicesLocationManager extends BaseLocationManager { |
| 146 | + Location location = locationResult.getLastLocation(); |
| 147 | + |
| 148 | + if (location == null) { |
| 149 | +- callbackHolder.error(PositionError.buildError(PositionError.POSITION_UNAVAILABLE, "No location provided (FusedLocationProvider/lastLocation).")); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | ++ timeoutHandler.removeCallbacks(timeoutRunnable); |
| 154 | + callbackHolder.success(location); |
| 155 | + |
| 156 | + mFusedLocationClient.removeLocationUpdates(mSingleLocationCallback); |
| 157 | +@@ -174,7 +187,7 @@ public class PlayServicesLocationManager extends BaseLocationManager { |
| 158 | + @Override |
| 159 | + public void onLocationAvailability(@NonNull LocationAvailability locationAvailability) { |
| 160 | + if (!locationAvailability.isLocationAvailable()) { |
| 161 | +- callbackHolder.error(PositionError.buildError(PositionError.POSITION_UNAVAILABLE, "Location not available (FusedLocationProvider/lastLocation).")); |
| 162 | ++ return; |
| 163 | + } |
| 164 | + } |
| 165 | + }; |
0 commit comments