Skip to content

Commit

Permalink
Merge pull request #21 from radarlabs/update-trip-matrix
Browse files Browse the repository at this point in the history
Bump versions, expose updateTrip() and getMatrix() (WIP)
  • Loading branch information
nickpatrick authored Jun 17, 2021
2 parents d71b7a3 + 9471e39 commit 3749e77
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Cordova plugin for Radar, the leading geofencing and location tracking platform",
"homepage": "https://www.radar.io",
"license": "Apache-2.0",
"version": "3.1.0",
"version": "3.1.1",
"main": "www/Radar.js",
"devDependencies": {
"babel-eslint": "^7.1.1",
Expand Down
6 changes: 3 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
id="cordova-plugin-radar"
version="1.0.0">
version="3.1.1">
<name>Radar</name>
<js-module src="www/Radar.js" name="Radar">
<clobbers target="cordova.plugins.radar"/>
Expand Down Expand Up @@ -41,7 +41,7 @@
<source-file src="src/android/src/main/java/io/radar/cordova/RadarCordovaPlugin.java" target-dir="src/android/src/main/java/io/radar/cordova"/>
<source-file src="src/android/src/main/java/io/radar/cordova/RadarForegroundService.java" target-dir="src/android/src/main/java/io/radar/cordova"/>

<framework src="io.radar:sdk:3.1.3"/>
<framework src="io.radar:sdk:3.1.8"/>

</platform>
<platform name="ios">
Expand Down Expand Up @@ -69,6 +69,6 @@

<header-file src="src/ios/CDVRadar.h"/>
<source-file src="src/ios/CDVRadar.m"/>
<framework src="RadarSDK" type="podspec" spec="3.1.2"/>
<framework src="RadarSDK" type="podspec" spec="3.1.8"/>
</platform>
</plugin>
177 changes: 169 additions & 8 deletions src/android/src/main/java/io/radar/cordova/RadarCordovaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
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 android.app.Activity;
Expand Down Expand Up @@ -85,8 +87,12 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
offClientLocation(args, callbackContext);
} else if (action.equals("offError")) {
offError(args, callbackContext);
} else if (action.equals("getTripOptions")) {
getTripOptions(args, callbackContext);
} else if (action.equals("startTrip")) {
startTrip(args, callbackContext);
} else if (action.equals("updateTrip")) {
updateTrip(args, callbackContext);
} else if (action.equals("completeTrip")) {
completeTrip(args, callbackContext);
} else if (action.equals("cancelTrip")) {
Expand All @@ -107,6 +113,8 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
ipGeocode(args, callbackContext);
} else if (action.equals("getDistance")) {
getDistance(args, callbackContext);
} else if (action.equals("getMatrix")) {
getMatrix(args, callbackContext);
} else if (action.equals("startForegroundService")) {
startForegroundService(args, callbackContext);
} else if (action.equals("stopForegroundService")) {
Expand Down Expand Up @@ -457,25 +465,112 @@ public void offError(final JSONArray args, final CallbackContext callbackContext
RadarCordovaPlugin.errorCallbackContext = null;
}

public void getTripOptions(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
RadarTripOptions options = Radar.getTripOptions();
JSONObject optionsObj = null;
if (options != null) {
optionsObj = options.toJson();
}

PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, optionsObj);
callbackContext.sendPluginResult(pluginResult);
}

public void startTrip(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
final JSONObject optionsObj = args.getJSONObject(0);

RadarTripOptions options = RadarTripOptions.fromJson(optionsObj);
Radar.startTrip(options);
Radar.startTrip(options, new Radar.RadarTripCallback() {
@Override
public void onComplete(Radar.RadarStatus status) {
try {
JSONObject obj = new JSONObject();
obj.put("status", status.toString());

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, obj));
} catch (JSONException e) {
Log.e("RadarCordovaPlugin", "JSONException", e);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
}
});

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
}

public void completeTrip(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
Radar.completeTrip();
public void updateTrip(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
final JSONObject optionsObj = args.getJSONObject(0);

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
JSONObject tripOptionsObj = optionsObj.getJSONObject("options");

RadarTripOptions options = RadarTripOptions.fromJson(tripOptionsObj);
RadarTrip.RadarTripStatus status = RadarTrip.RadarTripStatus.UNKNOWN;

if (optionsObj.has("status")) {
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;
}
}
}

Radar.updateTrip(options, status, new Radar.RadarTripCallback() {
@Override
public void onComplete(Radar.RadarStatus status) {
try {
JSONObject obj = new JSONObject();
obj.put("status", status.toString());

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, obj));
} catch (JSONException e) {
Log.e("RadarCordovaPlugin", "JSONException", e);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
}
});
}

public void cancelTrip(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
Radar.cancelTrip();
public void completeTrip(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
Radar.completeTrip(new Radar.RadarTripCallback() {
@Override
public void onComplete(Radar.RadarStatus status) {
try {
JSONObject obj = new JSONObject();
obj.put("status", status.toString());

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, obj));
} catch (JSONException e) {
Log.e("RadarCordovaPlugin", "JSONException", e);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
}
});
}

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
public void cancelTrip(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
Radar.cancelTrip(new Radar.RadarTripCallback() {
@Override
public void onComplete(Radar.RadarStatus status) {
try {
JSONObject obj = new JSONObject();
obj.put("status", status.toString());

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, obj));
} catch (JSONException e) {
Log.e("RadarCordovaPlugin", "JSONException", e);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
}
});
}

public void getContext(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
Expand Down Expand Up @@ -763,6 +858,12 @@ public void onComplete(Radar.RadarStatus status, RadarRoutes routes) {
if (modesList.contains("CAR") || modesList.contains("car")) {
modes.add(Radar.RadarRouteMode.CAR);
}
if (modesList.contains("TRUCK") || modesList.contains("truck")) {
modes.add(Radar.RadarRouteMode.TRUCK);
}
if (modesList.contains("MOTORBIKE") || modesList.contains("motorbike")) {
modes.add(Radar.RadarRouteMode.MOTORBIKE);
}
String unitsStr = optionsObj.getString("units");
Radar.RadarRouteUnits units = unitsStr.equals("METRIC") || unitsStr.equals("metric") ? Radar.RadarRouteUnits.METRIC : Radar.RadarRouteUnits.IMPERIAL;

Expand All @@ -773,6 +874,66 @@ public void onComplete(Radar.RadarStatus status, RadarRoutes routes) {
}
}

public void getMatrix(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
final JSONObject optionsObj = args.getJSONObject(0);

JSONArray originsArr = optionsObj.getJSONArray("origins");
Location[] origins = new Location[originsArr.length()];
for (int i = 0; i < originsArr.length(); i++) {
JSONObject originObj = originsArr.getJSONObject(i);
double originLatitude = originObj.getDouble("latitude");
double originLongitude = originObj.getDouble("longitude");
Location origin = new Location("RadarCordovaPlugin");
origin.setLatitude(originLatitude);
origin.setLongitude(originLongitude);
origins[i] = origin;
}
JSONArray destinationsArr = optionsObj.getJSONArray("destinations");
Location[] destinations = new Location[destinationsArr.length()];
for (int i = 0; i < destinationsArr.length(); i++) {
JSONObject destinationObj = destinationsArr.getJSONObject(i);
double destinationLatitude = destinationObj.getDouble("latitude");
double destinationLongitude = destinationObj.getDouble("longitude");
Location destination = new Location("RadarCordovaPlugin");
destination.setLatitude(destinationLatitude);
destination.setLongitude(destinationLongitude);
destinations[i] = destination;
}
Radar.RadarRouteMode mode = Radar.RadarRouteMode.CAR;
String modeStr = optionsObj.getString("mode");
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;
}
String unitsStr = optionsObj.getString("units");
Radar.RadarRouteUnits units = 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(Radar.RadarStatus status, RadarRouteMatrix matrix) {
try {
JSONObject obj = new JSONObject();
obj.put("status", status.toString());
if (matrix != null) {
obj.put("matrix", matrix.toJson());
}

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, obj));
} catch (JSONException e) {
Log.e("RadarCordovaPlugin", "JSONException", e);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
}
});
}

public void startForegroundService(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (Build.VERSION.SDK_INT >= 26) {
Activity activity = cordova.getActivity();
Expand All @@ -798,4 +959,4 @@ public void stopForegroundService(final JSONArray args, final CallbackContext ca
}
}

}
}
Loading

0 comments on commit 3749e77

Please sign in to comment.