From 5ef100ce1df02b04a9d59a2b706b0fec5c2226a3 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 12 Dec 2017 19:24:42 +0100 Subject: [PATCH] list only those contact sources that contain some contacts --- .../contacts/activities/ContactActivity.kt | 2 +- .../dialogs/FilterContactSourcesDialog.kt | 4 ++- .../contacts/helpers/ContactsHelper.kt | 26 ++++++++++++++++--- .../contacts/models/Contact.kt | 2 +- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt index 6a93f748..b3231328 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/ContactActivity.kt @@ -135,7 +135,7 @@ class ContactActivity : SimpleActivity() { private fun setupNewContact() { window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) supportActionBar?.title = resources.getString(R.string.new_contact) - contact = Contact(0, "", "", "", "") + contact = Contact(0, "", "", "", "", "") } private fun applyPhotoPlaceholder() { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt index 064523b5..9bccf0e7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/FilterContactSourcesDialog.kt @@ -16,7 +16,9 @@ class FilterContactSourcesDialog(val activity: SimpleActivity, val callback: () init { ContactsHelper(activity).getContactSources { val selectedSources = activity.config.displayContactSources - view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources) + activity.runOnUiThread { + view.filter_contact_sources_list.adapter = FilterContactSourcesAdapter(activity, it, selectedSources) + } } dialog = AlertDialog.Builder(activity) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index 18facada..51c56a7f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -30,7 +30,8 @@ class ContactsHelper(val activity: SimpleActivity) { cursor?.close() } - callback(ArrayList(accounts)) + val sourcesWithContacts = ArrayList(accounts).filter { doesSourceContainContacts(it) } as ArrayList + callback(sourcesWithContacts) }.start() } @@ -49,7 +50,9 @@ class ContactsHelper(val activity: SimpleActivity) { val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) ?: continue val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: "" val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: "" - val contact = Contact(id, name, number, photoUri, "") + val email = "" // proper value is obtained below + val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) + val contact = Contact(id, name, number, photoUri, email, accountName) contacts.put(id, contact) } while (cursor.moveToNext()) } @@ -69,6 +72,20 @@ class ContactsHelper(val activity: SimpleActivity) { }.start() } + fun doesSourceContainContacts(source: String): Boolean { + val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI + val projection = arrayOf(ContactsContract.CommonDataKinds.Email.CONTACT_ID) + val selection = "${ContactsContract.RawContacts.ACCOUNT_NAME} = ?" + val selectionArgs = arrayOf(source) + var cursor: Cursor? = null + try { + cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null) + return (cursor?.moveToFirst() == true) + } finally { + cursor?.close() + } + } + private fun getEmails(): ArrayList> { val pairs = ArrayList>() val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI @@ -124,7 +141,7 @@ class ContactsHelper(val activity: SimpleActivity) { val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: "" val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: "" val email = getContactEmail(id) - return Contact(id, name, number, photoUri, email) + return Contact(id, name, number, photoUri, email, "") } } finally { cursor?.close() @@ -137,7 +154,8 @@ class ContactsHelper(val activity: SimpleActivity) { ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, - ContactsContract.CommonDataKinds.Phone.PHOTO_URI + ContactsContract.CommonDataKinds.Phone.PHOTO_URI, + ContactsContract.RawContacts.ACCOUNT_NAME ) private fun getSortString(): String { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt index 91df8050..50264391 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt @@ -3,7 +3,7 @@ package com.simplemobiletools.contacts.models import com.simplemobiletools.commons.helpers.SORT_BY_NUMBER import com.simplemobiletools.commons.helpers.SORT_DESCENDING -data class Contact(val id: Int, var name: String, var number: String, var photoUri: String, var email: String) : Comparable { +data class Contact(val id: Int, var name: String, var number: String, var photoUri: String, var email: String, var source: String) : Comparable { companion object { var sorting: Int = 0 }