From 4409aff9057769eb412dcf5f148fd5953782e8f0 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Tue, 13 Feb 2018 02:26:30 -0500 Subject: [PATCH] Tap to retry sending failed SMS --- .../java/data/repository/MessageRepository.kt | 68 ++++++++++++++++++- .../src/main/java/interactor/RetrySending.kt | 50 ++++++++++++++ qksms/src/main/java/interactor/SendMessage.kt | 28 +------- .../feature/compose/ComposeActivity.kt | 1 + .../feature/compose/ComposeView.kt | 1 + .../feature/compose/ComposeViewModel.kt | 7 ++ .../feature/compose/MessagesAdapter.kt | 6 +- 7 files changed, 130 insertions(+), 31 deletions(-) create mode 100644 qksms/src/main/java/interactor/RetrySending.kt diff --git a/qksms/src/main/java/data/repository/MessageRepository.kt b/qksms/src/main/java/data/repository/MessageRepository.kt index d67524a2..c51466ac 100644 --- a/qksms/src/main/java/data/repository/MessageRepository.kt +++ b/qksms/src/main/java/data/repository/MessageRepository.kt @@ -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> { 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) diff --git a/qksms/src/main/java/interactor/RetrySending.kt b/qksms/src/main/java/interactor/RetrySending.kt new file mode 100644 index 00000000..08b028a2 --- /dev/null +++ b/qksms/src/main/java/interactor/RetrySending.kt @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * 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 . + */ +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() { + + override fun buildObservable(params: Message): Flowable { + + // 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) } + } + +} \ No newline at end of file diff --git a/qksms/src/main/java/interactor/SendMessage.kt b/qksms/src/main/java/interactor/SendMessage.kt index 34c27784..1a8195b6 100644 --- a/qksms/src/main/java/interactor/SendMessage.kt +++ b/qksms/src/main/java/interactor/SendMessage.kt @@ -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, body: String, attachments: List) { val settings = Settings() val message = Message(body, addresses.toTypedArray()) diff --git a/qksms/src/main/java/presentation/feature/compose/ComposeActivity.kt b/qksms/src/main/java/presentation/feature/compose/ComposeActivity.kt index ac2ff044..bc5b5b04 100644 --- a/qksms/src/main/java/presentation/feature/compose/ComposeActivity.kt +++ b/qksms/src/main/java/presentation/feature/compose/ComposeActivity.kt @@ -67,6 +67,7 @@ class ComposeActivity : QkActivity(), ComposeView { override val copyTextIntent: Subject = PublishSubject.create() override val forwardMessageIntent: Subject = PublishSubject.create() override val deleteMessageIntent: Subject = PublishSubject.create() + override val messageClickIntent: Subject by lazy { messageAdapter.clicks } override val attachmentDeletedIntent: Subject by lazy { attachmentAdapter.attachmentDeleted } override val textChangedIntent by lazy { message.textChanges() } override val attachIntent by lazy { attach.clicks() } diff --git a/qksms/src/main/java/presentation/feature/compose/ComposeView.kt b/qksms/src/main/java/presentation/feature/compose/ComposeView.kt index 288fd4cb..7519201d 100644 --- a/qksms/src/main/java/presentation/feature/compose/ComposeView.kt +++ b/qksms/src/main/java/presentation/feature/compose/ComposeView.kt @@ -41,6 +41,7 @@ interface ComposeView : QkView { val copyTextIntent: Subject val forwardMessageIntent: Subject val deleteMessageIntent: Subject + val messageClickIntent: Subject val attachmentDeletedIntent: Subject val textChangedIntent: Observable val attachIntent: Observable diff --git a/qksms/src/main/java/presentation/feature/compose/ComposeViewModel.kt b/qksms/src/main/java/presentation/feature/compose/ComposeViewModel.kt index df98f50a..62f58d45 100644 --- a/qksms/src/main/java/presentation/feature/compose/ComposeViewModel.kt +++ b/qksms/src/main/java/presentation/feature/compose/ComposeViewModel.kt @@ -68,6 +68,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel( @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( .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 -> diff --git a/qksms/src/main/java/presentation/feature/compose/MessagesAdapter.kt b/qksms/src/main/java/presentation/feature/compose/MessagesAdapter.kt index 6eb85fa0..8050eab5 100644 --- a/qksms/src/main/java/presentation/feature/compose/MessagesAdapter.kt +++ b/qksms/src/main/java/presentation/feature/compose/MessagesAdapter.kt @@ -59,6 +59,7 @@ class MessagesAdapter @Inject constructor( private const val TIMESTAMP_THRESHOLD = 10 } + val clicks: Subject = PublishSubject.create() val longClicks: Subject = PublishSubject.create() var data: Pair>? = 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)