Enable JS callbacks to be kept around for multiple callbacks from Java.

This commit is contained in:
Bryce Curtis 2010-10-27 07:16:13 +08:00
parent bc1e039ea1
commit 37a9307681
4 changed files with 64 additions and 43 deletions

View File

@ -383,18 +383,16 @@ PhoneGap.clone = function(obj) {
PhoneGap.callbackId = 0; PhoneGap.callbackId = 0;
PhoneGap.callbacks = {}; PhoneGap.callbacks = {};
PhoneGap.callbackStatus = { PhoneGap.callbackStatus = {
OK: 0, NO_RESULT: 0,
CLASS_NOT_FOUND_EXCEPTION: 1, OK: 1,
ILLEGAL_ACCESS_EXCEPTION: 2, CLASS_NOT_FOUND_EXCEPTION: 2,
INSTANTIATION_EXCEPTION: 3, ILLEGAL_ACCESS_EXCEPTION: 3,
MALFORMED_URL_EXCEPTION: 4, INSTANTIATION_EXCEPTION: 4,
IO_EXCEPTION: 5, MALFORMED_URL_EXCEPTION: 5,
INVALID_ACTION: 6, IO_EXCEPTION: 6,
JSON_EXCEPTION: 7, INVALID_ACTION: 7,
ERROR: 8, JSON_EXCEPTION: 8,
RESULT_TO_BE_SENT: 9, ERROR: 9
NEXT_RESULT: 10,
NO_MORE_RESULTS: 11
}; };
@ -427,7 +425,7 @@ PhoneGap.exec = function(success, fail, service, action, args) {
eval("var v="+r+";"); eval("var v="+r+";");
// If status is OK, then return value back to caller // If status is OK, then return value back to caller
if ((v.status == PhoneGap.callbackStatus.OK) || (v.status == PhoneGap.callbackStatus.NEXT_RESULT)) { if (v.status == PhoneGap.callbackStatus.OK) {
// If there is a success callback, then call it now with returned value // If there is a success callback, then call it now with returned value
if (success) { if (success) {
@ -439,15 +437,24 @@ PhoneGap.exec = function(success, fail, service, action, args) {
} }
// Clear callback if not expecting any more results // Clear callback if not expecting any more results
if ((v.status == PhoneGap.callbackStatus.OK) || (v.status == PhoneGap.callbackStatus.NO_MORE_RESULTS)) { if (!v.keepCallback) {
delete PhoneGap.callbacks[callbackId]; delete PhoneGap.callbacks[callbackId];
} }
} }
return v.message; return v.message;
} }
// If no result
else if (v.status == PhoneGap.callbackStatus.NO_RESULT) {
// Clear callback if not expecting any more results
if (!v.keepCallback) {
delete PhoneGap.callbacks[callbackId];
}
}
// If error, then display error // If error, then display error
else if (v.status != PhoneGap.callbackStatus.RESULT_TO_BE_SENT) { else {
console.log("Error: Status="+r.status+" Message="+v.message); console.log("Error: Status="+r.status+" Message="+v.message);
// If there is a fail callback, then call it now with returned value // If there is a fail callback, then call it now with returned value
@ -458,7 +465,11 @@ PhoneGap.exec = function(success, fail, service, action, args) {
catch (e) { catch (e) {
console.log("Error in error callback: "+callbackId+" = "+e); console.log("Error in error callback: "+callbackId+" = "+e);
} }
delete PhoneGap.callbacks[callbackId];
// Clear callback if not expecting any more results
if (!v.keepCallback) {
delete PhoneGap.callbacks[callbackId];
}
} }
return null; return null;
} }
@ -478,7 +489,7 @@ PhoneGap.callbackSuccess = function(callbackId, args) {
if (PhoneGap.callbacks[callbackId]) { if (PhoneGap.callbacks[callbackId]) {
// If result is to be sent to callback // If result is to be sent to callback
if ((args.status == PhoneGap.callbackStatus.OK) || (args.status == PhoneGap.callbackStatus.NEXT_RESULT)) { if (args.status == PhoneGap.callbackStatus.OK) {
try { try {
if (PhoneGap.callbacks[callbackId].success) { if (PhoneGap.callbacks[callbackId].success) {
PhoneGap.callbacks[callbackId].success(args.message); PhoneGap.callbacks[callbackId].success(args.message);
@ -490,7 +501,7 @@ PhoneGap.callbackSuccess = function(callbackId, args) {
} }
// Clear callback if not expecting any more results // Clear callback if not expecting any more results
if ((args.status == PhoneGap.callbackStatus.OK) || (args.status == PhoneGap.callbackStatus.NO_MORE_RESULTS)) { if (!args.keepCallback) {
delete PhoneGap.callbacks[callbackId]; delete PhoneGap.callbacks[callbackId];
} }
} }
@ -512,7 +523,11 @@ PhoneGap.callbackError = function(callbackId, args) {
catch (e) { catch (e) {
console.log("Error in error callback: "+callbackId+" = "+e); console.log("Error in error callback: "+callbackId+" = "+e);
} }
delete PhoneGap.callbacks[callbackId];
// Clear callback if not expecting any more results
if (!args.keepCallback) {
delete PhoneGap.callbacks[callbackId];
}
} }
}; };

View File

@ -56,11 +56,15 @@ public class Notification extends Plugin {
} }
else if (action.equals("alert")) { else if (action.equals("alert")) {
this.alert(args.getString(0),args.getString(1),args.getString(2), callbackId); this.alert(args.getString(0),args.getString(1),args.getString(2), callbackId);
return new PluginResult(PluginResult.Status.RESULT_TO_BE_SENT); PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
return r;
} }
else if (action.equals("confirm")) { else if (action.equals("confirm")) {
this.confirm(args.getString(0),args.getString(1),args.getString(2), callbackId); this.confirm(args.getString(0),args.getString(1),args.getString(2), callbackId);
return new PluginResult(PluginResult.Status.RESULT_TO_BE_SENT); PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
return r;
} }
else if (action.equals("activityStart")) { else if (action.equals("activityStart")) {
this.activityStart(args.getString(0),args.getString(1)); this.activityStart(args.getString(0),args.getString(1));

View File

@ -89,16 +89,13 @@ public final class PluginManager {
PluginResult cr = plugin.execute(action, args, callbackId); PluginResult cr = plugin.execute(action, args, callbackId);
int status = cr.getStatus(); int status = cr.getStatus();
// Check the success (OK, NEXT_RESULT, NO_MORE_RESULTS) // If no result to be sent and keeping callback, then no need to sent back to JavaScript
if ((status == PluginResult.Status.OK.ordinal()) || if ((status == PluginResult.Status.NO_RESULT.ordinal()) && cr.getKeepCallback()) {
(status == PluginResult.Status.NEXT_RESULT.ordinal()) ||
(status == PluginResult.Status.NO_MORE_RESULTS.ordinal())
) {
ctx.sendJavascript(cr.toSuccessCallbackString(callbackId));
} }
// If return result will be sent later, no need to sent back to JavaScript // Check the success (OK, NO_RESULT & !KEEP_CALLBACK)
else if (status == PluginResult.Status.RESULT_TO_BE_SENT.ordinal()) { else if ((status == PluginResult.Status.OK.ordinal()) || (status == PluginResult.Status.NO_RESULT.ordinal())) {
ctx.sendJavascript(cr.toSuccessCallbackString(callbackId));
} }
// If error // If error
@ -117,8 +114,8 @@ public final class PluginManager {
// 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, callbackId); cr = plugin.execute(action, args, callbackId);
// If return result will be sent later // If no result to be sent and keeping callback, then no need to sent back to JavaScript
if (cr.getStatus() == PluginResult.Status.RESULT_TO_BE_SENT.ordinal()) { if ((cr.getStatus() == PluginResult.Status.NO_RESULT.ordinal()) && cr.getKeepCallback()) {
return ""; return "";
} }
} }

View File

@ -13,6 +13,7 @@ import org.json.JSONObject;
public class PluginResult { public class PluginResult {
private final int status; private final int status;
private final String message; private final String message;
private boolean keepCallback = false;
public PluginResult(Status status) { public PluginResult(Status status) {
this.status = status.ordinal(); this.status = status.ordinal();
@ -49,6 +50,10 @@ public class PluginResult {
this.message = ""+b; this.message = ""+b;
} }
public void setKeepCallback(boolean b) {
this.keepCallback = b;
}
public int getStatus() { public int getStatus() {
return status; return status;
} }
@ -57,8 +62,12 @@ public class PluginResult {
return message; return message;
} }
public boolean getKeepCallback() {
return this.keepCallback;
}
public String getJSONString() { 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) { public String toSuccessCallbackString(String callbackId) {
@ -70,6 +79,7 @@ public class PluginResult {
} }
public static String[] StatusMessages = new String[] { public static String[] StatusMessages = new String[] {
"No result",
"OK", "OK",
"Class not found", "Class not found",
"Illegal access", "Illegal access",
@ -78,13 +88,11 @@ public class PluginResult {
"IO error", "IO error",
"Invalid action", "Invalid action",
"JSON error", "JSON error",
"Error", "Error"
"Result to be sent later",
"Next result",
"No more results"
}; };
public enum Status { public enum Status {
NO_RESULT,
OK, OK,
CLASS_NOT_FOUND_EXCEPTION, CLASS_NOT_FOUND_EXCEPTION,
ILLEGAL_ACCESS_EXCEPTION, ILLEGAL_ACCESS_EXCEPTION,
@ -93,9 +101,6 @@ public class PluginResult {
IO_EXCEPTION, IO_EXCEPTION,
INVALID_ACTION, INVALID_ACTION,
JSON_EXCEPTION, JSON_EXCEPTION,
ERROR, ERROR
RESULT_TO_BE_SENT,
NEXT_RESULT,
NO_MORE_RESULTS
} }
} }