Commit cfc139fb authored by Chris Scott's avatar Chris Scott

Remove Location-error timeout. LocationManager pausesLocationUpdatesAutomatically will work fine

parent a3a05e74
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#define paceChangeNoSound 1112 #define paceChangeNoSound 1112
#define acquiringLocationSound 1103 #define acquiringLocationSound 1103
#define acquiredLocationSound 1052 #define acquiredLocationSound 1052
#define locationError 1073 #define locationErrorSound 1073
@implementation CDVBackgroundGeoLocation { @implementation CDVBackgroundGeoLocation {
BOOL isDebugging; BOOL isDebugging;
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
UIBackgroundTaskIdentifier bgTask; UIBackgroundTaskIdentifier bgTask;
NSDate *lastBgTaskAt; NSDate *lastBgTaskAt;
NSError *locationError;
BOOL isMoving; BOOL isMoving;
NSNumber *maxBackgroundHours; NSNumber *maxBackgroundHours;
...@@ -336,9 +338,12 @@ ...@@ -336,9 +338,12 @@
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{ {
NSLog(@"- CDVBackgroundGeoLocation didUpdateLocations (isMoving: %d)", isMoving); NSLog(@"- CDVBackgroundGeoLocation didUpdateLocations (isMoving: %d)", isMoving);
locationError = nil;
if (isMoving && !isUpdatingLocation) { if (isMoving && !isUpdatingLocation) {
[self startUpdatingLocation]; [self startUpdatingLocation];
} }
CLLocation *location = [locations lastObject]; CLLocation *location = [locations lastObject];
lastLocation = location; lastLocation = location;
...@@ -368,10 +373,6 @@ ...@@ -368,10 +373,6 @@
if (++locationAcquisitionAttempts == maxStationaryLocationAttempts) { if (++locationAcquisitionAttempts == maxStationaryLocationAttempts) {
isAcquiringStationaryLocation = NO; isAcquiringStationaryLocation = NO;
[self startMonitoringStationaryRegion:stationaryLocation]; [self startMonitoringStationaryRegion:stationaryLocation];
if (isDebugging) {
AudioServicesPlaySystemSound (acquiredLocationSound);
[self notify:@"Acquired stationary location"];
}
} else { } else {
// Unacceptable stationary-location: bail-out and wait for another. // Unacceptable stationary-location: bail-out and wait for another.
return; return;
...@@ -405,25 +406,38 @@ ...@@ -405,25 +406,38 @@
// Uh-oh: already a background-task in-effect. // Uh-oh: already a background-task in-effect.
// If we have a bgTask hanging around for 60 seconds, kill it and move on; otherwise, wait a bit longer for the existing bgTask to finish. // If we have a bgTask hanging around for 60 seconds, kill it and move on; otherwise, wait a bit longer for the existing bgTask to finish.
if (bgTask != UIBackgroundTaskInvalid) { if (bgTask != UIBackgroundTaskInvalid) {
// Fail-safe: If Javascript doesn't explicitly execute our #finish method (perhaps it crashed or waiting for long HTTP timeout),
// bgTask will never get cleared. Enforces that bgTask can never last longer than 60s
NSTimeInterval duration = -[lastBgTaskAt timeIntervalSinceNow]; NSTimeInterval duration = -[lastBgTaskAt timeIntervalSinceNow];
if (duration > 60.0) { if (duration > 60.0) {
[self stopBackgroundTask]; [self stopBackgroundTask];
} else { } else {
NSLog(@"Abort: found existing background-task"); NSLog(@"Abort: found existing background-task");
if (isDebugging) { if (isDebugging) {
[self notify:@"found existing background-task"]; [self notify:@"waiting for existing background-task"];
} }
return; return;
} }
} }
// Create a background-task and delegate to Javascript for syncing location // Create a background-task and delegate to Javascript for syncing location
bgTask = [self createBackgroundTask]; bgTask = [self createBackgroundTask];
[self.commandDelegate runInBackground:^{ [self.commandDelegate runInBackground:^{
[self sync:location]; [self sync:location];
}]; }];
NSLog(@" position: %f,%f, speed: %f", location.coordinate.latitude, location.coordinate.longitude, location.speed);
if (isDebugging) {
[self notify:[NSString stringWithFormat:@"Location update: %s\nSPD: %0.1f | DF: %0.1f | ACY: %d",
((isMoving) ? "MOVING" : "STATIONARY"),
fabsf(location.speed),
locationManager.distanceFilter,
(int) location.horizontalAccuracy]];
AudioServicesPlaySystemSound (locationSyncSound);
}
} }
-(UIBackgroundTaskIdentifier) createBackgroundTask -(UIBackgroundTaskIdentifier) createBackgroundTask
{ {
lastBgTaskAt = [NSDate date]; lastBgTaskAt = [NSDate date];
...@@ -433,7 +447,7 @@ ...@@ -433,7 +447,7 @@
} }
/** /**
* Calculates distanceFilter by rounding speed to nearest 5 and multiplying by 10. * Calculates distanceFilter by rounding speed to nearest 5 and multiplying by 10. Clamped at 1km max.
*/ */
-(float) calculateDistanceFilter:(float)speed -(float) calculateDistanceFilter:(float)speed
{ {
...@@ -441,7 +455,7 @@ ...@@ -441,7 +455,7 @@
if (speed < 100) { if (speed < 100) {
// (rounded-speed-to-nearest-5) / 2)^2 // (rounded-speed-to-nearest-5) / 2)^2
// eg 5.2 becomes (5/2)^2 // eg 5.2 becomes (5/2)^2
newDistanceFilter = pow((5.0 * floorf(speed / 5.0 + 0.5f)), 2) + distanceFilter; newDistanceFilter = pow((5.0 * floorf(fabsf(speed) / 5.0 + 0.5f)), 2) + distanceFilter;
} }
return (newDistanceFilter < 1000) ? newDistanceFilter : 1000; return (newDistanceFilter < 1000) ? newDistanceFilter : 1000;
} }
...@@ -453,16 +467,6 @@ ...@@ -453,16 +467,6 @@
*/ */
-(void) sync:(CLLocation*)location -(void) sync:(CLLocation*)location
{ {
NSLog(@" position: %f,%f, speed: %f", location.coordinate.latitude, location.coordinate.longitude, location.speed);
if (isDebugging) {
[self notify:[NSString stringWithFormat:@"Location update: moving? %d\nSPD: %d, ACY: %d, DF: %d",
isMoving,
(int) location.speed,
(int) location.horizontalAccuracy,
(int) locationManager.distanceFilter]];
AudioServicesPlaySystemSound (locationSyncSound);
}
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"];
...@@ -490,6 +494,10 @@ ...@@ -490,6 +494,10 @@
CLLocationCoordinate2D coord = [location coordinate]; CLLocationCoordinate2D coord = [location coordinate];
NSLog(@"- CDVBackgroundGeoLocation createStationaryRegion (%f,%f)", coord.latitude, coord.longitude); NSLog(@"- CDVBackgroundGeoLocation createStationaryRegion (%f,%f)", coord.latitude, coord.longitude);
if (isDebugging) {
AudioServicesPlaySystemSound (acquiredLocationSound);
[self notify:@"Acquired stationary location"];
}
if (stationaryRegion != nil) { if (stationaryRegion != nil) {
[locationManager stopMonitoringForRegion:stationaryRegion]; [locationManager stopMonitoringForRegion:stationaryRegion];
} }
...@@ -539,7 +547,13 @@ ...@@ -539,7 +547,13 @@
if (isDebugging) { if (isDebugging) {
[self notify:@"Stop detected"]; [self notify:@"Stop detected"];
} }
[self setPace:NO]; if (locationError) {
isMoving = NO;
[self startMonitoringStationaryRegion:lastLocation];
[self stopUpdatingLocation];
} else {
[self setPace:NO];
}
} }
/** /**
...@@ -560,9 +574,12 @@ ...@@ -560,9 +574,12 @@
{ {
NSLog(@"- CDVBackgroundGeoLocation locationManager failed: %@", error); NSLog(@"- CDVBackgroundGeoLocation locationManager failed: %@", error);
if (isDebugging) { if (isDebugging) {
AudioServicesPlaySystemSound (locationError); AudioServicesPlaySystemSound (locationErrorSound);
[self notify:[NSString stringWithFormat:@"Location error: %@", error.localizedDescription]]; [self notify:[NSString stringWithFormat:@"Location error: %@", error.localizedDescription]];
} }
locationError = error;
switch(error.code) { switch(error.code) {
case kCLErrorLocationUnknown: case kCLErrorLocationUnknown:
case kCLErrorNetwork: case kCLErrorNetwork:
...@@ -580,14 +597,15 @@ ...@@ -580,14 +597,15 @@
[self stopUpdatingLocation]; [self stopUpdatingLocation];
} }
} }
- (void) stopUpdatingLocation - (void) stopUpdatingLocation
{ {
[locationManager stopUpdatingLocation]; [locationManager stopUpdatingLocation];
isUpdatingLocation = NO; isUpdatingLocation = NO;
} }
- (void) startUpdatingLocation - (void) startUpdatingLocation
{ {
locationManager.pausesLocationUpdatesAutomatically = YES;
[locationManager startUpdatingLocation]; [locationManager startUpdatingLocation];
isUpdatingLocation = YES; isUpdatingLocation = YES;
} }
......
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