allow sharing a contact via creating a vcf file

This commit is contained in:
tibbi 2018-01-15 23:42:03 +01:00
parent d17ec1ff4f
commit dc308a94dc
4 changed files with 58 additions and 28 deletions

View File

@ -87,6 +87,35 @@ class ContactActivity : SimpleActivity() {
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_contact, menu)
if (wasActivityInitialized) {
menu.findItem(R.id.delete).isVisible = contact?.id != 0
menu.findItem(R.id.share).isVisible = contact?.id != 0
}
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.save -> saveContact()
R.id.delete -> deleteContact()
R.id.share -> shareContact()
else -> return super.onOptionsItemSelected(item)
}
return true
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (resultCode == RESULT_OK) {
when (requestCode) {
INTENT_TAKE_PHOTO, INTENT_CHOOSE_PHOTO -> startCropPhotoIntent(lastPhotoIntentUri!!)
INTENT_CROP_PHOTO -> updateContactPhoto(lastPhotoIntentUri.toString())
}
}
}
private fun initContact() {
var contactId = intent.getIntExtra(CONTACT_ID, 0)
val action = intent.action
@ -173,33 +202,6 @@ class ContactActivity : SimpleActivity() {
invalidateOptionsMenu()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_contact, menu)
if (wasActivityInitialized) {
menu.findItem(R.id.delete).isVisible = contact?.id != 0
}
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.save -> saveContact()
R.id.delete -> deleteContact()
else -> return super.onOptionsItemSelected(item)
}
return true
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (resultCode == RESULT_OK) {
when (requestCode) {
INTENT_TAKE_PHOTO, INTENT_CHOOSE_PHOTO -> startCropPhotoIntent(lastPhotoIntentUri!!)
INTENT_CROP_PHOTO -> updateContactPhoto(lastPhotoIntentUri.toString())
}
}
}
private fun startCropPhotoIntent(uri: Uri) {
lastPhotoIntentUri = getCachePhotoUri()
Intent("com.android.camera.action.CROP").apply {
@ -309,6 +311,10 @@ class ContactActivity : SimpleActivity() {
}
}
private fun shareContact() {
shareContact(contact!!)
}
private fun showPhotoPlaceholder() {
val placeholder = resources.getColoredBitmap(R.drawable.ic_person, config.primaryColor.getContrastColor())
val padding = resources.getDimension(R.dimen.activity_margin).toInt()

View File

@ -5,11 +5,15 @@ import android.net.Uri
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.getFilePublicUri
import com.simplemobiletools.commons.extensions.shareUri
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.PERMISSION_CALL_PHONE
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.contacts.BuildConfig
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.helpers.VcfExporter
import com.simplemobiletools.contacts.models.Contact
import java.io.File
@ -64,6 +68,21 @@ fun SimpleActivity.showContactSourcePicker(currentSource: String, callback: (new
}
}
fun BaseSimpleActivity.shareContact(contact: Contact) {
val file = getTempFile()
if (file == null) {
toast(R.string.unknown_error_occurred)
return
}
VcfExporter().exportContacts(this, file, arrayListOf(contact)) {
if (it == VcfExporter.ExportResult.EXPORT_OK) {
val uri = getFilePublicUri(file, BuildConfig.APPLICATION_ID)
shareUri(uri, BuildConfig.APPLICATION_ID)
}
}
}
fun BaseSimpleActivity.getTempFile(): File? {
val folder = File(cacheDir, "contacts")
if (!folder.exists()) {

View File

@ -48,7 +48,7 @@ class VcfExporter {
contact.events.forEach {
if (it.type == CommonDataKinds.Event.TYPE_BIRTHDAY) {
out.writeLn("$BDAY:${it.value}")
out.writeLn("$BDAY${it.value}")
}
}

View File

@ -11,4 +11,9 @@
android:icon="@drawable/ic_delete"
android:title="@string/delete"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/share"
android:icon="@drawable/ic_share"
android:title="@string/share"
app:showAsAction="ifRoom"/>
</menu>