2020-05-11 08:19:26 +02:00
|
|
|
package org.thoughtcrime.securesms.loki.views
|
2019-12-17 16:24:42 +01:00
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.util.AttributeSet
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
2019-12-19 11:15:58 +01:00
|
|
|
import android.widget.ImageView
|
2019-12-17 16:24:42 +01:00
|
|
|
import android.widget.RelativeLayout
|
2020-09-03 14:15:03 +02:00
|
|
|
import androidx.annotation.DimenRes
|
2019-12-19 11:15:58 +01:00
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
2019-12-18 11:36:09 +01:00
|
|
|
import kotlinx.android.synthetic.main.view_profile_picture.view.*
|
2019-12-17 16:24:42 +01:00
|
|
|
import network.loki.messenger.R
|
2021-05-18 08:03:47 +02:00
|
|
|
import org.session.libsession.avatars.ProfileContactPhoto
|
2021-05-24 02:27:31 +02:00
|
|
|
import org.session.libsession.messaging.contacts.Contact
|
2021-04-26 05:12:05 +02:00
|
|
|
import org.session.libsession.messaging.mentions.MentionsManager
|
2021-05-18 08:11:38 +02:00
|
|
|
import org.session.libsession.utilities.Address
|
2021-05-18 08:03:47 +02:00
|
|
|
import org.session.libsession.utilities.recipients.Recipient
|
2021-01-19 00:48:02 +01:00
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
2021-03-03 01:33:35 +01:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
|
|
|
import org.thoughtcrime.securesms.loki.utilities.AvatarPlaceholderGenerator
|
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests
|
2019-12-17 16:24:42 +01:00
|
|
|
|
|
|
|
class ProfilePictureView : RelativeLayout {
|
2019-12-19 11:15:58 +01:00
|
|
|
lateinit var glide: GlideRequests
|
2020-07-15 06:26:20 +02:00
|
|
|
var publicKey: String? = null
|
2020-09-03 14:15:03 +02:00
|
|
|
var displayName: String? = null
|
2020-07-15 06:26:20 +02:00
|
|
|
var additionalPublicKey: String? = null
|
2020-09-03 14:15:03 +02:00
|
|
|
var additionalDisplayName: String? = null
|
2020-01-06 04:26:52 +01:00
|
|
|
var isLarge = false
|
2021-05-28 08:09:15 +02:00
|
|
|
|
|
|
|
private val profilePicturesCache = mutableMapOf<String, String?>()
|
2019-12-17 16:24:42 +01:00
|
|
|
|
|
|
|
// region Lifecycle
|
|
|
|
constructor(context: Context) : super(context) {
|
|
|
|
setUpViewHierarchy()
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
|
|
|
|
setUpViewHierarchy()
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
|
|
|
|
setUpViewHierarchy()
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
|
|
|
|
setUpViewHierarchy()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun setUpViewHierarchy() {
|
2020-08-25 15:52:42 +02:00
|
|
|
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
|
2019-12-18 11:36:09 +01:00
|
|
|
val contentView = inflater.inflate(R.layout.view_profile_picture, null)
|
2019-12-17 16:24:42 +01:00
|
|
|
addView(contentView)
|
|
|
|
}
|
2019-12-18 11:36:09 +01:00
|
|
|
// endregion
|
2019-12-17 16:24:42 +01:00
|
|
|
|
2019-12-18 11:36:09 +01:00
|
|
|
// region Updating
|
2020-09-07 08:13:34 +02:00
|
|
|
fun update(recipient: Recipient, threadID: Long) {
|
2021-05-24 02:27:31 +02:00
|
|
|
fun getUserDisplayName(publicKey: String): String {
|
|
|
|
val contact = DatabaseFactory.getSessionContactDatabase(context).getContactWithSessionID(publicKey)
|
|
|
|
return contact?.displayName(Contact.ContactContext.REGULAR) ?: publicKey
|
2020-09-09 05:14:46 +02:00
|
|
|
}
|
2020-10-28 05:04:47 +01:00
|
|
|
fun isOpenGroupWithProfilePicture(recipient: Recipient): Boolean {
|
2020-10-01 13:15:22 +02:00
|
|
|
return recipient.isOpenGroupRecipient && recipient.groupAvatarId != null
|
2020-09-25 13:11:55 +02:00
|
|
|
}
|
2020-10-28 05:04:47 +01:00
|
|
|
if (recipient.isGroupRecipient && !isOpenGroupWithProfilePicture(recipient)) {
|
2021-05-24 02:27:31 +02:00
|
|
|
val users = MentionsManager.userPublicKeyCache[threadID]?.toMutableList() ?: mutableListOf()
|
2020-09-28 11:56:40 +02:00
|
|
|
users.remove(TextSecurePreferences.getLocalNumber(context))
|
|
|
|
val randomUsers = users.sorted().toMutableList() // Sort to provide a level of stability
|
|
|
|
if (users.count() == 1) {
|
2021-01-19 00:48:02 +01:00
|
|
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context)!!
|
2020-09-28 11:56:40 +02:00
|
|
|
randomUsers.add(0, userPublicKey) // Ensure the current user is at the back visually
|
2020-09-07 08:13:34 +02:00
|
|
|
}
|
2020-09-28 11:56:40 +02:00
|
|
|
val pk = randomUsers.getOrNull(0) ?: ""
|
|
|
|
publicKey = pk
|
|
|
|
displayName = getUserDisplayName(pk)
|
|
|
|
val apk = randomUsers.getOrNull(1) ?: ""
|
|
|
|
additionalPublicKey = apk
|
|
|
|
additionalDisplayName = getUserDisplayName(apk)
|
2020-09-07 08:13:34 +02:00
|
|
|
} else {
|
2021-05-24 02:27:31 +02:00
|
|
|
val publicKey = recipient.address.toString()
|
|
|
|
this.publicKey = publicKey
|
2020-09-17 03:39:34 +02:00
|
|
|
displayName = getUserDisplayName(publicKey)
|
2020-09-07 08:13:34 +02:00
|
|
|
additionalPublicKey = null
|
|
|
|
}
|
|
|
|
update()
|
|
|
|
}
|
|
|
|
|
2019-12-17 16:24:42 +01:00
|
|
|
fun update() {
|
2020-07-15 06:26:20 +02:00
|
|
|
val publicKey = publicKey ?: return
|
|
|
|
val additionalPublicKey = additionalPublicKey
|
2021-05-28 08:09:15 +02:00
|
|
|
if (additionalPublicKey != null) {
|
|
|
|
setProfilePictureIfNeeded(doubleModeImageView1, publicKey, displayName, R.dimen.small_profile_picture_size)
|
|
|
|
setProfilePictureIfNeeded(doubleModeImageView2, additionalPublicKey, additionalDisplayName, R.dimen.small_profile_picture_size)
|
|
|
|
doubleModeImageViewContainer.visibility = View.VISIBLE
|
2021-02-04 06:57:24 +01:00
|
|
|
} else {
|
|
|
|
glide.clear(doubleModeImageView1)
|
|
|
|
glide.clear(doubleModeImageView2)
|
2021-05-28 08:09:15 +02:00
|
|
|
doubleModeImageViewContainer.visibility = View.INVISIBLE
|
2021-02-04 06:57:24 +01:00
|
|
|
}
|
2021-05-28 08:09:15 +02:00
|
|
|
if (additionalPublicKey == null && !isLarge) {
|
|
|
|
setProfilePictureIfNeeded(singleModeImageView, publicKey, displayName, R.dimen.medium_profile_picture_size)
|
|
|
|
singleModeImageView.visibility = View.VISIBLE
|
2021-02-04 06:57:24 +01:00
|
|
|
} else {
|
|
|
|
glide.clear(singleModeImageView)
|
2021-05-28 08:09:15 +02:00
|
|
|
singleModeImageView.visibility = View.INVISIBLE
|
2021-02-04 06:57:24 +01:00
|
|
|
}
|
2021-05-28 08:09:15 +02:00
|
|
|
if (additionalPublicKey == null && isLarge) {
|
|
|
|
setProfilePictureIfNeeded(largeSingleModeImageView, publicKey, displayName, R.dimen.large_profile_picture_size)
|
|
|
|
largeSingleModeImageView.visibility = View.VISIBLE
|
2021-02-04 06:57:24 +01:00
|
|
|
} else {
|
|
|
|
glide.clear(largeSingleModeImageView)
|
2021-05-28 08:09:15 +02:00
|
|
|
largeSingleModeImageView.visibility = View.INVISIBLE
|
2021-02-04 06:57:24 +01:00
|
|
|
}
|
2020-09-03 14:15:03 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 05:57:22 +02:00
|
|
|
private fun setProfilePictureIfNeeded(imageView: ImageView, publicKey: String, displayName: String?, @DimenRes sizeResId: Int) {
|
|
|
|
if (publicKey.isNotEmpty()) {
|
2021-02-04 06:57:24 +01:00
|
|
|
val recipient = Recipient.from(context, Address.fromSerialized(publicKey), false)
|
2021-05-28 08:09:15 +02:00
|
|
|
if (profilePicturesCache.containsKey(publicKey) && profilePicturesCache[publicKey] == recipient.profileAvatar) return
|
2020-09-03 14:15:03 +02:00
|
|
|
val signalProfilePicture = recipient.contactPhoto
|
2021-05-21 05:24:49 +02:00
|
|
|
val avatar = (signalProfilePicture as? ProfileContactPhoto)?.avatarObject
|
|
|
|
if (signalProfilePicture != null && avatar != "0" && avatar != "") {
|
2021-02-04 06:57:24 +01:00
|
|
|
glide.clear(imageView)
|
2021-03-03 01:33:35 +01:00
|
|
|
glide.load(signalProfilePicture).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).circleCrop().into(imageView)
|
2021-05-28 08:09:15 +02:00
|
|
|
profilePicturesCache[publicKey] = recipient.profileAvatar
|
2019-12-19 11:15:58 +01:00
|
|
|
} else {
|
2020-09-09 05:57:22 +02:00
|
|
|
val sizeInPX = resources.getDimensionPixelSize(sizeResId)
|
2021-02-04 06:57:24 +01:00
|
|
|
glide.clear(imageView)
|
2021-05-28 08:09:15 +02:00
|
|
|
glide.load(AvatarPlaceholderGenerator.generate(context, sizeInPX, publicKey, displayName))
|
|
|
|
.diskCacheStrategy(DiskCacheStrategy.ALL).circleCrop().into(imageView)
|
|
|
|
profilePicturesCache[publicKey] = recipient.profileAvatar
|
2019-12-19 11:15:58 +01:00
|
|
|
}
|
2020-09-03 14:15:03 +02:00
|
|
|
} else {
|
|
|
|
imageView.setImageDrawable(null)
|
2019-12-19 11:15:58 +01:00
|
|
|
}
|
2019-12-17 16:24:42 +01:00
|
|
|
}
|
2021-02-04 06:57:24 +01:00
|
|
|
|
|
|
|
fun recycle() {
|
2021-05-28 08:09:15 +02:00
|
|
|
profilePicturesCache.clear()
|
2021-02-04 06:57:24 +01:00
|
|
|
}
|
2019-12-17 16:24:42 +01:00
|
|
|
// endregion
|
2021-05-21 05:36:21 +02:00
|
|
|
}
|