Commit 0e445d21 authored by Chris Scott's avatar Chris Scott

Merge pull request #66 from christocracy/geofencing

Implement removeGeofence to js API.  
parents ef95e372 2656a720
......@@ -40,7 +40,9 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
public static final String ACTION_GET_ODOMETER = "getOdometer";
public static final String ACTION_RESET_ODOMETER = "resetOdometer";
public static final String ACTION_ADD_GEOFENCE = "addGeofence";
public static final String ACTION_REMOVE_GEOFENCE = "removeGeofence";
public static final String ACTION_ON_GEOFENCE = "onGeofence";
public static final String ACTION_PLAY_SOUND = "playSound";
private Boolean isEnabled = false;
private Boolean stopOnTerminate = false;
......@@ -162,6 +164,21 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
event.putDouble("longitude", config.getDouble("longitude"));
event.putString("identifier", config.getString("identifier"));
EventBus.getDefault().post(event);
callbackContext.success();
} catch (JSONException e) {
Log.w(TAG, e);
callbackContext.error("Failed to add geofence");
result = false;
}
} else if (ACTION_REMOVE_GEOFENCE.equalsIgnoreCase(action)) {
result = true;
try {
Bundle event = new Bundle();
event.putString("name", action);
event.putBoolean("request", true);
event.putString("identifier", data.getString(0));
EventBus.getDefault().post(event);
callbackContext.success();
} catch (JSONException e) {
......@@ -172,6 +189,14 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
} else if (ACTION_ON_GEOFENCE.equalsIgnoreCase(action)) {
result = true;
geofenceCallbacks.add(callbackContext);
} else if (ACTION_PLAY_SOUND.equalsIgnoreCase(action)) {
result = true;
Bundle event = new Bundle();
event.putString("name", action);
event.putBoolean("request", true);
event.putInt("soundId", data.getInt(0));
EventBus.getDefault().post(event);
callbackContext.success();
}
return result;
}
......
......@@ -10,6 +10,7 @@
@interface CDVBackgroundGeolocation : CDVPlugin
@property (nonatomic, strong) NSString* syncCallbackId;
@property (nonatomic, strong) NSString* locationCallbackId;
@property (nonatomic, strong) NSMutableArray* geofenceListeners;
@property (nonatomic, strong) NSMutableArray* stationaryRegionListeners;
......@@ -26,6 +27,7 @@
- (void) getOdometer:(CDVInvokedUrlCommand *)command;
- (void) resetOdometer:(CDVInvokedUrlCommand *)command;
- (void) addGeofence:(CDVInvokedUrlCommand *)command;
- (void) removeGeofence:(CDVInvokedUrlCommand *)command;
- (void) onGeofence:(CDVInvokedUrlCommand *)command;
- (void) playSound:(CDVInvokedUrlCommand *)command;
@end
......
......@@ -10,7 +10,7 @@
NSDictionary *config;
}
@synthesize syncCallbackId, geofenceListeners, stationaryRegionListeners;
@synthesize syncCallbackId, locationCallbackId, geofenceListeners, stationaryRegionListeners;
- (void)pluginInitialize
{
......@@ -19,6 +19,7 @@
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLocationChanged:) name:@"TSLocationManager.location" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onStationaryLocation:) name:@"TSLocationManager.stationary" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEnterGeofence:) name:@"TSLocationManager.geofence" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSyncComplete:) name:@"TSLocationManager.sync" object:nil];
}
/**
......@@ -30,7 +31,7 @@
*/
- (void) configure:(CDVInvokedUrlCommand*)command
{
self.syncCallbackId = command.callbackId;
self.locationCallbackId = command.callbackId;
config = [command.arguments objectAtIndex:0];
......@@ -97,7 +98,7 @@
[result setKeepCallbackAsBool:YES];
[self.commandDelegate runInBackground:^{
[self.commandDelegate sendPluginResult:result callbackId:self.syncCallbackId];
[self.commandDelegate sendPluginResult:result callbackId:self.locationCallbackId];
}];
}
......@@ -143,6 +144,15 @@
}
}
- (void) onSyncComplete:(NSNotification*)notification
{
NSArray *locations = [notification.userInfo objectForKey:@"locations"];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:locations];
[self.commandDelegate sendPluginResult:result callbackId:syncCallbackId];
syncCallbackId = nil;
}
/**
* Fetches current stationaryLocation
*/
......@@ -170,13 +180,16 @@
*/
- (void) sync:(CDVInvokedUrlCommand *)command
{
NSArray* locations = [bgGeo sync];
if (locations != nil) {
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:locations];
if (syncCallbackId != nil) {
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"A sync action is already in progress."];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} else {
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Failed to sync to server. Is there a network connection?"];
return;
}
syncCallbackId = command.callbackId;
NSArray* locations = [bgGeo sync];
if (locations == nil) {
syncCallbackId = nil;
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Sync failed. Is there a network connection?"];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
}
......@@ -192,16 +205,32 @@
- (void) addGeofence:(CDVInvokedUrlCommand*)command
{
NSDictionary *cfg = [command.arguments objectAtIndex:0];
NSString *notifyOnExit = [cfg objectForKey:@"notifyOnExit"];
NSString *notifyOnEntry = [cfg objectForKey:@"notifyOnEntry"];
[bgGeo addGeofence:[cfg objectForKey:@"identifier"]
radius:[[cfg objectForKey:@"radius"] doubleValue]
latitude:[[cfg objectForKey:@"latitude"] doubleValue]
longitude:[[cfg objectForKey:@"longitude"] doubleValue]
notifyOnEntry: (notifyOnEntry) ? [notifyOnEntry boolValue] : NO
notifyOnExit: (notifyOnExit) ? [notifyOnExit boolValue] : NO
];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (void) removeGeofence:(CDVInvokedUrlCommand*)command
{
NSString *identifier = [command.arguments objectAtIndex:0];
CDVPluginResult *result;
if ([bgGeo removeGeofence:identifier]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Failed to locate geofence"];
}
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (void) onGeofence:(CDVInvokedUrlCommand*)command
{
if (self.geofenceListeners == nil) {
......
......@@ -21,7 +21,8 @@
- (void) onAppTerminate;
- (BOOL) isEnabled;
- (NSDictionary*) locationToDictionary:(CLLocation*)location;
- (void) addGeofence:(NSString*)identifier radius:(CLLocationDistance)radius latitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude;
- (void) addGeofence:(NSString*)identifier radius:(CLLocationDistance)radius latitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude notifyOnEntry:(BOOL)notifyOnEntry notifyOnExit:(BOOL)notifyOnExit;
- (BOOL) removeGeofence:(NSString*)identifier;
- (void) playSound:(SystemSoundID)soundId;
@end
......@@ -6,7 +6,7 @@
<dict>
<key>Headers/TSLocationManager.h</key>
<data>
1VuoMC44vEUkt1CU0bbgpBs3zGY=
0rtf3tEfqeHo6WQSpvbXktE2waw=
</data>
<key>Info.plist</key>
<data>
......@@ -21,7 +21,7 @@
<dict>
<key>Headers/TSLocationManager.h</key>
<data>
1VuoMC44vEUkt1CU0bbgpBs3zGY=
0rtf3tEfqeHo6WQSpvbXktE2waw=
</data>
<key>Modules/module.modulemap</key>
<data>
......
......@@ -190,6 +190,20 @@ module.exports = {
'addGeofence',
[config]);
},
/**
* remove a geofence
* @param {String} identifier
*/
removeGeofence: function(identifier, success, failure) {
if (!identifier) {
throw "#removeGeofence requires an 'identifier'";
}
exec(success || function() {},
failure || function() {},
'BackgroundGeoLocation',
'removeGeofence',
[identifier]);
},
onGeofence: function(success, failure) {
if (!typeof(success) === 'function') {
throw "#onGeofence requires a success callback";
......@@ -206,6 +220,11 @@ module.exports = {
'onGeofence',
[]);
},
/**
* Play a system sound. This is totally experimental.
* iOS http://iphonedevwiki.net/index.php/AudioServices
* Android:
*/
playSound: function(soundId) {
var success = function() {};
var failure = function() {};
......
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