Add callbackId to Plugin.execute() so result can be sent back when overlapping calls to same plugin occur.

This commit is contained in:
Bryce Curtis 2010-10-05 20:35:51 -05:00
parent 23b02e7267
commit 9adb85a64b
16 changed files with 111 additions and 125 deletions

View File

@ -59,13 +59,14 @@ public class AccelListener extends Plugin implements SensorEventListener {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";

View File

@ -35,13 +35,14 @@ public class AudioHandler extends Plugin {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";

View File

@ -46,13 +46,14 @@ public class CameraLauncher extends Plugin {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";

View File

@ -54,13 +54,14 @@ public class CompassListener extends Plugin implements SensorEventListener {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";

View File

@ -19,13 +19,14 @@ public class ContactManager extends Plugin {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
if (contactAccessor == null) { if (contactAccessor == null) {
contactAccessor = ContactAccessor.getInstance(webView, ctx); contactAccessor = ContactAccessor.getInstance(webView, ctx);
} }

View File

@ -15,13 +15,14 @@ public class CryptoHandler extends Plugin {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";

View File

@ -56,13 +56,14 @@ public class Device extends Plugin {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";

View File

@ -33,13 +33,14 @@ public class FileUtils extends Plugin {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
//System.out.println("FileUtils.execute("+action+")"); //System.out.println("FileUtils.execute("+action+")");

View File

@ -29,13 +29,14 @@ public class GeoBroker extends Plugin {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";

View File

@ -37,13 +37,14 @@ public class NetworkManager extends Plugin {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
try { try {

View File

@ -22,13 +22,14 @@ public class Notification extends Plugin {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";

11
framework/src/com/phonegap/Storage.java Normal file → Executable file
View File

@ -25,13 +25,14 @@ public class Storage extends Plugin {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";

View File

@ -36,13 +36,14 @@ public class TempListener extends Plugin implements SensorEventListener {
} }
/** /**
* Executes the request and returns CommandResult. * Executes the request and returns PluginResult.
* *
* @param action The command to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";

View File

@ -17,9 +17,10 @@ public interface IPlugin {
* *
* @param action The action to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the plugin. * @param args JSONArry of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message. * @return A PluginResult object with a status and message.
*/ */
PluginResult execute(String action, JSONArray args); PluginResult execute(String action, JSONArray args, String callbackId);
/** /**
* Identifies if action to be executed returns a value and should be run synchronously. * Identifies if action to be executed returns a value and should be run synchronously.
@ -45,17 +46,6 @@ public interface IPlugin {
*/ */
void setView(WebView webView); void setView(WebView webView);
/**
* Sets the callback ID that is required to call a success or error
* JavaScript callback.
*
* The JavaScript callback call looks like this:
* PhoneGap.callbackSuccess(callbackId, { message: 'foo' });
*
* @param callbackId
*/
void setCallbackId(String callbackId);
/** /**
* Called when the system is about to start resuming a previous activity. * Called when the system is about to start resuming a previous activity.
*/ */

View File

@ -14,16 +14,16 @@ public abstract class Plugin implements IPlugin {
public WebView webView; // WebView object public WebView webView; // WebView object
public DroidGap ctx; // DroidGap object public DroidGap ctx; // DroidGap object
public String callbackId; // key for the JavaScript callback
/** /**
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
* *
* @param action The action to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the plugin. * @param args JSONArry of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message. * @return A PluginResult object with a status and message.
*/ */
public abstract PluginResult execute(String action, JSONArray args); public abstract PluginResult execute(String action, JSONArray args, String callbackId);
/** /**
* Identifies if action to be executed returns a value and should be run synchronously. * Identifies if action to be executed returns a value and should be run synchronously.
@ -55,19 +55,6 @@ public abstract class Plugin implements IPlugin {
this.webView = webView; this.webView = webView;
} }
/**
* Sets the callback ID that is required to call a success or error
* JavaScript callback.
*
* The JavaScript callback call looks like this:
* PhoneGap.callbackSuccess(callbackId, { message: 'foo' });
*
* @param callbackId
*/
public void setCallbackId(String callbackId) {
this.callbackId = callbackId;
}
/** /**
* Called when the system is about to start resuming a previous activity. * Called when the system is about to start resuming a previous activity.
*/ */
@ -115,18 +102,20 @@ public abstract class Plugin implements IPlugin {
* that execute should return null and the callback from the async operation can * that execute should return null and the callback from the async operation can
* call success(...) or error(...) * call success(...) or error(...)
* *
* @param pluginResult * @param pluginResult The result to return.
* @param callbackId The callback id used when calling back into JavaScript.
*/ */
public void success(PluginResult pluginResult) { public void success(PluginResult pluginResult, String callbackId) {
this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(this.callbackId)); this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(callbackId));
} }
/** /**
* Call the JavaScript error callback for this plugin. * Call the JavaScript error callback for this plugin.
* *
* @param pluginResult * @param pluginResult The result to return.
* @param callbackId The callback id used when calling back into JavaScript.
*/ */
public void error(PluginResult pluginResult) { public void error(PluginResult pluginResult, String callbackId) {
this.ctx.callbackServer.sendJavascript(pluginResult.toErrorCallbackString(this.callbackId)); this.ctx.callbackServer.sendJavascript(pluginResult.toErrorCallbackString(callbackId));
} }
} }

View File

@ -70,28 +70,33 @@ public final class PluginManager {
c = getClassByName(clazz); c = getClassByName(clazz);
} }
if (isPhoneGapPlugin(c)) { if (isPhoneGapPlugin(c)) {
final Plugin plugin = this.addPlugin(clazz, c, callbackId); final Plugin plugin = this.addPlugin(clazz, c);
final DroidGap ctx = this.ctx; final DroidGap ctx = this.ctx;
runAsync = async && !plugin.isSynch(action); runAsync = async && !plugin.isSynch(action);
if (runAsync) { if (runAsync) {
// Run this on a different thread so that this one can return back to JS // Run this on a different thread so that this one can return back to JS
Thread thread = new Thread(new Runnable() { Thread thread = new Thread(new Runnable() {
public void run() { public void run() {
try {
// Call execute on the plugin so that it can do it's thing // Call execute on the plugin so that it can do it's thing
PluginResult cr = plugin.execute(action, args); PluginResult cr = plugin.execute(action, args, callbackId);
// Check the status for 0 (success) or otherwise // Check the status for 0 (success) or otherwise
if (cr.getStatus() == 0) { if (cr.getStatus() == 0) {
ctx.sendJavascript(cr.toSuccessCallbackString(callbackId)); ctx.sendJavascript(cr.toSuccessCallbackString(callbackId));
} else { } else {
ctx.sendJavascript(cr.toErrorCallbackString(callbackId)); ctx.sendJavascript(cr.toErrorCallbackString(callbackId));
} }
} catch (Exception e) {
PluginResult cr = new PluginResult(PluginResult.Status.ERROR);
ctx.sendJavascript(cr.toErrorCallbackString(callbackId));
}
} }
}); });
thread.start(); thread.start();
return ""; return "";
} else { } else {
// Call execute on the plugin so that it can do it's thing // Call execute on the plugin so that it can do it's thing
cr = plugin.execute(action, args); cr = plugin.execute(action, args, callbackId);
} }
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@ -157,7 +162,7 @@ public final class PluginManager {
*/ */
public Plugin addPlugin(String className) { public Plugin addPlugin(String className) {
try { try {
return this.addPlugin(className, this.getClassByName(className), ""); return this.addPlugin(className, this.getClassByName(className));
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
System.out.println("Error adding plugin "+className+"."); System.out.println("Error adding plugin "+className+".");
@ -175,7 +180,7 @@ public final class PluginManager {
* @return The plugin * @return The plugin
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Plugin addPlugin(String className, Class clazz, String callbackId) { private Plugin addPlugin(String className, Class clazz) {
if (this.plugins.containsKey(className)) { if (this.plugins.containsKey(className)) {
return this.getPlugin(className); return this.getPlugin(className);
} }
@ -185,7 +190,6 @@ public final class PluginManager {
this.plugins.put(className, plugin); this.plugins.put(className, plugin);
plugin.setContext((DroidGap)this.ctx); plugin.setContext((DroidGap)this.ctx);
plugin.setView(this.app); plugin.setView(this.app);
plugin.setCallbackId(callbackId);
return plugin; return plugin;
} }
catch (Exception e) { catch (Exception e) {
@ -217,16 +221,6 @@ public final class PluginManager {
this.services.put(serviceType, className); this.services.put(serviceType, className);
} }
/**
* Get the class that implements a service.
*
* @param serviceType
* @return
*/
//private String getClassForService(String serviceType) {
// return this.services.get(serviceType);
//}
/** /**
* Called when the system is about to start resuming a previous activity. * Called when the system is about to start resuming a previous activity.
*/ */