use of third_parties.ezvcard_android.ContactOperations in project android by nextcloud.
the class ContactsImportJob method onRunJob.
@NonNull
@Override
protected Result onRunJob(@NonNull Params params) {
PersistableBundleCompat bundle = params.getExtras();
String vCardFilePath = bundle.getString(VCARD_FILE_PATH, "");
String accountName = bundle.getString(ACCOUNT_NAME, "");
String accountType = bundle.getString(ACCOUNT_TYPE, "");
int[] intArray = bundle.getIntArray(CHECKED_ITEMS_ARRAY);
File file = new File(vCardFilePath);
ArrayList<VCard> vCards = new ArrayList<>();
Cursor cursor = null;
try {
ContactOperations operations = new ContactOperations(getContext(), accountName, accountType);
vCards.addAll(Ezvcard.parse(file).all());
Collections.sort(vCards, new ContactListFragment.VCardComparator());
cursor = getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
TreeMap<VCard, Long> ownContactList = new TreeMap<>(new ContactListFragment.VCardComparator());
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
VCard vCard = getContactFromCursor(cursor);
if (vCard != null) {
ownContactList.put(vCard, cursor.getLong(cursor.getColumnIndex("NAME_RAW_CONTACT_ID")));
}
cursor.moveToNext();
}
}
for (int i = 0; i < intArray.length; i++) {
VCard vCard = vCards.get(intArray[i]);
if (ContactListFragment.getDisplayName(vCard).length() != 0) {
if (!ownContactList.containsKey(vCard)) {
operations.insertContact(vCard);
} else {
operations.updateContact(vCard, ownContactList.get(vCard));
}
} else {
// Insert All the contacts without name
operations.insertContact(vCard);
}
}
} catch (Exception e) {
Log_OC.e(TAG, e.getMessage());
} finally {
if (cursor != null) {
cursor.close();
}
}
return Result.SUCCESS;
}
Aggregations