fix #362, properly handle exporting contacts that are filtered out

This commit is contained in:
tibbi 2019-01-24 16:49:38 +01:00
parent 02ea571166
commit e5862e700a
4 changed files with 39 additions and 35 deletions

View file

@ -32,7 +32,6 @@ import com.simplemobiletools.contacts.pro.extensions.getTempFile
import com.simplemobiletools.contacts.pro.fragments.MyViewPagerFragment
import com.simplemobiletools.contacts.pro.helpers.*
import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener
import com.simplemobiletools.contacts.pro.models.Contact
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_contacts.*
import kotlinx.android.synthetic.main.fragment_favorites.*
@ -429,23 +428,20 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
private fun exportContacts() {
FilePickerDialog(this, pickFile = false, showFAB = true) {
ExportContactsDialog(this, it) { file, contactSources ->
Thread {
ContactsHelper(this).getContacts { allContacts ->
val contacts = allContacts.filter { contactSources.contains(it.source) }
if (contacts.isEmpty()) {
toast(R.string.no_entries_for_exporting)
} else {
VcfExporter().exportContacts(this, file, contacts as ArrayList<Contact>, true) { result ->
toast(when (result) {
VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed
else -> R.string.exporting_failed
})
}
ExportContactsDialog(this, it) { file, ignoredContactSources ->
ContactsHelper(this).getContacts(ignoredContactSources) { contacts ->
if (contacts.isEmpty()) {
toast(R.string.no_entries_for_exporting)
} else {
VcfExporter().exportContacts(this, file, contacts, true) { result ->
toast(when (result) {
VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed
else -> R.string.exporting_failed
})
}
}
}.start()
}
}
}
}

View file

@ -8,13 +8,12 @@ import com.simplemobiletools.contacts.pro.activities.SimpleActivity
import com.simplemobiletools.contacts.pro.adapters.FilterContactSourcesAdapter
import com.simplemobiletools.contacts.pro.extensions.getVisibleContactSources
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
import com.simplemobiletools.contacts.pro.helpers.SMT_PRIVATE
import com.simplemobiletools.contacts.pro.models.ContactSource
import kotlinx.android.synthetic.main.dialog_export_contacts.view.*
import java.io.File
import java.util.*
class ExportContactsDialog(val activity: SimpleActivity, val path: String, private val callback: (file: File, contactSources: HashSet<String>) -> Unit) {
class ExportContactsDialog(val activity: SimpleActivity, val path: String, private val callback: (file: File, ignoredContactSources: HashSet<String>) -> Unit) {
private var contactSources = ArrayList<ContactSource>()
init {
@ -50,13 +49,12 @@ class ExportContactsDialog(val activity: SimpleActivity, val path: String, priva
return@setOnClickListener
}
val selectedSources = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources()
val selectedContactSourceNames = HashSet<String>()
selectedSources.forEach {
selectedContactSourceNames.add(if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.name)
}
callback(file, selectedContactSourceNames)
dismiss()
Thread {
val selectedSources = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources()
val ignoredSources = contactSources.filter { !selectedSources.contains(it) }.map { it.getFullIdentifier() }.toHashSet()
callback(file, ignoredSources)
dismiss()
}.start()
}
else -> activity.toast(R.string.invalid_name)
}

View file

@ -304,13 +304,17 @@ fun Context.getContactPublicUri(contact: Contact): Uri {
}
fun Context.getVisibleContactSources(): ArrayList<String> {
val sources = getAllContactSources()
val ignoredContactSources = config.ignoredContactSources
return ArrayList(sources).filter { !ignoredContactSources.contains(it.getFullIdentifier()) }
.map { if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.name }.toMutableList() as ArrayList<String>
}
fun Context.getAllContactSources(): List<ContactSource> {
val sources = ContactsHelper(this).getDeviceContactSources()
val phoneSecret = getString(R.string.phone_storage_hidden)
sources.add(ContactSource(phoneSecret, SMT_PRIVATE, phoneSecret))
val ignoredContactSources = config.ignoredContactSources
val sourceNames = ArrayList(sources).filter { !ignoredContactSources.contains(it.getFullIdentifier()) }
.map { if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.name }.toMutableList() as ArrayList<String>
return sourceNames
return sources.toMutableList()
}
@TargetApi(Build.VERSION_CODES.N)

View file

@ -28,11 +28,17 @@ class ContactsHelper(val context: Context) {
private val BATCH_SIZE = 100
private var displayContactSources = ArrayList<String>()
fun getContacts(callback: (ArrayList<Contact>) -> Unit) {
fun getContacts(ignoredContactSources: HashSet<String>? = null, callback: (ArrayList<Contact>) -> Unit) {
Thread {
val contacts = SparseArray<Contact>()
displayContactSources = context.getVisibleContactSources()
getDeviceContacts(contacts)
if (ignoredContactSources != null) {
displayContactSources = context.getAllContactSources().filter {
!ignoredContactSources.contains(it.getFullIdentifier())
}.map { it.getFullIdentifier() }.toMutableList() as ArrayList
}
getDeviceContacts(contacts, ignoredContactSources)
if (displayContactSources.contains(SMT_PRIVATE)) {
LocalContactsHelper(context).getAllContacts().forEach {
@ -46,7 +52,7 @@ class ContactsHelper(val context: Context) {
val resultContacts = ArrayList<Contact>(contactsSize)
(0 until contactsSize).filter {
if (showOnlyContactsWithNumbers) {
if (ignoredContactSources == null && showOnlyContactsWithNumbers) {
contacts.valueAt(it).phoneNumbers.isNotEmpty()
} else {
true
@ -55,7 +61,7 @@ class ContactsHelper(val context: Context) {
contacts.valueAt(it)
}
if (context.config.filterDuplicates) {
if (ignoredContactSources == null && context.config.filterDuplicates) {
tempContacts = tempContacts.distinctBy {
it.getHashToCompare()
} as ArrayList<Contact>
@ -127,12 +133,12 @@ class ContactsHelper(val context: Context) {
}
}
private fun getDeviceContacts(contacts: SparseArray<Contact>) {
private fun getDeviceContacts(contacts: SparseArray<Contact>, ignoredContactSources: HashSet<String>?) {
if (!context.hasContactPermissions()) {
return
}
val ignoredSources = context.config.ignoredContactSources
val ignoredSources = ignoredContactSources ?: context.config.ignoredContactSources
val uri = ContactsContract.Data.CONTENT_URI
val projection = getContactProjection()