Skip to content

Commit 682744e

Browse files
osaatciogluJesse
authored andcommitted
corrected the context usage
1 parent 87308b6 commit 682744e

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

Android/ClipboardManager/ClipboardManagerPlugin.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Omer Saatcioglu 2011
44
*
55
*/
6-
6+
77
package com.saatcioglu.phonegap.clipboardmanager;
88

99
import org.json.JSONArray;
@@ -12,14 +12,23 @@
1212
import android.content.Context;
1313
import android.text.ClipboardManager;
1414

15+
import com.phonegap.DroidGap;
1516
import com.phonegap.api.Plugin;
1617
import com.phonegap.api.PluginResult;
1718

1819
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";
20+
private static final String actionCopy = "copy";
21+
private static final String actionPaste = "paste";
22+
private static final String errorParse = "Couldn't get the text to copy";
23+
private static final String errorUnknown = "Unknown Error";
24+
25+
private ClipboardManager mClipboardManager;
26+
27+
public void setContext(DroidGap ctx) {
28+
super.setContext(ctx);
29+
mClipboardManager = (ClipboardManager) ctx
30+
.getSystemService(Context.CLIPBOARD_SERVICE);
31+
}
2332

2433
/**
2534
* Executes the request and returns PluginResult.
@@ -37,21 +46,15 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {
3746
String arg = "";
3847
try {
3948
arg = (String) args.get(0);
40-
ClipboardManager clipboard = (ClipboardManager) ContextHolder
41-
.get().getSystemService(Context.CLIPBOARD_SERVICE);
42-
clipboard.setText(arg);
49+
mClipboardManager.setText(arg);
4350
} catch (JSONException e) {
44-
return new PluginResult(PluginResult.Status.ERROR,
45-
errorParse);
51+
return new PluginResult(PluginResult.Status.ERROR, errorParse);
4652
} catch (Exception e) {
47-
return new PluginResult(PluginResult.Status.ERROR,
48-
errorUnknown);
53+
return new PluginResult(PluginResult.Status.ERROR, errorUnknown);
4954
}
5055
return new PluginResult(PluginResult.Status.OK, arg);
5156
} else if (action.equals(actionPaste)) {
52-
ClipboardManager clipboard = (ClipboardManager) ContextHolder.get()
53-
.getSystemService(Context.CLIPBOARD_SERVICE);
54-
String arg = (String) clipboard.getText();
57+
String arg = (String) mClipboardManager.getText();
5558
if (arg == null) {
5659
arg = "";
5760
}

Android/ClipboardManager/README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ By Omer Saatcioglu
44
## Adding the Plugin to your project ##
55
1. To install the plugin, move clipboardmanager.js to your project's www folder and include a reference to it in your html files.
66
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.
7+
3. And copy ClipboardManagerPlugin.java into that new folder.
88

99
`mkdir <your_project>/src/com/saatcioglu/phonegap/clipboardmanager`
1010
`cp ./ClipboardManagerPlugin.java <your_project>/src/com/beetight/barcodescanner`
11-
`cp ./ContextHolder.java <your_project>/src/com/beetight/barcodescanner`
1211

1312
## 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-
1913
The plugin creates the object `window.plugins.clipboardManager` with the methods
2014

2115
`copy(str, success, fail)` that copies the given string

0 commit comments

Comments
 (0)