From 5f55ebf1d9381883d41c7bf3bc4a05d742fe905e Mon Sep 17 00:00:00 2001 From: macdonst Date: Tue, 2 Nov 2010 05:16:00 +0800 Subject: [PATCH] Adding Contact.save() for Android 1.X and Android 2.X --- framework/assets/js/contact.js | 54 +- framework/assets/js/phonegap.js.base | 33 +- .../src/com/phonegap/ContactAccessor.java | 27 +- .../com/phonegap/ContactAccessorSdk3_4.java | 384 +++++- .../src/com/phonegap/ContactAccessorSdk5.java | 1106 ++++++++++++++++- .../src/com/phonegap/ContactManager.java | 2 +- 6 files changed, 1576 insertions(+), 30 deletions(-) diff --git a/framework/assets/js/contact.js b/framework/assets/js/contact.js index 974b9f65..ee99e0d4 100755 --- a/framework/assets/js/contact.js +++ b/framework/assets/js/contact.js @@ -36,6 +36,7 @@ var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, ad ims, organizations, published, updated, birthday, anniversary, gender, note, preferredUsername, photos, tags, relationships, urls, accounts, utcOffset, connected) { this.id = id || null; + this.rawId = null; this.displayName = displayName || null; this.name = name || null; // ContactName this.nickname = nickname || null; @@ -83,6 +84,48 @@ Contact.prototype.remove = function(successCB, errorCB) { Contact.prototype.clone = function() { var clonedContact = PhoneGap.clone(this); clonedContact.id = null; + clonedContact.rawId = null; + // Loop through and clear out any id's in phones, emails, etc. + if (clonedContact.phoneNumbers) { + for (i=0; i 0) { + JSONObject org; + for (int i=0; i 0) { + JSONObject entry; + values.put(Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL); + for (int i=0; i 0 ) { + buffer.append(", "); + } + buffer.append(getJsonString(entry, "region")); + } + if (getJsonString(entry, "postalCode") != null ) { + if (buffer.length() > 0 ) { + buffer.append(", "); + } + buffer.append(getJsonString(entry, "postalCode")); + } + if (getJsonString(entry, "country") != null ) { + if (buffer.length() > 0 ) { + buffer.append(", "); + } + buffer.append(getJsonString(entry, "country")); + } + return buffer.toString(); + } + + /** + * Takes a JSON contact object and loops through the available entries (Emails/IM's). If the + * entry has an id that is not equal to null the entry will be updated in the database. + * If the id is null then we treat it as a new entry. + * + * @param contact the contact to extract the entries from + * @param uri the base URI for this contact. + */ + private void saveEntries(JSONObject contact, Uri uri, String dataType, int contactKind) { + ContentValues values = new ContentValues(); + Uri newUri = Uri.withAppendedPath(uri, + Contacts.People.ContactMethods.CONTENT_DIRECTORY); + String id = null; + + try { + JSONArray entries = contact.getJSONArray(dataType); + if (entries != null && entries.length() > 0) { + JSONObject entry; + values.put(Contacts.ContactMethods.KIND, contactKind); + for (int i=0; i 0) { + JSONObject phone; + for (int i=0; i 0) ? true : false; diff --git a/framework/src/com/phonegap/ContactAccessorSdk5.java b/framework/src/com/phonegap/ContactAccessorSdk5.java index 1267a64a..2fa976cc 100644 --- a/framework/src/com/phonegap/ContactAccessorSdk5.java +++ b/framework/src/com/phonegap/ContactAccessorSdk5.java @@ -32,8 +32,14 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import android.accounts.Account; +import android.accounts.AccountManager; import android.app.Activity; +import android.content.ContentProviderOperation; +import android.content.ContentValues; +import android.content.OperationApplicationException; import android.database.Cursor; +import android.os.RemoteException; import android.provider.ContactsContract; import android.util.Log; import android.webkit.WebView; @@ -151,6 +157,10 @@ public class ContactAccessorSdk5 extends ContactAccessor { } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } + + //Log.d(LOG_TAG, "Search Term = " + searchTerm); + //Log.d(LOG_TAG, "Field Length = " + fields.length()); + //Log.d(LOG_TAG, "Fields = " + fields.toString()); // Loop through the fields the user provided to see what data should be returned. HashMap populate = buildPopulationSet(fields); @@ -165,7 +175,10 @@ public class ContactAccessorSdk5 extends ContactAccessor { whereOptions.getWhereArgs(), ContactsContract.Data.CONTACT_ID + " ASC"); + //Log.d(LOG_TAG, "Cursor length = " + c.getCount()); + String contactId = ""; + String rawId = ""; String oldContactId = ""; boolean newContact = true; String mimetype = ""; @@ -180,9 +193,11 @@ public class ContactAccessorSdk5 extends ContactAccessor { JSONArray websites = new JSONArray(); JSONArray relationships = new JSONArray(); - while (c.moveToNext() && (contacts.length() < (limit-1))) { + while (c.moveToNext() && (contacts.length() <= (limit-1))) { try { contactId = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID)); + rawId = c.getString(c.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID)); + //Log.d(LOG_TAG, "Contact ID = " + contactId + " Raw ID = " + rawId); // If we are in the first row set the oldContactId if (c.getPosition() == 0) { @@ -216,6 +231,7 @@ public class ContactAccessorSdk5 extends ContactAccessor { if (newContact) { newContact = false; contact.put("id", contactId); + contact.put("rawId", rawId); contact.put("displayName", c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); } @@ -345,6 +361,7 @@ public class ContactAccessorSdk5 extends ContactAccessor { String key; try { + Log.d(LOG_TAG, "How many fields do we have = " + fields.length()); for (int i=0; i ops = new ArrayList(); + + //Add contact type + ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) + .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type) + .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name) + .build()); + + // Modify display name + String displayName = getJsonString(contact, "displayName"); + if (displayName != null) { + ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) + .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + + ContactsContract.Data.MIMETYPE + "=?", + new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE}) + .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, getJsonString(contact, "displayName")) + .build()); + } + + // Modify name + JSONObject name; + try { + name = contact.getJSONObject("name"); + if (name != null) { + ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) + .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + + ContactsContract.Data.MIMETYPE + "=?", + new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE}); + + String familyName = getJsonString(name, "familyName"); + if (familyName != null) { + builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName); + } + String middleName = getJsonString(name, "familyName"); + if (middleName != null) { + builder.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName); + } + String givenName = getJsonString(name, "givenName"); + if (givenName != null) { + builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName); + } + String honorificPrefix = getJsonString(name, "honorificPrefix"); + if (honorificPrefix != null) { + builder.withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, honorificPrefix); + } + String honorificSuffix = getJsonString(name, "honorificSuffix"); + if (honorificSuffix != null) { + builder.withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, honorificSuffix); + } + + ops.add(builder.build()); + } + } catch (JSONException e1) { + Log.d(LOG_TAG, "Could not get name"); + } + + // Modify phone numbers + JSONArray phones = null; + try { + phones = contact.getJSONArray("phoneNumbers"); + if (phones != null) { + for (int i=0; i ops, + JSONObject relationship) { + ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Relation.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.Relation.NAME, getJsonString(relationship, "value")) + .withValue(ContactsContract.CommonDataKinds.Relation.TYPE, getRelationshipType(getJsonString(relationship, "type"))) + .build()); + } + + /** + * Add a website to a list of database actions to be performed + * + * @param ops the list of database actions + * @param website the item to be inserted + */ + private void insertWebsite(ArrayList ops, + JSONObject website) { + ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.Website.DATA, getJsonString(website, "value")) + .withValue(ContactsContract.CommonDataKinds.Website.TYPE, getContactType(getJsonString(website, "type"))) + .build()); + } + + /** + * Add an im to a list of database actions to be performed + * + * @param ops the list of database actions + * @param im the item to be inserted + */ + private void insertIm(ArrayList ops, JSONObject im) { + ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value")) + .withValue(ContactsContract.CommonDataKinds.Im.TYPE, getContactType(getJsonString(im, "type"))) + .build()); + } + + /** + * Add an organization to a list of database actions to be performed + * + * @param ops the list of database actions + * @param org the item to be inserted + */ + private void insertOrganization(ArrayList ops, + JSONObject org) { + ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, getJsonString(org, "department")) + .withValue(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION, getJsonString(org, "description")) + .withValue(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION, getJsonString(org, "location")) + .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, getJsonString(org, "name")) + .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, getJsonString(org, "title")) + .build()); + } + + /** + * Add an address to a list of database actions to be performed + * + * @param ops the list of database actions + * @param address the item to be inserted + */ + private void insertAddress(ArrayList ops, + JSONObject address) { + ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, getJsonString(address, "formatted")) + .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, getJsonString(address, "streetAddress")) + .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, getJsonString(address, "locality")) + .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, getJsonString(address, "region")) + .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, getJsonString(address, "postalCode")) + .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, getJsonString(address, "country")) + .build()); + } + + /** + * Add an email to a list of database actions to be performed + * + * @param ops the list of database actions + * @param email the item to be inserted + */ + private void insertEmail(ArrayList ops, + JSONObject email) { + ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.Email.DATA, getJsonString(email, "value")) + .withValue(ContactsContract.CommonDataKinds.Email.TYPE, getPhoneType(getJsonString(email, "type"))) + .build()); + } + + /** + * Add a phone to a list of database actions to be performed + * + * @param ops the list of database actions + * @param phone the item to be inserted + */ + private void insertPhone(ArrayList ops, + JSONObject phone) { + ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, getJsonString(phone, "value")) + .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, getPhoneType(getJsonString(phone, "type"))) + .build()); + } + + /** + * Creates a new contact and stores it in the database + * + * @param contact the contact to be saved + * @param account the account to be saved under + */ + private boolean createNewContact(JSONObject contact, Account account) { + // Create a list of attributes to add to the contact database + ArrayList ops = new ArrayList(); + + //Add contact type + ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) + .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type) + .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name) + .build()); + + //Add display name + ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, getJsonString(contact, "displayName")) + .build()); + + // Add name + ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) + .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) + .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, getJsonString(contact, "familyName")) + .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, getJsonString(contact, "middleName")) + .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, getJsonString(contact, "givenName")) + .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, getJsonString(contact, "honorificPrefix")) + .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, getJsonString(contact, "honorificSuffix")) + .build()); + + //Add phone numbers + JSONArray phones = null; + try { + phones = contact.getJSONArray("phoneNumbers"); + if (phones != null) { + for (int i=0; i 0) ? true : false; } + +/************************************************************************** + * + * All methods below this comment are used to convert from JavaScript + * text types to Android integer types and vice versa. + * + *************************************************************************/ + + /** + * Converts a string from the W3C Contact API to it's Android int value. + * @param string + * @return Android int value + */ + private int getPhoneType(String string) { + int type = ContactsContract.CommonDataKinds.Phone.TYPE_OTHER; + if ("home".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_HOME; + } + else if ("mobile".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE; + } + else if ("work".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_WORK; + } + else if ("work fax".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK; + } + else if ("home fax".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME; + } + else if ("fax".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK; + } + else if ("pager".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_PAGER; + } + else if ("other".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_OTHER; + } + else if ("car".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_CAR; + } + else if ("company main".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN; + } + else if ("isdn".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_ISDN; + } + else if ("main".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_MAIN; + } + else if ("other fax".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX; + } + else if ("radio".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_RADIO; + } + else if ("telex".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_TELEX; + } + else if ("work mobile".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE; + } + else if ("work pager".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER; + } + else if ("assistant".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT; + } + else if ("mms".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_MMS; + } + else if ("callback".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK; + } + else if ("tty ttd".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD; + } + else if ("custom".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM; + } + return type; + } + + /** + * getPhoneType converts an Android phone type into a string + * @param type + * @return phone type as string. + */ + private String getPhoneType(int type) { + String stringType; + switch (type) { + case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM: + stringType = "custom"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME: + stringType = "home fax"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK: + stringType = "work fax"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: + stringType = "home"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: + stringType = "mobile"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER: + stringType = "pager"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: + stringType = "work"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK: + stringType = "callback"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_CAR: + stringType = "car"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN: + stringType = "company main"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX: + stringType = "other fax"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_RADIO: + stringType = "radio"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_TELEX: + stringType = "telex"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD: + stringType = "tty tdd"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE: + stringType = "work mobile"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER: + stringType = "work pager"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT: + stringType = "assistant"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_MMS: + stringType = "mms"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN: + stringType = "isdn"; + break; + case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER: + default: + stringType = "other"; + break; + } + return stringType; + } + + /** + * Converts a string from the W3C Contact API to it's Android int value. + * @param string + * @return Android int value + */ + private int getContactType(String string) { + int type = ContactsContract.CommonDataKinds.Email.TYPE_OTHER; + if (string!=null) { + if ("home".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Email.TYPE_HOME; + } + else if ("work".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Email.TYPE_WORK; + } + else if ("other".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Email.TYPE_OTHER; + } + else if ("mobile".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Email.TYPE_MOBILE; + } + else if ("custom".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM; + } + } + return type; + } + + /** + * getPhoneType converts an Android phone type into a string + * @param type + * @return phone type as string. + */ + private String getContactType(int type) { + String stringType; + switch (type) { + case ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM: + stringType = "custom"; + break; + case ContactsContract.CommonDataKinds.Email.TYPE_HOME: + stringType = "home"; + break; + case ContactsContract.CommonDataKinds.Email.TYPE_WORK: + stringType = "work"; + break; + case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE: + stringType = "mobile"; + break; + case ContactsContract.CommonDataKinds.Email.TYPE_OTHER: + default: + stringType = "other"; + break; + } + return stringType; + } + + /** + * Converts a string from the W3C Contact API to it's Android int value. + * @param string + * @return Android int value + */ + private int getRelationshipType(String string) { + int type = ContactsContract.CommonDataKinds.Relation.TYPE_CUSTOM; + if (string!=null) { + if ("assistant".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_ASSISTANT; + } + else if ("brother".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_BROTHER; + } + else if ("child".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_CHILD; + } + else if ("domestic partner".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_DOMESTIC_PARTNER; + } + else if ("father".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_FATHER; + } + else if ("friend".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_FRIEND; + } + else if ("manager".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_MANAGER; + } + else if ("mother".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_MOTHER; + } + else if ("parent".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_PARENT; + } + else if ("partner".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_PARTNER; + } + else if ("referred by".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_REFERRED_BY; + } + else if ("relative".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_RELATIVE; + } + else if ("sister".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_SISTER; + } + else if ("spouse".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_SPOUSE; + } + else if ("custom".equals(string.toLowerCase())) { + return ContactsContract.CommonDataKinds.Relation.TYPE_CUSTOM; + } + } + return type; + } + + /** + * getPhoneType converts an Android phone type into a string + * @param type + * @return phone type as string. + */ + private String getRelationshipType(int type) { + String stringType; + switch (type) { + case ContactsContract.CommonDataKinds.Relation.TYPE_ASSISTANT: + stringType = "assistant"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_BROTHER: + stringType = "brother"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_CHILD: + stringType = "child"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_DOMESTIC_PARTNER: + stringType = "domestic partner"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_FATHER: + stringType = "father"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_FRIEND: + stringType = "friend"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_MANAGER: + stringType = "manager"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_MOTHER: + stringType = "mother"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_PARENT: + stringType = "parent"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_PARTNER: + stringType = "partner"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_REFERRED_BY: + stringType = "referred by"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_RELATIVE: + stringType = "relative"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_SISTER: + stringType = "sister"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_SPOUSE: + stringType = "spouse"; + break; + case ContactsContract.CommonDataKinds.Relation.TYPE_CUSTOM: + default: + stringType = "custom"; + break; + } + return stringType; + } } \ No newline at end of file diff --git a/framework/src/com/phonegap/ContactManager.java b/framework/src/com/phonegap/ContactManager.java index 30ff5051..e09d3b91 100755 --- a/framework/src/com/phonegap/ContactManager.java +++ b/framework/src/com/phonegap/ContactManager.java @@ -46,7 +46,7 @@ public class ContactManager extends Plugin { return new PluginResult(status, res); } else if (action.equals("save")) { - // TODO Coming soon! + return new PluginResult(status, contactAccessor.save(args.getJSONObject(0))); } else if (action.equals("remove")) { if (contactAccessor.remove(args.getString(0))) {