Improved how we load high-res contact picture to fallback on thumbnail if needed
This commit is contained in:
parent
4883bc8293
commit
4e0d38ca35
3 changed files with 30 additions and 31 deletions
|
@ -19,7 +19,6 @@
|
|||
*/
|
||||
package org.linphone.contact
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.activities.main.viewmodels.MessageNotifierViewModel
|
||||
|
@ -37,12 +36,6 @@ interface ContactDataInterface {
|
|||
|
||||
val showGroupChatAvatar: Boolean
|
||||
get() = false
|
||||
|
||||
val thumbnailUri: Uri?
|
||||
get() = contact.value?.getThumbnailUri()
|
||||
|
||||
val pictureUri: Uri?
|
||||
get() = contact.value?.getPictureUri()
|
||||
}
|
||||
|
||||
open class GenericContactData(private val sipAddress: Address) : ContactDataInterface {
|
||||
|
|
|
@ -33,7 +33,7 @@ import android.util.Patterns
|
|||
import androidx.core.app.Person
|
||||
import androidx.core.graphics.drawable.IconCompat
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import java.lang.NumberFormatException
|
||||
import java.io.IOException
|
||||
import kotlinx.coroutines.*
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.LinphoneApplication.Companion.corePreferences
|
||||
|
@ -353,36 +353,41 @@ fun Friend.hasPresence(): Boolean {
|
|||
return false
|
||||
}
|
||||
|
||||
fun Friend.getPictureUri(): Uri? {
|
||||
val refKey = refKey
|
||||
if (refKey != null) {
|
||||
try {
|
||||
val nativeId = refKey.toLong()
|
||||
return Uri.withAppendedPath(
|
||||
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, nativeId),
|
||||
ContactsContract.Contacts.Photo.DISPLAY_PHOTO
|
||||
)
|
||||
} catch (nfe: NumberFormatException) {}
|
||||
}
|
||||
|
||||
val photoUri = photo ?: return null
|
||||
return Uri.parse(photoUri)
|
||||
fun Friend.getThumbnailUri(): Uri? {
|
||||
return getPictureUri(true)
|
||||
}
|
||||
|
||||
fun Friend.getThumbnailUri(): Uri? {
|
||||
fun Friend.getPictureUri(thumbnailPreferred: Boolean = false): Uri? {
|
||||
val refKey = refKey
|
||||
if (refKey != null) {
|
||||
try {
|
||||
val nativeId = refKey.toLong()
|
||||
val lookupUri = ContentUris.withAppendedId(
|
||||
ContactsContract.Contacts.CONTENT_URI,
|
||||
refKey.toLong()
|
||||
)
|
||||
|
||||
if (!thumbnailPreferred) {
|
||||
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 {
|
||||
if (contentResolver.openAssetFileDescriptor(pictureUri, "r") != null) {
|
||||
return pictureUri
|
||||
}
|
||||
} catch (ioe: IOException) { }
|
||||
}
|
||||
|
||||
// Fallback to thumbnail if high res picture isn't available
|
||||
return Uri.withAppendedPath(
|
||||
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, nativeId),
|
||||
lookupUri,
|
||||
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY
|
||||
)
|
||||
} catch (nfe: NumberFormatException) {}
|
||||
} catch (e: Exception) { }
|
||||
}
|
||||
|
||||
val photoUri = photo ?: return null
|
||||
return Uri.parse(photoUri)
|
||||
return null
|
||||
}
|
||||
|
||||
fun Friend.getPerson(): Person {
|
||||
|
|
|
@ -52,6 +52,7 @@ import org.linphone.activities.voip.data.ConferenceParticipantDeviceData
|
|||
import org.linphone.activities.voip.views.HorizontalScrollDotsView
|
||||
import org.linphone.contact.ContactAvatarGenerator
|
||||
import org.linphone.contact.ContactDataInterface
|
||||
import org.linphone.contact.getPictureUri
|
||||
import org.linphone.core.tools.Log
|
||||
import org.linphone.views.VoiceRecordProgressBar
|
||||
|
||||
|
@ -344,7 +345,7 @@ private fun loadContactPictureWithCoil(
|
|||
if (contact != null) {
|
||||
val context = imageView.context
|
||||
val displayName = contact.contact.value?.name ?: contact.displayName.value.orEmpty()
|
||||
val source = if (useThumbnail) contact.thumbnailUri else contact.pictureUri
|
||||
val source = contact.contact.value?.getPictureUri(useThumbnail)
|
||||
imageView.load(source) {
|
||||
transformations(CircleCropTransformation())
|
||||
error(
|
||||
|
@ -390,7 +391,7 @@ fun loadContactPictureWithCoil(imageView: ImageView, contact: ContactDataInterfa
|
|||
@BindingAdapter("coilContactBig")
|
||||
fun loadBigContactPictureWithCoil(imageView: ImageView, contact: ContactDataInterface?) {
|
||||
loadContactPictureWithCoil(
|
||||
imageView, contact, true,
|
||||
imageView, contact, false,
|
||||
R.dimen.contact_avatar_big_size, R.dimen.contact_avatar_text_big_size
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue