forked from upstream-mirrors/quik
limit bubble display of text to 5000 chars.
add instruction status message when bubble char limit exceeded. add share text feature. make copy and share text menu options only available when there is text in selected messages. new FileProvider to share messages text files out of app's cache dir. add message subject to copy messages text feature. a lot of refactoring of MessagesAdapter class. add new FileUtils class. convert file logging to use new FileUtils class. change MediaRecorder to use new FileUtils class. housekeeping worker now cleans messages text cache files. housekeeping worker now checks cache files older than 2 hours before deleting.
This commit is contained in:
@@ -24,21 +24,22 @@ import android.content.Context
|
||||
import android.widget.Toast
|
||||
import dev.octoshrimpy.quik.R
|
||||
|
||||
object ClipboardUtils {
|
||||
|
||||
fun copy(context: Context, string: String) {
|
||||
try {
|
||||
(context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
|
||||
.setPrimaryClip(ClipData.newPlainText("SMS", string))
|
||||
} catch (e: Exception) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
if ((e is RuntimeException) &&
|
||||
e.message?.startsWith("android.os.TransactionTooLargeException") == true
|
||||
) R.string.clipboard_too_large_to_copy
|
||||
else R.string.clipboard_unable_to_copy_to,
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
class ClipboardUtils {
|
||||
companion object {
|
||||
fun copy(context: Context, string: String) {
|
||||
try {
|
||||
(context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager)
|
||||
.setPrimaryClip(ClipData.newPlainText("SMS", string))
|
||||
} catch (e: Exception) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
if ((e is RuntimeException) &&
|
||||
e.message?.startsWith("android.os.TransactionTooLargeException") == true
|
||||
) R.string.clipboard_too_large_to_copy
|
||||
else R.string.clipboard_unable_to_copy_to,
|
||||
Toast.LENGTH_LONG
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,16 +20,11 @@ package dev.octoshrimpy.quik.common.util
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
import androidx.core.content.contentValuesOf
|
||||
import dev.octoshrimpy.quik.util.Preferences
|
||||
import dev.octoshrimpy.quik.util.tryOrNull
|
||||
import dev.octoshrimpy.quik.util.FileUtils
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
@@ -78,48 +73,33 @@ class FileLoggingTree @Inject constructor(
|
||||
Log.getStackTraceString(t)
|
||||
}\n"
|
||||
|
||||
try {
|
||||
// if uri of log file not yet determined, get one now
|
||||
if (logFileUri == null) {
|
||||
val filename = "Quik-log-${
|
||||
SimpleDateFormat(
|
||||
"yyyy-MM-dd",
|
||||
Locale.getDefault()
|
||||
).format(System.currentTimeMillis())
|
||||
}.log"
|
||||
// if uri of log file not yet determined, get one now
|
||||
if (logFileUri == null) {
|
||||
val filename = "Quik-log-${
|
||||
SimpleDateFormat(
|
||||
"yyyy-MM-dd",
|
||||
Locale.getDefault()
|
||||
).format(System.currentTimeMillis())
|
||||
}.log"
|
||||
|
||||
logFileUri =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
|
||||
// use media store
|
||||
context.contentResolver.insert(
|
||||
MediaStore.Downloads.EXTERNAL_CONTENT_URI,
|
||||
contentValuesOf(
|
||||
MediaStore.MediaColumns.MIME_TYPE to "text/plain",
|
||||
MediaStore.MediaColumns.RELATIVE_PATH to
|
||||
Environment.DIRECTORY_DOWNLOADS,
|
||||
MediaStore.MediaColumns.DISPLAY_NAME to filename,
|
||||
)
|
||||
)
|
||||
else
|
||||
// use direct access to 'external' dir
|
||||
File(Environment.getExternalStorageDirectory(), filename).let {
|
||||
tryOrNull { it.createNewFile() }
|
||||
Uri.fromFile(it)
|
||||
}
|
||||
}
|
||||
val (uri, e) = FileUtils.create(
|
||||
FileUtils.Companion.Location.Downloads,
|
||||
context,
|
||||
filename,
|
||||
"text/plain"
|
||||
)
|
||||
if (e is Exception)
|
||||
Log.e(TAG, "Error opening log file", e)
|
||||
else
|
||||
logFileUri = uri
|
||||
}
|
||||
|
||||
logFileUri?.let {
|
||||
context.contentResolver.openOutputStream(it, "wa")?.use {
|
||||
// write the log entry
|
||||
it.write(logItem.toByteArray())
|
||||
}
|
||||
}
|
||||
} catch (e: FileNotFoundException) {
|
||||
Log.e(TAG, "Log file went away. Lost log file item: $logItem")
|
||||
// log file seems to have gone away. start a new file next time through
|
||||
logFileUri = null
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Error while logging into file", e)
|
||||
logFileUri?.let {
|
||||
val e = FileUtils.append(context, it, logItem.toByteArray())
|
||||
if (e is FileNotFoundException)
|
||||
Log.e(TAG, "Log file went away. Lost log file item: $logItem", e)
|
||||
else if (e is Exception)
|
||||
Log.e(TAG, "Error while logging into file", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ import dev.octoshrimpy.quik.common.util.extensions.setVisible
|
||||
import dev.octoshrimpy.quik.common.util.extensions.showKeyboard
|
||||
import dev.octoshrimpy.quik.common.widget.MicInputCloudView
|
||||
import dev.octoshrimpy.quik.common.widget.QkEditText
|
||||
import dev.octoshrimpy.quik.common.widget.TextInputDialog
|
||||
import dev.octoshrimpy.quik.extensions.mapNotNull
|
||||
import dev.octoshrimpy.quik.feature.compose.editing.ChipsAdapter
|
||||
import dev.octoshrimpy.quik.feature.contacts.ContactsActivity
|
||||
@@ -206,6 +207,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
override val recordAudioMsgRecordVisible: Subject<Boolean> = PublishSubject.create()
|
||||
override val recordAudioChronometer: Subject<Boolean> = PublishSubject.create()
|
||||
override val recordAudioRecord: Subject<MicInputCloudView.ViewState> = PublishSubject.create()
|
||||
// override val saveMessagesTextIntent: Subject<String> = PublishSubject.create()
|
||||
|
||||
private var seekBarUpdater: Disposable? = null
|
||||
|
||||
@@ -224,25 +226,6 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
return SpeechRecognizer.isRecognitionAvailable(this)
|
||||
}
|
||||
|
||||
private val speechResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode != Activity.RESULT_OK)
|
||||
return@registerForActivityResult
|
||||
|
||||
// check returned results are good
|
||||
val match = result.data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
|
||||
if ((match === null) || (match.size < 1) || (match[0].isNullOrEmpty()))
|
||||
return@registerForActivityResult
|
||||
|
||||
// get the edit text view
|
||||
val message = findViewById<QkEditText>(R.id.message)
|
||||
if (message === null)
|
||||
return@registerForActivityResult
|
||||
|
||||
// populate message box with data returned by STT, set cursor to end, and focus
|
||||
message.append(match[0])
|
||||
message.setSelection(message.text.length)
|
||||
message.requestFocus()
|
||||
}
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
AndroidInjection.inject(this)
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -495,7 +478,10 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
&& state.query.isEmpty()
|
||||
toolbar.menu.findItem(R.id.info)?.isVisible = !state.editingMode && state.selectedMessages == 0
|
||||
&& state.query.isEmpty()
|
||||
toolbar.menu.findItem(R.id.copy)?.isVisible = !state.editingMode && state.selectedMessages > 0
|
||||
toolbar.menu.findItem(R.id.copy)?.isVisible =
|
||||
!state.editingMode && state.selectedMessages > 0 && state.selectedMessagesHaveText
|
||||
toolbar.menu.findItem(R.id.share)?.isVisible =
|
||||
!state.editingMode && state.selectedMessages > 0 && state.selectedMessagesHaveText
|
||||
toolbar.menu.findItem(R.id.details)?.isVisible = !state.editingMode && state.selectedMessages == 1
|
||||
toolbar.menu.findItem(R.id.delete)?.isVisible = !state.editingMode && ((state.selectedMessages > 0) || state.canSend)
|
||||
toolbar.menu.findItem(R.id.forward)?.isVisible = !state.editingMode && state.selectedMessages == 1
|
||||
@@ -628,6 +614,14 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
.setNegativeButton(R.string.messageLinkHandling_dialog_negative) { _, _ -> { } }
|
||||
.show()
|
||||
}
|
||||
//
|
||||
// override fun showSaveMessagesFilenameDialog(filename: String) {
|
||||
// TextInputDialog(
|
||||
// this,
|
||||
// getString(R.string.messages_save_file_dialog_title),
|
||||
// saveMessagesTextIntent::onNext
|
||||
// ).setText(filename).show()
|
||||
// }
|
||||
|
||||
override fun requestDefaultSms() {
|
||||
navigator.showDefaultSmsDialog(this)
|
||||
|
||||
@@ -39,6 +39,7 @@ data class ComposeState(
|
||||
val searchResults: Int = 0,
|
||||
val messages: Pair<Conversation, RealmResults<Message>>? = null,
|
||||
val selectedMessages: Int = 0,
|
||||
val selectedMessagesHaveText: Boolean = false,
|
||||
val scheduled: Long = 0,
|
||||
val attachments: List<Attachment> = listOf(),
|
||||
val attaching: Boolean = false,
|
||||
|
||||
@@ -89,12 +89,14 @@ interface ComposeView : QkView<ComposeState> {
|
||||
val recordAudioMsgRecordVisible: Subject<Boolean>
|
||||
val recordAudioRecord: Subject<MicInputCloudView.ViewState>
|
||||
val recordAudioChronometer: Subject<Boolean>
|
||||
// val saveMessagesTextIntent: Subject<String>
|
||||
|
||||
fun clearSelection()
|
||||
fun toggleSelectAll()
|
||||
fun expandMessages(messageIds: List<Long>, expand: Boolean)
|
||||
fun showDetails(details: String)
|
||||
fun showMessageLinkAskDialog(uri: Uri)
|
||||
// fun showSaveMessagesFilenameDialog(filename: String)
|
||||
fun requestDefaultSms()
|
||||
fun requestStoragePermission()
|
||||
fun requestRecordAudioPermission()
|
||||
|
||||
@@ -25,23 +25,33 @@ import android.media.AudioDeviceInfo
|
||||
import android.net.Uri
|
||||
import android.os.Vibrator
|
||||
import android.telephony.SmsMessage
|
||||
import android.widget.Toast
|
||||
import androidx.core.content.getSystemService
|
||||
import androidx.core.content.FileProvider
|
||||
import androidx.core.net.toFile
|
||||
import androidx.core.net.toUri
|
||||
import com.google.android.exoplayer2.util.MimeTypes
|
||||
import com.moez.QKSMS.common.QkMediaPlayer
|
||||
import com.moez.QKSMS.contentproviders.MmsPartProvider
|
||||
import com.moez.QKSMS.manager.MediaRecorderManager
|
||||
import com.moez.QKSMS.manager.BluetoothMicManager
|
||||
import com.moez.QKSMS.manager.MediaRecorderManager
|
||||
import com.moez.QKSMS.manager.MediaRecorderManager.AUDIO_FILE_PREFIX
|
||||
import com.moez.QKSMS.manager.MediaRecorderManager.AUDIO_FILE_SUFFIX
|
||||
import com.moez.QKSMS.util.Constants.Companion.SAVED_MESSAGE_TEXT_FILE_PREFIX
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.autoDisposable
|
||||
import dev.octoshrimpy.quik.R
|
||||
import dev.octoshrimpy.quik.common.Navigator
|
||||
import dev.octoshrimpy.quik.common.base.QkViewModel
|
||||
import dev.octoshrimpy.quik.common.util.ClipboardUtils
|
||||
import dev.octoshrimpy.quik.common.util.MessageDetailsFormatter
|
||||
import dev.octoshrimpy.quik.common.util.extensions.makeToast
|
||||
import dev.octoshrimpy.quik.common.widget.MicInputCloudView
|
||||
import dev.octoshrimpy.quik.common.widget.QkContextMenuRecyclerView
|
||||
import dev.octoshrimpy.quik.compat.SubscriptionManagerCompat
|
||||
import dev.octoshrimpy.quik.compat.TelephonyCompat
|
||||
import dev.octoshrimpy.quik.extensions.asObservable
|
||||
import dev.octoshrimpy.quik.extensions.isImage
|
||||
import dev.octoshrimpy.quik.extensions.isSmil
|
||||
import dev.octoshrimpy.quik.extensions.isVideo
|
||||
import dev.octoshrimpy.quik.extensions.mapNotNull
|
||||
import dev.octoshrimpy.quik.interactor.AddScheduledMessage
|
||||
@@ -49,6 +59,7 @@ import dev.octoshrimpy.quik.interactor.CancelDelayedMessage
|
||||
import dev.octoshrimpy.quik.interactor.DeleteMessages
|
||||
import dev.octoshrimpy.quik.interactor.MarkRead
|
||||
import dev.octoshrimpy.quik.interactor.RetrySending
|
||||
import dev.octoshrimpy.quik.interactor.SaveImage
|
||||
import dev.octoshrimpy.quik.interactor.SendMessage
|
||||
import dev.octoshrimpy.quik.manager.ActiveConversationManager
|
||||
import dev.octoshrimpy.quik.manager.BillingManager
|
||||
@@ -56,21 +67,17 @@ import dev.octoshrimpy.quik.manager.PermissionManager
|
||||
import dev.octoshrimpy.quik.model.Attachment
|
||||
import dev.octoshrimpy.quik.model.Conversation
|
||||
import dev.octoshrimpy.quik.model.Message
|
||||
import dev.octoshrimpy.quik.model.MmsPart
|
||||
import dev.octoshrimpy.quik.model.Recipient
|
||||
import dev.octoshrimpy.quik.model.getText
|
||||
import dev.octoshrimpy.quik.repository.ContactRepository
|
||||
import dev.octoshrimpy.quik.repository.ConversationRepository
|
||||
import dev.octoshrimpy.quik.repository.MessageRepository
|
||||
import dev.octoshrimpy.quik.util.ActiveSubscriptionObservable
|
||||
import dev.octoshrimpy.quik.util.PhoneNumberUtils
|
||||
import dev.octoshrimpy.quik.util.Preferences
|
||||
import dev.octoshrimpy.quik.util.FileUtils
|
||||
import dev.octoshrimpy.quik.util.tryOrNull
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.autoDisposable
|
||||
import dev.octoshrimpy.quik.common.widget.MicInputCloudView
|
||||
import dev.octoshrimpy.quik.common.widget.QkContextMenuRecyclerView
|
||||
import dev.octoshrimpy.quik.extensions.isSmil
|
||||
import dev.octoshrimpy.quik.interactor.SaveImage
|
||||
import dev.octoshrimpy.quik.model.MmsPart
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxkotlin.Observables
|
||||
@@ -81,7 +88,7 @@ import io.reactivex.subjects.BehaviorSubject
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
@@ -120,11 +127,6 @@ class ComposeViewModel @Inject constructor(
|
||||
threadId = threadId,
|
||||
query = query)
|
||||
) {
|
||||
|
||||
companion object {
|
||||
private const val AUDIO_FILE_PREFIX = "recorded-"
|
||||
}
|
||||
|
||||
private val chipsReducer: Subject<(List<Recipient>) -> List<Recipient>> = PublishSubject.create()
|
||||
private val conversation: Subject<Conversation> = BehaviorSubject.create()
|
||||
private val messages: Subject<List<Message>> = BehaviorSubject.create()
|
||||
@@ -380,23 +382,76 @@ class ComposeViewModel @Inject constructor(
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.copy }
|
||||
.withLatestFrom(view.messagesSelectedIntent) { _, messageIds ->
|
||||
val messages = messageIds.mapNotNull(messageRepo::getMessage).sortedBy { it.date }
|
||||
ClipboardUtils.copy(
|
||||
context,
|
||||
messages.foldIndexed(StringBuilder()) { index, acc, message ->
|
||||
when {
|
||||
index == 0 ->
|
||||
acc.append(message.getText())
|
||||
messages[index - 1].compareSender(message) ->
|
||||
acc.appendLine().append(message.getText())
|
||||
else ->
|
||||
acc.appendLine().appendLine().append(message.getText())
|
||||
}
|
||||
}.toString())
|
||||
messageIds
|
||||
.mapNotNull(messageRepo::getMessage)
|
||||
.sortedBy { it.date }
|
||||
.getText()
|
||||
)
|
||||
}
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.clearSelection() }
|
||||
|
||||
// share the message text contents
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.share }
|
||||
.observeOn(Schedulers.io())
|
||||
.withLatestFrom(view.messagesSelectedIntent) { _, messageIds -> messageIds }
|
||||
.mapNotNull { messageIds ->
|
||||
val filename = "$SAVED_MESSAGE_TEXT_FILE_PREFIX${
|
||||
SimpleDateFormat(
|
||||
"yyyy-MM-dd-HH-mm-ss",
|
||||
Locale.getDefault()
|
||||
).format(System.currentTimeMillis())}.txt"
|
||||
|
||||
val mimeType = "${MimeTypes.BASE_TYPE_TEXT}/plain"
|
||||
|
||||
// save all messages text to a file in cache
|
||||
val (uri, e) = FileUtils.createAndWrite(
|
||||
context,
|
||||
FileUtils.Companion.Location.Cache,
|
||||
filename,
|
||||
mimeType,
|
||||
messageIds
|
||||
.mapNotNull(messageRepo::getMessage)
|
||||
.sortedBy { it.date }
|
||||
.getText()
|
||||
.toByteArray()
|
||||
)
|
||||
|
||||
if (e is Exception)
|
||||
Pair(filename, e)
|
||||
else {
|
||||
// share file from cache
|
||||
navigator.viewFile(
|
||||
FileProvider.getUriForFile(
|
||||
context,
|
||||
"dev.octoshrimpy.quik.messagesText",
|
||||
uri.toFile()
|
||||
),
|
||||
mimeType
|
||||
)
|
||||
|
||||
Pair(filename, null)
|
||||
}
|
||||
}
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.map { (filename, e) ->
|
||||
if (e is Exception)
|
||||
Toast.makeText(
|
||||
context,
|
||||
context.getString(R.string.messages_text_share_file_error),
|
||||
Toast.LENGTH_LONG
|
||||
).show().also {
|
||||
Timber.e("Error writing to messages text cache file", e)
|
||||
}
|
||||
else
|
||||
Timber.d("Created and shared messages text file: $filename", e)
|
||||
}
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.clearSelection() }
|
||||
|
||||
// Show the message details
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.details }
|
||||
@@ -572,14 +627,27 @@ class ComposeViewModel @Inject constructor(
|
||||
|
||||
// Update the State when the message selected count changes
|
||||
view.messagesSelectedIntent
|
||||
.map { selection -> selection.size }
|
||||
.map {
|
||||
Pair(
|
||||
it.size,
|
||||
it.any { messageRepo.getMessage(it)?.hasNonWhitespaceText() ?: false }
|
||||
)
|
||||
}
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { messages -> newState { copy(selectedMessages = messages, editingMode = false) } }
|
||||
.subscribe {
|
||||
newState {
|
||||
copy(
|
||||
selectedMessages = it.first,
|
||||
selectedMessagesHaveText = it.second,
|
||||
editingMode = false
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Cancel sending a message
|
||||
view.cancelSendingIntent
|
||||
.mapNotNull(messageRepo::getMessage)
|
||||
.doOnNext { message -> view.setDraft(message.getText()) }
|
||||
.doOnNext { message -> view.setDraft(message.getText(false)) }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { message ->
|
||||
cancelMessage.execute(CancelDelayedMessage.Params(message.id, message.threadId))
|
||||
@@ -646,7 +714,15 @@ class ComposeViewModel @Inject constructor(
|
||||
.observeOn(Schedulers.io())
|
||||
.withLatestFrom(view.textChangedIntent, state) { threadId, draftText, state ->
|
||||
if (state.saveDraft)
|
||||
conversationRepo.saveDraft(threadId, draftText.toString())
|
||||
conversationRepo.saveDraft(
|
||||
threadId,
|
||||
if (draftText.isNotBlank()) draftText.toString()
|
||||
else ""
|
||||
)
|
||||
|
||||
// remove attachments
|
||||
state.attachments.forEach { it.removeCacheFile() }
|
||||
|
||||
newState { copy(saveDraft = true) }
|
||||
}
|
||||
.autoDisposable(view.scope())
|
||||
@@ -846,7 +922,7 @@ class ComposeViewModel @Inject constructor(
|
||||
// if leaving audio recording mode
|
||||
if (!it.audioMsgRecording) {
|
||||
// ensure recording stopped and delete any recording file
|
||||
safeDeleteLocalFile(MediaRecorderManager.stopRecording())
|
||||
FileUtils.deleteFile(MediaRecorderManager.stopRecording())
|
||||
view.recordAudioStartStopRecording.onNext(false)
|
||||
}
|
||||
}
|
||||
@@ -925,7 +1001,7 @@ class ComposeViewModel @Inject constructor(
|
||||
view.recordAudioStartStopRecording.onNext(false) // stop recording
|
||||
view.recordAudioPlayerVisible.onNext(true) // show audio player
|
||||
} else { // state = start recording
|
||||
safeDeleteLocalFile(MediaRecorderManager.uri) // delete old recording file
|
||||
FileUtils.deleteFile(MediaRecorderManager.uri) // delete old recording file
|
||||
view.recordAudioStartStopRecording.onNext(true) // start new recording
|
||||
}
|
||||
}
|
||||
@@ -937,21 +1013,25 @@ class ComposeViewModel @Inject constructor(
|
||||
MediaRecorderManager.stopRecording()
|
||||
|
||||
try {
|
||||
// create new filename for recorded file (so cache clean doesn't accidentally
|
||||
// delete the attachment file)
|
||||
val newFile = File(
|
||||
context.cacheDir,
|
||||
"${AUDIO_FILE_PREFIX}${UUID.randomUUID()}${MediaRecorderManager.AUDIO_FILE_SUFFIX}"
|
||||
// create new filename for recorded file because leaving the recording ui
|
||||
// will delete the original filename as a catch-all to not leave orphaned files
|
||||
val (newUri, e) = FileUtils.create(
|
||||
FileUtils.Companion.Location.Cache,
|
||||
context,
|
||||
"$AUDIO_FILE_PREFIX-${UUID.randomUUID()}$AUDIO_FILE_SUFFIX",
|
||||
""
|
||||
)
|
||||
if (e is Exception)
|
||||
throw e
|
||||
|
||||
// rename recorded file to new name
|
||||
MediaRecorderManager.uri.toFile().renameTo(newFile)
|
||||
FileUtils.renameTo(MediaRecorderManager.uri, newUri)
|
||||
|
||||
// attach newly named file to message
|
||||
newState {
|
||||
copy(
|
||||
audioMsgRecording = false,
|
||||
attachments = attachments + Attachment(context, newFile.toUri())
|
||||
attachments = attachments + Attachment(context, newUri)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1133,6 +1213,11 @@ class ComposeViewModel @Inject constructor(
|
||||
// clear the current message schedule, text and attachments
|
||||
view.clearCurrentMessageIntent
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.withLatestFrom(state) { hasError, state ->
|
||||
// remove attachments
|
||||
state.attachments.forEach { it.removeCacheFile() }
|
||||
hasError
|
||||
}
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe {
|
||||
view.setDraft("")
|
||||
@@ -1147,13 +1232,4 @@ class ComposeViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun safeDeleteLocalFile(uri: Uri): Boolean {
|
||||
return try {
|
||||
if (uri == Uri.EMPTY)
|
||||
false
|
||||
uri.toFile().delete()
|
||||
}
|
||||
catch (e: Exception) { false }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,9 +37,11 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.ProgressBar
|
||||
import androidx.core.net.toUri
|
||||
import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.moez.QKSMS.common.QkMediaPlayer
|
||||
import dev.octoshrimpy.quik.R
|
||||
import dev.octoshrimpy.quik.common.Navigator
|
||||
import dev.octoshrimpy.quik.common.base.QkRealmAdapter
|
||||
import dev.octoshrimpy.quik.common.base.QkViewHolder
|
||||
import dev.octoshrimpy.quik.common.util.Colors
|
||||
@@ -54,6 +56,9 @@ import dev.octoshrimpy.quik.common.util.extensions.withAlpha
|
||||
import dev.octoshrimpy.quik.compat.SubscriptionManagerCompat
|
||||
import dev.octoshrimpy.quik.extensions.isSmil
|
||||
import dev.octoshrimpy.quik.extensions.isText
|
||||
import dev.octoshrimpy.quik.extensions.joinTo
|
||||
import dev.octoshrimpy.quik.extensions.millisecondsToMinutes
|
||||
import dev.octoshrimpy.quik.extensions.truncateWithEllipses
|
||||
import dev.octoshrimpy.quik.feature.compose.BubbleUtils.canGroup
|
||||
import dev.octoshrimpy.quik.feature.compose.BubbleUtils.getBubble
|
||||
import dev.octoshrimpy.quik.feature.compose.part.PartsAdapter
|
||||
@@ -89,9 +94,9 @@ class MessagesAdapter @Inject constructor(
|
||||
private val partsAdapterProvider: Provider<PartsAdapter>,
|
||||
private val phoneNumberUtils: PhoneNumberUtils,
|
||||
private val prefs: Preferences,
|
||||
private val textViewStyler: TextViewStyler
|
||||
private val textViewStyler: TextViewStyler,
|
||||
private val navigator: Navigator,
|
||||
) : QkRealmAdapter<Message>() {
|
||||
|
||||
class AudioState(
|
||||
var partId: Long = -1,
|
||||
var state: QkMediaPlayer.PlayingState = QkMediaPlayer.PlayingState.Stopped,
|
||||
@@ -103,10 +108,30 @@ class MessagesAdapter @Inject constructor(
|
||||
private const val VIEW_TYPE_MESSAGE_IN = 0
|
||||
private const val VIEW_TYPE_MESSAGE_OUT = 1
|
||||
|
||||
private const val MAX_MESSAGE_DISPLAY_LENGTH = 5000
|
||||
|
||||
// Thanks to Cory Kilger for this regex
|
||||
// https://gist.github.com/cmkilger/b8f7dba3e76244a84e7e
|
||||
private val EMOJI_REGEX = Regex(
|
||||
"^[\\s\n\r]*(?:(?:[\u00a9\u00ae\u203c\u2049\u2122\u2139\u2194-\u2199\u21a9-\u21aa\u231a-\u231b\u2328\u23cf\u23e9-\u23f3\u23f8-\u23fa\u24c2\u25aa-\u25ab\u25b6\u25c0\u25fb-\u25fe\u2600-\u2604\u260e\u2611\u2614-\u2615\u2618\u261d\u2620\u2622-\u2623\u2626\u262a\u262e-\u262f\u2638-\u263a\u2648-\u2653\u2660\u2663\u2665-\u2666\u2668\u267b\u267f\u2692-\u2694\u2696-\u2697\u2699\u269b-\u269c\u26a0-\u26a1\u26aa-\u26ab\u26b0-\u26b1\u26bd-\u26be\u26c4-\u26c5\u26c8\u26ce-\u26cf\u26d1\u26d3-\u26d4\u26e9-\u26ea\u26f0-\u26f5\u26f7-\u26fa\u26fd\u2702\u2705\u2708-\u270d\u270f\u2712\u2714\u2716\u271d\u2721\u2728\u2733-\u2734\u2744\u2747\u274c\u274e\u2753-\u2755\u2757\u2763-\u2764\u2795-\u2797\u27a1\u27b0\u27bf\u2934-\u2935\u2b05-\u2b07\u2b1b-\u2b1c\u2b50\u2b55\u3030\u303d\u3297\u3299\ud83c\udc04\ud83c\udccf\ud83c\udd70-\ud83c\udd71\ud83c\udd7e-\ud83c\udd7f\ud83c\udd8e\ud83c\udd91-\ud83c\udd9a\ud83c\ude01-\ud83c\ude02\ud83c\ude1a\ud83c\ude2f\ud83c\ude32-\ud83c\ude3a\ud83c\ude50-\ud83c\ude51\u200d\ud83c\udf00-\ud83d\uddff\ud83d\ude00-\ud83d\ude4f\ud83d\ude80-\ud83d\udeff\ud83e\udd00-\ud83e\uddff\udb40\udc20-\udb40\udc7f]|\u200d[\u2640\u2642]|[\ud83c\udde6-\ud83c\uddff]{2}|.[\u20e0\u20e3\ufe0f]+)+[\\s\n\r]*)+$")
|
||||
"^[\\s\n\r]*(?:(?:[\u00a9\u00ae\u203c\u2049\u2122\u2139\u2194-\u2199" +
|
||||
"\u21a9-\u21aa\u231a-\u231b\u2328\u23cf\u23e9-\u23f3\u23f8-\u23fa\u24c2" +
|
||||
"\u25aa-\u25ab\u25b6\u25c0\u25fb-\u25fe\u2600-\u2604\u260e\u2611\u2614-" +
|
||||
"\u2615\u2618\u261d\u2620\u2622-\u2623\u2626\u262a\u262e-\u262f\u2638-" +
|
||||
"\u263a\u2648-\u2653\u2660\u2663\u2665-\u2666\u2668\u267b\u267f\u2692-" +
|
||||
"\u2694\u2696-\u2697\u2699\u269b-\u269c\u26a0-\u26a1\u26aa-\u26ab\u26b0-" +
|
||||
"\u26b1\u26bd-\u26be\u26c4-\u26c5\u26c8\u26ce-\u26cf\u26d1\u26d3-\u26d4" +
|
||||
"\u26e9-\u26ea\u26f0-\u26f5\u26f7-\u26fa\u26fd\u2702\u2705\u2708-\u270d" +
|
||||
"\u270f\u2712\u2714\u2716\u271d\u2721\u2728\u2733-\u2734\u2744\u2747\u274c" +
|
||||
"\u274e\u2753-\u2755\u2757\u2763-\u2764\u2795-\u2797\u27a1\u27b0\u27bf" +
|
||||
"\u2934-\u2935\u2b05-\u2b07\u2b1b-\u2b1c\u2b50\u2b55\u3030\u303d\u3297" +
|
||||
"\u3299\ud83c\udc04\ud83c\udccf\ud83c\udd70-\ud83c\udd71\ud83c\udd7e-" +
|
||||
"\ud83c\udd7f\ud83c\udd8e\ud83c\udd91-\ud83c\udd9a\ud83c\ude01-\ud83c" +
|
||||
"\ude02\ud83c\ude1a\ud83c\ude2f\ud83c\ude32-\ud83c\ude3a\ud83c\ude50-" +
|
||||
"\ud83c\ude51\u200d\ud83c\udf00-\ud83d\uddff\ud83d\ude00-\ud83d\ude4f" +
|
||||
"\ud83d\ude80-\ud83d\udeff\ud83e\udd00-\ud83e\uddff\udb40\udc20-\udb40" +
|
||||
"\udc7f]|\u200d[\u2640\u2642]|[\ud83c\udde6-\ud83c\uddff]" +
|
||||
"{2}|.[\u20e0\u20e3\ufe0f]+)+[\\s\n\r]*)+$"
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
@@ -143,20 +168,18 @@ class MessagesAdapter @Inject constructor(
|
||||
private val audioState = AudioState()
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
|
||||
|
||||
// Use the parent's context to inflate the layout, otherwise link clicks will crash the app
|
||||
val layoutInflater = LayoutInflater.from(parent.context)
|
||||
val view: View
|
||||
val inflater = LayoutInflater.from(parent.context)
|
||||
|
||||
if (viewType == VIEW_TYPE_MESSAGE_OUT) {
|
||||
view = layoutInflater.inflate(R.layout.message_list_item_out, parent, false)
|
||||
view.findViewById<ImageView>(R.id.cancelIcon).setTint(theme.theme)
|
||||
view.findViewById<ProgressBar>(R.id.cancel).setTint(theme.theme)
|
||||
view.findViewById<ImageView>(R.id.sendNowIcon).setTint(theme.theme)
|
||||
view.findViewById<ImageView>(R.id.resendIcon).setTint(theme.theme)
|
||||
} else {
|
||||
view = layoutInflater.inflate(R.layout.message_list_item_in, parent, false)
|
||||
}
|
||||
val view = if (viewType == VIEW_TYPE_MESSAGE_OUT) {
|
||||
inflater.inflate(R.layout.message_list_item_out, parent,false).apply {
|
||||
findViewById<ImageView>(R.id.cancelIcon).setTint(theme.theme)
|
||||
findViewById<ProgressBar>(R.id.cancel).setTint(theme.theme)
|
||||
findViewById<ImageView>(R.id.sendNowIcon).setTint(theme.theme)
|
||||
findViewById<ImageView>(R.id.resendIcon).setTint(theme.theme)
|
||||
}
|
||||
} else
|
||||
inflater.inflate(R.layout.message_list_item_in, parent, false)
|
||||
|
||||
view.body.hyphenationFrequency = Layout.HYPHENATION_FREQUENCY_NONE
|
||||
|
||||
@@ -165,19 +188,21 @@ class MessagesAdapter @Inject constructor(
|
||||
|
||||
return QkViewHolder(view).apply {
|
||||
view.setOnClickListener {
|
||||
val message = getItem(adapterPosition) ?: return@setOnClickListener
|
||||
when (toggleSelection(message.id, false)) {
|
||||
true -> view.isActivated = isSelected(message.id)
|
||||
false -> {
|
||||
expanded[message.id] = view.status.visibility != View.VISIBLE
|
||||
notifyItemChanged(adapterPosition)
|
||||
getItem(adapterPosition)?.let {
|
||||
when (toggleSelection(it.id, false)) {
|
||||
true -> view.isActivated = isSelected(it.id)
|
||||
false -> {
|
||||
expanded[it.id] = view.status.visibility != View.VISIBLE
|
||||
notifyItemChanged(adapterPosition)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
view.setOnLongClickListener {
|
||||
val message = getItem(adapterPosition) ?: return@setOnLongClickListener true
|
||||
toggleSelection(message.id)
|
||||
view.isActivated = isSelected(message.id)
|
||||
getItem(adapterPosition)?.let {
|
||||
toggleSelection(it.id)
|
||||
view.isActivated = isSelected(it.id)
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -193,208 +218,246 @@ class MessagesAdapter @Inject constructor(
|
||||
false -> colors.theme(contactCache[message.address])
|
||||
}
|
||||
|
||||
holder.parts.adapter = partsAdapterProvider.get().apply {
|
||||
contextMenuValue = message.id
|
||||
clicks.subscribe(partClicks) // part clicks gets passed back to compose view model
|
||||
}
|
||||
|
||||
// Update the selected state
|
||||
holder.containerView.isActivated = isSelected(message.id) || highlight == message.id
|
||||
|
||||
// Bind the cancelFrame (cancel button) view
|
||||
if (holder.cancelFrame != null) {
|
||||
holder.cancelFrame.let { cancelFrame ->
|
||||
val isCancellable = message.isSending() && message.date > System.currentTimeMillis()
|
||||
cancelFrame.visibility = if (isCancellable) View.VISIBLE else View.GONE
|
||||
cancelFrame.clicks().subscribe { cancelSendingClicks.onNext(message.id) }
|
||||
cancelFrame.cancel.progress = 2
|
||||
holder.cancelFrame?.let {
|
||||
val isCancellable = message.isSending() && message.date > System.currentTimeMillis()
|
||||
it.visibility = if (isCancellable) View.VISIBLE else View.GONE
|
||||
it.let {
|
||||
it.clicks().subscribe { cancelSendingClicks.onNext(message.id) }
|
||||
}
|
||||
it.cancel.progress = 2
|
||||
|
||||
if (isCancellable) {
|
||||
val delay = when (prefs.sendDelay.get()) {
|
||||
Preferences.SEND_DELAY_SHORT -> 3000
|
||||
Preferences.SEND_DELAY_MEDIUM -> 5000
|
||||
Preferences.SEND_DELAY_LONG -> 10000
|
||||
else -> 0
|
||||
}
|
||||
val progress =
|
||||
(1 - (message.date - System.currentTimeMillis()) / delay.toFloat()) * 100
|
||||
|
||||
ObjectAnimator.ofInt(cancelFrame.cancel, "progress", progress.toInt(), 100)
|
||||
.setDuration(message.date - System.currentTimeMillis())
|
||||
.start()
|
||||
if (isCancellable) {
|
||||
val delay = when (prefs.sendDelay.get()) {
|
||||
Preferences.SEND_DELAY_SHORT -> 3000
|
||||
Preferences.SEND_DELAY_MEDIUM -> 5000
|
||||
Preferences.SEND_DELAY_LONG -> 10000
|
||||
else -> 0
|
||||
}
|
||||
val progress =
|
||||
(1 - (message.date - System.currentTimeMillis()) / delay.toFloat()) * 100
|
||||
|
||||
ObjectAnimator.ofInt(it.cancel, "progress", progress.toInt(), 100)
|
||||
.setDuration(message.date - System.currentTimeMillis())
|
||||
.start()
|
||||
}
|
||||
}
|
||||
|
||||
// bind the send now icon view
|
||||
if (holder.sendNowIcon != null) {
|
||||
holder.sendNowIcon.let { sendNowIcon ->
|
||||
if (message.isSending() && message.date > System.currentTimeMillis()) {
|
||||
sendNowIcon.visibility = View.VISIBLE
|
||||
sendNowIcon.clicks().subscribe { sendNowClicks.onNext(message.id) }
|
||||
} else
|
||||
sendNowIcon.visibility = View.GONE
|
||||
}
|
||||
holder.sendNowIcon?.let {
|
||||
if (message.isSending() && message.date > System.currentTimeMillis()) {
|
||||
it.visibility = View.VISIBLE
|
||||
it.clicks().subscribe { sendNowClicks.onNext(message.id) }
|
||||
} else
|
||||
it.visibility = View.GONE
|
||||
}
|
||||
|
||||
// bind the resend icon view
|
||||
if (holder.resendIcon != null) {
|
||||
holder.resendIcon.let { resendIcon ->
|
||||
if (message.isFailedMessage()) {
|
||||
resendIcon.visibility = View.VISIBLE
|
||||
resendIcon.clicks().subscribe {
|
||||
resendClicks.onNext(message.id)
|
||||
resendIcon.visibility = View.GONE
|
||||
}
|
||||
} else
|
||||
holder.resendIcon?.let { resendIcon ->
|
||||
if (message.isFailedMessage()) {
|
||||
resendIcon.visibility = View.VISIBLE
|
||||
resendIcon.clicks().subscribe {
|
||||
resendClicks.onNext(message.id)
|
||||
resendIcon.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
} else
|
||||
resendIcon.visibility = View.GONE
|
||||
}
|
||||
|
||||
val subject = message.getCleansedSubject()
|
||||
|
||||
var isMsgTextTruncated = false
|
||||
|
||||
// get message text to display, which may need to be truncated
|
||||
val displayText = subject.joinTo(message.getText(false), "\n").let {
|
||||
isMsgTextTruncated = (it.length > MAX_MESSAGE_DISPLAY_LENGTH)
|
||||
|
||||
// make subject sub-string bold, if subject is not blank
|
||||
if (subject.isNotBlank())
|
||||
SpannableString(it.truncateWithEllipses(MAX_MESSAGE_DISPLAY_LENGTH)).apply {
|
||||
setSpan(
|
||||
StyleSpan(Typeface.BOLD),
|
||||
0,
|
||||
subject.length,
|
||||
Spannable.SPAN_INCLUSIVE_EXCLUSIVE
|
||||
)
|
||||
}
|
||||
else
|
||||
it.truncateWithEllipses(MAX_MESSAGE_DISPLAY_LENGTH)
|
||||
}
|
||||
|
||||
// Bind the message status
|
||||
bindStatus(holder, message, next)
|
||||
bindStatus(holder, isMsgTextTruncated, message, next)
|
||||
|
||||
// Bind the timestamp
|
||||
val timeSincePrevious = TimeUnit.MILLISECONDS.toMinutes(message.date - (previous?.date ?: 0))
|
||||
val subscription = subs.find { sub -> sub.subscriptionId == message.subId }
|
||||
val subscription = subs.find { it.subscriptionId == message.subId }
|
||||
|
||||
holder.timestamp.apply {
|
||||
text = dateFormatter.getMessageTimestamp(message.date)
|
||||
setVisible(
|
||||
((message.date - (previous?.date ?: 0))
|
||||
.millisecondsToMinutes() >= BubbleUtils.TIMESTAMP_THRESHOLD) ||
|
||||
(message.subId != previous?.subId) &&
|
||||
(subscription != null)
|
||||
)
|
||||
}
|
||||
|
||||
holder.timestamp.text = dateFormatter.getMessageTimestamp(message.date)
|
||||
holder.simIndex.text = subscription?.simSlotIndex?.plus(1)?.toString()
|
||||
|
||||
holder.timestamp.setVisible(timeSincePrevious >= BubbleUtils.TIMESTAMP_THRESHOLD
|
||||
|| message.subId != previous?.subId && subscription != null)
|
||||
|
||||
holder.sim.setVisible(message.subId != previous?.subId && subscription != null && subs.size > 1)
|
||||
holder.simIndex.setVisible(message.subId != previous?.subId && subscription != null && subs.size > 1)
|
||||
((message.subId != previous?.subId) && (subscription != null) && (subs.size > 1)).also {
|
||||
holder.sim.setVisible(it)
|
||||
holder.simIndex.setVisible(it)
|
||||
}
|
||||
|
||||
// Bind the grouping
|
||||
val media = message.parts.filter { !it.isSmil() && !it.isText() }
|
||||
holder.containerView.setPadding(bottom = if (canGroup(message, next)) 0 else 16.dpToPx(context))
|
||||
holder.containerView.setPadding(
|
||||
bottom = if (canGroup(message, next)) 0 else 16.dpToPx(context)
|
||||
)
|
||||
|
||||
// Bind the avatar and bubble colour
|
||||
if (!message.isMe()) {
|
||||
holder.avatar.setRecipient(contactCache[message.address])
|
||||
holder.avatar.setVisible(!canGroup(message, next), View.INVISIBLE)
|
||||
|
||||
holder.body.setTextColor(theme.textPrimary)
|
||||
holder.body.setBackgroundTint(theme.theme)
|
||||
holder.body.highlightColor = R.attr.bubbleColor.withAlpha(0x5d)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
holder.body.textSelectHandle?.setTint(R.attr.bubbleColor.withAlpha(0x7d))
|
||||
holder.body.textSelectHandleLeft?.setTint(R.attr.bubbleColor.withAlpha(0x7d))
|
||||
holder.body.textSelectHandleRight?.setTint(R.attr.bubbleColor.withAlpha(0x7d))
|
||||
holder.avatar.apply {
|
||||
setRecipient(contactCache[message.address])
|
||||
setVisible(!canGroup(message, next), View.INVISIBLE)
|
||||
}
|
||||
} else {
|
||||
holder.body.highlightColor = theme.theme.withAlpha(0x5d)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
holder.body.textSelectHandle?.setTint(theme.theme.withAlpha(0xad))
|
||||
holder.body.textSelectHandleLeft?.setTint(theme.theme.withAlpha(0xad))
|
||||
holder.body.textSelectHandleRight?.setTint(theme.theme.withAlpha(0xad))
|
||||
}
|
||||
}
|
||||
|
||||
// Bind the body text
|
||||
val messageText = when (message.isSms()) {
|
||||
true -> message.body
|
||||
false -> {
|
||||
val subject = message.getCleansedSubject()
|
||||
val body = message.parts
|
||||
.filter { part -> part.isText() }
|
||||
.mapNotNull { part -> part.text }
|
||||
.filter { text -> text.isNotBlank() }
|
||||
.joinToString("\n")
|
||||
|
||||
when {
|
||||
subject.isNotBlank() -> {
|
||||
val spannable = SpannableString(if (body.isNotBlank()) "$subject\n$body" else subject)
|
||||
spannable.setSpan(StyleSpan(Typeface.BOLD), 0, subject.length,
|
||||
Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
|
||||
spannable
|
||||
}
|
||||
else -> body
|
||||
holder.body.apply {
|
||||
setTextColor(theme.textPrimary)
|
||||
setBackgroundTint(theme.theme)
|
||||
highlightColor = R.attr.bubbleColor.withAlpha(0x5d)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
textSelectHandle?.setTint(R.attr.bubbleColor.withAlpha(0x7d))
|
||||
textSelectHandleLeft?.setTint(R.attr.bubbleColor.withAlpha(0x7d))
|
||||
textSelectHandleRight?.setTint(R.attr.bubbleColor.withAlpha(0x7d))
|
||||
}
|
||||
}
|
||||
} else
|
||||
holder.body.apply {
|
||||
highlightColor = theme.theme.withAlpha(0x5d)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
textSelectHandle?.setTint(theme.theme.withAlpha(0xad))
|
||||
textSelectHandleLeft?.setTint(theme.theme.withAlpha(0xad))
|
||||
textSelectHandleRight?.setTint(theme.theme.withAlpha(0xad))
|
||||
}
|
||||
}
|
||||
}
|
||||
val emojiOnly = messageText.isNotBlank() && messageText.matches(EMOJI_REGEX)
|
||||
textViewStyler.setTextSize(holder.body, when (emojiOnly) {
|
||||
true -> TextViewStyler.SIZE_EMOJI
|
||||
false -> TextViewStyler.SIZE_PRIMARY
|
||||
})
|
||||
|
||||
val spanString = SpannableStringBuilder(messageText)
|
||||
// Bind the body text
|
||||
val emojiOnly = (displayText.isNotBlank()) &&
|
||||
(displayText.matches(EMOJI_REGEX))
|
||||
textViewStyler.setTextSize(
|
||||
holder.body,
|
||||
when (emojiOnly) {
|
||||
true -> TextViewStyler.SIZE_EMOJI
|
||||
false -> TextViewStyler.SIZE_PRIMARY
|
||||
}
|
||||
)
|
||||
|
||||
val spanString = SpannableStringBuilder(displayText)
|
||||
|
||||
when (prefs.messageLinkHandling.get()) {
|
||||
Preferences.MESSAGE_LINK_HANDLING_BLOCK -> holder.body.autoLinkMask = 0
|
||||
Preferences.MESSAGE_LINK_HANDLING_ASK -> {
|
||||
// manually handle link clicks if user has set to ask before opening links
|
||||
holder.body.isClickable = false
|
||||
holder.body.linksClickable = false
|
||||
holder.body.movementMethod = LinkMovementMethod.getInstance()
|
||||
holder.body.apply {
|
||||
isClickable = false
|
||||
linksClickable = false
|
||||
movementMethod = LinkMovementMethod.getInstance()
|
||||
|
||||
Linkify.addLinks(spanString, holder.body.autoLinkMask)
|
||||
Linkify.addLinks(spanString, autoLinkMask)
|
||||
}
|
||||
|
||||
for (span in spanString.getSpans(
|
||||
0,
|
||||
spanString.length,
|
||||
URLSpan::class.java)
|
||||
) {
|
||||
// set handler for when user touches a link into new span
|
||||
spanString.setSpan(
|
||||
object : ClickableSpan() {
|
||||
override fun onClick(widget: View) {
|
||||
messageLinkClicks.onNext(Uri.parse(span.url))
|
||||
}
|
||||
},
|
||||
spanString.getSpanStart(span),
|
||||
spanString.getSpanEnd(span),
|
||||
spanString.getSpanFlags(span)
|
||||
)
|
||||
spanString.apply {
|
||||
for (span in getSpans(0, length, URLSpan::class.java)) {
|
||||
// set handler for when user touches a link into new span
|
||||
setSpan(
|
||||
object : ClickableSpan() {
|
||||
override fun onClick(widget: View) {
|
||||
messageLinkClicks.onNext(span.url.toUri())
|
||||
}
|
||||
},
|
||||
getSpanStart(span),
|
||||
getSpanEnd(span),
|
||||
getSpanFlags(span)
|
||||
)
|
||||
|
||||
// remove original span
|
||||
spanString.removeSpan(span)
|
||||
// remove original span
|
||||
removeSpan(span)
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> holder.body.movementMethod = LinkMovementMethod.getInstance()
|
||||
}
|
||||
|
||||
holder.body.text = spanString
|
||||
holder.body.setVisible(message.isSms() || spanString.isNotBlank())
|
||||
holder.body.apply {
|
||||
text = spanString
|
||||
setVisible(message.isSms() || spanString.isNotBlank())
|
||||
|
||||
holder.body.setBackgroundResource(getBubble(
|
||||
emojiOnly = emojiOnly,
|
||||
canGroupWithPrevious = canGroup(message, previous) || media.isNotEmpty(),
|
||||
canGroupWithNext = canGroup(message, next),
|
||||
isMe = message.isMe()))
|
||||
|
||||
// Bind the parts
|
||||
val partsAdapter = (holder.parts.adapter as PartsAdapter)
|
||||
partsAdapter.theme = theme
|
||||
partsAdapter.setData(message, previous, next, holder, audioState)
|
||||
}
|
||||
|
||||
private fun bindStatus(holder: QkViewHolder, message: Message, next: Message?) {
|
||||
val age = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - message.date)
|
||||
|
||||
holder.status.text = when {
|
||||
message.isSending() -> context.getString(R.string.message_status_sending)
|
||||
message.isDelivered() -> context.getString(R.string.message_status_delivered,
|
||||
dateFormatter.getTimestamp(message.dateSent))
|
||||
message.isFailedMessage() -> context.getString(R.string.message_status_failed)
|
||||
!message.isMe() && (conversation?.recipients?.size ?: 0) > 1 -> { // incoming group message
|
||||
"${contactCache[message.address]?.getDisplayName()} • ${dateFormatter.getTimestamp(message.date)}"
|
||||
}
|
||||
else -> dateFormatter.getTimestamp(message.date)
|
||||
setBackgroundResource(
|
||||
getBubble(
|
||||
emojiOnly = emojiOnly,
|
||||
canGroupWithPrevious = canGroup(message, previous) ||
|
||||
message.parts.any { !it.isSmil() && !it.isText() },
|
||||
canGroupWithNext = canGroup(message, next),
|
||||
isMe = message.isMe()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
holder.status.setVisible(when {
|
||||
expanded[message.id] == true -> true
|
||||
message.isSending() -> true
|
||||
message.isFailedMessage() -> true
|
||||
expanded[message.id] == false -> false
|
||||
(conversation?.recipients?.size ?: 0) > 1 && !message.isMe() && next?.compareSender(message) != true -> true
|
||||
message.isDelivered() && next?.isDelivered() != true && age <= BubbleUtils.TIMESTAMP_THRESHOLD -> true
|
||||
else -> false
|
||||
})
|
||||
// Bind the parts
|
||||
holder.parts.adapter = partsAdapterProvider.get().apply {
|
||||
this.theme = theme
|
||||
setData(message, previous, next, holder, audioState)
|
||||
contextMenuValue = message.id
|
||||
clicks.subscribe(partClicks) // part clicks gets passed back to compose view model
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindStatus(
|
||||
holder: QkViewHolder,
|
||||
bodyTextTruncated: Boolean,
|
||||
message: Message,
|
||||
next: Message?
|
||||
) {
|
||||
holder.status.apply {
|
||||
text = when {
|
||||
message.isSending() -> context.getString(R.string.message_status_sending)
|
||||
message.isDelivered() -> context.getString(
|
||||
R.string.message_status_delivered,
|
||||
dateFormatter.getTimestamp(message.dateSent)
|
||||
)
|
||||
message.isFailedMessage() -> context.getString(R.string.message_status_failed)
|
||||
bodyTextTruncated -> context.getString(R.string.message_body_too_long_to_display)
|
||||
(!message.isMe() && (conversation?.recipients?.size ?: 0) > 1) ->
|
||||
// incoming group message
|
||||
"${contactCache[message.address]?.getDisplayName()} • ${
|
||||
dateFormatter.getTimestamp(message.date)}"
|
||||
else -> dateFormatter.getTimestamp(message.date)
|
||||
}
|
||||
|
||||
val age = TimeUnit.MILLISECONDS.toMinutes(
|
||||
System.currentTimeMillis() - message.date
|
||||
)
|
||||
|
||||
setVisible(
|
||||
when {
|
||||
expanded[message.id] == true -> true
|
||||
message.isSending() -> true
|
||||
message.isFailedMessage() -> true
|
||||
bodyTextTruncated -> true
|
||||
expanded[message.id] == false -> false
|
||||
((conversation?.recipients?.size ?: 0) > 1) &&
|
||||
!message.isMe() && next?.compareSender(message) != true -> true
|
||||
(message.isDelivered() &&
|
||||
(next?.isDelivered() != true) &&
|
||||
(age <= BubbleUtils.TIMESTAMP_THRESHOLD)) -> true
|
||||
|
||||
else -> false
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemId(position: Int): Long {
|
||||
@@ -410,7 +473,7 @@ class MessagesAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
fun expandMessages(messageIds: List<Long>, expand: Boolean) {
|
||||
messageIds.forEach { messageId -> expanded[messageId] = expand }
|
||||
messageIds.forEach { expanded[it] = expand }
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
@@ -419,11 +482,14 @@ class MessagesAdapter @Inject constructor(
|
||||
* a reference to the contact.
|
||||
*/
|
||||
private inner class ContactCache : HashMap<String, Recipient?>() {
|
||||
|
||||
override fun get(key: String): Recipient? {
|
||||
if (super.get(key)?.isValid != true) {
|
||||
set(key, conversation?.recipients?.firstOrNull { phoneNumberUtils.compare(it.address, key) })
|
||||
}
|
||||
if (super.get(key)?.isValid != true)
|
||||
set(
|
||||
key,
|
||||
conversation?.recipients?.firstOrNull {
|
||||
phoneNumberUtils.compare(it.address, key)
|
||||
}
|
||||
)
|
||||
|
||||
return super.get(key)?.takeIf { it.isValid }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user