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
######@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.changePace(false); // <-- Disable aggressive GPS monitoring. Engages stationary-mode.
bgGeo.getCurrentPosition(function(location, taskId) {
// 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)`
......
......@@ -31,6 +31,12 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
private static final String TAG = "TSLocationManager";
private static CordovaWebView gWebView;
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_STOP = "stop";
......@@ -57,6 +63,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
private Boolean stopOnTerminate = false;
private Boolean isMoving = false;
private Boolean isAcquiringCurrentPosition = false;
private long isAcquiringCurrentPositionSince;
private Intent backgroundServiceIntent;
private DetectedActivity currentActivity;
......@@ -208,13 +215,18 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
callbackContext.success();
} else if (BackgroundGeolocationService.ACTION_GET_CURRENT_POSITION.equalsIgnoreCase(action)) {
result = true;
onGetCurrentPosition(callbackContext);
if (!isEnabled) {
callbackContext.error(401); // aka HTTP UNAUTHORIZED
} else {
onGetCurrentPosition(callbackContext);
}
}
return result;
}
private void onGetCurrentPosition(CallbackContext callbackContext) {
isAcquiringCurrentPosition = true;
isAcquiringCurrentPositionSince = System.nanoTime();
addCurrentPositionListener(callbackContext);
Bundle event = new Bundle();
......@@ -508,8 +520,18 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
*/
public void onEventMainThread(ActivityRecognitionResult result) {
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
......@@ -528,6 +550,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
runInBackground(locationCallback, result);
if (isAcquiringCurrentPosition) {
// Current position has arrived: release the hounds.
isAcquiringCurrentPosition = false;
for (CallbackContext callback : currentPositionCallbacks) {
result = new PluginResult(PluginResult.Status.OK, location);
......
......@@ -20,7 +20,7 @@
[[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(onSyncComplete:) name:@"TSLocationManager.sync" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onLocationManagerError:) name:@"TSLocationManager.error" object:nil];
}
/**
......@@ -352,6 +352,25 @@
[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
* new location arrives, essentially monitoring the user's location even when they've killed the app.
......
......@@ -6,6 +6,7 @@
@property (nonatomic) CLLocationDistance odometer;
@property (nonatomic, strong) CLLocationManager* locationManager;
- (void) configure:(NSDictionary*)config;
- (void) start;
- (void) stop;
......
......@@ -6,7 +6,7 @@
<dict>
<key>Headers/TSLocationManager.h</key>
<data>
JKFHIrez0AKQjHebrRIQXFDiIfQ=
dTpEJTYyFANcuJNr9upQ8PrgD3Q=
</data>
<key>Info.plist</key>
<data>
......@@ -21,7 +21,7 @@
<dict>
<key>Headers/TSLocationManager.h</key>
<data>
JKFHIrez0AKQjHebrRIQXFDiIfQ=
dTpEJTYyFANcuJNr9upQ8PrgD3Q=
</data>
<key>Modules/module.modulemap</key>
<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