Refactor out the Java casting code

When we return JSON to the Java side it does not have the proper methods such as Contact.save() so we need to cast the JSON to the correct JS object. This used to be done from the Java layer calling the right method to cast the JSON. In this new approach the JavaScript layer will no what needs to be cast and call it's own internal function to do the cast.
This commit is contained in:
macdonst
2012-02-08 14:12:06 -05:00
parent 77547f8826
commit 9aa1cd756a
12 changed files with 260 additions and 187 deletions
@@ -31,14 +31,6 @@ public class PluginResult extends org.apache.cordova.api.PluginResult {
super(status, message);
}
public PluginResult(Status status, JSONArray message, String cast) {
super(status, message, cast);
}
public PluginResult(Status status, JSONObject message, String cast) {
super(status, message, cast);
}
public PluginResult(Status status, JSONArray message) {
super(status, message);
}
@@ -236,7 +236,7 @@ public class Capture extends Plugin {
if (results.length() >= limit) {
// Send Uri back to JavaScript for listening to audio
this.success(new PluginResult(PluginResult.Status.OK, results, "navigator.device.capture._castMediaFile"), this.callbackId);
this.success(new PluginResult(PluginResult.Status.OK, results), this.callbackId);
} else {
// still need to capture more audio clips
captureAudio();
@@ -291,7 +291,7 @@ public class Capture extends Plugin {
if (results.length() >= limit) {
// Send Uri back to JavaScript for viewing image
this.success(new PluginResult(PluginResult.Status.OK, results, "navigator.device.capture._castMediaFile"), this.callbackId);
this.success(new PluginResult(PluginResult.Status.OK, results), this.callbackId);
} else {
// still need to capture more images
captureImage();
@@ -308,7 +308,7 @@ public class Capture extends Plugin {
if (results.length() >= limit) {
// Send Uri back to JavaScript for viewing video
this.success(new PluginResult(PluginResult.Status.OK, results, "navigator.device.capture._castMediaFile"), this.callbackId);
this.success(new PluginResult(PluginResult.Status.OK, results), this.callbackId);
} else {
// still need to capture more video clips
captureVideo(duration);
@@ -319,7 +319,7 @@ public class Capture extends Plugin {
else if (resultCode == Activity.RESULT_CANCELED) {
// If we have partial results send them back to the user
if (results.length() > 0) {
this.success(new PluginResult(PluginResult.Status.OK, results, "navigator.device.capture._castMediaFile"), this.callbackId);
this.success(new PluginResult(PluginResult.Status.OK, results), this.callbackId);
}
// user canceled the action
else {
@@ -330,7 +330,7 @@ public class Capture extends Plugin {
else {
// If we have partial results send them back to the user
if (results.length() > 0) {
this.success(new PluginResult(PluginResult.Status.OK, results, "navigator.device.capture._castMediaFile"), this.callbackId);
this.success(new PluginResult(PluginResult.Status.OK, results), this.callbackId);
}
// something bad happened
else {
@@ -119,7 +119,7 @@ public class CompassListener extends Plugin implements SensorEventListener {
}
}
//float f = this.getHeading();
return new PluginResult(status, getCompassHeading(), "navigator.compass._castDate");
return new PluginResult(status, getCompassHeading());
}
else if (action.equals("setTimeout")) {
this.setTimeout(args.getLong(0));
@@ -85,7 +85,7 @@ public class ContactManager extends Plugin {
try {
if (action.equals("search")) {
JSONArray res = contactAccessor.search(args.getJSONArray(0), args.optJSONObject(1));
return new PluginResult(status, res, "navigator.contacts.cast");
return new PluginResult(status, res);
}
else if (action.equals("save")) {
String id = contactAccessor.save(args.getJSONObject(0));
@@ -101,7 +101,7 @@ public class FileTransfer extends Plugin {
} else if (action.equals("download")) {
JSONObject r = download(source, target);
Log.d(LOG_TAG, "****** About to return a result from download");
return new PluginResult(PluginResult.Status.OK, r, "window.localFileSystem._castEntry");
return new PluginResult(PluginResult.Status.OK, r);
} else {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
+10 -10
View File
@@ -137,31 +137,31 @@ public class FileUtils extends Plugin {
}
}
JSONObject obj = requestFileSystem(args.getInt(0));
return new PluginResult(status, obj, "window.localFileSystem._castFS");
return new PluginResult(status, obj);
}
else if (action.equals("resolveLocalFileSystemURI")) {
JSONObject obj = resolveLocalFileSystemURI(args.getString(0));
return new PluginResult(status, obj, "window.localFileSystem._castEntry");
return new PluginResult(status, obj);
}
else if (action.equals("getMetadata")) {
JSONObject obj = getMetadata(args.getString(0));
return new PluginResult(status, obj, "window.localFileSystem._castDate");
return new PluginResult(status, obj);
}
else if (action.equals("getFileMetadata")) {
JSONObject obj = getFileMetadata(args.getString(0));
return new PluginResult(status, obj, "window.localFileSystem._castDate");
return new PluginResult(status, obj);
}
else if (action.equals("getParent")) {
JSONObject obj = getParent(args.getString(0));
return new PluginResult(status, obj, "window.localFileSystem._castEntry");
return new PluginResult(status, obj);
}
else if (action.equals("getDirectory")) {
JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), true);
return new PluginResult(status, obj, "window.localFileSystem._castEntry");
return new PluginResult(status, obj);
}
else if (action.equals("getFile")) {
JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), false);
return new PluginResult(status, obj, "window.localFileSystem._castEntry");
return new PluginResult(status, obj);
}
else if (action.equals("remove")) {
boolean success;
@@ -187,15 +187,15 @@ public class FileUtils extends Plugin {
}
else if (action.equals("moveTo")) {
JSONObject entry = transferTo(args.getString(0), args.getJSONObject(1), args.optString(2), true);
return new PluginResult(status, entry, "window.localFileSystem._castEntry");
return new PluginResult(status, entry);
}
else if (action.equals("copyTo")) {
JSONObject entry = transferTo(args.getString(0), args.getJSONObject(1), args.optString(2), false);
return new PluginResult(status, entry, "window.localFileSystem._castEntry");
return new PluginResult(status, entry);
}
else if (action.equals("readEntries")) {
JSONArray entries = readEntries(args.getString(0));
return new PluginResult(status, entries, "window.localFileSystem._castEntries");
return new PluginResult(status, entries);
}
return new PluginResult(status, result);
} catch (FileNotFoundException e) {
@@ -27,7 +27,6 @@ public class PluginResult {
private final int status;
private final String message;
private boolean keepCallback = false;
private String cast = null;
public PluginResult(Status status) {
this.status = status.ordinal();
@@ -39,18 +38,6 @@ public class PluginResult {
this.message = JSONObject.quote(message);
}
public PluginResult(Status status, JSONArray message, String cast) {
this.status = status.ordinal();
this.message = message.toString();
this.cast = cast;
}
public PluginResult(Status status, JSONObject message, String cast) {
this.status = status.ordinal();
this.message = message.toString();
this.cast = cast;
}
public PluginResult(Status status, JSONArray message) {
this.status = status.ordinal();
this.message = message.toString();
@@ -97,15 +84,7 @@ public class PluginResult {
}
public String toSuccessCallbackString(String callbackId) {
StringBuffer buf = new StringBuffer("");
if (cast != null) {
buf.append("var temp = "+cast+"("+this.getJSONString() + ");\n");
buf.append("Cordova.callbackSuccess('"+callbackId+"',temp);");
}
else {
buf.append("Cordova.callbackSuccess('"+callbackId+"',"+this.getJSONString()+");");
}
return buf.toString();
return "Cordova.callbackSuccess('"+callbackId+"',"+this.getJSONString()+");";
}
public String toErrorCallbackString(String callbackId) {