Skip to content

Commit

Permalink
android performance fix + added addresses (morenoh149#127)
Browse files Browse the repository at this point in the history
 * android performance fix - no longer repeatedly creating files for contact icons

 * added addresses for both iOS and android
  • Loading branch information
npomfret authored Dec 19, 2016
1 parent f63ff26 commit 88311b6
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 88 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Rx support with [react-native-contacts-rx](https://github.com/JeanLebrument/reac

## API
`getAll` (callback) - returns *all* contacts as an array of objects
`getAllWithoutPhotos` - same as `getAll` on Android, but on iOS it will not return uris for contact photos (because there's a significant overhead in creating the images)
`getPhotoForId(contactId, callback)` - returns a URI (or null) for a contacts photo
`addContact` (contact, callback) - adds a contact to the AddressBook.
`updateContact` (contact, callback) - where contact is an object with a valid recordID
`deleteContact` (contact, callback) - where contact is an object with a valid recordID
Expand All @@ -44,7 +46,7 @@ Contacts.getAll((err, contacts) => {
## Example Contact Record
```js
{
recordID: 1,
recordID: '6b2237ee0df85980',
company: "",
emailAddresses: [{
label: "work",
Expand All @@ -58,7 +60,18 @@ Contacts.getAll((err, contacts) => {
label: "mobile",
number: "(555) 555-5555",
}],
thumbnailPath: "",
thumbnailPath: 'content://com.android.contacts/display_photo/3',
postalAddresses:
[
{
postCode: 'Postcooode',
city: 'City',
neighborhood: 'neighborhood',
street: 'Home Street',
formattedAddress: 'Home Street\nneighborhood\nCity Postcooode',
label: 'work'
}
]
}
```
**NOTE**
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ repositories {
}

dependencies {
compile 'com.facebook.react:react-native:0.12.+'
compile 'com.facebook.react:react-native:+'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.RawContacts;

import com.facebook.react.bridge.Callback;
Expand All @@ -33,20 +33,59 @@ public ContactsManager(ReactApplicationContext reactContext) {
*/
@ReactMethod
public void getAll(final Callback callback) {
getAllContacts(callback);
}

/**
* Introduced for iOS compatibility. Same as getAll
*
* @param callback callback
*/
@ReactMethod
public void getAllWithoutPhotos(final Callback callback) {
getAllContacts(callback);
}

/**
* Retrieves contacts.
* Uses raw URI when <code>rawUri</code> is <code>true</code>, makes assets copy otherwise.
* @param callback callback
*/
private void getAllContacts(final Callback callback) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
Context context = getReactApplicationContext();
ContentResolver cr = context.getContentResolver();

ContactsProvider contactsProvider = new ContactsProvider(cr, context);
ContactsProvider contactsProvider = new ContactsProvider(cr);
WritableArray contacts = contactsProvider.getContacts();

callback.invoke(null, contacts);
}
});
}

/**
* Retrieves <code>thumbnailPath</code> for contact, or <code>null</code> if not available.
* @param contactId contact identifier, <code>recordID</code>
* @param callback callback
*/
@ReactMethod
public void getPhotoForId(final String contactId, final Callback callback) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
Context context = getReactApplicationContext();
ContentResolver cr = context.getContentResolver();
ContactsProvider contactsProvider = new ContactsProvider(cr);
String photoUri = contactsProvider.getPhotoUriFromContactId(contactId);

callback.invoke(null, photoUri);
}
});
}

/*
* Adds contact to phone's addressbook
*/
Expand Down Expand Up @@ -220,7 +259,7 @@ public void updateContact(ReadableMap contact, Callback callback) {

for (int i = 0; i < numOfPhones; i++) {
op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[]{String.valueOf(recordID), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE})
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[]{String.valueOf(recordID), CommonDataKinds.Phone.CONTENT_ITEM_TYPE})
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Phone.NUMBER, phones[i])
.withValue(CommonDataKinds.Phone.TYPE, phonesLabels[i]);
Expand All @@ -229,7 +268,7 @@ public void updateContact(ReadableMap contact, Callback callback) {

for (int i = 0; i < numOfEmails; i++) {
op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[]{String.valueOf(recordID), ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE})
.withSelection(ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[]{String.valueOf(recordID), CommonDataKinds.Email.CONTENT_ITEM_TYPE})
.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(CommonDataKinds.Email.ADDRESS, emails[i])
.withValue(CommonDataKinds.Email.TYPE, emailsLabels[i]);
Expand Down
Loading

0 comments on commit 88311b6

Please sign in to comment.