react-native-contacts 8.0.3 → 8.0.5

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