handle importing contacts via opening vcf file intent

This commit is contained in:
tibbi 2018-01-15 23:19:16 +01:00
parent eefb6a1dab
commit d17ec1ff4f
4 changed files with 60 additions and 5 deletions

View File

@ -41,6 +41,16 @@
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<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

View File

@ -4,6 +4,7 @@ import android.app.SearchManager
import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Bundle
import android.support.v4.view.MenuItemCompat
import android.support.v4.view.ViewPager
@ -21,6 +22,7 @@ import com.simplemobiletools.contacts.dialogs.ExportContactsDialog
import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog
import com.simplemobiletools.contacts.dialogs.ImportContactsDialog
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.getTempFile
import com.simplemobiletools.contacts.extensions.onTabSelectionChanged
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.helpers.VcfExporter
@ -29,6 +31,7 @@ import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_contacts.*
import kotlinx.android.synthetic.main.fragment_favorites.*
import java.io.FileOutputStream
class MainActivity : SimpleActivity(), RefreshContactsListener {
private var isFirstResume = true
@ -238,6 +241,10 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
it.icon?.applyColorFilter(getAdjustedPrimaryColor())
}
)
if (intent?.action == Intent.ACTION_VIEW && intent.data != null) {
tryImportContactsFromFile(intent.data)
}
}
private fun showSortingDialog() {
@ -264,16 +271,39 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
private fun importContacts() {
FilePickerDialog(this) {
ImportContactsDialog(this, it) {
if (it) {
runOnUiThread {
refreshContacts()
}
showImportContactsDialog(it)
}
}
private fun showImportContactsDialog(path: String) {
ImportContactsDialog(this, path) {
if (it) {
runOnUiThread {
refreshContacts()
}
}
}
}
private fun tryImportContactsFromFile(uri: Uri) {
when {
uri.scheme == "file" -> 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) {

View File

@ -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")
}

View File

@ -3,4 +3,5 @@
<external-path name="external_files" path="."/>
<root-path name="external_files" path="/storage/" />
<cache-path name="cache_files" path="my_cache/" />
<cache-path name="shared_contacts" path="contacts/"/>
</paths>