Skip to content

Commit 1a4a122

Browse files
authored
SES-4977 : Don't show link preview acceptance dialog when a user is typing, only when a link is pasted (#1773)
* Added check for chunky inserts on text change * Lazy init * Updated some paste logic * Cleaned up imports
1 parent c4b0590 commit 1a4a122

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,15 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
13221322
if (textSecurePreferences.isLinkPreviewsEnabled()) {
13231323
linkPreviewViewModel.onTextChanged(this, inputBarText, 0, 0)
13241324
}
1325-
if (LinkPreviewUtil.findWhitelistedUrls(newContent.toString()).isNotEmpty()
1325+
1326+
// use the normalised version of the text's body to get the characters amount with the
1327+
// mentions as their account id
1328+
viewModel.onTextChanged(mentionViewModel.deconstructMessageMentions())
1329+
}
1330+
1331+
override fun onInputBarEditTextPasted() {
1332+
val inputBarText = binding.inputBar.text
1333+
if (LinkPreviewUtil.findWhitelistedUrls(inputBarText).isNotEmpty()
13261334
&& !textSecurePreferences.isLinkPreviewsEnabled() && !textSecurePreferences.hasSeenLinkPreviewSuggestionDialog()) {
13271335
LinkPreviewDialog {
13281336
setUpLinkPreviewObserver()
@@ -1331,10 +1339,6 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
13311339
}.show(supportFragmentManager, "Link Preview Dialog")
13321340
textSecurePreferences.setHasSeenLinkPreviewSuggestionDialog()
13331341
}
1334-
1335-
// use the normalised version of the text's body to get the characters amount with the
1336-
// mentions as their account id
1337-
viewModel.onTextChanged(mentionViewModel.deconstructMessageMentions())
13381342
}
13391343

13401344
override fun toggleAttachmentOptions() {

app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBar.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import dagger.hilt.android.AndroidEntryPoint
2020
import network.loki.messenger.R
2121
import network.loki.messenger.databinding.ViewInputBarBinding
2222
import org.session.libsession.messaging.sending_receiving.link_preview.LinkPreview
23-
import org.session.libsession.utilities.Address
2423
import org.session.libsession.utilities.TextSecurePreferences
2524
import org.session.libsession.utilities.getColorFromAttr
2625
import org.session.libsession.utilities.recipients.Recipient
@@ -300,6 +299,10 @@ class InputBar @JvmOverloads constructor(
300299
requestLayout()
301300
}
302301

302+
override fun onPaste() {
303+
delegate?.onInputBarEditTextPasted()
304+
}
305+
303306
private fun showOrHideInputIfNeeded() {
304307
if (!showInput) {
305308
cancelQuoteDraft()
@@ -383,6 +386,7 @@ class InputBar @JvmOverloads constructor(
383386

384387
interface InputBarDelegate {
385388
fun inputBarEditTextContentChanged(newContent: CharSequence)
389+
fun onInputBarEditTextPasted() {} // no-op by default
386390
fun toggleAttachmentOptions()
387391
fun showVoiceMessageUI()
388392
fun startRecordingVoiceMessage()

app/src/main/java/org/thoughtcrime/securesms/conversation/v2/input_bar/InputBarEditText.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ import android.net.Uri
66
import android.util.AttributeSet
77
import android.view.inputmethod.EditorInfo
88
import android.view.inputmethod.InputConnection
9-
import android.widget.RelativeLayout
109
import androidx.appcompat.widget.AppCompatEditText
1110
import androidx.core.view.inputmethod.EditorInfoCompat
1211
import androidx.core.view.inputmethod.InputConnectionCompat
13-
import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities
1412
import org.thoughtcrime.securesms.util.toPx
15-
import kotlin.math.max
16-
import kotlin.math.min
1713
import kotlin.math.roundToInt
1814

1915
class InputBarEditText : AppCompatEditText {
@@ -22,21 +18,42 @@ class InputBarEditText : AppCompatEditText {
2218

2319
var allowMultimediaInput: Boolean = true
2420

25-
2621
constructor(context: Context) : super(context)
2722
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
2823
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
2924

3025
override fun onTextChanged(text: CharSequence, start: Int, lengthBefore: Int, lengthAfter: Int) {
3126
super.onTextChanged(text, start, lengthBefore, lengthAfter)
3227
delegate?.inputBarEditTextContentChanged(text)
28+
29+
// - A "chunk" got inserted (lengthAfter >= 3)
30+
// - If it is large enough, treat it as paste. We do this because some IME's clipboard history
31+
// will not be treated as a "paste" but instead just a normal text insertion
32+
if (lengthAfter >= 3) { // catch most real paste
33+
val inserted = safeSubSequence(text, start, start + lengthAfter)
34+
if (!inserted.isNullOrEmpty()) {
35+
// Bulk insert will mostly come from IME that supports clipboard history
36+
val isBulkInsert = inserted.length >= 5 // small enough to catch URIs
37+
38+
if (isBulkInsert) {
39+
delegate?.onPaste()
40+
}
41+
}
42+
}
43+
3344
// Calculate the width manually to get it right even before layout has happened (i.e.
3445
// when restoring a draft). The 64 DP is the horizontal margin around the input bar
3546
// edit text.
3647
val width = (screenWidth - 2 * toPx(64.0f, resources)).roundToInt()
3748
if (width < 0) { return } // screenWidth initially evaluates to 0
3849
}
3950

51+
// Small helper to avoid IndexOutOfBounds on weird IME behavior
52+
private fun safeSubSequence(text: CharSequence, start: Int, end: Int): String? {
53+
if (start < 0 || end > text.length || start >= end) return null
54+
return text.subSequence(start, end).toString()
55+
}
56+
4057
override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection? {
4158
val ic = super.onCreateInputConnection(editorInfo) ?: return null
4259
EditorInfoCompat.setContentMimeTypes(editorInfo,
@@ -62,12 +79,15 @@ class InputBarEditText : AppCompatEditText {
6279

6380
true // return true if succeeded
6481
}
82+
83+
84+
6585
return InputConnectionCompat.createWrapper(ic, editorInfo, callback)
6686
}
67-
6887
}
6988

7089
interface InputBarEditTextDelegate {
7190
fun inputBarEditTextContentChanged(text: CharSequence)
7291
fun commitInputContent(contentUri: Uri)
92+
fun onPaste()
7393
}

0 commit comments

Comments
 (0)