From d17ec1ff4f338c07912aa1fe1a2bc570a32f8d8e Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 15 Jan 2018 23:19:16 +0100 Subject: [PATCH] handle importing contacts via opening vcf file intent --- app/src/main/AndroidManifest.xml | 10 +++++ .../contacts/activities/MainActivity.kt | 40 ++++++++++++++++--- .../contacts/extensions/Activity.kt | 14 +++++++ app/src/main/res/xml/provider_paths.xml | 1 + 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f3a6d48e..6175cbd1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -41,6 +41,16 @@ + + + + + + + + + + showImportContactsDialog(uri.path) + uri.scheme == "content" -> { + val tempFile = getTempFile() + if (tempFile == null) { + toast(R.string.unknown_error_occurred) + return + } + + val inputStream = contentResolver.openInputStream(uri) + val out = FileOutputStream(tempFile) + inputStream.copyTo(out) + showImportContactsDialog(tempFile.absolutePath) + } + else -> toast(R.string.invalid_file_format) + } + } + private fun tryExportContacts() { handlePermission(PERMISSION_WRITE_STORAGE) { if (it) { 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 3cb06261..7f631c8e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/extensions/Activity.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.contacts.extensions import android.content.Intent 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.toast import com.simplemobiletools.commons.helpers.PERMISSION_CALL_PHONE @@ -10,6 +11,7 @@ import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.models.Contact +import java.io.File fun SimpleActivity.startCallIntent(recipient: String) { handlePermission(PERMISSION_CALL_PHONE) { @@ -61,3 +63,15 @@ fun SimpleActivity.showContactSourcePicker(currentSource: String, callback: (new } } } + +fun BaseSimpleActivity.getTempFile(): File? { + val folder = File(cacheDir, "contacts") + if (!folder.exists()) { + if (!folder.mkdir()) { + toast(R.string.unknown_error_occurred) + return null + } + } + + return File(folder, "contacts.vcf") +} diff --git a/app/src/main/res/xml/provider_paths.xml b/app/src/main/res/xml/provider_paths.xml index 90100f76..c91794e3 100644 --- a/app/src/main/res/xml/provider_paths.xml +++ b/app/src/main/res/xml/provider_paths.xml @@ -3,4 +3,5 @@ +