From 052e17bed6059ecba4ef1e6b5395bb1788e6a6c1 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 15 Jan 2018 18:12:09 +0100 Subject: [PATCH] create a vcf file contacts exporter --- app/build.gradle | 2 +- .../contacts/helpers/Constants.kt | 2 + .../contacts/helpers/VcfExporter.kt | 41 +++++++++++++++++++ .../contacts/helpers/VcfImporter.kt | 3 +- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 663494c9..dac99717 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -41,7 +41,7 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:3.7.3' + implementation 'com.simplemobiletools:commons:3.7.4' implementation 'joda-time:joda-time:2.9.9' debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion" diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt index 3c4d4a3e..49d1115b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt @@ -33,6 +33,7 @@ val ANNIVERSARY = "ANNIVERSARY:" val PHOTO = "PHOTO" val EMAIL = "EMAIL" val BASE64 = "BASE64" +val VERSION_2_1 = "VERSION:2.1" // phone number/email types val CELL = "CELL" @@ -45,3 +46,4 @@ val WORK_FAX = "WORK;FAX" val HOME_FAX = "HOME;FAX" val PAGER = "PAGER" val MOBILE = "MOBILE" +val VOICE = "VOICE" 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 f014f987..0a92aede 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt @@ -1,7 +1,9 @@ package com.simplemobiletools.contacts.helpers +import android.provider.ContactsContract.CommonDataKinds import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.getFileOutputStream +import com.simplemobiletools.commons.extensions.writeLn import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.* import com.simplemobiletools.contacts.models.Contact import java.io.File @@ -23,7 +25,28 @@ class VcfExporter() { it.bufferedWriter().use { out -> for (contact in contacts) { + out.writeLn(BEGIN_VCARD) + out.writeLn(VERSION_2_1) + out.writeLn("$N${contact.surname};${contact.firstName};${contact.middleName};;") + contact.phoneNumbers.forEach { + out.writeLn("$TEL;${getPhoneNumberLabel(it.type)}:${it.value}") + } + + contact.emails.forEach { + val type = getEmailTypeLabel(it.type) + val delimiterType = if (type.isEmpty()) "" else ";$type" + out.writeLn("$EMAIL$delimiterType:${it.value}") + } + + contact.events.forEach { + if (it.type == CommonDataKinds.Event.TYPE_BIRTHDAY) { + out.writeLn("$BDAY:${it.value}") + } + } + + out.writeLn(END_VCARD) + contactsExported++ } } } @@ -34,4 +57,22 @@ class VcfExporter() { else -> EXPORT_OK }) } + + private fun getPhoneNumberLabel(type: Int) = when (type) { + CommonDataKinds.Phone.TYPE_MOBILE -> CELL + CommonDataKinds.Phone.TYPE_HOME -> HOME + CommonDataKinds.Phone.TYPE_WORK -> WORK + CommonDataKinds.Phone.TYPE_MAIN -> PREF + CommonDataKinds.Phone.TYPE_FAX_WORK -> WORK_FAX + CommonDataKinds.Phone.TYPE_FAX_HOME -> HOME_FAX + CommonDataKinds.Phone.TYPE_PAGER -> PAGER + else -> VOICE + } + + private fun getEmailTypeLabel(type: Int) = when (type) { + CommonDataKinds.Email.TYPE_HOME -> HOME + CommonDataKinds.Email.TYPE_WORK -> WORK + CommonDataKinds.Email.TYPE_MOBILE -> MOBILE + else -> "" + } } 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 2e5104d5..0b33a7ff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt @@ -17,7 +17,6 @@ import com.simplemobiletools.contacts.models.PhoneNumber import java.io.File import java.io.FileOutputStream - class VcfImporter(val activity: SimpleActivity) { enum class ImportResult { IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL @@ -108,8 +107,8 @@ class VcfImporter(val activity: SimpleActivity) { private fun getPhoneNumberTypeId(type: String, subType: String) = when (type) { CELL -> CommonDataKinds.Phone.TYPE_MOBILE - WORK -> CommonDataKinds.Phone.TYPE_WORK HOME -> CommonDataKinds.Phone.TYPE_HOME + WORK -> CommonDataKinds.Phone.TYPE_WORK PREF, MAIN -> CommonDataKinds.Phone.TYPE_MAIN WORK_FAX -> CommonDataKinds.Phone.TYPE_FAX_WORK HOME_FAX -> CommonDataKinds.Phone.TYPE_FAX_HOME