adding the Import Contacts dialog

This commit is contained in:
tibbi
2018-01-14 20:53:29 +01:00
parent 82d10f8b18
commit 9950d5d3ce
7 changed files with 178 additions and 23 deletions

View File

@ -22,3 +22,14 @@ val PHOTO_UNCHANGED = 4
val DEFAULT_EMAIL_TYPE = ContactsContract.CommonDataKinds.Email.TYPE_HOME
val DEFAULT_PHONE_NUMBER_TYPE = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
val DEFAULT_EVENT_TYPE = ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY
// export/import
val BEGIN_VCARD = "BEGIN:VCARD"
val END_VCARD = "END:VCARD"
val N = "N"
val FN = "FN"
val TEL = "TEL"
val VERSION = "VERSION"
val BDAY = "BDAY"
val PHOTO = "PHOTO"
val EMAIL = "EMAIL"

View File

@ -0,0 +1,53 @@
package com.simplemobiletools.contacts.helpers
import android.widget.Toast
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.helpers.VcfImporter.ImportResult.*
import java.io.File
class VcfImporter(val activity: SimpleActivity) {
enum class ImportResult {
IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL
}
private var contactsImported = 0
private var contactsFailed = 0
fun importContacts(path: String, targetContactSource: String): ImportResult {
try {
val inputStream = if (path.contains("/")) {
File(path).inputStream()
} else {
activity.assets.open(path)
}
inputStream.bufferedReader().use {
while (true) {
val line = it.readLine() ?: break
if (line.trim().isEmpty())
continue
if (line == BEGIN_VCARD) {
resetValues()
} else if (line == END_VCARD) {
contactsImported++
}
}
}
} catch (e: Exception) {
activity.showErrorToast(e, Toast.LENGTH_LONG)
contactsFailed++
}
return when {
contactsImported == 0 -> IMPORT_FAIL
contactsFailed > 0 -> IMPORT_PARTIAL
else -> IMPORT_OK
}
}
private fun resetValues() {
}
}