Commit 5b494337 authored by Chris Scott's avatar Chris Scott

Merge pull request #70 from christocracy/geofencing

Geofencing
parents f5de6d22 3a09facb
...@@ -31,6 +31,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -31,6 +31,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
public static final String ACTION_START = "start"; public static final String ACTION_START = "start";
public static final String ACTION_STOP = "stop"; public static final String ACTION_STOP = "stop";
public static final String ACTION_FINISH = "finish";
public static final String ACTION_ON_PACE_CHANGE = "onPaceChange"; public static final String ACTION_ON_PACE_CHANGE = "onPaceChange";
public static final String ACTION_CONFIGURE = "configure"; public static final String ACTION_CONFIGURE = "configure";
public static final String ACTION_SET_CONFIG = "setConfig"; public static final String ACTION_SET_CONFIG = "setConfig";
...@@ -41,6 +42,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -41,6 +42,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 +62,8 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -60,6 +62,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() {
...@@ -85,9 +89,13 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -85,9 +89,13 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
this.setEnabled(true); this.setEnabled(true);
callbackContext.success(); callbackContext.success();
} else if (ACTION_STOP.equalsIgnoreCase(action)) { } else if (ACTION_STOP.equalsIgnoreCase(action)) {
// No implementation to stop background-tasks with Android. Just say "success"
result = true; result = true;
this.setEnabled(false); this.setEnabled(false);
callbackContext.success(); callbackContext.success();
} else if (ACTION_FINISH.equalsIgnoreCase(action)) {
result = true;
callbackContext.success();
} else if (ACTION_CONFIGURE.equalsIgnoreCase(action)) { } else if (ACTION_CONFIGURE.equalsIgnoreCase(action)) {
result = applyConfig(data); result = applyConfig(data);
if (result) { if (result) {
...@@ -163,7 +171,13 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -163,7 +171,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("notifyOnEntry")) {
event.putBoolean("notifyOnEntry", config.getBoolean("notifyOnEntry"));
}
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 +203,14 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -189,6 +203,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 +356,8 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -334,6 +356,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 +383,17 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -359,6 +383,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 +435,18 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -400,9 +435,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();
} }
......
...@@ -184,6 +184,9 @@ module.exports = { ...@@ -184,6 +184,9 @@ module.exports = {
if (!config.radius) { if (!config.radius) {
throw "#addGeofence requires a #radius"; throw "#addGeofence requires a #radius";
} }
if ( (typeof(config.notifyOnEnter) === 'undefined') && (typeof(config.notifyOnExit) === 'undefined') ) {
throw "#addGeofence requires at least notifyOnEnter {Boolean} and/or #notifyOnExit {Boolean}";
}
exec(success || function() {}, exec(success || function() {},
failure || function() {}, failure || function() {},
'BackgroundGeoLocation', 'BackgroundGeoLocation',
......
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