session-android/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt

318 lines
15 KiB
Kotlin
Raw Normal View History

package org.thoughtcrime.securesms.conversation.v2.messages
import android.content.Context
2021-06-23 03:32:05 +02:00
import android.content.res.Resources
2021-06-09 07:12:48 +02:00
import android.graphics.Canvas
import android.graphics.Rect
2021-06-09 04:04:50 +02:00
import android.graphics.drawable.ColorDrawable
import android.os.Build
2021-06-09 03:18:15 +02:00
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.*
import android.widget.LinearLayout
2021-06-09 07:12:48 +02:00
import androidx.core.content.ContextCompat
2021-06-07 08:06:37 +02:00
import androidx.core.view.isVisible
import kotlinx.android.synthetic.main.view_visible_message.view.*
2021-06-22 07:41:14 +02:00
import kotlinx.android.synthetic.main.view_visible_message.view.profilePictureView
2021-05-31 06:29:11 +02:00
import network.loki.messenger.R
import org.session.libsession.messaging.contacts.Contact.ContactContext
import org.session.libsession.messaging.open_groups.OpenGroupAPIV2
2021-06-07 07:37:21 +02:00
import org.session.libsession.utilities.ViewUtil
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.model.MessageRecord
2021-06-09 04:04:50 +02:00
import org.thoughtcrime.securesms.loki.utilities.getColorWithID
import org.thoughtcrime.securesms.loki.utilities.toDp
2021-06-09 07:12:48 +02:00
import org.thoughtcrime.securesms.loki.utilities.toPx
2021-06-21 07:26:09 +02:00
import org.thoughtcrime.securesms.mms.GlideRequests
2021-06-07 07:37:21 +02:00
import org.thoughtcrime.securesms.util.DateUtils
2021-06-07 08:06:37 +02:00
import java.util.*
import kotlin.math.abs
2021-06-09 07:12:48 +02:00
import kotlin.math.min
2021-06-07 07:37:21 +02:00
import kotlin.math.roundToInt
import kotlin.math.sqrt
class VisibleMessageView : LinearLayout {
2021-06-23 03:32:05 +02:00
private val screenWidth = Resources.getSystem().displayMetrics.widthPixels
2021-06-10 02:39:15 +02:00
private val swipeToReplyIcon = ContextCompat.getDrawable(context, R.drawable.ic_baseline_reply_24)!!.mutate()
2021-06-09 07:12:48 +02:00
private val swipeToReplyIconRect = Rect()
private var dx = 0.0f
private var previousTranslationX = 0.0f
2021-06-09 03:18:15 +02:00
private val gestureHandler = Handler(Looper.getMainLooper())
2021-06-28 07:41:23 +02:00
private var pressCallback: Runnable? = null
2021-06-09 03:18:15 +02:00
private var longPressCallback: Runnable? = null
2021-06-09 03:37:50 +02:00
private var onDownTimestamp = 0L
2021-06-28 07:41:23 +02:00
private var onDoubleTap: (() -> Unit)? = null
2021-06-09 04:04:50 +02:00
var snIsSelected = false
set(value) { field = value; handleIsSelectedChanged()}
2021-06-25 08:30:23 +02:00
var onPress: ((rawX: Int, rawY: Int) -> Unit)? = null
2021-06-09 03:37:50 +02:00
var onSwipeToReply: (() -> Unit)? = null
var onLongPress: (() -> Unit)? = null
companion object {
2021-06-18 08:24:56 +02:00
const val swipeToReplyThreshold = 80.0f // dp
2021-06-09 03:18:15 +02:00
const val longPressMovementTreshold = 10.0f // dp
2021-06-09 03:37:50 +02:00
const val longPressDurationThreshold = 250L // ms
2021-06-28 07:41:23 +02:00
const val maxDoubleTapInterval = 200L
}
// 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() }
private fun initialize() {
LayoutInflater.from(context).inflate(R.layout.view_visible_message, this)
2021-06-01 06:28:14 +02:00
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
isHapticFeedbackEnabled = true
2021-06-09 07:12:48 +02:00
setWillNotDraw(false)
}
// endregion
// region Updating
2021-06-21 07:26:09 +02:00
fun bind(message: MessageRecord, previous: MessageRecord?, next: MessageRecord?, glide: GlideRequests) {
val sender = message.individualRecipient
val senderSessionID = sender.address.serialize()
val threadID = message.threadId
val threadDB = DatabaseFactory.getThreadDatabase(context)
2021-06-23 05:39:24 +02:00
val thread = threadDB.getRecipientForThreadId(threadID)!!
val contactDB = DatabaseFactory.getSessionContactDatabase(context)
2021-06-23 05:57:13 +02:00
val isGroupThread = thread.isGroupRecipient
val isStartOfMessageCluster = isStartOfMessageCluster(message, previous, isGroupThread)
val isEndOfMessageCluster = isEndOfMessageCluster(message, next, isGroupThread)
2021-06-01 06:28:14 +02:00
// Show profile picture and sender name if this is a group thread AND
// the message is incoming
if (isGroupThread && !message.isOutgoing) {
profilePictureContainer.visibility = if (isEndOfMessageCluster) View.VISIBLE else View.INVISIBLE
profilePictureView.publicKey = senderSessionID
profilePictureView.glide = glide
profilePictureView.update()
if (thread.isOpenGroupRecipient) {
val openGroup = DatabaseFactory.getLokiThreadDatabase(context).getOpenGroupChat(threadID)!!
val isModerator = OpenGroupAPIV2.isUserModerator(senderSessionID, openGroup.room, openGroup.server)
moderatorIconImageView.visibility = if (isModerator) View.VISIBLE else View.INVISIBLE
} else {
moderatorIconImageView.visibility = View.INVISIBLE
}
senderNameTextView.isVisible = isStartOfMessageCluster
val context = if (thread.isOpenGroupRecipient) ContactContext.OPEN_GROUP else ContactContext.REGULAR
senderNameTextView.text = contactDB.getContactWithSessionID(senderSessionID)?.displayName(context) ?: senderSessionID
} else {
profilePictureContainer.visibility = View.GONE
senderNameTextView.visibility = View.GONE
}
2021-06-01 06:38:52 +02:00
// Date break
2021-06-07 08:06:37 +02:00
val showDateBreak = (previous == null || !DateUtils.isSameDay(message.timestamp, previous.timestamp))
dateBreakTextView.isVisible = showDateBreak
dateBreakTextView.text = if (showDateBreak) DateUtils.getRelativeDate(context, Locale.getDefault(), message.timestamp) else ""
2021-06-08 06:06:16 +02:00
// Timestamp
messageTimestampTextView.text = DateUtils.getExtendedRelativeTimeSpanString(context, Locale.getDefault(), message.timestamp)
2021-06-01 06:28:14 +02:00
// Margins
2021-06-08 06:06:16 +02:00
val messageContentContainerLayoutParams = messageContentContainer.layoutParams as LinearLayout.LayoutParams
2021-06-01 07:43:37 +02:00
if (isGroupThread) {
2021-06-08 06:06:16 +02:00
messageContentContainerLayoutParams.leftMargin = if (message.isOutgoing) resources.getDimension(R.dimen.very_large_spacing).toInt() else 0
2021-06-01 07:43:37 +02:00
} else {
2021-06-08 06:06:16 +02:00
messageContentContainerLayoutParams.leftMargin = if (message.isOutgoing) resources.getDimension(R.dimen.very_large_spacing).toInt()
2021-06-01 07:43:37 +02:00
else resources.getDimension(R.dimen.medium_spacing).toInt()
}
2021-06-08 06:06:16 +02:00
messageContentContainerLayoutParams.rightMargin = if (message.isOutgoing) resources.getDimension(R.dimen.medium_spacing).toInt()
2021-06-01 07:43:37 +02:00
else resources.getDimension(R.dimen.very_large_spacing).toInt()
2021-06-08 06:06:16 +02:00
messageContentContainer.layoutParams = messageContentContainerLayoutParams
2021-06-07 07:37:21 +02:00
// Set inter-message spacing
2021-06-07 07:48:22 +02:00
setMessageSpacing(isStartOfMessageCluster, isEndOfMessageCluster)
2021-06-01 06:28:14 +02:00
// Gravity
2021-06-01 06:38:52 +02:00
val gravity = if (message.isOutgoing) Gravity.RIGHT else Gravity.LEFT
mainContainer.gravity = gravity or Gravity.BOTTOM
2021-06-22 07:41:14 +02:00
// Message status indicator
val iconID = getMessageStatusImage(message)
if (iconID != null) {
messageStatusImageView.setImageResource(iconID)
}
if (message.isOutgoing) {
val lastMessageID = DatabaseFactory.getMmsSmsDatabase(context).getLastMessageID(message.threadId)
messageStatusImageView.isVisible = !message.isSent || message.id == lastMessageID
} else {
messageStatusImageView.isVisible = false
}
2021-06-23 05:57:13 +02:00
// Calculate max message bubble width
2021-06-23 03:32:05 +02:00
var maxWidth = screenWidth - messageContentContainerLayoutParams.leftMargin - messageContentContainerLayoutParams.rightMargin
2021-06-23 05:39:24 +02:00
if (profilePictureContainer.visibility != View.GONE) { maxWidth -= profilePictureContainer.width }
2021-06-23 05:57:13 +02:00
// Populate content view
2021-06-23 05:39:24 +02:00
messageContentView.bind(message, isStartOfMessageCluster, isEndOfMessageCluster, glide, maxWidth, thread)
2021-06-28 07:41:23 +02:00
onDoubleTap = { messageContentView.onContentDoubleTap?.invoke() }
}
2021-06-07 07:48:22 +02:00
private fun setMessageSpacing(isStartOfMessageCluster: Boolean, isEndOfMessageCluster: Boolean) {
val topPadding = if (isStartOfMessageCluster) R.dimen.conversation_vertical_message_spacing_default else R.dimen.conversation_vertical_message_spacing_collapse
2021-06-07 07:37:21 +02:00
ViewUtil.setPaddingTop(this, resources.getDimension(topPadding).roundToInt())
2021-06-07 07:48:22 +02:00
val bottomPadding = if (isEndOfMessageCluster) R.dimen.conversation_vertical_message_spacing_default else R.dimen.conversation_vertical_message_spacing_collapse
2021-06-07 07:37:21 +02:00
ViewUtil.setPaddingBottom(this, resources.getDimension(bottomPadding).roundToInt())
}
private fun isStartOfMessageCluster(current: MessageRecord, previous: MessageRecord?, isGroupThread: Boolean): Boolean {
return if (isGroupThread) {
previous == null || previous.isUpdate || !DateUtils.isSameDay(current.timestamp, previous.timestamp)
|| current.recipient.address != previous.recipient.address
} else {
previous == null || previous.isUpdate || !DateUtils.isSameDay(current.timestamp, previous.timestamp)
|| current.isOutgoing != previous.isOutgoing
}
}
private fun isEndOfMessageCluster(current: MessageRecord, next: MessageRecord?, isGroupThread: Boolean): Boolean {
return if (isGroupThread) {
next == null || next.isUpdate || !DateUtils.isSameDay(current.timestamp, next.timestamp)
|| current.recipient.address != next.recipient.address
} else {
next == null || next.isUpdate || !DateUtils.isSameDay(current.timestamp, next.timestamp)
|| current.isOutgoing != next.isOutgoing
}
}
2021-06-22 07:41:14 +02:00
private fun getMessageStatusImage(message: MessageRecord): Int? {
when {
!message.isOutgoing -> return null
message.isFailed -> return R.drawable.ic_error
message.isPending -> return R.drawable.ic_circle_dot_dot_dot
message.isRead -> return R.drawable.ic_filled_circle_check
else -> return R.drawable.ic_circle_check
}
}
2021-06-09 04:04:50 +02:00
private fun handleIsSelectedChanged() {
background = if (snIsSelected) {
2021-06-10 03:37:24 +02:00
ColorDrawable(context.resources.getColorWithID(R.color.message_selected, context.theme))
2021-06-09 04:04:50 +02:00
} else {
null
}
}
2021-06-09 07:12:48 +02:00
override fun onDraw(canvas: Canvas) {
if (translationX < 0) {
val spacing = context.resources.getDimensionPixelSize(R.dimen.small_spacing)
val threshold = VisibleMessageView.swipeToReplyThreshold
val iconSize = toPx(24, context.resources)
2021-06-28 07:44:11 +02:00
val bottomVOffset = paddingBottom + messageStatusImageView.height + (messageContentView.height - iconSize) / 2
2021-06-09 07:12:48 +02:00
swipeToReplyIconRect.left = messageContentContainer.right + spacing
swipeToReplyIconRect.top = height - bottomVOffset - iconSize
swipeToReplyIconRect.right = messageContentContainer.right + iconSize + spacing
swipeToReplyIconRect.bottom = height - bottomVOffset
swipeToReplyIcon.bounds = swipeToReplyIconRect
swipeToReplyIcon.alpha = (255.0f * (min(abs(translationX), threshold) / threshold)).roundToInt()
} else {
swipeToReplyIcon.alpha = 0
}
2021-06-10 02:04:50 +02:00
swipeToReplyIcon.draw(canvas)
2021-06-09 07:12:48 +02:00
super.onDraw(canvas)
}
fun recycle() {
profilePictureView.recycle()
messageContentView.recycle()
}
// endregion
// region Interaction
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> onDown(event)
MotionEvent.ACTION_MOVE -> onMove(event)
MotionEvent.ACTION_CANCEL -> onCancel(event)
MotionEvent.ACTION_UP -> onUp(event)
}
return true
}
private fun onDown(event: MotionEvent) {
dx = x - event.rawX
2021-06-09 03:37:50 +02:00
longPressCallback?.let { gestureHandler.removeCallbacks(it) }
val newLongPressCallback = Runnable { onLongPress() }
this.longPressCallback = newLongPressCallback
gestureHandler.postDelayed(newLongPressCallback, VisibleMessageView.longPressDurationThreshold)
onDownTimestamp = Date().time
}
private fun onMove(event: MotionEvent) {
val translationX = toDp(event.rawX + dx, context.resources)
2021-06-09 04:04:50 +02:00
if (abs(translationX) < VisibleMessageView.longPressMovementTreshold || snIsSelected) {
2021-06-09 03:18:15 +02:00
return
} else {
2021-06-09 03:37:50 +02:00
longPressCallback?.let { gestureHandler.removeCallbacks(it) }
2021-06-09 03:18:15 +02:00
}
// The idea here is to asymptotically approach a maximum drag distance
val damping = 50.0f
val sign = -1.0f
val x = (damping * (sqrt(abs(translationX)) / sqrt(damping))) * sign
this.translationX = x
2021-06-10 02:54:26 +02:00
this.dateBreakTextView.translationX = -x // Bit of a hack to keep the date break text view from moving
2021-06-09 07:12:48 +02:00
postInvalidate() // Ensure onDraw(canvas:) is called
if (abs(x) > VisibleMessageView.swipeToReplyThreshold && abs(previousTranslationX) < VisibleMessageView.swipeToReplyThreshold) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
} else {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
}
}
previousTranslationX = x
}
private fun onCancel(event: MotionEvent) {
2021-06-21 01:40:23 +02:00
if (abs(translationX) > VisibleMessageView.swipeToReplyThreshold) {
onSwipeToReply?.invoke()
}
longPressCallback?.let { gestureHandler.removeCallbacks(it) }
2021-06-16 06:50:41 +02:00
resetPosition()
}
private fun onUp(event: MotionEvent) {
if (abs(translationX) > VisibleMessageView.swipeToReplyThreshold) {
2021-06-09 03:37:50 +02:00
onSwipeToReply?.invoke()
} else if ((Date().time - onDownTimestamp) < VisibleMessageView.longPressDurationThreshold) {
longPressCallback?.let { gestureHandler.removeCallbacks(it) }
2021-06-28 07:41:23 +02:00
val pressCallback = this.pressCallback
if (pressCallback != null) {
// If we're here and pressCallback isn't null, it means that we tapped again within
// maxDoubleTapInterval ms and we should count this as a double tap
gestureHandler.removeCallbacks(pressCallback)
this.pressCallback = null
onDoubleTap?.invoke()
} else {
val newPressCallback = Runnable { onPress(event.rawX.toInt(), event.rawY.toInt()) }
2021-06-28 07:41:23 +02:00
this.pressCallback = newPressCallback
gestureHandler.postDelayed(newPressCallback, VisibleMessageView.maxDoubleTapInterval)
}
}
2021-06-16 06:50:41 +02:00
resetPosition()
}
private fun resetPosition() {
animate()
.translationX(0.0f)
.setDuration(150)
2021-06-10 02:54:26 +02:00
.setUpdateListener {
postInvalidate() // Ensure onDraw(canvas:) is called
}
.start()
2021-06-10 02:54:26 +02:00
// Bit of a hack to keep the date break text view from moving
dateBreakTextView.animate()
2021-06-16 06:50:41 +02:00
.translationX(0.0f)
.setDuration(150)
.start()
}
2021-06-09 03:18:15 +02:00
private fun onLongPress() {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
2021-06-09 03:37:50 +02:00
onLongPress?.invoke()
2021-06-09 03:18:15 +02:00
}
2021-06-25 08:30:23 +02:00
fun onContentClick(rawRect: Rect) {
messageContentView.onContentClick?.invoke(rawRect)
}
private fun onPress(rawX: Int, rawY: Int) {
onPress?.invoke(rawX, rawY)
2021-06-28 07:41:23 +02:00
pressCallback = null
}
// endregion
2021-06-09 03:37:50 +02:00
}