Add clipboard plugin
This commit is contained in:
parent
fe53f1b87c
commit
66482cd9e2
4 changed files with 110 additions and 1 deletions
62
mobile/src/com/verso/cordova/clipboard/Clipboard.java
Normal file
62
mobile/src/com/verso/cordova/clipboard/Clipboard.java
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue