refactored webworks to have a common exec and got rid of the common manager since it didn't do anything
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/b78cd902 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/b78cd902 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/b78cd902 Branch: refs/heads/master Commit: b78cd902ce78618b0362349fbca8036798b190ab Parents: cdc172b Author: Gord Tanner <[email protected]> Authored: Mon May 28 15:10:57 2012 -0400 Committer: Tim Kim <[email protected]> Committed: Thu Jun 7 13:49:29 2012 -0700 ---------------------------------------------------------------------- lib/blackberry/exec.js | 57 ---- lib/blackberry/platform.js | 6 +- lib/blackberry/plugin/blackberry/app.js | 4 +- lib/blackberry/plugin/blackberry/manager.js | 87 ------ lib/blackberry/plugin/manager.js | 70 +++++ lib/playbook/exec.js | 58 ---- lib/playbook/plugin/manager.js | 320 +++++++++++++++++++++ lib/playbook/plugin/playbook/manager.js | 323 ---------------------- lib/webworks/exec.js | 58 ++++ lib/webworks/plugin/webworks/manager.js | 14 - 10 files changed, 453 insertions(+), 544 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b78cd902/lib/blackberry/exec.js ---------------------------------------------------------------------- diff --git a/lib/blackberry/exec.js b/lib/blackberry/exec.js deleted file mode 100644 index 806aae6..0000000 --- a/lib/blackberry/exec.js +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Execute a cordova command. It is up to the native side whether this action - * is synchronous or asynchronous. The native side can return: - * Synchronous: PluginResult object as a JSON string - * Asynchrounous: Empty string "" - * If async, the native side will cordova.callbackSuccess or cordova.callbackError, - * depending upon the result of the action. - * - * @param {Function} success The success callback - * @param {Function} fail The fail callback - * @param {String} service The name of the service to use - * @param {String} action Action to be run in cordova - * @param {String[]} [args] Zero or more arguments to pass to the method - */ -var blackberry = require('cordova/plugin/blackberry/manager'), - cordova = require('cordova'), - utils = require('cordova/utils'); - -module.exports = function(success, fail, service, action, args) { - try { - var v = blackberry.exec(success, fail, service, action, args); - - // If status is OK, then return value back to caller - if (v.status == cordova.callbackStatus.OK) { - - // If there is a success callback, then call it now with returned value - if (success) { - try { - success(v.message); - } - catch (e) { - console.log("Error in success callback: "+ service + "." + action + " = "+e); - } - - } - return v.message; - } else if (v.status == cordova.callbackStatus.NO_RESULT) { - - } else { - // If error, then display error - console.log("Error: " + service + "." + action + " Status="+v.status+" Message="+v.message); - - // If there is a fail callback, then call it now with returned value - if (fail) { - try { - fail(v.message); - } - catch (e) { - console.log("Error in error callback: " + service + "." + action + " = "+e); - } - } - return null; - } - } catch (e) { - utils.alert("Error: "+e); - } -}; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b78cd902/lib/blackberry/platform.js ---------------------------------------------------------------------- diff --git a/lib/blackberry/platform.js b/lib/blackberry/platform.js index 4263f8f..0eee961 100644 --- a/lib/blackberry/platform.js +++ b/lib/blackberry/platform.js @@ -4,7 +4,7 @@ module.exports = { var cordova = require('cordova'), exec = require('cordova/exec'), channel = require('cordova/channel'), - blackberryManager = require('cordova/plugin/blackberry/manager'), + manager = require('cordova/plugin/manager'), app = require('cordova/plugin/blackberry/app'); // BB OS 5 does not define window.console. @@ -72,11 +72,11 @@ module.exports = { // Fires off necessary code to pause/resume app var resume = function() { cordova.fireDocumentEvent('resume'); - blackberryManager.resume(); + manager.resume(); }; var pause = function() { cordova.fireDocumentEvent('pause'); - blackberryManager.pause(); + manager.pause(); }; /************************************************ http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b78cd902/lib/blackberry/plugin/blackberry/app.js ---------------------------------------------------------------------- diff --git a/lib/blackberry/plugin/blackberry/app.js b/lib/blackberry/plugin/blackberry/app.js index cf6a003..a046013 100644 --- a/lib/blackberry/plugin/blackberry/app.js +++ b/lib/blackberry/plugin/blackberry/app.js @@ -1,5 +1,5 @@ var exec = require('cordova/exec'); -var manager = require('cordova/plugin/blackberry/manager'); +var manager = require('cordova/plugin/manager'); module.exports = { /** @@ -47,4 +47,4 @@ module.exports = { // exit the app blackberry.app.exit(); } -}; \ No newline at end of file +}; http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b78cd902/lib/blackberry/plugin/blackberry/manager.js ---------------------------------------------------------------------- diff --git a/lib/blackberry/plugin/blackberry/manager.js b/lib/blackberry/plugin/blackberry/manager.js deleted file mode 100644 index d3a08bc..0000000 --- a/lib/blackberry/plugin/blackberry/manager.js +++ /dev/null @@ -1,87 +0,0 @@ -var webworks = require('cordova/plugin/webworks/manager'), - Cordova = require('cordova'), - plugins = {}; - -function _exec(win, fail, clazz, action, args) { - var callbackId = clazz + Cordova.callbackId++, - origResult, - evalResult, - execResult; - - try { - - if (win || fail) { - Cordova.callbacks[callbackId] = {success: win, fail: fail}; - } - - // Note: Device returns string, but for some reason emulator returns object - so convert to string. - origResult = "" + org.apache.cordova.JavaPluginManager.exec(clazz, action, callbackId, JSON.stringify(args), true); - - // If a result was returned - if (origResult.length > 0) { - eval("evalResult = " + origResult + ";"); - - // If status is OK, then return evalResultalue back to caller - if (evalResult.status === Cordova.callbackStatus.OK) { - - // If there is a success callback, then call it now with returned evalResultalue - if (win) { - // Clear callback if not expecting any more results - if (!evalResult.keepCallback) { - delete Cordova.callbacks[callbackId]; - } - } - } else if (evalResult.status === Cordova.callbackStatus.NO_RESULT) { - - // Clear callback if not expecting any more results - if (!evalResult.keepCallback) { - delete Cordova.callbacks[callbackId]; - } - } else { - // If there is a fail callback, then call it now with returned evalResultalue - if (fail) { - - // Clear callback if not expecting any more results - if (!evalResult.keepCallback) { - delete Cordova.callbacks[callbackId]; - } - } - } - execResult = evalResult; - } else { - // Asynchronous calls return an empty string. Return a NO_RESULT - // status for those executions. - execResult = {"status" : Cordova.callbackStatus.NO_RESULT, - "message" : ""}; - } - } catch (e) { - console.log("BlackBerryPluginManager Error: " + e); - execResult = {"status" : Cordova.callbackStatus.ERROR, - "message" : e.message}; - } - - return execResult; -} - -module.exports = { - exec: function (win, fail, clazz, action, args) { - var result = webworks.exec(win, fail, clazz, action, args); - - //We got a sync result or a not found from WW that we can pass on to get a native mixin - //For async calls there's nothing to do - if (result.status === Cordova.callbackStatus.CLASS_NOT_FOUND_EXCEPTION || - result.status === Cordova.callbackStatus.INVALID_ACTION || - result.status === Cordova.callbackStatus.OK) { - if (plugins[clazz]) { - return plugins[clazz].execute(result.message, action, args, win, fail); - } else { - result = _exec(win, fail, clazz, action, args); - } - } - - return result; - }, - resume: org.apache.cordova.JavaPluginManager.resume, - pause: org.apache.cordova.JavaPluginManager.pause, - destroy: org.apache.cordova.JavaPluginManager.destroy -}; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b78cd902/lib/blackberry/plugin/manager.js ---------------------------------------------------------------------- diff --git a/lib/blackberry/plugin/manager.js b/lib/blackberry/plugin/manager.js new file mode 100644 index 0000000..f0d0bb9 --- /dev/null +++ b/lib/blackberry/plugin/manager.js @@ -0,0 +1,70 @@ +var cordova = require('cordova'); + +function _exec(win, fail, clazz, action, args) { + var callbackId = clazz + cordova.callbackId++, + origResult, + evalResult, + execResult; + + try { + if (win || fail) { + cordova.callbacks[callbackId] = {success: win, fail: fail}; + } + + // Note: Device returns string, but for some reason emulator returns object - so convert to string. + origResult = "" + org.apache.cordova.JavaPluginManager.exec(clazz, action, callbackId, JSON.stringify(args), true); + + // If a result was returned + if (origResult.length > 0) { + eval("evalResult = " + origResult + ";"); + + // If status is OK, then return evalResultalue back to caller + if (evalResult.status === cordova.callbackStatus.OK) { + + // If there is a success callback, then call it now with returned evalResultalue + if (win) { + // Clear callback if not expecting any more results + if (!evalResult.keepCallback) { + delete cordova.callbacks[callbackId]; + } + } + } else if (evalResult.status === cordova.callbackStatus.NO_RESULT) { + + // Clear callback if not expecting any more results + if (!evalResult.keepCallback) { + delete cordova.callbacks[callbackId]; + } + } else { + // If there is a fail callback, then call it now with returned evalResultalue + if (fail) { + + // Clear callback if not expecting any more results + if (!evalResult.keepCallback) { + delete cordova.callbacks[callbackId]; + } + } + } + execResult = evalResult; + } else { + // Asynchronous calls return an empty string. Return a NO_RESULT + // status for those executions. + execResult = {"status" : cordova.callbackStatus.NO_RESULT, + "message" : ""}; + } + } catch (e) { + console.log("BlackBerryPluginManager Error: " + e); + execResult = {"status" : cordova.callbackStatus.ERROR, + "message" : e.message}; + } + + return execResult; +} + +module.exports = { + exec: function (win, fail, clazz, action, args) { + return _exec(win, fail, clazz, action, args); + }, + resume: org.apache.cordova.JavaPluginManager.resume, + pause: org.apache.cordova.JavaPluginManager.pause, + destroy: org.apache.cordova.JavaPluginManager.destroy +}; http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b78cd902/lib/playbook/exec.js ---------------------------------------------------------------------- diff --git a/lib/playbook/exec.js b/lib/playbook/exec.js deleted file mode 100644 index bd221ea..0000000 --- a/lib/playbook/exec.js +++ /dev/null @@ -1,58 +0,0 @@ -var playbook = require('cordova/plugin/playbook/manager'), - cordova = require('cordova'), - utils = require('cordova/utils'); - -/** - * Execute a cordova command. It is up to the native side whether this action - * is synchronous or asynchronous. The native side can return: - * Synchronous: PluginResult object as a JSON string - * Asynchrounous: Empty string "" - * If async, the native side will cordova.callbackSuccess or cordova.callbackError, - * depending upon the result of the action. - * - * @param {Function} success The success callback - * @param {Function} fail The fail callback - * @param {String} service The name of the service to use - * @param {String} action Action to be run in cordova - * @param {String[]} [args] Zero or more arguments to pass to the method - */ - -module.exports = function(success, fail, service, action, args) { - try { - var v = playbook.exec(success, fail, service, action, args); - - // If status is OK, then return value back to caller - if (v.status == cordova.callbackStatus.OK) { - - // If there is a success callback, then call it now with returned value - if (success) { - try { - success(v.message); - } - catch (e) { - console.log("Error in success callback: "+cordova.callbackId+" = "+e); - } - - } - return v.message; - } else if (v.status == cordova.callbackStatus.NO_RESULT) { - - } else { - // If error, then display error - console.log("Error: Status="+v.status+" Message="+v.message); - - // If there is a fail callback, then call it now with returned value - if (fail) { - try { - fail(v.message); - } - catch (e) { - console.log("Error in error callback: "+cordova.callbackId+" = "+e); - } - } - return null; - } - } catch (e) { - utils.alert("Error: "+e); - } -}; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b78cd902/lib/playbook/plugin/manager.js ---------------------------------------------------------------------- diff --git a/lib/playbook/plugin/manager.js b/lib/playbook/plugin/manager.js new file mode 100644 index 0000000..593c812 --- /dev/null +++ b/lib/playbook/plugin/manager.js @@ -0,0 +1,320 @@ +var cordova = require('cordova'), + MediaFile = require('cordova/plugin/MediaFile'), + /** + * Private list of HTML 5 audio objects, indexed by the Cordova media object ids + */ + audioObjects = {}, + retInvalidAction = function () { + return { "status" : cordova.callbackStatus.INVALID_ACTION, "message" : "Action not found" }; + }, + retAsyncCall = function () { + return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "WebWorks Is On It" }; + }, + batteryAPI = { + execute: function (webWorksResult, action, args, win, fail) { + if (action === 'start') { + // Register one listener to each of level and state change + // events using WebWorks API. + blackberry.system.event.deviceBatteryStateChange(function(state) { + var me = navigator.battery; + // state is either CHARGING or UNPLUGGED + if (state === 2 || state === 3) { + var info = { + "level" : me._level, + "isPlugged" : state === 2 + }; + + if (me._isPlugged !== info.isPlugged && typeof win === 'function') { + win(info); + } + } + }); + blackberry.system.event.deviceBatteryLevelChange(function(level) { + var me = navigator.battery; + if (level != me._level && typeof win === 'function') { + win({'level' : level, 'isPlugged' : me._isPlugged}); + } + }); + } else if (action === 'stop') { + // Unregister battery listeners. + blackberry.system.event.deviceBatteryStateChange(null); + blackberry.system.event.deviceBatteryLevelChange(null); + } else { + return retInvalidAction(); + } + } + }, + cameraAPI = { + execute: function (webWorksResult, action, args, win, fail) { + if (action === 'takePicture') { + blackberry.media.camera.takePicture(win, fail, fail); + return retAsyncCall(); + } + else { + return retInvalidAction(); + } + } + }, + deviceAPI = { + execute: function (webWorksResult, action, args, win, fail) { + if (action === 'getDeviceInfo') { + return {"status" : cordova.callbackStatus.OK, + "message" : { + "version" : blackberry.system.softwareVersion, + "name" : blackberry.system.model, + "uuid" : blackberry.identity.PIN, + "platform" : "PlayBook", + "cordova" : "1.8.0" + } + }; + } + return retInvalidAction(); + } + }, + loggerAPI = { + execute: function (webWorksResult, action, args, win, fail) { + if (action === 'log') { + console.log(args); + return {"status" : cordova.callbackStatus.OK, + "message" : 'Message logged to console: ' + args}; + } + else { + return retInvalidAction(); + } + } + }, + mediaAPI = { + execute: function (webWorksResult, action, args, win, fail) { + if (!args.length) { + return {"status" : 9, "message" : "Media Object id was not sent in arguments"}; + } + + var id = args[0], + audio = audioObjects[id], + result; + + switch (action) { + case 'startPlayingAudio': + if (args.length === 1) { + result = {"status" : 9, "message" : "Media source argument not found"}; + + } + + if (audio) { + audio.pause(); + audioObjects[id] = undefined; + } + + audio = audioObjects[id] = new Audio(args[1]); + audio.play(); + + result = {"status" : 1, "message" : "Audio play started" }; + break; + case 'stopPlayingAudio': + if (!audio) { + return {"status" : 2, "message" : "Audio Object has not been initialized"}; + } + + audio.pause(); + audioObjects[id] = undefined; + + result = {"status" : 1, "message" : "Audio play stopped" }; + break; + case 'seekToAudio': + if (!audio) { + result = {"status" : 2, "message" : "Audio Object has not been initialized"}; + } else if (args.length === 1) { + result = {"status" : 9, "message" : "Media seek time argument not found"}; + } else { + try { + audio.currentTime = args[1]; + } catch (e) { + console.log('Error seeking audio: ' + e); + return {"status" : 3, "message" : "Error seeking audio: " + e}; + } + + result = {"status" : 1, "message" : "Seek to audio succeeded" }; + } + break; + case 'pausePlayingAudio': + if (!audio) { + return {"status" : 2, "message" : "Audio Object has not been initialized"}; + } + + audio.pause(); + + result = {"status" : 1, "message" : "Audio paused" }; + break; + case 'getCurrentPositionAudio': + if (!audio) { + return {"status" : 2, "message" : "Audio Object has not been initialized"}; + } + + result = {"status" : 1, "message" : audio.currentTime }; + break; + case 'getDuration': + if (!audio) { + return {"status" : 2, "message" : "Audio Object has not been initialized"}; + } + + result = {"status" : 1, "message" : audio.duration }; + break; + case 'startRecordingAudio': + if (args.length <= 1) { + result = {"status" : 9, "message" : "Media start recording, insufficient arguments"}; + } + + blackberry.media.microphone.record(args[1], win, fail); + result = retAsyncCall(); + break; + case 'stopRecordingAudio': + break; + case 'release': + if (audio) { + audioObjects[id] = undefined; + audio.src = undefined; + //delete audio; + } + + result = {"status" : 1, "message" : "Media resources released"}; + break; + default: + result = retInvalidAction(); + } + + return result; + } + }, + mediaCaptureAPI = { + execute: function (webWorksResult, action, args, win, fail) { + var limit = args[0], + pictureFiles = [], + captureMethod; + + function captureCB(filePath) { + var mediaFile; + + if (filePath) { + mediaFile = new MediaFile(); + mediaFile.fullPath = filePath; + pictureFiles.push(mediaFile); + } + + if (limit > 0) { + limit--; + blackberry.media.camera[captureMethod](win, fail, fail); + return; + } + + win(pictureFiles); + + return retAsyncCall(); + } + + switch (action) { + case 'getSupportedAudioModes': + case 'getSupportedImageModes': + case 'getSupportedVideoModes': + return {"status": cordova.callbackStatus.OK, "message": []}; + case 'captureImage': + captureMethod = "takePicture"; + captureCB(); + break; + case 'captureVideo': + captureMethod = "takeVideo"; + captureCB(); + break; + case 'captureAudio': + return {"status": cordova.callbackStatus.INVALID_ACTION, "message": "captureAudio is not currently supported"}; + } + + return retAsyncCall(); + } + }, + networkAPI = { + execute: function (webWorksResult, action, args, win, fail) { + if (action !== 'getConnectionInfo') { + return retInvalidAction(); + } + + var connectionType = require("cordova/plugin/Connection").NONE, + eventType = "offline", + callbackID, + request; + + /** + * For PlayBooks, we currently only have WiFi connections, so + * return WiFi if there is any access at all. + * TODO: update if/when PlayBook gets other connection types... + */ + if (blackberry.system.hasDataCoverage()) { + connectionType = require("cordova/plugin/Connection").WIFI; + eventType = "online"; + } + + //Register an event handler for the networkChange event + callbackID = blackberry.events.registerEventHandler("networkChange", function (status) { + win(status.type); + }); + + //pass our callback id down to our network extension + request = new blackberry.transport.RemoteFunctionCall("org/apache/cordova/getConnectionInfo"); + request.addParam("networkStatusChangedID", callbackID); + request.makeSyncCall(); + + return { "status": cordova.callbackStatus.OK, "message": connectionType}; + } + }, + notificationAPI = { + execute: function (webWorksResult, action, args, win, fail) { + if (args.length !== 3) { + return {"status" : 9, "message" : "Notification action - " + action + " arguments not found"}; + + } + + //Unpack and map the args + var msg = args[0], + title = args[1], + btnLabel = args[2], + btnLabels; + + switch (action) { + case 'alert': + blackberry.ui.dialog.customAskAsync.apply(this, [ msg, [ btnLabel ], win, { "title" : title } ]); + return retAsyncCall(); + case 'confirm': + btnLabels = btnLabel.split(","); + blackberry.ui.dialog.customAskAsync.apply(this, [msg, btnLabels, win, {"title" : title} ]); + return retAsyncCall(); + } + return retInvalidAction(); + + } + }, + plugins = { + 'Battery' : batteryAPI, + 'Camera' : cameraAPI, + 'Device' : deviceAPI, + 'Logger' : loggerAPI, + 'Media' : mediaAPI, + 'Capture' : mediaCaptureAPI, + 'NetworkStatus' : networkAPI, + 'Notification' : notificationAPI + }; + +module.exports = { + exec: function (win, fail, clazz, action, args) { + var result = {"status" : cordova.callbackStatus.CLASS_NOT_FOUND_EXCEPTION, "message" : "Class " + clazz + " cannot be found"}; + + //We got a sync result or a not found from WW that we can pass on to get a native mixin + //For async calls there's nothing to do + if (plugins[clazz]) { + result = plugins[clazz].execute(result.message, action, args, win, fail); + } + + return result; + }, + resume: function () {}, + pause: function () {}, + destroy: function () {} +}; http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b78cd902/lib/playbook/plugin/playbook/manager.js ---------------------------------------------------------------------- diff --git a/lib/playbook/plugin/playbook/manager.js b/lib/playbook/plugin/playbook/manager.js deleted file mode 100644 index 33acb43..0000000 --- a/lib/playbook/plugin/playbook/manager.js +++ /dev/null @@ -1,323 +0,0 @@ -var webworks = require('cordova/plugin/webworks/manager'), - cordova = require('cordova'), - MediaFile = require('cordova/plugin/MediaFile'), - /** - * Private list of HTML 5 audio objects, indexed by the Cordova media object ids - */ - audioObjects = {}, - retInvalidAction = function () { - return { "status" : cordova.callbackStatus.INVALID_ACTION, "message" : "Action not found" }; - }, - retAsyncCall = function () { - return { "status" : cordova.callbackStatus.NO_RESULT, "message" : "WebWorks Is On It" }; - }, - batteryAPI = { - execute: function (webWorksResult, action, args, win, fail) { - if (action === 'start') { - // Register one listener to each of level and state change - // events using WebWorks API. - blackberry.system.event.deviceBatteryStateChange(function(state) { - var me = navigator.battery; - // state is either CHARGING or UNPLUGGED - if (state === 2 || state === 3) { - var info = { - "level" : me._level, - "isPlugged" : state === 2 - }; - - if (me._isPlugged !== info.isPlugged && typeof win === 'function') { - win(info); - } - } - }); - blackberry.system.event.deviceBatteryLevelChange(function(level) { - var me = navigator.battery; - if (level != me._level && typeof win === 'function') { - win({'level' : level, 'isPlugged' : me._isPlugged}); - } - }); - } else if (action === 'stop') { - // Unregister battery listeners. - blackberry.system.event.deviceBatteryStateChange(null); - blackberry.system.event.deviceBatteryLevelChange(null); - } else { - return retInvalidAction(); - } - } - }, - cameraAPI = { - execute: function (webWorksResult, action, args, win, fail) { - if (action === 'takePicture') { - blackberry.media.camera.takePicture(win, fail, fail); - return retAsyncCall(); - } - else { - return retInvalidAction(); - } - } - }, - deviceAPI = { - execute: function (webWorksResult, action, args, win, fail) { - if (action === 'getDeviceInfo') { - return {"status" : cordova.callbackStatus.OK, - "message" : { - "version" : blackberry.system.softwareVersion, - "name" : blackberry.system.model, - "uuid" : blackberry.identity.PIN, - "platform" : "PlayBook", - "cordova" : "1.8.0" - } - }; - } - return retInvalidAction(); - } - }, - loggerAPI = { - execute: function (webWorksResult, action, args, win, fail) { - if (action === 'log') { - console.log(args); - return {"status" : cordova.callbackStatus.OK, - "message" : 'Message logged to console: ' + args}; - } - else { - return retInvalidAction(); - } - } - }, - mediaAPI = { - execute: function (webWorksResult, action, args, win, fail) { - if (!args.length) { - return {"status" : 9, "message" : "Media Object id was not sent in arguments"}; - } - - var id = args[0], - audio = audioObjects[id], - result; - - switch (action) { - case 'startPlayingAudio': - if (args.length === 1) { - result = {"status" : 9, "message" : "Media source argument not found"}; - - } - - if (audio) { - audio.pause(); - audioObjects[id] = undefined; - } - - audio = audioObjects[id] = new Audio(args[1]); - audio.play(); - - result = {"status" : 1, "message" : "Audio play started" }; - break; - case 'stopPlayingAudio': - if (!audio) { - return {"status" : 2, "message" : "Audio Object has not been initialized"}; - } - - audio.pause(); - audioObjects[id] = undefined; - - result = {"status" : 1, "message" : "Audio play stopped" }; - break; - case 'seekToAudio': - if (!audio) { - result = {"status" : 2, "message" : "Audio Object has not been initialized"}; - } else if (args.length === 1) { - result = {"status" : 9, "message" : "Media seek time argument not found"}; - } else { - try { - audio.currentTime = args[1]; - } catch (e) { - console.log('Error seeking audio: ' + e); - return {"status" : 3, "message" : "Error seeking audio: " + e}; - } - - result = {"status" : 1, "message" : "Seek to audio succeeded" }; - } - break; - case 'pausePlayingAudio': - if (!audio) { - return {"status" : 2, "message" : "Audio Object has not been initialized"}; - } - - audio.pause(); - - result = {"status" : 1, "message" : "Audio paused" }; - break; - case 'getCurrentPositionAudio': - if (!audio) { - return {"status" : 2, "message" : "Audio Object has not been initialized"}; - } - - result = {"status" : 1, "message" : audio.currentTime }; - break; - case 'getDuration': - if (!audio) { - return {"status" : 2, "message" : "Audio Object has not been initialized"}; - } - - result = {"status" : 1, "message" : audio.duration }; - break; - case 'startRecordingAudio': - if (args.length <= 1) { - result = {"status" : 9, "message" : "Media start recording, insufficient arguments"}; - } - - blackberry.media.microphone.record(args[1], win, fail); - result = retAsyncCall(); - break; - case 'stopRecordingAudio': - break; - case 'release': - if (audio) { - audioObjects[id] = undefined; - audio.src = undefined; - //delete audio; - } - - result = {"status" : 1, "message" : "Media resources released"}; - break; - default: - result = retInvalidAction(); - } - - return result; - } - }, - mediaCaptureAPI = { - execute: function (webWorksResult, action, args, win, fail) { - var limit = args[0], - pictureFiles = [], - captureMethod; - - function captureCB(filePath) { - var mediaFile; - - if (filePath) { - mediaFile = new MediaFile(); - mediaFile.fullPath = filePath; - pictureFiles.push(mediaFile); - } - - if (limit > 0) { - limit--; - blackberry.media.camera[captureMethod](win, fail, fail); - return; - } - - win(pictureFiles); - - return retAsyncCall(); - } - - switch (action) { - case 'getSupportedAudioModes': - case 'getSupportedImageModes': - case 'getSupportedVideoModes': - return {"status": cordova.callbackStatus.OK, "message": []}; - case 'captureImage': - captureMethod = "takePicture"; - captureCB(); - break; - case 'captureVideo': - captureMethod = "takeVideo"; - captureCB(); - break; - case 'captureAudio': - return {"status": cordova.callbackStatus.INVALID_ACTION, "message": "captureAudio is not currently supported"}; - } - - return retAsyncCall(); - } - }, - networkAPI = { - execute: function (webWorksResult, action, args, win, fail) { - if (action !== 'getConnectionInfo') { - return retInvalidAction(); - } - - var connectionType = require("cordova/plugin/Connection").NONE, - eventType = "offline", - callbackID, - request; - - /** - * For PlayBooks, we currently only have WiFi connections, so - * return WiFi if there is any access at all. - * TODO: update if/when PlayBook gets other connection types... - */ - if (blackberry.system.hasDataCoverage()) { - connectionType = require("cordova/plugin/Connection").WIFI; - eventType = "online"; - } - - //Register an event handler for the networkChange event - callbackID = blackberry.events.registerEventHandler("networkChange", function (status) { - win(status.type); - }); - - //pass our callback id down to our network extension - request = new blackberry.transport.RemoteFunctionCall("org/apache/cordova/getConnectionInfo"); - request.addParam("networkStatusChangedID", callbackID); - request.makeSyncCall(); - - return { "status": cordova.callbackStatus.OK, "message": connectionType}; - } - }, - notificationAPI = { - execute: function (webWorksResult, action, args, win, fail) { - if (args.length !== 3) { - return {"status" : 9, "message" : "Notification action - " + action + " arguments not found"}; - - } - - //Unpack and map the args - var msg = args[0], - title = args[1], - btnLabel = args[2], - btnLabels; - - switch (action) { - case 'alert': - blackberry.ui.dialog.customAskAsync.apply(this, [ msg, [ btnLabel ], win, { "title" : title } ]); - return retAsyncCall(); - case 'confirm': - btnLabels = btnLabel.split(","); - blackberry.ui.dialog.customAskAsync.apply(this, [msg, btnLabels, win, {"title" : title} ]); - return retAsyncCall(); - } - return retInvalidAction(); - - } - }, - plugins = { - 'Battery' : batteryAPI, - 'Camera' : cameraAPI, - 'Device' : deviceAPI, - 'Logger' : loggerAPI, - 'Media' : mediaAPI, - 'Capture' : mediaCaptureAPI, - 'NetworkStatus' : networkAPI, - 'Notification' : notificationAPI - }; - -module.exports = { - exec: function (win, fail, clazz, action, args) { - var wwResult = webworks.exec(win, fail, clazz, action, args); - - //We got a sync result or a not found from WW that we can pass on to get a native mixin - //For async calls there's nothing to do - if ((wwResult.status === cordova.callbackStatus.OK || - wwResult.status === cordova.callbackStatus.CLASS_NOT_FOUND_EXCEPTION) && - plugins[clazz]) { - return plugins[clazz].execute(wwResult.message, action, args, win, fail); - } - - return wwResult; - }, - resume: function () {}, - pause: function () {}, - destroy: function () {} -}; http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b78cd902/lib/webworks/exec.js ---------------------------------------------------------------------- diff --git a/lib/webworks/exec.js b/lib/webworks/exec.js new file mode 100644 index 0000000..aaec6e5 --- /dev/null +++ b/lib/webworks/exec.js @@ -0,0 +1,58 @@ +var manager = require('cordova/plugin/manager'), + cordova = require('cordova'), + utils = require('cordova/utils'); + +/** + * Execute a cordova command. It is up to the native side whether this action + * is synchronous or asynchronous. The native side can return: + * Synchronous: PluginResult object as a JSON string + * Asynchrounous: Empty string "" + * If async, the native side will cordova.callbackSuccess or cordova.callbackError, + * depending upon the result of the action. + * + * @param {Function} success The success callback + * @param {Function} fail The fail callback + * @param {String} service The name of the service to use + * @param {String} action Action to be run in cordova + * @param {String[]} [args] Zero or more arguments to pass to the method + */ + +module.exports = function(success, fail, service, action, args) { + try { + var v = manager.exec(success, fail, service, action, args); + + // If status is OK, then return value back to caller + if (v.status == cordova.callbackStatus.OK) { + + // If there is a success callback, then call it now with returned value + if (success) { + try { + success(v.message); + } + catch (e) { + console.log("Error in success callback: "+cordova.callbackId+" = "+e); + } + + } + return v.message; + } else if (v.status == cordova.callbackStatus.NO_RESULT) { + + } else { + // If error, then display error + console.log("Error: Status="+v.status+" Message="+v.message); + + // If there is a fail callback, then call it now with returned value + if (fail) { + try { + fail(v.message); + } + catch (e) { + console.log("Error in error callback: "+cordova.callbackId+" = "+e); + } + } + return null; + } + } catch (e) { + utils.alert("Error: "+e); + } +}; http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/b78cd902/lib/webworks/plugin/webworks/manager.js ---------------------------------------------------------------------- diff --git a/lib/webworks/plugin/webworks/manager.js b/lib/webworks/plugin/webworks/manager.js deleted file mode 100644 index 9c0a454..0000000 --- a/lib/webworks/plugin/webworks/manager.js +++ /dev/null @@ -1,14 +0,0 @@ -// Define JavaScript plugin implementations that are common across -// WebWorks platforms (phone/tablet). -var plugins = {}, - cordova = require('cordova'); - -module.exports = { - exec: function (win, fail, clazz, action, args) { - if (plugins[clazz]) { - return plugins[clazz].execute(action, args, win, fail); - } - - return {"status" : cordova.callbackStatus.CLASS_NOT_FOUND_EXCEPTION, "message" : "Class " + clazz + " cannot be found"}; - } -}; \ No newline at end of file
