create a vcf file contacts exporter

This commit is contained in:
tibbi 2018-01-15 18:12:09 +01:00
parent ca463d056b
commit 052e17bed6
4 changed files with 45 additions and 3 deletions

View file

@ -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"

View file

@ -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"

View file

@ -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 -> ""
}
}

View file

@ -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