pass an OutputStream to exporter instead of a File

This commit is contained in:
tibbi 2020-03-16 23:09:25 +01:00
parent fbfe7133ca
commit eb4be51f92
3 changed files with 133 additions and 135 deletions

View File

@ -526,7 +526,8 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
if (contacts.isEmpty()) {
toast(R.string.no_entries_for_exporting)
} else {
VcfExporter().exportContacts(this, file, contacts, true) { result ->
getFileOutputStream(file.toFileDirItem(this), true) {
VcfExporter().exportContacts(this, it, contacts, true) { result ->
toast(when (result) {
VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed
@ -537,6 +538,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
}
}
}
}
private fun launchAbout() {
val licenses = LICENSE_JODA or LICENSE_GLIDE or LICENSE_GSON or LICENSE_INDICATOR_FAST_SCROLL

View File

@ -4,9 +4,7 @@ import android.content.Intent
import android.net.Uri
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.sharePathIntent
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_CALL_PHONE
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.contacts.pro.BuildConfig
@ -92,7 +90,8 @@ fun BaseSimpleActivity.shareContacts(contacts: ArrayList<Contact>) {
return
}
VcfExporter().exportContacts(this, file, contacts, false) {
getFileOutputStream(file.toFileDirItem(this), true) {
VcfExporter().exportContacts(this, it, contacts, false) {
if (it == VcfExporter.ExportResult.EXPORT_OK) {
sharePathIntent(file.absolutePath, BuildConfig.APPLICATION_ID)
} else {
@ -100,6 +99,7 @@ fun BaseSimpleActivity.shareContacts(contacts: ArrayList<Contact>) {
}
}
}
}
fun SimpleActivity.handleGenericContactClick(contact: Contact) {
when (config.onContactClick) {

View File

@ -4,9 +4,7 @@ import android.net.Uri
import android.provider.ContactsContract.CommonDataKinds
import android.provider.MediaStore
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.getFileOutputStream
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.extensions.toFileDirItem
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.extensions.getByteArray
@ -18,7 +16,7 @@ import ezvcard.VCard
import ezvcard.parameter.ImageType
import ezvcard.property.*
import ezvcard.util.PartialDate
import java.io.File
import java.io.OutputStream
import java.util.*
class VcfExporter {
@ -29,12 +27,11 @@ class VcfExporter {
private var contactsExported = 0
private var contactsFailed = 0
fun exportContacts(activity: BaseSimpleActivity, file: File, contacts: ArrayList<Contact>, showExportingToast: Boolean, callback: (result: ExportResult) -> Unit) {
activity.getFileOutputStream(file.toFileDirItem(activity), true) {
fun exportContacts(activity: BaseSimpleActivity, outputStream: OutputStream?, contacts: ArrayList<Contact>, showExportingToast: Boolean, callback: (result: ExportResult) -> Unit) {
try {
if (it == null) {
if (outputStream == null) {
callback(EXPORT_FAIL)
return@getFileOutputStream
return
}
if (showExportingToast) {
@ -152,7 +149,7 @@ class VcfExporter {
contactsExported++
}
Ezvcard.write(cards).go(it)
Ezvcard.write(cards).go(outputStream)
} catch (e: Exception) {
activity.showErrorToast(e)
}
@ -163,7 +160,6 @@ class VcfExporter {
else -> ExportResult.EXPORT_OK
})
}
}
private fun getPhoneNumberTypeLabel(type: Int, label: String) = when (type) {
CommonDataKinds.Phone.TYPE_MOBILE -> CELL