Modify PluginResult(status) so it generates a JSON string that works with JSON.parse()

This commit is contained in:
macdonst 2012-07-06 17:39:04 -04:00
parent eb0348d47c
commit 401c2f42f9

View File

@ -25,13 +25,13 @@ public class PluginResult {
private final int status; private final int status;
private final String message; private final String message;
private boolean keepCallback = false; private boolean keepCallback = false;
public PluginResult(Status status) { public PluginResult(Status status) {
this.status = status.ordinal(); this.status = status.ordinal();
this.message = "'" + PluginResult.StatusMessages[this.status] + "'"; this.message = "\"" + PluginResult.StatusMessages[this.status] + "\"";
} }
public PluginResult(Status status, String message) { public PluginResult(Status status, String message) {
this.status = status.ordinal(); this.status = status.ordinal();
this.message = JSONObject.quote(message); this.message = JSONObject.quote(message);
@ -61,11 +61,11 @@ public class PluginResult {
this.status = status.ordinal(); this.status = status.ordinal();
this.message = ""+b; this.message = ""+b;
} }
public void setKeepCallback(boolean b) { public void setKeepCallback(boolean b) {
this.keepCallback = b; this.keepCallback = b;
} }
public int getStatus() { public int getStatus() {
return status; return status;
} }
@ -73,23 +73,23 @@ public class PluginResult {
public String getMessage() { public String getMessage() {
return message; return message;
} }
public boolean getKeepCallback() { public boolean getKeepCallback() {
return this.keepCallback; return this.keepCallback;
} }
public String getJSONString() { public String getJSONString() {
return "{\"status\":" + this.status + ",\"message\":" + this.message + ",\"keepCallback\":" + this.keepCallback + "}"; return "{\"status\":" + this.status + ",\"message\":" + this.message + ",\"keepCallback\":" + this.keepCallback + "}";
} }
public String toSuccessCallbackString(String callbackId) { public String toSuccessCallbackString(String callbackId) {
return "cordova.callbackSuccess('"+callbackId+"',"+this.getJSONString()+");"; return "cordova.callbackSuccess('"+callbackId+"',"+this.getJSONString()+");";
} }
public String toErrorCallbackString(String callbackId) { public String toErrorCallbackString(String callbackId) {
return "cordova.callbackError('"+callbackId+"', " + this.getJSONString()+ ");"; return "cordova.callbackError('"+callbackId+"', " + this.getJSONString()+ ");";
} }
public static String[] StatusMessages = new String[] { public static String[] StatusMessages = new String[] {
"No result", "No result",
"OK", "OK",
@ -102,7 +102,7 @@ public class PluginResult {
"JSON error", "JSON error",
"Error" "Error"
}; };
public enum Status { public enum Status {
NO_RESULT, NO_RESULT,
OK, OK,