Commit e0c61bd6 authored by Chris Scott's avatar Chris Scott

Added life-cycle sounds for debugging. Turn them on by sending debug: true to...

Added life-cycle sounds for debugging.  Turn them on by sending debug: true to configure method.  also must enable background-audio background-mode manually in XCode.  See defines at top of CDVBackgroundGeoLocation.m for the sound-definitions.
parent 5481b52f
...@@ -6,8 +6,19 @@ ...@@ -6,8 +6,19 @@
#import "CDVLocation.h" #import "CDVLocation.h"
#import "CDVBackgroundGeoLocation.h" #import "CDVBackgroundGeoLocation.h"
#import <Cordova/CDVJSON.h> #import <Cordova/CDVJSON.h>
#import <AudioToolbox/AudioToolbox.h>
// Debug sounds for bg-geolocation life-cycle events.
// http://iphonedevwiki.net/index.php/AudioServices
#define exitRegionSound 1005
#define locationSyncSound 1004
#define paceChangeYesSound 1110
#define paceChangeNoSound 1112
#define acquiringLocationSound 1103
#define acquiredLocationSound 1052
@implementation CDVBackgroundGeoLocation { @implementation CDVBackgroundGeoLocation {
BOOL isDebugging;
BOOL enabled; BOOL enabled;
NSString *token; NSString *token;
NSString *url; NSString *url;
...@@ -32,8 +43,6 @@ ...@@ -32,8 +43,6 @@
NSInteger distanceFilter; NSInteger distanceFilter;
NSInteger locationTimeout; NSInteger locationTimeout;
NSInteger desiredAccuracy; NSInteger desiredAccuracy;
} }
- (void)pluginInitialize - (void)pluginInitialize
...@@ -44,6 +53,7 @@ ...@@ -44,6 +53,7 @@
locationManager.delegate = self; locationManager.delegate = self;
stationaryLocation = nil; stationaryLocation = nil;
isDebugging = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSuspend:) name:UIApplicationDidEnterBackgroundNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSuspend:) name:UIApplicationDidEnterBackgroundNotification object:nil];
...@@ -63,11 +73,12 @@ ...@@ -63,11 +73,12 @@
token = [command.arguments objectAtIndex: 0]; token = [command.arguments objectAtIndex: 0];
url = [command.arguments objectAtIndex: 1]; url = [command.arguments objectAtIndex: 1];
// Location filtering. // Params.
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]; desiredAccuracy = [[command.arguments objectAtIndex: 5] intValue];
isDebugging = [[command.arguments objectAtIndex: 6] boolValue];
syncCallbackId = command.callbackId; syncCallbackId = command.callbackId;
...@@ -100,6 +111,7 @@ ...@@ -100,6 +111,7 @@
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); NSLog(@" - desiredAccuracy: %ld", (long)desiredAccuracy);
NSLog(@" - debug: %hhd", isDebugging);
} }
- (void) setConfig:(CDVInvokedUrlCommand*)command - (void) setConfig:(CDVInvokedUrlCommand*)command
{ {
...@@ -143,7 +155,6 @@ ...@@ -143,7 +155,6 @@
{ {
isMoving = [[command.arguments objectAtIndex: 0] boolValue]; isMoving = [[command.arguments objectAtIndex: 0] boolValue];
NSLog(@"- CDVBackgroundGeoLocation onPaceChange %hhd", isMoving); NSLog(@"- CDVBackgroundGeoLocation onPaceChange %hhd", isMoving);
UIApplicationState state = [[UIApplication sharedApplication] applicationState]; UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground) { if (state == UIApplicationStateBackground) {
[self setPace:isMoving]; [self setPace:isMoving];
...@@ -196,7 +207,7 @@ ...@@ -196,7 +207,7 @@
*/ */
-(void) onSuspend:(NSNotification *) notification -(void) onSuspend:(NSNotification *) notification
{ {
NSLog(@"- CDVBackgroundGeoLocation suspend (enabled? %hhdd", enabled); NSLog(@"- CDVBackgroundGeoLocation suspend (enabled? %hhd)", enabled);
suspendedAt = [NSDate date]; suspendedAt = [NSDate date];
if (enabled) { if (enabled) {
...@@ -242,8 +253,14 @@ ...@@ -242,8 +253,14 @@
if (isAcquiringStationaryLocation) { if (isAcquiringStationaryLocation) {
NSLog(@"- Acquiring stationary location, accuracy: %f", newLocation.horizontalAccuracy); NSLog(@"- Acquiring stationary location, accuracy: %f", newLocation.horizontalAccuracy);
if (![self isBestStationaryLocation:newLocation]) { if (![self isBestStationaryLocation:newLocation]) {
if (isDebugging) {
AudioServicesPlaySystemSound (acquiringLocationSound);
}
return; return;
} }
if (isDebugging) {
AudioServicesPlaySystemSound (acquiredLocationSound);
}
[locationManager stopUpdatingLocation]; [locationManager stopUpdatingLocation];
isAcquiringStationaryLocation = NO; isAcquiringStationaryLocation = NO;
[locationManager startMonitoringSignificantLocationChanges]; [locationManager startMonitoringSignificantLocationChanges];
...@@ -266,7 +283,7 @@ ...@@ -266,7 +283,7 @@
} }
-(BOOL) isBestStationaryLocation:(CLLocation*)location { -(BOOL) isBestStationaryLocation:(CLLocation*)location {
stationaryLocationAttempts++; stationaryLocationAttempts++;
if (stationaryLocationAttempts == 5) { if (stationaryLocationAttempts == 7) {
return true; return true;
} }
if (stationaryLocation == nil || stationaryLocation.horizontalAccuracy > location.horizontalAccuracy) { if (stationaryLocation == nil || stationaryLocation.horizontalAccuracy > location.horizontalAccuracy) {
...@@ -286,6 +303,9 @@ ...@@ -286,6 +303,9 @@
-(void) sync:(CLLocation*)location -(void) sync:(CLLocation*)location
{ {
NSLog(@" %f,%f", location.coordinate.latitude, location.coordinate.longitude); NSLog(@" %f,%f", location.coordinate.latitude, location.coordinate.longitude);
if (isDebugging) {
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)];
...@@ -323,6 +343,9 @@ ...@@ -323,6 +343,9 @@
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{ {
NSLog(@"- CDVBackgroundGeoLocation exit region"); NSLog(@"- CDVBackgroundGeoLocation exit region");
if (isDebugging) {
AudioServicesPlaySystemSound (exitRegionSound);
}
[self setPace:YES]; [self setPace:YES];
} }
/** /**
...@@ -354,6 +377,9 @@ ...@@ -354,6 +377,9 @@
{ {
NSLog(@"- CDVBackgroundGeoLocation setPace %d, stationaryRegion? %d", value, stationaryRegion!=nil); NSLog(@"- CDVBackgroundGeoLocation setPace %d, stationaryRegion? %d", value, stationaryRegion!=nil);
isMoving = value; isMoving = value;
if (isDebugging) {
AudioServicesPlaySystemSound (isMoving ? paceChangeYesSound : paceChangeNoSound);
}
if (value == YES) { if (value == YES) {
if (stationaryRegion != nil) { if (stationaryRegion != nil) {
[locationManager stopMonitoringForRegion:stationaryRegion]; [locationManager stopMonitoringForRegion:stationaryRegion];
...@@ -369,7 +395,7 @@ ...@@ -369,7 +395,7 @@
isAcquiringStationaryLocation = YES; isAcquiringStationaryLocation = YES;
stationaryLocationAttempts = 0; stationaryLocationAttempts = 0;
locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[locationManager startUpdatingLocation]; [locationManager startUpdatingLocation];
} }
} }
...@@ -385,7 +411,7 @@ ...@@ -385,7 +411,7 @@
} }
stationaryRegion = [[CLCircularRegion alloc] initWithCenter: coord radius:stationaryRadius identifier:@"BackgroundGeoLocation stationary region"]; stationaryRegion = [[CLCircularRegion alloc] initWithCenter: coord radius:stationaryRadius identifier:@"BackgroundGeoLocation stationary region"];
stationaryRegion.notifyOnExit = YES; stationaryRegion.notifyOnExit = YES;
[locationManager startMonitoringForRegion:stationaryRegion]; [locationManager startMonitoringForRegion:stationaryRegion desiredAccuracy:desiredAccuracy];
} }
// If you don't stopMonitorying when application terminates, the app will be awoken still when a // If you don't stopMonitorying when application terminates, the app will be awoken still when a
......
cordova.define("org.transistorsoft.cordova.background-geolocation.BackgroundGeoLocation", function(require, exports, module) {/***
* Custom Cordova Background GeoLocation plugin. Uses iOS native API
* @author <chris@transistorsoft.com>
* @author <brian@briansamson.com>
* iOS native-side is largely based upon http://www.mindsizzlers.com/2011/07/ios-background-location/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
var exec = require("cordova/exec"); var exec = require("cordova/exec");
module.exports = { module.exports = {
configure: function(success, failure, config) { configure: function(success, failure, config) {
...@@ -31,12 +7,13 @@ module.exports = { ...@@ -31,12 +7,13 @@ module.exports = {
distanceFilter = (config.distanceFilter >= 0) ? config.distanceFilter : 500, // meters distanceFilter = (config.distanceFilter >= 0) ? config.distanceFilter : 500, // meters
locationTimeout = (config.locationTimeout >= 0) ? config.locationTimeout : 60, // seconds locationTimeout = (config.locationTimeout >= 0) ? config.locationTimeout : 60, // seconds
desiredAccuracy = (config.desiredAccuracy >= 0) ? config.desiredAccuracy : 100; // meters desiredAccuracy = (config.desiredAccuracy >= 0) ? config.desiredAccuracy : 100; // meters
debug = config.debug || false;
exec(success || function() {}, exec(success || function() {},
failure || function() {}, failure || function() {},
'BackgroundGeoLocation', 'BackgroundGeoLocation',
'configure', 'configure',
[authToken, url, stationaryRadius, distanceFilter, locationTimeout, desiredAccuracy]); [authToken, url, stationaryRadius, distanceFilter, locationTimeout, desiredAccuracy, debug]);
}, },
start: function(success, failure, config) { start: function(success, failure, config) {
exec(success || function() {}, exec(success || function() {},
...@@ -89,4 +66,4 @@ module.exports = { ...@@ -89,4 +66,4 @@ module.exports = {
'getStationaryLocation', 'getStationaryLocation',
[]); []);
} }
};}); };
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