Commit c5e439c0 authored by Chris Scott's avatar Chris Scott

IMplement notifyOnEntry, notifyOnExit for Android

parent f5de6d22
...@@ -41,6 +41,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -41,6 +41,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
public static final String ACTION_RESET_ODOMETER = "resetOdometer"; public static final String ACTION_RESET_ODOMETER = "resetOdometer";
public static final String ACTION_ADD_GEOFENCE = "addGeofence"; public static final String ACTION_ADD_GEOFENCE = "addGeofence";
public static final String ACTION_REMOVE_GEOFENCE = "removeGeofence"; public static final String ACTION_REMOVE_GEOFENCE = "removeGeofence";
public static final String ACTION_GET_GEOFENCES = "getGeofences";
public static final String ACTION_ON_GEOFENCE = "onGeofence"; public static final String ACTION_ON_GEOFENCE = "onGeofence";
public static final String ACTION_PLAY_SOUND = "playSound"; public static final String ACTION_PLAY_SOUND = "playSound";
...@@ -60,6 +61,8 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -60,6 +61,8 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
private CallbackContext getOdometerCallback; private CallbackContext getOdometerCallback;
private CallbackContext resetOdometerCallback; private CallbackContext resetOdometerCallback;
private CallbackContext paceChangeCallback; private CallbackContext paceChangeCallback;
private CallbackContext getGeofencesCallback;
private List<CallbackContext> geofenceCallbacks = new ArrayList<CallbackContext>(); private List<CallbackContext> geofenceCallbacks = new ArrayList<CallbackContext>();
public static boolean isActive() { public static boolean isActive() {
...@@ -163,7 +166,13 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -163,7 +166,13 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
event.putDouble("latitude", config.getDouble("latitude")); event.putDouble("latitude", config.getDouble("latitude"));
event.putDouble("longitude", config.getDouble("longitude")); event.putDouble("longitude", config.getDouble("longitude"));
event.putString("identifier", config.getString("identifier")); event.putString("identifier", config.getString("identifier"));
if (config.has("notifyOnEnter")) {
event.putBoolean("notifyOnEnter", config.getBoolean("notifyOnEnter"));
}
if (config.has("notifyOnExit")) {
event.putBoolean("notifyOnExit", config.getBoolean("notifyOnExit"));
}
EventBus.getDefault().post(event); EventBus.getDefault().post(event);
callbackContext.success(); callbackContext.success();
} catch (JSONException e) { } catch (JSONException e) {
...@@ -189,6 +198,14 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -189,6 +198,14 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
} else if (ACTION_ON_GEOFENCE.equalsIgnoreCase(action)) { } else if (ACTION_ON_GEOFENCE.equalsIgnoreCase(action)) {
result = true; result = true;
geofenceCallbacks.add(callbackContext); geofenceCallbacks.add(callbackContext);
} else if (ACTION_GET_GEOFENCES.equalsIgnoreCase(action)) {
result = true;
getGeofencesCallback = callbackContext;
Bundle event = new Bundle();
event.putString("name", action);
event.putBoolean("request", true);
EventBus.getDefault().post(event);
} else if (ACTION_PLAY_SOUND.equalsIgnoreCase(action)) { } else if (ACTION_PLAY_SOUND.equalsIgnoreCase(action)) {
result = true; result = true;
Bundle event = new Bundle(); Bundle event = new Bundle();
...@@ -334,6 +351,8 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -334,6 +351,8 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
} catch (JSONException e) { } catch (JSONException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
PluginResult result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
runInBackground(getLocationsCallback, result);
} }
} else if (ACTION_SYNC.equalsIgnoreCase(name)) { } else if (ACTION_SYNC.equalsIgnoreCase(name)) {
Boolean success = event.getBoolean("success"); Boolean success = event.getBoolean("success");
...@@ -359,6 +378,17 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -359,6 +378,17 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
} else if (ACTION_ON_PACE_CHANGE.equalsIgnoreCase(name)) { } else if (ACTION_ON_PACE_CHANGE.equalsIgnoreCase(name)) {
PluginResult result = new PluginResult(PluginResult.Status.OK); PluginResult result = new PluginResult(PluginResult.Status.OK);
paceChangeCallback.sendPluginResult(result); paceChangeCallback.sendPluginResult(result);
} else if (ACTION_GET_GEOFENCES.equalsIgnoreCase(name)) {
try {
JSONArray json = new JSONArray(event.getString("data"));
PluginResult result = new PluginResult(PluginResult.Status.OK, json);
runInBackground(getGeofencesCallback, result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
PluginResult result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
runInBackground(getGeofencesCallback, result);
}
} }
} }
...@@ -400,9 +430,18 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -400,9 +430,18 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
if (!geofenceCallbacks.isEmpty()) { if (!geofenceCallbacks.isEmpty()) {
for (Geofence geofence : geofenceEvent.getTriggeringGeofences()) { for (Geofence geofence : geofenceEvent.getTriggeringGeofences()) {
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
String action = "";
int transitionType = geofenceEvent.getGeofenceTransition();
if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) {
action = "ENTER";
} else if (transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
action = "EXIT";
} else {
action = "DWELL";
}
try { try {
params.put("identifier", geofence.getRequestId()); params.put("identifier", geofence.getRequestId());
params.put("action", "TODO"); params.put("action", action);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment