Use better quality of native contact picture if available, otherwise fallback to thumbnail

This commit is contained in:
Sylvain Berfini 2023-11-22 11:55:31 +01:00
parent 5f9edb4fcc
commit 688f797acf
3 changed files with 47 additions and 27 deletions

View file

@ -19,10 +19,8 @@
*/ */
package org.linphone.contacts package org.linphone.contacts
import android.content.ContentUris
import android.database.Cursor import android.database.Cursor
import android.database.StaleDataException import android.database.StaleDataException
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.ContactsContract import android.provider.ContactsContract
import android.util.Patterns import android.util.Patterns
@ -134,13 +132,10 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
) )
friend.name = displayName friend.name = displayName
friend.photo = Uri.withAppendedPath( val uri = friend.getNativeContactPictureUri()
ContentUris.withAppendedId( if (uri != null) {
ContactsContract.Contacts.CONTENT_URI, friend.photo = uri.toString()
id.toLong() }
),
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY
).toString()
val starred = val starred =
cursor.getInt( cursor.getInt(

View file

@ -32,6 +32,7 @@ import androidx.core.app.ActivityCompat
import androidx.core.app.Person import androidx.core.app.Person
import androidx.core.graphics.drawable.IconCompat import androidx.core.graphics.drawable.IconCompat
import androidx.loader.app.LoaderManager import androidx.loader.app.LoaderManager
import java.io.IOException
import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.core.Address import org.linphone.core.Address
import org.linphone.core.ConferenceInfo import org.linphone.core.ConferenceInfo
@ -374,14 +375,10 @@ class ContactsManager @UiThread constructor() {
} }
if (friend.photo.isNullOrEmpty()) { if (friend.photo.isNullOrEmpty()) {
val picture = Uri.withAppendedPath( val uri = friend.getNativeContactPictureUri()
ContentUris.withAppendedId( if (uri != null) {
ContactsContract.Contacts.CONTENT_URI, friend.photo = uri.toString()
id.toLong() }
),
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY
).toString()
friend.photo = picture
} }
if (friend.nativeUri.isNullOrEmpty()) { if (friend.nativeUri.isNullOrEmpty()) {
@ -450,6 +447,42 @@ fun Friend.getAvatarBitmap(): Bitmap? {
return ImageUtils.getBitmap(coreContext.context, photo) return ImageUtils.getBitmap(coreContext.context, photo)
} }
@WorkerThread
fun Friend.getNativeContactPictureUri(): Uri? {
val contactId = refKey
if (contactId != null) {
try {
val lookupUri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI,
contactId.toLong()
)
val pictureUri = Uri.withAppendedPath(
lookupUri,
ContactsContract.Contacts.Photo.DISPLAY_PHOTO
)
// Check that the URI points to a real file
val contentResolver = coreContext.context.contentResolver
try {
val fd = contentResolver.openAssetFileDescriptor(pictureUri, "r")
if (fd != null) {
fd.close()
return pictureUri
}
} catch (_: IOException) { }
// Fallback to thumbnail
return Uri.withAppendedPath(
lookupUri,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY
)
} catch (numberFormatException: NumberFormatException) {
// Expected for contacts created by Linphone
}
}
return null
}
@WorkerThread @WorkerThread
fun Friend.getPerson(): Person { fun Friend.getPerson(): Person {
val personBuilder = Person.Builder().setName(name) val personBuilder = Person.Builder().setName(name)

View file

@ -19,14 +19,13 @@
*/ */
package org.linphone.ui.main.contacts.model package org.linphone.ui.main.contacts.model
import android.content.ContentUris
import android.net.Uri import android.net.Uri
import android.provider.ContactsContract
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.R import org.linphone.R
import org.linphone.contacts.AbstractAvatarModel import org.linphone.contacts.AbstractAvatarModel
import org.linphone.contacts.getNativeContactPictureUri
import org.linphone.core.ChatRoom.SecurityLevel import org.linphone.core.ChatRoom.SecurityLevel
import org.linphone.core.ConsolidatedPresence import org.linphone.core.ConsolidatedPresence
import org.linphone.core.Friend import org.linphone.core.Friend
@ -107,14 +106,7 @@ class ContactAvatarModel @WorkerThread constructor(val friend: Friend) : Abstrac
val refKey = friend.refKey val refKey = friend.refKey
if (refKey != null) { if (refKey != null) {
try { try {
val lookupUri = ContentUris.withAppendedId( return friend.getNativeContactPictureUri()
ContactsContract.Contacts.CONTENT_URI,
refKey.toLong()
)
return Uri.withAppendedPath(
lookupUri,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY
)
} catch (numberFormatException: NumberFormatException) { } catch (numberFormatException: NumberFormatException) {
// Expected for contacts created by Linphone // Expected for contacts created by Linphone
} }