package com.tenforwardconsulting.cordova.bgloc;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;

public class BackgroundGpsPlugin extends CordovaPlugin {
    private static final String TAG = "BackgroundGpsPlugin";

    public static final String ACTION_START = "start";
    public static final String ACTION_STOP = "stop";
    public static final String ACTION_CONFIGURE = "configure";

    private Intent updateServiceIntent;

    private String authToken;
    private String url;
    private String stationaryRadius = "30";
    private String desiredAccuracy = "100";
    private String distanceFilter = "30";
    private String locationTimeout = "60";
    private String isDebugging = "false";

    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
        Activity activity = this.cordova.getActivity();
        updateServiceIntent = new Intent(activity, LocationUpdateService.class);
        if (ACTION_START.equalsIgnoreCase(action)) {
            if (authToken == null || url == null) {
                callbackContext.error("Call configure before calling start");
                return false;
            }
            updateServiceIntent.putExtra("authToken", authToken);
            updateServiceIntent.putExtra("url", url);
            updateServiceIntent.putExtra("stationaryRadius", stationaryRadius);
            updateServiceIntent.putExtra("desiredAccuracy", desiredAccuracy);
            updateServiceIntent.putExtra("distanceFilter", distanceFilter);
            updateServiceIntent.putExtra("locationTimeout", locationTimeout);
            updateServiceIntent.putExtra("desiredAccuracy", desiredAccuracy);
            updateServiceIntent.putExtra("isDebugging", isDebugging);

            activity.startService(updateServiceIntent);

        } else if (ACTION_STOP.equalsIgnoreCase(action)) {
            activity.stopService(updateServiceIntent);
        } else if (ACTION_CONFIGURE.equalsIgnoreCase(action)) {
            try {
                // [authToken, url, stationaryRadius, distanceFilter, locationTimeout, desiredAccuracy, debug]);

                this.authToken = data.getString(0);
                this.url = data.getString(1);
                this.stationaryRadius = data.getString(2);
                this.distanceFilter = data.getString(3);
                this.locationTimeout = data.getString(4);
                this.desiredAccuracy = data.getString(5);
                this.isDebugging = data.getString(6);

            } catch (JSONException e) {
                callbackContext.error("authToken/url required as parameters: " + e.getMessage());
                return false;
            }
        }

        return true;
    }
}

@@ -633,16 +633,80 @@
 figure%22;
+%0A    public static final String ACTION_SET_CONFIG = %22setConfig%22;
 %0A%0A    pr
@@ -1159,16 +1159,48 @@
 vity();%0A
+        Boolean result = false;%0A
         
@@ -1317,32 +1317,59 @@
 Case(action)) %7B%0A
+            result = true;%0A
             if (
@@ -1494,33 +1494,24 @@
         
-    return false;
+%7D else %7B
 %0A       
@@ -1507,34 +1507,65 @@
 e %7B%0A            
-%7D%0A
+    callbackContext.success();%0A  
             upda
@@ -1552,32 +1552,34 @@
 ;%0A              
+  
 updateServiceInt
@@ -1616,16 +1616,20 @@
 Token);%0A
+    
         
@@ -1678,32 +1678,36 @@
 l);%0A            
+    
 updateServiceInt
@@ -1762,32 +1762,36 @@
 s);%0A            
+    
 updateServiceInt
@@ -1832,32 +1832,36 @@
 siredAccuracy);%0A
+    
             upda
@@ -1924,32 +1924,36 @@
 r);%0A            
+    
 updateServiceInt
@@ -2006,32 +2006,36 @@
 t);%0A            
+    
 updateServiceInt
@@ -2076,32 +2076,36 @@
 siredAccuracy);%0A
+    
             upda
@@ -2163,32 +2163,36 @@
 );%0A%0A            
+    
 activity.startSe
@@ -2219,16 +2219,29 @@
 ntent);%0A
+            %7D
 %0A       
@@ -2284,32 +2284,59 @@
 Case(action)) %7B%0A
+            result = true;%0A
             acti
@@ -2366,32 +2366,71 @@
 ServiceIntent);%0A
+            callbackContext.success();%0A
         %7D else i
@@ -2469,32 +2469,59 @@
 Case(action)) %7B%0A
+            result = true;%0A
             try 
@@ -3175,39 +3175,180 @@
     
+%7D%0A
     
-return false;%0A            %7D
+    %7D else if (ACTION_SET_CONFIG.equalsIgnoreCase(action)) %7B%0A            result = true;%0A            // TODO reconfigure Service%0A            callbackContext.success();
 %0A   
@@ -3370,20 +3370,22 @@
  return 
-true
+result
 ;%0A    %7D%0A

Fix callbacks in Android plugin