Commit 58768c92 authored by Chris Scott's avatar Chris Scott

Implement ability of both platforms to getCurrentPosition when plugin is not...

Implement ability of both platforms to getCurrentPosition when plugin is not enabled (via bgGeo.start() )
parent e2e7212e
......@@ -217,11 +217,7 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
callbackContext.success();
} else if (BackgroundGeolocationService.ACTION_GET_CURRENT_POSITION.equalsIgnoreCase(action)) {
result = true;
if (!isEnabled) {
callbackContext.error(401); // aka HTTP UNAUTHORIZED
} else {
onGetCurrentPosition(callbackContext);
}
onGetCurrentPosition(callbackContext);
}
return result;
}
......@@ -231,10 +227,18 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
isAcquiringCurrentPositionSince = System.nanoTime();
addCurrentPositionListener(callbackContext);
Bundle event = new Bundle();
event.putString("name", BackgroundGeolocationService.ACTION_GET_CURRENT_POSITION);
event.putBoolean("request", true);
EventBus.getDefault().post(event);
if (!isEnabled) {
EventBus.getDefault().register(this);
if (!BackgroundGeolocationService.isInstanceCreated()) {
backgroundServiceIntent.putExtra("command", BackgroundGeolocationService.ACTION_GET_CURRENT_POSITION);
this.cordova.getActivity().startService(backgroundServiceIntent);
}
} else {
Bundle event = new Bundle();
event.putString("name", BackgroundGeolocationService.ACTION_GET_CURRENT_POSITION);
event.putBoolean("request", true);
EventBus.getDefault().post(event);
}
}
private Boolean onAddGeofence(JSONObject config) {
try {
......@@ -310,6 +314,8 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
try {
JSONObject location = new JSONObject(launchIntent.getStringExtra("location"));
onLocationChange(location);
launchIntent.removeExtra("forceReload");
launchIntent.removeExtra("location");
} catch (JSONException e) {
Log.w(TAG, e);
}
......@@ -553,15 +559,24 @@ public class CDVBackgroundGeolocation extends CordovaPlugin {
this.onLocationChange(locationData);
}
private void onLocationChange(JSONObject location) {
Log.i(TAG, "- CDVBackgroundGeolocation Rx Location: " + isEnabled);
PluginResult result = new PluginResult(PluginResult.Status.OK, location);
result.setKeepCallback(true);
result.setKeepCallback(true);
runInBackground(locationCallback, result);
if (isAcquiringCurrentPosition) {
// Current position has arrived: release the hounds.
isAcquiringCurrentPosition = false;
// When currentPosition is explicitly requested while plugin is stopped, shut Service down again and stop listening to EventBus
if (!isEnabled) {
backgroundServiceIntent.removeExtra("command");
EventBus.getDefault().unregister(this);
this.cordova.getActivity().stopService(backgroundServiceIntent);
}
// Execute callbacks.
for (CallbackContext callback : currentPositionCallbacks) {
result = new PluginResult(PluginResult.Status.OK, location);
result.setKeepCallback(false);
......
......@@ -324,13 +324,6 @@
- (void) getCurrentPosition:(CDVInvokedUrlCommand*)command
{
if (![bgGeo isEnabled]) {
NSLog(@"- CDVBackgroundGeolocation#getCurrentPosition cannot be used when plugin is disabled");
// If plugin isn't enabled, return 401 Unauthorized
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsInt:401];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
return;
}
if (self.currentPositionListeners == nil) {
self.currentPositionListeners = [[NSMutableArray alloc] init];
}
......
......@@ -10,7 +10,7 @@
</data>
<key>Info.plist</key>
<data>
1G+AqP61j6Sq6MmsyYP98Uqa1/Y=
ksNaWGtWPICW6cxKIrDME82hHsk=
</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