diff --git a/mobile/cordova_plugins.js b/mobile/cordova_plugins.js index 695e5081b..aab670afa 100644 --- a/mobile/cordova_plugins.js +++ b/mobile/cordova_plugins.js @@ -20,6 +20,13 @@ module.exports = [ "clobbers": [ "navigator.splashscreen" ] + }, + { + "file": "plugins/com.verso.cordova.clipboard/www/clipboard.js", + "id": "com.verso.cordova.clipboard.Clipboard", + "clobbers": [ + "cordova.plugins.clipboard" + ] } ]; module.exports.metadata = @@ -27,7 +34,8 @@ module.exports.metadata = { "de.appplant.cordova.plugin.email-composer": "0.8.2dev", "com.phonegap.plugins.barcodescanner": "1.0.1", - "org.apache.cordova.splashscreen": "0.3.0" + "org.apache.cordova.splashscreen": "0.3.0", + "com.verso.cordova.clipboard": "0.1.0" } // BOTTOM OF METADATA }); \ No newline at end of file diff --git a/mobile/plugins/com.verso.cordova.clipboard/www/clipboard.js b/mobile/plugins/com.verso.cordova.clipboard/www/clipboard.js new file mode 100644 index 000000000..129c11130 --- /dev/null +++ b/mobile/plugins/com.verso.cordova.clipboard/www/clipboard.js @@ -0,0 +1,36 @@ +cordova.define("com.verso.cordova.clipboard.Clipboard", function(require, exports, module) { var cordova = require('cordova'); + +/** + * Clipboard plugin for Cordova + * + * @constructor + */ +function Clipboard () {} + +/** + * Sets the clipboard content + * + * @param {String} text The content to copy to the clipboard + * @param {Function} onSuccess The function to call in case of success (takes the copied text as argument) + * @param {Function} onFail The function to call in case of error + */ +Clipboard.prototype.copy = function (text, onSuccess, onFail) { + if (typeof text === "undefined" || text === null) text = ""; + cordova.exec(onSuccess, onFail, "Clipboard", "copy", [text]); +}; + +/** + * Gets the clipboard content + * + * @param {Function} onSuccess The function to call in case of success + * @param {Function} onFail The function to call in case of error + */ +Clipboard.prototype.paste = function (onSuccess, onFail) { + cordova.exec(onSuccess, onFail, "Clipboard", "paste", []); +}; + +// Register the plugin +var clipboard = new Clipboard(); +module.exports = clipboard; + +}); diff --git a/mobile/res/xml/config.xml b/mobile/res/xml/config.xml index 86c57a5ca..871fce104 100644 --- a/mobile/res/xml/config.xml +++ b/mobile/res/xml/config.xml @@ -28,4 +28,7 @@ + + + diff --git a/mobile/src/com/verso/cordova/clipboard/Clipboard.java b/mobile/src/com/verso/cordova/clipboard/Clipboard.java new file mode 100644 index 000000000..438ebc1ea --- /dev/null +++ b/mobile/src/com/verso/cordova/clipboard/Clipboard.java @@ -0,0 +1,62 @@ +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; + } +} + +