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
|
package org.linphone.contact
|
||||||
|
|
||||||
import android.net.Uri
|
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||||
import org.linphone.activities.main.viewmodels.MessageNotifierViewModel
|
import org.linphone.activities.main.viewmodels.MessageNotifierViewModel
|
||||||
|
@ -37,12 +36,6 @@ interface ContactDataInterface {
|
||||||
|
|
||||||
val showGroupChatAvatar: Boolean
|
val showGroupChatAvatar: Boolean
|
||||||
get() = false
|
get() = false
|
||||||
|
|
||||||
val thumbnailUri: Uri?
|
|
||||||
get() = contact.value?.getThumbnailUri()
|
|
||||||
|
|
||||||
val pictureUri: Uri?
|
|
||||||
get() = contact.value?.getPictureUri()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open class GenericContactData(private val sipAddress: Address) : ContactDataInterface {
|
open class GenericContactData(private val sipAddress: Address) : ContactDataInterface {
|
||||||
|
|
|
@ -33,7 +33,7 @@ import android.util.Patterns
|
||||||
import androidx.core.app.Person
|
import androidx.core.app.Person
|
||||||
import androidx.core.graphics.drawable.IconCompat
|
import androidx.core.graphics.drawable.IconCompat
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import java.lang.NumberFormatException
|
import java.io.IOException
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||||
import org.linphone.LinphoneApplication.Companion.corePreferences
|
import org.linphone.LinphoneApplication.Companion.corePreferences
|
||||||
|
@ -353,36 +353,41 @@ fun Friend.hasPresence(): Boolean {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Friend.getPictureUri(): Uri? {
|
fun Friend.getThumbnailUri(): Uri? {
|
||||||
val refKey = refKey
|
return getPictureUri(true)
|
||||||
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? {
|
fun Friend.getPictureUri(thumbnailPreferred: Boolean = false): Uri? {
|
||||||
val refKey = refKey
|
val refKey = refKey
|
||||||
if (refKey != null) {
|
if (refKey != null) {
|
||||||
try {
|
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(
|
return Uri.withAppendedPath(
|
||||||
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, nativeId),
|
lookupUri,
|
||||||
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY
|
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY
|
||||||
)
|
)
|
||||||
} catch (nfe: NumberFormatException) {}
|
} catch (e: Exception) { }
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
val photoUri = photo ?: return null
|
|
||||||
return Uri.parse(photoUri)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Friend.getPerson(): Person {
|
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.activities.voip.views.HorizontalScrollDotsView
|
||||||
import org.linphone.contact.ContactAvatarGenerator
|
import org.linphone.contact.ContactAvatarGenerator
|
||||||
import org.linphone.contact.ContactDataInterface
|
import org.linphone.contact.ContactDataInterface
|
||||||
|
import org.linphone.contact.getPictureUri
|
||||||
import org.linphone.core.tools.Log
|
import org.linphone.core.tools.Log
|
||||||
import org.linphone.views.VoiceRecordProgressBar
|
import org.linphone.views.VoiceRecordProgressBar
|
||||||
|
|
||||||
|
@ -344,7 +345,7 @@ private fun loadContactPictureWithCoil(
|
||||||
if (contact != null) {
|
if (contact != null) {
|
||||||
val context = imageView.context
|
val context = imageView.context
|
||||||
val displayName = contact.contact.value?.name ?: contact.displayName.value.orEmpty()
|
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) {
|
imageView.load(source) {
|
||||||
transformations(CircleCropTransformation())
|
transformations(CircleCropTransformation())
|
||||||
error(
|
error(
|
||||||
|
@ -390,7 +391,7 @@ fun loadContactPictureWithCoil(imageView: ImageView, contact: ContactDataInterfa
|
||||||
@BindingAdapter("coilContactBig")
|
@BindingAdapter("coilContactBig")
|
||||||
fun loadBigContactPictureWithCoil(imageView: ImageView, contact: ContactDataInterface?) {
|
fun loadBigContactPictureWithCoil(imageView: ImageView, contact: ContactDataInterface?) {
|
||||||
loadContactPictureWithCoil(
|
loadContactPictureWithCoil(
|
||||||
imageView, contact, true,
|
imageView, contact, false,
|
||||||
R.dimen.contact_avatar_big_size, R.dimen.contact_avatar_text_big_size
|
R.dimen.contact_avatar_big_size, R.dimen.contact_avatar_text_big_size
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue