Skip to content

Commit 2e3d9f3

Browse files
osaatciogluJesse
authored andcommitted
Created ClipboardManager Plugin.
1 parent 72a32e5 commit 2e3d9f3

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Phonegap ClipboardManager plugin
3+
* Omer Saatcioglu 2011
4+
*
5+
*/
6+
7+
package com.saatcioglu.phonegap.clipboardmanager;
8+
9+
import org.json.JSONArray;
10+
import org.json.JSONException;
11+
12+
import android.content.Context;
13+
import android.text.ClipboardManager;
14+
15+
import com.phonegap.api.Plugin;
16+
import com.phonegap.api.PluginResult;
17+
18+
public class ClipboardManagerPlugin extends Plugin {
19+
private static String actionCopy = "copy";
20+
private static String actionPaste = "paste";
21+
private static String errorParse = "Couldn't get the text to copy";
22+
private static String errorUnknown = "Unknown Error";
23+
24+
/**
25+
* Executes the request and returns PluginResult.
26+
*
27+
* @param action
28+
* The action to execute.
29+
* @param args
30+
* JSONArry of arguments for the plugin.
31+
* @param callbackId
32+
* The callback id used when calling back into JavaScript.
33+
* @return A PluginResult object with a status and message.
34+
*/
35+
public PluginResult execute(String action, JSONArray args, String callbackId) {
36+
if (action.equals(actionCopy)) {
37+
String arg = "";
38+
try {
39+
arg = (String) args.get(0);
40+
ClipboardManager clipboard = (ClipboardManager) ContextHolder
41+
.get().getSystemService(Context.CLIPBOARD_SERVICE);
42+
clipboard.setText(arg);
43+
} catch (JSONException e) {
44+
return new PluginResult(PluginResult.Status.ERROR,
45+
errorParse);
46+
} catch (Exception e) {
47+
return new PluginResult(PluginResult.Status.ERROR,
48+
errorUnknown);
49+
}
50+
return new PluginResult(PluginResult.Status.OK, arg);
51+
} else if (action.equals(actionPaste)) {
52+
ClipboardManager clipboard = (ClipboardManager) ContextHolder.get()
53+
.getSystemService(Context.CLIPBOARD_SERVICE);
54+
String arg = (String) clipboard.getText();
55+
if (arg == null) {
56+
arg = "";
57+
}
58+
return new PluginResult(PluginResult.Status.OK, arg);
59+
} else {
60+
return new PluginResult(PluginResult.Status.INVALID_ACTION);
61+
}
62+
}
63+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Phonegap Context Holder
3+
* Omer Saatcioglu 2011
4+
*
5+
*/
6+
7+
package com.saatcioglu.phonegap.clipboardmanager;
8+
9+
import android.content.Context;
10+
11+
/**
12+
* A method proposed to make the context reachable to the PhoneGap plug-ins
13+
* @author Omer Saatcioglu
14+
*
15+
*/
16+
public class ContextHolder{
17+
18+
private static Context mContext;
19+
20+
/**
21+
* @return the context
22+
*/
23+
public static Context get() {
24+
return mContext;
25+
}
26+
27+
/**
28+
* @param context the context
29+
*/
30+
public static void set(Context context) {
31+
mContext = context;
32+
}
33+
34+
}

Android/ClipboardManager/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# ClipboardManager plugin for Phonegap #
2+
By Omer Saatcioglu
3+
4+
## Adding the Plugin to your project ##
5+
1. To install the plugin, move clipboardmanager.js to your project's www folder and include a reference to it in your html files.
6+
2. Create a folder called 'src/com/saatcioglu/phonegap/clipboardmanager' within your project's src folder.
7+
3. And copy ContextHolder.java and ClipboardManagerPlugin.java into that new folder.
8+
9+
`mkdir <your_project>/src/com/saatcioglu/phonegap/clipboardmanager`
10+
`cp ./ClipboardManagerPlugin.java <your_project>/src/com/beetight/barcodescanner`
11+
`cp ./ContextHolder.java <your_project>/src/com/beetight/barcodescanner`
12+
13+
## Using the plugin ##
14+
Before using the plugin, we need to at the following line of code before calling the `loadUrl` method in the main activity to let the
15+
ClipboardManagerPlugin use the context of the main activity. Otherwise, it wouldn't be able to reach to the Clipboard Service
16+
17+
ContextHolder.set(this);
18+
19+
The plugin creates the object `window.plugins.clipboardManager` with the methods
20+
21+
`copy(str, success, fail)` that copies the given string
22+
`paste(success, fail)` that returns the text from the Clipboard
23+
24+
`success` and `fail` are callback functions.
25+
26+
An example for copy:
27+
28+
window.plugins.clipboardManager.copy(
29+
"the text to copy",
30+
function(r){alert("copy is successful")},
31+
function(e){alert(e)}
32+
);
33+
34+
An example for paste:
35+
36+
window.plugins.clipboardManager.paste(
37+
function(r){alert("The text in the clipboard is " + r)},
38+
function(e){alert(e)}
39+
);
40+
41+
42+
## BUGS AND CONTRIBUTIONS ##
43+
The latest version is available [at GitHub](https://github.com/osaatcioglu/phonegap-plugins/tree/master/Android)
44+
If you have a patch, fork my repo baby and send me a pull request. Submit bug reports on GitHub, please.
45+
46+
## Licence ##
47+
The MIT License
48+
49+
Copyright (c) 2010 Matt Kane
50+
51+
Permission is hereby granted, free of charge, to any person obtaining a copy
52+
of this software and associated documentation files (the "Software"), to deal
53+
in the Software without restriction, including without limitation the rights
54+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
55+
copies of the Software, and to permit persons to whom the Software is
56+
furnished to do so, subject to the following conditions:
57+
58+
The above copyright notice and this permission notice shall be included in
59+
all copies or substantial portions of the Software.
60+
61+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
62+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
63+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
64+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
65+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
66+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
67+
THE SOFTWARE.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Phonegap ClipboardManager plugin
3+
* Omer Saatcioglu 2011
4+
*
5+
*/
6+
7+
var ClipboardManager = function() {
8+
}
9+
10+
ClipboardManager.prototype.copy = function(str, success, fail) {
11+
PhoneGap.execAsync(success, fail, "ClipboardManager", "copy", [str,]);
12+
};
13+
14+
ClipboardManager.prototype.paste = function(success, fail) {
15+
PhoneGap.execAsync(success, fail, "ClipboardManager", "paste", []);
16+
};
17+
18+
PhoneGap.addConstructor(function() {
19+
PhoneGap.addPlugin('clipboardManager', new ClipboardManager());
20+
PluginManager.addService("ClipboardManager","com.saatcioglu.phonegap.clipboardmanager.ClipboardManagerPlugin");
21+
});

0 commit comments

Comments
 (0)