package com.verso.cordova.clipboard;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CallbackContext;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.ClipDescription;

public class Clipboard extends CordovaPlugin {

    private static final String actionCopy = "copy";
    private static final String actionPaste = "paste";

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        ClipboardManager clipboard = (ClipboardManager) cordova.getActivity().getSystemService(Context.CLIPBOARD_SERVICE);

        if (action.equals(actionCopy)) {
            try {
                String text = args.getString(0);
                ClipData clip = ClipData.newPlainText("Text", text);

                clipboard.setPrimaryClip(clip);

                callbackContext.success(text);

                return true;
            } catch (JSONException e) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
            } catch (Exception e) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.toString()));
            }
        } else if (action.equals(actionPaste)) {
            if (!clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT));
            }

            try {
                ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
                String text = item.getText().toString();

                if (text == null) text = "";

                callbackContext.success(text);

                return true;
            } catch (Exception e) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.toString()));
            }
        }

        return false;
    }
}



@@ -442,26 +442,27 @@
  String 
-actionCopy
+ACTION_COPY
  = %22copy
@@ -496,27 +496,28 @@
  String 
-actionPaste
+ACTION_PASTE
  = %22past
@@ -781,30 +781,35 @@
         if (
-action
+ACTION_COPY
 .equals(acti
@@ -814,12 +814,8 @@
 tion
-Copy
 )) %7B
@@ -1400,22 +1400,28 @@
 lse if (
-action
+ACTION_PASTE
 .equals(
@@ -1430,238 +1430,12 @@
 tion
-Paste)) %7B%0A            if (!clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) %7B%0A                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT));%0A            %7D%0A
+)) %7B
 %0A   
@@ -1877,17 +1877,16 @@
       %7D%0A
-%0A
         
@@ -1909,8 +1909,6 @@
   %7D%0A
+
 %7D%0A
-%0A%0A

Remove MIME type check in paste