mirror of
https://github.com/apache/cordova-android.git
synced 2026-04-23 00:00:09 +08:00
Add callbackId to Plugin.execute() so result can be sent back when overlapping calls to same plugin occur.
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user