Commit e0ec782d authored by Chris Scott's avatar Chris Scott

Refactor to callback into javascript during sync. Implement standard location...

Refactor to callback into javascript during sync.  Implement standard location services for background when detected to be moving, significant-changes api when stopped
parent 1f7459c5
......@@ -12,12 +12,19 @@
- (void) start:(CDVInvokedUrlCommand*)command;
- (void) stop:(CDVInvokedUrlCommand*)command;
- (void) test:(CDVInvokedUrlCommand*)command;
- (void) setHighAccuracy:(CDVInvokedUrlCommand*)command;
- (void) finish:(CDVInvokedUrlCommand*)command;
- (void) sync;
- (void) onSuspend:(NSNotification *)notification;
- (void) onResume:(NSNotification *)notification;
@property(nonatomic,retain) NSString *token;
@property(nonatomic,retain) NSString *url;
@property(nonatomic,retain) NSString *callbackId;
@property UIBackgroundTaskIdentifier bgTask;
@property(nonatomic,assign) BOOL enabled;
@property(nonatomic,retain) NSNumber *maxBackgroundHours;
@property (nonatomic, strong) CLLocationManager* locationManager;
......
......@@ -35,10 +35,13 @@
self.token = [command.arguments objectAtIndex: 0];
self.url = [command.arguments objectAtIndex: 1];
self.callbackId = command.callbackId;
NSLog(@"CDVBackgroundGeoLocation configure");
NSLog(@" -token: %@", self.token);
NSLog(@" -url: %@", self.url);
NSLog(@" -callback: %@", self.callbackId);
}
- (void) start:(CDVInvokedUrlCommand*)command
......@@ -53,7 +56,10 @@
self.enabled = NO;
[self.locationManager stopMonitoringSignificantLocationChanges];
}
- (void) setHighAccuracy:(CDVInvokedUrlCommand *)command
{
NSLog(@"- CDVBackgroundGeoLocation setHighAccuracy");
}
- (void) test:(CDVInvokedUrlCommand*)command
{
NSLog(@"CDVBackgroundGeoLocation test");
......@@ -95,8 +101,20 @@
// application design.
[self.locationCache addObjectsFromArray:locations];
[self sync];
}
-(void) finish:(CDVInvokedUrlCommand*)command
{
NSLog(@"CDVBackgroundGeoLocation finish");
//_completionHandler(UIBackgroundFetchResultNewData);
// Finish the voodoo.
if (self.bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}
}
/**
* We are running in the background if this is being executed.
* We can't assume normal network access.
......@@ -104,6 +122,49 @@
*/
-(void) sync
{
NSLog(@"- CDVBackgroundGeoLocation sync");
// Some voodoo.
// Note that the expiration handler block simply ends the task. It is important that we always
// end tasks that we have started.
UIBackgroundTaskIdentifier bgTask = 0;
UIApplication *app = [UIApplication sharedApplication];
// Some voodoo.
self.bgTask = [app beginBackgroundTaskWithExpirationHandler:
^{
[app endBackgroundTask:bgTask];
}];
CDVPluginResult* result = nil;
// The list of successfully recorded locations on server.
NSMutableArray *recordedLocations = [NSMutableArray array];
// Iterate the queue.
CLLocation *location;
NSMutableDictionary* returnInfo = [NSMutableDictionary dictionaryWithCapacity:8];
location = [self.locationCache lastObject];
NSNumber* timestamp = [NSNumber numberWithDouble:([location.timestamp timeIntervalSince1970] * 1000)];
[returnInfo setObject:timestamp forKey:@"timestamp"];
[returnInfo setObject:[NSNumber numberWithDouble:location.speed] forKey:@"velocity"];
[returnInfo setObject:[NSNumber numberWithDouble:location.verticalAccuracy] forKey:@"altitudeAccuracy"];
[returnInfo setObject:[NSNumber numberWithDouble:location.horizontalAccuracy] forKey:@"accuracy"];
[returnInfo setObject:[NSNumber numberWithDouble:location.course] forKey:@"heading"];
[returnInfo setObject:[NSNumber numberWithDouble:location.altitude] forKey:@"altitude"];
[returnInfo setObject:[NSNumber numberWithDouble:location.coordinate.latitude] forKey:@"latitude"];
[returnInfo setObject:[NSNumber numberWithDouble:location.coordinate.longitude] forKey:@"longitude"];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:returnInfo];
[result setKeepCallbackAsBool:YES];
// Inform javascript a background-fetch event has occurred.
[self.commandDelegate sendPluginResult:result callbackId:self.callbackId];
// Remove our successfully recorded locations from cache.
[self.locationCache removeAllObjects];
/*
NSLog(@"BackgroundGeoLocation sync");
// Note that the expiration handler block simply ends the task. It is important that we always
// end tasks that we have started.
......@@ -173,6 +234,7 @@
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
*/
}
// If you don't stopMonitorying when application terminates, the app will be awoken still when a
......
......@@ -32,7 +32,7 @@ module.exports = {
failure || function() {},
'BackgroundGeoLocation',
'configure',
[config.auth_token, config.url]);
[config.auth_token, config.url, config.callback]);
},
start: function(success, failure, config) {
exec(success || function() {},
......@@ -54,6 +54,20 @@ module.exports = {
'BackgroundGeoLocation',
'test',
[]);
},
setHighAccuracy: function(enabled, success, failure) {
exec(success || function() {},
failure || function() {},
'BackgroundGeoLocation',
'setHighAccuracy',
[]);
},
finish: function(success, failure) {
exec(success || function() {},
failure || function() {},
'BackgroundGeoLocation',
'finish',
[]);
}
};
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