session-android/src/org/thoughtcrime/securesms/loki/views/ProfilePictureView.kt

151 lines
6.8 KiB
Kotlin
Raw Normal View History

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.text.TextUtils
2019-12-17 16:24:42 +01:00
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
import androidx.annotation.DimenRes
2019-12-19 11:15:58 +01:00
import com.bumptech.glide.load.engine.DiskCacheStrategy
import kotlinx.android.synthetic.main.view_conversation.view.*
import kotlinx.android.synthetic.main.view_profile_picture.view.*
2019-12-17 16:24:42 +01:00
import network.loki.messenger.R
2020-01-07 04:51:11 +01:00
import org.thoughtcrime.securesms.contacts.avatars.ProfileContactPhoto
2019-12-19 11:15:58 +01:00
import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.loki.todo.AvatarPlaceholderGenerator
2019-12-19 11:15:58 +01:00
import org.thoughtcrime.securesms.mms.GlideRequests
import org.thoughtcrime.securesms.recipients.Recipient
2020-04-01 05:16:31 +02:00
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.loki.protocol.mentions.MentionsManager
2019-12-17 16:24:42 +01:00
2020-01-06 06:05:57 +01:00
// TODO: Look into a better way of handling different sizes. Maybe an enum (with associated values) encapsulating the different modes?
2020-01-06 04:26:52 +01:00
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
var displayName: String? = null
2020-07-15 06:26:20 +02:00
var additionalPublicKey: String? = null
var additionalDisplayName: String? = null
2019-12-17 16:24:42 +01:00
var isRSSFeed = false
2020-01-06 04:26:52 +01:00
var isLarge = false
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
val contentView = inflater.inflate(R.layout.view_profile_picture, null)
2019-12-17 16:24:42 +01:00
addView(contentView)
}
// endregion
2019-12-17 16:24:42 +01:00
// region Updating
fun update(recipient: Recipient, threadID: Long) {
fun getUserDisplayName(publicKey: String?): String? {
if (publicKey == null || publicKey.isBlank()) {
return null
} else {
return DatabaseFactory.getLokiUserDatabase(context).getDisplayName(publicKey!!)
}
}
if (recipient.isGroupRecipient) {
if ("Session Public Chat" == recipient.name) {
publicKey = ""
displayName = ""
additionalPublicKey = null
isRSSFeed = true
} else {
val users = MentionsManager.shared.userPublicKeyCache[threadID]?.toMutableList() ?: mutableListOf()
users.remove(TextSecurePreferences.getLocalNumber(context))
val masterPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context)
if (masterPublicKey != null) {
users.remove(masterPublicKey)
}
val randomUsers = users.sorted() // Sort to provide a level of stability
publicKey = randomUsers.getOrNull(0) ?: ""
displayName = getUserDisplayName(randomUsers.getOrNull(0) ?: "")
additionalPublicKey = randomUsers.getOrNull(1) ?: ""
additionalDisplayName = getUserDisplayName(randomUsers.getOrNull(1) ?: "")
isRSSFeed = recipient.name == "Loki News" || recipient.name == "Session Updates"
}
} else {
publicKey = recipient.address.toString()
displayName = recipient.name
additionalPublicKey = null
isRSSFeed = false
}
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
doubleModeImageViewContainer.visibility = if (additionalPublicKey != null && !isRSSFeed) View.VISIBLE else View.INVISIBLE
singleModeImageViewContainer.visibility = if (additionalPublicKey == null && !isRSSFeed && !isLarge) View.VISIBLE else View.INVISIBLE
largeSingleModeImageViewContainer.visibility = if (additionalPublicKey == null && !isRSSFeed && isLarge) View.VISIBLE else View.INVISIBLE
2020-01-23 00:25:22 +01:00
rssImageView.visibility = if (isRSSFeed) View.VISIBLE else View.INVISIBLE
setProfilePictureIfNeeded(
doubleModeImageView1,
publicKey,
displayName,
R.dimen.small_profile_picture_size)
setProfilePictureIfNeeded(
doubleModeImageView2,
additionalPublicKey ?: "",
additionalDisplayName,
R.dimen.small_profile_picture_size)
setProfilePictureIfNeeded(
singleModeImageView,
publicKey,
displayName,
R.dimen.medium_profile_picture_size)
setProfilePictureIfNeeded(
largeSingleModeImageView,
publicKey,
displayName,
R.dimen.large_profile_picture_size)
}
private fun setProfilePictureIfNeeded(imageView: ImageView, hexEncodedPublicKey: String, displayName: String?, @DimenRes sizeResId: Int) {
glide.clear(imageView)
if (hexEncodedPublicKey.isNotEmpty()) {
val recipient = Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false);
val signalProfilePicture = recipient.contactPhoto
if (signalProfilePicture != null && (signalProfilePicture as? ProfileContactPhoto)?.avatarObject != "0" && (signalProfilePicture as? ProfileContactPhoto)?.avatarObject != "") {
glide.load(signalProfilePicture).diskCacheStrategy(DiskCacheStrategy.ALL).circleCrop().into(imageView)
2019-12-19 11:15:58 +01:00
} else {
val sizePx = resources.getDimensionPixelSize(sizeResId)
val masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context)
val hepk = if (recipient.isLocalNumber && masterHexEncodedPublicKey != null) masterHexEncodedPublicKey else hexEncodedPublicKey
// val jazzIcon = JazzIdenticonDrawable(size, size, hepk)
glide.load(AvatarPlaceholderGenerator.generate(
context,
sizePx,
hepk,
displayName
)).diskCacheStrategy(DiskCacheStrategy.ALL).circleCrop().into(imageView)
2019-12-19 11:15:58 +01:00
}
} else {
imageView.setImageDrawable(null)
2019-12-19 11:15:58 +01:00
}
2019-12-17 16:24:42 +01:00
}
// endregion
}