var FSEAPI = (function() { var API = {}, scheme = "fseapi"; API.version = "1.3.0"; // Override this if you want to output log messages API.log = function(message) { } // Sound API API.sound = {}; // API.sound.play // // Plays a short sound file embedded and shipped with the app as resource. // // fileName: file name of sound file embedded in app. Required. // // Sound files that you play using this function must be: // // No longer than 30 seconds in duration // In linear PCM or IMA4 (IMA/ADPCM) format // Packaged in a .caf, .aif, or .wav file // // Sounds play at the current system audio volume, with no programmatic volume control available // Sounds play immediately // Looping and stereo positioning are unavailable // Simultaneous playback is unavailable: You can play only one sound at a time API.sound.play = function(fileName) { if (arguments.length < 1) return; nativeCall("soundPlay", { "fileName" : fileName }, null, null); } // Voice API API.voice = {}; // API.voice.getCommand // Valid keys and volues for the options object: // detectionType: "short" if the recognizer should end recording after a short pause, or "long" // if the recognizer should wait for a long pause. Defaults to "short". // language: Can be one of "en_US", "en_GB", "fr_FR", or "de_DE". Defaults to "en_US". // recognizerType: "search" to optimize recognition for search queries, or "dictation" to optimize for // dictation of full sentences. Defaults to "dictation". API.voice.getCommand = function(successCallback, failureCallback, options) { if (arguments.length < 3) { options = {}; } nativeCall("getVoiceCommand", options, successCallback, failureCallback); } API.voice.cancelRecording = function() { nativeCall("cancelRecording", null, null, null); } API.voice.speak = function(options, successCallback, failureCallback) { nativeCall("speak", options, successCallback, failureCallback); } // Camera API API.camera = {}; API.camera.getPicture = function(successCallback, failureCallback, options) { if (arguments.length < 3) { options = {}; } nativeCall("getPicture", options, successCallback, failureCallback); }; API.camera.getBarcode = function(successCallback, failureCallback) { nativeCall("getBarcode", {}, successCallback, failureCallback); } // FileTransfer API API.FileUploadOptions = function(fileKey, fileName, mimeType, params, headers) { this.fileKey = fileKey || null; this.fileName = fileName || null; this.mimeType = mimeType || null; this.params = params || null; this.headers = headers || null; }; var lastFileTransferID = 0; API.FileTransfer = function() { self._id = lastFileTransferID++; }; API.FileTransfer.prototype.upload = function(fileURL, postURL, successCallback, failureCallback, options) { if (!fileURL || !postURL) throw new Error("FileTransfer.upload requires fileURL and postURL parameters at the minimum."); nativeCall("upload", { "fileURL" : fileURL, "postURL" : postURL, "options" : options }, successCallback, failureCallback); } // Native bridge -- stuff intended to be called from Objective C API.bridge = {}; API.bridge.successCallback = function(transactionID, obj) { setTimeout(function() { var transaction = removeTransaction(transactionID); if (transaction) { transaction.successCallback(obj); } // else do nothing so that successCallback is optional. }, 0); }; API.bridge.failureCallback = function(transactionID, obj) { setTimeout(function() { var transaction = removeTransaction(transactionID); if (transaction) { transaction.failureCallback(obj); } // else do nothing so that failureCallback is optional. }, 0); }; API.bridge.optionalCallback = function(transactionID, name) { var transaction = transactions[transactionID]; if (transaction && name && transaction.options && typeof(transaction.options[name]) == "function") { return transaction.options[name]; } else { return null; } } // Private methods -- stuff called from public and bridge code nativeCall = function(methodName, options, successCallback, failureCallback) { var fseAPIURL = scheme + ":" + transactionPath(methodName, options, successCallback, failureCallback); API.log("Calling " + fseAPIURL); setTimeout(function() { window.location.href = fseAPIURL}, 0); }; var transactions = {}; var lastTransactionID = Math.floor(Math.random() * 2000000000); transactionPath = function(methodName, options, successCallback, failureCallback) { var transaction = createTransaction(methodName, options, successCallback, failureCallback); return "/" + encodeURI(JSON.stringify({ "apiVersion" : API.version, "transactionID" : transaction["transactionID"], "method" : methodName, "options" : options })); }; createTransaction = function(methodName, options, successCallback, failureCallback) { lastTransactionID++; var transaction = { transactionID : lastTransactionID, method : methodName, options : options, successCallback : successCallback, failureCallback : failureCallback }; transactions[lastTransactionID] = transaction; API.log("Created transaction: " + JSON.stringify(transaction)); return transaction; }; removeTransaction = function(transactionID) { var transaction = transactions[transactionID]; delete transactions[transactionID]; API.log("Deleted transaction: " + JSON.stringify(transaction)); return transaction; }; return API; }());