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
|
2020-09-09 05:14:46 +02:00
|
|
|
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
|
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
|
2020-09-07 08:13:34 +02:00
|
|
|
import kotlinx.android.synthetic.main.view_conversation.view.*
|
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
|
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
|
2020-09-09 05:14:46 +02:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2020-09-03 14:15:03 +02:00
|
|
|
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
|
2020-09-07 08:13:34 +02:00
|
|
|
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
|
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
|
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
|
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) {
|
2020-09-09 05:14:46 +02:00
|
|
|
fun getUserDisplayName(publicKey: String?): String? {
|
|
|
|
if (publicKey == null || publicKey.isBlank()) {
|
|
|
|
return null
|
|
|
|
} else {
|
|
|
|
return DatabaseFactory.getLokiUserDatabase(context).getDisplayName(publicKey!!)
|
|
|
|
}
|
|
|
|
}
|
2020-09-07 08:13:34 +02:00
|
|
|
if (recipient.isGroupRecipient) {
|
|
|
|
if ("Session Public Chat" == recipient.name) {
|
|
|
|
publicKey = ""
|
2020-09-09 05:14:46 +02:00
|
|
|
displayName = ""
|
2020-09-07 08:13:34 +02:00
|
|
|
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) ?: ""
|
2020-09-09 05:14:46 +02:00
|
|
|
displayName = getUserDisplayName(randomUsers.getOrNull(0) ?: "")
|
2020-09-07 08:13:34 +02:00
|
|
|
additionalPublicKey = randomUsers.getOrNull(1) ?: ""
|
2020-09-09 05:14:46 +02:00
|
|
|
additionalDisplayName = getUserDisplayName(randomUsers.getOrNull(1) ?: "")
|
2020-09-07 08:13:34 +02:00
|
|
|
isRSSFeed = recipient.name == "Loki News" || recipient.name == "Session Updates"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
publicKey = recipient.address.toString()
|
2020-09-09 05:14:46 +02:00
|
|
|
displayName = recipient.name
|
2020-09-07 08:13:34 +02:00
|
|
|
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
|
2020-09-03 14:15:03 +02:00
|
|
|
|
|
|
|
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 {
|
2020-09-03 14:15:03 +02:00
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
}
|