From 04310820ccc3904d82fa212f74a59d919e00bac5 Mon Sep 17 00:00:00 2001 From: Luuc Date: Fri, 18 Jan 2019 23:06:04 +0100 Subject: [PATCH] updateContact update phone numbers and emails instead of insert [Android] (#348) * update email instead of insert * removes all numbers of contact and then adds the passed ones * emails now added but removed and then added, so removes duplicates * remove from buglist --- README.md | 1 - .../reactnativecontacts/ContactsManager.java | 54 ++++++++++--------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 60cee9d2..30cc1f77 100644 --- a/README.md +++ b/README.md @@ -267,7 +267,6 @@ Update reference contacts by their recordID (as returned by the OS in getContact There are issues with updating contacts on Android: 1. custom labels get overwritten to "Other", 1. postal address update code doesn't exist. (it exists for addContact) -1. phoneNumbers and emails insert instead of update. See https://github.com/rt2zz/react-native-contacts/issues/332#issuecomment-455675041 for current discussions. ## Delete Contacts diff --git a/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java b/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java index 8962d7e5..4fa6408a 100644 --- a/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java +++ b/android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java @@ -474,7 +474,7 @@ public byte[] toByteArray(Bitmap bitmap) { */ @ReactMethod public void updateContact(ReadableMap contact, Callback callback) { - + String recordID = contact.hasKey("recordID") ? contact.getString("recordID") : null; String rawContactId = contact.hasKey("rawContactId") ? contact.getString("rawContactId") : null; @@ -573,19 +573,21 @@ 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++) { - if (phoneIds[i] == null) { - 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]); - } else { - op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) - .withSelection(ContactsContract.Data._ID + "=?", new String[]{String.valueOf(phoneIds[i])}) - .withValue(CommonDataKinds.Phone.NUMBER, phones[i]) - .withValue(CommonDataKinds.Phone.TYPE, phonesLabels[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]); ops.add(op.build()); } @@ -603,19 +605,21 @@ 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++) { - if (emailIds[i] == null) { - 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]); - } else { - op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI) - .withSelection(ContactsContract.Data._ID + "=?", new String[]{String.valueOf(emailIds[i])}) - .withValue(CommonDataKinds.Email.ADDRESS, emails[i]) - .withValue(CommonDataKinds.Email.TYPE, emailsLabels[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]); ops.add(op.build()); }