Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,15 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
if (textSecurePreferences.isLinkPreviewsEnabled()) {
linkPreviewViewModel.onTextChanged(this, inputBarText, 0, 0)
}
if (LinkPreviewUtil.findWhitelistedUrls(newContent.toString()).isNotEmpty()

// use the normalised version of the text's body to get the characters amount with the
// mentions as their account id
viewModel.onTextChanged(mentionViewModel.deconstructMessageMentions())
}

override fun onInputBarEditTextPasted() {
val inputBarText = binding.inputBar.text
if (LinkPreviewUtil.findWhitelistedUrls(inputBarText).isNotEmpty()
&& !textSecurePreferences.isLinkPreviewsEnabled() && !textSecurePreferences.hasSeenLinkPreviewSuggestionDialog()) {
LinkPreviewDialog {
setUpLinkPreviewObserver()
Expand All @@ -1331,10 +1339,6 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
}.show(supportFragmentManager, "Link Preview Dialog")
textSecurePreferences.setHasSeenLinkPreviewSuggestionDialog()
}

// use the normalised version of the text's body to get the characters amount with the
// mentions as their account id
viewModel.onTextChanged(mentionViewModel.deconstructMessageMentions())
}

override fun toggleAttachmentOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import dagger.hilt.android.AndroidEntryPoint
import network.loki.messenger.R
import network.loki.messenger.databinding.ViewInputBarBinding
import org.session.libsession.messaging.sending_receiving.link_preview.LinkPreview
import org.session.libsession.utilities.Address
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.getColorFromAttr
import org.session.libsession.utilities.recipients.Recipient
Expand Down Expand Up @@ -300,6 +299,10 @@ class InputBar @JvmOverloads constructor(
requestLayout()
}

override fun onPaste() {
delegate?.onInputBarEditTextPasted()
}

private fun showOrHideInputIfNeeded() {
if (!showInput) {
cancelQuoteDraft()
Expand Down Expand Up @@ -383,6 +386,7 @@ class InputBar @JvmOverloads constructor(

interface InputBarDelegate {
fun inputBarEditTextContentChanged(newContent: CharSequence)
fun onInputBarEditTextPasted() {} // no-op by default
fun toggleAttachmentOptions()
fun showVoiceMessageUI()
fun startRecordingVoiceMessage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import android.net.Uri
import android.util.AttributeSet
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.widget.RelativeLayout
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.view.inputmethod.EditorInfoCompat
import androidx.core.view.inputmethod.InputConnectionCompat
import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities
import org.thoughtcrime.securesms.util.toPx
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt

class InputBarEditText : AppCompatEditText {
Expand All @@ -22,21 +18,42 @@ class InputBarEditText : AppCompatEditText {

var allowMultimediaInput: Boolean = true


constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

override fun onTextChanged(text: CharSequence, start: Int, lengthBefore: Int, lengthAfter: Int) {
super.onTextChanged(text, start, lengthBefore, lengthAfter)
delegate?.inputBarEditTextContentChanged(text)

// - A "chunk" got inserted (lengthAfter >= 3)
// - If it is large enough, treat it as paste. We do this because some IME's clipboard history
// will not be treated as a "paste" but instead just a normal text insertion
if (lengthAfter >= 3) { // catch most real paste
val inserted = safeSubSequence(text, start, start + lengthAfter)
if (!inserted.isNullOrEmpty()) {
// Bulk insert will mostly come from IME that supports clipboard history
val isBulkInsert = inserted.length >= 5 // small enough to catch URIs

if (isBulkInsert) {
delegate?.onPaste()
}
}
}

// Calculate the width manually to get it right even before layout has happened (i.e.
// when restoring a draft). The 64 DP is the horizontal margin around the input bar
// edit text.
val width = (screenWidth - 2 * toPx(64.0f, resources)).roundToInt()
if (width < 0) { return } // screenWidth initially evaluates to 0
}

// Small helper to avoid IndexOutOfBounds on weird IME behavior
private fun safeSubSequence(text: CharSequence, start: Int, end: Int): String? {
if (start < 0 || end > text.length || start >= end) return null
return text.subSequence(start, end).toString()
}

override fun onCreateInputConnection(editorInfo: EditorInfo): InputConnection? {
val ic = super.onCreateInputConnection(editorInfo) ?: return null
EditorInfoCompat.setContentMimeTypes(editorInfo,
Expand All @@ -62,12 +79,15 @@ class InputBarEditText : AppCompatEditText {

true // return true if succeeded
}



return InputConnectionCompat.createWrapper(ic, editorInfo, callback)
}

}

interface InputBarEditTextDelegate {
fun inputBarEditTextContentChanged(text: CharSequence)
fun commitInputContent(contentUri: Uri)
fun onPaste()
}