From d156df99b4104e349ddbee0d877ebff6b45c0f0e Mon Sep 17 00:00:00 2001 From: Fil Maj Date: Sat, 3 Mar 2012 11:02:21 -0800 Subject: [PATCH] changed over CompassConstants -> CompassError, added a proper CompassError obj, tweaking CompassHeading constructor, added fail wrappers to compass, set a default quality level for camera.getPicture. --- README.md | 9 +-------- lib/platform/common.js | 2 +- lib/plugin/Camera.js | 4 +++- lib/plugin/CompassConstants.js | 4 ---- lib/plugin/CompassError.js | 17 +++++++++++++++++ lib/plugin/CompassHeading.js | 10 +++++----- lib/plugin/compass.js | 30 ++++++++++++++++++------------ 7 files changed, 45 insertions(+), 31 deletions(-) delete mode 100644 lib/plugin/CompassConstants.js create mode 100644 lib/plugin/CompassError.js diff --git a/README.md b/README.md index c58267847..45dffe939 100644 --- a/README.md +++ b/README.md @@ -212,26 +212,19 @@ Use the cordova.proto platform in ripple. However, Compass requires that JS initiates a `start`. This is dumb. - Media (and other plugin) implementations across platforms need to use the established cordova/exec callback method (instead of triggering globally-accessible functions to - dispatch listeners). On iOS and Android, grep for "cast" in the native + dispatch listeners). On iOS, grep for "cast" in the native code - you'll see a bunch of invoked JavaScript from native, which shouldn't be there. - Media needs updates across all platforms. Methods need fixing with respect to timing: some methods use milliseconds, some use seconds. Some methods not documented (setVolume on Android). Consolidate / implement properly across platforms. -- Storage shim on Android needs to change its win/fail callbacks to - `require('cordova/plugin/android/storage').failQuery / completeQuery` - (away from droiddb.fail / completeQuery) - Normalize `Entry.toURL` return values. iOS returns `"file://localhost" + fullPath`, Android returns `"file://" + fullPath`, BlackBerry returns just `fullPath` - APIs that are not cross-platform - what to do with these? - Crypto on Android - SMS, telephony, splashscreen on iOS -- Need to normalize native return values as much as possible across - platforms. For example, error objects. Should we return JSON objects - from native or minimal primitives (i.e. error codes as numbers)? Both - are in use today, we need to decide on a standard. - Once-over all of the cordova-docs with the APIs defined in here to make sure all is consistent. There were function signature tweaks, undocumented procedures, etc. diff --git a/lib/platform/common.js b/lib/platform/common.js index 3942b6c7e..e07bf037d 100644 --- a/lib/platform/common.js +++ b/lib/platform/common.js @@ -69,7 +69,7 @@ module.exports = { path: 'cordova/plugin/CompassHeading' }, CompassError:{ - path: 'cordova/plugin/CompassConstants' + path: 'cordova/plugin/CompassError' }, ConfigurationData: { path: 'cordova/plugin/ConfigurationData' diff --git a/lib/plugin/Camera.js b/lib/plugin/Camera.js index 5aa8025f9..05eaac22f 100644 --- a/lib/plugin/Camera.js +++ b/lib/plugin/Camera.js @@ -31,7 +31,7 @@ cameraExport.getPicture = function(successCallback, errorCallback, options) { return; } - + var quality = 50; if (options && typeof options.quality == "number") { quality = options.quality; } else if (options && typeof options.quality == "string") { @@ -75,6 +75,8 @@ cameraExport.getPicture = function(successCallback, errorCallback, options) { if (typeof options.encodingType == "number") { encodingType = options.encodingType; } + // TODO: parse MediaType + // TODO: enable allow edit? exec(successCallback, errorCallback, "Camera", "takePicture", [quality, destinationType, sourceType, targetWidth, targetHeight, encodingType]); } diff --git a/lib/plugin/CompassConstants.js b/lib/plugin/CompassConstants.js deleted file mode 100644 index bf231568f..000000000 --- a/lib/plugin/CompassConstants.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - COMPASS_INTERNAL_ERR:0, - COMPASS_NOT_SUPPORTED:20 -}; diff --git a/lib/plugin/CompassError.js b/lib/plugin/CompassError.js new file mode 100644 index 000000000..60067d2f1 --- /dev/null +++ b/lib/plugin/CompassError.js @@ -0,0 +1,17 @@ +/** + * CompassError. + * An error code assigned by an implementation when an error has occured + * @constructor + */ +var CompassError = function(err) { + this.code = (typeof err != 'undefined' ? err : null); +}; + +/** + * Error codes + */ +CompassError.COMPASS_INTERNAL_ERR = 0; +CompassError.COMPASS_NOT_SUPPORTED = 20; + +module.exports = CompassError; + diff --git a/lib/plugin/CompassHeading.js b/lib/plugin/CompassHeading.js index 839b37017..f9fe5e7bc 100644 --- a/lib/plugin/CompassHeading.js +++ b/lib/plugin/CompassHeading.js @@ -1,8 +1,8 @@ -var CompassHeading = function() { - this.magneticHeading = null; - this.trueHeading = null; - this.headingAccuracy = null; - this.timestamp = new Date(); +var CompassHeading = function(magneticHeading, trueHeading, headingAccuracy, timestamp) { + this.magneticHeading = magneticHeading || null; + this.trueHeading = trueHeading || null; + this.headingAccuracy = headingAccuracy || null; + this.timestamp = (timestamp ? new Date(timestamp) : new Date()); }; module.exports = CompassHeading; diff --git a/lib/plugin/compass.js b/lib/plugin/compass.js index bd9358216..3e6049c1d 100644 --- a/lib/plugin/compass.js +++ b/lib/plugin/compass.js @@ -1,5 +1,7 @@ var exec = require('cordova/exec'), utils = require('cordova/utils'), + CompassHeading = require('cordova/plugin/CompassHeading'), + CompassError = require('cordova/plugin/CompassError'), timers = {}, compass = { /** @@ -24,15 +26,16 @@ var exec = require('cordova/exec'), } var win = function(result) { - if (result.timestamp) { - var timestamp = new Date(result.timestamp); - result.timestamp = timestamp; - } - successCallback(result); + var ch = new CompassHeading(result.magneticHeading, result.trueHeading, result.headingAccuracy, result.timestamp); + successCallback(ch); }; + var fail = function(code) { + var ce = new CompassError(code); + errorCallback(ce); + } // Get heading - exec(win, errorCallback, "Compass", "getHeading", []); + exec(win, fail, "Compass", "getHeading", []); }, /** @@ -63,14 +66,16 @@ var exec = require('cordova/exec'), // Start watch timer to get headings var id = utils.createUUID(); var win = function(result) { - if (result.timestamp) { - var timestamp = new Date(result.timestamp); - result.timestamp = timestamp; - } - successCallback(result); + var ch = new CompassHeading(result.magneticHeading, result.trueHeading, result.headingAccuracy, result.timestamp); + successCallback(ch); + }; + var fail = function(code) { + var ce = new CompassError(code); + errorCallback(ce); }; + timers[id] = window.setInterval(function() { - exec(win, errorCallback, "Compass", "getHeading", []); + exec(win, fail, "Compass", "getHeading", []); }, frequency); return id; @@ -87,6 +92,7 @@ var exec = require('cordova/exec'), delete timers[id]; } } + // TODO: add the filter-based iOS-only methods }; module.exports = compass;