From 297ddb99fecf765a97699247360469c341671787 Mon Sep 17 00:00:00 2001 From: macdonst Date: Thu, 30 Sep 2010 23:51:07 +0800 Subject: [PATCH 01/11] Adding clone functionality to Contact object --- framework/assets/js/contact.js | 26 ++++++----- framework/assets/js/phonegap.js.base | 36 +++++++++++++++ framework/assets/www/phonegap.js | 67 +++++++++++++++++++++++----- 3 files changed, 107 insertions(+), 22 deletions(-) diff --git a/framework/assets/js/contact.js b/framework/assets/js/contact.js index 1d5eb4c0..d5f0529b 100644 --- a/framework/assets/js/contact.js +++ b/framework/assets/js/contact.js @@ -27,6 +27,20 @@ var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, ad this.connected = connected || null; }; + +Contact.prototype.remove = function(contact) { +}; + +Contact.prototype.clone = function() { + var clonedContact = PhoneGap.clone(this); + clonedContact.id = null; + return clonedContact; +}; + +Contact.prototype.save = function(contact) { +}; + + var ContactName = function(formatted, familyName, givenName, middle, prefix, suffix) { this.formatted = formatted || null; this.familyName = familyName || null; @@ -72,7 +86,6 @@ var Contacts = function() { this.records = new Array(); } -// Contacts.prototype.find = function(obj, win, fail) { Contacts.prototype.find = function(fields, win, fail, options) { this.win = win; this.fail = fail; @@ -84,16 +97,9 @@ Contacts.prototype.droidDone = function(contacts) { this.win(eval('(' + contacts + ')')); }; -Contacts.prototype.remove = function(contact) { - -}; - -Contacts.prototype.save = function(contact) { - -}; - +// This function does not create a new contact in the db. +// Must call contact.save() for it to be persisted in the db. Contacts.prototype.create = function(contact) { - }; Contacts.prototype.m_foundContacts = function(win, contacts) { diff --git a/framework/assets/js/phonegap.js.base b/framework/assets/js/phonegap.js.base index 0c31d018..ab71d3d7 100755 --- a/framework/assets/js/phonegap.js.base +++ b/framework/assets/js/phonegap.js.base @@ -324,6 +324,42 @@ PhoneGap.stringify = function(args) { } }; +/** + * Does a deep clone of the object. + * + * @param obj + * @return + */ +PhoneGap.clone = function(obj) { + if(!obj) { + return obj; + } + + if(obj instanceof Array){ + var retVal = new Array(); + for(var i = 0; i < obj.length; ++i){ + retVal.push(PhoneGap.clone(obj[i])); + } + return retVal; + } + + if (obj instanceof Function) { + return obj; + } + + if(!(obj instanceof Object)){ + return obj; + } + + retVal = new Object(); + for(i in obj){ + if(!(i in retVal) || retVal[i] != obj[i]) { + retVal[i] = PhoneGap.clone(obj[i]); + } + } + return retVal; +}; + PhoneGap.callbackId = 0; PhoneGap.callbacks = {}; diff --git a/framework/assets/www/phonegap.js b/framework/assets/www/phonegap.js index 94a90752..5c047c2f 100644 --- a/framework/assets/www/phonegap.js +++ b/framework/assets/www/phonegap.js @@ -324,6 +324,42 @@ PhoneGap.stringify = function(args) { } }; +/** + * Does a deep clone of the object. + * + * @param obj + * @return + */ +PhoneGap.clone = function(obj) { + if(!obj) { + return obj; + } + + if(obj instanceof Array){ + var retVal = new Array(); + for(var i = 0; i < obj.length; ++i){ + retVal.push(PhoneGap.clone(obj[i])); + } + return retVal; + } + + if (obj instanceof Function) { + return obj; + } + + if(!(obj instanceof Object)){ + return obj; + } + + retVal = new Object(); + for(i in obj){ + if(!(i in retVal) || retVal[i] != obj[i]) { + retVal[i] = PhoneGap.clone(obj[i]); + } + } + return retVal; +}; + PhoneGap.callbackId = 0; PhoneGap.callbacks = {}; @@ -940,6 +976,21 @@ var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, ad this.connected = connected || null; }; + +Contact.prototype.remove = function(contact) { +}; + +Contact.prototype.clone = function() { + console.log("PhoneGap clone version 2"); + var clonedContact = PhoneGap.clone(this); + clonedContact.id = null; + return clonedContact; +}; + +Contact.prototype.save = function(contact) { +}; + + var ContactName = function(formatted, familyName, givenName, middle, prefix, suffix) { this.formatted = formatted || null; this.familyName = familyName || null; @@ -985,7 +1036,6 @@ var Contacts = function() { this.records = new Array(); } -// Contacts.prototype.find = function(obj, win, fail) { Contacts.prototype.find = function(fields, win, fail, options) { this.win = win; this.fail = fail; @@ -997,16 +1047,9 @@ Contacts.prototype.droidDone = function(contacts) { this.win(eval('(' + contacts + ')')); }; -Contacts.prototype.remove = function(contact) { - -}; - -Contacts.prototype.save = function(contact) { - -}; - +// This function does not create a new contact in the db. +// Must call contact.save() for it to be persisted in the db. Contacts.prototype.create = function(contact) { - }; Contacts.prototype.m_foundContacts = function(win, contacts) { @@ -2050,7 +2093,7 @@ Notification.prototype.blink = function(count, colour) { * @param {Integer} mills The number of milliseconds to vibrate for. */ Notification.prototype.vibrate = function(mills) { - PhoneGap.execAsync(null, null, "Device", "vibrate", [mills]); + PhoneGap.execAsync(null, null, "Notification", "vibrate", [mills]); }; /** @@ -2060,7 +2103,7 @@ Notification.prototype.vibrate = function(mills) { * @param {Integer} count The number of beeps. */ Notification.prototype.beep = function(count) { - PhoneGap.execAsync(null, null, "Device", "beep", [count]); + PhoneGap.execAsync(null, null, "Notification", "beep", [count]); }; // TODO: of course on Blackberry and Android there notifications in the UI as well From 668bc9e0caced58e1aaca2a365848324ce39b8aa Mon Sep 17 00:00:00 2001 From: Justin Tyberg Date: Fri, 1 Oct 2010 09:52:21 +0800 Subject: [PATCH 02/11] Corrected check for existence of accelerometer timer to allow clearWatch to clearInterval correctly. --- framework/assets/js/accelerometer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/assets/js/accelerometer.js b/framework/assets/js/accelerometer.js index 3923a56a..f87cd082 100755 --- a/framework/assets/js/accelerometer.js +++ b/framework/assets/js/accelerometer.js @@ -102,7 +102,7 @@ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallb Accelerometer.prototype.clearWatch = function(id) { // Stop javascript timer & remove from timer list - if (id && navigator.accelerometer.timers[id]) { + if (id && navigator.accelerometer.timers[id] != undefined) { clearInterval(navigator.accelerometer.timers[id]); delete navigator.accelerometer.timers[id]; } From 6071b9c75a581dfb9490273ba715042e2ad5d44f Mon Sep 17 00:00:00 2001 From: macdonst Date: Fri, 1 Oct 2010 11:09:59 +0800 Subject: [PATCH 03/11] Adding Contact.remove method --- framework/assets/js/contact.js | 23 +++++++++++------- framework/assets/www/phonegap.js | 24 ++++++++++++------- .../src/com/phonegap/ContactAccessor.java | 10 ++++++++ .../com/phonegap/ContactAccessorSdk3_4.java | 19 +++++++++++++++ .../src/com/phonegap/ContactAccessorSdk5.java | 18 ++++++++++++++ .../src/com/phonegap/ContactManager.java | 10 +++++++- 6 files changed, 86 insertions(+), 18 deletions(-) diff --git a/framework/assets/js/contact.js b/framework/assets/js/contact.js index d5f0529b..02bfc7f4 100644 --- a/framework/assets/js/contact.js +++ b/framework/assets/js/contact.js @@ -28,7 +28,14 @@ var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, ad }; -Contact.prototype.remove = function(contact) { +Contact.prototype.remove = function(successCB, errorCB) { + if (this.id == null) { + var errorObj = new ContactError(); + errorObj.code = ContactError.NOT_FOUND_ERROR; + errorCB(errorObj); + } + + PhoneGap.execAsync(successCB, errorCB, "Contacts", "remove", [this.id]); }; Contact.prototype.clone = function() { @@ -37,7 +44,7 @@ Contact.prototype.clone = function() { return clonedContact; }; -Contact.prototype.save = function(contact) { +Contact.prototype.save = function(win, fail) { }; @@ -118,14 +125,14 @@ var ContactError = function() { this.code=null; }; -ContactError.INVALID_ARGUMENT_ERROR = 0; -ContactError.IO_ERROR = 1; +ContactError.UNKNOWN_ERROR = 0; +ContactError.INVALID_ARGUMENT_ERROR = 1; ContactError.NOT_FOUND_ERROR = 2; -ContactError.NOT_SUPPORTED_ERROR = 3; +ContactError.TIMEOUT_ERROR = 3; ContactError.PENDING_OPERATION_ERROR = 4; -ContactError.PERMISSION_DENIED_ERROR = 5; -ContactError.TIMEOUT_ERROR = 6; -ContactError.UNKNOWN_ERROR = 7; +ContactError.IO_ERROR = 5; +ContactError.NOT_SUPPORTED_ERROR = 6; +ContactError.PERMISSION_DENIED_ERROR = 20; PhoneGap.addConstructor(function() { if(typeof navigator.service == "undefined") navigator.service = new Object(); diff --git a/framework/assets/www/phonegap.js b/framework/assets/www/phonegap.js index 5c047c2f..9b3f3955 100644 --- a/framework/assets/www/phonegap.js +++ b/framework/assets/www/phonegap.js @@ -977,17 +977,23 @@ var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, ad }; -Contact.prototype.remove = function(contact) { +Contact.prototype.remove = function(successCB, errorCB) { + if (this.id == null) { + var errorObj = new ContactError(); + errorObj.code = ContactError.NOT_FOUND_ERROR; + errorCB(errorObj); + } + + PhoneGap.execAsync(successCB, errorCB, "Contacts", "remove", [this.id]); }; Contact.prototype.clone = function() { - console.log("PhoneGap clone version 2"); var clonedContact = PhoneGap.clone(this); clonedContact.id = null; return clonedContact; }; -Contact.prototype.save = function(contact) { +Contact.prototype.save = function(win, fail) { }; @@ -1068,14 +1074,14 @@ var ContactError = function() { this.code=null; }; -ContactError.INVALID_ARGUMENT_ERROR = 0; -ContactError.IO_ERROR = 1; +ContactError.UNKNOWN_ERROR = 0; +ContactError.INVALID_ARGUMENT_ERROR = 1; ContactError.NOT_FOUND_ERROR = 2; -ContactError.NOT_SUPPORTED_ERROR = 3; +ContactError.TIMEOUT_ERROR = 3; ContactError.PENDING_OPERATION_ERROR = 4; -ContactError.PERMISSION_DENIED_ERROR = 5; -ContactError.TIMEOUT_ERROR = 6; -ContactError.UNKNOWN_ERROR = 7; +ContactError.IO_ERROR = 5; +ContactError.NOT_SUPPORTED_ERROR = 6; +ContactError.PERMISSION_DENIED_ERROR = 20; PhoneGap.addConstructor(function() { if(typeof navigator.service == "undefined") navigator.service = new Object(); diff --git a/framework/src/com/phonegap/ContactAccessor.java b/framework/src/com/phonegap/ContactAccessor.java index ac03c414..0fc02b41 100644 --- a/framework/src/com/phonegap/ContactAccessor.java +++ b/framework/src/com/phonegap/ContactAccessor.java @@ -83,8 +83,18 @@ public abstract class ContactAccessor { return sInstance; } + /** + * Handles adding a JSON Contact object into the database. + */ + public abstract void save(); + /** * Handles searching through SDK-specific contacts API. */ public abstract void search(JSONArray filter, JSONObject options); + + /** + * Handles removing a contact from the database. + */ + public abstract boolean remove(String id); } \ 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 086e1052..ce608db9 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk3_4.java +++ b/framework/src/com/phonegap/ContactAccessorSdk3_4.java @@ -31,6 +31,7 @@ import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; +import android.provider.ContactsContract; import android.provider.Contacts.ContactMethods; import android.provider.Contacts.ContactMethodsColumns; import android.provider.Contacts.Organizations; @@ -359,4 +360,22 @@ public class ContactAccessorSdk3_4 extends ContactAccessor { } return emails; } + + @Override + public void save() { + // TODO Auto-generated method stub + + } + + @Override + public boolean remove(String id) { + ContentResolver cr = mApp.getContentResolver(); + + int result = cr.delete(People.CONTENT_URI, "people._id = ?", new String[] {id}); + Log.d(LOG_TAG, "Content URI = " + People.CONTENT_URI); + Log.d(LOG_TAG, "Where = " + "people._id = ?"); + Log.d(LOG_TAG, "Number of rows deleted = " + result); + + return (result > 0) ? true : false; + } } \ No newline at end of file diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index bcad148f..84117569 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -549,5 +549,23 @@ public class ContactAccessorSdk5 extends ContactAccessor { } cursor.close(); return retVal; + } + + @Override + public void save() { + // TODO Auto-generated method stub + } + + @Override + public boolean remove(String id) { + ContentResolver cr = mApp.getContentResolver(); + int result = cr.delete(ContactsContract.Data.CONTENT_URI, + ContactsContract.Data.CONTACT_ID + " = ?", + new String[] {id}); + Log.d(LOG_TAG, "Content URI = " + ContactsContract.Data.CONTENT_URI); + Log.d(LOG_TAG, "Where = " + ContactsContract.Data.CONTACT_ID + " = ?"); + Log.d(LOG_TAG, "Number of rows deleted = " + result); + + return (result > 0) ? true : false; } } \ No newline at end of file diff --git a/framework/src/com/phonegap/ContactManager.java b/framework/src/com/phonegap/ContactManager.java index 9cf6a891..38cc899e 100755 --- a/framework/src/com/phonegap/ContactManager.java +++ b/framework/src/com/phonegap/ContactManager.java @@ -2,6 +2,7 @@ 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; @@ -69,7 +70,14 @@ public class ContactManager implements Plugin { // TODO Coming soon! } else if (action.equals("remove")) { - // TODO Coming soon! + if (contactAccessor.remove(args.getString(0))) { + return new PluginResult(status, result); + } + else { + JSONObject r = new JSONObject(); + r.put("code", 2); + return new PluginResult(PluginResult.Status.ERROR, r); + } } return new PluginResult(status, result); } catch (JSONException e) { From 9671083bedcdbeb246590a2946bbac813592bc97 Mon Sep 17 00:00:00 2001 From: macdonst Date: Fri, 1 Oct 2010 11:28:31 +0800 Subject: [PATCH 04/11] Removing logging messages --- framework/assets/www/phonegap.js | 2 +- framework/src/com/phonegap/ContactAccessorSdk3_4.java | 9 +++------ framework/src/com/phonegap/ContactAccessorSdk5.java | 9 ++------- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/framework/assets/www/phonegap.js b/framework/assets/www/phonegap.js index 9b3f3955..20eba86f 100644 --- a/framework/assets/www/phonegap.js +++ b/framework/assets/www/phonegap.js @@ -723,7 +723,7 @@ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallb Accelerometer.prototype.clearWatch = function(id) { // Stop javascript timer & remove from timer list - if (id && navigator.accelerometer.timers[id]) { + if (id && navigator.accelerometer.timers[id] != undefined) { clearInterval(navigator.accelerometer.timers[id]); delete navigator.accelerometer.timers[id]; } diff --git a/framework/src/com/phonegap/ContactAccessorSdk3_4.java b/framework/src/com/phonegap/ContactAccessorSdk3_4.java index ce608db9..7e43ec2a 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk3_4.java +++ b/framework/src/com/phonegap/ContactAccessorSdk3_4.java @@ -369,12 +369,9 @@ public class ContactAccessorSdk3_4 extends ContactAccessor { @Override public boolean remove(String id) { - ContentResolver cr = mApp.getContentResolver(); - - int result = cr.delete(People.CONTENT_URI, "people._id = ?", new String[] {id}); - Log.d(LOG_TAG, "Content URI = " + People.CONTENT_URI); - Log.d(LOG_TAG, "Where = " + "people._id = ?"); - Log.d(LOG_TAG, "Number of rows deleted = " + result); + int result = mApp.getContentResolver().delete(People.CONTENT_URI, + "people._id = ?", + new String[] {id}); return (result > 0) ? true : false; } diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index 84117569..a99a6d7e 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -558,14 +558,9 @@ public class ContactAccessorSdk5 extends ContactAccessor { @Override public boolean remove(String id) { - ContentResolver cr = mApp.getContentResolver(); - int result = cr.delete(ContactsContract.Data.CONTENT_URI, + int result = mApp.getContentResolver().delete(ContactsContract.Data.CONTENT_URI, ContactsContract.Data.CONTACT_ID + " = ?", - new String[] {id}); - Log.d(LOG_TAG, "Content URI = " + ContactsContract.Data.CONTENT_URI); - Log.d(LOG_TAG, "Where = " + ContactsContract.Data.CONTACT_ID + " = ?"); - Log.d(LOG_TAG, "Number of rows deleted = " + result); - + new String[] {id}); return (result > 0) ? true : false; } } \ No newline at end of file From c91ea37438a07583fd8779ddf2b8c1b3e767b844 Mon Sep 17 00:00:00 2001 From: macdonst Date: Sat, 2 Oct 2010 04:55:20 +0800 Subject: [PATCH 05/11] Adding Contacts.create method --- framework/assets/js/contact.js | 10 ++++++++++ framework/assets/www/phonegap.js | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/framework/assets/js/contact.js b/framework/assets/js/contact.js index 02bfc7f4..a4c13669 100644 --- a/framework/assets/js/contact.js +++ b/framework/assets/js/contact.js @@ -100,6 +100,16 @@ Contacts.prototype.find = function(fields, win, fail, options) { PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]); }; +Contacts.prototype.wacky = function(properties) { + var contact = new Contact(); + for (i in properties) { + if (contact[i]!='undefined') { + contact[i]=properties[i]; + } + } + return contact; +}; + Contacts.prototype.droidDone = function(contacts) { this.win(eval('(' + contacts + ')')); }; diff --git a/framework/assets/www/phonegap.js b/framework/assets/www/phonegap.js index 20eba86f..f581a56f 100644 --- a/framework/assets/www/phonegap.js +++ b/framework/assets/www/phonegap.js @@ -1049,6 +1049,16 @@ Contacts.prototype.find = function(fields, win, fail, options) { PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]); }; +Contacts.prototype.wacky = function(properties) { + var contact = new Contact(); + for (i in properties) { + if (contact[i]!='undefined') { + contact[i]=properties[i]; + } + } + return contact; +}; + Contacts.prototype.droidDone = function(contacts) { this.win(eval('(' + contacts + ')')); }; From c80397ad68ecfcc6f63d78df0c55ef0ab05ee724 Mon Sep 17 00:00:00 2001 From: macdonst Date: Sat, 2 Oct 2010 05:10:24 +0800 Subject: [PATCH 06/11] Fix duplicate method name --- framework/assets/js/contact.js | 9 +++------ framework/assets/www/phonegap.js | 9 +++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/framework/assets/js/contact.js b/framework/assets/js/contact.js index a4c13669..1ebe89a7 100644 --- a/framework/assets/js/contact.js +++ b/framework/assets/js/contact.js @@ -100,7 +100,9 @@ Contacts.prototype.find = function(fields, win, fail, options) { PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]); }; -Contacts.prototype.wacky = function(properties) { +//This function does not create a new contact in the db. +//Must call contact.save() for it to be persisted in the db. +Contacts.prototype.create = function(properties) { var contact = new Contact(); for (i in properties) { if (contact[i]!='undefined') { @@ -114,11 +116,6 @@ Contacts.prototype.droidDone = function(contacts) { this.win(eval('(' + contacts + ')')); }; -// This function does not create a new contact in the db. -// Must call contact.save() for it to be persisted in the db. -Contacts.prototype.create = function(contact) { -}; - Contacts.prototype.m_foundContacts = function(win, contacts) { this.inProgress = false; win(contacts); diff --git a/framework/assets/www/phonegap.js b/framework/assets/www/phonegap.js index f581a56f..839ba758 100644 --- a/framework/assets/www/phonegap.js +++ b/framework/assets/www/phonegap.js @@ -1049,7 +1049,9 @@ Contacts.prototype.find = function(fields, win, fail, options) { PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]); }; -Contacts.prototype.wacky = function(properties) { +//This function does not create a new contact in the db. +//Must call contact.save() for it to be persisted in the db. +Contacts.prototype.create = function(properties) { var contact = new Contact(); for (i in properties) { if (contact[i]!='undefined') { @@ -1063,11 +1065,6 @@ Contacts.prototype.droidDone = function(contacts) { this.win(eval('(' + contacts + ')')); }; -// This function does not create a new contact in the db. -// Must call contact.save() for it to be persisted in the db. -Contacts.prototype.create = function(contact) { -}; - Contacts.prototype.m_foundContacts = function(win, contacts) { this.inProgress = false; win(contacts); From 2bbf62c489510bd5886ba9b1dd70cf3b4ea54b00 Mon Sep 17 00:00:00 2001 From: macdonst Date: Mon, 4 Oct 2010 09:47:12 +0800 Subject: [PATCH 07/11] Fixing Contacts.find to use PluginResult --- framework/assets/js/contact.js | 5 +---- framework/assets/www/phonegap.js | 5 +---- framework/src/com/phonegap/ContactAccessor.java | 2 +- framework/src/com/phonegap/ContactAccessorSdk3_4.java | 5 ++--- framework/src/com/phonegap/ContactAccessorSdk5.java | 7 ++++--- framework/src/com/phonegap/ContactManager.java | 3 ++- framework/src/com/phonegap/api/PluginResult.java | 6 ++++++ 7 files changed, 17 insertions(+), 16 deletions(-) diff --git a/framework/assets/js/contact.js b/framework/assets/js/contact.js index 1ebe89a7..2d735027 100644 --- a/framework/assets/js/contact.js +++ b/framework/assets/js/contact.js @@ -94,10 +94,7 @@ var Contacts = function() { } Contacts.prototype.find = function(fields, win, fail, options) { - this.win = win; - this.fail = fail; - - PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]); + PhoneGap.execAsync(win, fail, "Contacts", "search", [fields, options]); }; //This function does not create a new contact in the db. diff --git a/framework/assets/www/phonegap.js b/framework/assets/www/phonegap.js index 839ba758..8aa92570 100644 --- a/framework/assets/www/phonegap.js +++ b/framework/assets/www/phonegap.js @@ -1043,10 +1043,7 @@ var Contacts = function() { } Contacts.prototype.find = function(fields, win, fail, options) { - this.win = win; - this.fail = fail; - - PhoneGap.execAsync(null, null, "Contacts", "search", [fields, options]); + PhoneGap.execAsync(win, fail, "Contacts", "search", [fields, options]); }; //This function does not create a new contact in the db. diff --git a/framework/src/com/phonegap/ContactAccessor.java b/framework/src/com/phonegap/ContactAccessor.java index 0fc02b41..5e7ccde9 100644 --- a/framework/src/com/phonegap/ContactAccessor.java +++ b/framework/src/com/phonegap/ContactAccessor.java @@ -91,7 +91,7 @@ public abstract class ContactAccessor { /** * Handles searching through SDK-specific contacts API. */ - public abstract void search(JSONArray filter, JSONObject options); + public abstract JSONArray search(JSONArray filter, JSONObject options); /** * Handles removing a contact from the database. diff --git a/framework/src/com/phonegap/ContactAccessorSdk3_4.java b/framework/src/com/phonegap/ContactAccessorSdk3_4.java index 7e43ec2a..9feeba7c 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk3_4.java +++ b/framework/src/com/phonegap/ContactAccessorSdk3_4.java @@ -31,7 +31,6 @@ import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; -import android.provider.ContactsContract; import android.provider.Contacts.ContactMethods; import android.provider.Contacts.ContactMethodsColumns; import android.provider.Contacts.Organizations; @@ -80,7 +79,7 @@ public class ContactAccessorSdk3_4 extends ContactAccessor { } @Override - public void search(JSONArray filter, JSONObject options) { + public JSONArray search(JSONArray filter, JSONObject options) { String searchTerm = ""; int limit = Integer.MAX_VALUE; boolean multiple = true; @@ -152,7 +151,7 @@ public class ContactAccessorSdk3_4 extends ContactAccessor { } contacts.put(contact); } - mView.loadUrl("javascript:navigator.service.contacts.droidDone('" + contacts.toString() + "');"); + return contacts; } private Set buildSetOfContactIds(JSONArray filter, String searchTerm) { diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index a99a6d7e..58639cdc 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -114,7 +114,7 @@ public class ContactAccessorSdk5 extends ContactAccessor { } @Override - public void search(JSONArray filter, JSONObject options) { + public JSONArray search(JSONArray filter, JSONObject options) { String searchTerm = ""; int limit = Integer.MAX_VALUE; boolean multiple = true; @@ -166,11 +166,12 @@ public class ContactAccessorSdk5 extends ContactAccessor { } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } + Log.d(LOG_TAG, "putting in contact ID = " + contactId); contacts.put(contact); pos++; - } - mView.loadUrl("javascript:navigator.service.contacts.droidDone('" + contacts.toString() + "');"); + } + return contacts; } private Set buildSetOfContactIds(JSONArray filter, String searchTerm) { diff --git a/framework/src/com/phonegap/ContactManager.java b/framework/src/com/phonegap/ContactManager.java index 38cc899e..7f35056d 100755 --- a/framework/src/com/phonegap/ContactManager.java +++ b/framework/src/com/phonegap/ContactManager.java @@ -61,7 +61,8 @@ public class ContactManager implements Plugin { try { if (action.equals("search")) { - contactAccessor.search(args.getJSONArray(0), args.getJSONObject(1)); + JSONArray res = contactAccessor.search(args.getJSONArray(0), args.getJSONObject(1)); + return new PluginResult(status, res); } else if (action.equals("create")) { // TODO Coming soon! diff --git a/framework/src/com/phonegap/api/PluginResult.java b/framework/src/com/phonegap/api/PluginResult.java index 777f2605..7523bfe7 100755 --- a/framework/src/com/phonegap/api/PluginResult.java +++ b/framework/src/com/phonegap/api/PluginResult.java @@ -1,5 +1,6 @@ package com.phonegap.api; +import org.json.JSONArray; import org.json.JSONObject; public class PluginResult { @@ -16,6 +17,11 @@ public class PluginResult { this.message = "'" + message + "'"; } + public PluginResult(Status status, JSONArray message) { + this.status = status.ordinal(); + this.message = message.toString(); + } + public PluginResult(Status status, JSONObject message) { this.status = status.ordinal(); this.message = message.toString(); From 8eaaa047469634d6b10fe358f6853bdf4b63909d Mon Sep 17 00:00:00 2001 From: macdonst Date: Wed, 6 Oct 2010 00:17:43 +0800 Subject: [PATCH 08/11] Small fix to speed up contact retrieval --- example/index.html | 1 + .../src/com/phonegap/ContactAccessor.java | 2 +- .../com/phonegap/ContactAccessorSdk3_4.java | 2 +- .../src/com/phonegap/ContactAccessorSdk5.java | 72 +++++++++++++++---- .../src/com/phonegap/ContactManager.java | 3 - 5 files changed, 62 insertions(+), 18 deletions(-) diff --git a/example/index.html b/example/index.html index bdfee7c3..ecf05ee7 100644 --- a/example/index.html +++ b/example/index.html @@ -104,6 +104,7 @@ var obj = new ContactFindOptions(); obj.filter=""; obj.multiple=true; + obj.limit=5; navigator.service.contacts.find(["displayName", "phoneNumbers", "emails"], count_contacts, fail, obj); } diff --git a/framework/src/com/phonegap/ContactAccessor.java b/framework/src/com/phonegap/ContactAccessor.java index 5e7ccde9..b2a1843c 100644 --- a/framework/src/com/phonegap/ContactAccessor.java +++ b/framework/src/com/phonegap/ContactAccessor.java @@ -86,7 +86,7 @@ public abstract class ContactAccessor { /** * Handles adding a JSON Contact object into the database. */ - public abstract void save(); + public abstract void save(JSONObject contact); /** * Handles searching through SDK-specific contacts API. diff --git a/framework/src/com/phonegap/ContactAccessorSdk3_4.java b/framework/src/com/phonegap/ContactAccessorSdk3_4.java index 9feeba7c..c64d105b 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk3_4.java +++ b/framework/src/com/phonegap/ContactAccessorSdk3_4.java @@ -361,7 +361,7 @@ public class ContactAccessorSdk3_4 extends ContactAccessor { } @Override - public void save() { + public void save(JSONObject contact) { // TODO Auto-generated method stub } diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index 58639cdc..fa2047ad 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -107,14 +107,17 @@ public class ContactAccessorSdk5 extends ContactAccessor { //dbMap.put("connected", null); } - public ContactAccessorSdk5(WebView view, Activity app) - { + public ContactAccessorSdk5(WebView view, Activity app) { mApp = app; mView = view; } @Override public JSONArray search(JSONArray filter, JSONObject options) { + long totalEnd; + long totalStart = System.currentTimeMillis(); + long start = System.currentTimeMillis(); + long stop; String searchTerm = ""; int limit = Integer.MAX_VALUE; boolean multiple = true; @@ -133,11 +136,19 @@ public class ContactAccessorSdk5 extends ContactAccessor { } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } + stop = System.currentTimeMillis(); + Log.d(LOG_TAG, "Parsing parameters took = " + (stop-start)); + start = System.currentTimeMillis(); + // Get a cursor by creating the query. ContentResolver cr = mApp.getContentResolver(); Set contactIds = buildSetOfContactIds(filter, searchTerm); + stop = System.currentTimeMillis(); + Log.d(LOG_TAG, "Building contact ID's took = " + (stop-start)); + start = System.currentTimeMillis(); + Iterator it = contactIds.iterator(); JSONArray contacts = new JSONArray(); @@ -171,12 +182,28 @@ public class ContactAccessorSdk5 extends ContactAccessor { contacts.put(contact); pos++; } + stop = System.currentTimeMillis(); + totalEnd = System.currentTimeMillis(); + Log.d(LOG_TAG, "Populating contact Array took = " + (stop - start)); + Log.d(LOG_TAG, "Total search took = " + (totalEnd - totalStart)); return contacts; } private Set buildSetOfContactIds(JSONArray filter, String searchTerm) { Set contactIds = new HashSet(); + /* + * Special case for when the user wants all the contacts + */ + if ("%".equals(searchTerm)) { + doQuery(searchTerm, contactIds, + ContactsContract.Contacts.CONTENT_URI, + ContactsContract.Contacts._ID, + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?", + new String[] {searchTerm}); + return contactIds; + } + String key; try { for (int i=0; i Date: Wed, 6 Oct 2010 00:22:43 +0800 Subject: [PATCH 09/11] Remove logging --- .../src/com/phonegap/ContactAccessorSdk5.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index fa2047ad..e02b0795 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -114,10 +114,6 @@ public class ContactAccessorSdk5 extends ContactAccessor { @Override public JSONArray search(JSONArray filter, JSONObject options) { - long totalEnd; - long totalStart = System.currentTimeMillis(); - long start = System.currentTimeMillis(); - long stop; String searchTerm = ""; int limit = Integer.MAX_VALUE; boolean multiple = true; @@ -136,19 +132,12 @@ public class ContactAccessorSdk5 extends ContactAccessor { } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } - stop = System.currentTimeMillis(); - Log.d(LOG_TAG, "Parsing parameters took = " + (stop-start)); - start = System.currentTimeMillis(); // Get a cursor by creating the query. ContentResolver cr = mApp.getContentResolver(); Set contactIds = buildSetOfContactIds(filter, searchTerm); - stop = System.currentTimeMillis(); - Log.d(LOG_TAG, "Building contact ID's took = " + (stop-start)); - start = System.currentTimeMillis(); - Iterator it = contactIds.iterator(); JSONArray contacts = new JSONArray(); @@ -182,10 +171,6 @@ public class ContactAccessorSdk5 extends ContactAccessor { contacts.put(contact); pos++; } - stop = System.currentTimeMillis(); - totalEnd = System.currentTimeMillis(); - Log.d(LOG_TAG, "Populating contact Array took = " + (stop - start)); - Log.d(LOG_TAG, "Total search took = " + (totalEnd - totalStart)); return contacts; } From f20e5cf943d36cde281778b2037b7eead6838f5a Mon Sep 17 00:00:00 2001 From: macdonst Date: Wed, 6 Oct 2010 02:22:25 +0800 Subject: [PATCH 10/11] Shave .2 sec off each contact returned in a query --- .../src/com/phonegap/ContactAccessorSdk5.java | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index e02b0795..8aa2b067 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -114,6 +114,10 @@ public class ContactAccessorSdk5 extends ContactAccessor { @Override public JSONArray search(JSONArray filter, JSONObject options) { + long totalEnd; + long totalStart = System.currentTimeMillis(); + long start = System.currentTimeMillis(); + long stop; String searchTerm = ""; int limit = Integer.MAX_VALUE; boolean multiple = true; @@ -132,18 +136,25 @@ public class ContactAccessorSdk5 extends ContactAccessor { } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } + stop = System.currentTimeMillis(); + Log.d(LOG_TAG, "Parsing parameters took = " + (stop-start)); + start = System.currentTimeMillis(); // Get a cursor by creating the query. ContentResolver cr = mApp.getContentResolver(); Set contactIds = buildSetOfContactIds(filter, searchTerm); + stop = System.currentTimeMillis(); + Log.d(LOG_TAG, "Building contact ID's took = " + (stop-start)); + Iterator it = contactIds.iterator(); JSONArray contacts = new JSONArray(); JSONObject contact; String contactId; int pos = 0; + String[] events = null; while (it.hasNext() && (pos < limit)) { contact = new JSONObject(); contactId = it.next(); @@ -161,8 +172,9 @@ public class ContactAccessorSdk5 extends ContactAccessor { contact.put("nickname",nicknameQuery(cr, contactId)); contact.put("urls",websiteQuery(cr, contactId)); contact.put("relationships",relationshipQuery(cr, contactId)); - contact.put("birthday",birthdayQuery(cr, contactId)); - contact.put("anniversary",anniversaryQuery(cr, contactId)); + events = eventQuery(cr, contactId); + contact.put("birthday",events[0]); + contact.put("anniversary",events[1]); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } @@ -171,6 +183,10 @@ public class ContactAccessorSdk5 extends ContactAccessor { contacts.put(contact); pos++; } + stop = System.currentTimeMillis(); + totalEnd = System.currentTimeMillis(); + Log.d(LOG_TAG, "Populating contact Array took = " + (stop - start)); + Log.d(LOG_TAG, "Total search took = " + (totalEnd - totalStart)); return contacts; } @@ -555,32 +571,22 @@ public class ContactAccessorSdk5 extends ContactAccessor { return relationships; } - private String birthdayQuery(ContentResolver cr, String contactId) { - String birthday = conditionalStringQuery(cr, contactId, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, - ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY, ContactsContract.CommonDataKinds.Event.TYPE, - ContactsContract.CommonDataKinds.Event.START_DATE); - return birthday; - } - - private String anniversaryQuery(ContentResolver cr, String contactId) { - String anniversary = conditionalStringQuery(cr, contactId, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, - ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY, ContactsContract.CommonDataKinds.Event.TYPE, - ContactsContract.CommonDataKinds.Event.START_DATE); - return anniversary; - } - - private String conditionalStringQuery(ContentResolver cr, String contactId, String dataType, int type, String label, String data) { - String[] whereParams = new String[]{contactId, dataType}; + private String[] eventQuery(ContentResolver cr, String contactId) { + String[] whereParams = new String[]{contactId, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE}; Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, null, WHERE_STRING, whereParams, null); - String retVal = new String(""); + String anniversary = null; + String birthday = null; while (cursor.moveToNext()) { - if (type == cursor.getInt(cursor.getColumnIndex(label))) { - retVal = cursor.getString(cursor.getColumnIndex(data)); + if (ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY == cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))) { + anniversary = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)); + } + else if (ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY == cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))) { + birthday = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)); } } cursor.close(); - return retVal; + return new String[] {anniversary, birthday}; } @Override From 3138178feacb8d4c8f5f4eeede14f0e985a38ec5 Mon Sep 17 00:00:00 2001 From: macdonst Date: Thu, 7 Oct 2010 00:43:20 +0800 Subject: [PATCH 11/11] Speeding up contacts.find --- .../src/com/phonegap/ContactAccessor.java | 62 ++++++++++++++++ .../com/phonegap/ContactAccessorSdk3_4.java | 38 ++++++---- .../src/com/phonegap/ContactAccessorSdk5.java | 71 +++++++++++-------- 3 files changed, 126 insertions(+), 45 deletions(-) diff --git a/framework/src/com/phonegap/ContactAccessor.java b/framework/src/com/phonegap/ContactAccessor.java index b2a1843c..e276118c 100644 --- a/framework/src/com/phonegap/ContactAccessor.java +++ b/framework/src/com/phonegap/ContactAccessor.java @@ -19,11 +19,14 @@ package com.phonegap; import java.lang.reflect.Constructor; +import java.util.HashMap; import android.app.Activity; +import android.util.Log; import android.webkit.WebView; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; /** @@ -82,6 +85,65 @@ public abstract class ContactAccessor { return sInstance; } + + protected boolean isRequired(String key, HashMap map) { + Boolean retVal = map.get(key); + return (retVal == null) ? false : retVal.booleanValue(); + } + + protected HashMap buildPopulationSet(JSONArray filter) { + HashMap map = new HashMap(); + + String key; + try { + for (int i=0; i contactIds = buildSetOfContactIds(filter, searchTerm); - + HashMap populate = buildPopulationSet(filter); + Iterator it = contactIds.iterator(); JSONArray contacts = new JSONArray(); @@ -124,20 +125,27 @@ public class ContactAccessorSdk3_4 extends ContactAccessor { null); cur.moveToFirst(); - // name - contact.put("displayName", cur.getString(cur.getColumnIndex(People.DISPLAY_NAME))); - // phone number - contact.put("phoneNumbers", phoneQuery(cr, contactId)); - // email - contact.put("emails", emailQuery(cr, contactId)); - // addresses - contact.put("addresses", addressQuery(cr, contactId)); - // organizations - contact.put("organizations", organizationQuery(cr, contactId)); - // ims - contact.put("ims", imQuery(cr, contactId)); - // note - contact.put("note", cur.getString(cur.getColumnIndex(People.NOTES))); + if (isRequired("displayName",populate)) { + contact.put("displayName", cur.getString(cur.getColumnIndex(People.DISPLAY_NAME))); + } + if (isRequired("phoneNumbers",populate)) { + contact.put("phoneNumbers", phoneQuery(cr, contactId)); + } + if (isRequired("emails",populate)) { + contact.put("emails", emailQuery(cr, contactId)); + } + if (isRequired("addresses",populate)) { + contact.put("addresses", addressQuery(cr, contactId)); + } + if (isRequired("organizations",populate)) { + contact.put("organizations", organizationQuery(cr, contactId)); + } + if (isRequired("ims",populate)) { + contact.put("ims", imQuery(cr, contactId)); + } + if (isRequired("note",populate)) { + contact.put("note", cur.getString(cur.getColumnIndex(People.NOTES))); + } // nickname // urls // relationship diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index 8aa2b067..b4ab52a6 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -114,10 +114,6 @@ public class ContactAccessorSdk5 extends ContactAccessor { @Override public JSONArray search(JSONArray filter, JSONObject options) { - long totalEnd; - long totalStart = System.currentTimeMillis(); - long start = System.currentTimeMillis(); - long stop; String searchTerm = ""; int limit = Integer.MAX_VALUE; boolean multiple = true; @@ -136,18 +132,13 @@ public class ContactAccessorSdk5 extends ContactAccessor { } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } - stop = System.currentTimeMillis(); - Log.d(LOG_TAG, "Parsing parameters took = " + (stop-start)); - start = System.currentTimeMillis(); // Get a cursor by creating the query. ContentResolver cr = mApp.getContentResolver(); Set contactIds = buildSetOfContactIds(filter, searchTerm); - - stop = System.currentTimeMillis(); - Log.d(LOG_TAG, "Building contact ID's took = " + (stop-start)); - + HashMap populate = buildPopulationSet(filter); + Iterator it = contactIds.iterator(); JSONArray contacts = new JSONArray(); @@ -161,20 +152,44 @@ public class ContactAccessorSdk5 extends ContactAccessor { try { contact.put("id", contactId); - contact.put("displayName", displayNameQuery(cr, contactId)); - contact.put("name", nameQuery(cr, contactId)); - contact.put("phoneNumbers", phoneQuery(cr, contactId)); - contact.put("emails", emailQuery(cr, contactId)); - contact.put("addresses", addressQuery(cr, contactId)); - contact.put("organizations", organizationQuery(cr, contactId)); - contact.put("ims",imQuery(cr, contactId)); - contact.put("note",noteQuery(cr, contactId)); - contact.put("nickname",nicknameQuery(cr, contactId)); - contact.put("urls",websiteQuery(cr, contactId)); - contact.put("relationships",relationshipQuery(cr, contactId)); - events = eventQuery(cr, contactId); - contact.put("birthday",events[0]); - contact.put("anniversary",events[1]); + if (isRequired("displayName",populate)) { + contact.put("displayName", displayNameQuery(cr, contactId)); + } + if (isRequired("name",populate)) { + contact.put("name", nameQuery(cr, contactId)); + } + if (isRequired("phoneNumbers",populate)) { + contact.put("phoneNumbers", phoneQuery(cr, contactId)); + } + if (isRequired("emails",populate)) { + contact.put("emails", emailQuery(cr, contactId)); + } + if (isRequired("addresses",populate)) { + contact.put("addresses", addressQuery(cr, contactId)); + } + if (isRequired("organizations",populate)) { + contact.put("organizations", organizationQuery(cr, contactId)); + } + if (isRequired("ims",populate)) { + contact.put("ims",imQuery(cr, contactId)); + } + if (isRequired("note",populate)) { + contact.put("note",noteQuery(cr, contactId)); + } + if (isRequired("nickname",populate)) { + contact.put("nickname",nicknameQuery(cr, contactId)); + } + if (isRequired("urls",populate)) { + contact.put("urls",websiteQuery(cr, contactId)); + } + if (isRequired("relationships",populate)) { + contact.put("relationships",relationshipQuery(cr, contactId)); + } + if (isRequired("birthday",populate) || isRequired("anniversary",populate)) { + events = eventQuery(cr, contactId); + contact.put("birthday",events[0]); + contact.put("anniversary",events[1]); + } } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } @@ -183,13 +198,9 @@ public class ContactAccessorSdk5 extends ContactAccessor { contacts.put(contact); pos++; } - stop = System.currentTimeMillis(); - totalEnd = System.currentTimeMillis(); - Log.d(LOG_TAG, "Populating contact Array took = " + (stop - start)); - Log.d(LOG_TAG, "Total search took = " + (totalEnd - totalStart)); return contacts; } - + private Set buildSetOfContactIds(JSONArray filter, String searchTerm) { Set contactIds = new HashSet();