2021-06-01 05:26:57 +02:00
|
|
|
package org.thoughtcrime.securesms.conversation.v2.messages
|
|
|
|
|
|
|
|
import android.content.Context
|
2021-06-07 07:48:22 +02:00
|
|
|
import android.graphics.drawable.Drawable
|
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-25 04:18:52 +02:00
|
|
|
import android.util.Log
|
2021-06-01 07:43:37 +02:00
|
|
|
import android.util.TypedValue
|
2021-06-01 05:26:57 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
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-01 05:26:57 +02:00
|
|
|
import androidx.core.content.res.ResourcesCompat
|
|
|
|
import androidx.core.graphics.BlendModeColorFilterCompat
|
|
|
|
import androidx.core.graphics.BlendModeCompat
|
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
|
|
|
|
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-25 01:57:58 +02:00
|
|
|
import org.thoughtcrime.securesms.conversation.v2.AlbumThumbnailView
|
2021-06-24 06:13:36 +02:00
|
|
|
import org.thoughtcrime.securesms.components.emoji.EmojiTextView
|
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-24 07:17:12 +02:00
|
|
|
import org.thoughtcrime.securesms.loki.utilities.*
|
|
|
|
import org.thoughtcrime.securesms.loki.utilities.MentionUtilities.highlightMentions
|
2021-06-21 07:26:09 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests
|
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-21 06:48:27 +02:00
|
|
|
var onContentClick: (() -> Unit)? = null
|
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,
|
|
|
|
glide: GlideRequests, maxWidth: Int, thread: Recipient) {
|
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-01 06:56:58 +02:00
|
|
|
if (message is MmsMessageRecord && message.linkPreviews.isNotEmpty()) {
|
|
|
|
val linkPreviewView = LinkPreviewView(context)
|
2021-06-22 01:34:23 +02:00
|
|
|
linkPreviewView.bind(message, glide, background)
|
2021-06-01 06:56:58 +02:00
|
|
|
mainContainer.addView(linkPreviewView)
|
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-06-23 05:57:13 +02:00
|
|
|
// The max content width is the max message bubble size - 2 times the horizontal padding - the
|
|
|
|
// quote view content area's start margin. This unfortunately has to be calculated manually
|
|
|
|
// here to get the layout right.
|
2021-06-23 03:32:05 +02:00
|
|
|
val maxContentWidth = (maxWidth - 2 * resources.getDimension(R.dimen.medium_spacing) - toPx(16, resources)).roundToInt()
|
2021-06-23 05:39:24 +02:00
|
|
|
quoteView.bind(quote.author.toString(), quote.text, quote.attachment, thread,
|
2021-06-23 03:32:05 +02:00
|
|
|
message.isOutgoing, maxContentWidth, message.isOpenGroupInvitation)
|
2021-06-01 06:56:58 +02:00
|
|
|
mainContainer.addView(quoteView)
|
2021-06-22 01:34:23 +02:00
|
|
|
val bodyTextView = VisibleMessageContentView.getBodyTextView(context, message)
|
2021-06-21 02:53:52 +02:00
|
|
|
ViewUtil.setPaddingTop(bodyTextView, 0)
|
|
|
|
mainContainer.addView(bodyTextView)
|
2021-06-01 06:56:58 +02:00
|
|
|
} else if (message is MmsMessageRecord && message.slideDeck.audioSlide != null) {
|
|
|
|
val voiceMessageView = VoiceMessageView(context)
|
2021-06-21 05:36:45 +02:00
|
|
|
voiceMessageView.bind(message, background)
|
2021-06-01 06:56:58 +02:00
|
|
|
mainContainer.addView(voiceMessageView)
|
2021-06-23 05:57:13 +02:00
|
|
|
// 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.
|
2021-06-21 06:48:27 +02:00
|
|
|
onContentClick = { voiceMessageView.togglePlayback() }
|
2021-06-01 06:56:58 +02:00
|
|
|
} else if (message is MmsMessageRecord && message.slideDeck.documentSlide != null) {
|
|
|
|
val documentView = DocumentView(context)
|
2021-06-22 01:34:23 +02:00
|
|
|
documentView.bind(message, VisibleMessageContentView.getTextColor(context, message))
|
2021-06-01 06:56:58 +02:00
|
|
|
mainContainer.addView(documentView)
|
|
|
|
} else if (message is MmsMessageRecord && message.slideDeck.asAttachments().isNotEmpty()) {
|
2021-06-24 08:15:13 +02:00
|
|
|
val albumThumbnailView = AlbumThumbnailView(context)
|
|
|
|
mainContainer.addView(albumThumbnailView)
|
|
|
|
// isStart and isEnd of cluster needed for calculating the mask for full bubble image groups
|
2021-06-25 01:57:58 +02:00
|
|
|
// bind after add view because views are inflated and calculated during bind
|
2021-06-25 04:18:52 +02:00
|
|
|
albumThumbnailView.bind(
|
|
|
|
glideRequests = glide,
|
|
|
|
message = message,
|
|
|
|
isStart = isStartOfMessageCluster,
|
|
|
|
isEnd = isEndOfMessageCluster,
|
|
|
|
clickListener = { slide ->
|
|
|
|
Log.d("Loki-UI","clicked to display the slide $slide")
|
|
|
|
},
|
|
|
|
downloadClickListener = { slide ->
|
|
|
|
// trigger download of content?
|
|
|
|
Log.d("Loki-UI","clicked to download the slide $slide")
|
|
|
|
},
|
|
|
|
readMoreListener = {
|
|
|
|
Log.d("Loki-UI", "clicked to read more the message $message")
|
|
|
|
}
|
|
|
|
)
|
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-01 06:56:58 +02:00
|
|
|
} else {
|
2021-06-22 01:34:23 +02:00
|
|
|
val bodyTextView = VisibleMessageContentView.getBodyTextView(context, message)
|
2021-06-01 06:56:58 +02:00
|
|
|
mainContainer.addView(bodyTextView)
|
|
|
|
}
|
|
|
|
}
|
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-22 01:34:23 +02:00
|
|
|
fun getBodyTextView(context: Context, message: MessageRecord): 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-06-24 07:17:12 +02:00
|
|
|
var body = message.body.toSpannable()
|
|
|
|
Linkify.addLinks(body, Linkify.WEB_URLS)
|
|
|
|
body = MentionUtilities.highlightMentions(body, message.isOutgoing, message.threadId, context);
|
|
|
|
result.text = body
|
2021-06-22 01:34:23 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
@ColorInt
|
|
|
|
fun getTextColor(context: Context, message: MessageRecord): Int {
|
|
|
|
val uiMode = UiModeUtilities.getUserSelectedUiMode(context)
|
|
|
|
val colorID = if (message.isOutgoing) {
|
|
|
|
if (uiMode == UiMode.NIGHT) R.color.black else R.color.white
|
|
|
|
} else {
|
|
|
|
if (uiMode == UiMode.NIGHT) R.color.white else R.color.black
|
|
|
|
}
|
|
|
|
return context.resources.getColorWithID(colorID, context.theme)
|
2021-06-02 05:03:22 +02:00
|
|
|
}
|
2021-06-01 05:26:57 +02:00
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
}
|