Commit 3d031b3b authored by Chris Scott's avatar Chris Scott

Remove Cordova references from BackgroundGeolocation lib. All cordova...

Remove Cordova references from BackgroundGeolocation lib.  All cordova references and callbacks will be handled in CDVBackgroundGeolocation
parent 6dc678e5
...@@ -4,19 +4,16 @@ ...@@ -4,19 +4,16 @@
@interface BackgroundGeolocation : NSObject <CLLocationManagerDelegate> @interface BackgroundGeolocation : NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) NSString* syncCallbackId;
@property (nonatomic, strong) NSMutableArray* stationaryRegionListeners;
@property (nonatomic, weak) id <CDVCommandDelegate> commandDelegate; @property (nonatomic, weak) id <CDVCommandDelegate> commandDelegate;
- (void) configure:(CDVInvokedUrlCommand*)config; - (void) configure:(NSDictionary*)config;
- (void) start:(CDVInvokedUrlCommand*)command; - (void) start;
- (void) stop:(CDVInvokedUrlCommand*)command; - (void) stop;
- (void) finish:(CDVInvokedUrlCommand*)command; - (void) finish;
- (void) onPaceChange:(CDVInvokedUrlCommand*)command; - (void) stopBackgroundTask;
- (void) setConfig:(CDVInvokedUrlCommand*)command; - (void) onPaceChange:(BOOL)value;
- (void) addStationaryRegionListener:(CDVInvokedUrlCommand*)command; - (void) setConfig:(NSDictionary*)command;
- (void) getStationaryLocation:(CDVInvokedUrlCommand *)command; - (NSDictionary*) getStationaryLocation;
- (void) onSuspend:(NSNotification *)notification; - (void) onSuspend:(NSNotification *)notification;
- (void) onResume:(NSNotification *)notification; - (void) onResume:(NSNotification *)notification;
- (void) onAppTerminate; - (void) onAppTerminate;
......
...@@ -65,7 +65,7 @@ ...@@ -65,7 +65,7 @@
} }
@synthesize syncCallbackId, stationaryRegionListeners, commandDelegate; //@synthesize syncCallbackId, stationaryRegionListeners, commandDelegate;
- (id) init - (id) init
{ {
...@@ -102,13 +102,9 @@ ...@@ -102,13 +102,9 @@
return [super init]; return [super init];
} }
- (void) configure:(CDVInvokedUrlCommand*)command - (void) configure:(NSDictionary*)config
{ {
NSDictionary *config = [command.arguments objectAtIndex:0]; [self setConfig:config];
[self applyConfig:config];
self.syncCallbackId = command.callbackId;
locationManager.activityType = activityType; locationManager.activityType = activityType;
locationManager.pausesLocationUpdatesAutomatically = YES; locationManager.pausesLocationUpdatesAutomatically = YES;
...@@ -126,7 +122,7 @@ ...@@ -126,7 +122,7 @@
} }
} }
- (void) applyConfig:(NSDictionary*)config - (void) setConfig:(NSDictionary*)config
{ {
if (config[@"debug"]) { if (config[@"debug"]) {
isDebugging = [[config objectForKey:@"debug"] boolValue]; isDebugging = [[config objectForKey:@"debug"] boolValue];
...@@ -192,7 +188,7 @@ ...@@ -192,7 +188,7 @@
/** /**
* Turn on background geolocation * Turn on background geolocation
*/ */
- (void) start:(CDVInvokedUrlCommand*)command - (void) start
{ {
enabled = YES; enabled = YES;
UIApplicationState state = [[UIApplication sharedApplication] applicationState]; UIApplicationState state = [[UIApplication sharedApplication] applicationState];
...@@ -207,7 +203,7 @@ ...@@ -207,7 +203,7 @@
/** /**
* Turn it off * Turn it off
*/ */
- (void) stop:(CDVInvokedUrlCommand*)command - (void) stop
{ {
[self log: @"- CDVBackgroundGeoLocation stop"]; [self log: @"- CDVBackgroundGeoLocation stop"];
enabled = NO; enabled = NO;
...@@ -225,9 +221,9 @@ ...@@ -225,9 +221,9 @@
* Change pace to moving/stopped * Change pace to moving/stopped
* @param {Boolean} isMoving * @param {Boolean} isMoving
*/ */
- (void) onPaceChange:(CDVInvokedUrlCommand *)command - (void) onPaceChange:(BOOL)value
{ {
isMoving = [[command.arguments objectAtIndex: 0] boolValue]; isMoving = value;
[self log: @"- CDVBackgroundGeoLocation onPaceChange %d", isMoving]; [self log: @"- CDVBackgroundGeoLocation onPaceChange %d", isMoving];
UIApplicationState state = [[UIApplication sharedApplication] applicationState]; UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground) { if (state == UIApplicationStateBackground) {
...@@ -235,18 +231,9 @@ ...@@ -235,18 +231,9 @@
} }
} }
- (void) setConfig:(CDVInvokedUrlCommand*)command
{
NSDictionary *config = [command.arguments objectAtIndex:0];
[self applyConfig:config];
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (void) addStationaryRegionListener:(CDVInvokedUrlCommand*)command - (void) addStationaryRegionListener:(CDVInvokedUrlCommand*)command
{ {
/*
if (self.stationaryRegionListeners == nil) { if (self.stationaryRegionListeners == nil) {
self.stationaryRegionListeners = [[NSMutableArray alloc] init]; self.stationaryRegionListeners = [[NSMutableArray alloc] init];
} }
...@@ -254,6 +241,7 @@ ...@@ -254,6 +241,7 @@
if (stationaryRegion) { if (stationaryRegion) {
[self queue:stationaryLocation type:@"stationary"]; [self queue:stationaryLocation type:@"stationary"];
} }
*/
} }
/** /**
...@@ -290,7 +278,6 @@ ...@@ -290,7 +278,6 @@
} }
} }
/** /**
* toggle passive or aggressive location services * toggle passive or aggressive location services
*/ */
...@@ -514,12 +501,12 @@ ...@@ -514,12 +501,12 @@
// Build a resultset for javascript callback. // Build a resultset for javascript callback.
NSString *locationType = [data objectForKey:@"location_type"]; NSString *locationType = [data objectForKey:@"location_type"];
if ([locationType isEqualToString:@"stationary"]) { if ([locationType isEqualToString:@"stationary"]) {
[self fireStationaryRegionListeners:data]; // Any javascript stationaryRegion event-listeners?
[data setObject:[NSNumber numberWithDouble:stationaryRadius] forKey:@"radius"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"BackgroundGeolocation.stationarylocation" object:data];
//[self fireStationaryRegionListeners:data];
} else if ([locationType isEqualToString:@"current"]) { } else if ([locationType isEqualToString:@"current"]) {
CDVPluginResult* result = nil; [[NSNotificationCenter defaultCenter] postNotificationName:@"BackgroundGeolocation.location" object:data];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];
[result setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:result callbackId:self.syncCallbackId];
} else { } else {
[self log: @"- CDVBackgroundGeoLocation#sync could not determine location_type."]; [self log: @"- CDVBackgroundGeoLocation#sync could not determine location_type."];
[self stopBackgroundTask]; [self stopBackgroundTask];
...@@ -529,48 +516,20 @@ ...@@ -529,48 +516,20 @@
/** /**
* Called by js to signify the end of a background-geolocation event * Called by js to signify the end of a background-geolocation event
*/ */
-(void) finish:(CDVInvokedUrlCommand*)command -(void) finish
{ {
[self log: @"- CDVBackgroundGeoLocation finish"]; [self log: @"- CDVBackgroundGeoLocation finish"];
[self stopBackgroundTask]; [self stopBackgroundTask];
} }
- (void) fireStationaryRegionListeners:(NSMutableDictionary*)data
{
[self log: @"- CDVBackgroundGeoLocation#fireStationaryRegionListener"];
if (![self.stationaryRegionListeners count]) {
[self stopBackgroundTask];
return;
}
// Any javascript stationaryRegion event-listeners?
[data setObject:[NSNumber numberWithDouble:stationaryRadius] forKey:@"radius"];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];
[result setKeepCallbackAsBool:YES];
for (NSString *callbackId in self.stationaryRegionListeners)
{
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}
}
/** /**
* Fetches current stationaryLocation * Fetches current stationaryLocation
*/ */
- (void) getStationaryLocation:(CDVInvokedUrlCommand *)command - (NSDictionary*) getStationaryLocation
{ {
[self log: @"- CDVBackgroundGeoLocation getStationaryLocation"]; NSDictionary *data = [self locationToHash:stationaryLocation];
return data;
// Build a resultset for javascript callback.
CDVPluginResult* result = nil;
if (stationaryLocation) {
NSMutableDictionary *returnInfo = [self locationToHash:stationaryLocation];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:returnInfo];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:NO];
}
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} }
- (bool) stationaryRegionContainsLocation:(CLLocation*)location { - (bool) stationaryRegionContainsLocation:(CLLocation*)location {
......
...@@ -8,6 +8,9 @@ ...@@ -8,6 +8,9 @@
@interface CDVBackgroundGeoLocation : CDVPlugin @interface CDVBackgroundGeoLocation : CDVPlugin
@property (nonatomic, strong) NSString* syncCallbackId;
@property (nonatomic, strong) NSMutableArray* stationaryRegionListeners;
- (void) configure:(CDVInvokedUrlCommand*)command; - (void) configure:(CDVInvokedUrlCommand*)command;
- (void) start:(CDVInvokedUrlCommand*)command; - (void) start:(CDVInvokedUrlCommand*)command;
- (void) stop:(CDVInvokedUrlCommand*)command; - (void) stop:(CDVInvokedUrlCommand*)command;
......
...@@ -8,13 +8,20 @@ ...@@ -8,13 +8,20 @@
@implementation CDVBackgroundGeoLocation { @implementation CDVBackgroundGeoLocation {
BackgroundGeolocation *bgGeo; BackgroundGeolocation *bgGeo;
NSDictionary *config;
} }
@synthesize syncCallbackId, stationaryRegionListeners;
- (void)pluginInitialize - (void)pluginInitialize
{ {
bgGeo = [[BackgroundGeolocation alloc] init]; bgGeo = [[BackgroundGeolocation alloc] init];
bgGeo.commandDelegate = self.commandDelegate; bgGeo.commandDelegate = self.commandDelegate;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLocationChanged:) name:@"BackgroundGeolocation.location" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onStationaryLocation:) name:@"BackgroundGeolocation.stationarylocation" object:nil];
} }
/** /**
* configure plugin * configure plugin
* @param {String} token * @param {String} token
...@@ -24,12 +31,21 @@ ...@@ -24,12 +31,21 @@
*/ */
- (void) configure:(CDVInvokedUrlCommand*)command - (void) configure:(CDVInvokedUrlCommand*)command
{ {
[bgGeo configure:command]; self.syncCallbackId = command.callbackId;
config = [command.arguments objectAtIndex:0];
[bgGeo configure:config];
} }
- (void) setConfig:(CDVInvokedUrlCommand*)command - (void) setConfig:(CDVInvokedUrlCommand*)command
{ {
[bgGeo setConfig:command]; NSDictionary *cfg = [command.arguments objectAtIndex:0];
[bgGeo setConfig:cfg];
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} }
/** /**
...@@ -37,14 +53,23 @@ ...@@ -37,14 +53,23 @@
*/ */
- (void) start:(CDVInvokedUrlCommand*)command - (void) start:(CDVInvokedUrlCommand*)command
{ {
[bgGeo start:command]; [bgGeo start];
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} }
/** /**
* Turn it off * Turn it off
*/ */
- (void) stop:(CDVInvokedUrlCommand*)command - (void) stop:(CDVInvokedUrlCommand*)command
{ {
[bgGeo stop:command]; [bgGeo stop];
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} }
/** /**
...@@ -53,7 +78,45 @@ ...@@ -53,7 +78,45 @@
*/ */
- (void) onPaceChange:(CDVInvokedUrlCommand *)command - (void) onPaceChange:(CDVInvokedUrlCommand *)command
{ {
[bgGeo onPaceChange:command]; BOOL moving = [[command.arguments objectAtIndex: 0] boolValue];
[bgGeo onPaceChange:moving];
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
/**
* location handler from BackgroundGeolocation
*/
- (void)onLocationChanged:(NSNotification*)notification {
NSDictionary *locationData = notification.object;
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:locationData];
[result setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:result callbackId:self.syncCallbackId];
NSLog(@"-------------------------------onLocation %@", locationData);
}
- (void) onStationaryLocation:(NSNotification*)notification
{
NSMutableDictionary *data = notification.object;
NSLog(@"-------------------------------onStationaryLocation %@", data);
if (![self.stationaryRegionListeners count]) {
[bgGeo stopBackgroundTask];
return;
}
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:data];
[result setKeepCallbackAsBool:YES];
for (NSString *callbackId in self.stationaryRegionListeners)
{
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}
[bgGeo stopBackgroundTask];
} }
/** /**
...@@ -61,12 +124,19 @@ ...@@ -61,12 +124,19 @@
*/ */
- (void) getStationaryLocation:(CDVInvokedUrlCommand *)command - (void) getStationaryLocation:(CDVInvokedUrlCommand *)command
{ {
[bgGeo getStationaryLocation:command]; NSDictionary* location = [bgGeo getStationaryLocation];
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:location];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} }
- (void) addStationaryRegionListener:(CDVInvokedUrlCommand*)command - (void) addStationaryRegionListener:(CDVInvokedUrlCommand*)command
{ {
[bgGeo addStationaryRegionListener:command]; if (self.stationaryRegionListeners == nil) {
self.stationaryRegionListeners = [[NSMutableArray alloc] init];
}
[self.stationaryRegionListeners addObject:command.callbackId];
} }
/** /**
...@@ -74,7 +144,7 @@ ...@@ -74,7 +144,7 @@
*/ */
-(void) finish:(CDVInvokedUrlCommand*)command -(void) finish:(CDVInvokedUrlCommand*)command
{ {
[bgGeo finish:command]; [bgGeo finish];
} }
/** /**
* If you don't stopMonitoring when application terminates, the app will be awoken still when a * If you don't stopMonitoring when application terminates, the app will be awoken still when a
......
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