corrected the context usage

This commit is contained in:
Omer Saatcioglu (eomesaa)
2011-03-01 13:25:39 +08:00
parent 26f87879a4
commit 349cb064d6
2 changed files with 19 additions and 22 deletions

View File

@@ -3,7 +3,7 @@
* Omer Saatcioglu 2011
*
*/
package com.saatcioglu.phonegap.clipboardmanager;
import org.json.JSONArray;
@@ -12,14 +12,23 @@ import org.json.JSONException;
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 class ClipboardManagerPlugin extends Plugin {
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 = "";
}

View File

@@ -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