Add PluginResult status values to handle RESULT_TO_BE_SENT, NEXT_RESULT, NO_MORE_RESULTS .

This commit is contained in:
Bryce Curtis 2010-10-25 14:33:48 -05:00
parent 8663ed412f
commit 5647e54399
3 changed files with 81 additions and 20 deletions

View File

@ -375,6 +375,21 @@ PhoneGap.clone = function(obj) {
PhoneGap.callbackId = 0;
PhoneGap.callbacks = {};
PhoneGap.callbackStatus = {
OK: 0,
CLASS_NOT_FOUND_EXCEPTION: 1,
ILLEGAL_ACCESS_EXCEPTION: 2,
INSTANTIATION_EXCEPTION: 3,
MALFORMED_URL_EXCEPTION: 4,
IO_EXCEPTION: 5,
INVALID_ACTION: 6,
JSON_EXCEPTION: 7,
ERROR: 8,
RESULT_TO_BE_SENT: 9,
NEXT_RESULT: 10,
NO_MORE_RESULTS: 11
};
/**
* Execute a PhoneGap command. It is up to the native side whether this action is synch or async.
@ -405,23 +420,37 @@ PhoneGap.exec = function(success, fail, service, action, args) {
eval("var v="+r+";");
// If status is OK, then return value back to caller
if (v.status == 0) {
if ((v.status == PhoneGap.callbackStatus.OK) || (v.status == PhoneGap.callbackStatus.NEXT_RESULT)) {
// If there is a success callback, then call it now with returned value
if (success) {
success(v.message);
delete PhoneGap.callbacks[callbackId];
try {
success(v.message);
}
catch (e) {
console.log("Error in success callback: "+callbackId+" = "+e);
}
// Clear callback if not expecting any more results
if ((v.status == PhoneGap.callbackStatus.OK) || (v.status == PhoneGap.callbackStatus.NO_MORE_RESULTS)) {
delete PhoneGap.callbacks[callbackId];
}
}
return v.message;
}
// If error, then display error
else {
else if (v.status != PhoneGap.callbackStatus.RESULT_TO_BE_SENT) {
console.log("Error: Status="+r.status+" Message="+v.message);
// If there is a fail callback, then call it now with returned value
if (fail) {
fail(v.message);
try {
fail(v.message);
}
catch (e) {
console.log("Error in error callback: "+callbackId+" = "+e);
}
delete PhoneGap.callbacks[callbackId];
}
return null;
@ -440,15 +469,23 @@ PhoneGap.exec = function(success, fail, service, action, args) {
*/
PhoneGap.callbackSuccess = function(callbackId, args) {
if (PhoneGap.callbacks[callbackId]) {
try {
if (PhoneGap.callbacks[callbackId].success) {
PhoneGap.callbacks[callbackId].success(args.message);
// If result is to be sent to callback
if ((args.status == PhoneGap.callbackStatus.OK) || (args.status == PhoneGap.callbackStatus.NEXT_RESULT)) {
try {
if (PhoneGap.callbacks[callbackId].success) {
PhoneGap.callbacks[callbackId].success(args.message);
}
}
catch (e) {
console.log("Error in success callback: "+callbackId+" = "+e);
}
}
catch (e) {
console.log("Error in success callback: "+callbackId+" = "+e);
// Clear callback if not expecting any more results
if ((args.status == PhoneGap.callbackStatus.OK) || (args.status == PhoneGap.callbackStatus.NO_MORE_RESULTS)) {
delete PhoneGap.callbacks[callbackId];
}
delete PhoneGap.callbacks[callbackId];
}
};

View File

@ -87,10 +87,22 @@ public final class PluginManager {
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) {
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())
) {
ctx.sendJavascript(cr.toSuccessCallbackString(callbackId));
} else {
}
// 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));
}
} catch (Exception e) {
@ -104,6 +116,11 @@ public final class PluginManager {
} else {
// 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()) {
return "";
}
}
}
} catch (ClassNotFoundException e) {

View File

@ -33,16 +33,17 @@ public class PluginResult {
this.status = status.ordinal();
this.message = message.toString();
}
// TODO: BC: Added
public PluginResult(Status status, int i) {
this.status = status.ordinal();
this.message = ""+i;
}
public PluginResult(Status status, float f) {
this.status = status.ordinal();
this.message = ""+f;
}
public PluginResult(Status status, boolean b) {
this.status = status.ordinal();
this.message = ""+b;
@ -55,9 +56,9 @@ public class PluginResult {
public String getMessage() {
return message;
}
public String getJSONString() {
return "{ status: " + this.getStatus() + ", message: " + this.getMessage() + " }";
return "{ status: " + this.getStatus() + ", message: '" + this.getMessage() + "' }";
}
public String toSuccessCallbackString(String callbackId) {
@ -77,7 +78,10 @@ public class PluginResult {
"IO error",
"Invalid action",
"JSON error",
"Error"
"Error",
"Result to be sent later",
"Next result",
"No more results"
};
public enum Status {
@ -89,6 +93,9 @@ public class PluginResult {
IO_EXCEPTION,
INVALID_ACTION,
JSON_EXCEPTION,
ERROR
ERROR,
RESULT_TO_BE_SENT,
NEXT_RESULT,
NO_MORE_RESULTS
}
}