mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 12:03:28 +08:00
Add PluginResult status values to handle RESULT_TO_BE_SENT, NEXT_RESULT, NO_MORE_RESULTS .
This commit is contained in:
parent
8663ed412f
commit
5647e54399
@ -375,6 +375,21 @@ PhoneGap.clone = function(obj) {
|
|||||||
|
|
||||||
PhoneGap.callbackId = 0;
|
PhoneGap.callbackId = 0;
|
||||||
PhoneGap.callbacks = {};
|
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.
|
* 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+";");
|
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 == 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 there is a success callback, then call it now with returned value
|
||||||
if (success) {
|
if (success) {
|
||||||
|
try {
|
||||||
success(v.message);
|
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];
|
delete PhoneGap.callbacks[callbackId];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return v.message;
|
return v.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If error, then display error
|
// 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);
|
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
|
||||||
if (fail) {
|
if (fail) {
|
||||||
|
try {
|
||||||
fail(v.message);
|
fail(v.message);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.log("Error in error callback: "+callbackId+" = "+e);
|
||||||
|
}
|
||||||
delete PhoneGap.callbacks[callbackId];
|
delete PhoneGap.callbacks[callbackId];
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -440,6 +469,9 @@ PhoneGap.exec = function(success, fail, service, action, args) {
|
|||||||
*/
|
*/
|
||||||
PhoneGap.callbackSuccess = function(callbackId, args) {
|
PhoneGap.callbackSuccess = function(callbackId, args) {
|
||||||
if (PhoneGap.callbacks[callbackId]) {
|
if (PhoneGap.callbacks[callbackId]) {
|
||||||
|
|
||||||
|
// If result is to be sent to callback
|
||||||
|
if ((args.status == PhoneGap.callbackStatus.OK) || (args.status == PhoneGap.callbackStatus.NEXT_RESULT)) {
|
||||||
try {
|
try {
|
||||||
if (PhoneGap.callbacks[callbackId].success) {
|
if (PhoneGap.callbacks[callbackId].success) {
|
||||||
PhoneGap.callbacks[callbackId].success(args.message);
|
PhoneGap.callbacks[callbackId].success(args.message);
|
||||||
@ -448,8 +480,13 @@ PhoneGap.callbackSuccess = function(callbackId, args) {
|
|||||||
catch (e) {
|
catch (e) {
|
||||||
console.log("Error in success callback: "+callbackId+" = "+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];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -87,10 +87,22 @@ public final class PluginManager {
|
|||||||
try {
|
try {
|
||||||
// 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
|
||||||
PluginResult cr = plugin.execute(action, args, callbackId);
|
PluginResult cr = plugin.execute(action, args, callbackId);
|
||||||
// Check the status for 0 (success) or otherwise
|
int status = cr.getStatus();
|
||||||
if (cr.getStatus() == 0) {
|
|
||||||
|
// 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));
|
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));
|
ctx.sendJavascript(cr.toErrorCallbackString(callbackId));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -104,6 +116,11 @@ public final class PluginManager {
|
|||||||
} else {
|
} else {
|
||||||
// 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 (cr.getStatus() == PluginResult.Status.RESULT_TO_BE_SENT.ordinal()) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
|
@ -34,15 +34,16 @@ public class PluginResult {
|
|||||||
this.message = message.toString();
|
this.message = message.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: BC: Added
|
|
||||||
public PluginResult(Status status, int i) {
|
public PluginResult(Status status, int i) {
|
||||||
this.status = status.ordinal();
|
this.status = status.ordinal();
|
||||||
this.message = ""+i;
|
this.message = ""+i;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PluginResult(Status status, float f) {
|
public PluginResult(Status status, float f) {
|
||||||
this.status = status.ordinal();
|
this.status = status.ordinal();
|
||||||
this.message = ""+f;
|
this.message = ""+f;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PluginResult(Status status, boolean b) {
|
public PluginResult(Status status, boolean b) {
|
||||||
this.status = status.ordinal();
|
this.status = status.ordinal();
|
||||||
this.message = ""+b;
|
this.message = ""+b;
|
||||||
@ -57,7 +58,7 @@ public class PluginResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getJSONString() {
|
public String getJSONString() {
|
||||||
return "{ status: " + this.getStatus() + ", message: " + this.getMessage() + " }";
|
return "{ status: " + this.getStatus() + ", message: '" + this.getMessage() + "' }";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toSuccessCallbackString(String callbackId) {
|
public String toSuccessCallbackString(String callbackId) {
|
||||||
@ -77,7 +78,10 @@ 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 {
|
||||||
@ -89,6 +93,9 @@ 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user