From dc308a94dc3c857c9a1ded442ed50fa33fee517a Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 15 Jan 2018 23:42:03 +0100 Subject: [PATCH] allow sharing a contact via creating a vcf file --- .../contacts/activities/ContactActivity.kt | 60 ++++++++++--------- .../contacts/extensions/Activity.kt | 19 ++++++ .../contacts/helpers/VcfExporter.kt | 2 +- app/src/main/res/menu/menu_contact.xml | 5 ++ 4 files changed, 58 insertions(+), 28 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt index c5ffd9d4..f7592b94 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt @@ -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() diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt index 7f631c8e..ff191b4f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt @@ -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()) { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt index b8c53758..b45bd0af 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt @@ -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}") } } diff --git a/app/src/main/res/menu/menu_contact.xml b/app/src/main/res/menu/menu_contact.xml index 262a8535..f7605732 100644 --- a/app/src/main/res/menu/menu_contact.xml +++ b/app/src/main/res/menu/menu_contact.xml @@ -11,4 +11,9 @@ android:icon="@drawable/ic_delete" android:title="@string/delete" app:showAsAction="ifRoom"/> +