diff --git a/android/src/main/java/io/radar/react/RNRadarModule.java b/android/src/main/java/io/radar/react/RNRadarModule.java index 4e117954..3b1e795b 100644 --- a/android/src/main/java/io/radar/react/RNRadarModule.java +++ b/android/src/main/java/io/radar/react/RNRadarModule.java @@ -9,8 +9,6 @@ import android.os.Build; import android.util.Log; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.core.app.ActivityCompat; import com.facebook.react.bridge.Arguments; @@ -27,17 +25,11 @@ import io.radar.sdk.RadarTrackingOptions; import io.radar.sdk.RadarTrackingOptions.RadarTrackingOptionsForegroundService; import io.radar.sdk.RadarTripOptions; -import io.radar.sdk.RadarVerifiedReceiver; import io.radar.sdk.model.RadarAddress; -import io.radar.sdk.model.RadarContext; import io.radar.sdk.model.RadarEvent; import io.radar.sdk.model.RadarGeofence; import io.radar.sdk.model.RadarPlace; -import io.radar.sdk.model.RadarRouteMatrix; -import io.radar.sdk.model.RadarRoutes; import io.radar.sdk.model.RadarTrip; -import io.radar.sdk.model.RadarUser; -import io.radar.sdk.model.RadarVerifiedLocationToken; import io.radar.sdk.RadarNotificationOptions; import org.json.JSONException; @@ -266,42 +258,35 @@ public void requestPermissions(boolean background, final Promise promise) { @ReactMethod public void getLocation(String desiredAccuracy, final Promise promise) { - RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy accuracyLevel = RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy.MEDIUM; - String accuracy = desiredAccuracy != null ? desiredAccuracy.toLowerCase() : "medium"; - - if (accuracy.equals("low")) { - accuracyLevel = RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy.LOW; - } else if (accuracy.equals("medium")) { - accuracyLevel = RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy.MEDIUM; - } else if (accuracy.equals("high")) { - accuracyLevel = RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy.HIGH; - } else { + RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy accuracyLevel; + try { + String accuracy = desiredAccuracy != null ? desiredAccuracy.toUpperCase() : "MEDIUM"; + accuracyLevel = RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy.valueOf(accuracy); + } catch (IllegalArgumentException e) { promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); + return; } - Radar.getLocation(accuracyLevel, new Radar.RadarLocationCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable Location location, boolean stopped) { - if (promise == null) { - return; - } + Radar.getLocation(accuracyLevel, (status, location, stopped) -> { + if (promise == null) { + return; + } - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (location != null) { - map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location))); - } - map.putBoolean("stopped", stopped); - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (location != null) { + map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location))); } - } else { - promise.reject(status.toString(), status.toString()); + map.putBoolean("stopped", stopped); + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }); } @@ -314,8 +299,8 @@ public void trackOnce(ReadableMap optionsMap, final Promise promise) { boolean beaconsTrackingOption = false; if (optionsMap != null) { - if (optionsMap.hasKey("location")) { - ReadableMap locationMap = optionsMap.getMap("location"); + ReadableMap locationMap = optionsMap.getMap("location"); + if (locationMap != null) { location = new Location("RNRadarModule"); double latitude = locationMap.getDouble("latitude"); double longitude = locationMap.getDouble("longitude"); @@ -324,51 +309,41 @@ public void trackOnce(ReadableMap optionsMap, final Promise promise) { location.setLongitude(longitude); location.setAccuracy(accuracy); } - if (optionsMap.hasKey("desiredAccuracy")) { - String desiredAccuracy = optionsMap.getString("desiredAccuracy").toLowerCase(); - if (desiredAccuracy.equals("none")) { - accuracyLevel = RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy.NONE; - } else if (desiredAccuracy.equals("low")) { - accuracyLevel = RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy.LOW; - } else if (desiredAccuracy.equals("medium")) { - accuracyLevel = RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy.MEDIUM; - } else if (desiredAccuracy.equals("high")) { - accuracyLevel = RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy.HIGH; - } + String desiredAccuracy = optionsMap.getString("desiredAccuracy"); + if (desiredAccuracy != null) { + String accuracy = desiredAccuracy.toUpperCase(); + accuracyLevel = RadarTrackingOptions.RadarTrackingOptionsDesiredAccuracy.valueOf(accuracy); } if (optionsMap.hasKey("beacons")) { beaconsTrackingOption = optionsMap.getBoolean("beacons"); } } - Radar.RadarTrackCallback trackCallback = new Radar.RadarTrackCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable Location location, @Nullable RadarEvent[] events, @Nullable RadarUser user) { - if (promise == null) { - return; - } + Radar.RadarTrackCallback trackCallback = (status, location1, events, user) -> { + if (promise == null) { + return; + } - try { - if (status == Radar.RadarStatus.SUCCESS) { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (location != null) { - map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location))); - } - if (events != null) { - map.putArray("events", RNRadarUtils.arrayForJson(RadarEvent.toJson(events))); - } - if (user != null) { - map.putMap("user", RNRadarUtils.mapForJson(user.toJson())); - } - promise.resolve(map); - } else { - promise.reject(status.toString(), status.toString()); + try { + if (status == Radar.RadarStatus.SUCCESS) { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (location1 != null) { + map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location1))); } - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + if (events != null) { + map.putArray("events", RNRadarUtils.arrayForJson(RadarEvent.toJson(events))); + } + if (user != null) { + map.putMap("user", RNRadarUtils.mapForJson(user.toJson())); + } + promise.resolve(map); + } else { + promise.reject(status.toString(), status.toString()); } + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } }; @@ -390,9 +365,8 @@ public void trackVerified(ReadableMap optionsMap, final Promise promise) { } } - Radar.RadarTrackVerifiedCallback trackCallback = new Radar.RadarTrackVerifiedCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarVerifiedLocationToken token) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + Radar.trackVerified(beaconsTrackingOption, (status, token) -> { if (promise == null) { return; } @@ -412,17 +386,14 @@ public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarVerifie Log.e(TAG, "JSONException", e); promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } - } - }; - - Radar.trackVerified(beaconsTrackingOption, trackCallback); + }); + } } @ReactMethod public void getVerifiedLocationToken(final Promise promise) { - Radar.RadarTrackVerifiedCallback trackCallback = new Radar.RadarTrackVerifiedCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarVerifiedLocationToken token) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + Radar.getVerifiedLocationToken((status, token) -> { if (promise == null) { return; } @@ -442,10 +413,8 @@ public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarVerifie Log.e(TAG, "JSONException", e); promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } - } - }; - - Radar.getVerifiedLocationToken(trackCallback); + }); + } } @ReactMethod @@ -484,7 +453,9 @@ public void startTrackingVerified(ReadableMap optionsMap) { interval = optionsMap.hasKey("interval") ? optionsMap.getInt("interval") : interval; } - Radar.startTrackingVerified(interval, beacons); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + Radar.startTrackingVerified(interval, beacons); + } } @ReactMethod @@ -502,22 +473,17 @@ public void mockTracking(ReadableMap optionsMap) { destination.setLatitude(destinationLatitude); destination.setLongitude(destinationLongitude); String modeStr = optionsMap.getString("mode"); - Radar.RadarRouteMode mode = Radar.RadarRouteMode.CAR; - if (modeStr.equals("FOOT") || modeStr.equals("foot")) { - mode = Radar.RadarRouteMode.FOOT; - } else if (modeStr.equals("BIKE") || modeStr.equals("bike")) { - mode = Radar.RadarRouteMode.BIKE; - } else if (modeStr.equals("CAR") || modeStr.equals("car")) { + Radar.RadarRouteMode mode; + try { + mode = Radar.RadarRouteMode.valueOf(modeStr != null ? modeStr.toUpperCase() : "CAR"); + } catch (IllegalArgumentException ignored) { mode = Radar.RadarRouteMode.CAR; } int steps = optionsMap.hasKey("steps") ? optionsMap.getInt("steps") : 10; int interval = optionsMap.hasKey("interval") ? optionsMap.getInt("interval") : 1; - Radar.mockTracking(origin, destination, mode, steps, interval, new Radar.RadarTrackCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable Location location, @Nullable RadarEvent[] events, @Nullable RadarUser user) { + Radar.mockTracking(origin, destination, mode, steps, interval, (status, location, events, user) -> { - } }); } @@ -528,7 +494,9 @@ public void stopTracking() { @ReactMethod public void stopTrackingVerified() { - Radar.stopTrackingVerified(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + Radar.stopTrackingVerified(); + } } @ReactMethod @@ -626,43 +594,7 @@ public void startTrip(ReadableMap optionsMap, final Promise promise) { if (trackingOptionsJson != null) { trackingOptions = RadarTrackingOptions.fromJson(trackingOptionsJson); } - Radar.startTrip(options, trackingOptions, new Radar.RadarTripCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarTrip trip, @Nullable RadarEvent[] events) { - if (promise == null) { - return; - } - try { - if (status == Radar.RadarStatus.SUCCESS) { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (trip != null) { - map.putMap("trip", RNRadarUtils.mapForJson(trip.toJson())); - } - if (events != null) { - map.putArray("events", RNRadarUtils.arrayForJson(RadarEvent.toJson(events))); - } - promise.resolve(map); - } else { - promise.reject(status.toString(), status.toString()); - } - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); - } - } - }); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); - } - } - - @ReactMethod - public void completeTrip(final Promise promise) { - Radar.completeTrip(new Radar.RadarTripCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarTrip trip, @Nullable RadarEvent[] events) { + Radar.startTrip(options, trackingOptions, (status, trip, events) -> { if (promise == null) { return; } @@ -684,36 +616,63 @@ public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarTrip tr Log.e(TAG, "JSONException", e); promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + }); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); + } + } + + @ReactMethod + public void completeTrip(final Promise promise) { + Radar.completeTrip((status, trip, events) -> { + if (promise == null) { + return; + } + try { + if (status == Radar.RadarStatus.SUCCESS) { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (trip != null) { + map.putMap("trip", RNRadarUtils.mapForJson(trip.toJson())); + } + if (events != null) { + map.putArray("events", RNRadarUtils.arrayForJson(RadarEvent.toJson(events))); + } + promise.resolve(map); + } else { + promise.reject(status.toString(), status.toString()); + } + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } }); } @ReactMethod public void cancelTrip(final Promise promise) { - Radar.cancelTrip(new Radar.RadarTripCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarTrip trip, @Nullable RadarEvent[] events) { - if (promise == null) { - return; - } - try { - if (status == Radar.RadarStatus.SUCCESS) { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (trip != null) { - map.putMap("trip", RNRadarUtils.mapForJson(trip.toJson())); - } - if (events != null) { - map.putArray("events", RNRadarUtils.arrayForJson(RadarEvent.toJson(events))); - } - promise.resolve(map); - } else { - promise.reject(status.toString(), status.toString()); + Radar.cancelTrip((status, trip, events) -> { + if (promise == null) { + return; + } + try { + if (status == Radar.RadarStatus.SUCCESS) { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (trip != null) { + map.putMap("trip", RNRadarUtils.mapForJson(trip.toJson())); } - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + if (events != null) { + map.putArray("events", RNRadarUtils.arrayForJson(RadarEvent.toJson(events))); + } + promise.resolve(map); + } else { + promise.reject(status.toString(), status.toString()); } + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } }); } @@ -724,55 +683,36 @@ public void updateTrip(ReadableMap optionsMap, final Promise promise) { try { JSONObject optionsObj = RNRadarUtils.jsonForMap(optionsMap); RadarTripOptions options = RadarTripOptions.fromJson(optionsObj.getJSONObject("options")); - RadarTrip.RadarTripStatus status = RadarTrip.RadarTripStatus.UNKNOWN; - - if (optionsObj.has("status")) { + RadarTrip.RadarTripStatus status; + try { String statusStr = optionsObj.getString("status"); - if (statusStr != null) { - if (statusStr.equalsIgnoreCase("started")) { - status = RadarTrip.RadarTripStatus.STARTED; - } else if (statusStr.equalsIgnoreCase("approaching")) { - status = RadarTrip.RadarTripStatus.APPROACHING; - } else if (statusStr.equalsIgnoreCase("arrived")) { - status = RadarTrip.RadarTripStatus.ARRIVED; - } else if (statusStr.equalsIgnoreCase("completed")) { - status = RadarTrip.RadarTripStatus.COMPLETED; - } else if (statusStr.equalsIgnoreCase("canceled")) { - status = RadarTrip.RadarTripStatus.CANCELED; - } else if (statusStr.equalsIgnoreCase("unknown")) { - status = RadarTrip.RadarTripStatus.UNKNOWN; - } else { - promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); - } - } - } else { + status = RadarTrip.RadarTripStatus.valueOf(statusStr.toUpperCase()); + } catch (JSONException | IllegalArgumentException ignored) { promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); + return; } - Radar.updateTrip(options, status, new Radar.RadarTripCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarTrip trip, @Nullable RadarEvent[] events) { - if (promise == null) { - return; - } - try { - if (status == Radar.RadarStatus.SUCCESS) { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (trip != null) { - map.putMap("trip", RNRadarUtils.mapForJson(trip.toJson())); - } - if (events != null) { - map.putArray("events", RNRadarUtils.arrayForJson(RadarEvent.toJson(events))); - } - promise.resolve(map); - } else { - promise.reject(status.toString(), status.toString()); + Radar.updateTrip(options, status, (status1, trip, events) -> { + if (promise == null) { + return; + } + try { + if (status1 == Radar.RadarStatus.SUCCESS) { + WritableMap map = Arguments.createMap(); + map.putString("status", status1.toString()); + if (trip != null) { + map.putMap("trip", RNRadarUtils.mapForJson(trip.toJson())); + } + if (events != null) { + map.putArray("events", RNRadarUtils.arrayForJson(RadarEvent.toJson(events))); } - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + promise.resolve(map); + } else { + promise.reject(status1.toString(), status1.toString()); } + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } }); } catch (JSONException e) { @@ -787,27 +727,24 @@ public void getContext(final Promise promise) { return; } - Radar.getContext(new Radar.RadarContextCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable Location location, @Nullable RadarContext context) { - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (location != null) { - map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location))); - } - if (context != null) { - map.putMap("context", RNRadarUtils.mapForJson(context.toJson())); - } - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + Radar.getContext((status, location, context) -> { + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (location != null) { + map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location))); } - } else { - promise.reject(status.toString(), status.toString()); + if (context != null) { + map.putMap("context", RNRadarUtils.mapForJson(context.toJson())); + } + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }); } @@ -824,27 +761,24 @@ public void getContext(ReadableMap locationMap, final Promise promise) { location.setLatitude(latitude); location.setLongitude(longitude); - Radar.getContext(location, new Radar.RadarContextCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable Location location, @Nullable RadarContext context) { - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (location != null) { - map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location))); - } - if (context != null) { - map.putMap("context", RNRadarUtils.mapForJson(context.toJson())); - } - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + Radar.getContext(location, (status, location1, context) -> { + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (location1 != null) { + map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location1))); } - } else { - promise.reject(status.toString(), status.toString()); + if (context != null) { + map.putMap("context", RNRadarUtils.mapForJson(context.toJson())); + } + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }); } @@ -856,8 +790,8 @@ public void searchPlaces(ReadableMap optionsMap, final Promise promise) { } Location near = null; - if (optionsMap.hasKey("near")) { - ReadableMap nearMap = optionsMap.getMap("near"); + ReadableMap nearMap = optionsMap.getMap("near"); + if (nearMap != null) { double latitude = nearMap.getDouble("latitude"); double longitude = nearMap.getDouble("longitude"); near = new Location("RNRadarModule"); @@ -871,27 +805,24 @@ public void searchPlaces(ReadableMap optionsMap, final Promise promise) { String[] groups = optionsMap.hasKey("groups") ? RNRadarUtils.stringArrayForArray(optionsMap.getArray("groups")) : null; int limit = optionsMap.hasKey("limit") ? optionsMap.getInt("limit") : 10; - Radar.RadarSearchPlacesCallback callback = new Radar.RadarSearchPlacesCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable Location location, @Nullable RadarPlace[] places) { - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (location != null) { - map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location))); - } - if (places != null) { - map.putArray("places", RNRadarUtils.arrayForJson(RadarPlace.toJson(places))); - } - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + Radar.RadarSearchPlacesCallback callback = (status, location, places) -> { + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (location != null) { + map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location))); } - } else { - promise.reject(status.toString(), status.toString()); + if (places != null) { + map.putArray("places", RNRadarUtils.arrayForJson(RadarPlace.toJson(places))); + } + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }; @@ -909,8 +840,8 @@ public void searchGeofences(ReadableMap optionsMap, final Promise promise) { } Location near = null; - if (optionsMap.hasKey("near")) { - ReadableMap nearMap = optionsMap.getMap("near"); + ReadableMap nearMap = optionsMap.getMap("near"); + if (nearMap != null) { double latitude = nearMap.getDouble("latitude"); double longitude = nearMap.getDouble("longitude"); near = new Location("RNRadarModule"); @@ -932,29 +863,26 @@ public void searchGeofences(ReadableMap optionsMap, final Promise promise) { } int limit = optionsMap.hasKey("limit") ? optionsMap.getInt("limit") : 100; - boolean includeGeometry = optionsMap.hasKey("includeGeometry") ? optionsMap.getBoolean("includeGeometry") : false; + boolean includeGeometry = optionsMap.hasKey("includeGeometry") && optionsMap.getBoolean("includeGeometry"); - Radar.RadarSearchGeofencesCallback callback = new Radar.RadarSearchGeofencesCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable Location location, @Nullable RadarGeofence[] geofences) { - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (location != null) { - map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location))); - } - if (geofences != null) { - map.putArray("geofences", RNRadarUtils.arrayForJson(RadarGeofence.toJson(geofences))); - } - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + Radar.RadarSearchGeofencesCallback callback = (status, location, geofences) -> { + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (location != null) { + map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location))); } - } else { - promise.reject(status.toString(), status.toString()); + if (geofences != null) { + map.putArray("geofences", RNRadarUtils.arrayForJson(RadarGeofence.toJson(geofences))); + } + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }; @@ -971,14 +899,14 @@ public void autocomplete(ReadableMap optionsMap, final Promise promise) { return; } - if (!optionsMap.hasKey("query") || !optionsMap.hasKey("near")) { - promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); + String query = optionsMap.getString("query"); + ReadableMap nearMap = optionsMap.getMap("near"); + if (query == null || nearMap == null) { + promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); return; } - String query = optionsMap.getString("query"); - ReadableMap nearMap = optionsMap.getMap("near"); double latitude = nearMap.getDouble("latitude"); double longitude = nearMap.getDouble("longitude"); Location near = new Location("RNRadarModule"); @@ -988,26 +916,23 @@ public void autocomplete(ReadableMap optionsMap, final Promise promise) { String country = optionsMap.hasKey("countryCode") ? optionsMap.getString("countryCode") : optionsMap.hasKey("country") ? optionsMap.getString("country") : null; String[] layers = optionsMap.hasKey("layers") ? RNRadarUtils.stringArrayForArray(optionsMap.getArray("layers")) : null; - boolean mailable = optionsMap.hasKey("mailable") ? optionsMap.getBoolean("mailable") : false; + boolean mailable = optionsMap.hasKey("mailable") && optionsMap.getBoolean("mailable"); - Radar.autocomplete(query, near, layers, limit, country, true, mailable, new Radar.RadarGeocodeCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarAddress[] addresses) { - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (addresses != null) { - map.putArray("addresses", RNRadarUtils.arrayForJson(RadarAddress.toJson(addresses))); - } - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + Radar.autocomplete(query, near, layers, limit, country, true, mailable, (status, addresses) -> { + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (addresses != null) { + map.putArray("addresses", RNRadarUtils.arrayForJson(RadarAddress.toJson(addresses))); } - } else { - promise.reject(status.toString(), status.toString()); + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }); } @@ -1018,34 +943,30 @@ public void geocode(ReadableMap optionsMap, final Promise promise) { return; } - if (!optionsMap.hasKey("address")) { + String address = optionsMap.getString("address"); + if (address == null) { promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); - return; } - String address = optionsMap.getString("address"); String[] layers = optionsMap.hasKey("layers") ? RNRadarUtils.stringArrayForArray(optionsMap.getArray("layers")) : null; String[] countries = optionsMap.hasKey("countries") ? RNRadarUtils.stringArrayForArray(optionsMap.getArray("countries")) : null; - Radar.geocode(address, layers, countries, new Radar.RadarGeocodeCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarAddress[] addresses) { - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (addresses != null) { - map.putArray("addresses", RNRadarUtils.arrayForJson(RadarAddress.toJson(addresses))); - } - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + Radar.geocode(address, layers, countries, (status, addresses) -> { + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (addresses != null) { + map.putArray("addresses", RNRadarUtils.arrayForJson(RadarAddress.toJson(addresses))); } - } else { - promise.reject(status.toString(), status.toString()); + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }); } @@ -1064,24 +985,21 @@ public void reverseGeocode(ReadableMap optionsMap, final Promise promise) { layers = optionsMap.hasKey("layers") ? RNRadarUtils.stringArrayForArray(optionsMap.getArray("layers")) : null; } - Radar.RadarGeocodeCallback callback = new Radar.RadarGeocodeCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarAddress[] addresses) { - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (addresses != null) { - map.putArray("addresses", RNRadarUtils.arrayForJson(RadarAddress.toJson(addresses))); - } - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + Radar.RadarGeocodeCallback callback = (status, addresses) -> { + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (addresses != null) { + map.putArray("addresses", RNRadarUtils.arrayForJson(RadarAddress.toJson(addresses))); } - } else { - promise.reject(status.toString(), status.toString()); + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }; @@ -1104,25 +1022,22 @@ public void ipGeocode(final Promise promise) { return; } - Radar.ipGeocode(new Radar.RadarIpGeocodeCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarAddress address, boolean proxy) { - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (address != null) { - map.putMap("address", RNRadarUtils.mapForJson(address.toJson())); - map.putBoolean("proxy", proxy); - } - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + Radar.ipGeocode((status, address, proxy) -> { + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (address != null) { + map.putMap("address", RNRadarUtils.mapForJson(address.toJson())); + map.putBoolean("proxy", proxy); } - } else { - promise.reject(status.toString(), status.toString()); + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }); } @@ -1133,11 +1048,6 @@ public void getDistance(ReadableMap optionsMap, final Promise promise) { return; } - if (!optionsMap.hasKey("destination")) { - promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); - - return; - } ReadableMap originMap = optionsMap.getMap("origin"); Location origin = null; @@ -1149,6 +1059,10 @@ public void getDistance(ReadableMap optionsMap, final Promise promise) { origin.setLongitude(longitude); } ReadableMap destinationMap = optionsMap.getMap("destination"); + if (destinationMap == null) { + promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); + return; + } double latitude = destinationMap.getDouble("latitude"); double longitude = destinationMap.getDouble("longitude"); Location destination = new Location("RNRadarModule"); @@ -1170,24 +1084,21 @@ public void getDistance(ReadableMap optionsMap, final Promise promise) { String unitsStr = optionsMap.hasKey("units") ? optionsMap.getString("units") : null; Radar.RadarRouteUnits units = unitsStr != null && (unitsStr.equals("METRIC") || unitsStr.equals("metric")) ? Radar.RadarRouteUnits.METRIC : Radar.RadarRouteUnits.IMPERIAL; - Radar.RadarRouteCallback callback = new Radar.RadarRouteCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarRoutes routes) { - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (routes != null) { - map.putMap("routes", RNRadarUtils.mapForJson(routes.toJson())); - } - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + Radar.RadarRouteCallback callback = (status, routes) -> { + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (routes != null) { + map.putMap("routes", RNRadarUtils.mapForJson(routes.toJson())); } - } else { - promise.reject(status.toString(), status.toString()); + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }; @@ -1227,41 +1138,31 @@ public void getMatrix(ReadableMap optionsMap, final Promise promise) { destinations[i] = destination; } String modeStr = optionsMap.getString("mode"); - Radar.RadarRouteMode mode = Radar.RadarRouteMode.CAR; - if (modeStr != null) { - if (modeStr.equals("FOOT") || modeStr.equals("foot")) { - mode = Radar.RadarRouteMode.FOOT; - } else if (modeStr.equals("BIKE") || modeStr.equals("bike")) { - mode = Radar.RadarRouteMode.BIKE; - } else if (modeStr.equals("CAR") || modeStr.equals("car")) { - mode = Radar.RadarRouteMode.CAR; - } else if (modeStr.equals("TRUCK") || modeStr.equals("truck")) { - mode = Radar.RadarRouteMode.TRUCK; - } else if (modeStr.equals("MOTORBIKE") || modeStr.equals("motorbike")) { - mode = Radar.RadarRouteMode.MOTORBIKE; - } + Radar.RadarRouteMode mode; + try { + mode = Radar.RadarRouteMode.valueOf(modeStr != null ? modeStr.toUpperCase() : "CAR"); + } catch (NullPointerException | IllegalArgumentException ignored) { + mode = Radar.RadarRouteMode.CAR; } + String unitsStr = optionsMap.hasKey("units") ? optionsMap.getString("units") : null; Radar.RadarRouteUnits units = unitsStr != null && (unitsStr.equals("METRIC") || unitsStr.equals("metric")) ? Radar.RadarRouteUnits.METRIC : Radar.RadarRouteUnits.IMPERIAL; - Radar.getMatrix(origins, destinations, mode, units, new Radar.RadarMatrixCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarRouteMatrix matrix) { - if (status == Radar.RadarStatus.SUCCESS) { - try { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (matrix != null) { - map.putArray("matrix", RNRadarUtils.arrayForJson(matrix.toJson())); - } - promise.resolve(map); - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + Radar.getMatrix(origins, destinations, mode, units, (status, matrix) -> { + if (status == Radar.RadarStatus.SUCCESS) { + try { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (matrix != null) { + map.putArray("matrix", RNRadarUtils.arrayForJson(matrix.toJson())); } - } else { - promise.reject(status.toString(), status.toString()); + promise.resolve(map); + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } + } else { + promise.reject(status.toString(), status.toString()); } }); } @@ -1271,36 +1172,30 @@ public void logConversion(ReadableMap optionsMap, final Promise promise) throws if (promise == null) { return; } - - if (!optionsMap.hasKey("name")) { + String name = optionsMap.getString("name"); + if (name == null) { promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString()); - return; } - - String name = optionsMap.getString("name"); Double revenue = optionsMap.hasKey("revenue") ? new Double(optionsMap.getDouble("revenue")) : null; ReadableMap metadata = optionsMap.hasKey("metadata") ? optionsMap.getMap("metadata") : null; JSONObject metadataObj = RNRadarUtils.jsonForMap(metadata); - Radar.RadarLogConversionCallback callback = new Radar.RadarLogConversionCallback() { - @Override - public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarEvent event) { - try { - if (status == Radar.RadarStatus.SUCCESS) { - WritableMap map = Arguments.createMap(); - map.putString("status", status.toString()); - if (event != null) { - map.putMap("event", RNRadarUtils.mapForJson(event.toJson())); - } - promise.resolve(map); - } else { - promise.reject(status.toString(), status.toString()); + Radar.RadarLogConversionCallback callback = (status, event) -> { + try { + if (status == Radar.RadarStatus.SUCCESS) { + WritableMap map = Arguments.createMap(); + map.putString("status", status.toString()); + if (event != null) { + map.putMap("event", RNRadarUtils.mapForJson(event.toJson())); } - } catch (JSONException e) { - Log.e(TAG, "JSONException", e); - promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); + promise.resolve(map); + } else { + promise.reject(status.toString(), status.toString()); } + } catch (JSONException e) { + Log.e(TAG, "JSONException", e); + promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString()); } }; diff --git a/example/App.tsx b/example/App.tsx index 891fc3cf..e8a0dbf4 100644 --- a/example/App.tsx +++ b/example/App.tsx @@ -262,6 +262,7 @@ export default function App() { radius: 1000, tags: ["venue"], limit: 10, + includeGeometry: false, }) .then((result) => { handlePopulateText("searchGeofences:" + stringify(result));