react-native-contacts 8.0.2 → 8.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1336 @@
1
+ package com.rt2zz.reactnativecontacts.impl;
2
+
3
+ import android.Manifest;
4
+ import android.app.Activity;
5
+ import android.content.ContentProviderOperation;
6
+ import android.content.ContentProviderResult;
7
+ import android.content.ContentResolver;
8
+ import android.content.ContentUris;
9
+ import android.content.ContentValues;
10
+ import android.content.Context;
11
+ import android.content.Intent;
12
+ import android.content.pm.PackageManager;
13
+ import android.content.res.AssetManager;
14
+ import android.graphics.Bitmap;
15
+ import android.graphics.BitmapFactory;
16
+ import android.net.Uri;
17
+ import android.os.Bundle;
18
+ import android.provider.ContactsContract;
19
+
20
+ import androidx.annotation.NonNull;
21
+ import androidx.annotation.Nullable;
22
+ import androidx.core.app.ActivityCompat;
23
+
24
+ import com.facebook.react.bridge.Promise;
25
+ import com.facebook.react.bridge.ReactApplicationContext;
26
+ import com.facebook.react.bridge.ReadableArray;
27
+ import com.facebook.react.bridge.ReadableMap;
28
+ import com.facebook.react.bridge.WritableArray;
29
+ import com.facebook.react.bridge.WritableMap;
30
+ import com.rt2zz.reactnativecontacts.ContactsProvider;
31
+
32
+ import java.io.ByteArrayOutputStream;
33
+ import java.io.FileNotFoundException;
34
+ import java.io.FileOutputStream;
35
+ import java.io.IOException;
36
+ import java.io.InputStream;
37
+ import java.io.OutputStream;
38
+ import java.util.ArrayList;
39
+ import java.util.Hashtable;
40
+ import java.util.Objects;
41
+ import java.util.concurrent.Executor;
42
+ import java.util.concurrent.Executors;
43
+
44
+ public class ContactsManagerImpl {
45
+
46
+ private static final String PERMISSION_DENIED = "denied";
47
+ private static final String PERMISSION_AUTHORIZED = "authorized";
48
+ private static final String PERMISSION_READ_CONTACTS = Manifest.permission.READ_CONTACTS;
49
+ private static final int PERMISSION_REQUEST_CODE = 888;
50
+ private static final int REQUEST_OPEN_CONTACT_FORM = 52941;
51
+ private static final int REQUEST_OPEN_EXISTING_CONTACT = 52942;
52
+
53
+ private static Promise updateContactPromise;
54
+ private static Promise requestPromise;
55
+
56
+ private final ReactApplicationContext reactApplicationContext;
57
+
58
+ private Executor executor;
59
+
60
+
61
+ public ContactsManagerImpl(ReactApplicationContext reactContext, boolean useSerialExecutor) {
62
+ this.reactApplicationContext = reactContext;
63
+ this.executor = initializeExecutor(useSerialExecutor);
64
+ }
65
+
66
+ private Executor initializeExecutor(boolean useSerialExecutor){
67
+ if(useSerialExecutor){
68
+ return Executors.newSingleThreadExecutor(); //AsyncTask.SERIAL_EXECUTOR
69
+ }
70
+ return Executors.newCachedThreadPool();
71
+ }
72
+
73
+ protected Executor getExecutor() {
74
+ return executor;
75
+ }
76
+
77
+ @NonNull
78
+ protected ReactApplicationContext getReactApplicationContext() {
79
+ return Objects.requireNonNull(reactApplicationContext, "Context not initialized");
80
+ }
81
+
82
+ @Nullable
83
+ protected final Activity getCurrentActivity() {
84
+ return reactApplicationContext.getCurrentActivity();
85
+ }
86
+
87
+ /*
88
+ * Returns all contactable records on phone
89
+ * queries CommonDataKinds.Contactables to get phones and emails
90
+ */
91
+ public void getAll(Promise promise) {
92
+ getAllContacts(promise);
93
+ }
94
+
95
+ /**
96
+ * Introduced for iOS compatibility. Same as getAll
97
+ *
98
+ * @param promise promise
99
+ */
100
+ public void getAllWithoutPhotos(Promise promise) {
101
+ getAllContacts(promise);
102
+ }
103
+
104
+ /**
105
+ * Retrieves contacts.
106
+ * Uses raw URI when <code>rawUri</code> is <code>true</code>, makes assets copy
107
+ * otherwise.
108
+ */
109
+ private void getAllContacts(final Promise promise) {
110
+ getExecutor().execute(() -> {
111
+ Context context = getReactApplicationContext();
112
+ ContentResolver cr = context.getContentResolver();
113
+ ContactsProvider contactsProvider = new ContactsProvider(cr);
114
+ WritableArray contacts = contactsProvider.getContacts();
115
+ promise.resolve(contacts);
116
+ });
117
+ }
118
+
119
+ public void getCount(final Promise promise) {
120
+ getExecutor().execute(() -> {
121
+ Context context = getReactApplicationContext();
122
+ ContentResolver cr = context.getContentResolver();
123
+ ContactsProvider contactsProvider = new ContactsProvider(cr);
124
+ try {
125
+ Integer contacts = contactsProvider.getContactsCount();
126
+ promise.resolve(contacts);
127
+ } catch (Exception e) {
128
+ promise.reject(e);
129
+ }
130
+ });
131
+ }
132
+
133
+ /**
134
+ * Retrieves contacts matching String.
135
+ * Uses raw URI when <code>rawUri</code> is <code>true</code>, makes assets copy
136
+ * otherwise.
137
+ *
138
+ * @param searchString String to match
139
+ */
140
+ public void getContactsMatchingString(final String searchString, final Promise promise) {
141
+ getExecutor().execute(() -> {
142
+ Context context = getReactApplicationContext();
143
+ ContentResolver cr = context.getContentResolver();
144
+ ContactsProvider contactsProvider = new ContactsProvider(cr);
145
+ WritableArray contacts = contactsProvider.getContactsMatchingString(searchString);
146
+ promise.resolve(contacts);
147
+ });
148
+ }
149
+
150
+ /**
151
+ * Retrieves contacts matching a phone number.
152
+ * Uses raw URI when <code>rawUri</code> is <code>true</code>, makes assets copy
153
+ * otherwise.
154
+ *
155
+ * @param phoneNumber phone number to match
156
+ */
157
+ public void getContactsByPhoneNumber(final String phoneNumber, final Promise promise) {
158
+ getExecutor().execute(new Runnable() {
159
+ @Override
160
+ public void run() {
161
+ Context context = getReactApplicationContext();
162
+ ContentResolver cr = context.getContentResolver();
163
+ ContactsProvider contactsProvider = new ContactsProvider(cr);
164
+ WritableArray contacts = contactsProvider.getContactsByPhoneNumber(phoneNumber);
165
+ promise.resolve(contacts);
166
+ }
167
+ });
168
+ }
169
+
170
+ /**
171
+ * Retrieves contacts matching an email address.
172
+ * Uses raw URI when <code>rawUri</code> is <code>true</code>, makes assets copy
173
+ * otherwise.
174
+ *
175
+ * @param emailAddress email address to match
176
+ */
177
+ public void getContactsByEmailAddress(final String emailAddress, final Promise promise) {
178
+ getExecutor().execute(() -> {
179
+ Context context = getReactApplicationContext();
180
+ ContentResolver cr = context.getContentResolver();
181
+ ContactsProvider contactsProvider = new ContactsProvider(cr);
182
+ WritableArray contacts = contactsProvider.getContactsByEmailAddress(emailAddress);
183
+ promise.resolve(contacts);
184
+ });
185
+ }
186
+
187
+ /**
188
+ * Retrieves <code>thumbnailPath</code> for contact, or <code>null</code> if not
189
+ * available.
190
+ *
191
+ * @param contactId contact identifier, <code>recordID</code>
192
+ */
193
+ public void getPhotoForId(final String contactId, final Promise promise) {
194
+ getExecutor().execute(() -> {
195
+ Context context = getReactApplicationContext();
196
+ ContentResolver cr = context.getContentResolver();
197
+ ContactsProvider contactsProvider = new ContactsProvider(cr);
198
+ String photoUri = contactsProvider.getPhotoUriFromContactId(contactId);
199
+ promise.resolve(photoUri);
200
+ });
201
+ }
202
+
203
+ /**
204
+ * Retrieves <code>contact</code> for contact, or <code>null</code> if not
205
+ * available.
206
+ *
207
+ * @param contactId contact identifier, <code>recordID</code>
208
+ */
209
+ public void getContactById(final String contactId, final Promise promise) {
210
+ getExecutor().execute(() -> {
211
+ Context context = getReactApplicationContext();
212
+ ContentResolver cr = context.getContentResolver();
213
+ ContactsProvider contactsProvider = new ContactsProvider(cr);
214
+ WritableMap contact = contactsProvider.getContactById(contactId);
215
+ promise.resolve(contact);
216
+ });
217
+ }
218
+
219
+
220
+ public void writePhotoToPath(final String contactId, final String file, final Promise promise) {
221
+ getExecutor().execute(() -> {
222
+ Context context = getReactApplicationContext();
223
+ ContentResolver cr = context.getContentResolver();
224
+
225
+ Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
226
+ InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
227
+ OutputStream outputStream = null;
228
+ try {
229
+ outputStream = new FileOutputStream(file);
230
+ BitmapFactory.decodeStream(inputStream).compress(Bitmap.CompressFormat.PNG, 100, outputStream);
231
+ promise.resolve(true);
232
+ } catch (FileNotFoundException e) {
233
+ promise.reject(e.toString());
234
+ } finally {
235
+ try {
236
+ if (outputStream != null) {
237
+ outputStream.close();
238
+ }
239
+ } catch (IOException e) {
240
+ e.printStackTrace();
241
+ }
242
+ }
243
+ try {
244
+ inputStream.close();
245
+ } catch (IOException e) {
246
+ e.printStackTrace();
247
+ }
248
+ });
249
+ }
250
+
251
+ private Bitmap getThumbnailBitmap(String thumbnailPath) {
252
+ // Thumbnail from absolute path
253
+ Bitmap photo = BitmapFactory.decodeFile(thumbnailPath);
254
+
255
+ if (photo == null) {
256
+ // Try to find the thumbnail from assets
257
+ AssetManager assetManager = getReactApplicationContext().getAssets();
258
+ InputStream inputStream = null;
259
+ try {
260
+ inputStream = assetManager.open(thumbnailPath);
261
+ photo = BitmapFactory.decodeStream(inputStream);
262
+ inputStream.close();
263
+ } catch (IOException e) {
264
+ e.printStackTrace();
265
+ }
266
+ }
267
+
268
+ return photo;
269
+ }
270
+
271
+ /*
272
+ * Start open contact form
273
+ */
274
+ public void openContactForm(ReadableMap contact, Promise promise) {
275
+
276
+ String givenName = contact.hasKey("givenName") ? contact.getString("givenName") : null;
277
+ String middleName = contact.hasKey("middleName") ? contact.getString("middleName") : null;
278
+ String displayName = contact.hasKey("displayName") ? contact.getString("displayName") : null;
279
+ String familyName = contact.hasKey("familyName") ? contact.getString("familyName") : null;
280
+ String prefix = contact.hasKey("prefix") ? contact.getString("prefix") : null;
281
+ String suffix = contact.hasKey("suffix") ? contact.getString("suffix") : null;
282
+ String company = contact.hasKey("company") ? contact.getString("company") : null;
283
+ String jobTitle = contact.hasKey("jobTitle") ? contact.getString("jobTitle") : null;
284
+ String department = contact.hasKey("department") ? contact.getString("department") : null;
285
+ String note = contact.hasKey("note") ? contact.getString("note") : null;
286
+ String thumbnailPath = contact.hasKey("thumbnailPath") ? contact.getString("thumbnailPath") : null;
287
+
288
+ ReadableArray phoneNumbers = contact.hasKey("phoneNumbers") ? contact.getArray("phoneNumbers") : null;
289
+ int numOfPhones = 0;
290
+ String[] phones = null;
291
+ String[] phonesLabels = null;
292
+ Integer[] phonesLabelsTypes = null;
293
+ if (phoneNumbers != null) {
294
+ numOfPhones = phoneNumbers.size();
295
+ phones = new String[numOfPhones];
296
+ phonesLabels = new String[numOfPhones];
297
+ phonesLabelsTypes = new Integer[numOfPhones];
298
+ for (int i = 0; i < numOfPhones; i++) {
299
+ phones[i] = phoneNumbers.getMap(i).getString("number");
300
+ String label = phoneNumbers.getMap(i).getString("label");
301
+ phonesLabels[i] = label;
302
+ phonesLabelsTypes[i] = mapStringToPhoneType(label);
303
+ }
304
+ }
305
+
306
+ ReadableArray urlAddresses = contact.hasKey("urlAddresses") ? contact.getArray("urlAddresses") : null;
307
+ int numOfUrls = 0;
308
+ String[] urls = null;
309
+ if (urlAddresses != null) {
310
+ numOfUrls = urlAddresses.size();
311
+ urls = new String[numOfUrls];
312
+ for (int i = 0; i < numOfUrls; i++) {
313
+ urls[i] = urlAddresses.getMap(i).getString("url");
314
+ }
315
+ }
316
+
317
+ ReadableArray emailAddresses = contact.hasKey("emailAddresses") ? contact.getArray("emailAddresses") : null;
318
+ int numOfEmails = 0;
319
+ String[] emails = null;
320
+ Integer[] emailsLabels = null;
321
+ if (emailAddresses != null) {
322
+ numOfEmails = emailAddresses.size();
323
+ emails = new String[numOfEmails];
324
+ emailsLabels = new Integer[numOfEmails];
325
+ for (int i = 0; i < numOfEmails; i++) {
326
+ emails[i] = emailAddresses.getMap(i).getString("email");
327
+ String label = emailAddresses.getMap(i).getString("label");
328
+ emailsLabels[i] = mapStringToEmailType(label);
329
+ }
330
+ }
331
+
332
+ ReadableArray postalAddresses = contact.hasKey("postalAddresses") ? contact.getArray("postalAddresses") : null;
333
+ int numOfPostalAddresses = 0;
334
+ String[] postalAddressesStreet = null;
335
+ String[] postalAddressesCity = null;
336
+ String[] postalAddressesState = null;
337
+ String[] postalAddressesRegion = null;
338
+ String[] postalAddressesPostCode = null;
339
+ String[] postalAddressesCountry = null;
340
+ String[] postalAddressesFormattedAddress = null;
341
+ String[] postalAddressesLabel = null;
342
+ Integer[] postalAddressesType = null;
343
+
344
+ if (postalAddresses != null) {
345
+ numOfPostalAddresses = postalAddresses.size();
346
+ postalAddressesStreet = new String[numOfPostalAddresses];
347
+ postalAddressesCity = new String[numOfPostalAddresses];
348
+ postalAddressesState = new String[numOfPostalAddresses];
349
+ postalAddressesRegion = new String[numOfPostalAddresses];
350
+ postalAddressesPostCode = new String[numOfPostalAddresses];
351
+ postalAddressesCountry = new String[numOfPostalAddresses];
352
+ postalAddressesFormattedAddress = new String[numOfPostalAddresses];
353
+ postalAddressesLabel = new String[numOfPostalAddresses];
354
+ postalAddressesType = new Integer[numOfPostalAddresses];
355
+ for (int i = 0; i < numOfPostalAddresses; i++) {
356
+ postalAddressesStreet[i] = postalAddresses.getMap(i).getString("street");
357
+ postalAddressesCity[i] = postalAddresses.getMap(i).getString("city");
358
+ postalAddressesState[i] = postalAddresses.getMap(i).getString("state");
359
+ postalAddressesRegion[i] = postalAddresses.getMap(i).getString("region");
360
+ postalAddressesPostCode[i] = postalAddresses.getMap(i).getString("postCode");
361
+ postalAddressesCountry[i] = postalAddresses.getMap(i).getString("country");
362
+ postalAddressesFormattedAddress[i] = postalAddresses.getMap(i).getString("formattedAddress");
363
+ postalAddressesLabel[i] = postalAddresses.getMap(i).getString("label");
364
+ postalAddressesType[i] = mapStringToPostalAddressType(postalAddresses.getMap(i).getString("label"));
365
+ }
366
+ }
367
+
368
+ ReadableArray imAddresses = contact.hasKey("imAddresses") ? contact.getArray("imAddresses") : null;
369
+ int numOfIMAddresses = 0;
370
+ String[] imAccounts = null;
371
+ String[] imProtocols = null;
372
+ if (imAddresses != null) {
373
+ numOfIMAddresses = imAddresses.size();
374
+ imAccounts = new String[numOfIMAddresses];
375
+ imProtocols = new String[numOfIMAddresses];
376
+ for (int i = 0; i < numOfIMAddresses; i++) {
377
+ imAccounts[i] = imAddresses.getMap(i).getString("username");
378
+ imProtocols[i] = imAddresses.getMap(i).getString("service");
379
+ }
380
+ }
381
+
382
+ ArrayList<ContentValues> contactData = new ArrayList<>();
383
+
384
+ ContentValues name = new ContentValues();
385
+ name.put(ContactsContract.Contacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Identity.CONTENT_ITEM_TYPE);
386
+ name.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName);
387
+ name.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName);
388
+ name.put(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName);
389
+ name.put(ContactsContract.CommonDataKinds.StructuredName.PREFIX, prefix);
390
+ name.put(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, suffix);
391
+ contactData.add(name);
392
+
393
+ ContentValues organization = new ContentValues();
394
+ organization.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
395
+ organization.put(ContactsContract.CommonDataKinds.Organization.COMPANY, company);
396
+ organization.put(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle);
397
+ organization.put(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, department);
398
+ contactData.add(organization);
399
+
400
+ for (int i = 0; i < numOfUrls; i++) {
401
+ ContentValues url = new ContentValues();
402
+ url.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
403
+ url.put(ContactsContract.CommonDataKinds.Website.URL, urls[i]);
404
+ contactData.add(url);
405
+ }
406
+
407
+ for (int i = 0; i < numOfEmails; i++) {
408
+ ContentValues email = new ContentValues();
409
+ email.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
410
+ email.put(ContactsContract.CommonDataKinds.Email.TYPE, emailsLabels[i]);
411
+ email.put(ContactsContract.CommonDataKinds.Email.ADDRESS, emails[i]);
412
+ contactData.add(email);
413
+ }
414
+
415
+ for (int i = 0; i < numOfPhones; i++) {
416
+ ContentValues phone = new ContentValues();
417
+ phone.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
418
+ phone.put(ContactsContract.CommonDataKinds.Phone.TYPE, phonesLabelsTypes[i]);
419
+ phone.put(ContactsContract.CommonDataKinds.Phone.LABEL, phonesLabels[i]);
420
+ phone.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phones[i]);
421
+
422
+ contactData.add(phone);
423
+ }
424
+
425
+ for (int i = 0; i < numOfPostalAddresses; i++) {
426
+ ContentValues structuredPostal = new ContentValues();
427
+ structuredPostal.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
428
+ structuredPostal.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, postalAddressesStreet[i]);
429
+ structuredPostal.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, postalAddressesCity[i]);
430
+ structuredPostal.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION, postalAddressesRegion[i]);
431
+ structuredPostal.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, postalAddressesCountry[i]);
432
+ structuredPostal.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, postalAddressesPostCode[i]);
433
+ structuredPostal.put(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
434
+ postalAddressesFormattedAddress[i]);
435
+ structuredPostal.put(ContactsContract.CommonDataKinds.StructuredPostal.LABEL, postalAddressesLabel[i]);
436
+ structuredPostal.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, postalAddressesType[i]);
437
+ // No state column in StructuredPostal
438
+ // structuredPostal.put(CommonDataKinds.StructuredPostal.???,
439
+ // postalAddressesState[i]);
440
+ contactData.add(structuredPostal);
441
+ }
442
+
443
+ for (int i = 0; i < numOfIMAddresses; i++) {
444
+ ContentValues imAddress = new ContentValues();
445
+ imAddress.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
446
+ imAddress.put(ContactsContract.CommonDataKinds.Im.DATA, imAccounts[i]);
447
+ imAddress.put(ContactsContract.CommonDataKinds.Im.TYPE, ContactsContract.CommonDataKinds.Im.TYPE_HOME);
448
+ imAddress.put(ContactsContract.CommonDataKinds.Im.PROTOCOL, ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM);
449
+ imAddress.put(ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL, imProtocols[i]);
450
+ contactData.add(imAddress);
451
+ }
452
+
453
+ if (note != null) {
454
+ ContentValues structuredNote = new ContentValues();
455
+ structuredNote.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
456
+ structuredNote.put(ContactsContract.CommonDataKinds.Note.NOTE, note);
457
+ contactData.add(structuredNote);
458
+ }
459
+
460
+ if (thumbnailPath != null && !thumbnailPath.isEmpty()) {
461
+ Bitmap photo = getThumbnailBitmap(thumbnailPath);
462
+
463
+ if (photo != null) {
464
+ ContentValues thumbnail = new ContentValues();
465
+ thumbnail.put(ContactsContract.Data.RAW_CONTACT_ID, 0);
466
+ thumbnail.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
467
+ thumbnail.put(ContactsContract.CommonDataKinds.Photo.PHOTO, toByteArray(photo));
468
+ thumbnail.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
469
+ contactData.add(thumbnail);
470
+ }
471
+ }
472
+
473
+ Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
474
+ intent.putExtra(ContactsContract.Intents.Insert.NAME, displayName);
475
+ intent.putExtra("finishActivityOnSaveCompleted", true);
476
+ intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, contactData);
477
+
478
+ updateContactPromise = promise;
479
+ getReactApplicationContext().startActivityForResult(intent, REQUEST_OPEN_CONTACT_FORM, Bundle.EMPTY);
480
+ }
481
+
482
+ /*
483
+ * Open contact in native app
484
+ */
485
+ public void openExistingContact(ReadableMap contact, Promise promise) {
486
+
487
+ String recordID = contact.hasKey("recordID") ? contact.getString("recordID") : null;
488
+
489
+ try {
490
+ Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, recordID);
491
+ Intent intent = new Intent(Intent.ACTION_EDIT);
492
+ intent.setDataAndType(uri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
493
+ intent.putExtra("finishActivityOnSaveCompleted", true);
494
+
495
+ updateContactPromise = promise;
496
+ getReactApplicationContext().startActivityForResult(intent, REQUEST_OPEN_EXISTING_CONTACT, Bundle.EMPTY);
497
+
498
+ } catch (Exception e) {
499
+ promise.reject(e.toString());
500
+ }
501
+ }
502
+
503
+ /*
504
+ * View contact in native app
505
+ */
506
+ public void viewExistingContact(ReadableMap contact, Promise promise) {
507
+
508
+ String recordID = contact.hasKey("recordID") ? contact.getString("recordID") : null;
509
+
510
+ try {
511
+ Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, recordID);
512
+ Intent intent = new Intent(Intent.ACTION_VIEW);
513
+ intent.setDataAndType(uri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
514
+ intent.putExtra("finishActivityOnSaveCompleted", true);
515
+
516
+ updateContactPromise = promise;
517
+ getReactApplicationContext().startActivityForResult(intent, REQUEST_OPEN_EXISTING_CONTACT, Bundle.EMPTY);
518
+
519
+ } catch (Exception e) {
520
+ promise.reject(e.toString());
521
+ }
522
+ }
523
+
524
+ /*
525
+ * Edit contact in native app
526
+ */
527
+ public void editExistingContact(ReadableMap contact, Promise promise) {
528
+
529
+ String recordID = contact.hasKey("recordID") ? contact.getString("recordID") : null;
530
+
531
+ try {
532
+ Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, recordID);
533
+
534
+ ReadableArray phoneNumbers = contact.hasKey("phoneNumbers") ? contact.getArray("phoneNumbers") : null;
535
+ int numOfPhones = 0;
536
+ String[] phones = null;
537
+ Integer[] phonesLabels = null;
538
+ if (phoneNumbers != null) {
539
+ numOfPhones = phoneNumbers.size();
540
+ phones = new String[numOfPhones];
541
+ phonesLabels = new Integer[numOfPhones];
542
+ for (int i = 0; i < numOfPhones; i++) {
543
+ phones[i] = phoneNumbers.getMap(i).getString("number");
544
+ String label = phoneNumbers.getMap(i).getString("label");
545
+ phonesLabels[i] = mapStringToPhoneType(label);
546
+ }
547
+ }
548
+
549
+ ArrayList<ContentValues> contactData = new ArrayList<>();
550
+ for (int i = 0; i < numOfPhones; i++) {
551
+ ContentValues phone = new ContentValues();
552
+ phone.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
553
+ phone.put(ContactsContract.CommonDataKinds.Phone.TYPE, phonesLabels[i]);
554
+ phone.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phones[i]);
555
+ contactData.add(phone);
556
+ }
557
+
558
+ Intent intent = new Intent(Intent.ACTION_EDIT);
559
+ intent.setDataAndType(uri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
560
+ intent.putExtra("finishActivityOnSaveCompleted", true);
561
+ intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, contactData);
562
+
563
+ updateContactPromise = promise;
564
+ getReactApplicationContext().startActivityForResult(intent, REQUEST_OPEN_EXISTING_CONTACT, Bundle.EMPTY);
565
+
566
+ } catch (Exception e) {
567
+ promise.reject(e.toString());
568
+ }
569
+ }
570
+
571
+ /*
572
+ * Adds contact to phone's addressbook
573
+ */
574
+ public void addContact(ReadableMap contact, Promise promise) {
575
+ if (contact == null) {
576
+ promise.reject("New contact cannot be null.");
577
+ return;
578
+ }
579
+ String givenName = contact.hasKey("givenName") ? contact.getString("givenName") : null;
580
+ String middleName = contact.hasKey("middleName") ? contact.getString("middleName") : null;
581
+ String familyName = contact.hasKey("familyName") ? contact.getString("familyName") : null;
582
+ String prefix = contact.hasKey("prefix") ? contact.getString("prefix") : null;
583
+ String suffix = contact.hasKey("suffix") ? contact.getString("suffix") : null;
584
+ String company = contact.hasKey("company") ? contact.getString("company") : null;
585
+ String jobTitle = contact.hasKey("jobTitle") ? contact.getString("jobTitle") : null;
586
+ String department = contact.hasKey("department") ? contact.getString("department") : null;
587
+ String note = contact.hasKey("note") ? contact.getString("note") : null;
588
+ String thumbnailPath = contact.hasKey("thumbnailPath") ? contact.getString("thumbnailPath") : null;
589
+
590
+ ReadableArray phoneNumbers = contact.hasKey("phoneNumbers") ? contact.getArray("phoneNumbers") : null;
591
+ int numOfPhones = 0;
592
+ String[] phones = null;
593
+ Integer[] phonesTypes = null;
594
+ String[] phonesLabels = null;
595
+ if (phoneNumbers != null) {
596
+ numOfPhones = phoneNumbers.size();
597
+ phones = new String[numOfPhones];
598
+ phonesTypes = new Integer[numOfPhones];
599
+ phonesLabels = new String[numOfPhones];
600
+ for (int i = 0; i < numOfPhones; i++) {
601
+ phones[i] = phoneNumbers.getMap(i).getString("number");
602
+ String label = phoneNumbers.getMap(i).getString("label");
603
+ phonesTypes[i] = mapStringToPhoneType(label);
604
+ phonesLabels[i] = label;
605
+ }
606
+ }
607
+
608
+ ReadableArray urlAddresses = contact.hasKey("urlAddresses") ? contact.getArray("urlAddresses") : null;
609
+ int numOfUrls = 0;
610
+ String[] urls = null;
611
+ if (urlAddresses != null) {
612
+ numOfUrls = urlAddresses.size();
613
+ urls = new String[numOfUrls];
614
+ for (int i = 0; i < numOfUrls; i++) {
615
+ urls[i] = urlAddresses.getMap(i).getString("url");
616
+ }
617
+ }
618
+
619
+ ReadableArray emailAddresses = contact.hasKey("emailAddresses") ? contact.getArray("emailAddresses") : null;
620
+ int numOfEmails = 0;
621
+ String[] emails = null;
622
+ Integer[] emailsTypes = null;
623
+ String[] emailsLabels = null;
624
+ if (emailAddresses != null) {
625
+ numOfEmails = emailAddresses.size();
626
+ emails = new String[numOfEmails];
627
+ emailsTypes = new Integer[numOfEmails];
628
+ emailsLabels = new String[numOfEmails];
629
+ for (int i = 0; i < numOfEmails; i++) {
630
+ emails[i] = emailAddresses.getMap(i).getString("email");
631
+ String label = emailAddresses.getMap(i).getString("label");
632
+ emailsTypes[i] = mapStringToEmailType(label);
633
+ emailsLabels[i] = label;
634
+ }
635
+ }
636
+
637
+ ReadableArray imAddresses = contact.hasKey("imAddresses") ? contact.getArray("imAddresses") : null;
638
+ int numOfIMAddresses = 0;
639
+ String[] imAccounts = null;
640
+ String[] imProtocols = null;
641
+ if (imAddresses != null) {
642
+ numOfIMAddresses = imAddresses.size();
643
+ imAccounts = new String[numOfIMAddresses];
644
+ imProtocols = new String[numOfIMAddresses];
645
+ for (int i = 0; i < numOfIMAddresses; i++) {
646
+ imAccounts[i] = imAddresses.getMap(i).getString("username");
647
+ imProtocols[i] = imAddresses.getMap(i).getString("service");
648
+ }
649
+ }
650
+
651
+ ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
652
+
653
+ ContentProviderOperation.Builder op = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
654
+ .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
655
+ .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null);
656
+ ops.add(op.build());
657
+
658
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
659
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
660
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
661
+ // .withValue(StructuredName.DISPLAY_NAME, name)
662
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName)
663
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
664
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName)
665
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, prefix)
666
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, suffix);
667
+ ops.add(op.build());
668
+
669
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
670
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
671
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
672
+ .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note);
673
+ ops.add(op.build());
674
+
675
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
676
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
677
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
678
+ .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company)
679
+ .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle)
680
+ .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, department);
681
+ ops.add(op.build());
682
+
683
+ // TODO not sure where to allow yields
684
+ op.withYieldAllowed(true);
685
+
686
+ for (int i = 0; i < numOfPhones; i++) {
687
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
688
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
689
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
690
+ .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phones[i])
691
+ .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, phonesTypes[i])
692
+ .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, phonesLabels[i]);
693
+ ops.add(op.build());
694
+ }
695
+
696
+ for (int i = 0; i < numOfUrls; i++) {
697
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
698
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
699
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
700
+ .withValue(ContactsContract.CommonDataKinds.Website.URL, urls[i]);
701
+ ops.add(op.build());
702
+ }
703
+
704
+ for (int i = 0; i < numOfEmails; i++) {
705
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
706
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
707
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
708
+ .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, emails[i])
709
+ .withValue(ContactsContract.CommonDataKinds.Email.TYPE, emailsTypes[i])
710
+ .withValue(ContactsContract.CommonDataKinds.Email.LABEL, emailsLabels[i]);
711
+ ops.add(op.build());
712
+ }
713
+
714
+ if (thumbnailPath != null && !thumbnailPath.isEmpty()) {
715
+ Bitmap photo = getThumbnailBitmap(thumbnailPath);
716
+
717
+ if (photo != null) {
718
+ ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
719
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
720
+ .withValue(ContactsContract.Data.MIMETYPE,
721
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
722
+ .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, toByteArray(photo))
723
+ .build());
724
+ }
725
+ }
726
+
727
+ ReadableArray postalAddresses = contact.hasKey("postalAddresses") ? contact.getArray("postalAddresses") : null;
728
+ if (postalAddresses != null) {
729
+ for (int i = 0; i < postalAddresses.size(); i++) {
730
+ ReadableMap address = postalAddresses.getMap(i);
731
+
732
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
733
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
734
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
735
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
736
+ mapStringToPostalAddressType(address.getString("label")))
737
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.LABEL, address.getString("label"))
738
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, address.getString("street"))
739
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, address.getString("city"))
740
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, address.getString("state"))
741
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, address.getString("postCode"))
742
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, address.getString("country"));
743
+
744
+ ops.add(op.build());
745
+ }
746
+ }
747
+
748
+ for (int i = 0; i < numOfIMAddresses; i++) {
749
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
750
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
751
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
752
+ .withValue(ContactsContract.CommonDataKinds.Im.DATA, imAccounts[i])
753
+ .withValue(ContactsContract.CommonDataKinds.Im.TYPE, ContactsContract.CommonDataKinds.Im.TYPE_HOME)
754
+ .withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL, ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM)
755
+ .withValue(ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL, imProtocols[i]);
756
+ ops.add(op.build());
757
+ }
758
+
759
+ Context ctx = getReactApplicationContext();
760
+ try {
761
+ ContentResolver cr = ctx.getContentResolver();
762
+ ContentProviderResult[] result = cr.applyBatch(ContactsContract.AUTHORITY, ops);
763
+
764
+ if (result != null && result.length > 0) {
765
+
766
+ String rawId = String.valueOf(ContentUris.parseId(result[0].uri));
767
+
768
+ ContactsProvider contactsProvider = new ContactsProvider(cr);
769
+ WritableMap newlyAddedContact = contactsProvider.getContactByRawId(rawId);
770
+
771
+ promise.resolve(newlyAddedContact); // success
772
+ }
773
+ } catch (Exception e) {
774
+ promise.reject(e.toString());
775
+ }
776
+ }
777
+
778
+ public byte[] toByteArray(Bitmap bitmap) {
779
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
780
+ bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
781
+ return stream.toByteArray();
782
+ }
783
+
784
+ /*
785
+ * Update contact to phone's addressbook
786
+ */
787
+ public void updateContact(ReadableMap contact, Promise promise) {
788
+
789
+ String recordID = contact.hasKey("recordID") ? contact.getString("recordID") : null;
790
+ String rawContactId = contact.hasKey("rawContactId") ? contact.getString("rawContactId") : null;
791
+
792
+ if (rawContactId == null || recordID == null) {
793
+ promise.reject("Invalid recordId or rawContactId");
794
+ return;
795
+ }
796
+
797
+ String givenName = contact.hasKey("givenName") ? contact.getString("givenName") : null;
798
+ String middleName = contact.hasKey("middleName") ? contact.getString("middleName") : null;
799
+ String familyName = contact.hasKey("familyName") ? contact.getString("familyName") : null;
800
+ String prefix = contact.hasKey("prefix") ? contact.getString("prefix") : null;
801
+ String suffix = contact.hasKey("suffix") ? contact.getString("suffix") : null;
802
+ String company = contact.hasKey("company") ? contact.getString("company") : null;
803
+ String jobTitle = contact.hasKey("jobTitle") ? contact.getString("jobTitle") : null;
804
+ String department = contact.hasKey("department") ? contact.getString("department") : null;
805
+ String note = contact.hasKey("note") ? contact.getString("note") : null;
806
+ String thumbnailPath = contact.hasKey("thumbnailPath") ? contact.getString("thumbnailPath") : null;
807
+
808
+ ReadableArray phoneNumbers = contact.hasKey("phoneNumbers") ? contact.getArray("phoneNumbers") : null;
809
+ int numOfPhones = 0;
810
+ String[] phones = null;
811
+ Integer[] phonesTypes = null;
812
+ String[] phonesLabels = null;
813
+ String[] phoneIds = null;
814
+ if (phoneNumbers != null) {
815
+ numOfPhones = phoneNumbers.size();
816
+ phones = new String[numOfPhones];
817
+ phonesTypes = new Integer[numOfPhones];
818
+ phonesLabels = new String[numOfPhones];
819
+ phoneIds = new String[numOfPhones];
820
+ for (int i = 0; i < numOfPhones; i++) {
821
+ ReadableMap phoneMap = phoneNumbers.getMap(i);
822
+ String phoneNumber = phoneMap.getString("number");
823
+ String phoneLabel = phoneMap.getString("label");
824
+ String phoneId = phoneMap.hasKey("id") ? phoneMap.getString("id") : null;
825
+ phones[i] = phoneNumber;
826
+ phonesTypes[i] = mapStringToPhoneType(phoneLabel);
827
+ phonesLabels[i] = phoneLabel;
828
+ phoneIds[i] = phoneId;
829
+ }
830
+ }
831
+
832
+ ReadableArray urlAddresses = contact.hasKey("urlAddresses") ? contact.getArray("urlAddresses") : null;
833
+ int numOfUrls = 0;
834
+ String[] urls = null;
835
+ String[] urlIds = null;
836
+
837
+ if (urlAddresses != null) {
838
+ numOfUrls = urlAddresses.size();
839
+ urls = new String[numOfUrls];
840
+ urlIds = new String[numOfUrls];
841
+ for (int i = 0; i < numOfUrls; i++) {
842
+ ReadableMap urlMap = urlAddresses.getMap(i);
843
+ urls[i] = urlMap.getString("url");
844
+ urlIds[i] = urlMap.hasKey("id") ? urlMap.getString("id") : null;
845
+ }
846
+ }
847
+
848
+ ReadableArray emailAddresses = contact.hasKey("emailAddresses") ? contact.getArray("emailAddresses") : null;
849
+ int numOfEmails = 0;
850
+ String[] emails = null;
851
+ Integer[] emailsTypes = null;
852
+ String[] emailsLabels = null;
853
+ String[] emailIds = null;
854
+
855
+ if (emailAddresses != null) {
856
+ numOfEmails = emailAddresses.size();
857
+ emails = new String[numOfEmails];
858
+ emailIds = new String[numOfEmails];
859
+ emailsTypes = new Integer[numOfEmails];
860
+ emailsLabels = new String[numOfEmails];
861
+ for (int i = 0; i < numOfEmails; i++) {
862
+ ReadableMap emailMap = emailAddresses.getMap(i);
863
+ emails[i] = emailMap.getString("email");
864
+ String label = emailMap.getString("label");
865
+ emailsTypes[i] = mapStringToEmailType(label);
866
+ emailsLabels[i] = label;
867
+ emailIds[i] = emailMap.hasKey("id") ? emailMap.getString("id") : null;
868
+ }
869
+ }
870
+
871
+ ReadableArray postalAddresses = contact.hasKey("postalAddresses") ? contact.getArray("postalAddresses") : null;
872
+ int numOfPostalAddresses = 0;
873
+ String[] postalAddressesStreet = null;
874
+ String[] postalAddressesCity = null;
875
+ String[] postalAddressesState = null;
876
+ String[] postalAddressesRegion = null;
877
+ String[] postalAddressesPostCode = null;
878
+ String[] postalAddressesCountry = null;
879
+ Integer[] postalAddressesType = null;
880
+ String[] postalAddressesLabel = null;
881
+ if (postalAddresses != null) {
882
+ numOfPostalAddresses = postalAddresses.size();
883
+ postalAddressesStreet = new String[numOfPostalAddresses];
884
+ postalAddressesCity = new String[numOfPostalAddresses];
885
+ postalAddressesState = new String[numOfPostalAddresses];
886
+ postalAddressesRegion = new String[numOfPostalAddresses];
887
+ postalAddressesPostCode = new String[numOfPostalAddresses];
888
+ postalAddressesCountry = new String[numOfPostalAddresses];
889
+ postalAddressesType = new Integer[numOfPostalAddresses];
890
+ postalAddressesLabel = new String[numOfPostalAddresses];
891
+ for (int i = 0; i < numOfPostalAddresses; i++) {
892
+ String postalLabel = getValueFromKey(postalAddresses.getMap(i), "label");
893
+ postalAddressesStreet[i] = getValueFromKey(postalAddresses.getMap(i), "street");
894
+ postalAddressesCity[i] = getValueFromKey(postalAddresses.getMap(i), "city");
895
+ postalAddressesState[i] = getValueFromKey(postalAddresses.getMap(i), "state");
896
+ postalAddressesRegion[i] = getValueFromKey(postalAddresses.getMap(i), "region");
897
+ postalAddressesPostCode[i] = getValueFromKey(postalAddresses.getMap(i), "postCode");
898
+ postalAddressesCountry[i] = getValueFromKey(postalAddresses.getMap(i), "country");
899
+ postalAddressesType[i] = mapStringToPostalAddressType(postalLabel);
900
+ postalAddressesLabel[i] = postalLabel;
901
+ }
902
+ }
903
+
904
+ ReadableArray imAddresses = contact.hasKey("imAddresses") ? contact.getArray("imAddresses") : null;
905
+ int numOfIMAddresses = 0;
906
+ String[] imAccounts = null;
907
+ String[] imProtocols = null;
908
+ String[] imAddressIds = null;
909
+
910
+ if (imAddresses != null) {
911
+ numOfIMAddresses = imAddresses.size();
912
+ imAccounts = new String[numOfIMAddresses];
913
+ imProtocols = new String[numOfIMAddresses];
914
+ imAddressIds = new String[numOfIMAddresses];
915
+ for (int i = 0; i < numOfIMAddresses; i++) {
916
+ ReadableMap imAddressMap = imAddresses.getMap(i);
917
+ imAccounts[i] = imAddressMap.getString("username");
918
+ imProtocols[i] = imAddressMap.getString("service");
919
+ imAddressIds[i] = imAddressMap.hasKey("id") ? imAddressMap.getString("id") : null;
920
+ }
921
+ }
922
+
923
+ ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
924
+
925
+ ContentProviderOperation.Builder op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
926
+ .withSelection(ContactsContract.Data.CONTACT_ID + "=?", new String[] { String.valueOf(recordID) })
927
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
928
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName)
929
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
930
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName)
931
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, prefix)
932
+ .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, suffix);
933
+ ops.add(op.build());
934
+
935
+ op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
936
+ .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + " = ?",
937
+ new String[] { String.valueOf(recordID), ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE })
938
+ .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company)
939
+ .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle)
940
+ .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, department);
941
+ ops.add(op.build());
942
+
943
+ op.withYieldAllowed(true);
944
+
945
+ if (phoneNumbers != null) {
946
+ // remove existing phoneNumbers first
947
+ op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
948
+ .withSelection(
949
+ ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ?",
950
+ new String[] { String.valueOf(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE),
951
+ String.valueOf(rawContactId) });
952
+ ops.add(op.build());
953
+
954
+ // add passed phonenumbers
955
+ for (int i = 0; i < numOfPhones; i++) {
956
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
957
+ .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId))
958
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
959
+ .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phones[i])
960
+ .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, phonesTypes[i])
961
+ .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, phonesLabels[i]);
962
+ ops.add(op.build());
963
+ }
964
+ }
965
+
966
+ for (int i = 0; i < numOfUrls; i++) {
967
+ if (urlIds[i] == null) {
968
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
969
+ .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId))
970
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
971
+ .withValue(ContactsContract.CommonDataKinds.Website.URL, urls[i]);
972
+ } else {
973
+ op = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
974
+ .withSelection(ContactsContract.Data._ID + "=?", new String[] { String.valueOf(urlIds[i]) })
975
+ .withValue(ContactsContract.CommonDataKinds.Website.URL, urls[i]);
976
+ }
977
+ ops.add(op.build());
978
+ }
979
+
980
+ if (emailAddresses != null) {
981
+ // remove existing emails first
982
+ op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
983
+ .withSelection(
984
+ ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ?",
985
+ new String[] { String.valueOf(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE),
986
+ String.valueOf(rawContactId) });
987
+ ops.add(op.build());
988
+
989
+ // add passed email addresses
990
+ for (int i = 0; i < numOfEmails; i++) {
991
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
992
+ .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId))
993
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
994
+ .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, emails[i])
995
+ .withValue(ContactsContract.CommonDataKinds.Email.TYPE, emailsTypes[i])
996
+ .withValue(ContactsContract.CommonDataKinds.Email.LABEL, emailsLabels[i]);
997
+ ops.add(op.build());
998
+ }
999
+ }
1000
+
1001
+ // remove existing note first
1002
+ op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
1003
+ .withSelection(
1004
+ ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ?",
1005
+ new String[] { String.valueOf(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE), String.valueOf(rawContactId) });
1006
+ ops.add(op.build());
1007
+
1008
+ if (note != null) {
1009
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
1010
+ .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId))
1011
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
1012
+ .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note);
1013
+ ops.add(op.build());
1014
+ }
1015
+
1016
+ if (thumbnailPath != null && !thumbnailPath.isEmpty()) {
1017
+ Bitmap photo = getThumbnailBitmap(thumbnailPath);
1018
+
1019
+ if (photo != null) {
1020
+ ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
1021
+ .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
1022
+ .withValue(ContactsContract.Data.MIMETYPE,
1023
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
1024
+ .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, toByteArray(photo))
1025
+ .build());
1026
+ }
1027
+ }
1028
+
1029
+ if (postalAddresses != null) {
1030
+ // remove existing addresses
1031
+ op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
1032
+ .withSelection(
1033
+ ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ?",
1034
+ new String[] { String.valueOf(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE),
1035
+ String.valueOf(rawContactId) });
1036
+ ops.add(op.build());
1037
+
1038
+ for (int i = 0; i < numOfPostalAddresses; i++) {
1039
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
1040
+ .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId))
1041
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
1042
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, postalAddressesType[i])
1043
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.LABEL, postalAddressesLabel[i])
1044
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, postalAddressesStreet[i])
1045
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, postalAddressesCity[i])
1046
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, postalAddressesState[i])
1047
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, postalAddressesPostCode[i])
1048
+ .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, postalAddressesCountry[i]);
1049
+ ops.add(op.build());
1050
+ }
1051
+ }
1052
+
1053
+ if (imAddresses != null) {
1054
+ // remove existing IM addresses
1055
+ op = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
1056
+ .withSelection(
1057
+ ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ?",
1058
+ new String[] { String.valueOf(ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE),
1059
+ String.valueOf(rawContactId) });
1060
+ ops.add(op.build());
1061
+
1062
+ // add passed IM addresses
1063
+ for (int i = 0; i < numOfIMAddresses; i++) {
1064
+ op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
1065
+ .withValue(ContactsContract.Data.RAW_CONTACT_ID, String.valueOf(rawContactId))
1066
+ .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE)
1067
+ .withValue(ContactsContract.CommonDataKinds.Im.DATA, imAccounts[i])
1068
+ .withValue(ContactsContract.CommonDataKinds.Im.TYPE, ContactsContract.CommonDataKinds.Im.TYPE_HOME)
1069
+ .withValue(ContactsContract.CommonDataKinds.Im.PROTOCOL, ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM)
1070
+ .withValue(ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL, imProtocols[i]);
1071
+ ops.add(op.build());
1072
+ }
1073
+ }
1074
+
1075
+ Context ctx = getReactApplicationContext();
1076
+ try {
1077
+ ContentResolver cr = ctx.getContentResolver();
1078
+ ContentProviderResult[] result = cr.applyBatch(ContactsContract.AUTHORITY, ops);
1079
+
1080
+ if (result != null && result.length > 0) {
1081
+
1082
+ ContactsProvider contactsProvider = new ContactsProvider(cr);
1083
+ WritableMap updatedContact = contactsProvider.getContactById(recordID);
1084
+
1085
+ promise.resolve(updatedContact); // success
1086
+ }
1087
+ } catch (Exception e) {
1088
+ promise.reject(e.toString());
1089
+ }
1090
+ }
1091
+
1092
+ /*
1093
+ * Update contact to phone's addressbook
1094
+ */
1095
+ public void deleteContact(ReadableMap contact, Promise promise) {
1096
+
1097
+ String recordID = contact.hasKey("recordID") ? contact.getString("recordID") : null;
1098
+
1099
+ try {
1100
+ Context ctx = getReactApplicationContext();
1101
+
1102
+ Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, recordID);
1103
+ ContentResolver cr = ctx.getContentResolver();
1104
+ int deleted = cr.delete(uri, null, null);
1105
+
1106
+ if (deleted > 0)
1107
+ promise.resolve(recordID); // success
1108
+ else
1109
+ promise.resolve(null); // something was wrong
1110
+
1111
+ } catch (Exception e) {
1112
+ promise.reject(e.toString());
1113
+ }
1114
+ }
1115
+
1116
+ /*
1117
+ * Check permission
1118
+ */
1119
+ public void checkPermission(Promise promise) {
1120
+ promise.resolve(isPermissionGranted());
1121
+ }
1122
+
1123
+ /*
1124
+ * Request permission
1125
+ */
1126
+ public void requestPermission(Promise promise) {
1127
+ requestReadContactsPermission(promise);
1128
+ }
1129
+
1130
+ /*
1131
+ * Enable note usage
1132
+ */
1133
+ public void iosEnableNotesUsage(boolean enabled) {
1134
+ // this method is only needed for iOS
1135
+ }
1136
+
1137
+ private void requestReadContactsPermission(Promise promise) {
1138
+ Activity currentActivity = getCurrentActivity();
1139
+ if (currentActivity == null) {
1140
+ promise.reject(PERMISSION_DENIED);
1141
+ return;
1142
+ }
1143
+
1144
+ if (isPermissionGranted().equals(PERMISSION_AUTHORIZED)) {
1145
+ promise.resolve(PERMISSION_AUTHORIZED);
1146
+ return;
1147
+ }
1148
+
1149
+ requestPromise = promise;
1150
+ ActivityCompat.requestPermissions(currentActivity, new String[] { PERMISSION_READ_CONTACTS },
1151
+ PERMISSION_REQUEST_CODE);
1152
+ }
1153
+
1154
+ protected static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
1155
+ @NonNull int[] grantResults) {
1156
+ if (requestPromise == null) {
1157
+ return;
1158
+ }
1159
+
1160
+ if (requestCode != PERMISSION_REQUEST_CODE) {
1161
+ requestPromise.resolve(PERMISSION_DENIED);
1162
+ return;
1163
+ }
1164
+
1165
+ Hashtable<String, Boolean> results = new Hashtable<>();
1166
+ for (int i = 0; i < permissions.length; i++) {
1167
+ results.put(permissions[i], grantResults[i] == PackageManager.PERMISSION_GRANTED);
1168
+ }
1169
+
1170
+ if (results.containsKey(PERMISSION_READ_CONTACTS) && results.get(PERMISSION_READ_CONTACTS)) {
1171
+ requestPromise.resolve(PERMISSION_AUTHORIZED);
1172
+ } else {
1173
+ requestPromise.resolve(PERMISSION_DENIED);
1174
+ }
1175
+
1176
+ requestPromise = null;
1177
+ }
1178
+
1179
+ /*
1180
+ * Get string value from key
1181
+ */
1182
+ private String getValueFromKey(ReadableMap item, String key) {
1183
+ return item.hasKey(key) ? item.getString(key) : "";
1184
+ }
1185
+
1186
+ /*
1187
+ * Check if READ_CONTACTS permission is granted
1188
+ */
1189
+ private String isPermissionGranted() {
1190
+ // return -1 for denied and 1
1191
+ int res = ActivityCompat.checkSelfPermission(getReactApplicationContext(), PERMISSION_READ_CONTACTS);
1192
+ return (res == PackageManager.PERMISSION_GRANTED) ? PERMISSION_AUTHORIZED : PERMISSION_DENIED;
1193
+ }
1194
+
1195
+ /*
1196
+ * TODO support all phone types
1197
+ * http://developer.android.com/reference/android/provider/ContactsContract.
1198
+ * CommonDataKinds.Phone.html
1199
+ */
1200
+ private int mapStringToPhoneType(String label) {
1201
+ int phoneType;
1202
+ switch (label) {
1203
+ case "home":
1204
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_HOME;
1205
+ break;
1206
+ case "work":
1207
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_WORK;
1208
+ break;
1209
+ case "mobile":
1210
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
1211
+ break;
1212
+ case "main":
1213
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_MAIN;
1214
+ break;
1215
+ case "work fax":
1216
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK;
1217
+ break;
1218
+ case "home fax":
1219
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME;
1220
+ break;
1221
+ case "pager":
1222
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_PAGER;
1223
+ break;
1224
+ case "work_pager":
1225
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER;
1226
+ break;
1227
+ case "work_mobile":
1228
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE;
1229
+ break;
1230
+ case "other":
1231
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_OTHER;
1232
+ break;
1233
+ case "cell":
1234
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
1235
+ break;
1236
+ default:
1237
+ phoneType = ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM;
1238
+ break;
1239
+ }
1240
+ return phoneType;
1241
+ }
1242
+
1243
+ /*
1244
+ * TODO support TYPE_CUSTOM
1245
+ * http://developer.android.com/reference/android/provider/ContactsContract.
1246
+ * CommonDataKinds.Email.html
1247
+ */
1248
+ private int mapStringToEmailType(String label) {
1249
+ int emailType;
1250
+ switch (label) {
1251
+ case "home":
1252
+ emailType = ContactsContract.CommonDataKinds.Email.TYPE_HOME;
1253
+ break;
1254
+ case "work":
1255
+ emailType = ContactsContract.CommonDataKinds.Email.TYPE_WORK;
1256
+ break;
1257
+ case "mobile":
1258
+ emailType = ContactsContract.CommonDataKinds.Email.TYPE_MOBILE;
1259
+ break;
1260
+ case "other":
1261
+ emailType = ContactsContract.CommonDataKinds.Email.TYPE_OTHER;
1262
+ break;
1263
+ case "personal":
1264
+ emailType = ContactsContract.CommonDataKinds.Email.TYPE_HOME;
1265
+ break;
1266
+ default:
1267
+ emailType = ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM;
1268
+ break;
1269
+ }
1270
+ return emailType;
1271
+ }
1272
+
1273
+ private int mapStringToPostalAddressType(String label) {
1274
+ int postalAddressType;
1275
+ switch (label) {
1276
+ case "home":
1277
+ postalAddressType = ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME;
1278
+ break;
1279
+ case "work":
1280
+ postalAddressType = ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK;
1281
+ break;
1282
+ default:
1283
+ postalAddressType = ContactsContract.CommonDataKinds.StructuredPostal.TYPE_CUSTOM;
1284
+ break;
1285
+ }
1286
+ return postalAddressType;
1287
+ }
1288
+
1289
+
1290
+ public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
1291
+ if (requestCode != REQUEST_OPEN_CONTACT_FORM && requestCode != REQUEST_OPEN_EXISTING_CONTACT) {
1292
+ return;
1293
+ }
1294
+
1295
+ if (updateContactPromise == null) {
1296
+ return;
1297
+ }
1298
+
1299
+ if (resultCode != Activity.RESULT_OK) {
1300
+ updateContactPromise.resolve(null); // user probably pressed cancel
1301
+ updateContactPromise = null;
1302
+ return;
1303
+ }
1304
+
1305
+ if (data == null) {
1306
+ updateContactPromise.reject("Error received activity result with no data!");
1307
+ updateContactPromise = null;
1308
+ return;
1309
+ }
1310
+
1311
+ try {
1312
+ Uri contactUri = data.getData();
1313
+
1314
+ if (contactUri == null) {
1315
+ updateContactPromise.reject("Error wrong data. No content uri found!"); // something was wrong
1316
+ updateContactPromise = null;
1317
+ return;
1318
+ }
1319
+
1320
+ Context ctx = getReactApplicationContext();
1321
+ ContentResolver cr = ctx.getContentResolver();
1322
+ ContactsProvider contactsProvider = new ContactsProvider(cr);
1323
+ WritableMap newlyModifiedContact = contactsProvider.getContactById(contactUri.getLastPathSegment());
1324
+
1325
+ updateContactPromise.resolve(newlyModifiedContact); // success
1326
+ } catch (Exception e) {
1327
+ updateContactPromise.reject(e.getMessage());
1328
+ }
1329
+ updateContactPromise = null;
1330
+ }
1331
+
1332
+
1333
+ public void onNewIntent(Intent intent) {
1334
+ }
1335
+
1336
+ }