package com.jianzhi_inc.dandelion.bdpush;

import android.util.Log;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * Baidu Push Message Cordova Plugin
 * Created by Sam on 9/19/15.
 */
public class BDPush extends CordovaPlugin {

    public final static String ACTION_BIND = "bind";
    public final static String ACTION_UNBIND = "unbind";


    //public final static String CALLBACK_BIND_SUCCESS = "bind_success";

    protected String apiKey;
    public static CallbackContext currentCallbackContext;

    /** TAG to Log */
    public static final String TAG = BDPush.class.getSimpleName();


    @Override
    protected void pluginInitialize() {
        super.pluginInitialize();
        if(null == this.apiKey) {
            this.apiKey = preferences.getString("BD_PUSH_API_KEY", "");
        }
    }

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

        Log.i(TAG, action + " is called, param [" + args.toString() + "]");

        if(ACTION_BIND.equals(action)) {
            return bindBDPush(args, callbackContext);
        }
        if(ACTION_UNBIND.equals(action)) {
            return unbindBDPush(args, callbackContext);
        }
        return super.execute(action, args, callbackContext);
    }


    private boolean bindBDPush(JSONArray args, CallbackContext callbackContext) {
        PushManager.startWork(webView.getContext(), PushConstants.LOGIN_TYPE_API_KEY, this.apiKey);
        currentCallbackContext = callbackContext;
        return true;
    }

    private boolean unbindBDPush(JSONArray args, CallbackContext callbackContext) {
        PushManager.stopWork(webView.getContext());
        currentCallbackContext = callbackContext;
        return true;
    }
}

@@ -206,16 +206,60 @@
 ontext;%0A
+import org.apache.cordova.CordovaInterface;%0A
 import o
@@ -1609,32 +1609,151 @@
 lbackContext) %7B%0A
+        cordova.getThreadPool().execute(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         PushMana
@@ -1836,16 +1836,24 @@
 piKey);%0A
+        
         
@@ -1886,32 +1886,58 @@
 allbackContext;%0A
+            %7D%0A        %7D);%0A
         return t
@@ -2024,32 +2024,223 @@
 lbackContext) %7B%0A
+        cordova.getThreadPool().execute(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                if(PushManager.isConnected(webView.getContext())) %7B%0A            
         PushMana
@@ -2275,32 +2275,44 @@
 ext());%0A        
+      
+      
 currentCallbackC
@@ -2329,32 +2329,76 @@
 allbackContext;%0A
+                %7D%0A            %7D%0A        %7D);%0A
         return t

fix unbind method