mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
fix #362, properly handle exporting contacts that are filtered out
This commit is contained in:
@ -32,7 +32,6 @@ import com.simplemobiletools.contacts.pro.extensions.getTempFile
|
|||||||
import com.simplemobiletools.contacts.pro.fragments.MyViewPagerFragment
|
import com.simplemobiletools.contacts.pro.fragments.MyViewPagerFragment
|
||||||
import com.simplemobiletools.contacts.pro.helpers.*
|
import com.simplemobiletools.contacts.pro.helpers.*
|
||||||
import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener
|
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.activity_main.*
|
||||||
import kotlinx.android.synthetic.main.fragment_contacts.*
|
import kotlinx.android.synthetic.main.fragment_contacts.*
|
||||||
import kotlinx.android.synthetic.main.fragment_favorites.*
|
import kotlinx.android.synthetic.main.fragment_favorites.*
|
||||||
@ -429,23 +428,20 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
|
|||||||
|
|
||||||
private fun exportContacts() {
|
private fun exportContacts() {
|
||||||
FilePickerDialog(this, pickFile = false, showFAB = true) {
|
FilePickerDialog(this, pickFile = false, showFAB = true) {
|
||||||
ExportContactsDialog(this, it) { file, contactSources ->
|
ExportContactsDialog(this, it) { file, ignoredContactSources ->
|
||||||
Thread {
|
ContactsHelper(this).getContacts(ignoredContactSources) { contacts ->
|
||||||
ContactsHelper(this).getContacts { allContacts ->
|
if (contacts.isEmpty()) {
|
||||||
val contacts = allContacts.filter { contactSources.contains(it.source) }
|
toast(R.string.no_entries_for_exporting)
|
||||||
if (contacts.isEmpty()) {
|
} else {
|
||||||
toast(R.string.no_entries_for_exporting)
|
VcfExporter().exportContacts(this, file, contacts, true) { result ->
|
||||||
} else {
|
toast(when (result) {
|
||||||
VcfExporter().exportContacts(this, file, contacts as ArrayList<Contact>, true) { result ->
|
VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
|
||||||
toast(when (result) {
|
VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed
|
||||||
VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
|
else -> R.string.exporting_failed
|
||||||
VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed
|
})
|
||||||
else -> R.string.exporting_failed
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.start()
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,12 @@ import com.simplemobiletools.contacts.pro.activities.SimpleActivity
|
|||||||
import com.simplemobiletools.contacts.pro.adapters.FilterContactSourcesAdapter
|
import com.simplemobiletools.contacts.pro.adapters.FilterContactSourcesAdapter
|
||||||
import com.simplemobiletools.contacts.pro.extensions.getVisibleContactSources
|
import com.simplemobiletools.contacts.pro.extensions.getVisibleContactSources
|
||||||
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
|
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
|
||||||
import com.simplemobiletools.contacts.pro.helpers.SMT_PRIVATE
|
|
||||||
import com.simplemobiletools.contacts.pro.models.ContactSource
|
import com.simplemobiletools.contacts.pro.models.ContactSource
|
||||||
import kotlinx.android.synthetic.main.dialog_export_contacts.view.*
|
import kotlinx.android.synthetic.main.dialog_export_contacts.view.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.*
|
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>()
|
private var contactSources = ArrayList<ContactSource>()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@ -50,13 +49,12 @@ class ExportContactsDialog(val activity: SimpleActivity, val path: String, priva
|
|||||||
return@setOnClickListener
|
return@setOnClickListener
|
||||||
}
|
}
|
||||||
|
|
||||||
val selectedSources = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources()
|
Thread {
|
||||||
val selectedContactSourceNames = HashSet<String>()
|
val selectedSources = (view.export_contacts_list.adapter as FilterContactSourcesAdapter).getSelectedContactSources()
|
||||||
selectedSources.forEach {
|
val ignoredSources = contactSources.filter { !selectedSources.contains(it) }.map { it.getFullIdentifier() }.toHashSet()
|
||||||
selectedContactSourceNames.add(if (it.type == SMT_PRIVATE) SMT_PRIVATE else it.name)
|
callback(file, ignoredSources)
|
||||||
}
|
dismiss()
|
||||||
callback(file, selectedContactSourceNames)
|
}.start()
|
||||||
dismiss()
|
|
||||||
}
|
}
|
||||||
else -> activity.toast(R.string.invalid_name)
|
else -> activity.toast(R.string.invalid_name)
|
||||||
}
|
}
|
||||||
|
@ -304,13 +304,17 @@ fun Context.getContactPublicUri(contact: Contact): Uri {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getVisibleContactSources(): ArrayList<String> {
|
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 sources = ContactsHelper(this).getDeviceContactSources()
|
||||||
val phoneSecret = getString(R.string.phone_storage_hidden)
|
val phoneSecret = getString(R.string.phone_storage_hidden)
|
||||||
sources.add(ContactSource(phoneSecret, SMT_PRIVATE, phoneSecret))
|
sources.add(ContactSource(phoneSecret, SMT_PRIVATE, phoneSecret))
|
||||||
val ignoredContactSources = config.ignoredContactSources
|
return sources.toMutableList()
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.N)
|
@TargetApi(Build.VERSION_CODES.N)
|
||||||
|
@ -28,11 +28,17 @@ class ContactsHelper(val context: Context) {
|
|||||||
private val BATCH_SIZE = 100
|
private val BATCH_SIZE = 100
|
||||||
private var displayContactSources = ArrayList<String>()
|
private var displayContactSources = ArrayList<String>()
|
||||||
|
|
||||||
fun getContacts(callback: (ArrayList<Contact>) -> Unit) {
|
fun getContacts(ignoredContactSources: HashSet<String>? = null, callback: (ArrayList<Contact>) -> Unit) {
|
||||||
Thread {
|
Thread {
|
||||||
val contacts = SparseArray<Contact>()
|
val contacts = SparseArray<Contact>()
|
||||||
displayContactSources = context.getVisibleContactSources()
|
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)) {
|
if (displayContactSources.contains(SMT_PRIVATE)) {
|
||||||
LocalContactsHelper(context).getAllContacts().forEach {
|
LocalContactsHelper(context).getAllContacts().forEach {
|
||||||
@ -46,7 +52,7 @@ class ContactsHelper(val context: Context) {
|
|||||||
val resultContacts = ArrayList<Contact>(contactsSize)
|
val resultContacts = ArrayList<Contact>(contactsSize)
|
||||||
|
|
||||||
(0 until contactsSize).filter {
|
(0 until contactsSize).filter {
|
||||||
if (showOnlyContactsWithNumbers) {
|
if (ignoredContactSources == null && showOnlyContactsWithNumbers) {
|
||||||
contacts.valueAt(it).phoneNumbers.isNotEmpty()
|
contacts.valueAt(it).phoneNumbers.isNotEmpty()
|
||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
@ -55,7 +61,7 @@ class ContactsHelper(val context: Context) {
|
|||||||
contacts.valueAt(it)
|
contacts.valueAt(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context.config.filterDuplicates) {
|
if (ignoredContactSources == null && context.config.filterDuplicates) {
|
||||||
tempContacts = tempContacts.distinctBy {
|
tempContacts = tempContacts.distinctBy {
|
||||||
it.getHashToCompare()
|
it.getHashToCompare()
|
||||||
} as ArrayList<Contact>
|
} 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()) {
|
if (!context.hasContactPermissions()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val ignoredSources = context.config.ignoredContactSources
|
val ignoredSources = ignoredContactSources ?: context.config.ignoredContactSources
|
||||||
val uri = ContactsContract.Data.CONTENT_URI
|
val uri = ContactsContract.Data.CONTENT_URI
|
||||||
val projection = getContactProjection()
|
val projection = getContactProjection()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user