mirror of
https://github.com/apache/cordova-android.git
synced 2026-05-11 00:00:05 +08:00
Enable JS callbacks to be kept around for multiple callbacks from Java.
This commit is contained in:
@@ -88,19 +88,16 @@ public final class PluginManager {
|
||||
// Call execute on the plugin so that it can do it's thing
|
||||
PluginResult cr = plugin.execute(action, args, callbackId);
|
||||
int status = cr.getStatus();
|
||||
|
||||
// Check the success (OK, NEXT_RESULT, NO_MORE_RESULTS)
|
||||
if ((status == PluginResult.Status.OK.ordinal()) ||
|
||||
(status == PluginResult.Status.NEXT_RESULT.ordinal()) ||
|
||||
(status == PluginResult.Status.NO_MORE_RESULTS.ordinal())
|
||||
) {
|
||||
|
||||
// If no result to be sent and keeping callback, then no need to sent back to JavaScript
|
||||
if ((status == PluginResult.Status.NO_RESULT.ordinal()) && cr.getKeepCallback()) {
|
||||
}
|
||||
|
||||
// Check the success (OK, NO_RESULT & !KEEP_CALLBACK)
|
||||
else if ((status == PluginResult.Status.OK.ordinal()) || (status == PluginResult.Status.NO_RESULT.ordinal())) {
|
||||
ctx.sendJavascript(cr.toSuccessCallbackString(callbackId));
|
||||
}
|
||||
|
||||
// If return result will be sent later, no need to sent back to JavaScript
|
||||
else if (status == PluginResult.Status.RESULT_TO_BE_SENT.ordinal()) {
|
||||
}
|
||||
|
||||
// If error
|
||||
else {
|
||||
ctx.sendJavascript(cr.toErrorCallbackString(callbackId));
|
||||
@@ -117,8 +114,8 @@ public final class PluginManager {
|
||||
// Call execute on the plugin so that it can do it's thing
|
||||
cr = plugin.execute(action, args, callbackId);
|
||||
|
||||
// If return result will be sent later
|
||||
if (cr.getStatus() == PluginResult.Status.RESULT_TO_BE_SENT.ordinal()) {
|
||||
// If no result to be sent and keeping callback, then no need to sent back to JavaScript
|
||||
if ((cr.getStatus() == PluginResult.Status.NO_RESULT.ordinal()) && cr.getKeepCallback()) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.json.JSONObject;
|
||||
public class PluginResult {
|
||||
private final int status;
|
||||
private final String message;
|
||||
private boolean keepCallback = false;
|
||||
|
||||
public PluginResult(Status status) {
|
||||
this.status = status.ordinal();
|
||||
@@ -49,6 +50,10 @@ public class PluginResult {
|
||||
this.message = ""+b;
|
||||
}
|
||||
|
||||
public void setKeepCallback(boolean b) {
|
||||
this.keepCallback = b;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
@@ -56,9 +61,13 @@ public class PluginResult {
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
public boolean getKeepCallback() {
|
||||
return this.keepCallback;
|
||||
}
|
||||
|
||||
public String getJSONString() {
|
||||
return "{ status: " + this.getStatus() + ", message: " + this.getMessage() + " }";
|
||||
return "{status:" + this.status + ",message:" + this.message + ",keepCallback:" + this.keepCallback + "}";
|
||||
}
|
||||
|
||||
public String toSuccessCallbackString(String callbackId) {
|
||||
@@ -70,6 +79,7 @@ public class PluginResult {
|
||||
}
|
||||
|
||||
public static String[] StatusMessages = new String[] {
|
||||
"No result",
|
||||
"OK",
|
||||
"Class not found",
|
||||
"Illegal access",
|
||||
@@ -78,13 +88,11 @@ public class PluginResult {
|
||||
"IO error",
|
||||
"Invalid action",
|
||||
"JSON error",
|
||||
"Error",
|
||||
"Result to be sent later",
|
||||
"Next result",
|
||||
"No more results"
|
||||
"Error"
|
||||
};
|
||||
|
||||
public enum Status {
|
||||
NO_RESULT,
|
||||
OK,
|
||||
CLASS_NOT_FOUND_EXCEPTION,
|
||||
ILLEGAL_ACCESS_EXCEPTION,
|
||||
@@ -93,9 +101,6 @@ public class PluginResult {
|
||||
IO_EXCEPTION,
|
||||
INVALID_ACTION,
|
||||
JSON_EXCEPTION,
|
||||
ERROR,
|
||||
RESULT_TO_BE_SENT,
|
||||
NEXT_RESULT,
|
||||
NO_MORE_RESULTS
|
||||
ERROR
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user