mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
adding the Import Contacts dialog
This commit is contained in:
@ -304,7 +304,11 @@ class ContactActivity : SimpleActivity() {
|
|||||||
supportActionBar?.title = resources.getString(R.string.new_contact)
|
supportActionBar?.title = resources.getString(R.string.new_contact)
|
||||||
contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), "", 0, 0)
|
contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), "", 0, 0)
|
||||||
contact_source.text = config.lastUsedContactSource
|
contact_source.text = config.lastUsedContactSource
|
||||||
contact_source.setOnClickListener { showAccountSourcePicker() }
|
contact_source.setOnClickListener {
|
||||||
|
showContactSourcePicker(contact_source.value) {
|
||||||
|
contact_source.text = it
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showPhotoPlaceholder() {
|
private fun showPhotoPlaceholder() {
|
||||||
@ -639,27 +643,6 @@ class ContactActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showAccountSourcePicker() {
|
|
||||||
ContactsHelper(this).getContactSources {
|
|
||||||
val items = ArrayList<RadioItem>()
|
|
||||||
val sources = it
|
|
||||||
val currentSource = contact_source.value
|
|
||||||
var currentSourceIndex = -1
|
|
||||||
sources.forEachIndexed { index, account ->
|
|
||||||
items.add(RadioItem(index, account))
|
|
||||||
if (account == currentSource) {
|
|
||||||
currentSourceIndex = index
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
runOnUiThread {
|
|
||||||
RadioGroupDialog(this, items, currentSourceIndex) {
|
|
||||||
contact_source.text = sources[it as Int]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun toggleFavorite() {
|
private fun toggleFavorite() {
|
||||||
val isStarred = isContactStarred()
|
val isStarred = isContactStarred()
|
||||||
contact_toggle_favorite.apply {
|
contact_toggle_favorite.apply {
|
||||||
|
@ -18,6 +18,7 @@ import com.simplemobiletools.contacts.R
|
|||||||
import com.simplemobiletools.contacts.adapters.ViewPagerAdapter
|
import com.simplemobiletools.contacts.adapters.ViewPagerAdapter
|
||||||
import com.simplemobiletools.contacts.dialogs.ChangeSortingDialog
|
import com.simplemobiletools.contacts.dialogs.ChangeSortingDialog
|
||||||
import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog
|
import com.simplemobiletools.contacts.dialogs.FilterContactSourcesDialog
|
||||||
|
import com.simplemobiletools.contacts.dialogs.ImportContactsDialog
|
||||||
import com.simplemobiletools.contacts.extensions.config
|
import com.simplemobiletools.contacts.extensions.config
|
||||||
import com.simplemobiletools.contacts.extensions.onTabSelectionChanged
|
import com.simplemobiletools.contacts.extensions.onTabSelectionChanged
|
||||||
import com.simplemobiletools.contacts.interfaces.RefreshContactsListener
|
import com.simplemobiletools.contacts.interfaces.RefreshContactsListener
|
||||||
@ -258,7 +259,13 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
|
|||||||
|
|
||||||
private fun importContacts() {
|
private fun importContacts() {
|
||||||
FilePickerDialog(this) {
|
FilePickerDialog(this) {
|
||||||
|
ImportContactsDialog(this, it) {
|
||||||
|
if (it) {
|
||||||
|
runOnUiThread {
|
||||||
|
refreshContacts()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
package com.simplemobiletools.contacts.dialogs
|
||||||
|
|
||||||
|
import android.support.v7.app.AlertDialog
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import com.simplemobiletools.commons.extensions.setupDialogStuff
|
||||||
|
import com.simplemobiletools.commons.extensions.toast
|
||||||
|
import com.simplemobiletools.commons.extensions.value
|
||||||
|
import com.simplemobiletools.contacts.R
|
||||||
|
import com.simplemobiletools.contacts.activities.SimpleActivity
|
||||||
|
import com.simplemobiletools.contacts.extensions.config
|
||||||
|
import com.simplemobiletools.contacts.extensions.showContactSourcePicker
|
||||||
|
import com.simplemobiletools.contacts.helpers.VcfImporter
|
||||||
|
import com.simplemobiletools.contacts.helpers.VcfImporter.ImportResult.IMPORT_FAIL
|
||||||
|
import kotlinx.android.synthetic.main.dialog_import_contacts.view.*
|
||||||
|
|
||||||
|
class ImportContactsDialog(val activity: SimpleActivity, val path: String, val callback: (refreshView: Boolean) -> Unit) {
|
||||||
|
init {
|
||||||
|
val view = (activity.layoutInflater.inflate(R.layout.dialog_import_contacts, null) as ViewGroup).apply {
|
||||||
|
import_contacts_title.text = activity.config.lastUsedContactSource
|
||||||
|
import_contacts_title.setOnClickListener {
|
||||||
|
activity.showContactSourcePicker(import_contacts_title.value) {
|
||||||
|
import_contacts_title.text = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AlertDialog.Builder(activity)
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.create().apply {
|
||||||
|
activity.setupDialogStuff(view, this, R.string.import_contacts) {
|
||||||
|
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
||||||
|
activity.toast(R.string.importing)
|
||||||
|
Thread {
|
||||||
|
val result = VcfImporter(activity).importContacts(path, view.import_contacts_title.value)
|
||||||
|
handleParseResult(result)
|
||||||
|
dismiss()
|
||||||
|
}.start()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleParseResult(result: VcfImporter.ImportResult) {
|
||||||
|
activity.toast(when (result) {
|
||||||
|
VcfImporter.ImportResult.IMPORT_OK -> R.string.importing_successful
|
||||||
|
VcfImporter.ImportResult.IMPORT_PARTIAL -> R.string.importing_some_entries_failed
|
||||||
|
else -> R.string.importing_failed
|
||||||
|
})
|
||||||
|
callback(result != IMPORT_FAIL)
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ import com.simplemobiletools.commons.extensions.toast
|
|||||||
import com.simplemobiletools.commons.helpers.PERMISSION_CALL_PHONE
|
import com.simplemobiletools.commons.helpers.PERMISSION_CALL_PHONE
|
||||||
import com.simplemobiletools.commons.models.RadioItem
|
import com.simplemobiletools.commons.models.RadioItem
|
||||||
import com.simplemobiletools.contacts.activities.SimpleActivity
|
import com.simplemobiletools.contacts.activities.SimpleActivity
|
||||||
|
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
||||||
import com.simplemobiletools.contacts.models.Contact
|
import com.simplemobiletools.contacts.models.Contact
|
||||||
|
|
||||||
fun SimpleActivity.startCallIntent(recipient: String) {
|
fun SimpleActivity.startCallIntent(recipient: String) {
|
||||||
@ -40,3 +41,23 @@ fun SimpleActivity.tryStartCall(contact: Contact) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun SimpleActivity.showContactSourcePicker(currentSource: String, callback: (newSource: String) -> Unit) {
|
||||||
|
ContactsHelper(this).getContactSources {
|
||||||
|
val items = ArrayList<RadioItem>()
|
||||||
|
val sources = it
|
||||||
|
var currentSourceIndex = -1
|
||||||
|
sources.forEachIndexed { index, account ->
|
||||||
|
items.add(RadioItem(index, account))
|
||||||
|
if (account == currentSource) {
|
||||||
|
currentSourceIndex = index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
runOnUiThread {
|
||||||
|
RadioGroupDialog(this, items, currentSourceIndex) {
|
||||||
|
callback(sources[it as Int])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -22,3 +22,14 @@ val PHOTO_UNCHANGED = 4
|
|||||||
val DEFAULT_EMAIL_TYPE = ContactsContract.CommonDataKinds.Email.TYPE_HOME
|
val DEFAULT_EMAIL_TYPE = ContactsContract.CommonDataKinds.Email.TYPE_HOME
|
||||||
val DEFAULT_PHONE_NUMBER_TYPE = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
|
val DEFAULT_PHONE_NUMBER_TYPE = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
|
||||||
val DEFAULT_EVENT_TYPE = ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY
|
val DEFAULT_EVENT_TYPE = ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY
|
||||||
|
|
||||||
|
// export/import
|
||||||
|
val BEGIN_VCARD = "BEGIN:VCARD"
|
||||||
|
val END_VCARD = "END:VCARD"
|
||||||
|
val N = "N"
|
||||||
|
val FN = "FN"
|
||||||
|
val TEL = "TEL"
|
||||||
|
val VERSION = "VERSION"
|
||||||
|
val BDAY = "BDAY"
|
||||||
|
val PHOTO = "PHOTO"
|
||||||
|
val EMAIL = "EMAIL"
|
||||||
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.simplemobiletools.contacts.helpers
|
||||||
|
|
||||||
|
import android.widget.Toast
|
||||||
|
import com.simplemobiletools.commons.extensions.showErrorToast
|
||||||
|
import com.simplemobiletools.contacts.activities.SimpleActivity
|
||||||
|
import com.simplemobiletools.contacts.helpers.VcfImporter.ImportResult.*
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class VcfImporter(val activity: SimpleActivity) {
|
||||||
|
enum class ImportResult {
|
||||||
|
IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL
|
||||||
|
}
|
||||||
|
|
||||||
|
private var contactsImported = 0
|
||||||
|
private var contactsFailed = 0
|
||||||
|
|
||||||
|
fun importContacts(path: String, targetContactSource: String): ImportResult {
|
||||||
|
try {
|
||||||
|
val inputStream = if (path.contains("/")) {
|
||||||
|
File(path).inputStream()
|
||||||
|
} else {
|
||||||
|
activity.assets.open(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
inputStream.bufferedReader().use {
|
||||||
|
while (true) {
|
||||||
|
val line = it.readLine() ?: break
|
||||||
|
if (line.trim().isEmpty())
|
||||||
|
continue
|
||||||
|
|
||||||
|
if (line == BEGIN_VCARD) {
|
||||||
|
resetValues()
|
||||||
|
} else if (line == END_VCARD) {
|
||||||
|
contactsImported++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
activity.showErrorToast(e, Toast.LENGTH_LONG)
|
||||||
|
contactsFailed++
|
||||||
|
}
|
||||||
|
|
||||||
|
return when {
|
||||||
|
contactsImported == 0 -> IMPORT_FAIL
|
||||||
|
contactsFailed > 0 -> IMPORT_PARTIAL
|
||||||
|
else -> IMPORT_OK
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun resetValues() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
28
app/src/main/res/layout/dialog_import_contacts.xml
Normal file
28
app/src/main/res/layout/dialog_import_contacts.xml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:id="@+id/import_contacts_holder"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:paddingLeft="@dimen/activity_margin"
|
||||||
|
android:paddingRight="@dimen/activity_margin"
|
||||||
|
android:paddingTop="@dimen/activity_margin">
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyTextView
|
||||||
|
android:id="@+id/import_contacts_source_label"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/target_contact_source"
|
||||||
|
android:textSize="@dimen/smaller_text_size"/>
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyTextView
|
||||||
|
android:id="@+id/import_contacts_title"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="?attr/selectableItemBackground"
|
||||||
|
android:padding="@dimen/activity_margin"
|
||||||
|
tools:text="Phone"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
Reference in New Issue
Block a user