Commit 8ddb474a authored by Chris Scott's avatar Chris Scott

Back-tracking on configurable accuracy settings, since it broke something.

parent d8370c35
...@@ -19,19 +19,19 @@ ...@@ -19,19 +19,19 @@
NSNumber *maxBackgroundHours; NSNumber *maxBackgroundHours;
CLLocationManager *locationManager; CLLocationManager *locationManager;
CDVLocationData *locationData; CDVLocationData *locationData;
NSMutableArray *locationCache;
NSDate *suspendedAt; NSDate *suspendedAt;
CLCircularRegion *myRegion; CLCircularRegion *myRegion;
NSInteger stationaryRadius; NSInteger stationaryRadius;
NSInteger distanceFilter; NSInteger distanceFilter;
NSInteger desiredAccuracy;
NSInteger locationTimeout; NSInteger locationTimeout;
} }
- (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;
...@@ -46,7 +46,6 @@ ...@@ -46,7 +46,6 @@
* @param {Number} stationaryRadius * @param {Number} stationaryRadius
* @param {Number} distanceFilter * @param {Number} distanceFilter
* @param {Number} locationTimeout * @param {Number} locationTimeout
* @param {Number} desiredAccuracy
*/ */
- (void) configure:(CDVInvokedUrlCommand*)command - (void) configure:(CDVInvokedUrlCommand*)command
{ {
...@@ -58,16 +57,15 @@ ...@@ -58,16 +57,15 @@
stationaryRadius = [[command.arguments objectAtIndex: 2] intValue]; stationaryRadius = [[command.arguments objectAtIndex: 2] intValue];
distanceFilter = [[command.arguments objectAtIndex: 3] intValue]; distanceFilter = [[command.arguments objectAtIndex: 3] intValue];
locationTimeout = [[command.arguments objectAtIndex: 4] intValue]; locationTimeout = [[command.arguments objectAtIndex: 4] intValue];
desiredAccuracy = [[command.arguments objectAtIndex: 5] intValue];
syncCallbackId = command.callbackId; syncCallbackId = command.callbackId;
// Set a movement threshold for new events. // Set a movement threshold for new events.
locationManager.activityType = CLActivityTypeOther; locationManager.activityType = CLActivityTypeOther;
locationManager.pausesLocationUpdatesAutomatically = YES; locationManager.pausesLocationUpdatesAutomatically = YES;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.distanceFilter = distanceFilter; // meters
myRegion = nil; myRegion = nil;
isMoving = NO;
NSLog(@"CDVBackgroundGeoLocation configure"); NSLog(@"CDVBackgroundGeoLocation configure");
NSLog(@" - token: %@", token); NSLog(@" - token: %@", token);
...@@ -75,35 +73,16 @@ ...@@ -75,35 +73,16 @@
NSLog(@" - distanceFilter: %ld", (long)distanceFilter); NSLog(@" - distanceFilter: %ld", (long)distanceFilter);
NSLog(@" - stationaryRadius: %ld", (long)stationaryRadius); NSLog(@" - stationaryRadius: %ld", (long)stationaryRadius);
NSLog(@" - locationTimeout: %ld", (long)locationTimeout); NSLog(@" - locationTimeout: %ld", (long)locationTimeout);
NSLog(@" - desiredAccuracy: %ld", (long)desiredAccuracy);
} }
- (void) setConfig:(CDVInvokedUrlCommand*)command - (void) setConfig:(CDVInvokedUrlCommand*)command
{ {
NSLog(@"- CDVBackgroundGeoLocation setConfig"); NSLog(@"- CDVBackgroundGeoLocation setConfig");
NSDictionary *config = [command.arguments objectAtIndex:0];
if (config[@"desiredAccuracy"]) {
desiredAccuracy = [config[@"desiredAccuracy"] intValue];
NSLog(@" desiredAccuracy: %@", config[@"desiredAccuracy"]);
}
if (config[@"stationaryRadius"]) {
stationaryRadius = [config[@"stationaryRadius"] intValue];
NSLog(@" stationaryRadius: %@", config[@"stationaryRadius"]);
}
if (config[@"distanceFilter"]) {
distanceFilter = [config[@"distanceFilter"] intValue];
NSLog(@" distanceFilter: %@", config[@"distanceFilter"]);
}
if (config[@"timeout"]) {
locationTimeout = [config[@"timeout"] intValue];
NSLog(@" locationTimeout: %@", config[@"timeout"]);
}
CDVPluginResult* result = nil; CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; [self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} }
/** /**
* Turn on background geolocation * Turn on background geolocation
*/ */
...@@ -115,12 +94,8 @@ ...@@ -115,12 +94,8 @@
NSLog(@"- CDVBackgroundGeoLocation start (background? %d)", state); NSLog(@"- CDVBackgroundGeoLocation start (background? %d)", state);
if (state == UIApplicationStateBackground) { if (state == UIApplicationStateBackground) {
[self setPace:NO]; [self setPace:isMoving];
} }
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} }
/** /**
* Turn it off * Turn it off
...@@ -129,18 +104,9 @@ ...@@ -129,18 +104,9 @@
{ {
NSLog(@"- CDVBackgroundGeoLocation stop"); NSLog(@"- CDVBackgroundGeoLocation stop");
enabled = NO; enabled = NO;
[locationManager stopUpdatingLocation]; [locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges]; [locationManager stopMonitoringSignificantLocationChanges];
if (myRegion != nil) {
[locationManager stopMonitoringForRegion:myRegion];
myRegion = nil;
}
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} }
/** /**
* Change pace to moving/stopped * Change pace to moving/stopped
...@@ -170,7 +136,7 @@ ...@@ -170,7 +136,7 @@
*/ */
-(void) onSuspend:(NSNotification *) notification -(void) onSuspend:(NSNotification *) notification
{ {
NSLog(@"- CDVBackgroundGeoLocation suspend (%hhd)", enabled); NSLog(@"- CDVBackgroundGeoLocation suspend");
suspendedAt = [NSDate date]; suspendedAt = [NSDate date];
if (enabled) { if (enabled) {
...@@ -193,13 +159,13 @@ ...@@ -193,13 +159,13 @@
{ {
NSLog(@"- CDVBackgroundGeoLocation didUpdateLocations"); NSLog(@"- CDVBackgroundGeoLocation didUpdateLocations");
UIApplication *app = [UIApplication sharedApplication]; // 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];
// Bail out if there's already a background-task in-effect. UIApplication *app = [UIApplication sharedApplication];
if (bgTask != UIBackgroundTaskInvalid) {
NSLog(@" Abort: found existing background-task");
return;
}
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[self stopBackgroundTask]; [self stopBackgroundTask];
...@@ -214,10 +180,8 @@ ...@@ -214,10 +180,8 @@
* 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:(CLLocation *)location -(void) sync:(CLLocation*)location
{ {
// Fetch last recorded location
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)];
[returnInfo setObject:timestamp forKey:@"timestamp"]; [returnInfo setObject:timestamp forKey:@"timestamp"];
...@@ -240,9 +204,9 @@ ...@@ -240,9 +204,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;
} }
...@@ -254,9 +218,7 @@ ...@@ -254,9 +218,7 @@
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{ {
NSLog(@"- CDVBackgroundGeoLocation exit region"); NSLog(@"- CDVBackgroundGeoLocation exit region");
if (enabled) { [self setPace:YES];
[self setPace:YES];
}
} }
/** /**
* 1. turn off std location services * 1. turn off std location services
...@@ -284,61 +246,30 @@ ...@@ -284,61 +246,30 @@
*/ */
- (void)setPace:(BOOL)value - (void)setPace:(BOOL)value
{ {
// When stationaryRadius is 0, don't use sig.changes -- keep GPS on constantly. NSLog(@"- CDVBackgroundGeoLocation setPace %d", value);
isMoving = value; isMoving = value;
NSLog(@"- CDVBackgroundGeoLocation setPace");
NSLog(@" isMoving %d", isMoving);
NSLog(@" desiredAccuracy: %d", desiredAccuracy);
NSLog(@" distanceFilter: %d", distanceFilter);
NSLog(@" stationaryRadius: %d", stationaryRadius);
// First turn off all monitoring.
if (myRegion != nil) { if (myRegion != nil) {
[locationManager stopMonitoringForRegion:myRegion]; [locationManager stopMonitoringForRegion:myRegion];
myRegion = nil; myRegion = nil;
} }
[locationManager stopMonitoringSignificantLocationChanges]; if (value == YES) {
[locationManager stopUpdatingLocation]; [locationManager stopMonitoringSignificantLocationChanges];
switch (desiredAccuracy) {
case 1000:
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
break;
case 100:
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
break;
case 10:
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
break;
case 0:
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
break;
}
locationManager.distanceFilter = (distanceFilter > 0) ? distanceFilter : kCLDistanceFilterNone;
if (isMoving) {
[locationManager startUpdatingLocation]; [locationManager startUpdatingLocation];
} else { } else {
[locationManager startMonitoringSignificantLocationChanges]; [locationManager stopUpdatingLocation];
[self startMonitoringStationaryRegion]; [self startMonitoringStationaryRegion];
[locationManager startMonitoringSignificantLocationChanges];
} }
} }
/** /**
* Creates a new circle around user and region-monitors it for exit * Creates a new circle around user and region-monitors it for exit
*/ */
- (void) startMonitoringStationaryRegion { - (void) startMonitoringStationaryRegion {
NSInteger radius = (stationaryRadius>0) ? stationaryRadius : 1; NSLog(@"- CDVBackgroundGeoLocation createStationaryRegion");
CLLocationCoordinate2D coord = [[locationManager location] coordinate];
NSLog(@"- CDVBackgroundGeoLocation createStationaryRegion (%f, %f)", coord.latitude, coord.longitude);
if (myRegion != nil) { if (myRegion != nil) {
[locationManager stopMonitoringForRegion:myRegion]; [locationManager stopMonitoringForRegion:myRegion];
} }
myRegion = [[CLCircularRegion alloc] initWithCenter: [[locationManager location] coordinate] radius:stationaryRadius identifier:@"BackgroundGeoLocation stationary region"];
myRegion = [[CLCircularRegion alloc] initWithCenter:coord radius:radius identifier:@"BackgroundGeoLocation stationary region"];
myRegion.notifyOnExit = YES; myRegion.notifyOnExit = YES;
[locationManager startMonitoringForRegion:myRegion]; [locationManager startMonitoringForRegion:myRegion];
} }
...@@ -359,4 +290,4 @@ ...@@ -359,4 +290,4 @@
locationManager.delegate = nil; locationManager.delegate = nil;
} }
@end @end
\ No newline at end of file
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