From b3d99e99e0bc0724a723c8ee283e15987e46c445 Mon Sep 17 00:00:00 2001 From: Dirkster99 Date: Mon, 3 Sep 2018 16:32:53 +0200 Subject: [PATCH] Adding group import and export capability https://github.com/SimpleMobileTools/Simple-Contacts/issues/146 --- .../contacts/helpers/VcfExporter.kt | 12 +++++++ .../contacts/helpers/VcfImporter.kt | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt index b4651fb5..399ab49b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt @@ -123,6 +123,18 @@ class VcfExporter { card.addPhoto(photo) } + // Export a list of groups where this contact is a member of + if (contact.groups.size > 0){ + + // Copy groups into a categorized list and add this category into VCard + val groupList = Categories(); + contact.groups.forEach{ + groupList.addParameter("GROUP", it.title); + } + + card.addCategories(groupList); + } + cards.add(card) contactsExported++ } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt index ae9dc510..ca81848c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt @@ -5,7 +5,9 @@ import android.graphics.BitmapFactory import android.provider.ContactsContract.CommonDataKinds import android.widget.Toast import com.simplemobiletools.commons.extensions.showErrorToast +import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.activities.SimpleActivity +import com.simplemobiletools.contacts.extensions.dbHelper import com.simplemobiletools.contacts.extensions.getCachePhoto import com.simplemobiletools.contacts.extensions.getCachePhotoUri import com.simplemobiletools.contacts.helpers.VcfImporter.ImportResult.* @@ -102,6 +104,36 @@ class VcfImporter(val activity: SimpleActivity) { val contactId = 0 val notes = ezContact.notes.firstOrNull()?.value ?: "" val groups = ArrayList() + + if (ezContact.categories != null){ // Iterate through categories of this contact (if any) + + // Iterate through group names for this contact + // Check if group already exist and create it if not + // Associate group with this user + val groupNames = ezContact.categories.getParameters("GROUP"); + + if (groupNames != null){ // Resolve group references for this contact + + val storedGroups = ContactsHelper(activity).getStoredGroups(); + groupNames.forEach { + + val groupName = it; + val storedGroup = storedGroups.firstOrNull { it.title == groupName } + + if (storedGroup != null){ + groups.add(storedGroup); // Group is already present on this device + } + else { + // Group is not present on this device, yet so we create a new one + val newcontactGroup = activity.dbHelper.insertGroup(Group(0, groupName)); + + if (newcontactGroup != null) // and associate it with this contact + groups.add(newcontactGroup); + } + } + } + } + val company = ezContact.organization?.values?.firstOrNull() ?: "" val jobPosition = ezContact.titles?.firstOrNull()?.value ?: "" val organization = Organization(company, jobPosition)