From 9aa1cd756a232fe1f28b68493f9faf7af6db5f33 Mon Sep 17 00:00:00 2001 From: macdonst Date: Wed, 8 Feb 2012 14:12:06 -0500 Subject: [PATCH] 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. --- framework/assets/js/capture.js | 56 ++-- framework/assets/js/compass.js | 19 +- framework/assets/js/contact.js | 27 +- framework/assets/js/file.js | 262 ++++++++++++------ framework/assets/js/filetransfer.js | 16 +- .../src/com/phonegap/api/PluginResult.java | 8 - framework/src/org/apache/cordova/Capture.java | 10 +- .../org/apache/cordova/CompassListener.java | 2 +- .../org/apache/cordova/ContactManager.java | 2 +- .../src/org/apache/cordova/FileTransfer.java | 2 +- .../src/org/apache/cordova/FileUtils.java | 20 +- .../org/apache/cordova/api/PluginResult.java | 23 +- 12 files changed, 260 insertions(+), 187 deletions(-) diff --git a/framework/assets/js/capture.js b/framework/assets/js/capture.js index ba441366..66920e79 100644 --- a/framework/assets/js/capture.js +++ b/framework/assets/js/capture.js @@ -95,7 +95,7 @@ var Capture = function(){ * @param {CaptureAudioOptions} options */ Capture.prototype.captureAudio = function(successCallback, errorCallback, options){ - Cordova.exec(successCallback, errorCallback, "Capture", "captureAudio", [options]); + navigator.device.capture._capture("captureAudio", successCallback, errorCallback, options); }; /** @@ -106,30 +106,7 @@ Capture.prototype.captureAudio = function(successCallback, errorCallback, option * @param {CaptureImageOptions} options */ Capture.prototype.captureImage = function(successCallback, errorCallback, options){ - Cordova.exec(successCallback, errorCallback, "Capture", "captureImage", [options]); -}; - -/** - * Launch camera application for taking image(s). - * - * @param {Function} successCB - * @param {Function} errorCB - * @param {CaptureImageOptions} options - */ -Capture.prototype._castMediaFile = function(pluginResult){ - var mediaFiles = []; - var i; - for (i = 0; i < pluginResult.message.length; i++) { - var mediaFile = new MediaFile(); - mediaFile.name = pluginResult.message[i].name; - mediaFile.fullPath = pluginResult.message[i].fullPath; - mediaFile.type = pluginResult.message[i].type; - mediaFile.lastModifiedDate = pluginResult.message[i].lastModifiedDate; - mediaFile.size = pluginResult.message[i].size; - mediaFiles.push(mediaFile); - } - pluginResult.message = mediaFiles; - return pluginResult; + navigator.device.capture._capture("captureImage", successCallback, errorCallback, options); }; /** @@ -140,9 +117,36 @@ Capture.prototype._castMediaFile = function(pluginResult){ * @param {CaptureVideoOptions} options */ Capture.prototype.captureVideo = function(successCallback, errorCallback, options){ - Cordova.exec(successCallback, errorCallback, "Capture", "captureVideo", [options]); + navigator.device.capture._capture("captureVideo", successCallback, errorCallback, options); }; +/** + * Launches the correct capture. + * + * @param (DOMString} type + * @param {Function} successCB + * @param {Function} errorCB + * @param {CaptureVideoOptions} options + */ +Capture.prototype._capture = function(type, successCallback, errorCallback, options){ + var win = function(result) { + var mediaFiles = []; + var i; + for (i = 0; i < pluginResult.message.length; i++) { + var mediaFile = new MediaFile(); + mediaFile.name = pluginResult.message[i].name; + mediaFile.fullPath = pluginResult.message[i].fullPath; + mediaFile.type = pluginResult.message[i].type; + mediaFile.lastModifiedDate = pluginResult.message[i].lastModifiedDate; + mediaFile.size = pluginResult.message[i].size; + mediaFiles.push(mediaFile); + } + successCallback(mediaFiles); + }; + Cordova.exec(win, errorCallback, "Capture", type, [options]); +}; + + /** * Encapsulates a set of parameters that the capture device supports. */ diff --git a/framework/assets/js/compass.js b/framework/assets/js/compass.js index b8c9e136..ef9a9ba6 100755 --- a/framework/assets/js/compass.js +++ b/framework/assets/js/compass.js @@ -73,9 +73,17 @@ Compass.prototype.getCurrentHeading = function(successCallback, errorCallback, o console.log("Compass Error: errorCallback is not a function"); return; } + + var win = function(result) { + if (result.timestamp) { + var timestamp = new Date(result.timestamp); + result.timestamp = timestamp; + } + successCallback(result); + }; // Get heading - Cordova.exec(successCallback, errorCallback, "Compass", "getHeading", []); + Cordova.exec(win, errorCallback, "Compass", "getHeading", []); }; /** @@ -116,7 +124,14 @@ Compass.prototype.watchHeading= function(successCallback, errorCallback, options var id = Cordova.createUUID(); navigator.compass.timers[id] = setInterval( function() { - Cordova.exec(successCallback, errorCallback, "Compass", "getHeading", []); + var win = function(result) { + if (result.timestamp) { + var timestamp = new Date(result.timestamp); + result.timestamp = timestamp; + } + successCallback(result); + }; + Cordova.exec(win, errorCallback, "Compass", "getHeading", []); }, (frequency ? frequency : 1)); return id; diff --git a/framework/assets/js/contact.js b/framework/assets/js/contact.js index a4d78233..b8948c5a 100755 --- a/framework/assets/js/contact.js +++ b/framework/assets/js/contact.js @@ -259,7 +259,14 @@ Contacts.prototype.find = function(fields, successCB, errorCB, options) { errorCB({"code": ContactError.INVALID_ARGUMENT_ERROR}); } } else { - Cordova.exec(successCB, errorCB, "Contacts", "search", [fields, options]); + var win = function(result) { + var cs = []; + for (var i = 0, l = result.length; i < l; i++) { + cs.push(navigator.contacts.create(result[i])); + } + successCB(cs); + }; + Cordova.exec(win, errorCB, "Contacts", "search", [fields, options]); } }; @@ -281,24 +288,6 @@ Contacts.prototype.create = function(properties) { return contact; }; -/** -* This function returns and array of contacts. It is required as we need to convert raw -* JSON objects into concrete Contact objects. Currently this method is called after -* navigator.contacts.find but before the find methods success call back. -* -* @param jsonArray an array of JSON Objects that need to be converted to Contact objects. -* @returns an array of Contact objects -*/ -Contacts.prototype.cast = function(pluginResult) { - var contacts = []; - var i; - for (i=0; i= 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 { diff --git a/framework/src/org/apache/cordova/CompassListener.java b/framework/src/org/apache/cordova/CompassListener.java index 9ebe4248..49f0bb8e 100755 --- a/framework/src/org/apache/cordova/CompassListener.java +++ b/framework/src/org/apache/cordova/CompassListener.java @@ -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)); diff --git a/framework/src/org/apache/cordova/ContactManager.java b/framework/src/org/apache/cordova/ContactManager.java index 48d279fb..1231ed05 100755 --- a/framework/src/org/apache/cordova/ContactManager.java +++ b/framework/src/org/apache/cordova/ContactManager.java @@ -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)); diff --git a/framework/src/org/apache/cordova/FileTransfer.java b/framework/src/org/apache/cordova/FileTransfer.java index 286c2027..8f8727aa 100644 --- a/framework/src/org/apache/cordova/FileTransfer.java +++ b/framework/src/org/apache/cordova/FileTransfer.java @@ -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); } diff --git a/framework/src/org/apache/cordova/FileUtils.java b/framework/src/org/apache/cordova/FileUtils.java index c2bef707..47bafee3 100755 --- a/framework/src/org/apache/cordova/FileUtils.java +++ b/framework/src/org/apache/cordova/FileUtils.java @@ -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) { diff --git a/framework/src/org/apache/cordova/api/PluginResult.java b/framework/src/org/apache/cordova/api/PluginResult.java index 48419955..89ee1b5f 100755 --- a/framework/src/org/apache/cordova/api/PluginResult.java +++ b/framework/src/org/apache/cordova/api/PluginResult.java @@ -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) {