mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
Merge pull request #391 from gavine99/anr-when-receiving-a-text-380
move sms receive code path to a worker model to avoid anr on message receive
This commit is contained in:
@@ -18,7 +18,6 @@
|
||||
*/
|
||||
package dev.octoshrimpy.quik.interactor
|
||||
|
||||
import android.telephony.SmsMessage
|
||||
import dev.octoshrimpy.quik.blocking.BlockingClient
|
||||
import dev.octoshrimpy.quik.extensions.mapNotNull
|
||||
import dev.octoshrimpy.quik.manager.NotificationManager
|
||||
@@ -38,60 +37,67 @@ class ReceiveSms @Inject constructor(
|
||||
private val notificationManager: NotificationManager,
|
||||
private val updateBadge: UpdateBadge,
|
||||
private val shortcutManager: ShortcutManager
|
||||
) : Interactor<ReceiveSms.Params>() {
|
||||
) : Interactor<Long>() {
|
||||
|
||||
class Params(val subId: Int, val messages: Array<SmsMessage>)
|
||||
|
||||
override fun buildObservable(params: Params): Flowable<*> {
|
||||
override fun buildObservable(params: Long): Flowable<*> {
|
||||
return Flowable.just(params)
|
||||
.filter { it.messages.isNotEmpty() }
|
||||
.mapNotNull {
|
||||
// Don't continue if the sender is blocked
|
||||
val messages = it.messages
|
||||
val address = messages[0].displayOriginatingAddress
|
||||
val action = blockingClient.shouldBlock(address).blockingGet()
|
||||
val shouldDrop = prefs.drop.get()
|
||||
Timber.v("block=$action, drop=$shouldDrop")
|
||||
.mapNotNull { messageRepo.getMessage(it) }
|
||||
.mapNotNull {
|
||||
val action = blockingClient.shouldBlock(it.address).blockingGet()
|
||||
|
||||
// If we should drop the message, don't even save it
|
||||
if (action is BlockingClient.Action.Block && shouldDrop) {
|
||||
when {
|
||||
((action is BlockingClient.Action.Block) && prefs.drop.get()) -> {
|
||||
// blocked and 'drop blocked.' remove from db and don't continue
|
||||
Timber.v("address is blocked and drop blocked is on. dropped")
|
||||
messageRepo.deleteMessages(it.id)
|
||||
return@mapNotNull null
|
||||
}
|
||||
|
||||
val time = messages[0].timestampMillis
|
||||
val body: String = messages
|
||||
.mapNotNull { message -> message.displayMessageBody }
|
||||
.reduce { body, new -> body + new }
|
||||
|
||||
// Add the message to the db
|
||||
val message = messageRepo.insertReceivedSms(it.subId, address, body, time)
|
||||
|
||||
when (action) {
|
||||
is BlockingClient.Action.Block -> {
|
||||
messageRepo.markRead(message.threadId)
|
||||
conversationRepo.markBlocked(listOf(message.threadId), prefs.blockingManager.get(), action.reason)
|
||||
}
|
||||
is BlockingClient.Action.Unblock -> conversationRepo.markUnblocked(message.threadId)
|
||||
else -> Unit
|
||||
action is BlockingClient.Action.Block -> {
|
||||
// blocked
|
||||
Timber.v("address is blocked")
|
||||
messageRepo.markRead(it.threadId)
|
||||
conversationRepo.markBlocked(
|
||||
listOf(it.threadId),
|
||||
prefs.blockingManager.get(),
|
||||
action.reason
|
||||
)
|
||||
}
|
||||
action is BlockingClient.Action.Unblock -> {
|
||||
// unblock
|
||||
Timber.v("unblock conversation if blocked")
|
||||
conversationRepo.markUnblocked(it.threadId)
|
||||
}
|
||||
}
|
||||
|
||||
message
|
||||
// update and fetch conversation
|
||||
conversationRepo.updateConversations(it.threadId)
|
||||
conversationRepo.getOrCreateConversation(it.threadId)
|
||||
}
|
||||
.mapNotNull {
|
||||
// don't notify (continue) for blocked conversations
|
||||
if (it.blocked) {
|
||||
Timber.v("no notifications for blocked")
|
||||
return@mapNotNull null
|
||||
}
|
||||
.doOnNext { message ->
|
||||
conversationRepo.updateConversations(message.threadId) // Update the conversation
|
||||
|
||||
// unarchive conversation if necessary
|
||||
if (it.archived) {
|
||||
Timber.v("conversation unarchived")
|
||||
conversationRepo.markUnarchived(it.id)
|
||||
}
|
||||
.mapNotNull { message ->
|
||||
conversationRepo.getOrCreateConversation(message.threadId) // Map message to conversation
|
||||
}
|
||||
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
|
||||
.doOnNext { conversation ->
|
||||
// Unarchive conversation if necessary
|
||||
if (conversation.archived) conversationRepo.markUnarchived(conversation.id)
|
||||
}
|
||||
.map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread
|
||||
.doOnNext { threadId -> notificationManager.update(threadId) } // Update the notification
|
||||
.doOnNext { shortcutManager.updateShortcuts() } // Update shortcuts
|
||||
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge and widget
|
||||
|
||||
// update/create notification
|
||||
Timber.v("update/create notification")
|
||||
notificationManager.update(it.id)
|
||||
|
||||
// update shortcuts
|
||||
Timber.v("update shortcuts")
|
||||
shortcutManager.updateShortcuts()
|
||||
|
||||
// update the badge and widget
|
||||
Timber.v("update badge and widget")
|
||||
updateBadge.buildObservable(Unit)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user