package me.tonny.cordova.plugins.apsalar;

import android.app.Activity;

import com.apsalar.sdk.Apsalar;

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

public class ApsalarPlugin extends CordovaPlugin {
    public static final String ACTIONS_SEND_EVENT = "sendEvent";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

        if (action.equals(ACTIONS_SEND_EVENT)) {
            this.sendEvent(args.getString(0), args.getString(1), callbackContext);
            return true;
        }

        return false;
      }

    private void sendEvent(final String eventName, final String key, final String value, final CallbackContext callbackContext) {
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                Apsalar.event(eventName, key, value);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, true));
            }
        });
    }

    @Override
    public void onPause(boolean multitasking) {
        Apsalar.unregisterApsalarReceiver();
    }

    @Override
    public void onResume(boolean multitasking) {
        Apsalar.registerReceiver(this.cordova.getActivity().getApplicationContext());
    }
}

@@ -40,38 +40,8 @@
 r;%0A%0A
-import android.app.Activity;%0A%0A
 impo
@@ -145,22 +145,108 @@
 .Cordova
-Plugin
+Interface;%0Aimport org.apache.cordova.CordovaPlugin;%0Aimport org.apache.cordova.CordovaWebView
 ;%0Aimport
@@ -337,16 +337,45 @@
 eption;%0A
+import org.json.JSONObject;%0A%0A
 %0Apublic 
@@ -513,279 +513,887 @@
 lic 
-boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException %7B%0A%0A        if (action.equals(ACTIONS_SEND_EVENT)) %7B%0A            this.sendEvent(args.getString(0), args.getString(1), callbackContext);%0A            return true;%0A        %7D%0A
+void initialize(CordovaInterface cordova, CordovaWebView webView) %7B%0A%0A        int aplKeyId = cordova.getActivity().getResources().getIdentifier(%22asl_key%22, %22string%22, cordova.getActivity().getPackageName());%0A        String aplKey = cordova.getActivity().getString(aplKeyId);%0A%0A        int aplSecretId = cordova.getActivity().getResources().getIdentifier(%22asl_secret%22, %22string%22, cordova.getActivity().getPackageName());%0A        String aplSecret = cordova.getActivity().getString(aplSecretId);%0A%0A        if (aplKey != null && aplSecret != null) %7B%0A            Apsalar.startSession(cordova.getActivity(), aplKey, aplSecret);%0A        %7D%0A        super.initialize(cordova, webView);%0A    %7D%0A%0A    @Override%0A    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException %7B%0A        this.sendEvent(action, (JSONObject) args.get(0), callbackContext);
 %0A   
@@ -1415,19 +1415,18 @@
 se;%0A    
-  
 %7D
+%0A
 %0A%0A    pr
@@ -1480,38 +1480,23 @@
 nal 
-String key, final String value
+JSONObject args
 , fi
@@ -1643,45 +1643,173 @@
     
-Apsalar.event(eventName, key, value);
+if (args == null) %7B%0A                    Apsalar.event(eventName);%0A                %7D else %7B%0A                    Apsalar.eventJSON(eventName, args);%0A                %7D%0A
 %0A   
@@ -1990,32 +1990,69 @@
 multitasking) %7B%0A
+        super.onPause(multitasking);%0A
         Apsalar.
@@ -2142,32 +2142,70 @@
 multitasking) %7B%0A
+        super.onResume(multitasking);%0A
         Apsalar.

Add Apsalar initialize and event