mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
fix(reactions): readable fallback, working swipe, hidden sender copy
- Rework the quik wire format: the visible body is now an iOS-style
fallback ("Reacted 👍 to \"...\"" or "Reacted 👍 to a message") and the
machine data (emoji, sender address, timestamp, text) is hidden in a
zero-width payload using U+2061..U+2064 so it never collides with the
iOS/Google tapback characters. Non-quik clients see a clean sentence.
- Fix swipe-to-react: the message item touch listener no longer consumes
events, so the RecyclerView's ItemTouchHelper receives swipes again.
- Restore long-press to select messages (removed long-press-to-react).
- Always hide our own outgoing reaction, even when the target message
can't be resolved locally, so the sender never sees the raw payload.
- Tune the reaction picker popup: theme-tinted background, themed text,
smaller emoji, and width capped to the screen so all options fit
without horizontal scrolling.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -35,18 +35,22 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
||||
private val keyManager: KeyManager,
|
||||
private val moshi: Moshi,
|
||||
) : EmojiReactionRepository {
|
||||
|
||||
companion object {
|
||||
// Invisible (default-ignorable) characters used to hide quik's machine payload.
|
||||
// Chosen from U+2061..U+2064 so they don't collide with the U+200A..U+200D chars used
|
||||
// by the iOS/Google tapback patterns, and won't appear in normal message text.
|
||||
private const val ZW_START = '\u2061' // function application
|
||||
private const val ZW_END = '\u2064' // invisible plus
|
||||
private const val ZW_ZERO = '\u2062' // invisible times
|
||||
private const val ZW_ONE = '\u2063' // invisible separator
|
||||
|
||||
private const val PAYLOAD_FIELD_SEPARATOR = '\u001f' // unit separator
|
||||
private const val MAX_PAYLOAD_TEXT = 120
|
||||
}
|
||||
|
||||
// We use an ordered map to make sure we can test tapback regexes before generic ones
|
||||
private val reactionPatterns: LinkedHashMap<Regex, (MatchResult) -> ParsedEmojiReaction?> = linkedMapOf(
|
||||
Regex( // quik-to-quik (must be first so it wins over the generic Google Messages pattern)
|
||||
"(?s)^\u200aQUIK\u200b([^\u200b]*)\u200b([^\u200b]*)\u200b([^\u200b]*)\u200a(.*)\u200aQUIK\u200a\\Z"
|
||||
) to { match ->
|
||||
ParsedEmojiReaction(
|
||||
emoji = match.groupValues[1],
|
||||
originalMessage = match.groupValues[4],
|
||||
quikSenderAddress = match.groupValues[2],
|
||||
quikTimestamp = match.groupValues[3].toLongOrNull(),
|
||||
)
|
||||
},
|
||||
Regex( // Google Messages
|
||||
"(?s)^\u200a[^\u200b\u200a]*\u200b([^\u200b]*)\u200b[^\u200b\u200a]*\u200a(.*)\u200a[^\u200b\u200a]*\u200a\\Z"
|
||||
) to { match ->
|
||||
@@ -144,6 +148,12 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
||||
}
|
||||
|
||||
override fun parseEmojiReaction(body: String): ParsedEmojiReaction? {
|
||||
// quik's own zero-width payload takes priority over the iOS/Google text patterns
|
||||
parseQuikReaction(body)?.let {
|
||||
Timber.d("Quik reaction found with ${it.emoji}")
|
||||
return it
|
||||
}
|
||||
|
||||
val removal = parseRemoval(body)
|
||||
if (removal != null) return removal
|
||||
|
||||
@@ -161,8 +171,68 @@ class EmojiReactionRepositoryImpl @Inject constructor(
|
||||
override fun buildReactionBody(emoji: String, targetMessage: Message): String {
|
||||
val senderAddress = targetMessage.address
|
||||
val timestampMs = targetMessage.date
|
||||
val originalText = targetMessage.getText(false)
|
||||
return "\u200aQUIK\u200b$emoji\u200b$senderAddress\u200b$timestampMs\u200a$originalText\u200aQUIK\u200a"
|
||||
val originalText = targetMessage.getText(false).trim()
|
||||
|
||||
// Visible, iOS-style fallback so non-quik clients see a clean, readable line.
|
||||
val human = if (originalText.isEmpty()) "Reacted $emoji to a message"
|
||||
else "Reacted $emoji to \"$originalText\""
|
||||
|
||||
// Machine-readable payload, hidden in zero-width characters and parsed only by quik.
|
||||
// originalText is capped to keep the invisible payload (and thus the SMS) reasonably small.
|
||||
val payload = listOf(emoji, senderAddress, timestampMs.toString(), originalText.take(MAX_PAYLOAD_TEXT))
|
||||
.joinToString(PAYLOAD_FIELD_SEPARATOR.toString())
|
||||
|
||||
return human + encodeQuikPayload(payload)
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a UTF-8 string into an invisible run of zero-width characters, wrapped in sentinels.
|
||||
* Each byte becomes 8 zero-width bits ([ZW_ZERO]/[ZW_ONE]).
|
||||
*/
|
||||
private fun encodeQuikPayload(payload: String): String {
|
||||
val sb = StringBuilder()
|
||||
sb.append(ZW_START)
|
||||
for (byte in payload.toByteArray(Charsets.UTF_8)) {
|
||||
val value = byte.toInt() and 0xFF
|
||||
for (bit in 7 downTo 0)
|
||||
sb.append(if ((value shr bit) and 1 == 1) ZW_ONE else ZW_ZERO)
|
||||
}
|
||||
sb.append(ZW_END)
|
||||
return sb.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract and decode a quik zero-width payload from a message body, or null if absent/invalid.
|
||||
*/
|
||||
private fun decodeQuikPayload(body: String): String? {
|
||||
val start = body.indexOf(ZW_START)
|
||||
if (start == -1) return null
|
||||
val end = body.indexOf(ZW_END, start + 1)
|
||||
if (end == -1) return null
|
||||
|
||||
val bits = body.substring(start + 1, end).filter { it == ZW_ZERO || it == ZW_ONE }
|
||||
if (bits.isEmpty() || bits.length % 8 != 0) return null
|
||||
|
||||
val bytes = ByteArray(bits.length / 8)
|
||||
for (i in bytes.indices) {
|
||||
var value = 0
|
||||
for (bit in 0 until 8)
|
||||
value = (value shl 1) or (if (bits[i * 8 + bit] == ZW_ONE) 1 else 0)
|
||||
bytes[i] = value.toByte()
|
||||
}
|
||||
return String(bytes, Charsets.UTF_8)
|
||||
}
|
||||
|
||||
private fun parseQuikReaction(body: String): ParsedEmojiReaction? {
|
||||
val decoded = decodeQuikPayload(body) ?: return null
|
||||
val parts = decoded.split(PAYLOAD_FIELD_SEPARATOR)
|
||||
if (parts.size < 3) return null
|
||||
return ParsedEmojiReaction(
|
||||
emoji = parts[0],
|
||||
originalMessage = parts.getOrElse(3) { "" },
|
||||
quikSenderAddress = parts[1].ifEmpty { null },
|
||||
quikTimestamp = parts[2].toLongOrNull(),
|
||||
)
|
||||
}
|
||||
|
||||
private fun parseRemoval(body: String): ParsedEmojiReaction? {
|
||||
|
||||
@@ -631,6 +631,10 @@ open class MessageRepositoryImpl @Inject constructor(
|
||||
parsedReaction.quikTimestamp,
|
||||
)
|
||||
realm.executeTransaction {
|
||||
// always hide our own sent reaction, even if the target message
|
||||
// can't be resolved locally (saveEmojiReaction only sets the flag
|
||||
// when a target is found)
|
||||
savedMessage.isEmojiReaction = true
|
||||
reactions.saveEmojiReaction(savedMessage, parsedReaction, targetMessage, realm)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -733,28 +733,37 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
|
||||
override fun showReactionPicker(messageId: Long) {
|
||||
val defaultEmojis = listOf("❤️", "👍", "👎", "😂", "😮", "😢")
|
||||
val pad = 12.dpToPx(this)
|
||||
val theme = colors.theme()
|
||||
val vPad = 8.dpToPx(this)
|
||||
|
||||
// Cap the popup to the screen width (minus a margin) so every emoji + the "+" fit
|
||||
// without horizontal scrolling; each item shares the width evenly via layout weight.
|
||||
val margin = 16.dpToPx(this)
|
||||
val popupWidth = resources.displayMetrics.widthPixels - margin * 2
|
||||
|
||||
val row = LinearLayout(this).apply {
|
||||
orientation = LinearLayout.HORIZONTAL
|
||||
setPadding(pad, pad, pad, pad)
|
||||
setPadding(vPad, vPad, vPad, vPad)
|
||||
}
|
||||
|
||||
val popup = PopupWindow(
|
||||
row,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
true
|
||||
).apply {
|
||||
val background = ContextCompat.getDrawable(this, R.drawable.rounded_rectangle_4dp)
|
||||
?.mutate()
|
||||
?.apply { setTint(theme.theme) }
|
||||
|
||||
val popup = PopupWindow(row, popupWidth, ViewGroup.LayoutParams.WRAP_CONTENT, true).apply {
|
||||
elevation = 8.dpToPx(this@ComposeActivity).toFloat()
|
||||
setBackgroundDrawable(ContextCompat.getDrawable(this@ComposeActivity, R.drawable.rounded_rectangle_4dp))
|
||||
setBackgroundDrawable(background)
|
||||
}
|
||||
|
||||
fun emojiView(label: String, onClick: () -> Unit) = TextView(this).apply {
|
||||
text = label
|
||||
textSize = 24f
|
||||
textSize = 20f
|
||||
gravity = Gravity.CENTER
|
||||
setPadding(pad, pad / 2, pad, pad / 2)
|
||||
includeFontPadding = false
|
||||
setTextColor(theme.textPrimary)
|
||||
setPadding(0, vPad, 0, vPad)
|
||||
// weight = 1 so all items share the popup width equally and shrink to fit
|
||||
layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)
|
||||
setOnClickListener {
|
||||
onClick()
|
||||
popup.dismiss()
|
||||
@@ -786,8 +795,11 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
?.let { binding.messageList.layoutManager?.findViewByPosition(it) }
|
||||
|
||||
if (messageView != null) {
|
||||
row.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
|
||||
popup.showAsDropDown(messageView, 0, -(messageView.height + row.measuredHeight))
|
||||
row.measure(
|
||||
View.MeasureSpec.makeMeasureSpec(popupWidth, View.MeasureSpec.EXACTLY),
|
||||
View.MeasureSpec.UNSPECIFIED
|
||||
)
|
||||
popup.showAsDropDown(messageView, margin, -(messageView.height + row.measuredHeight))
|
||||
} else {
|
||||
popup.showAtLocation(binding.messageList, Gravity.CENTER, 0, 0)
|
||||
}
|
||||
|
||||
@@ -174,10 +174,11 @@ class MessagesAdapter @Inject constructor(
|
||||
|
||||
return QkViewHolder(view).apply {
|
||||
val holder = this
|
||||
// A single GestureDetector on the item root handles tap / double-tap / long-press.
|
||||
// Child views (links, images, parts) keep handling their own touch events.
|
||||
// GestureDetector handles single-tap (select/expand) and double-tap (react).
|
||||
// It must NOT consume events, otherwise the RecyclerView's ItemTouchHelper never
|
||||
// sees swipe gestures. The view is long-clickable (below), so it stays a touch
|
||||
// target and the detector keeps receiving move/up events for double-tap detection.
|
||||
val gestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
|
||||
// must return true so the detector receives the subsequent events
|
||||
override fun onDown(e: MotionEvent): Boolean = true
|
||||
|
||||
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
|
||||
@@ -197,19 +198,19 @@ class MessagesAdapter @Inject constructor(
|
||||
getItem(holder.adapterPosition)?.let { doubleTapIntent.onNext(it.id) }
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onLongPress(e: MotionEvent) {
|
||||
getItem(holder.adapterPosition)?.let {
|
||||
// If a selection is already active, keep extending it (multi-select).
|
||||
// Otherwise, long-press opens the emoji reaction picker for this message.
|
||||
// Selection mode remains reachable via the toolbar (select all) once active.
|
||||
if (toggleSelection(it.id, false)) view.isActivated = isSelected(it.id)
|
||||
else reactionPickerIntent.onNext(it.id)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
view.setOnTouchListener { _, e -> gestureDetector.onTouchEvent(e) }
|
||||
// return false: don't consume, so swipe-to-react (ItemTouchHelper) keeps working
|
||||
view.setOnTouchListener { _, e -> gestureDetector.onTouchEvent(e); false }
|
||||
|
||||
// long-press enters/extends message selection mode
|
||||
view.setOnLongClickListener {
|
||||
getItem(holder.adapterPosition)?.let {
|
||||
toggleSelection(it.id)
|
||||
view.isActivated = isSelected(it.id)
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user