2021-06-01 05:26:57 +02:00
|
|
|
package org.thoughtcrime.securesms.conversation.v2.messages
|
|
|
|
|
|
|
|
import android.content.Context
|
2021-06-28 01:56:49 +02:00
|
|
|
import android.graphics.Color
|
2021-06-25 06:43:22 +02:00
|
|
|
import android.graphics.Rect
|
2021-06-07 07:48:22 +02:00
|
|
|
import android.graphics.drawable.Drawable
|
2021-08-12 07:36:08 +02:00
|
|
|
import android.text.Spannable
|
2021-06-29 03:49:45 +02:00
|
|
|
import android.text.style.BackgroundColorSpan
|
|
|
|
import android.text.style.ForegroundColorSpan
|
2021-06-29 08:05:40 +02:00
|
|
|
import android.text.style.URLSpan
|
2021-06-23 06:08:17 +02:00
|
|
|
import android.text.util.Linkify
|
2021-06-01 05:26:57 +02:00
|
|
|
import android.util.AttributeSet
|
2021-06-01 07:43:37 +02:00
|
|
|
import android.util.TypedValue
|
2021-06-01 05:26:57 +02:00
|
|
|
import android.view.LayoutInflater
|
2021-06-30 06:29:32 +02:00
|
|
|
import android.view.MotionEvent
|
2021-06-01 05:26:57 +02:00
|
|
|
import android.widget.LinearLayout
|
2021-06-01 06:56:58 +02:00
|
|
|
import android.widget.TextView
|
2021-06-21 05:58:01 +02:00
|
|
|
import androidx.annotation.ColorInt
|
2021-06-07 07:48:22 +02:00
|
|
|
import androidx.annotation.DrawableRes
|
2021-06-29 08:05:40 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2021-06-01 05:26:57 +02:00
|
|
|
import androidx.core.content.res.ResourcesCompat
|
|
|
|
import androidx.core.graphics.BlendModeColorFilterCompat
|
|
|
|
import androidx.core.graphics.BlendModeCompat
|
2021-06-29 08:05:40 +02:00
|
|
|
import androidx.core.text.getSpans
|
2021-06-24 07:17:12 +02:00
|
|
|
import androidx.core.text.toSpannable
|
2021-06-01 05:26:57 +02:00
|
|
|
import kotlinx.android.synthetic.main.view_visible_message_content.view.*
|
|
|
|
import network.loki.messenger.R
|
2021-08-03 08:43:17 +02:00
|
|
|
import okhttp3.HttpUrl
|
2021-06-01 05:26:57 +02:00
|
|
|
import org.session.libsession.utilities.ThemeUtil
|
2021-06-21 02:53:52 +02:00
|
|
|
import org.session.libsession.utilities.ViewUtil
|
2021-06-23 05:39:24 +02:00
|
|
|
import org.session.libsession.utilities.recipients.Recipient
|
2021-06-24 06:13:36 +02:00
|
|
|
import org.thoughtcrime.securesms.components.emoji.EmojiTextView
|
2021-07-08 02:24:10 +02:00
|
|
|
import org.thoughtcrime.securesms.conversation.v2.ConversationActivityV2
|
2021-09-14 02:27:34 +02:00
|
|
|
import org.thoughtcrime.securesms.conversation.v2.ModalUrlBottomSheet
|
2021-08-12 07:36:08 +02:00
|
|
|
import org.thoughtcrime.securesms.conversation.v2.components.AlbumThumbnailView
|
2021-07-09 05:18:48 +02:00
|
|
|
import org.thoughtcrime.securesms.conversation.v2.utilities.MentionUtilities
|
2021-06-29 08:05:40 +02:00
|
|
|
import org.thoughtcrime.securesms.conversation.v2.utilities.ModalURLSpan
|
2021-06-30 06:29:32 +02:00
|
|
|
import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities.getIntersectedModalSpans
|
2021-06-01 05:26:57 +02:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord
|
2021-06-01 06:56:58 +02:00
|
|
|
import org.thoughtcrime.securesms.database.model.MmsMessageRecord
|
2021-06-21 07:26:09 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests
|
2021-06-29 03:49:45 +02:00
|
|
|
import org.thoughtcrime.securesms.util.SearchUtil
|
|
|
|
import org.thoughtcrime.securesms.util.SearchUtil.StyleFactory
|
2021-07-09 05:18:48 +02:00
|
|
|
import org.thoughtcrime.securesms.util.UiModeUtilities
|
2021-08-12 07:36:08 +02:00
|
|
|
import org.thoughtcrime.securesms.util.getColorWithID
|
|
|
|
import org.thoughtcrime.securesms.util.toPx
|
2021-06-29 03:49:45 +02:00
|
|
|
import java.util.*
|
2021-06-23 03:32:05 +02:00
|
|
|
import kotlin.math.roundToInt
|
2021-06-01 05:26:57 +02:00
|
|
|
|
|
|
|
class VisibleMessageContentView : LinearLayout {
|
2021-06-30 06:29:32 +02:00
|
|
|
var onContentClick: ((event: MotionEvent) -> Unit)? = null
|
2021-06-28 07:41:23 +02:00
|
|
|
var onContentDoubleTap: (() -> Unit)? = null
|
2021-06-30 02:30:10 +02:00
|
|
|
var delegate: VisibleMessageContentViewDelegate? = null
|
2021-07-14 01:37:18 +02:00
|
|
|
var indexInAdapter: Int = -1
|
2021-06-01 05:26:57 +02:00
|
|
|
|
|
|
|
// region Lifecycle
|
2021-06-18 07:54:24 +02:00
|
|
|
constructor(context: Context) : super(context) { initialize() }
|
|
|
|
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { initialize() }
|
|
|
|
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { initialize() }
|
2021-06-01 05:26:57 +02:00
|
|
|
|
2021-06-18 07:54:24 +02:00
|
|
|
private fun initialize() {
|
2021-06-01 05:26:57 +02:00
|
|
|
LayoutInflater.from(context).inflate(R.layout.view_visible_message_content, this)
|
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Updating
|
2021-06-23 05:39:24 +02:00
|
|
|
fun bind(message: MessageRecord, isStartOfMessageCluster: Boolean, isEndOfMessageCluster: Boolean,
|
2021-07-09 07:13:43 +02:00
|
|
|
glide: GlideRequests, maxWidth: Int, thread: Recipient, searchQuery: String?, contactIsTrusted: Boolean) {
|
2021-06-01 05:26:57 +02:00
|
|
|
// Background
|
2021-06-07 07:48:22 +02:00
|
|
|
val background = getBackground(message.isOutgoing, isStartOfMessageCluster, isEndOfMessageCluster)
|
2021-06-01 05:26:57 +02:00
|
|
|
val colorID = if (message.isOutgoing) R.attr.message_sent_background_color else R.attr.message_received_background_color
|
|
|
|
val color = ThemeUtil.getThemedColor(context, colorID)
|
|
|
|
val filter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(color, BlendModeCompat.SRC_IN)
|
|
|
|
background.colorFilter = filter
|
|
|
|
setBackground(background)
|
|
|
|
// Body
|
2021-06-04 01:58:04 +02:00
|
|
|
mainContainer.removeAllViews()
|
2021-06-21 06:48:27 +02:00
|
|
|
onContentClick = null
|
2021-06-28 07:41:23 +02:00
|
|
|
onContentDoubleTap = null
|
2021-08-12 03:43:33 +02:00
|
|
|
if (message.isDeleted) {
|
2021-08-11 08:35:48 +02:00
|
|
|
val deletedMessageView = DeletedMessageView(context)
|
|
|
|
deletedMessageView.bind(message, VisibleMessageContentView.getTextColor(context,message))
|
|
|
|
mainContainer.addView(deletedMessageView)
|
|
|
|
} else if (message is MmsMessageRecord && message.linkPreviews.isNotEmpty()) {
|
2021-06-01 06:56:58 +02:00
|
|
|
val linkPreviewView = LinkPreviewView(context)
|
2021-06-29 03:49:45 +02:00
|
|
|
linkPreviewView.bind(message, glide, isStartOfMessageCluster, isEndOfMessageCluster, searchQuery)
|
2021-06-01 06:56:58 +02:00
|
|
|
mainContainer.addView(linkPreviewView)
|
2021-06-30 06:29:32 +02:00
|
|
|
onContentClick = { event -> linkPreviewView.calculateHit(event) }
|
2021-06-22 01:34:23 +02:00
|
|
|
// Body text view is inside the link preview for layout convenience
|
2021-06-01 06:56:58 +02:00
|
|
|
} else if (message is MmsMessageRecord && message.quote != null) {
|
2021-06-21 02:53:52 +02:00
|
|
|
val quote = message.quote!!
|
|
|
|
val quoteView = QuoteView(context, QuoteView.Mode.Regular)
|
2021-07-07 02:55:07 +02:00
|
|
|
// The max content width is the max message bubble size - 2 times the horizontal padding - 2
|
|
|
|
// times the horizontal margin. This unfortunately has to be calculated manually
|
2021-06-23 05:57:13 +02:00
|
|
|
// here to get the layout right.
|
2021-07-07 02:55:07 +02:00
|
|
|
val maxContentWidth = (maxWidth - 2 * resources.getDimension(R.dimen.medium_spacing) - 2 * toPx(16, resources)).roundToInt()
|
2021-07-05 01:59:54 +02:00
|
|
|
val quoteText = if (quote.isOriginalMissing) {
|
|
|
|
context.getString(R.string.QuoteView_original_missing)
|
|
|
|
} else {
|
|
|
|
quote.text
|
|
|
|
}
|
|
|
|
quoteView.bind(quote.author.toString(), quoteText, quote.attachment, thread,
|
2021-07-05 08:52:56 +02:00
|
|
|
message.isOutgoing, maxContentWidth, message.isOpenGroupInvitation, message.threadId,
|
|
|
|
quote.isOriginalMissing, glide)
|
2021-06-01 06:56:58 +02:00
|
|
|
mainContainer.addView(quoteView)
|
2021-06-29 03:49:45 +02:00
|
|
|
val bodyTextView = VisibleMessageContentView.getBodyTextView(context, message, searchQuery)
|
2021-06-21 02:53:52 +02:00
|
|
|
ViewUtil.setPaddingTop(bodyTextView, 0)
|
|
|
|
mainContainer.addView(bodyTextView)
|
2021-06-30 06:51:24 +02:00
|
|
|
onContentClick = { event ->
|
2021-06-30 02:30:10 +02:00
|
|
|
val r = Rect()
|
|
|
|
quoteView.getGlobalVisibleRect(r)
|
2021-06-30 06:51:24 +02:00
|
|
|
if (r.contains(event.rawX.roundToInt(), event.rawY.roundToInt())) {
|
2021-06-30 02:30:10 +02:00
|
|
|
delegate?.scrollToMessageIfPossible(quote.id)
|
2021-08-12 07:36:08 +02:00
|
|
|
} else {
|
|
|
|
bodyTextView.getIntersectedModalSpans(event).forEach { span ->
|
|
|
|
span.onClick(bodyTextView)
|
|
|
|
}
|
2021-06-30 02:30:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-01 06:56:58 +02:00
|
|
|
} else if (message is MmsMessageRecord && message.slideDeck.audioSlide != null) {
|
2021-07-09 07:13:43 +02:00
|
|
|
// Audio attachment
|
|
|
|
if (contactIsTrusted || message.isOutgoing) {
|
|
|
|
val voiceMessageView = VoiceMessageView(context)
|
2021-07-14 01:37:18 +02:00
|
|
|
voiceMessageView.indexInAdapter = indexInAdapter
|
2021-07-12 02:51:01 +02:00
|
|
|
voiceMessageView.delegate = context as? ConversationActivityV2
|
|
|
|
voiceMessageView.bind(message, isStartOfMessageCluster, isEndOfMessageCluster)
|
2021-07-09 07:13:43 +02:00
|
|
|
mainContainer.addView(voiceMessageView)
|
|
|
|
// We have to use onContentClick (rather than a click listener directly on the voice
|
|
|
|
// message view) so as to not interfere with all the other gestures.
|
|
|
|
onContentClick = { voiceMessageView.togglePlayback() }
|
|
|
|
onContentDoubleTap = { voiceMessageView.handleDoubleTap() }
|
|
|
|
} else {
|
|
|
|
val untrustedView = UntrustedAttachmentView(context)
|
|
|
|
untrustedView.bind(UntrustedAttachmentView.AttachmentType.AUDIO, VisibleMessageContentView.getTextColor(context,message))
|
|
|
|
mainContainer.addView(untrustedView)
|
|
|
|
onContentClick = { untrustedView.showTrustDialog(message.individualRecipient) }
|
|
|
|
}
|
2021-06-01 06:56:58 +02:00
|
|
|
} else if (message is MmsMessageRecord && message.slideDeck.documentSlide != null) {
|
2021-07-09 07:13:43 +02:00
|
|
|
// Document attachment
|
|
|
|
if (contactIsTrusted || message.isOutgoing) {
|
|
|
|
val documentView = DocumentView(context)
|
|
|
|
documentView.bind(message, VisibleMessageContentView.getTextColor(context, message))
|
|
|
|
mainContainer.addView(documentView)
|
|
|
|
} else {
|
|
|
|
val untrustedView = UntrustedAttachmentView(context)
|
|
|
|
untrustedView.bind(UntrustedAttachmentView.AttachmentType.DOCUMENT, VisibleMessageContentView.getTextColor(context,message))
|
|
|
|
mainContainer.addView(untrustedView)
|
|
|
|
onContentClick = { untrustedView.showTrustDialog(message.individualRecipient) }
|
|
|
|
}
|
2021-06-01 06:56:58 +02:00
|
|
|
} else if (message is MmsMessageRecord && message.slideDeck.asAttachments().isNotEmpty()) {
|
2021-07-09 07:13:43 +02:00
|
|
|
// Images/Video attachment
|
|
|
|
if (contactIsTrusted || message.isOutgoing) {
|
|
|
|
val albumThumbnailView = AlbumThumbnailView(context)
|
|
|
|
mainContainer.addView(albumThumbnailView)
|
|
|
|
// isStart and isEnd of cluster needed for calculating the mask for full bubble image groups
|
|
|
|
// bind after add view because views are inflated and calculated during bind
|
|
|
|
albumThumbnailView.bind(
|
|
|
|
glideRequests = glide,
|
|
|
|
message = message,
|
|
|
|
isStart = isStartOfMessageCluster,
|
|
|
|
isEnd = isEndOfMessageCluster
|
|
|
|
)
|
|
|
|
onContentClick = { event ->
|
|
|
|
albumThumbnailView.calculateHitObject(event, message, thread)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
val untrustedView = UntrustedAttachmentView(context)
|
|
|
|
untrustedView.bind(UntrustedAttachmentView.AttachmentType.MEDIA, VisibleMessageContentView.getTextColor(context,message))
|
|
|
|
mainContainer.addView(untrustedView)
|
|
|
|
onContentClick = { untrustedView.showTrustDialog(message.individualRecipient) }
|
2021-06-30 06:29:32 +02:00
|
|
|
}
|
2021-06-22 02:39:34 +02:00
|
|
|
} else if (message.isOpenGroupInvitation) {
|
|
|
|
val openGroupInvitationView = OpenGroupInvitationView(context)
|
|
|
|
openGroupInvitationView.bind(message, VisibleMessageContentView.getTextColor(context, message))
|
|
|
|
mainContainer.addView(openGroupInvitationView)
|
2021-06-29 06:23:36 +02:00
|
|
|
onContentClick = { openGroupInvitationView.joinOpenGroup() }
|
2021-06-01 06:56:58 +02:00
|
|
|
} else {
|
2021-06-29 08:17:19 +02:00
|
|
|
val bodyTextView = VisibleMessageContentView.getBodyTextView(context, message, searchQuery)
|
2021-06-01 06:56:58 +02:00
|
|
|
mainContainer.addView(bodyTextView)
|
2021-06-30 06:29:32 +02:00
|
|
|
onContentClick = { event ->
|
|
|
|
// intersectedModalSpans should only be a list of one item
|
|
|
|
bodyTextView.getIntersectedModalSpans(event).forEach { span ->
|
|
|
|
span.onClick(bodyTextView)
|
|
|
|
}
|
|
|
|
}
|
2021-06-01 06:56:58 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-04 01:58:04 +02:00
|
|
|
|
2021-06-07 07:48:22 +02:00
|
|
|
private fun getBackground(isOutgoing: Boolean, isStartOfMessageCluster: Boolean, isEndOfMessageCluster: Boolean): Drawable {
|
|
|
|
val isSingleMessage = (isStartOfMessageCluster && isEndOfMessageCluster)
|
|
|
|
@DrawableRes val backgroundID: Int
|
|
|
|
if (isSingleMessage) {
|
|
|
|
backgroundID = if (isOutgoing) R.drawable.message_bubble_background_sent_alone else R.drawable.message_bubble_background_received_alone
|
|
|
|
} else if (isStartOfMessageCluster) {
|
|
|
|
backgroundID = if (isOutgoing) R.drawable.message_bubble_background_sent_start else R.drawable.message_bubble_background_received_start
|
|
|
|
} else if (isEndOfMessageCluster) {
|
|
|
|
backgroundID = if (isOutgoing) R.drawable.message_bubble_background_sent_end else R.drawable.message_bubble_background_received_end
|
|
|
|
} else {
|
2021-06-07 08:06:37 +02:00
|
|
|
backgroundID = if (isOutgoing) R.drawable.message_bubble_background_sent_middle else R.drawable.message_bubble_background_received_middle
|
2021-06-07 07:48:22 +02:00
|
|
|
}
|
|
|
|
return ResourcesCompat.getDrawable(resources, backgroundID, context.theme)!!
|
|
|
|
}
|
|
|
|
|
2021-06-04 01:58:04 +02:00
|
|
|
fun recycle() {
|
|
|
|
mainContainer.removeAllViews()
|
|
|
|
}
|
2021-06-01 06:56:58 +02:00
|
|
|
// endregion
|
|
|
|
|
|
|
|
// region Convenience
|
2021-06-22 01:34:23 +02:00
|
|
|
companion object {
|
2021-06-21 05:58:01 +02:00
|
|
|
|
2021-06-29 03:49:45 +02:00
|
|
|
fun getBodyTextView(context: Context, message: MessageRecord, searchQuery: String?): TextView {
|
2021-06-24 06:13:36 +02:00
|
|
|
val result = EmojiTextView(context)
|
2021-06-22 01:34:23 +02:00
|
|
|
val vPadding = context.resources.getDimension(R.dimen.small_spacing).toInt()
|
|
|
|
val hPadding = toPx(12, context.resources)
|
|
|
|
result.setPadding(hPadding, vPadding, hPadding, vPadding)
|
|
|
|
result.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.resources.getDimension(R.dimen.small_font_size))
|
|
|
|
val color = getTextColor(context, message)
|
|
|
|
result.setTextColor(color)
|
2021-06-23 06:08:17 +02:00
|
|
|
result.setLinkTextColor(color)
|
2021-08-12 07:36:08 +02:00
|
|
|
val body = getBodySpans(context, message, searchQuery)
|
|
|
|
result.text = body
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getBodySpans(context: Context, message: MessageRecord, searchQuery: String?): Spannable {
|
2021-06-24 07:17:12 +02:00
|
|
|
var body = message.body.toSpannable()
|
2021-08-12 07:36:08 +02:00
|
|
|
|
|
|
|
body = MentionUtilities.highlightMentions(body, message.isOutgoing, message.threadId, context)
|
|
|
|
body = SearchUtil.getHighlightedSpan(Locale.getDefault(), StyleFactory { BackgroundColorSpan(Color.WHITE) }, body, searchQuery)
|
|
|
|
body = SearchUtil.getHighlightedSpan(Locale.getDefault(), StyleFactory { ForegroundColorSpan(Color.BLACK) }, body, searchQuery)
|
|
|
|
|
2021-06-24 07:17:12 +02:00
|
|
|
Linkify.addLinks(body, Linkify.WEB_URLS)
|
2021-06-29 08:05:40 +02:00
|
|
|
|
|
|
|
// replace URLSpans with ModalURLSpans
|
|
|
|
body.getSpans<URLSpan>(0, body.length).toList().forEach { urlSpan ->
|
2021-08-03 08:43:17 +02:00
|
|
|
val updatedUrl = urlSpan.url.let { HttpUrl.parse(it).toString() }
|
2021-08-03 06:21:05 +02:00
|
|
|
val replacementSpan = ModalURLSpan(updatedUrl) { url ->
|
2021-06-29 08:05:40 +02:00
|
|
|
val activity = context as AppCompatActivity
|
2021-09-14 02:27:34 +02:00
|
|
|
ModalUrlBottomSheet(url).show(activity.supportFragmentManager, "Open URL Dialog")
|
2021-06-29 08:05:40 +02:00
|
|
|
}
|
|
|
|
val start = body.getSpanStart(urlSpan)
|
|
|
|
val end = body.getSpanEnd(urlSpan)
|
|
|
|
val flags = body.getSpanFlags(urlSpan)
|
|
|
|
body.removeSpan(urlSpan)
|
|
|
|
body.setSpan(replacementSpan, start, end, flags)
|
|
|
|
}
|
2021-08-12 07:36:08 +02:00
|
|
|
return body
|
2021-06-22 01:34:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@ColorInt
|
|
|
|
fun getTextColor(context: Context, message: MessageRecord): Int {
|
2021-06-28 01:56:49 +02:00
|
|
|
val isDayUiMode = UiModeUtilities.isDayUiMode(context)
|
2021-06-22 01:34:23 +02:00
|
|
|
val colorID = if (message.isOutgoing) {
|
2021-06-28 01:56:49 +02:00
|
|
|
if (isDayUiMode) R.color.white else R.color.black
|
2021-06-22 01:34:23 +02:00
|
|
|
} else {
|
2021-06-28 01:56:49 +02:00
|
|
|
if (isDayUiMode) R.color.black else R.color.white
|
2021-06-22 01:34:23 +02:00
|
|
|
}
|
|
|
|
return context.resources.getColorWithID(colorID, context.theme)
|
2021-06-02 05:03:22 +02:00
|
|
|
}
|
2021-06-01 05:26:57 +02:00
|
|
|
}
|
|
|
|
// endregion
|
2021-06-30 02:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface VisibleMessageContentViewDelegate {
|
|
|
|
|
|
|
|
fun scrollToMessageIfPossible(timestamp: Long)
|
2021-06-01 05:26:57 +02:00
|
|
|
}
|