forked from upstream-mirrors/quik
Tap to retry sending failed SMS
This commit is contained in:
@@ -18,16 +18,21 @@
|
||||
*/
|
||||
package data.repository
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.ContentUris
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.BaseColumns
|
||||
import android.provider.Telephony
|
||||
import android.provider.Telephony.Sms
|
||||
import android.telephony.PhoneNumberUtils
|
||||
import android.telephony.SmsManager
|
||||
import com.klinker.android.send_message.StripAccents
|
||||
import common.util.Keys
|
||||
import common.util.MessageUtils
|
||||
import common.util.Preferences
|
||||
import common.util.extensions.*
|
||||
import data.mapper.CursorToConversation
|
||||
import data.mapper.CursorToRecipient
|
||||
@@ -43,6 +48,8 @@ import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmResults
|
||||
import io.realm.Sort
|
||||
import presentation.receiver.MessageDeliveredReceiver
|
||||
import presentation.receiver.MessageSentReceiver
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -51,7 +58,8 @@ class MessageRepository @Inject constructor(
|
||||
private val context: Context,
|
||||
private val messageIds: Keys,
|
||||
private val cursorToConversation: CursorToConversation,
|
||||
private val cursorToRecipient: CursorToRecipient) {
|
||||
private val cursorToRecipient: CursorToRecipient,
|
||||
private val prefs: Preferences) {
|
||||
|
||||
fun getConversations(archived: Boolean = false): Flowable<List<InboxItem>> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
@@ -244,6 +252,36 @@ class MessageRepository @Inject constructor(
|
||||
realm.close()
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists the SMS and attempts to send it
|
||||
*/
|
||||
fun sendSmsAndPersist(threadId: Long, address: String, body: String) {
|
||||
val message = insertSentSms(threadId, address, body)
|
||||
sendSms(message)
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to send the SMS message. This can be called if the message has already been persisted
|
||||
*/
|
||||
fun sendSms(message: Message) {
|
||||
val smsManager = SmsManager.getDefault()
|
||||
|
||||
val parts = smsManager.divideMessage(if (prefs.unicode.get()) StripAccents.stripAccents(message.body) else message.body)
|
||||
|
||||
val sentIntents = parts.map {
|
||||
val intent = Intent(context, MessageSentReceiver::class.java).putExtra("id", message.id)
|
||||
PendingIntent.getBroadcast(context, message.id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
}
|
||||
|
||||
val deliveredIntents = parts.map {
|
||||
val intent = Intent(context, MessageDeliveredReceiver::class.java).putExtra("id", message.id)
|
||||
val pendingIntent = PendingIntent.getBroadcast(context, message.id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
if (prefs.delivery.get()) pendingIntent else null
|
||||
}
|
||||
|
||||
smsManager.sendMultipartTextMessage(message.address, null, parts, ArrayList(sentIntents), ArrayList(deliveredIntents))
|
||||
}
|
||||
|
||||
fun insertSentSms(threadId: Long, address: String, body: String): Message {
|
||||
|
||||
// Insert the message to Realm
|
||||
@@ -318,6 +356,34 @@ class MessageRepository @Inject constructor(
|
||||
return message
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the message as sending, in case we need to retry sending it
|
||||
*/
|
||||
fun markSending(id: Long): Flowable<*> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
return realm.where(Message::class.java).equalTo("id", id)
|
||||
.findAllAsync()
|
||||
.asFlowable()
|
||||
.filter { it.isLoaded }
|
||||
.filter { it.isValid }
|
||||
.mapNotNull { it[0] }
|
||||
.take(1)
|
||||
.doOnNext { message ->
|
||||
// Update the message in realm
|
||||
realm.executeTransaction {
|
||||
message.boxId = Sms.MESSAGE_TYPE_OUTBOX
|
||||
}
|
||||
}
|
||||
.map { message -> message.getUri() }
|
||||
.observeOn(Schedulers.computation())
|
||||
.doOnNext { uri ->
|
||||
// Update the message in the native ContentProvider
|
||||
val values = ContentValues()
|
||||
values.put(Sms.TYPE, Sms.MESSAGE_TYPE_OUTBOX)
|
||||
context.contentResolver.update(uri, values, null, null)
|
||||
}
|
||||
}
|
||||
|
||||
fun markSent(id: Long): Flowable<*> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
return realm.where(Message::class.java).equalTo("id", id)
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package interactor
|
||||
|
||||
import data.model.Message
|
||||
import data.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import javax.inject.Inject
|
||||
|
||||
class RetrySending @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Message, Message>() {
|
||||
|
||||
override fun buildObservable(params: Message): Flowable<Message> {
|
||||
|
||||
// We don't want to touch the supplied message on another thread in case it's a live realm
|
||||
// object, so copy the required fields into a new object that is safe to pass around threads
|
||||
val message = Message().apply {
|
||||
id = params.id
|
||||
type = params.type
|
||||
threadId = params.threadId
|
||||
address = params.address
|
||||
body = params.body
|
||||
}
|
||||
|
||||
return Flowable.just(message)
|
||||
.filter { message.isSms() } // TODO support resending failed MMS
|
||||
.doOnNext { messageRepo.markSending(message.id) }
|
||||
.subscribeOn(AndroidSchedulers.mainThread())
|
||||
.observeOn(Schedulers.io())
|
||||
.doOnNext { messageRepo.sendSms(message) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,22 +18,16 @@
|
||||
*/
|
||||
package interactor
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Bitmap
|
||||
import android.net.Uri
|
||||
import android.telephony.SmsManager
|
||||
import com.klinker.android.send_message.Message
|
||||
import com.klinker.android.send_message.Settings
|
||||
import com.klinker.android.send_message.StripAccents
|
||||
import com.klinker.android.send_message.Transaction
|
||||
import com.mlsdev.rximagepicker.RxImageConverters
|
||||
import common.util.Preferences
|
||||
import data.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import presentation.receiver.MessageDeliveredReceiver
|
||||
import presentation.receiver.MessageSentReceiver
|
||||
import java.io.ByteArrayOutputStream
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -50,33 +44,13 @@ class SendMessage @Inject constructor(
|
||||
.filter { params.addresses.isNotEmpty() }
|
||||
.doOnNext {
|
||||
if (params.addresses.size == 1 && params.attachments.isEmpty()) {
|
||||
sendSms(params.threadId, params.addresses.first(), params.body)
|
||||
messageRepo.sendSmsAndPersist(params.threadId, params.addresses.first(), params.body)
|
||||
} else {
|
||||
sendMms(params.threadId, params.addresses, params.body, params.attachments)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendSms(threadId: Long, address: String, body: String) {
|
||||
val smsManager = SmsManager.getDefault()
|
||||
|
||||
val message = messageRepo.insertSentSms(threadId, address, body)
|
||||
val parts = smsManager.divideMessage(if (prefs.unicode.get()) StripAccents.stripAccents(body) else body)
|
||||
|
||||
val sentIntents = parts.map {
|
||||
val intent = Intent(context, MessageSentReceiver::class.java).putExtra("id", message.id)
|
||||
PendingIntent.getBroadcast(context, message.id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
}
|
||||
|
||||
val deliveredIntents = parts.map {
|
||||
val intent = Intent(context, MessageDeliveredReceiver::class.java).putExtra("id", message.id)
|
||||
val pendingIntent = PendingIntent.getBroadcast(context, message.id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
if (prefs.delivery.get()) pendingIntent else null
|
||||
}
|
||||
|
||||
smsManager.sendMultipartTextMessage(address, null, parts, ArrayList(sentIntents), ArrayList(deliveredIntents))
|
||||
}
|
||||
|
||||
private fun sendMms(threadId: Long, addresses: List<String>, body: String, attachments: List<Uri>) {
|
||||
val settings = Settings()
|
||||
val message = Message(body, addresses.toTypedArray())
|
||||
|
||||
@@ -67,6 +67,7 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
override val copyTextIntent: Subject<Message> = PublishSubject.create()
|
||||
override val forwardMessageIntent: Subject<Message> = PublishSubject.create()
|
||||
override val deleteMessageIntent: Subject<Message> = PublishSubject.create()
|
||||
override val messageClickIntent: Subject<Message> by lazy { messageAdapter.clicks }
|
||||
override val attachmentDeletedIntent: Subject<Uri> by lazy { attachmentAdapter.attachmentDeleted }
|
||||
override val textChangedIntent by lazy { message.textChanges() }
|
||||
override val attachIntent by lazy { attach.clicks() }
|
||||
|
||||
@@ -41,6 +41,7 @@ interface ComposeView : QkView<ComposeState> {
|
||||
val copyTextIntent: Subject<Message>
|
||||
val forwardMessageIntent: Subject<Message>
|
||||
val deleteMessageIntent: Subject<Message>
|
||||
val messageClickIntent: Subject<Message>
|
||||
val attachmentDeletedIntent: Subject<Uri>
|
||||
val textChangedIntent: Observable<CharSequence>
|
||||
val attachIntent: Observable<Unit>
|
||||
|
||||
@@ -68,6 +68,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
@Inject lateinit var markUnarchived: MarkUnarchived
|
||||
@Inject lateinit var deleteConversation: DeleteConversation
|
||||
@Inject lateinit var sendMessage: SendMessage
|
||||
@Inject lateinit var retrySending: RetrySending
|
||||
@Inject lateinit var markRead: MarkRead
|
||||
@Inject lateinit var deleteMessage: DeleteMessage
|
||||
|
||||
@@ -285,6 +286,12 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { conversation -> deleteConversation.execute(conversation.id) }
|
||||
|
||||
view.messageClickIntent
|
||||
.filter { message -> message.isFailedMessage() }
|
||||
.doOnNext { message -> retrySending.execute(message) }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
// Mark the conversation read, if in foreground
|
||||
Observables.combineLatest(messages, view.activityVisibleIntent, { _, b -> b })
|
||||
.withLatestFrom(conversation, { visible, conversation ->
|
||||
|
||||
@@ -59,6 +59,7 @@ class MessagesAdapter @Inject constructor(
|
||||
private const val TIMESTAMP_THRESHOLD = 10
|
||||
}
|
||||
|
||||
val clicks: Subject<Message> = PublishSubject.create<Message>()
|
||||
val longClicks: Subject<Message> = PublishSubject.create<Message>()
|
||||
|
||||
var data: Pair<Conversation, RealmResults<Message>>? = null
|
||||
@@ -107,11 +108,9 @@ class MessagesAdapter @Inject constructor(
|
||||
if (absViewType != VIEW_TYPE_ME) {
|
||||
val address = people[absViewType - 2]
|
||||
if (!contactMap.containsKey(address)) {
|
||||
val contact = data?.first?.recipients?.mapNotNull { it.contact } // Map the conversation to its contacts
|
||||
contactMap[address] = data?.first?.recipients?.mapNotNull { it.contact } // Map the conversation to its contacts
|
||||
?.firstOrNull { it.numbers.any { PhoneNumberUtils.compare(it.address, address) } } // See if any of the phone numbers match
|
||||
?: Contact(numbers = RealmList(PhoneNumber(address = address))) // Fallback to a fake contact
|
||||
|
||||
contactMap.put(address, contact)
|
||||
}
|
||||
|
||||
view.avatar.contact = contactMap[address]
|
||||
@@ -130,6 +129,7 @@ class MessagesAdapter @Inject constructor(
|
||||
val view = viewHolder.itemView
|
||||
|
||||
RxView.clicks(view).subscribe {
|
||||
clicks.onNext(message)
|
||||
if (selected.contains(message.id)) selected.remove(message.id)
|
||||
else selected.add(message.id)
|
||||
notifyItemChanged(position)
|
||||
|
||||
Reference in New Issue
Block a user