list only those contact sources that contain some contacts

This commit is contained in:
tibbi 2017-12-12 19:24:42 +01:00
parent d22e27ae40
commit 5ef100ce1d
4 changed files with 27 additions and 7 deletions

View file

@ -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() {

View file

@ -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)

View file

@ -30,7 +30,8 @@ class ContactsHelper(val activity: SimpleActivity) {
cursor?.close()
}
callback(ArrayList<String>(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<Pair<Int, String>> {
val pairs = ArrayList<Pair<Int, String>>()
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 {

View file

@ -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<Contact> {
data class Contact(val id: Int, var name: String, var number: String, var photoUri: String, var email: String, var source: String) : Comparable<Contact> {
companion object {
var sorting: Int = 0
}