From 8da131cc45d2866976a18c1a357ca0f5f4862cfb Mon Sep 17 00:00:00 2001 From: macdonst Date: Thu, 16 Sep 2010 11:35:49 -0400 Subject: [PATCH] Changed search function to take filter and option parameters --- example/index.html | 9 +- framework/assets/js/contact.js | 19 +- framework/assets/www/phonegap.js | 533 +++++------------- .../src/com/phonegap/ContactAccessor.java | 9 +- .../com/phonegap/ContactAccessorSdk3_4.java | 14 +- .../src/com/phonegap/ContactAccessorSdk5.java | 17 +- .../src/com/phonegap/ContactManager.java | 284 +--------- 7 files changed, 196 insertions(+), 689 deletions(-) diff --git a/example/index.html b/example/index.html index 124e8101..745856ce 100644 --- a/example/index.html +++ b/example/index.html @@ -101,11 +101,10 @@ function get_contacts() { - var obj = new Contact(); - var name = new ContactName(); - name.givenName = ''; - obj.name = name; - navigator.service.contacts.find(obj, count_contacts, fail); + var obj = new ContactFindOptions(); + obj.filter=""; + obj.multiple=true; + navigator.service.contacts.find(["name", "phone", "email"], count_contacts, fail, obj); } function count_contacts(contacts) diff --git a/framework/assets/js/contact.js b/framework/assets/js/contact.js index 001ac633..cdc8c97f 100644 --- a/framework/assets/js/contact.js +++ b/framework/assets/js/contact.js @@ -72,23 +72,12 @@ var Contacts = function() { this.records = []; } -Contacts.prototype.find = function(obj, win, fail) { +// Contacts.prototype.find = function(obj, win, fail) { +Contacts.prototype.find = function(fields, win, fail, options) { this.win = win; this.fail = fail; - if(obj.name != null) { - // Build up the search term that we'll use in SQL, based on the structure/contents of the contact object passed into find. - var searchTerm = ''; - if (obj.name.givenName && obj.name.givenName.length > 0) { - searchTerm = obj.name.givenName.split(' ').join('%'); - } - if (obj.name.familyName && obj.name.familyName.length > 0) { - searchTerm += obj.name.familyName.split(' ').join('%'); - } - if (!obj.name.familyName && !obj.name.givenName && obj.name.formatted) { - searchTerm = obj.name.formatted; - } - PhoneGap.execAsync(null, null, "Contacts", "search", [searchTerm, "", ""]); - } + + PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]); }; Contacts.prototype.droidFoundContact = function(name, npa, email) { diff --git a/framework/assets/www/phonegap.js b/framework/assets/www/phonegap.js index 543c3627..7cc3f022 100644 --- a/framework/assets/www/phonegap.js +++ b/framework/assets/www/phonegap.js @@ -1,10 +1,7 @@ + if (typeof(DeviceInfo) != 'object') DeviceInfo = {}; -var com = {}; - -com.phonegap = {}; - /** * This represents the PhoneGap API itself, and provides a global namespace for accessing * information about the state of PhoneGap. @@ -256,19 +253,35 @@ PhoneGap.exec = function(clazz, action, args) { PhoneGap.execAsync = function(success, fail, clazz, action, args) { try { var callbackId = clazz + PhoneGap.callbackId++; - PhoneGap.callbacks[callbackId] = {success:success, fail:fail}; + if (success || fail) { + PhoneGap.callbacks[callbackId] = {success:success, fail:fail}; + } var r = PluginManager.exec(clazz, action, callbackId, JSON.stringify(args), true); + + // If a result was returned if (r) { eval("var v="+r+";"); // If status is OK, then return value back to caller if (v.status == 0) { + + // If there is a success callback, then call it now with returned value + if (success) { + success(v.message); + delete PhoneGap.callbacks[callbackId]; + } return v.message; } // If error, then display error else { console.log("Error: Status="+r.status+" Message="+v.message); + + // If there is a fail callback, then call it now with returned value + if (fail) { + fail(v.message); + delete PhoneGap.callbacks[callbackId]; + } return null; } } @@ -278,23 +291,31 @@ PhoneGap.execAsync = function(success, fail, clazz, action, args) { }; PhoneGap.callbackSuccess = function(callbackId, args) { - try { - PhoneGap.callbacks[callbackId].success(args.message); + if (PhoneGap.callbacks[callbackId]) { + try { + if (PhoneGap.callbacks[callbackId].success) { + PhoneGap.callbacks[callbackId].success(args.message); + } + } + catch (e) { + console.log("Error in success callback: "+callbackId+" = "+e); + } + delete PhoneGap.callbacks[callbackId]; } - catch (e) { - console.log("Error in success callback: "+callbackId+" = "+e); - } - delete PhoneGap.callbacks[callbackId]; }; PhoneGap.callbackError = function(callbackId, args) { - try { - PhoneGap.callbacks[callbackId].fail(args.message); + if (PhoneGap.callbacks[callbackId]) { + try { + if (PhoneGap.callbacks[callbackId].fail) { + PhoneGap.callbacks[callbackId].fail(args.message); + } + } + catch (e) { + console.log("Error in error callback: "+callbackId+" = "+e); + } + delete PhoneGap.callbacks[callbackId]; } - catch (e) { - console.log("Error in error callback: "+callbackId+" = "+e); - } - delete PhoneGap.callbacks[callbackId]; }; @@ -425,38 +446,6 @@ PhoneGap.close = function(context, func, params) { } }; -com.phonegap.AccelListenerProxy = function() { - this.className = "com.phonegap.AccelListener"; - this.status = -1; // not set yet -}; -com.phonegap.AccelListenerProxy.prototype.getStatus = function() { - if (this.status == -1) { // if not set, then request status - this.status = PhoneGap.exec(this.className, "getStatus", []); - } - return this.status; -}; -com.phonegap.AccelListenerProxy.prototype.onStatus = function(status) { - console.log("AccelListener.onStatus("+status+")"); - this.status = status; -}; -com.phonegap.AccelListenerProxy.prototype.getAcceleration = function() { - var r = PhoneGap.exec(this.className, "getAcceleration", []); - var a = new Acceleration(r.x,r.y,r.z); - return a; -}; -com.phonegap.AccelListenerProxy.prototype.start = function() { - return PhoneGap.exec(this.className, "start", []); -}; -com.phonegap.AccelListenerProxy.prototype.stop = function() { - return PhoneGap.exec(this.className, "stop", []); -}; -com.phonegap.AccelListenerProxy.prototype.setTimeout = function(timeout) { - return PhoneGap.exec(this.className, "setTimeout", [timeout]); -}; -com.phonegap.AccelListenerProxy.prototype.getTimeout = function() { - return PhoneGap.exec(this.className, "getTimeout", []); -}; -com.phonegap.AccelListener = new com.phonegap.AccelListenerProxy(); function Acceleration(x, y, z) { this.x = x; @@ -482,20 +471,17 @@ function Accelerometer() { this.timers = {}; }; -Accelerometer.STOPPED = 0; -Accelerometer.STARTING = 1; -Accelerometer.RUNNING = 2; -Accelerometer.ERROR_FAILED_TO_START = 3; Accelerometer.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"]; /** * Asynchronously aquires the current acceleration. * * @param {Function} successCallback The function to call when the acceleration data is available - * @param {Function} errorCallback The function to call when there is an error getting the acceleration data. - * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. + * @param {Function} errorCallback The function to call when there is an error getting the acceleration data. (OPTIONAL) + * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL) */ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, errorCallback, options) { + console.log("Accelerometer.getCurrentAcceleration()"); // successCallback required if (typeof successCallback != "function") { @@ -509,60 +495,16 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error return; } - // Get current acceleration status - var status = com.phonegap.AccelListener.getStatus(); - - // If running, then call successCallback - if (status == Accelerometer.RUNNING) { - try { - var accel = com.phonegap.AccelListener.getAcceleration(); - successCallback(accel); - } catch (e) { - console.log("Accelerometer Error in successCallback: " + e); - } - } - - // If not running, then start it - else if (status >= 0) { - com.phonegap.AccelListener.start(); - - // Wait until started - var timer = setInterval(function() { - var status = com.phonegap.AccelListener.getStatus(); - - // If accelerometer is running - if (status == Accelerometer.RUNNING) { - clearInterval(timer); - try { - var accel = com.phonegap.AccelListener.getAcceleration(); - successCallback(accel); - } catch (e) { - console.log("Accelerometer Error in successCallback: " + e); - } - } - - // If accelerometer error - else if (status == Accelerometer.ERROR_FAILED_TO_START) { - clearInterval(timer); - console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]); - try { - if (errorCallback) { - errorCallback(status); - } - } catch (e) { - console.log("Accelerometer Error in errorCallback: " + e); - } - } - }, 10); - } + // Get acceleration + PhoneGap.execAsync(successCallback, errorCallback, "Accelerometer", "getAcceleration", []); }; /** * Asynchronously aquires the acceleration repeatedly at a given interval. * * @param {Function} successCallback The function to call each time the acceleration data is available - * @param {Function} errorCallback The function to call when there is an error getting the acceleration data. - * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. + * @param {Function} errorCallback The function to call when there is an error getting the acceleration data. (OPTIONAL) + * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL) * @return String The watch id that must be passed to #clearWatch to stop watching. */ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options) { @@ -583,40 +525,18 @@ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallb } // Make sure accelerometer timeout > frequency + 10 sec - var timeout = com.phonegap.AccelListener.getTimeout(); - if (timeout < (frequency + 10000)) { - com.phonegap.AccelListener.setTimeout(frequency + 10000); // set to frequency + 10 sec - } - - var id = PhoneGap.createUUID(); - com.phonegap.AccelListener.start(); + PhoneGap.execAsync( + function(timeout) { + if (timeout < (frequency + 10000)) { + PhoneGap.execAsync(null, null, "Accelerometer", "setTimeout", [frequency + 10000]); + } + }, + function(e) { }, "Accelerometer", "getTimeout", []); // Start watch timer + var id = PhoneGap.createUUID(); navigator.accelerometer.timers[id] = setInterval(function() { - var status = com.phonegap.AccelListener.getStatus(); - - // If accelerometer is running - if (status == Accelerometer.RUNNING) { - try { - var accel = com.phonegap.AccelListener.getAcceleration(); - successCallback(accel); - } catch (e) { - console.log("Accelerometer Error in successCallback: " + e); - } - } - - // If accelerometer had error - else if (status == Accelerometer.ERROR_FAILED_TO_START) { - console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]); - try { - navigator.accelerometer.clearWatch(id); - if (errorCallback) { - errorCallback(status); - } - } catch (e) { - console.log("Accelerometer Error in errorCallback: " + e); - } - } + PhoneGap.execAsync(successCallback, errorCallback, "Accelerometer", "getAcceleration", []); }, (frequency ? frequency : 1)); return id; @@ -639,16 +559,6 @@ Accelerometer.prototype.clearWatch = function(id) { PhoneGap.addConstructor(function() { if (typeof navigator.accelerometer == "undefined") navigator.accelerometer = new Accelerometer(); }); -com.phonegap.CameraLauncherProxy = function() { - this.className = "com.phonegap.CameraLauncher"; -}; -com.phonegap.CameraLauncherProxy.prototype.setBase64 = function(b) { - return PhoneGap.exec(this.className, "setBase64", [b]); -}; -com.phonegap.CameraLauncherProxy.prototype.takePicture = function(quality) { - return PhoneGap.exec(this.className, "takePicture", [quality]); -}; -com.phonegap.CameraLauncher = new com.phonegap.CameraLauncherProxy(); /** * This class provides access to the device camera. @@ -685,12 +595,15 @@ Camera.prototype.getPicture = function(successCallback, errorCallback, options) this.successCallback = successCallback; this.errorCallback = errorCallback; this.options = options; + var capturetype = "base64"; + var quality = 80; + if (this.options.capturetype) { + capturetype = this.options.capturetype; + } if (options.quality) { - com.phonegap.CameraLauncher.takePicture(options.quality); - } - else { - com.phonegap.CameraLauncher.takePicture(80); + quality = this.options.quality; } + PhoneGap.execAsync(null, null, "Camera", "takePicture", [quality, capturetype]); }; /** @@ -719,28 +632,6 @@ Camera.prototype.error = function(err) { PhoneGap.addConstructor(function() { if (typeof navigator.camera == "undefined") navigator.camera = new Camera(); }); -com.phonegap.CompassListenerProxy = function() { - this.className = "com.phonegap.CompassListener"; -}; -com.phonegap.CompassListenerProxy.prototype.start = function() { - return PhoneGap.exec(this.className, "start", []); -}; -com.phonegap.CompassListenerProxy.prototype.stop = function() { - return PhoneGap.exec(this.className, "stop", []); -}; -com.phonegap.CompassListenerProxy.prototype.getStatus = function() { - return PhoneGap.exec(this.className, "getStatus", []); -}; -com.phonegap.CompassListenerProxy.prototype.getHeading = function() { - return PhoneGap.exec(this.className, "getHeading", []); -}; -com.phonegap.CompassListenerProxy.prototype.setTimeout = function(timeout) { - return PhoneGap.exec(this.className, "setTimeout", [timeout]); -}; -com.phonegap.CompassListenerProxy.prototype.getTimeout = function() { - return PhoneGap.exec(this.className, "getTimeout", []); -}; -com.phonegap.CompassListener = new com.phonegap.CompassListenerProxy(); /** * This class provides access to device Compass data. @@ -758,18 +649,14 @@ function Compass() { this.timers = {}; }; -Compass.STOPPED = 0; -Compass.STARTING = 1; -Compass.RUNNING = 2; -Compass.ERROR_FAILED_TO_START = 3; Compass.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"]; /** * Asynchronously aquires the current heading. * * @param {Function} successCallback The function to call when the heading data is available - * @param {Function} errorCallback The function to call when there is an error getting the heading data. - * @param {PositionOptions} options The options for getting the heading data such as timeout. + * @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL) + * @param {PositionOptions} options The options for getting the heading data such as timeout. (OPTIONAL) */ Compass.prototype.getCurrentHeading = function(successCallback, errorCallback, options) { @@ -785,61 +672,16 @@ Compass.prototype.getCurrentHeading = function(successCallback, errorCallback, o return; } - // Get current compass status - var status = com.phonegap.CompassListener.getStatus(); - - // If running, then call successCallback - if (status == Compass.RUNNING) { - try { - var heading = com.phonegap.CompassListener.getHeading(); - successCallback(heading); - } catch (e) { - console.log("Compass Error in successCallback: " + e); - } - } - - // If not running, then start it - else { - com.phonegap.CompassListener.start(); - - // Wait until started - var timer = setInterval(function() { - var status = com.phonegap.CompassListener.getStatus(); - if (status != Compass.STARTING) { - clearInterval(timer); - - // If compass is running - if (status == Compass.RUNNING) { - try { - var heading = com.phonegap.CompassListener.getHeading(); - successCallback(heading); - } catch (e) { - console.log("Compass Error in successCallback: " + e); - } - } - - // If compass error - else { - console.log("Compass Error: "+ Compass.ERROR_MSG[status]); - try { - if (errorCallback) { - errorCallback(status); - } - } catch (e) { - console.log("Compass Error in errorCallback: " + e); - } - } - } - }, 10); - } + // Get heading + PhoneGap.execAsync(successCallback, errorCallback, "Compass", "getHeading", []); }; /** * Asynchronously aquires the heading repeatedly at a given interval. * * @param {Function} successCallback The function to call each time the heading data is available - * @param {Function} errorCallback The function to call when there is an error getting the heading data. - * @param {HeadingOptions} options The options for getting the heading data such as timeout and the frequency of the watch. + * @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL) + * @param {HeadingOptions} options The options for getting the heading data such as timeout and the frequency of the watch. (OPTIONAL) * @return String The watch id that must be passed to #clearWatch to stop watching. */ Compass.prototype.watchHeading= function(successCallback, errorCallback, options) { @@ -860,41 +702,20 @@ Compass.prototype.watchHeading= function(successCallback, errorCallback, options } // Make sure compass timeout > frequency + 10 sec - var timeout = com.phonegap.CompassListener.getTimeout(); - if (timeout < (frequency + 10000)) { - com.phonegap.CompassListener.setTimeout(frequency + 10000); // set to frequency + 10 sec - } + PhoneGap.execAsync( + function(timeout) { + if (timeout < (frequency + 10000)) { + PhoneGap.execAsync(null, null, "Compass", "setTimeout", [frequency + 10000]); + } + }, + function(e) { }, "Compass", "getTimeout", []); + // Start watch timer to get headings var id = PhoneGap.createUUID(); - com.phonegap.CompassListener.start(); - - // Start watch timer - navigator.compass.timers[id] = setInterval(function() { - var status = com.phonegap.CompassListener.getStatus(); - - // If compass is running - if (status == Compass.RUNNING) { - try { - var heading = com.phonegap.CompassListener.getHeading(); - successCallback(heading); - } catch (e) { - console.log("Compass Error in successCallback: " + e); - } - } - - // If compass had error - else if (status != Compass.STARTING) { - console.log("Compass Error: "+ Compass.ERROR_MSG[status]); - try { - navigator.compass.clearWatch(id); - if (errorCallback) { - errorCallback(status); - } - } catch (e) { - console.log("Compass Error in errorCallback: " + e); - } - } - }, (frequency ? frequency : 1)); + navigator.compass.timers[id] = setInterval( + function() { + PhoneGap.execAsync(successCallback, errorCallback, "Compass", "getHeading", []); + }, (frequency ? frequency : 1)); return id; }; @@ -917,16 +738,6 @@ Compass.prototype.clearWatch = function(id) { PhoneGap.addConstructor(function() { if (typeof navigator.compass == "undefined") navigator.compass = new Compass(); }); -com.phonegap.ContactManagerProxy = function() { - this.className = "com.phonegap.ContactManager"; -}; -com.phonegap.ContactManagerProxy.prototype.getContactsAndSendBack = function() { - return PhoneGap.exec(this.className, "getContactsAndSendBack", []); -}; -com.phonegap.ContactManagerProxy.prototype.search = function(name, npa, mail) { - return PhoneGap.exec(this.className, "search", [name, npa, mail]); -}; -com.phonegap.ContactManager = new com.phonegap.ContactManagerProxy(); var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, addresses, ims, organizations, published, updated, birthday, anniversary, gender, note, @@ -991,9 +802,9 @@ var ContactOrganization = function(name, dept, title, startDate, endDate, locati }; var ContactAccount = function(domain, username, userid) { - this.domain = domain || ''; - this.username = username || ''; - this.userid = userid || ''; + this.domain = domain || ''; + this.username = username || ''; + this.userid = userid || ''; } var Contacts = function() { @@ -1001,38 +812,27 @@ var Contacts = function() { this.records = []; } -Contacts.prototype.find = function(obj, win, fail) { - if(obj.name != null) { - // Build up the search term that we'll use in SQL, based on the structure/contents of the contact object passed into find. - var searchTerm = ''; - if (obj.name.givenName && obj.name.givenName.length > 0) { - searchTerm = obj.name.givenName.split(' ').join('%'); - } - if (obj.name.familyName && obj.name.familyName.length > 0) { - searchTerm += obj.name.familyName.split(' ').join('%'); - } - if (!obj.name.familyName && !obj.name.givenName && obj.name.formatted) { - searchTerm = obj.name.formatted; - } - com.phonegap.ContactManager.search(searchTerm, "", ""); - } +// Contacts.prototype.find = function(obj, win, fail) { +Contacts.prototype.find = function(fields, win, fail, options) { this.win = win; this.fail = fail; + + PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]); }; Contacts.prototype.droidFoundContact = function(name, npa, email) { - this.records = new Array(); + this.records = new Array(); var contact = new Contact(); contact.name = new ContactName(); contact.name.formatted = name; contact.name.givenName = name; - contact.emails = new Array(); + contact.emails = new Array(); var mail = new ContactField(); mail.type = "home"; mail.value = email; - mail.primary = true; + mail.primary = true; contact.emails.push(mail); - contact.phones = new Array(); + contact.phones = new Array(); phone = new ContactField(); phone.type = "home"; phone.value = npa; @@ -1081,31 +881,21 @@ ContactError.TIMEOUT_ERROR = 6; ContactError.UNKNOWN_ERROR = 7; PhoneGap.addConstructor(function() { - if(typeof navigator.service == "undefined") navigator.service = new Object(); + if(typeof navigator.service == "undefined") navigator.service = new Object(); if(typeof navigator.service.contacts == "undefined") navigator.service.contacts = new Contacts(); }); -com.phonegap.CryptoHandlerProxy = function() { - this.className = "com.phonegap.CryptoHandler"; -}; -com.phonegap.CryptoHandlerProxy.prototype.encrypt = function(pass, text) { - return PhoneGap.exec(this.className, "encrypt", [pass, text]); -}; -com.phonegap.CryptoHandlerProxy.prototype.decrypt = function(pass, text) { - return PhoneGap.exec(this.className, "decrypt", [pass, text]); -}; -com.phonegap.CryptoHandler = new com.phonegap.CryptoHandlerProxy(); var Crypto = function() { }; Crypto.prototype.encrypt = function(seed, string, callback) { - com.phonegap.CryptoHandler.encrypt(seed, string); this.encryptWin = callback; + PhoneGap.execAsync(null, null, "Crypto", "encrypt", [seed, string]); }; Crypto.prototype.decrypt = function(seed, string, callback) { - com.phonegap.CryptoHandler.decrypt(seed, string); this.decryptWin = callback; + PhoneGap.execAsync(null, null, "Crypto", "decrypt", [seed, string]); }; Crypto.prototype.gotCryptedString = function(string) { @@ -1506,31 +1296,6 @@ if (document.keyEvent == null || typeof document.keyEvent == 'undefined') { window.keyEvent = document.keyEvent = new KeyEvent(); } -com.phonegap.AudioHandlerProxy = function() { - this.className = "com.phonegap.AudioHandler"; -}; -com.phonegap.AudioHandlerProxy.prototype.startRecordingAudio = function(id, file) { - return PhoneGap.exec(this.className, "startRecordingAudio", [id, file]); -}; -com.phonegap.AudioHandlerProxy.prototype.stopRecordingAudio = function(id) { - return PhoneGap.exec(this.className, "stopRecordingAudio", [id]); -}; -com.phonegap.AudioHandlerProxy.prototype.startPlayingAudio = function(id, file) { - return PhoneGap.exec(this.className, "startPlayingAudio", [id, file]); -}; -com.phonegap.AudioHandlerProxy.prototype.pausePlayingAudio = function(id) { - return PhoneGap.exec(this.className, "pausePlayingAudio", [id]); -}; -com.phonegap.AudioHandlerProxy.prototype.stopPlayingAudio = function(id) { - return PhoneGap.exec(this.className, "stopPlayingAudio", [id]); -}; -com.phonegap.AudioHandlerProxy.prototype.getCurrentPositionAudio = function(id) { - return PhoneGap.exec(this.className, "getCurrentPositionAudio", [id]); -}; -com.phonegap.AudioHandlerProxy.prototype.getDurationAudio = function(id, file) { - return PhoneGap.exec(this.className, "getDurationAudio", [id, file]); -}; -com.phonegap.AudioHandler = new com.phonegap.AudioHandlerProxy(); /** * List of media objects. @@ -1596,8 +1361,10 @@ PhoneGap.Media.onStatus = function(id, msg, value) { * errorCallback(int errorCode) - OPTIONAL * @param statusCallback The callback to be called when media status has changed. * statusCallback(int statusCode) - OPTIONAL + * @param positionCallback The callback to be called when media position has changed. + * positionCallback(long position) - OPTIONAL */ -Media = function(src, successCallback, errorCallback, statusCallback) { +Media = function(src, successCallback, errorCallback, statusCallback, positionCallback) { // successCallback optional if (successCallback && (typeof successCallback != "function")) { @@ -1617,19 +1384,27 @@ Media = function(src, successCallback, errorCallback, statusCallback) { return; } + // statusCallback optional + if (positionCallback && (typeof positionCallback != "function")) { + console.log("Media Error: positionCallback is not a function"); + return; + } + this.id = PhoneGap.createUUID(); PhoneGap.mediaObjects[this.id] = this; this.src = src; this.successCallback = successCallback; this.errorCallback = errorCallback; this.statusCallback = statusCallback; + this.positionCallback = positionCallback; this._duration = -1; + this._position = -1; }; // Media messages Media.MEDIA_STATE = 1; Media.MEDIA_DURATION = 2; -Media.MEDIA_ERROR = 3; +Media.MEDIA_ERROR = 9; // Media states Media.MEDIA_NONE = 0; @@ -1658,21 +1433,21 @@ MediaError.MEDIA_ERR_NONE_SUPPORTED = 4; * Start or resume playing audio file. */ Media.prototype.play = function() { - com.phonegap.AudioHandler.startPlayingAudio(this.id, this.src); + PhoneGap.execAsync(null, null, "Media", "startPlayingAudio", [this.id, this.src]); }; /** * Stop playing audio file. */ Media.prototype.stop = function() { - com.phonegap.AudioHandler.stopPlayingAudio(this.id); + return PhoneGap.execAsync(null, null, "Media", "stopPlayingAudio", [this.id]); }; /** * Pause playing audio file. */ Media.prototype.pause = function() { - com.phonegap.AudioHandler.pausePlayingAudio(this.id); + PhoneGap.execAsync(null, null, "Media", "pausePlayingAudio", [this.id]); }; /** @@ -1690,37 +1465,24 @@ Media.prototype.getDuration = function() { * * @return */ -Media.prototype.getCurrentPosition = function() { - return com.phonegap.AudioHandler.getCurrentPositionAudio(this.id); +Media.prototype.getCurrentPosition = function(success, fail) { + PhoneGap.execAsync(success, fail, "Media", "getCurrentPositionAudio", [this.id]); }; /** * Start recording audio file. */ Media.prototype.startRecord = function() { - com.phonegap.AudioHandler.startRecordingAudio(this.id, this.src); + PhoneGap.execAsync(null, null, "Media", "startRecordingAudio", [this.id, this.src]); }; /** * Stop recording audio file. */ Media.prototype.stopRecord = function() { - com.phonegap.AudioHandler.stopRecordingAudio(this.id); + PhoneGap.execAsync(null, null, "Media", "stopRecordingAudio", [this.id]); }; -com.phonegap.NetworkManagerProxy = function() { - this.className = "com.phonegap.NetworkManager"; -}; -com.phonegap.NetworkManagerProxy.prototype.isAvailable = function() { - return PhoneGap.exec(this.className, "isAvailable", []); -}; -com.phonegap.NetworkManagerProxy.prototype.isWifiActive = function() { - return PhoneGap.exec(this.className, "isWifiActive", []); -}; -com.phonegap.NetworkManagerProxy.prototype.isReachable = function(uri) { - return PhoneGap.exec(this.className, "isReachable", [uri]); -}; -com.phonegap.NetworkManager = new com.phonegap.NetworkManagerProxy(); /** * This class contains information about any NetworkStatus. @@ -1752,35 +1514,58 @@ function Network() { * Called by the geolocation framework when the reachability status has changed. * @param {Reachibility} reachability The current reachability status. */ +// TODO: Callback from native code not implemented for Android Network.prototype.updateReachability = function(reachability) { this.lastReachability = reachability; }; /** - * + * Determine if a URI is reachable over the network. + * @param {Object} uri - * @param {Function} win + * @param {Function} callback * @param {Object} options (isIpAddress:boolean) */ -Network.prototype.isReachable = function(uri, win, options) { - var status = new NetworkStatus(); - if(com.phonegap.NetworkManager.isReachable(uri)) { - if (com.phonegap.NetworkManager.isWifiActive()) { - status.code = NetworkStatus.REACHABLE_VIA_WIFI_NETWORK; - } - else { - status.code = NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK; - } +Network.prototype.isReachable = function(uri, callback, options) { + + // callback required + if (typeof callback != "function") { + console.log("Network Error: callback is not a function"); + return; } - else { - status.code = NetworkStatus.NOT_REACHABLE; - } - win(status); + + PhoneGap.execAsync( + function(status) { + + // If reachable, the check for wifi vs carrier + if (status) { + PhoneGap.execAsync( + function(wifi) { + var s = new NetworkStatus(); + if (wifi) { + s.code = NetworkStatus.REACHABLE_VIA_WIFI_NETWORK; + } + else { + s.code = NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK; + } + callback(s); + }, null, "Network Status", "isWifiActive", []); + } + + // If not + else { + var s = new NetworkStatus(); + s.code = NetworkStatus.NOT_REACHABLE; + callback(s); + } + }, null, "Network Status", "isReachable", [uri]); }; PhoneGap.addConstructor(function() { if (typeof navigator.network == "undefined") navigator.network = new Network(); -});/** +}); + +/** * This class provides access to notifications on the device. */ function Notification() { @@ -1937,17 +1722,7 @@ PhoneGap.addConstructor(function() { if (typeof navigator.splashScreen == "undefined") { navigator.splashScreen = SplashScreen; // SplashScreen object come from native side through addJavaScriptInterface } -});com.phonegap.StorageProxy = function() { - this.className = "com.phonegap.Storage"; -}; -com.phonegap.StorageProxy.prototype.executeSql = function(query, params, id) { - return PhoneGap.exec(this.className, "executeSql", [query, params, id]); -}; -com.phonegap.StorageProxy.prototype.openDatabase = function(name, version, display_name, size) { - return PhoneGap.exec(this.className, "openDatabase", [name, version, display_name, size]); -}; -com.phonegap.Storage = new com.phonegap.StorageProxy(); - +}); /* * This is purely for the Android 1.5/1.6 HTML 5 Storage * I was hoping that Android 2.0 would deprecate this, but given the fact that @@ -1992,7 +1767,7 @@ var Tx = function() { }; Tx.prototype.executeSql = function(query, params, win, fail) { - com.phonegap.Storage.executeSql(query, params, this.id); + PhoneGap.execAsync(null, null, "Storage", "executeSql", [query, params, this.id]); tx.win = win; tx.fail = fail; }; @@ -2011,7 +1786,7 @@ Rows.prototype.item = function(row_id) { }; var dbSetup = function(name, version, display_name, size) { - com.phonegap.Storage.openDatabase(name, version, display_name, size) + PhoneGap.execAsync(null, null, "Storage", "openDatabase", [name, version, display_name, size]); db_object = new DatabaseShell(); return db_object; }; diff --git a/framework/src/com/phonegap/ContactAccessor.java b/framework/src/com/phonegap/ContactAccessor.java index 8f12f635..867fe5e1 100644 --- a/framework/src/com/phonegap/ContactAccessor.java +++ b/framework/src/com/phonegap/ContactAccessor.java @@ -21,12 +21,11 @@ package com.phonegap; import java.lang.reflect.Constructor; import android.app.Activity; -import android.content.ContentResolver; -import android.content.Intent; -import android.net.Uri; -import android.os.Build; import android.webkit.WebView; +import org.json.JSONArray; +import org.json.JSONObject; + /** * This abstract class defines SDK-independent API for communication with * Contacts Provider. The actual implementation used by the application depends @@ -94,5 +93,5 @@ public abstract class ContactAccessor { /** * Handles searching through SDK-specific contacts API. */ - public abstract void search(String name, String npa, String email); + public abstract void search(JSONArray filter, JSONObject options); } \ No newline at end of file diff --git a/framework/src/com/phonegap/ContactAccessorSdk3_4.java b/framework/src/com/phonegap/ContactAccessorSdk3_4.java index ec4a4d40..acdbd8af 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk3_4.java +++ b/framework/src/com/phonegap/ContactAccessorSdk3_4.java @@ -17,6 +17,9 @@ package com.phonegap; +import org.json.JSONArray; +import org.json.JSONObject; + import android.app.Activity; import android.content.AsyncQueryHandler; import android.database.Cursor; @@ -54,11 +57,12 @@ public class ContactAccessorSdk3_4 extends ContactAccessor { } @Override - public void search(String name, String npa, String email) { - if (email.length() > 0) - searchByEmail(email); - else - searchPeople(name, npa); + public void search(JSONArray filter, JSONObject options) { + //if (email.length() > 0) + // searchByEmail(email); + //else + // searchPeople(name, npa); + searchPeople("", ""); } private void searchByEmail(String email) diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index e9353e76..c16bc73a 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -17,6 +17,10 @@ package com.phonegap; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; @@ -57,8 +61,15 @@ public class ContactAccessorSdk5 extends ContactAccessor { } @Override - public void search(String name, String npa, String email) { - if (name.length()==0) name = "%"; + public void search(JSONArray filter, JSONObject options) { + String searchTerm = ""; + try { + searchTerm = options.getString("filter"); + if (searchTerm.length()==0) searchTerm = "%"; + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } // Get a cursor by creating the query. // TODO: parse name/number/email and dispatch to different query types. // Right now assumption is only name search. Lame but I'm on time constraints. @@ -67,7 +78,7 @@ public class ContactAccessorSdk5 extends ContactAccessor { ContactsContract.Contacts.CONTENT_URI, new String[] {ContactsContract.Contacts._ID, ContactsContract.Contacts.HAS_PHONE_NUMBER, ContactsContract.Contacts.DISPLAY_NAME}, ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?", - new String[] {name}, + new String[] {searchTerm}, ContactsContract.Contacts.DISPLAY_NAME + " ASC"); while (cursor.moveToNext()) { String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); diff --git a/framework/src/com/phonegap/ContactManager.java b/framework/src/com/phonegap/ContactManager.java index d498fb82..6a915740 100755 --- a/framework/src/com/phonegap/ContactManager.java +++ b/framework/src/com/phonegap/ContactManager.java @@ -2,29 +2,19 @@ package com.phonegap; import org.json.JSONArray; import org.json.JSONException; +import org.json.JSONObject; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; -import android.provider.Contacts.ContactMethods; -import android.provider.Contacts.People; import android.util.Log; import android.webkit.WebView; import android.content.Intent; import android.net.Uri; -import android.database.Cursor; -import android.database.sqlite.SQLiteException; @SuppressWarnings("deprecation") public class ContactManager implements Plugin { -// public class ContactTriplet -// { -// public String name = ""; -// public String email = ""; -// public String phone = ""; -// } - private static ContactAccessor contactAccessor; WebView webView; // WebView object DroidGap ctx; // DroidGap object @@ -81,7 +71,12 @@ public class ContactManager implements Plugin { //else if (action.equals("search")) { if (action.equals("search")) { Log.d(LOG_TAG, "Executing search using accessor"); - contactAccessor.search(args.getString(0), args.getString(1), args.getString(2)); + JSONArray fields = args.getJSONArray(0); + for (int i=0; i 0) -// searchByEmail(email); -// else -// searchPeople(name, npa); -// } -// -// private void searchByEmail(String email) -// { -// String[] projection = new String[] { -// ContactMethods._ID, -// ContactMethods.DATA, -// ContactMethods.KIND, -// ContactMethods.PERSON_ID -// }; -// String[] variables = new String[] { -// email -// }; -// -// try{ -// Cursor myCursor = this.ctx.managedQuery(mEmail, projection, -// "contact_methods." + ContactMethods.DATA + " = ?" + "AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC"); -// getMethodData(myCursor); -// -// } -// catch (SQLiteException ex) -// { -// Log.d(LOG_TAG, ex.getMessage()); -// } -// -// } -// -// private void searchPeople(String name, String number) -// { -// String conditions = ""; -// -// if (name.length() == 0) -// { -// name = "%"; -// conditions += People.NAME + " LIKE ? AND "; -// } -// else -// { -// conditions += People.NAME + " = ? AND "; -// } -// -// if (number.length() == 0) -// number = "%"; -// else -// { -// number = number.replace('+', '%'); -// number = number.replace('.', '%'); -// number = number.replace('-', '%'); -// } -// -// conditions += People.NUMBER + " LIKE ? "; -// -// String[] projection = new String[] { -// People._ID, -// People.NAME, -// People.NUMBER, -// People.PRIMARY_EMAIL_ID -// }; -// -// String[] variables = new String[] { -// name, number -// }; -// -// try{ -// Cursor myCursor = this.ctx.managedQuery(mPeople, projection, -// conditions, variables , People.NAME + " ASC"); -// processResults(myCursor, false); -// } -// catch (SQLiteException ex) -// { -// Log.d(LOG_TAG, ex.getMessage()); -// } -// -// } -// -// private void processResults(Cursor cur, boolean all){ -// -// if (cur.moveToFirst()) { -// -// String name; -// String phoneNumber; -// String email_id; -// String email; -// -// int nameColumn = cur.getColumnIndex(People.NAME); -// int phoneColumn = cur.getColumnIndex(People.NUMBER); -// int emailIdColumn = cur.getColumnIndex(People.PRIMARY_EMAIL_ID); -// -// do { -// // Get the field values -// name = cur.getString(nameColumn); -// phoneNumber = cur.getString(phoneColumn); -// email_id = cur.getString(emailIdColumn); -// if (email_id != null && email_id.length() > 0) -// email = getEmail(email_id); -// else -// email = ""; -// -// // Code for backwards compatibility with the OLD Contacts API -// if (all) { -// this.ctx.sendJavascript("navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"');"); -// } -// else { -// this.ctx.sendJavascript("navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"');"); -// } -// } while (cur.moveToNext()); -// if (all) { -// this.ctx.sendJavascript("navigator.ContactManager.droidDone();"); -// } -// else { -// this.ctx.sendJavascript("navigator.contacts.droidDone();"); -// } -// } -// else -// { -// if (all) { -// this.ctx.sendJavascript("navigator.ContactManager.fail();"); -// } -// else { -// this.ctx.sendJavascript("navigator.contacts.fail('None found!');"); -// } -// } -// } -// -// private void getMethodData(Cursor cur) -// { -// ContactTriplet data = new ContactTriplet(); -// String id; -// String email; -// -// if (cur.moveToFirst()) { -// -// int idColumn = cur.getColumnIndex(ContactMethods._ID); -// int emailColumn = cur.getColumnIndex(ContactMethods.DATA); -// do { -// // Get the field values -// id = cur.getString(idColumn); -// email = cur.getString(emailColumn); -// -// data = getContactData(id); -// if(data != null) -// { -// data.email = email; -// this.ctx.sendJavascript("navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"');"); -// } -// } while (cur.moveToNext()); -// this.ctx.sendJavascript("navigator.contacts.droidDoneContacts();"); -// } -// } -// -// private ContactTriplet getContactData(String id) { -// ContactTriplet data = null; -// String[] projection = new String[] { -// People._ID, -// People.NAME, -// People.NUMBER, -// People.PRIMARY_EMAIL_ID -// }; -// -// String[] variables = new String[] { -// id -// }; -// -// try{ -// Cursor myCursor = this.ctx.managedQuery(mPeople, projection, -// People.PRIMARY_EMAIL_ID + " = ?", variables , People.NAME + " ASC"); -// data = getTriplet(myCursor); -// } -// catch (SQLiteException ex) -// { -// Log.d(LOG_TAG, ex.getMessage()); -// } -// -// return data; -// } -// -// private ContactTriplet getTriplet(Cursor cur) { -// ContactTriplet data = new ContactTriplet(); -// if (cur.moveToFirst()) { -// -// int nameColumn = cur.getColumnIndex(People.NAME); -// int numberColumn = cur.getColumnIndex(People.NUMBER); -// do { -// -// data.name = cur.getString(nameColumn); -// data.phone = cur.getString(numberColumn); -// -// } while (cur.moveToNext()); -// } -// return data; -// } -// -// private String getEmailColumnData(Cursor cur) -// { -// String email = ""; -// if (cur != null && cur.moveToFirst()) { -// int emailColumn = cur.getColumnIndex(ContactMethods.DATA); -// do { -// // Get the field values -// email = cur.getString(emailColumn); -// } while (cur.moveToNext()); -// } -// return email; -// } -// -// private String getEmail(String id) -// { -// String email = ""; -// String[] projection = new String[] { -// ContactMethods._ID, -// ContactMethods.DATA, -// ContactMethods.KIND -// }; -// String[] variables = new String[] { -// id -// }; -// -// try -// { -// Cursor myCursor = this.ctx.managedQuery(mEmail, projection, -// "contact_methods." + ContactMethods._ID + " = ?" + " AND contact_methods.kind = 1", variables , ContactMethods.DATA + " ASC"); -// email = getEmailColumnData(myCursor); -// } -// catch (SQLiteException ex) -// { -// Log.d(LOG_TAG, ex.getMessage()); -// } -// -// return email; -// } - - }