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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
if (contactAccessor == null) {
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
//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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
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 args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The action to execute.
* @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.
*/
public PluginResult execute(String action, JSONArray args) {
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";

View File

@ -15,11 +15,12 @@ public interface IPlugin {
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @return A PluginResult object with a status and message.
* @param action The action to execute.
* @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.
*/
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.
@ -45,17 +46,6 @@ public interface IPlugin {
*/
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.
*/

View File

@ -14,16 +14,16 @@ public abstract class Plugin implements IPlugin {
public WebView webView; // WebView object
public DroidGap ctx; // DroidGap object
public String callbackId; // key for the JavaScript callback
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @return A PluginResult object with a status and message.
* @param action The action to execute.
* @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.
*/
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.
@ -55,19 +55,6 @@ public abstract class Plugin implements IPlugin {
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.
*/
@ -115,18 +102,20 @@ public abstract class Plugin implements IPlugin {
* that execute should return null and the callback from the async operation can
* 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) {
this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(this.callbackId));
public void success(PluginResult pluginResult, String callbackId) {
this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(callbackId));
}
/**
* 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) {
this.ctx.callbackServer.sendJavascript(pluginResult.toErrorCallbackString(this.callbackId));
public void error(PluginResult pluginResult, String callbackId) {
this.ctx.callbackServer.sendJavascript(pluginResult.toErrorCallbackString(callbackId));
}
}

View File

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