|
3 | 3 | import android.annotation.SuppressLint;
|
4 | 4 | import android.app.Activity;
|
5 | 5 | import android.location.Location;
|
6 |
| -import android.os.Build; |
7 | 6 | import android.os.Looper;
|
8 | 7 | import android.util.Log;
|
9 | 8 | import android.content.Context;
|
10 | 9 | import android.location.LocationManager;
|
11 | 10 |
|
12 |
| -import androidx.annotation.RequiresApi; |
| 11 | +import androidx.annotation.NonNull; |
13 | 12 |
|
14 | 13 | import com.facebook.react.bridge.Callback;
|
15 | 14 | import com.facebook.react.bridge.ReactApplicationContext;
|
16 | 15 | import com.facebook.react.bridge.ReadableMap;
|
| 16 | +import com.facebook.react.bridge.WritableMap; |
17 | 17 | import com.facebook.react.common.SystemClock;
|
18 | 18 | import com.facebook.react.modules.core.DeviceEventManagerModule;
|
19 | 19 | import com.google.android.gms.location.FusedLocationProviderClient;
|
|
23 | 23 | import com.google.android.gms.location.LocationResult;
|
24 | 24 | import com.google.android.gms.location.LocationServices;
|
25 | 25 | import com.google.android.gms.location.LocationSettingsRequest;
|
26 |
| -import com.google.android.gms.location.LocationSettingsResponse; |
27 |
| -import com.google.android.gms.location.Priority; |
28 | 26 | import com.google.android.gms.location.SettingsClient;
|
29 |
| -import com.google.android.gms.tasks.OnSuccessListener; |
30 |
| - |
31 |
| -import java.util.function.Consumer; |
32 |
| -import java.util.function.Function; |
33 | 27 |
|
34 | 28 | @SuppressLint("MissingPermission")
|
35 | 29 | public class PlayServicesLocationManager extends BaseLocationManager {
|
@@ -157,28 +151,58 @@ private boolean isAnyProviderAvailable() {
|
157 | 151 | return locationManager != null && (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
|
158 | 152 | }
|
159 | 153 |
|
160 |
| - private LocationCallback createSingleLocationCallback(Callback success, Callback error){ |
| 154 | + private LocationCallback createSingleLocationCallback(Callback success, Callback error) { |
| 155 | + final CallbackHolder callbackHolder = new CallbackHolder(success, error); |
| 156 | + |
161 | 157 | return new LocationCallback() {
|
162 | 158 | @Override
|
163 |
| - public void onLocationResult(LocationResult locationResult) { |
164 |
| - if (locationResult == null) { |
165 |
| - error.invoke(PositionError.buildError(PositionError.POSITION_UNAVAILABLE, "No location provided (FusedLocationProvider/lastLocation).")); |
| 159 | + public void onLocationResult(@NonNull LocationResult locationResult) { |
| 160 | + Location location = locationResult.getLastLocation(); |
| 161 | + |
| 162 | + if (location == null) { |
| 163 | + callbackHolder.error(PositionError.buildError(PositionError.POSITION_UNAVAILABLE, "No location provided (FusedLocationProvider/lastLocation).")); |
166 | 164 | return;
|
167 | 165 | }
|
168 | 166 |
|
169 |
| - Location location = locationResult.getLastLocation(); |
170 |
| - success.invoke(locationToMap(location)); |
| 167 | + callbackHolder.success(location); |
171 | 168 |
|
172 | 169 | mFusedLocationClient.removeLocationUpdates(mSingleLocationCallback);
|
173 | 170 | mSingleLocationCallback = null;
|
174 | 171 | }
|
175 | 172 |
|
176 | 173 | @Override
|
177 |
| - public void onLocationAvailability(LocationAvailability locationAvailability) { |
| 174 | + public void onLocationAvailability(@NonNull LocationAvailability locationAvailability) { |
178 | 175 | if (!locationAvailability.isLocationAvailable()) {
|
179 |
| - error.invoke(PositionError.buildError(PositionError.POSITION_UNAVAILABLE, "Location not available (FusedLocationProvider/lastLocation).")); |
| 176 | + callbackHolder.error(PositionError.buildError(PositionError.POSITION_UNAVAILABLE, "Location not available (FusedLocationProvider/lastLocation).")); |
180 | 177 | }
|
181 | 178 | }
|
182 | 179 | };
|
183 | 180 | }
|
| 181 | + |
| 182 | + private static class CallbackHolder { |
| 183 | + Callback success; |
| 184 | + Callback error; |
| 185 | + public CallbackHolder(Callback success, Callback error) { |
| 186 | + this.success = success; |
| 187 | + this.error = error; |
| 188 | + } |
| 189 | + |
| 190 | + public void error(WritableMap cause) { |
| 191 | + if (this.error == null) { |
| 192 | + Log.e(this.getClass().getSimpleName(), "tried to invoke null error callback -> " + cause.toString()); |
| 193 | + return; |
| 194 | + } |
| 195 | + this.error.invoke(cause); |
| 196 | + this.error = null; |
| 197 | + } |
| 198 | + |
| 199 | + public void success(Location location) { |
| 200 | + if (this.success == null) { |
| 201 | + Log.e(this.getClass().getSimpleName(), "tried to invoke null success callback"); |
| 202 | + return; |
| 203 | + } |
| 204 | + this.success.invoke(locationToMap(location)); |
| 205 | + this.success = null; |
| 206 | + } |
| 207 | + } |
184 | 208 | }
|
0 commit comments