From e5862e700a47c23751bb0e7e72bf713ce11f0c2b Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 24 Jan 2019 16:49:38 +0100 Subject: [PATCH] fix #362, properly handle exporting contacts that are filtered out --- .../contacts/pro/activities/MainActivity.kt | 28 ++++++++----------- .../pro/dialogs/ExportContactsDialog.kt | 16 +++++------ .../contacts/pro/extensions/Context.kt | 12 +++++--- .../contacts/pro/helpers/ContactsHelper.kt | 18 ++++++++---- 4 files changed, 39 insertions(+), 35 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt index e2d67b20..b291f9d0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt @@ -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, 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() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ExportContactsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ExportContactsDialog.kt index a23aed75..d1101471 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ExportContactsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/ExportContactsDialog.kt @@ -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) -> Unit) { +class ExportContactsDialog(val activity: SimpleActivity, val path: String, private val callback: (file: File, ignoredContactSources: HashSet) -> Unit) { private var contactSources = ArrayList() 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() - 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) } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt index 1b307d40..d1ce1b7e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt @@ -304,13 +304,17 @@ fun Context.getContactPublicUri(contact: Contact): Uri { } fun Context.getVisibleContactSources(): ArrayList { + 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 +} + +fun Context.getAllContactSources(): List { 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 - return sourceNames + return sources.toMutableList() } @TargetApi(Build.VERSION_CODES.N) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt index 8e178531..508671a5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt @@ -28,11 +28,17 @@ class ContactsHelper(val context: Context) { private val BATCH_SIZE = 100 private var displayContactSources = ArrayList() - fun getContacts(callback: (ArrayList) -> Unit) { + fun getContacts(ignoredContactSources: HashSet? = null, callback: (ArrayList) -> Unit) { Thread { val contacts = SparseArray() 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(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 @@ -127,12 +133,12 @@ class ContactsHelper(val context: Context) { } } - private fun getDeviceContacts(contacts: SparseArray) { + private fun getDeviceContacts(contacts: SparseArray, ignoredContactSources: HashSet?) { if (!context.hasContactPermissions()) { return } - val ignoredSources = context.config.ignoredContactSources + val ignoredSources = ignoredContactSources ?: context.config.ignoredContactSources val uri = ContactsContract.Data.CONTENT_URI val projection = getContactProjection()