mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-02-18 12:30:38 +01:00
report all exceptions thrown during exporting contacts
This commit is contained in:
parent
b617676c34
commit
197eb7b60c
@ -69,6 +69,7 @@ fun Context.getContactUriRawId(uri: Uri): Int {
|
|||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
return cursor.getIntValue(ContactsContract.Contacts.NAME_RAW_CONTACT_ID)
|
return cursor.getIntValue(ContactsContract.Contacts.NAME_RAW_CONTACT_ID)
|
||||||
}
|
}
|
||||||
|
} catch (ignored: Exception) {
|
||||||
} finally {
|
} finally {
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import android.provider.MediaStore
|
|||||||
import android.util.Base64
|
import android.util.Base64
|
||||||
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
||||||
import com.simplemobiletools.commons.extensions.getFileOutputStream
|
import com.simplemobiletools.commons.extensions.getFileOutputStream
|
||||||
|
import com.simplemobiletools.commons.extensions.showErrorToast
|
||||||
import com.simplemobiletools.commons.extensions.writeLn
|
import com.simplemobiletools.commons.extensions.writeLn
|
||||||
import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.*
|
import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.*
|
||||||
import com.simplemobiletools.contacts.models.Contact
|
import com.simplemobiletools.contacts.models.Contact
|
||||||
@ -24,59 +25,63 @@ class VcfExporter {
|
|||||||
private var contactsFailed = 0
|
private var contactsFailed = 0
|
||||||
|
|
||||||
fun exportContacts(activity: BaseSimpleActivity, file: File, contacts: ArrayList<Contact>, callback: (result: ExportResult) -> Unit) {
|
fun exportContacts(activity: BaseSimpleActivity, file: File, contacts: ArrayList<Contact>, callback: (result: ExportResult) -> Unit) {
|
||||||
activity.getFileOutputStream(file) {
|
try {
|
||||||
if (it == null) {
|
activity.getFileOutputStream(file) {
|
||||||
callback(EXPORT_FAIL)
|
if (it == null) {
|
||||||
return@getFileOutputStream
|
callback(EXPORT_FAIL)
|
||||||
}
|
return@getFileOutputStream
|
||||||
|
}
|
||||||
|
|
||||||
it.bufferedWriter().use { out ->
|
it.bufferedWriter().use { out ->
|
||||||
for (contact in contacts) {
|
for (contact in contacts) {
|
||||||
out.writeLn(BEGIN_VCARD)
|
out.writeLn(BEGIN_VCARD)
|
||||||
out.writeLn(VERSION_2_1)
|
out.writeLn(VERSION_2_1)
|
||||||
out.writeLn("$N${contact.surname};${contact.firstName};${contact.middleName};;")
|
out.writeLn("$N${contact.surname};${contact.firstName};${contact.middleName};;")
|
||||||
|
|
||||||
contact.phoneNumbers.forEach {
|
contact.phoneNumbers.forEach {
|
||||||
out.writeLn("$TEL;${getPhoneNumberLabel(it.type)}:${it.value}")
|
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}")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contact.thumbnailUri.isNotEmpty()) {
|
||||||
|
val firstLine = "$PHOTO;$ENCODING=$BASE64;$JPEG:"
|
||||||
|
val bitmap = MediaStore.Images.Media.getBitmap(activity.contentResolver, Uri.parse(contact.thumbnailUri))
|
||||||
|
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||||
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, byteArrayOutputStream)
|
||||||
|
bitmap.recycle()
|
||||||
|
val byteArray = byteArrayOutputStream.toByteArray()
|
||||||
|
val encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP)
|
||||||
|
|
||||||
|
val encodedFirstLineSection = encoded.substring(0, ENCODED_PHOTO_LINE_LENGTH - firstLine.length)
|
||||||
|
out.writeLn(firstLine + encodedFirstLineSection)
|
||||||
|
var curStartIndex = encodedFirstLineSection.length
|
||||||
|
do {
|
||||||
|
val part = encoded.substring(curStartIndex, Math.min(curStartIndex + ENCODED_PHOTO_LINE_LENGTH - 1, encoded.length))
|
||||||
|
out.writeLn(" $part")
|
||||||
|
curStartIndex += ENCODED_PHOTO_LINE_LENGTH - 1
|
||||||
|
} while (curStartIndex < encoded.length)
|
||||||
|
|
||||||
|
out.writeLn("")
|
||||||
|
}
|
||||||
|
|
||||||
|
out.writeLn(END_VCARD)
|
||||||
|
contactsExported++
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contact.thumbnailUri.isNotEmpty()) {
|
|
||||||
val firstLine = "$PHOTO;$ENCODING=$BASE64;$JPEG:"
|
|
||||||
val bitmap = MediaStore.Images.Media.getBitmap(activity.contentResolver, Uri.parse(contact.thumbnailUri))
|
|
||||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, byteArrayOutputStream)
|
|
||||||
bitmap.recycle()
|
|
||||||
val byteArray = byteArrayOutputStream.toByteArray()
|
|
||||||
val encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP)
|
|
||||||
|
|
||||||
val encodedFirstLineSection = encoded.substring(0, ENCODED_PHOTO_LINE_LENGTH - firstLine.length)
|
|
||||||
out.writeLn(firstLine + encodedFirstLineSection)
|
|
||||||
var curStartIndex = encodedFirstLineSection.length
|
|
||||||
do {
|
|
||||||
val part = encoded.substring(curStartIndex, Math.min(curStartIndex + ENCODED_PHOTO_LINE_LENGTH - 1, encoded.length))
|
|
||||||
out.writeLn(" $part")
|
|
||||||
curStartIndex += ENCODED_PHOTO_LINE_LENGTH - 1
|
|
||||||
} while (curStartIndex < encoded.length)
|
|
||||||
|
|
||||||
out.writeLn("")
|
|
||||||
}
|
|
||||||
|
|
||||||
out.writeLn(END_VCARD)
|
|
||||||
contactsExported++
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
activity.showErrorToast(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(when {
|
callback(when {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user