Commit 6099e4b8 authored by Chris Scott's avatar Chris Scott

Keep track of 'enabled' state in Settings -- when using autoStart, only start...

Keep track of 'enabled' state in Settings -- when using autoStart, only start when plugin is enabled
parent a4b1ccc2
...@@ -59,20 +59,16 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin { ...@@ -59,20 +59,16 @@ 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);
Activity activity = this.cordova.getActivity();
Boolean result = false;
Boolean result = false;
if (ACTION_START.equalsIgnoreCase(action) && !isEnabled) { if (ACTION_START.equalsIgnoreCase(action) && !isEnabled) {
result = true; result = true;
isEnabled = true; this.setEnabled(true);
if (!BackgroundGeolocationService.isInstanceCreated()) { callbackContext.success();
activity.startService(backgroundServiceIntent);
}
} else if (ACTION_STOP.equalsIgnoreCase(action)) { } else if (ACTION_STOP.equalsIgnoreCase(action)) {
result = true; result = true;
isEnabled = false; this.setEnabled(false);
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);
...@@ -108,6 +104,25 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin { ...@@ -108,6 +104,25 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin {
return result; return result;
} }
private void setEnabled(boolean value) {
isEnabled = value;
Activity activity = this.cordova.getActivity();
SharedPreferences settings = activity.getSharedPreferences(TAG, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("enabled", isEnabled);
editor.commit();
if (isEnabled) {
if (!BackgroundGeolocationService.isInstanceCreated()) {
activity.startService(backgroundServiceIntent);
}
} else {
activity.stopService(backgroundServiceIntent);
}
}
private boolean applyConfig(JSONArray data) { private boolean applyConfig(JSONArray data) {
Activity activity = this.cordova.getActivity(); Activity activity = this.cordova.getActivity();
......
...@@ -18,8 +18,10 @@ public class BootReceiver extends BroadcastReceiver { ...@@ -18,8 +18,10 @@ public class BootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
SharedPreferences settings = context.getSharedPreferences(TAG, 0); SharedPreferences settings = context.getSharedPreferences(TAG, 0);
boolean startOnBoot = settings.getBoolean("startOnBoot", false); boolean startOnBoot = settings.getBoolean("startOnBoot", false);
if (!startOnBoot) { boolean enabled = settings.getBoolean("enabled", false);
if (!startOnBoot || !enabled) {
return; return;
} }
Log.i(TAG, "- BootReceiver booting service"); Log.i(TAG, "- BootReceiver booting service");
......
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