Add support for null PluginResult payloads.

https://issues.apache.org/jira/browse/CB-1744
This commit is contained in:
Andrew Grieve 2012-10-25 12:05:39 -04:00
parent a2f35d2bda
commit 0eee2293dc
3 changed files with 10 additions and 12 deletions

View File

@ -27,7 +27,7 @@ public class Echo extends CordovaPlugin {
@Override @Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
final String result = args.getString(0); final String result = args.isNull(0) ? null : args.getString(0);
if ("echo".equals(action)) { if ("echo".equals(action)) {
callbackContext.success(result); callbackContext.success(result);
return true; return true;

View File

@ -391,19 +391,15 @@ public class NativeToJsMessageQueue {
int statusLen = String.valueOf(pluginResult.getStatus()).length(); int statusLen = String.valueOf(pluginResult.getStatus()).length();
int ret = 2 + statusLen + 1 + jsPayloadOrCallbackId.length() + 1; int ret = 2 + statusLen + 1 + jsPayloadOrCallbackId.length() + 1;
switch (pluginResult.getMessageType()) { switch (pluginResult.getMessageType()) {
case PluginResult.MESSAGE_TYPE_BOOLEAN: case PluginResult.MESSAGE_TYPE_BOOLEAN: // f or t
case PluginResult.MESSAGE_TYPE_NULL: // N
ret += 1; ret += 1;
break; break;
case PluginResult.MESSAGE_TYPE_NUMBER: // n case PluginResult.MESSAGE_TYPE_NUMBER: // n
ret += 1 + pluginResult.getMessage().length(); ret += 1 + pluginResult.getMessage().length();
break; break;
case PluginResult.MESSAGE_TYPE_STRING: // s case PluginResult.MESSAGE_TYPE_STRING: // s
if (pluginResult.getStrMessage() == null) {
ret += 1;
}
else {
ret += 1 + pluginResult.getStrMessage().length(); ret += 1 + pluginResult.getStrMessage().length();
}
break; break;
case PluginResult.MESSAGE_TYPE_JSON: case PluginResult.MESSAGE_TYPE_JSON:
default: default:
@ -433,15 +429,16 @@ public class NativeToJsMessageQueue {
case PluginResult.MESSAGE_TYPE_BOOLEAN: case PluginResult.MESSAGE_TYPE_BOOLEAN:
sb.append(pluginResult.getMessage().charAt(0)); // t or f. sb.append(pluginResult.getMessage().charAt(0)); // t or f.
break; break;
case PluginResult.MESSAGE_TYPE_NULL: // N
sb.append('N');
break;
case PluginResult.MESSAGE_TYPE_NUMBER: // n case PluginResult.MESSAGE_TYPE_NUMBER: // n
sb.append('n') sb.append('n')
.append(pluginResult.getMessage()); .append(pluginResult.getMessage());
break; break;
case PluginResult.MESSAGE_TYPE_STRING: // s case PluginResult.MESSAGE_TYPE_STRING: // s
sb.append('s'); sb.append('s');
if (pluginResult.getStrMessage() != null) {
sb.append(pluginResult.getStrMessage()); sb.append(pluginResult.getStrMessage());
}
break; break;
case PluginResult.MESSAGE_TYPE_JSON: case PluginResult.MESSAGE_TYPE_JSON:
default: default:

View File

@ -34,7 +34,7 @@ public class PluginResult {
public PluginResult(Status status, String message) { public PluginResult(Status status, String message) {
this.status = status.ordinal(); this.status = status.ordinal();
this.messageType = MESSAGE_TYPE_STRING; this.messageType = message == null ? MESSAGE_TYPE_NULL : MESSAGE_TYPE_STRING;
this.strMessage = message; this.strMessage = message;
} }
@ -133,6 +133,7 @@ public class PluginResult {
public static final int MESSAGE_TYPE_JSON = 2; public static final int MESSAGE_TYPE_JSON = 2;
public static final int MESSAGE_TYPE_NUMBER = 3; public static final int MESSAGE_TYPE_NUMBER = 3;
public static final int MESSAGE_TYPE_BOOLEAN = 4; public static final int MESSAGE_TYPE_BOOLEAN = 4;
public static final int MESSAGE_TYPE_NULL = 5;
public static String[] StatusMessages = new String[] { public static String[] StatusMessages = new String[] {
"No result", "No result",