Permalink
Browse files

corrected the context usage

  • Loading branch information...
osaatcioglu authored and Jesse committed Mar 1, 2011
1 parent 87308b6 commit 682744e7d3b5f7dd15a8b6b425b88d3e7cd3340b
Showing with 19 additions and 22 deletions.
  1. +18 −15 Android/ClipboardManager/ClipboardManagerPlugin.java
  2. +1 −7 Android/ClipboardManager/README.md
@@ -3,7 +3,7 @@
* Omer Saatcioglu 2011
*
*/
-
+
package com.saatcioglu.phonegap.clipboardmanager;
import org.json.JSONArray;
@@ -12,14 +12,23 @@
import android.content.Context;
import android.text.ClipboardManager;
+import com.phonegap.DroidGap;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
public class ClipboardManagerPlugin extends Plugin {
- private static String actionCopy = "copy";
- private static String actionPaste = "paste";
- private static String errorParse = "Couldn't get the text to copy";
- private static String errorUnknown = "Unknown Error";
+ private static final String actionCopy = "copy";
+ private static final String actionPaste = "paste";
+ private static final String errorParse = "Couldn't get the text to copy";
+ private static final String errorUnknown = "Unknown Error";
+
+ private ClipboardManager mClipboardManager;
+
+ public void setContext(DroidGap ctx) {
+ super.setContext(ctx);
+ mClipboardManager = (ClipboardManager) ctx
+ .getSystemService(Context.CLIPBOARD_SERVICE);
+ }
/**
* Executes the request and returns PluginResult.
@@ -37,21 +46,15 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {
String arg = "";
try {
arg = (String) args.get(0);
- ClipboardManager clipboard = (ClipboardManager) ContextHolder
- .get().getSystemService(Context.CLIPBOARD_SERVICE);
- clipboard.setText(arg);
+ mClipboardManager.setText(arg);
} catch (JSONException e) {
- return new PluginResult(PluginResult.Status.ERROR,
- errorParse);
+ return new PluginResult(PluginResult.Status.ERROR, errorParse);
} catch (Exception e) {
- return new PluginResult(PluginResult.Status.ERROR,
- errorUnknown);
+ return new PluginResult(PluginResult.Status.ERROR, errorUnknown);
}
return new PluginResult(PluginResult.Status.OK, arg);
} else if (action.equals(actionPaste)) {
- ClipboardManager clipboard = (ClipboardManager) ContextHolder.get()
- .getSystemService(Context.CLIPBOARD_SERVICE);
- String arg = (String) clipboard.getText();
+ String arg = (String) mClipboardManager.getText();
if (arg == null) {
arg = "";
}
@@ -4,18 +4,12 @@ By Omer Saatcioglu
## Adding the Plugin to your project ##
1. To install the plugin, move clipboardmanager.js to your project's www folder and include a reference to it in your html files.
2. Create a folder called 'src/com/saatcioglu/phonegap/clipboardmanager' within your project's src folder.
-3. And copy ContextHolder.java and ClipboardManagerPlugin.java into that new folder.
+3. And copy ClipboardManagerPlugin.java into that new folder.
`mkdir <your_project>/src/com/saatcioglu/phonegap/clipboardmanager`
`cp ./ClipboardManagerPlugin.java <your_project>/src/com/beetight/barcodescanner`
-`cp ./ContextHolder.java <your_project>/src/com/beetight/barcodescanner`
## Using the plugin ##
-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
-ClipboardManagerPlugin use the context of the main activity. Otherwise, it wouldn't be able to reach to the Clipboard Service
-
- ContextHolder.set(this);
-
The plugin creates the object `window.plugins.clipboardManager` with the methods
`copy(str, success, fail)` that copies the given string

0 comments on commit 682744e

Please sign in to comment.