Tap to retry sending failed SMS

This commit is contained in:
Moez Bhatti
2018-02-13 02:26:30 -05:00
parent ce8f01b707
commit 4409aff905
7 changed files with 130 additions and 31 deletions
@@ -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) }
}
}
+1 -27
View File
@@ -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())