Commit 95e62889 authored by Chris Scott's avatar Chris Scott

Introduce setter-methods for distanceFilter, stationaryRadius. ignore calls...

Introduce setter-methods for distanceFilter, stationaryRadius.  ignore calls to updateCurrentLocation when there already exists a bgTask
parent d3feab55
...@@ -11,11 +11,12 @@ ...@@ -11,11 +11,12 @@
- (void) configure:(CDVInvokedUrlCommand*)command; - (void) configure:(CDVInvokedUrlCommand*)command;
- (void) start:(CDVInvokedUrlCommand*)command; - (void) start:(CDVInvokedUrlCommand*)command;
- (void) stop:(CDVInvokedUrlCommand*)command; - (void) stop:(CDVInvokedUrlCommand*)command;
- (void) test:(CDVInvokedUrlCommand*)command;
- (void) finish:(CDVInvokedUrlCommand*)command; - (void) finish:(CDVInvokedUrlCommand*)command;
- (void) onPaceChange:(CDVInvokedUrlCommand*)command; - (void) onPaceChange:(CDVInvokedUrlCommand*)command;
- (void) setStationaryRadius:(CDVInvokedUrlCommand*)command;
- (void) setDistanceFilter:(CDVInvokedUrlCommand*)command;
- (void) sync; - (void) sync:(CLLocation*)location;
- (void) onSuspend:(NSNotification *)notification; - (void) onSuspend:(NSNotification *)notification;
- (void) onResume:(NSNotification *)notification; - (void) onResume:(NSNotification *)notification;
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
NSNumber *maxBackgroundHours; NSNumber *maxBackgroundHours;
CLLocationManager *locationManager; CLLocationManager *locationManager;
CDVLocationData *locationData; CDVLocationData *locationData;
NSMutableArray *locationCache;
NSDate *suspendedAt; NSDate *suspendedAt;
CLCircularRegion *myRegion; CLCircularRegion *myRegion;
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
- (void)pluginInitialize - (void)pluginInitialize
{ {
// background location cache, for when no network is detected. // background location cache, for when no network is detected.
locationCache = [NSMutableArray array];
locationManager = [[CLLocationManager alloc] init]; locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self; locationManager.delegate = self;
...@@ -99,16 +98,6 @@ ...@@ -99,16 +98,6 @@
[locationManager stopMonitoringSignificantLocationChanges]; [locationManager stopMonitoringSignificantLocationChanges];
} }
- (void) test:(CDVInvokedUrlCommand*)command
{
NSLog(@"- CDVBackgroundGeoLocation test");
[locationManager startMonitoringSignificantLocationChanges];
if ([locationCache count] > 0){
[self sync];
} else {
NSLog(@"- CDVBackgroundGeoLocation could not find a location to sync");
}
}
/** /**
* Change pace to moving/stopped * Change pace to moving/stopped
* @param {Boolean} isMoving * @param {Boolean} isMoving
...@@ -123,6 +112,31 @@ ...@@ -123,6 +112,31 @@
[self setPace:isMoving]; [self setPace:isMoving];
} }
} }
/**
* Change stationary radius
*/
- (void) setStationaryRadius:(CDVInvokedUrlCommand *)command
{
stationaryRadius = [[command.arguments objectAtIndex: 0] intValue];
NSLog(@"- CDVBackgroundGeoLocation setStationaryRadius %d", stationaryRadius);
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
/**
* Change distance-filter
*/
- (void) setDistanceFilter:(CDVInvokedUrlCommand *)command
{
distanceFilter = [[command.arguments objectAtIndex: 0] intValue];
NSLog(@"- CDVBackgroundGeoLocation setDistanceFilter %d", distanceFilter);
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
/** /**
* Called by js to signify the end of a background-geolocation event * Called by js to signify the end of a background-geolocation event
*/ */
...@@ -140,8 +154,8 @@ ...@@ -140,8 +154,8 @@
NSLog(@"- CDVBackgroundGeoLocation suspend"); NSLog(@"- CDVBackgroundGeoLocation suspend");
suspendedAt = [NSDate date]; suspendedAt = [NSDate date];
if (enabled && !isMoving) { if (enabled) {
[self setPace: NO]; [self setPace: isMoving];
} }
} }
/** /**
...@@ -154,30 +168,26 @@ ...@@ -154,30 +168,26 @@
[locationManager stopMonitoringSignificantLocationChanges]; [locationManager stopMonitoringSignificantLocationChanges];
[locationManager stopUpdatingLocation]; [locationManager stopUpdatingLocation];
} }
// When coming back-to-life, flush the background queue.
if ([locationCache count] > 0){
[self sync];
}
} }
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{ {
NSLog(@"- CDVBackgroundGeoLocation didUpdateLocations"); NSLog(@"- CDVBackgroundGeoLocation didUpdateLocations");
// Handle location updates as normal, code omitted for brevity.
// The omitted code should determine whether to reject the location update for being too
// old, too close to the previous one, too inaccurate and so forth according to your own
// application design.
[locationCache addObjectsFromArray:locations];
UIApplication *app = [UIApplication sharedApplication]; UIApplication *app = [UIApplication sharedApplication];
// Bail out if there's already a background-task in-effect.
if (bgTask != UIBackgroundTaskInvalid) {
NSLog(@" Abort: found existing background-task");
return;
}
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[self stopBackgroundTask]; [self stopBackgroundTask];
}]; }];
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
[self sync]; [self sync:[locations lastObject]];
}]; }];
} }
/** /**
...@@ -185,10 +195,9 @@ ...@@ -185,10 +195,9 @@
* We can't assume normal network access. * We can't assume normal network access.
* bgTask is defined as an instance variable of type UIBackgroundTaskIdentifier * bgTask is defined as an instance variable of type UIBackgroundTaskIdentifier
*/ */
-(void) sync -(void) sync:(CLLocation *)location
{ {
// Fetch last recorded location // Fetch last recorded location
CLLocation *location = [locationCache lastObject];
NSMutableDictionary* returnInfo = [NSMutableDictionary dictionaryWithCapacity:8]; NSMutableDictionary* returnInfo = [NSMutableDictionary dictionaryWithCapacity:8];
NSNumber* timestamp = [NSNumber numberWithDouble:([location.timestamp timeIntervalSince1970] * 1000)]; NSNumber* timestamp = [NSNumber numberWithDouble:([location.timestamp timeIntervalSince1970] * 1000)];
...@@ -212,9 +221,9 @@ ...@@ -212,9 +221,9 @@
- (void) stopBackgroundTask - (void) stopBackgroundTask
{ {
UIApplication *app = [UIApplication sharedApplication]; UIApplication *app = [UIApplication sharedApplication];
NSLog(@"- CDVBackgroundGeoLocation stopBackgroundTask (remaining t: %f)", app.backgroundTimeRemaining);
if (bgTask != UIBackgroundTaskInvalid) if (bgTask != UIBackgroundTaskInvalid)
{ {
NSLog(@"- CDVBackgroundGeoLocation stopBackgroundTask (remaining t: %f)", app.backgroundTimeRemaining);
[app endBackgroundTask:bgTask]; [app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid; bgTask = UIBackgroundTaskInvalid;
} }
...@@ -262,6 +271,7 @@ ...@@ -262,6 +271,7 @@
} }
if (value == YES) { if (value == YES) {
[locationManager stopMonitoringSignificantLocationChanges]; [locationManager stopMonitoringSignificantLocationChanges];
locationManager.distanceFilter = distanceFilter;
[locationManager startUpdatingLocation]; [locationManager startUpdatingLocation];
} else { } else {
[locationManager stopUpdatingLocation]; [locationManager stopUpdatingLocation];
......
...@@ -52,13 +52,6 @@ module.exports = { ...@@ -52,13 +52,6 @@ module.exports = {
'stop', 'stop',
[]); []);
}, },
test: function(success, failure, config) {
exec(success || function() {},
failure || function() {},
'BackgroundGeoLocation',
'test',
[]);
},
finish: function(success, failure) { finish: function(success, failure) {
exec(success || function() {}, exec(success || function() {},
failure || function() {}, failure || function() {},
...@@ -72,6 +65,20 @@ module.exports = { ...@@ -72,6 +65,20 @@ module.exports = {
'BackgroundGeoLocation', 'BackgroundGeoLocation',
'onPaceChange', 'onPaceChange',
[isMoving]); [isMoving]);
},
setStationaryRadius: function(value, success, failure) {
exec(success || function() {},
failure || function() {},
'BackgroundGeoLocation',
'setStationaryRadius',
[value]);
},
setDistanceFilter: function(value, success, failure) {
exec(success || function() {},
failure || function() {},
'BackgroundGeoLocation',
'setDistanceFilter',
[value]);
} }
}; };
......
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