package edu.berkeley.eecs.emission.cordova.comm;

import org.apache.cordova.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import edu.berkeley.eecs.emission.cordova.comm.CommunicationHelper;
import edu.berkeley.eecs.emission.cordova.connectionsettings.ConnectionSettings;

public class CommunicationHelperPlugin extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
        if (action.equals("pushGetJSON")) {
            try {
                Context ctxt = cordova.getActivity();
                String relativeURL = data.getString(0);
                JSONObject filledMessage = data.getJSONObject(1);

                String commuteTrackerHost = ConnectionSettings.getConnectURL(ctxt);
                String fullURL = commuteTrackerHost + relativeURL;

                String resultString = CommunicationHelper.pushGetJSON(ctxt, fullURL, filledMessage);
                callbackContext.success(new JSONObject(resultString));
            } catch (Exception e) {
                callbackContext.error("While pushing/getting from server "+e.getMessage());
            }
            return true;
        } else {
            return false;
        }
    }
}


@@ -475,16 +475,22 @@
 ay data,
+ final
  Callbac
@@ -616,16 +616,22 @@
         
+final 
 Context 
@@ -732,16 +732,22 @@
         
+final 
 JSONObje
@@ -885,24 +885,30 @@
             
+final 
 String fullU
@@ -947,16 +947,151 @@
 veURL;%0A%0A
+                cordova.getThreadPool().execute(new Runnable() %7B%0A                    public void run() %7B%0A                        try %7B%0A
         
@@ -1392,24 +1392,208 @@
           %7D%0A
+                    %7D%0A                %7D);%0A            %7D catch (Exception e) %7B%0A                callbackContext.error(%22While pushing/getting from server %22+e.getMessage());%0A            %7D%0A
             

Move the call to the server into a separate thread