Commit 9593a6c4 authored by Chris Scott's avatar Chris Scott

Re-organize methods

parent 4b1df825
...@@ -182,6 +182,9 @@ var app = { ...@@ -182,6 +182,9 @@ var app = {
headers: { headers: {
"X-FOO": "bar" "X-FOO": "bar"
}, },
params: {
"auth_token": "bar"
},
forceReload: false, forceReload: false,
activityType: 'AutomotiveNavigation', activityType: 'AutomotiveNavigation',
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle. debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
......
...@@ -41,6 +41,9 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin { ...@@ -41,6 +41,9 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin {
private Location stationaryLocation; private Location stationaryLocation;
public static boolean isActive() {
return gWebView != null;
}
@Override @Override
protected void pluginInitialize() { protected void pluginInitialize() {
...@@ -52,19 +55,20 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin { ...@@ -52,19 +55,20 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin {
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException { public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
Log.d(TAG, "execute / action : " + action); Log.d(TAG, "execute / action : " + action);
Boolean result = false;
Activity activity = this.cordova.getActivity();
Boolean result = false;
if (ACTION_START.equalsIgnoreCase(action) && !isEnabled) { if (ACTION_START.equalsIgnoreCase(action) && !isEnabled) {
result = true; result = true;
isEnabled = true; isEnabled = true;
if (!BackgroundGeolocationService.isInstanceCreated()) { if (!BackgroundGeolocationService.isInstanceCreated()) {
this.cordova.getActivity().startService(backgroundServiceIntent); activity.startService(backgroundServiceIntent);
} }
} else if (ACTION_STOP.equalsIgnoreCase(action)) { } else if (ACTION_STOP.equalsIgnoreCase(action)) {
result = true; result = true;
isEnabled = false; isEnabled = false;
this.cordova.getActivity().stopService(backgroundServiceIntent); activity.stopService(backgroundServiceIntent);
callbackContext.success(); callbackContext.success();
} else if (ACTION_CONFIGURE.equalsIgnoreCase(action)) { } else if (ACTION_CONFIGURE.equalsIgnoreCase(action)) {
result = applyConfig(data); result = applyConfig(data);
...@@ -83,11 +87,11 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin { ...@@ -83,11 +87,11 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin {
callbackContext.success(); callbackContext.success();
} }
} else if (ACTION_SET_CONFIG.equalsIgnoreCase(action)) { } else if (ACTION_SET_CONFIG.equalsIgnoreCase(action)) {
this.cordova.getActivity().stopService(backgroundServiceIntent); activity.stopService(backgroundServiceIntent);
result = applyConfig(data); result = applyConfig(data);
// TODO reconfigure Service // TODO reconfigure Service
if (result) { if (result) {
this.cordova.getActivity().stopService(backgroundServiceIntent); activity.stopService(backgroundServiceIntent);
callbackContext.success(); callbackContext.success();
} else { } else {
callbackContext.error("- Configuration error!"); callbackContext.error("- Configuration error!");
...@@ -96,9 +100,9 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin { ...@@ -96,9 +100,9 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin {
result = true; result = true;
this.stationaryCallback = callbackContext; this.stationaryCallback = callbackContext;
} }
return result; return result;
} }
private boolean applyConfig(JSONArray data) { private boolean applyConfig(JSONArray data) {
// This is the IntentService we'll provide to google-play API. // This is the IntentService we'll provide to google-play API.
Activity activity = this.cordova.getActivity(); Activity activity = this.cordova.getActivity();
...@@ -149,10 +153,6 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin { ...@@ -149,10 +153,6 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin {
} }
} }
public static boolean isActive() {
return gWebView != null;
}
public void onPause(boolean multitasking) { public void onPause(boolean multitasking) {
Log.i(TAG, "- onPause"); Log.i(TAG, "- onPause");
if (isEnabled) { if (isEnabled) {
...@@ -165,30 +165,26 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin { ...@@ -165,30 +165,26 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin {
//removeLocationUpdates(); //removeLocationUpdates();
} }
} }
/**
* EventBus listener
* @param {Location} location
*/
public void onEventMainThread(Location location) { public void onEventMainThread(Location location) {
PluginResult result = new PluginResult(PluginResult.Status.OK, BackgroundGeolocationService.locationToJson(location));
result.setKeepCallback(true);
if (location instanceof StationaryLocation) { if (location instanceof StationaryLocation) {
stationaryLocation = location; stationaryLocation = location;
fireStationaryListener(); if (stationaryCallback != null) {
runInBackground(stationaryCallback, result);
}
} else { } else {
PluginResult result = new PluginResult(PluginResult.Status.OK, BackgroundGeolocationService.locationToJson(location));
result.setKeepCallback(true); result.setKeepCallback(true);
runInBackground(locationCallback, result); runInBackground(locationCallback, result);
} }
} }
/**
* Execute onStationary javascript callback when device is determined to have just stopped
*/
private void fireStationaryListener() {
Log.i(TAG, "- fire stationary listener");
if ( (stationaryCallback != null) && (stationaryLocation != null) ) {
final PluginResult result = new PluginResult(PluginResult.Status.OK, BackgroundGeolocationService.locationToJson(stationaryLocation));
result.setKeepCallback(true);
runInBackground(stationaryCallback, result);
}
}
/** /**
* Run a javascript callback in Background * Run a javascript callback in Background
* @param cb * @param cb
......
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