mirror of
https://github.com/purplecabbage/phonegap-plugins.git
synced 2026-04-24 03:00:11 -04:00
corrected the context usage
This commit is contained in:
@@ -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 = "";
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user