Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.SEND" />

<data android:mimeType="text/directory" />
<data android:mimeType="text/vcard" />
<data android:mimeType="text/x-vcard" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,28 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
refreshMenuItems()
}

if (intent?.action == Intent.ACTION_VIEW && intent.data != null) {
tryImportContactsFromFile(intent.data!!) {
if (it) {
handleExternalIntent()
binding.mainDialpadButton.setOnClickListener {
launchDialpad()
}
}

private fun handleExternalIntent() {
val uri = when (intent?.action) {
Intent.ACTION_VIEW -> 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
}
}

Expand Down
16 changes: 6 additions & 10 deletions app/src/main/kotlin/org/fossify/contacts/extensions/Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/kotlin/org/fossify/contacts/extensions/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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
}
Loading