diff --git a/CHANGELOG.md b/CHANGELOG.md
index 47a86bb5a..b85c73293 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
+### Added
+- Support for importing contacts from vCards shared by other apps ([#321])
+
### Fixed
- Fixed search not matching full phone numbers
@@ -87,6 +90,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#201]: https://github.com/FossifyOrg/Contacts/issues/201
[#281]: https://github.com/FossifyOrg/Contacts/issues/281
[#289]: https://github.com/FossifyOrg/Contacts/issues/289
+[#321]: https://github.com/FossifyOrg/Contacts/issues/321
[#339]: https://github.com/FossifyOrg/Contacts/issues/339
[#360]: https://github.com/FossifyOrg/Contacts/issues/360
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 70211083b..a13d169a1 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -62,6 +62,16 @@
+
+
+
+
+
+
+
+
+
+
intent.data
+ Intent.ACTION_SEND -> intent.getParcelableExtra(Intent.EXTRA_STREAM)
+ else -> null
+ }
+
+ if (uri != null) {
+ tryImportContactsFromFile(uri) { success ->
+ if (success) {
runOnUiThread {
refreshContacts(ALL_TABS_MASK)
}
}
}
- intent.data = null
- }
-
- binding.mainDialpadButton.setOnClickListener {
- launchDialpad()
+ intent.action = null
}
}
diff --git a/app/src/main/kotlin/org/fossify/contacts/extensions/Activity.kt b/app/src/main/kotlin/org/fossify/contacts/extensions/Activity.kt
index 7c0af7236..977adc8bf 100644
--- a/app/src/main/kotlin/org/fossify/contacts/extensions/Activity.kt
+++ b/app/src/main/kotlin/org/fossify/contacts/extensions/Activity.kt
@@ -35,7 +35,6 @@ import org.fossify.contacts.activities.ViewContactActivity
import org.fossify.contacts.dialogs.ImportContactsDialog
import org.fossify.contacts.helpers.DEFAULT_FILE_NAME
import org.fossify.contacts.helpers.VcfExporter
-import java.io.FileOutputStream
fun SimpleActivity.startCallIntent(recipient: String) {
handlePermission(PERMISSION_CALL_PHONE) {
@@ -190,16 +189,13 @@ fun SimpleActivity.tryImportContactsFromFile(uri: Uri, callback: (Boolean) -> Un
when (uri.scheme) {
"file" -> showImportContactsDialog(uri.path!!, callback)
"content" -> {
- val tempFile = getTempFile()
- if (tempFile == null) {
- toast(org.fossify.commons.R.string.unknown_error_occurred)
- return
- }
-
try {
- val inputStream = contentResolver.openInputStream(uri)
- val out = FileOutputStream(tempFile)
- inputStream!!.copyTo(out)
+ val tempFile = copyUriToTempFile(uri, "import-${System.currentTimeMillis()}-$DEFAULT_FILE_NAME")
+ if (tempFile == null) {
+ toast(org.fossify.commons.R.string.unknown_error_occurred)
+ return
+ }
+
showImportContactsDialog(tempFile.absolutePath, callback)
} catch (e: Exception) {
showErrorToast(e)
diff --git a/app/src/main/kotlin/org/fossify/contacts/extensions/Context.kt b/app/src/main/kotlin/org/fossify/contacts/extensions/Context.kt
index 44759fcfc..8f6a9fcb6 100644
--- a/app/src/main/kotlin/org/fossify/contacts/extensions/Context.kt
+++ b/app/src/main/kotlin/org/fossify/contacts/extensions/Context.kt
@@ -6,6 +6,7 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
+import android.net.Uri
import androidx.core.app.AlarmManagerCompat
import androidx.core.content.FileProvider
import org.fossify.commons.extensions.*
@@ -148,3 +149,13 @@ fun Context.backupContacts() {
}
}
+fun Context.copyUriToTempFile(uri: Uri, name: String): File? {
+ val tempFile = getTempFile(name)
+ contentResolver.openInputStream(uri)?.use { input ->
+ FileOutputStream(tempFile).use { output ->
+ input.copyTo(output)
+ }
+ }
+
+ return tempFile
+}