package kr.method.papaya;

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

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class ChatdPlugin extends CordovaPlugin {
	private final String START_SERVICE = "start";
	private final String STOP_SERVICE = "stop";
	private BroadcastReceiver receiver;
	private Intent intentMyService;
    private Context context;

	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
		//서비스 시작
		if (action.equals(START_SERVICE)) {
			this.startService();
		//서비스 종료
		} else if (action.equals(STOP_SERVICE)){
			this.stopService();
		}
		
		return false;
	}
	
	@Override
	public void onDestroy(){LOG.d("ChatdPlugin", "OnDestroy");
		if(context!=null && receiver!=null){
			context.unregisterReceiver(receiver);
		}
		super.onDestroy();
	}
	
	/**
	 * 서비스 시작
	 */
	private void startService(){
		LOG.d("ChatdPlugin", "startService");
		context = cordova.getActivity().getApplicationContext();
		intentMyService = new Intent(context, ChatdService.class);
		receiver = new RestartService();
		
		try 
        {
            IntentFilter mainFilter = new IntentFilter("kr.method.papaya.USER_ACTION");
            
            context.registerReceiver(receiver, mainFilter);
            context.startService(intentMyService);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	/**
	 * 서비스 종료
	 */
	private void stopService(){
		
	}
}

@@ -236,16 +236,54 @@
 ceiver;%0A
+import android.content.ComponentName;%0A
 import a
@@ -373,16 +373,114 @@
 tFilter;
+%0Aimport android.content.ServiceConnection;%0Aimport android.os.IBinder;%0Aimport android.widget.Toast;
 %0A%0Apublic
@@ -614,16 +614,59 @@
 %22stop%22;%0A
+%09private final String CHATD_SEND = %22send%22;%0A
 %09private
@@ -755,16 +755,56 @@
 context;
+%0A    private ChatdService mBoundService;
 %0A%0A%09publi
@@ -1058,16 +1058,119 @@
 vice();%0A
+%09%09%7D else if (action.equals(CHATD_SEND))%7B%0A%09%09%09String message = args.getString(0);%0A%09%09%09this.send(message);%0A
 %09%09%7D%0A%09%09%0A%09
@@ -1344,16 +1344,80 @@
 r);%0A%09%09%7D%0A
+%09%09if(mBoundService!=null)%7B%0A%09%09%09mBoundService.unbindPlugin();%0A%09%09%7D%0A
 %09%09super.
@@ -1865,32 +1865,34 @@
 r);%0A            
+//
 context.startSer
@@ -1914,16 +1914,105 @@
 rvice);%0A
+            context.bindService(intentMyService, mConnection, Context.BIND_AUTO_CREATE);%0A
         
@@ -2139,10 +2139,840 @@
 %7B%0A%09%09%0A%09%7D%0A
+%09%0A%09public void send(String message)%7B%0A%09%09if(mBoundService!=null)%7B%0A%09%09%09mBoundService.send(message);%0A%09%09%7D%0A%09%7D%0A%09%0A%09public void onMessage(int action,String message)%7B%0A%09%09switch(action)%7B%0A%09%09case ChatdWebsocketClient.OPEN:%0A%09%09%09%0A%09%09break;%0A%09%09case ChatdWebsocketClient.CLOSE:%0A%09%09%09%0A%09%09break;%0A%09%09case ChatdWebsocketClient.MESSAGE:%0A%09%09%09%0A%09%09break;%0A%09%09case ChatdWebsocketClient.ERROR:%0A%09%09%09%0A%09%09break;%0A%09%09%7D%0A%09%7D%0A%09%0A%09private ServiceConnection mConnection = new ServiceConnection() %7B%0A        public void onServiceConnected(ComponentName className, IBinder service) %7B%0A            mBoundService = ((ChatdService.LocalBinder)service).getService();%0A            mBoundService.bindPlugin(ChatdPlugin.this);%0A        %7D%0A%0A        public void onServiceDisconnected(ComponentName className) %7B%0A        %09mBoundService.unbindPlugin();%0A            mBoundService = null;%0A        %7D%0A    %7D;%0A
 %7D%0A

Update ChatdPlugin.java