Commit 2656a720 authored by Chris Scott's avatar Chris Scott

Implement removeGeofence to js API. Improve manual sync functionality,...

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