Skip to content

Commit

Permalink
[Android] Several Fixes with updateContact (#349)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Git-Hub-Admin authored and morenoh149 committed Jan 21, 2019
1 parent ef7483e commit 11cdce7
Showing 1 changed file with 138 additions and 58 deletions.
196 changes: 138 additions & 58 deletions android/src/main/java/com/rt2zz/reactnativecontacts/ContactsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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"))
Expand Down Expand Up @@ -492,20 +501,23 @@ 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);
String phoneNumber = phoneMap.getString("number");
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;
}
}
Expand All @@ -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<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ContentProviderOperation.Builder op = ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI)
Expand Down Expand Up @@ -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++) {
Expand All @@ -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()) {
Expand All @@ -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());
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 11cdce7

Please sign in to comment.