Commit eb1fa765 authored by Chris Scott's avatar Chris Scott

error-handling in getCurrentPosition

parent a85d5b90
...@@ -282,8 +282,14 @@ Retrieves the current position. This method instructs the native code to fetch ...@@ -282,8 +282,14 @@ Retrieves the current position. This method instructs the native code to fetch
######@param {Integer} taskId The taskId used to send to bgGeo.finish(taskId) in order to signal completion of your callbackFn ######@param {Integer} taskId The taskId used to send to bgGeo.finish(taskId) in order to signal completion of your callbackFn
``` ```
bgGeo.changePace(true); // <-- Aggressive GPS monitoring immediately engaged. bgGeo.getCurrentPosition(function(location, taskId) {
bgGeo.changePace(false); // <-- Disable aggressive GPS monitoring. Engages stationary-mode. // This location is already persisted to plugin’s SQLite db.
// If you’ve configured #autoSync: true, the HTTP POST has already started.
console.log(“- Current position received: “, location);
bgGeo.finish(taskId);
});
``` ```
####`changePace(enabled, successFn, failureFn)` ####`changePace(enabled, successFn, failureFn)`
......
...@@ -31,6 +31,12 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -31,6 +31,12 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
private static final String TAG = "TSLocationManager"; private static final String TAG = "TSLocationManager";
private static CordovaWebView gWebView; private static CordovaWebView gWebView;
public static Boolean forceReload = false; public static Boolean forceReload = false;
/**
* Timeout in millis for a getCurrentPosition request to give up.
* TODO make configurable.
*/
private static final long GET_CURRENT_POSITION_TIMEOUT = 30000;
public static final String ACTION_START = "start"; public static final String ACTION_START = "start";
public static final String ACTION_STOP = "stop"; public static final String ACTION_STOP = "stop";
...@@ -57,6 +63,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -57,6 +63,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
private Boolean stopOnTerminate = false; private Boolean stopOnTerminate = false;
private Boolean isMoving = false; private Boolean isMoving = false;
private Boolean isAcquiringCurrentPosition = false; private Boolean isAcquiringCurrentPosition = false;
private long isAcquiringCurrentPositionSince;
private Intent backgroundServiceIntent; private Intent backgroundServiceIntent;
private DetectedActivity currentActivity; private DetectedActivity currentActivity;
...@@ -208,13 +215,18 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -208,13 +215,18 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
callbackContext.success(); callbackContext.success();
} else if (BackgroundGeolocationService.ACTION_GET_CURRENT_POSITION.equalsIgnoreCase(action)) { } else if (BackgroundGeolocationService.ACTION_GET_CURRENT_POSITION.equalsIgnoreCase(action)) {
result = true; result = true;
onGetCurrentPosition(callbackContext); if (!isEnabled) {
callbackContext.error(401); // aka HTTP UNAUTHORIZED
} else {
onGetCurrentPosition(callbackContext);
}
} }
return result; return result;
} }
private void onGetCurrentPosition(CallbackContext callbackContext) { private void onGetCurrentPosition(CallbackContext callbackContext) {
isAcquiringCurrentPosition = true; isAcquiringCurrentPosition = true;
isAcquiringCurrentPositionSince = System.nanoTime();
addCurrentPositionListener(callbackContext); addCurrentPositionListener(callbackContext);
Bundle event = new Bundle(); Bundle event = new Bundle();
...@@ -508,8 +520,18 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -508,8 +520,18 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
*/ */
public void onEventMainThread(ActivityRecognitionResult result) { public void onEventMainThread(ActivityRecognitionResult result) {
currentActivity = result.getMostProbableActivity(); currentActivity = result.getMostProbableActivity();
String activityName = BackgroundGeolocationService.getActivityName(currentActivity.getType());
int confidence = currentActivity.getConfidence(); if (isAcquiringCurrentPosition) {
long elapsedMillis = (System.nanoTime() - isAcquiringCurrentPositionSince) / 1000000;
if (elapsedMillis > GET_CURRENT_POSITION_TIMEOUT) {
isAcquiringCurrentPosition = false;
Log.i(TAG, "- getCurrentPosition timeout, giving up");
for (CallbackContext callback : currentPositionCallbacks) {
callback.error(408); // aka HTTP 408 Request Timeout
}
currentPositionCallbacks.clear();
}
}
} }
/** /**
* EventBus listener * EventBus listener
...@@ -528,6 +550,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin { ...@@ -528,6 +550,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
runInBackground(locationCallback, result); runInBackground(locationCallback, result);
if (isAcquiringCurrentPosition) { if (isAcquiringCurrentPosition) {
// Current position has arrived: release the hounds.
isAcquiringCurrentPosition = false; isAcquiringCurrentPosition = false;
for (CallbackContext callback : currentPositionCallbacks) { for (CallbackContext callback : currentPositionCallbacks) {
result = new PluginResult(PluginResult.Status.OK, location); result = new PluginResult(PluginResult.Status.OK, location);
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMotionChange:) name:@"TSLocationManager.motionchange" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMotionChange:) name:@"TSLocationManager.motionchange" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEnterGeofence:) name:@"TSLocationManager.geofence" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEnterGeofence:) name:@"TSLocationManager.geofence" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSyncComplete:) name:@"TSLocationManager.sync" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSyncComplete:) name:@"TSLocationManager.sync" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLocationManagerError:) name:@"TSLocationManager.error" object:nil];
} }
/** /**
...@@ -352,6 +352,25 @@ ...@@ -352,6 +352,25 @@
[bgGeo error:taskId message:error]; [bgGeo error:taskId message:error];
} }
- (void) onLocationManagerError:(NSNotification*)notification
{
NSLog(@" - onLocationManagerError: %@", notification.userInfo);
NSString *errorType = [notification.userInfo objectForKey:@"type"];
if ([errorType isEqualToString:@"location"]) {
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsInt:[[notification.userInfo objectForKey:@"code"] intValue]];
if ([self.currentPositionListeners count]) {
[result setKeepCallbackAsBool:NO];
for (NSString *callbackId in self.currentPositionListeners) {
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
}
[self.currentPositionListeners removeAllObjects];
}
[self.commandDelegate sendPluginResult:result callbackId:locationCallbackId];
}
}
/** /**
* If you don't stopMonitoring when application terminates, the app will be awoken still when a * If you don't stopMonitoring when application terminates, the app will be awoken still when a
* new location arrives, essentially monitoring the user's location even when they've killed the app. * new location arrives, essentially monitoring the user's location even when they've killed the app.
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
@property (nonatomic) CLLocationDistance odometer; @property (nonatomic) CLLocationDistance odometer;
@property (nonatomic, strong) CLLocationManager* locationManager; @property (nonatomic, strong) CLLocationManager* locationManager;
- (void) configure:(NSDictionary*)config; - (void) configure:(NSDictionary*)config;
- (void) start; - (void) start;
- (void) stop; - (void) stop;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<dict> <dict>
<key>Headers/TSLocationManager.h</key> <key>Headers/TSLocationManager.h</key>
<data> <data>
JKFHIrez0AKQjHebrRIQXFDiIfQ= dTpEJTYyFANcuJNr9upQ8PrgD3Q=
</data> </data>
<key>Info.plist</key> <key>Info.plist</key>
<data> <data>
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
<dict> <dict>
<key>Headers/TSLocationManager.h</key> <key>Headers/TSLocationManager.h</key>
<data> <data>
JKFHIrez0AKQjHebrRIQXFDiIfQ= dTpEJTYyFANcuJNr9upQ8PrgD3Q=
</data> </data>
<key>Modules/module.modulemap</key> <key>Modules/module.modulemap</key>
<data> <data>
......
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