implement the actual contact importer parser
This commit is contained in:
parent
5717dd48ed
commit
e0257c742d
2 changed files with 86 additions and 11 deletions
|
@ -27,13 +27,13 @@ val DEFAULT_EVENT_TYPE = CommonDataKinds.Event.TYPE_BIRTHDAY
|
|||
val BEGIN_VCARD = "BEGIN:VCARD"
|
||||
val END_VCARD = "END:VCARD"
|
||||
val N = "N:"
|
||||
val FN = "FN:"
|
||||
val TEL = "TEL"
|
||||
val BDAY = "BDAY:"
|
||||
val ANNIVERSARY = "ANNIVERSARY:"
|
||||
val PHOTO = "PHOTO"
|
||||
val EMAIL = "EMAIL;"
|
||||
val EMAIL = "EMAIL"
|
||||
|
||||
// phone number types
|
||||
// phone number/email types
|
||||
val CELL = "CELL"
|
||||
val WORK = "WORK"
|
||||
val HOME = "HOME"
|
||||
|
@ -41,5 +41,4 @@ val PREF = "PREF"
|
|||
val WORK_FAX = "WORK;FAX"
|
||||
val HOME_FAX = "HOME;FAX"
|
||||
val PAGER = "PAGER"
|
||||
val VOICE = "VOICE"
|
||||
val EMPTY = ""
|
||||
val MOBILE = "MOBILE"
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
package com.simplemobiletools.contacts.helpers
|
||||
|
||||
import android.provider.ContactsContract.CommonDataKinds
|
||||
import android.widget.Toast
|
||||
import com.simplemobiletools.commons.extensions.showErrorToast
|
||||
import com.simplemobiletools.contacts.activities.SimpleActivity
|
||||
import com.simplemobiletools.contacts.helpers.VcfImporter.ImportResult.*
|
||||
import com.simplemobiletools.contacts.models.Contact
|
||||
import com.simplemobiletools.contacts.models.Email
|
||||
import com.simplemobiletools.contacts.models.Event
|
||||
import com.simplemobiletools.contacts.models.PhoneNumber
|
||||
import java.io.File
|
||||
|
||||
class VcfImporter(val activity: SimpleActivity) {
|
||||
|
@ -11,6 +16,13 @@ class VcfImporter(val activity: SimpleActivity) {
|
|||
IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL
|
||||
}
|
||||
|
||||
private var curFirstName = ""
|
||||
private var curMiddleName = ""
|
||||
private var curSurname = ""
|
||||
private var curPhoneNumbers = ArrayList<PhoneNumber>()
|
||||
private var curEmails = ArrayList<Email>()
|
||||
private var curEvents = ArrayList<Event>()
|
||||
|
||||
private var contactsImported = 0
|
||||
private var contactsFailed = 0
|
||||
|
||||
|
@ -25,13 +37,18 @@ class VcfImporter(val activity: SimpleActivity) {
|
|||
inputStream.bufferedReader().use {
|
||||
while (true) {
|
||||
val line = it.readLine() ?: break
|
||||
if (line.trim().isEmpty())
|
||||
if (line.trim().isEmpty()) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (line == BEGIN_VCARD) {
|
||||
resetValues()
|
||||
} else if (line == END_VCARD) {
|
||||
contactsImported++
|
||||
when {
|
||||
line == BEGIN_VCARD -> resetValues()
|
||||
line.startsWith(N) -> parseNames(line.substring(N.length))
|
||||
line.startsWith(TEL) -> addPhoneNumber(line.substring(TEL.length))
|
||||
line.startsWith(EMAIL) -> addEmail(line.substring(EMAIL.length))
|
||||
line.startsWith(BDAY) -> addBirthday(line.substring(BDAY.length))
|
||||
line.startsWith(ANNIVERSARY) -> addAnniversary(line.substring(ANNIVERSARY.length))
|
||||
line == END_VCARD -> saveContact(targetContactSource)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +64,66 @@ class VcfImporter(val activity: SimpleActivity) {
|
|||
}
|
||||
}
|
||||
|
||||
private fun resetValues() {
|
||||
private fun parseNames(names: String) {
|
||||
val nameParts = names.split(";")
|
||||
curFirstName = nameParts[1]
|
||||
curMiddleName = nameParts[2]
|
||||
curSurname = nameParts[0]
|
||||
}
|
||||
|
||||
private fun addPhoneNumber(phoneNumber: String) {
|
||||
val phoneParts = phoneNumber.trimStart(';').split(":")
|
||||
val type = getPhoneNumberTypeId(phoneParts[0])
|
||||
val value = phoneParts[1]
|
||||
curPhoneNumbers.add(PhoneNumber(value, type))
|
||||
}
|
||||
|
||||
private fun getPhoneNumberTypeId(type: String) = when (type) {
|
||||
CELL -> CommonDataKinds.Phone.TYPE_MOBILE
|
||||
WORK -> CommonDataKinds.Phone.TYPE_WORK
|
||||
HOME -> CommonDataKinds.Phone.TYPE_HOME
|
||||
PREF -> CommonDataKinds.Phone.TYPE_MAIN
|
||||
WORK_FAX -> CommonDataKinds.Phone.TYPE_FAX_WORK
|
||||
HOME_FAX -> CommonDataKinds.Phone.TYPE_FAX_HOME
|
||||
PAGER -> CommonDataKinds.Phone.TYPE_PAGER
|
||||
else -> CommonDataKinds.Phone.TYPE_OTHER
|
||||
}
|
||||
|
||||
private fun addEmail(email: String) {
|
||||
val emailParts = email.trimStart(';').split(":")
|
||||
val type = getEmailTypeId(emailParts[0])
|
||||
val value = emailParts[1]
|
||||
curEmails.add(Email(value, type))
|
||||
}
|
||||
|
||||
private fun getEmailTypeId(type: String) = when (type) {
|
||||
HOME -> CommonDataKinds.Email.TYPE_HOME
|
||||
WORK -> CommonDataKinds.Email.TYPE_WORK
|
||||
MOBILE -> CommonDataKinds.Email.TYPE_MOBILE
|
||||
else -> CommonDataKinds.Email.TYPE_OTHER
|
||||
}
|
||||
|
||||
private fun addBirthday(birthday: String) {
|
||||
curEvents.add(Event(birthday, CommonDataKinds.Event.TYPE_BIRTHDAY))
|
||||
}
|
||||
|
||||
private fun addAnniversary(anniversary: String) {
|
||||
curEvents.add(Event(anniversary, CommonDataKinds.Event.TYPE_ANNIVERSARY))
|
||||
}
|
||||
|
||||
private fun saveContact(source: String) {
|
||||
val contact = Contact(0, curFirstName, curMiddleName, curSurname, "", curPhoneNumbers, curEmails, curEvents, source, 0, 0)
|
||||
if (ContactsHelper(activity).insertContact(contact)) {
|
||||
contactsImported++
|
||||
}
|
||||
}
|
||||
|
||||
private fun resetValues() {
|
||||
curFirstName = ""
|
||||
curMiddleName = ""
|
||||
curSurname = ""
|
||||
curPhoneNumbers = ArrayList()
|
||||
curEmails = ArrayList()
|
||||
curEvents = ArrayList()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue