From 11cdce70f86a6ff1d9e596b1f416b678fe505594 Mon Sep 17 00:00:00 2001 From: Harley11 <33711870+Git-Hub-Admin@users.noreply.github.com> Date: Mon, 21 Jan 2019 11:41:29 -0800 Subject: [PATCH] [Android] Several Fixes with updateContact (#349) * Fix updating postal addresses * Fix problem with the deleting portion of contact update * Allow custom labels and added a few more android default phone labels --- .../reactnativecontacts/ContactsManager.java | 196 ++++++++++++------ 1 file changed, 138 insertions(+), 58 deletions(-) diff --git a/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java b/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java index 4fa6408a..de03b9f5 100644 --- a/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java +++ b/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java @@ -314,15 +314,18 @@ public void addContact(ReadableMap contact, Callback callback) { ReadableArray phoneNumbers = contact.hasKey("phoneNumbers") ? contact.getArray("phoneNumbers") : null; int numOfPhones = 0; String[] phones = null; - Integer[] phonesLabels = null; + Integer[] phonesTypes = null; + String[] phonesLabels = null; if (phoneNumbers != null) { numOfPhones = phoneNumbers.size(); phones = new String[numOfPhones]; - phonesLabels = new Integer[numOfPhones]; + phonesTypes = new Integer[numOfPhones]; + phonesLabels = new String[numOfPhones]; for (int i = 0; i < numOfPhones; i++) { phones[i] = phoneNumbers.getMap(i).getString("number"); String label = phoneNumbers.getMap(i).getString("label"); - phonesLabels[i] = mapStringToPhoneType(label); + phonesTypes[i] = mapStringToPhoneType(label); + phonesLabels[i] = label; } } @@ -340,15 +343,18 @@ public void addContact(ReadableMap contact, Callback callback) { ReadableArray emailAddresses = contact.hasKey("emailAddresses") ? contact.getArray("emailAddresses") : null; int numOfEmails = 0; String[] emails = null; - Integer[] emailsLabels = null; + Integer[] emailsTypes = null; + String[] emailsLabels = null; if (emailAddresses != null) { numOfEmails = emailAddresses.size(); emails = new String[numOfEmails]; - emailsLabels = new Integer[numOfEmails]; + emailsTypes = new Integer[numOfEmails]; + emailsLabels = new String[numOfEmails]; for (int i = 0; i < numOfEmails; i++) { emails[i] = emailAddresses.getMap(i).getString("email"); String label = emailAddresses.getMap(i).getString("label"); - emailsLabels[i] = mapStringToEmailType(label); + emailsTypes[i] = mapStringToEmailType(label); + emailsLabels[i] = label; } } @@ -392,7 +398,8 @@ public void addContact(ReadableMap contact, Callback callback) { .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(CommonDataKinds.Phone.NUMBER, phones[i]) - .withValue(CommonDataKinds.Phone.TYPE, phonesLabels[i]); + .withValue(CommonDataKinds.Phone.TYPE, phonesTypes[i]) + .withValue(CommonDataKinds.Phone.LABEL, phonesLabels[i]); ops.add(op.build()); } @@ -409,7 +416,8 @@ public void addContact(ReadableMap contact, Callback callback) { .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Email.CONTENT_ITEM_TYPE) .withValue(CommonDataKinds.Email.ADDRESS, emails[i]) - .withValue(CommonDataKinds.Email.TYPE, emailsLabels[i]); + .withValue(CommonDataKinds.Email.TYPE, emailsTypes[i]) + .withValue(CommonDataKinds.Email.LABEL, emailsLabels[i]); ops.add(op.build()); } @@ -434,6 +442,7 @@ public void addContact(ReadableMap contact, Callback callback) { .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE) .withValue(CommonDataKinds.StructuredPostal.TYPE, mapStringToPostalAddressType(address.getString("label"))) + .withValue(CommonDataKinds.StructuredPostal.LABEL, address.getString("label")) .withValue(CommonDataKinds.StructuredPostal.STREET, address.getString("street")) .withValue(CommonDataKinds.StructuredPostal.CITY, address.getString("city")) .withValue(CommonDataKinds.StructuredPostal.REGION, address.getString("state")) @@ -492,12 +501,14 @@ public void updateContact(ReadableMap contact, Callback callback) { ReadableArray phoneNumbers = contact.hasKey("phoneNumbers") ? contact.getArray("phoneNumbers") : null; int numOfPhones = 0; String[] phones = null; - Integer[] phonesLabels = null; + Integer[] phonesTypes = null; + String[] phonesLabels = null; String[] phoneIds = null; if (phoneNumbers != null) { numOfPhones = phoneNumbers.size(); phones = new String[numOfPhones]; - phonesLabels = new Integer[numOfPhones]; + phonesTypes = new Integer[numOfPhones]; + phonesLabels = new String[numOfPhones]; phoneIds = new String[numOfPhones]; for (int i = 0; i < numOfPhones; i++) { ReadableMap phoneMap = phoneNumbers.getMap(i); @@ -505,7 +516,8 @@ public void updateContact(ReadableMap contact, Callback callback) { String phoneLabel = phoneMap.getString("label"); String phoneId = phoneMap.hasKey("id") ? phoneMap.getString("id") : null; phones[i] = phoneNumber; - phonesLabels[i] = mapStringToPhoneType(phoneLabel); + phonesTypes[i] = mapStringToPhoneType(phoneLabel); + phonesLabels[i] = phoneLabel; phoneIds[i] = phoneId; } } @@ -529,23 +541,59 @@ public void updateContact(ReadableMap contact, Callback callback) { ReadableArray emailAddresses = contact.hasKey("emailAddresses") ? contact.getArray("emailAddresses") : null; int numOfEmails = 0; String[] emails = null; - Integer[] emailsLabels = null; + Integer[] emailsTypes = null; + String[] emailsLabels = null; String[] emailIds = null; if (emailAddresses != null) { numOfEmails = emailAddresses.size(); emails = new String[numOfEmails]; emailIds = new String[numOfEmails]; - emailsLabels = new Integer[numOfEmails]; + emailsTypes = new Integer[numOfEmails]; + emailsLabels = new String[numOfEmails]; for (int i = 0; i < numOfEmails; i++) { ReadableMap emailMap = emailAddresses.getMap(i); emails[i] = emailMap.getString("email"); String label = emailMap.getString("label"); - emailsLabels[i] = mapStringToEmailType(label); + emailsTypes[i] = mapStringToEmailType(label); + emailsLabels[i] = label; emailIds[i] = emailMap.hasKey("id") ? emailMap.getString("id") : null; } } + ReadableArray postalAddresses = contact.hasKey("postalAddresses") ? contact.getArray("postalAddresses") : null; + int numOfPostalAddresses = 0; + String[] postalAddressesStreet = null; + String[] postalAddressesCity = null; + String[] postalAddressesState = null; + String[] postalAddressesRegion = null; + String[] postalAddressesPostCode = null; + String[] postalAddressesCountry = null; + Integer[] postalAddressesType = null; + String[] postalAddressesLabel = null; + if (postalAddresses != null) { + numOfPostalAddresses = postalAddresses.size(); + postalAddressesStreet = new String[numOfPostalAddresses]; + postalAddressesCity = new String[numOfPostalAddresses]; + postalAddressesState = new String[numOfPostalAddresses]; + postalAddressesRegion = new String[numOfPostalAddresses]; + postalAddressesPostCode = new String[numOfPostalAddresses]; + postalAddressesCountry = new String[numOfPostalAddresses]; + postalAddressesType = new Integer[numOfPostalAddresses]; + postalAddressesLabel = new String[numOfPostalAddresses]; + for (int i = 0; i < numOfPostalAddresses; i++) { + String postalLabel = postalAddresses.getMap(i).getString("label"); + postalAddressesStreet[i] = postalAddresses.getMap(i).getString("street"); + postalAddressesCity[i] = postalAddresses.getMap(i).getString("city"); + postalAddressesState[i] = postalAddresses.getMap(i).getString("state"); + postalAddressesRegion[i] = postalAddresses.getMap(i).getString("region"); + postalAddressesPostCode[i] = postalAddresses.getMap(i).getString("postCode"); + postalAddressesCountry[i] = postalAddresses.getMap(i).getString("country"); + postalAddressesType[i] = mapStringToPostalAddressType(postalLabel); + postalAddressesLabel[i] = postalLabel; + } + } + ArrayList ops = new ArrayList(); ContentProviderOperation.Builder op = ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI) @@ -573,22 +621,26 @@ public void updateContact(ReadableMap contact, Callback callback) { op.withYieldAllowed(true); - // remove existing phoneNumbers first - op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI) - .withSelection( - ContactsContract.Data.MIMETYPE + "=? AND "+ ContactsContract.Data.CONTACT_ID + " = ?", - new String[]{String.valueOf(CommonDataKinds.Phone.CONTENT_ITEM_TYPE), String.valueOf(recordID)} - ); - ops.add(op.build()); - // add passed phonenumbers - for (int i = 0; i < numOfPhones; i++) { - op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) - .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId)) - .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE) - .withValue(CommonDataKinds.Phone.NUMBER, phones[i]) - .withValue(CommonDataKinds.Phone.TYPE, phonesLabels[i]); + if (phoneNumbers != null) { + // remove existing phoneNumbers first + op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI) + .withSelection( + ContactsContract.Data.MIMETYPE + "=? AND "+ ContactsContract.Data.RAW_CONTACT_ID + " = ?", + new String[]{String.valueOf(CommonDataKinds.Phone.CONTENT_ITEM_TYPE), String.valueOf(rawContactId)} + ); ops.add(op.build()); + + // add passed phonenumbers + for (int i = 0; i < numOfPhones; i++) { + op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId)) + .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE) + .withValue(CommonDataKinds.Phone.NUMBER, phones[i]) + .withValue(CommonDataKinds.Phone.TYPE, phonesTypes[i]) + .withValue(CommonDataKinds.Phone.LABEL, phonesLabels[i]); + ops.add(op.build()); + } } for (int i = 0; i < numOfUrls; i++) { @@ -605,22 +657,25 @@ public void updateContact(ReadableMap contact, Callback callback) { ops.add(op.build()); } - // remove existing emails first - op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI) - .withSelection( - ContactsContract.Data.MIMETYPE + "=? AND "+ ContactsContract.Data.CONTACT_ID + " = ?", - new String[]{String.valueOf(CommonDataKinds.Email.CONTENT_ITEM_TYPE), String.valueOf(recordID)} - ); - ops.add(op.build()); - - // add passed email addresses - for (int i = 0; i < numOfEmails; i++) { - op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) - .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId)) - .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Email.CONTENT_ITEM_TYPE) - .withValue(CommonDataKinds.Email.ADDRESS, emails[i]) - .withValue(CommonDataKinds.Email.TYPE, emailsLabels[i]); + if (emailAddresses != null){ + // remove existing emails first + op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI) + .withSelection( + ContactsContract.Data.MIMETYPE + "=? AND "+ ContactsContract.Data.RAW_CONTACT_ID + " = ?", + new String[]{String.valueOf(CommonDataKinds.Email.CONTENT_ITEM_TYPE), String.valueOf(rawContactId)} + ); ops.add(op.build()); + + // add passed email addresses + for (int i = 0; i < numOfEmails; i++) { + op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId)) + .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Email.CONTENT_ITEM_TYPE) + .withValue(CommonDataKinds.Email.ADDRESS, emails[i]) + .withValue(CommonDataKinds.Email.TYPE, emailsTypes[i]) + .withValue(CommonDataKinds.Email.LABEL, emailsLabels[i]); + ops.add(op.build()); + } } if(thumbnailPath != null && !thumbnailPath.isEmpty()) { @@ -635,19 +690,26 @@ public void updateContact(ReadableMap contact, Callback callback) { } } - ReadableArray postalAddresses = contact.hasKey("postalAddresses") ? contact.getArray("postalAddresses") : null; - if (postalAddresses != null) { - for (int i = 0; i < postalAddresses.size(); i++) { - ReadableMap address = postalAddresses.getMap(i); - op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) - .withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[]{String.valueOf(recordID), CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE}) + if (postalAddresses != null){ + //remove existing addresses + op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI) + .withSelection( + ContactsContract.Data.MIMETYPE + "=? AND "+ ContactsContract.Data.RAW_CONTACT_ID + " = ?", + new String[]{String.valueOf(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE), String.valueOf(rawContactId)} + ); + ops.add(op.build()); + + for (int i = 0; i < numOfPostalAddresses; i++) { + op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) + .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId)) .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE) - .withValue(CommonDataKinds.StructuredPostal.TYPE, mapStringToPostalAddressType(address.getString("label"))) - .withValue(CommonDataKinds.StructuredPostal.STREET, address.getString("street")) - .withValue(CommonDataKinds.StructuredPostal.CITY, address.getString("city")) - .withValue(CommonDataKinds.StructuredPostal.REGION, address.getString("state")) - .withValue(CommonDataKinds.StructuredPostal.POSTCODE, address.getString("postCode")) - .withValue(CommonDataKinds.StructuredPostal.COUNTRY, address.getString("country")); + .withValue(CommonDataKinds.StructuredPostal.TYPE, postalAddressesType[i]) + .withValue(CommonDataKinds.StructuredPostal.LABEL, postalAddressesLabel[i]) + .withValue(CommonDataKinds.StructuredPostal.STREET, postalAddressesStreet[i]) + .withValue(CommonDataKinds.StructuredPostal.CITY, postalAddressesCity[i]) + .withValue(CommonDataKinds.StructuredPostal.REGION, postalAddressesState[i]) + .withValue(CommonDataKinds.StructuredPostal.POSTCODE, postalAddressesPostCode[i]) + .withValue(CommonDataKinds.StructuredPostal.COUNTRY, postalAddressesCountry[i]); ops.add(op.build()); } } @@ -775,8 +837,26 @@ private int mapStringToPhoneType(String label) { case "mobile": phoneType = CommonDataKinds.Phone.TYPE_MOBILE; break; + case "main": + phoneType = CommonDataKinds.Phone.TYPE_MAIN; + break; + case "work fax": + phoneType = CommonDataKinds.Phone.TYPE_FAX_WORK; + break; + case "home fax": + phoneType = CommonDataKinds.Phone.TYPE_FAX_HOME; + break; + case "pager": + phoneType = CommonDataKinds.Phone.TYPE_PAGER; + break; + case "work_pager": + phoneType = CommonDataKinds.Phone.TYPE_WORK_PAGER; + break; + case "work_mobile": + phoneType = CommonDataKinds.Phone.TYPE_WORK_MOBILE; + break; default: - phoneType = CommonDataKinds.Phone.TYPE_OTHER; + phoneType = CommonDataKinds.Phone.TYPE_CUSTOM; break; } return phoneType; @@ -799,7 +879,7 @@ private int mapStringToEmailType(String label) { emailType = CommonDataKinds.Email.TYPE_MOBILE; break; default: - emailType = CommonDataKinds.Email.TYPE_OTHER; + emailType = CommonDataKinds.Email.TYPE_CUSTOM; break; } return emailType; @@ -815,7 +895,7 @@ private int mapStringToPostalAddressType(String label) { postalAddressType = CommonDataKinds.StructuredPostal.TYPE_WORK; break; default: - postalAddressType = CommonDataKinds.StructuredPostal.TYPE_OTHER; + postalAddressType = CommonDataKinds.StructuredPostal.TYPE_CUSTOM; break; } return postalAddressType;