Show favorite (starred) Android contacts on top of contacts list

This commit is contained in:
Juha Heinanen
2023-10-30 16:46:37 +02:00
parent b9fec84bd5
commit 28da3c8fa1
3 changed files with 22 additions and 9 deletions

View File

@ -16,7 +16,8 @@ sealed class Contact {
var avatarImage: Bitmap? = null
}
class AndroidContact(var name: String, var color: Int, var thumbnailUri: Uri?): Contact() {
class AndroidContact(var name: String, var color: Int, var thumbnailUri: Uri?,
var favorite: Boolean): Contact() {
val uris = ArrayList<String>()
}
@ -118,11 +119,12 @@ sealed class Contact {
// If phone type is needed, add DATA2 to projection. Then phone type can be get from
// cursor using getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE
val projection = arrayOf(ContactsContract.Data.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.MIMETYPE, ContactsContract.Data.DATA1,
/* ContactsContract.Data.DATA2 ,*/ ContactsContract.Data.PHOTO_THUMBNAIL_URI)
ContactsContract.Data.MIMETYPE, ContactsContract.Data.DATA1,
/* ContactsContract.Data.DATA2 ,*/ ContactsContract.Data.PHOTO_THUMBNAIL_URI,
ContactsContract.Contacts.STARRED)
val selection =
ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + "' OR " +
ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'"
ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE + "' OR " +
ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'"
val cur: Cursor? = ctx.contentResolver.query(ContactsContract.Data.CONTENT_URI, projection,
selection, null, null)
BaresipService.androidContacts.clear()
@ -133,10 +135,11 @@ sealed class Contact {
val mime = cur.getString(2)
val data = cur.getString(3)
val thumb = cur.getString(4)?.toUri()
val starred = cur.getInt(5)
val contact = if (contacts.containsKey(id))
contacts[id]!!
else
AndroidContact(name, Utils.randomColor(), thumb)
AndroidContact(name, Utils.randomColor(), thumb, starred == 1)
if (contact.name == "" && name != "")
contact.name = name
if (contact.thumbnailUri == null && thumb != null)
@ -251,8 +254,8 @@ sealed class Contact {
private fun sortContacts() {
BaresipService.contacts.sortBy{ when (it) {
is BaresipContact -> it.name
is AndroidContact -> it.name
is BaresipContact -> "1" + it.name
is AndroidContact -> if (it.favorite) "0" + it.name else "1" + it.name
}}
}

View File

@ -153,7 +153,12 @@ class ContactListAdapter(private val ctx: Context, private val rows: ArrayList<C
}
}
viewHolder.actionView.visibility = View.GONE
if (contact.favorite) {
viewHolder.actionView.setImageResource(R.drawable.star)
viewHolder.actionView.visibility = View.VISIBLE
} else {
viewHolder.actionView.visibility = View.GONE
}
}

View File

@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="@color/colorSecondary" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
</vector>