mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
move old ReceiveSms interactor code to receive sms worker
move old ReceiveMms interactor code to receive mms worker move mms processing code from mmsreceivedreceiver to receive mms worker fix bug of non-unique pending intent for mms ack/notify send removed some unused android-smsmms files and code that was causing build issues
This commit is contained in:
@@ -34,39 +34,21 @@
|
||||
*/
|
||||
package dev.octoshrimpy.quik.receiver
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.ContentResolver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.AsyncTask
|
||||
import android.os.Parcelable
|
||||
import android.provider.Telephony
|
||||
import android.telephony.SmsManager
|
||||
import androidx.core.net.toUri
|
||||
import androidx.work.OneTimeWorkRequestBuilder
|
||||
import androidx.work.OutOfQuotaPolicy
|
||||
import androidx.work.WorkManager
|
||||
import androidx.work.workDataOf
|
||||
import com.android.mms.service_alt.DownloadRequest
|
||||
import com.android.mms.service_alt.MmsConfig
|
||||
import com.android.mms.service_alt.MmsConfig.Overridden
|
||||
import com.android.mms.transaction.DownloadManager
|
||||
import com.google.android.mms.pdu_alt.AcknowledgeInd
|
||||
import com.google.android.mms.pdu_alt.EncodedStringValue
|
||||
import com.google.android.mms.pdu_alt.NotificationInd
|
||||
import com.google.android.mms.pdu_alt.NotifyRespInd
|
||||
import com.google.android.mms.pdu_alt.PduComposer
|
||||
import com.google.android.mms.pdu_alt.PduHeaders
|
||||
import com.google.android.mms.pdu_alt.PduParser
|
||||
import com.google.android.mms.pdu_alt.PduPersister
|
||||
import com.google.android.mms.pdu_alt.RetrieveConf
|
||||
import com.google.android.mms.util_alt.SqliteWrapper
|
||||
import com.klinker.android.send_message.MmsSentReceiver
|
||||
import com.klinker.android.send_message.SmsManagerFactory
|
||||
import com.klinker.android.send_message.MmsReceivedReceiver.EXTRA_FILE_PATH
|
||||
import com.klinker.android.send_message.MmsReceivedReceiver.EXTRA_LOCATION_URL
|
||||
import com.klinker.android.send_message.MmsReceivedReceiver.EXTRA_URI
|
||||
import com.klinker.android.send_message.MmsReceivedReceiver.SUBSCRIPTION_ID
|
||||
import com.klinker.android.send_message.Utils
|
||||
import dev.octoshrimpy.quik.interactor.ReceiveMms
|
||||
import dev.octoshrimpy.quik.worker.ReceiveMmsWorker
|
||||
import dev.octoshrimpy.quik.worker.ReceiveMmsWorker.Companion.INPUT_DATA_EXTRA_FILE_PATH
|
||||
import dev.octoshrimpy.quik.worker.ReceiveMmsWorker.Companion.INPUT_DATA_EXTRA_LOCATION_URL
|
||||
@@ -74,237 +56,10 @@ import dev.octoshrimpy.quik.worker.ReceiveMmsWorker.Companion.INPUT_DATA_EXTRA_M
|
||||
import dev.octoshrimpy.quik.worker.ReceiveMmsWorker.Companion.INPUT_DATA_EXTRA_URI
|
||||
import dev.octoshrimpy.quik.worker.ReceiveMmsWorker.Companion.INPUT_DATA_SUBSCRIPTION_ID
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class MmsReceivedReceiver : BroadcastReceiver() {
|
||||
companion object {
|
||||
const val EXTRA_FILE_PATH: String = "file_path"
|
||||
const val EXTRA_LOCATION_URL: String = "location_url"
|
||||
const val EXTRA_URI: String = "notification_ind_uri"
|
||||
const val SUBSCRIPTION_ID: String = "subscription_id"
|
||||
|
||||
private const val LOCATION_SELECTION =
|
||||
Telephony.Mms.MESSAGE_TYPE + "=? AND " + Telephony.Mms.CONTENT_LOCATION + " =?"
|
||||
|
||||
private val RECEIVE_NOTIFICATION_EXECUTOR: ExecutorService =
|
||||
Executors.newSingleThreadExecutor()
|
||||
|
||||
private fun getNotificationInd(context: Context, extraUri: String) =
|
||||
PduPersister.getPduPersister(context).load(extraUri.toUri()) as NotificationInd
|
||||
|
||||
fun onReceiveWorker(
|
||||
context: Context, subscriptionId: Int, filePath: String, locationUrl: String,
|
||||
mmsHttpStatus: Int, extraUri: String, receiveMms: ReceiveMms
|
||||
) {
|
||||
if (filePath.isEmpty()) {
|
||||
Timber.e("empty file path")
|
||||
return
|
||||
}
|
||||
|
||||
if (locationUrl.isEmpty()) {
|
||||
Timber.e("empty location url")
|
||||
return
|
||||
}
|
||||
|
||||
if (extraUri.isEmpty()) {
|
||||
Timber.e("empty extra uri")
|
||||
return
|
||||
}
|
||||
|
||||
Timber.v(filePath)
|
||||
|
||||
val downloadFile = File(filePath)
|
||||
try {
|
||||
// read file into a bytearray
|
||||
val mmsData = FileInputStream(downloadFile).use { inputStream ->
|
||||
val fileLength = downloadFile.length().toInt()
|
||||
ByteArray(fileLength).let { bytes ->
|
||||
inputStream.read(bytes, 0, fileLength)
|
||||
bytes
|
||||
}
|
||||
}
|
||||
|
||||
Timber.v("response buffered length: ${mmsData.size}")
|
||||
|
||||
if (mmsData.isNotEmpty()) {
|
||||
val notificationTasks =
|
||||
getNotificationTasks(context, subscriptionId, extraUri, mmsData)
|
||||
|
||||
// persist message
|
||||
val messageUri = DownloadRequest.persist(
|
||||
context, mmsData, Overridden(MmsConfig(context), null),
|
||||
locationUrl, subscriptionId, null
|
||||
)
|
||||
|
||||
// run notifiers to notify back to mmsc
|
||||
Timber.v("running the common async notifier for download")
|
||||
for (task in notificationTasks)
|
||||
task.executeOnExecutor(RECEIVE_NOTIFICATION_EXECUTOR)
|
||||
|
||||
handleHttpError(context, mmsHttpStatus, locationUrl)
|
||||
|
||||
DownloadManager.finishDownload(locationUrl)
|
||||
|
||||
// message is 'raw' received, so do quik app processing
|
||||
if (messageUri != null)
|
||||
receiveMms.execute(messageUri)
|
||||
}
|
||||
else Timber.e("empty mms data")
|
||||
} catch (e: FileNotFoundException) {
|
||||
Timber.e("file not found", e)
|
||||
} catch (e: IOException) {
|
||||
Timber.e("io exception", e)
|
||||
} finally {
|
||||
downloadFile.delete()
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleHttpError(context: Context, mmsHttpStatus: Int, locationUrl: String) {
|
||||
if ((mmsHttpStatus == 404) || (mmsHttpStatus == 400)) {
|
||||
// Delete the corresponding NotificationInd
|
||||
SqliteWrapper.delete(
|
||||
context, context.contentResolver, Telephony.Mms.CONTENT_URI,
|
||||
LOCATION_SELECTION,
|
||||
arrayOf(
|
||||
PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND.toString(),
|
||||
locationUrl
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class CommonAsyncTask(
|
||||
protected val mContext: Context, private val subscriptionId: Int,
|
||||
protected val mNotificationInd: NotificationInd
|
||||
) : AsyncTask<Void?, Void?, Void?>() {
|
||||
|
||||
fun sendPdu(pduBytes: ByteArray) {
|
||||
// write pdu bytes to temporary file
|
||||
val fileName = "send.notify.${UUID.randomUUID()}.dat"
|
||||
|
||||
val contentUri = Uri.Builder()
|
||||
.authority("${mContext.packageName}.MmsFileProvider")
|
||||
.path(fileName)
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.build()
|
||||
|
||||
try {
|
||||
FileOutputStream(File(mContext.cacheDir, fileName)).use { outputStream ->
|
||||
outputStream.write(pduBytes)
|
||||
}
|
||||
Timber.v("notify send file $fileName")
|
||||
} catch (e: Exception) {
|
||||
Timber.e("error writing notify send file", e)
|
||||
}
|
||||
|
||||
val useWapMmsc = com.android.mms.MmsConfig.getNotifyWapMMSC()
|
||||
if (useWapMmsc)
|
||||
Timber.v("using content location for wap mmsc")
|
||||
|
||||
SmsManagerFactory.createSmsManager(subscriptionId).sendMultimediaMessage(
|
||||
mContext, contentUri,
|
||||
if (useWapMmsc) String(mNotificationInd.contentLocation)
|
||||
else null,
|
||||
null,
|
||||
PendingIntent.getBroadcast(
|
||||
mContext, 0,
|
||||
Intent(mContext, MessageSentReceiver::class.java)
|
||||
.putExtra(MmsSentReceiver.EXTRA_FILE_PATH, fileName)
|
||||
.putExtra(MessageSentReceiver.EXTRA_IS_NOTIFY, 1),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private class NotifyRespTask(context: Context, subscriptionId: Int, ind: NotificationInd) :
|
||||
CommonAsyncTask(context, subscriptionId, ind) {
|
||||
override fun doInBackground(vararg params: Void?): Void? {
|
||||
// Create the M-NotifyResp.ind
|
||||
Timber.v("create m-notifyresp.ind")
|
||||
try {
|
||||
val notifyRespInd = NotifyRespInd(
|
||||
PduHeaders.CURRENT_MMS_VERSION,
|
||||
mNotificationInd.transactionId,
|
||||
PduHeaders.STATUS_RETRIEVED
|
||||
)
|
||||
|
||||
// Pack M-NotifyResp.ind and send it
|
||||
sendPdu(PduComposer(mContext, notifyRespInd).make())
|
||||
} catch (e: Exception) {
|
||||
Timber.e("error creating m-notifyresp.ind", e)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private class AcknowledgeIndTask(
|
||||
context: Context, subscriptionId: Int, ind: NotificationInd,
|
||||
private val mRetrieveConf: RetrieveConf
|
||||
) : CommonAsyncTask(context, subscriptionId, ind) {
|
||||
override fun doInBackground(vararg params: Void?): Void? {
|
||||
// Send M-Acknowledge.ind to MMSC if required.
|
||||
// If the Transaction-ID isn't set in the M-Retrieve.conf, it means
|
||||
// the MMS proxy-relay doesn't require an ACK.
|
||||
Timber.v("create m-acknowledge.ind")
|
||||
mRetrieveConf.transactionId?.let { tranId ->
|
||||
// Create M-Acknowledge.ind
|
||||
try {
|
||||
val acknowledgeInd = AcknowledgeInd(PduHeaders.CURRENT_MMS_VERSION, tranId)
|
||||
|
||||
// insert the 'from' address per spec
|
||||
acknowledgeInd.from = EncodedStringValue(
|
||||
Utils.getMyPhoneNumber(mContext).orEmpty()
|
||||
)
|
||||
|
||||
// Pack M-Acknowledge.ind and send it
|
||||
sendPdu(PduComposer(mContext, acknowledgeInd).make())
|
||||
} catch (e: Exception) {
|
||||
Timber.e("error creating m-acknowledge.ind", e)
|
||||
}
|
||||
} ?: let { Timber.e("no transaction id for creating m-acknowledge.ind") }
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private fun getNotificationTasks(
|
||||
context: Context, subscriptionId: Int, extraUri: String, mmsData: ByteArray
|
||||
): List<CommonAsyncTask> {
|
||||
val pdu = PduParser(
|
||||
mmsData,
|
||||
Overridden(MmsConfig(context), null).supportMmsContentDisposition
|
||||
).parse()
|
||||
|
||||
if (pdu is RetrieveConf) {
|
||||
try {
|
||||
Timber.v("create notification tasks")
|
||||
return getNotificationInd(context, extraUri).let { notificationInd ->
|
||||
listOf(
|
||||
AcknowledgeIndTask(context, subscriptionId, notificationInd, pdu),
|
||||
NotifyRespTask(context, subscriptionId, notificationInd)
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.e("error get notification tasks", e)
|
||||
}
|
||||
} else
|
||||
Timber.e("MmsReceivedReceiver.sendNotification failed to parse pdu")
|
||||
|
||||
return listOf()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
Timber.v("mms downloaded, persisting it to the database with worker")
|
||||
Timber.v("mms downloaded. create worker to process")
|
||||
|
||||
// start worker with message id as param
|
||||
WorkManager.getInstance(context).enqueue(
|
||||
|
||||
@@ -23,17 +23,30 @@ import androidx.work.ListenableWorker
|
||||
import androidx.work.Worker
|
||||
import androidx.work.WorkerFactory
|
||||
import androidx.work.WorkerParameters
|
||||
import dev.octoshrimpy.quik.interactor.ReceiveMms
|
||||
import dev.octoshrimpy.quik.interactor.ReceiveSms
|
||||
import dev.octoshrimpy.quik.blocking.BlockingClient
|
||||
import dev.octoshrimpy.quik.interactor.UpdateBadge
|
||||
import dev.octoshrimpy.quik.manager.ActiveConversationManager
|
||||
import dev.octoshrimpy.quik.manager.NotificationManager
|
||||
import dev.octoshrimpy.quik.manager.ShortcutManager
|
||||
import dev.octoshrimpy.quik.repository.ConversationRepository
|
||||
import dev.octoshrimpy.quik.repository.MessageRepository
|
||||
import dev.octoshrimpy.quik.repository.ScheduledMessageRepository
|
||||
import dev.octoshrimpy.quik.repository.SyncRepository
|
||||
import dev.octoshrimpy.quik.util.Preferences
|
||||
import javax.inject.Inject
|
||||
|
||||
class InjectionWorkerFactory @Inject constructor(
|
||||
private val receiveSms: ReceiveSms,
|
||||
private val receiveMms: ReceiveMms,
|
||||
private val conversationRepo: ConversationRepository,
|
||||
private val blockingClient: BlockingClient,
|
||||
private val prefs: Preferences,
|
||||
private val messageRepo: MessageRepository,
|
||||
private val updateBadge: UpdateBadge,
|
||||
private val shortcutManager: ShortcutManager,
|
||||
private val scheduledMessageRepository: ScheduledMessageRepository,
|
||||
private val notificationManager: NotificationManager,
|
||||
private val activeConversationManager: ActiveConversationManager,
|
||||
private val syncRepo: SyncRepository,
|
||||
|
||||
) : WorkerFactory() {
|
||||
override fun createWorker(
|
||||
appContext: Context,
|
||||
@@ -50,12 +63,24 @@ class InjectionWorkerFactory @Inject constructor(
|
||||
is HousekeepingWorker ->
|
||||
instance.scheduledMessageRepository = scheduledMessageRepository
|
||||
is ReceiveSmsWorker -> {
|
||||
instance.receiveSms = receiveSms
|
||||
instance.conversationRepo = conversationRepo
|
||||
instance.blockingClient = blockingClient
|
||||
instance.prefs = prefs
|
||||
instance.messageRepo = messageRepo
|
||||
instance.shortcutManager = shortcutManager
|
||||
instance.notificationManager = notificationManager
|
||||
instance.updateBadge = updateBadge
|
||||
}
|
||||
is ReceiveMmsWorker -> {
|
||||
instance.receiveMms = receiveMms
|
||||
instance.syncRepo = syncRepo
|
||||
instance.activeConversationManager = activeConversationManager
|
||||
instance.conversationRepo = conversationRepo
|
||||
instance.blockingClient = blockingClient
|
||||
instance.prefs = prefs
|
||||
instance.messageRepo = messageRepo
|
||||
instance.shortcutManager = shortcutManager
|
||||
instance.notificationManager = notificationManager
|
||||
instance.updateBadge = updateBadge
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,14 +18,48 @@
|
||||
*/
|
||||
package dev.octoshrimpy.quik.worker
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.ContentResolver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony
|
||||
import androidx.core.net.toUri
|
||||
import androidx.work.ForegroundInfo
|
||||
import androidx.work.Worker
|
||||
import androidx.work.WorkerParameters
|
||||
import dev.octoshrimpy.quik.interactor.ReceiveMms
|
||||
import com.android.mms.service_alt.DownloadRequest
|
||||
import com.android.mms.service_alt.MmsConfig
|
||||
import com.android.mms.service_alt.MmsConfig.Overridden
|
||||
import com.android.mms.transaction.DownloadManager
|
||||
import com.google.android.mms.pdu_alt.AcknowledgeInd
|
||||
import com.google.android.mms.pdu_alt.EncodedStringValue
|
||||
import com.google.android.mms.pdu_alt.NotificationInd
|
||||
import com.google.android.mms.pdu_alt.NotifyRespInd
|
||||
import com.google.android.mms.pdu_alt.PduComposer
|
||||
import com.google.android.mms.pdu_alt.PduHeaders
|
||||
import com.google.android.mms.pdu_alt.PduPersister
|
||||
import com.google.android.mms.util_alt.SqliteWrapper
|
||||
import com.klinker.android.send_message.MmsSentReceiver
|
||||
import com.klinker.android.send_message.SmsManagerFactory
|
||||
import com.klinker.android.send_message.Utils
|
||||
import dev.octoshrimpy.quik.blocking.BlockingClient
|
||||
import dev.octoshrimpy.quik.interactor.UpdateBadge
|
||||
import dev.octoshrimpy.quik.manager.ActiveConversationManager
|
||||
import dev.octoshrimpy.quik.manager.NotificationManager
|
||||
import dev.octoshrimpy.quik.receiver.MmsReceivedReceiver
|
||||
import dev.octoshrimpy.quik.manager.ShortcutManager
|
||||
import dev.octoshrimpy.quik.receiver.MessageSentReceiver
|
||||
import dev.octoshrimpy.quik.repository.ConversationRepository
|
||||
import dev.octoshrimpy.quik.repository.MessageRepository
|
||||
import dev.octoshrimpy.quik.repository.SyncRepository
|
||||
import dev.octoshrimpy.quik.util.Preferences
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
class ReceiveMmsWorker(appContext: Context, workerParams: WorkerParameters)
|
||||
@@ -36,29 +70,281 @@ class ReceiveMmsWorker(appContext: Context, workerParams: WorkerParameters)
|
||||
const val INPUT_DATA_EXTRA_LOCATION_URL = "locationUrl"
|
||||
const val INPUT_DATA_EXTRA_MMS_HTTP_STATUS = "mmsHttpStatus"
|
||||
const val INPUT_DATA_EXTRA_URI = "extraUri"
|
||||
|
||||
private const val LOCATION_SELECTION =
|
||||
Telephony.Mms.MESSAGE_TYPE + "=? AND " + Telephony.Mms.CONTENT_LOCATION + " =?"
|
||||
}
|
||||
|
||||
@Inject lateinit var receiveMms: ReceiveMms
|
||||
@Inject lateinit var activeConversationManager: ActiveConversationManager
|
||||
@Inject lateinit var conversationRepo: ConversationRepository
|
||||
@Inject lateinit var blockingClient: BlockingClient
|
||||
@Inject lateinit var prefs: Preferences
|
||||
@Inject lateinit var syncRepo: SyncRepository
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
@Inject lateinit var notificationManager: NotificationManager
|
||||
@Inject lateinit var updateBadge: UpdateBadge
|
||||
@Inject lateinit var shortcutManager: ShortcutManager
|
||||
|
||||
override fun doWork(): Result {
|
||||
Timber.v("started")
|
||||
|
||||
MmsReceivedReceiver.onReceiveWorker(
|
||||
applicationContext,
|
||||
inputData.getInt(INPUT_DATA_SUBSCRIPTION_ID, -1),
|
||||
inputData.getString(INPUT_DATA_EXTRA_FILE_PATH) ?: "",
|
||||
inputData.getString(INPUT_DATA_EXTRA_LOCATION_URL) ?: "",
|
||||
inputData.getInt(INPUT_DATA_EXTRA_MMS_HTTP_STATUS, 0),
|
||||
inputData.getString(INPUT_DATA_EXTRA_URI) ?: "",
|
||||
receiveMms
|
||||
)
|
||||
val subscriptionId = inputData.getInt(INPUT_DATA_SUBSCRIPTION_ID, -1)
|
||||
val filePath = inputData.getString(INPUT_DATA_EXTRA_FILE_PATH) ?: ""
|
||||
val locationUrl = inputData.getString(INPUT_DATA_EXTRA_LOCATION_URL) ?: ""
|
||||
val extraUri = inputData.getString(INPUT_DATA_EXTRA_URI) ?: ""
|
||||
|
||||
if (filePath.isEmpty()) {
|
||||
Timber.e("empty file path")
|
||||
return Result.failure(inputData)
|
||||
}
|
||||
|
||||
if (locationUrl.isEmpty()) {
|
||||
Timber.e("empty location url")
|
||||
return Result.failure(inputData)
|
||||
}
|
||||
|
||||
if (extraUri.isEmpty()) {
|
||||
Timber.e("empty extra uri")
|
||||
return Result.failure(inputData)
|
||||
}
|
||||
|
||||
Timber.v(filePath)
|
||||
|
||||
val downloadFile = File(filePath)
|
||||
try {
|
||||
// read file into a bytearray
|
||||
val mmsData = FileInputStream(downloadFile).use { inputStream ->
|
||||
val fileLength = downloadFile.length().toInt()
|
||||
ByteArray(fileLength).let { bytes ->
|
||||
inputStream.read(bytes, 0, fileLength)
|
||||
bytes
|
||||
}
|
||||
}
|
||||
|
||||
Timber.v("response buffered length: ${mmsData.size}")
|
||||
|
||||
if (mmsData.isNotEmpty()) {
|
||||
// build notification ind (beware this must be done prior to downloadrequest.persist)
|
||||
val notificationInd =
|
||||
PduPersister.getPduPersister(applicationContext).load(extraUri.toUri())
|
||||
as NotificationInd
|
||||
|
||||
// persist message
|
||||
val messageUri = DownloadRequest.persist(
|
||||
applicationContext, mmsData,
|
||||
Overridden(MmsConfig(applicationContext), null),
|
||||
locationUrl, subscriptionId, null
|
||||
)
|
||||
|
||||
handleHttpError(
|
||||
applicationContext,
|
||||
inputData.getInt(INPUT_DATA_EXTRA_MMS_HTTP_STATUS, 0),
|
||||
locationUrl
|
||||
)
|
||||
|
||||
DownloadManager.finishDownload(locationUrl)
|
||||
|
||||
// if message was persisted ok
|
||||
if (messageUri != null) {
|
||||
// Sync the message
|
||||
val message = syncRepo.syncMessage(messageUri)
|
||||
?: return Result.failure(inputData)
|
||||
|
||||
// TODO: Ideally this is done when we're saving the MMS to ContentResolver
|
||||
// This change can be made once we move the MMS storing code to the Data module
|
||||
if (activeConversationManager.getActiveConversation() == message.threadId) {
|
||||
messageRepo.markRead(listOf(message.threadId))
|
||||
}
|
||||
|
||||
// Because we use the smsmms library for receiving and storing MMS, we'll need
|
||||
// to check if it should be blocked after we've pulled it into realm. If it
|
||||
// turns out that it should be dropped, then delete it
|
||||
// TODO Don't store blocked messages in the first place
|
||||
val action = blockingClient.shouldBlock(message.address).blockingGet()
|
||||
val shouldDrop = prefs.drop.get()
|
||||
Timber.v("block=$action, drop=$shouldDrop")
|
||||
|
||||
if (action is BlockingClient.Action.Block && shouldDrop) {
|
||||
messageRepo.deleteMessages(listOf(message.id))
|
||||
} else {
|
||||
when (action) {
|
||||
is BlockingClient.Action.Block -> {
|
||||
messageRepo.markRead(listOf(message.threadId))
|
||||
conversationRepo.markBlocked(
|
||||
listOf(message.threadId),
|
||||
prefs.blockingManager.get(),
|
||||
action.reason
|
||||
)
|
||||
}
|
||||
|
||||
is BlockingClient.Action.Unblock ->
|
||||
conversationRepo.markUnblocked(message.threadId)
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
|
||||
// update the conversation
|
||||
conversationRepo.updateConversations(listOf(message.threadId))
|
||||
val conversation =
|
||||
conversationRepo.getOrCreateConversation(message.threadId)
|
||||
?: return Result.failure(inputData)
|
||||
|
||||
// don't notify (continue) for blocked conversations
|
||||
if (conversation.blocked) {
|
||||
Timber.v("no notifications for blocked")
|
||||
return Result.success(inputData)
|
||||
}
|
||||
|
||||
// unarchive conversation if necessary
|
||||
if (conversation.archived)
|
||||
conversationRepo.markUnarchived(listOf(conversation.id))
|
||||
|
||||
// unarchive conversation if necessary
|
||||
if (conversation.archived) {
|
||||
Timber.v("conversation unarchived")
|
||||
conversationRepo.markUnarchived(listOf(conversation.id))
|
||||
}
|
||||
|
||||
// update/create notification
|
||||
Timber.v("update/create notification")
|
||||
notificationManager.update(conversation.id)
|
||||
|
||||
// update shortcuts
|
||||
Timber.v("update shortcuts")
|
||||
shortcutManager.updateShortcuts()
|
||||
shortcutManager.reportShortcutUsed(conversation.id)
|
||||
|
||||
// update the badge and widget
|
||||
Timber.v("update badge and widget")
|
||||
updateBadge.execute(Unit)
|
||||
}
|
||||
|
||||
// send ack to mmsc
|
||||
sendAcknowledgeInd(applicationContext, subscriptionId, notificationInd)
|
||||
|
||||
// send notify ind to mmsc
|
||||
sendNotifyRespInd(applicationContext, subscriptionId, notificationInd)
|
||||
}
|
||||
}
|
||||
else Timber.e("empty mms data")
|
||||
} catch (e: FileNotFoundException) {
|
||||
Timber.e("file not found: ${e.message}")
|
||||
} catch (e: IOException) {
|
||||
Timber.e("io exception: ${e.message}")
|
||||
} catch (e: Exception) {
|
||||
Timber.e("mms receive worker exception: ${e.message}")
|
||||
} finally {
|
||||
downloadFile.delete()
|
||||
}
|
||||
|
||||
Timber.v("finished")
|
||||
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
private fun handleHttpError(context: Context, mmsHttpStatus: Int, locationUrl: String) {
|
||||
if ((mmsHttpStatus == 404) || (mmsHttpStatus == 400)) {
|
||||
// Delete the corresponding NotificationInd
|
||||
SqliteWrapper.delete(
|
||||
context, context.contentResolver, Telephony.Mms.CONTENT_URI,
|
||||
LOCATION_SELECTION,
|
||||
arrayOf(
|
||||
PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND.toString(),
|
||||
locationUrl
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendResponsePdu(
|
||||
context: Context, subscriptionId: Int, contentLocation: ByteArray, pduBytes: ByteArray
|
||||
) {
|
||||
// write pdu bytes to temporary file
|
||||
val fileName = "send.notify.${UUID.randomUUID()}.dat"
|
||||
|
||||
val contentUri = Uri.Builder()
|
||||
.authority("${context.packageName}.MmsFileProvider")
|
||||
.path(fileName)
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.build()
|
||||
|
||||
try {
|
||||
FileOutputStream(File(context.cacheDir, fileName)).use { outputStream ->
|
||||
outputStream.write(pduBytes)
|
||||
}
|
||||
Timber.v("notify send file $fileName")
|
||||
} catch (e: Exception) {
|
||||
Timber.e("error writing notify send file", e)
|
||||
}
|
||||
|
||||
val useWapMmsc = com.android.mms.MmsConfig.getNotifyWapMMSC()
|
||||
if (useWapMmsc)
|
||||
Timber.v("using content location for wap mmsc")
|
||||
|
||||
SmsManagerFactory.createSmsManager(subscriptionId).sendMultimediaMessage(
|
||||
context, contentUri,
|
||||
if (useWapMmsc) String(contentLocation) else null,
|
||||
null,
|
||||
PendingIntent.getBroadcast(
|
||||
context, 0,
|
||||
Intent(context, MessageSentReceiver::class.java)
|
||||
.setData(contentUri) // this is only used to make pending intent unique
|
||||
.putExtra(MmsSentReceiver.EXTRA_FILE_PATH, fileName)
|
||||
.putExtra(MessageSentReceiver.EXTRA_IS_NOTIFY, 1),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun sendAcknowledgeInd(
|
||||
context: Context, subscriptionId: Int, notificationInd: NotificationInd
|
||||
) {
|
||||
Timber.v("create m-acknowledge.ind")
|
||||
|
||||
try {
|
||||
// if the Transaction-ID isn't set in the M-Retrieve.conf, it means
|
||||
// the MMS proxy-relay doesn't require an ACK
|
||||
if (notificationInd.transactionId != null) {
|
||||
val ackInd = AcknowledgeInd(
|
||||
PduHeaders.CURRENT_MMS_VERSION, notificationInd.transactionId
|
||||
).apply {
|
||||
// insert the 'from' address per spec
|
||||
from = EncodedStringValue(Utils.getMyPhoneNumber(context).orEmpty())
|
||||
}
|
||||
|
||||
val pdu = PduComposer(context, ackInd).make()
|
||||
|
||||
// pack m-acknowledge.ind and send it
|
||||
sendResponsePdu(context, subscriptionId, notificationInd.contentLocation, pdu)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.e("error creating m-acknowledge.ind", e)
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendNotifyRespInd(
|
||||
context: Context, subscriptionId: Int, notificationInd: NotificationInd
|
||||
) {
|
||||
Timber.v("create m-notifyresp.ind")
|
||||
|
||||
try {
|
||||
if (notificationInd.transactionId != null) {
|
||||
val notifyRespInd = NotifyRespInd(
|
||||
PduHeaders.CURRENT_MMS_VERSION,
|
||||
notificationInd.transactionId,
|
||||
PduHeaders.STATUS_RETRIEVED
|
||||
)
|
||||
|
||||
val pdu = PduComposer(context, notifyRespInd).make()
|
||||
|
||||
// pack m-notifyresp.ind and send it
|
||||
sendResponsePdu(context, subscriptionId, notificationInd.contentLocation, pdu)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.e("error creating m-notifyresp.ind", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getForegroundInfo() = ForegroundInfo(
|
||||
0,
|
||||
notificationManager.getForegroundNotificationForWorkersOnOlderAndroids()
|
||||
|
||||
@@ -22,8 +22,13 @@ import android.content.Context
|
||||
import androidx.work.ForegroundInfo
|
||||
import androidx.work.Worker
|
||||
import androidx.work.WorkerParameters
|
||||
import dev.octoshrimpy.quik.interactor.ReceiveSms
|
||||
import dev.octoshrimpy.quik.blocking.BlockingClient
|
||||
import dev.octoshrimpy.quik.interactor.UpdateBadge
|
||||
import dev.octoshrimpy.quik.manager.NotificationManager
|
||||
import dev.octoshrimpy.quik.manager.ShortcutManager
|
||||
import dev.octoshrimpy.quik.repository.ConversationRepository
|
||||
import dev.octoshrimpy.quik.repository.MessageRepository
|
||||
import dev.octoshrimpy.quik.util.Preferences
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -33,20 +38,82 @@ class ReceiveSmsWorker(appContext: Context, workerParams: WorkerParameters)
|
||||
const val INPUT_DATA_KEY_MESSAGE_ID = "messageId"
|
||||
}
|
||||
|
||||
@Inject lateinit var receiveSms: ReceiveSms
|
||||
@Inject lateinit var conversationRepo: ConversationRepository
|
||||
@Inject lateinit var blockingClient: BlockingClient
|
||||
@Inject lateinit var prefs: Preferences
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
@Inject lateinit var notificationManager: NotificationManager
|
||||
@Inject lateinit var updateBadge: UpdateBadge
|
||||
@Inject lateinit var shortcutManager: ShortcutManager
|
||||
|
||||
override fun doWork(): Result {
|
||||
Timber.v("started")
|
||||
|
||||
val messageId = inputData.getLong(INPUT_DATA_KEY_MESSAGE_ID, -1)
|
||||
if (messageId == -1L) {
|
||||
Timber.v("failed. message id was -1")
|
||||
if (messageId < 0) {
|
||||
Timber.v("failed. message id was {messageId}")
|
||||
return Result.failure(inputData)
|
||||
}
|
||||
|
||||
// process the new message
|
||||
receiveSms.execute(messageId)
|
||||
val message = messageRepo.getMessage(messageId) ?: return Result.failure(inputData)
|
||||
|
||||
val action = blockingClient.shouldBlock(message.address).blockingGet()
|
||||
|
||||
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(listOf(message.id))
|
||||
return Result.failure(inputData)
|
||||
}
|
||||
|
||||
action is BlockingClient.Action.Block -> {
|
||||
// blocked
|
||||
Timber.v("address is blocked")
|
||||
messageRepo.markRead(listOf(message.threadId))
|
||||
conversationRepo.markBlocked(
|
||||
listOf(message.threadId),
|
||||
prefs.blockingManager.get(),
|
||||
action.reason
|
||||
)
|
||||
}
|
||||
|
||||
action is BlockingClient.Action.Unblock -> {
|
||||
// unblock
|
||||
Timber.v("unblock conversation if blocked")
|
||||
conversationRepo.markUnblocked(message.threadId)
|
||||
}
|
||||
}
|
||||
|
||||
// update and fetch conversation
|
||||
conversationRepo.updateConversations(listOf(message.threadId))
|
||||
val conversation = conversationRepo.getOrCreateConversation(message.threadId)
|
||||
?: return Result.failure(inputData)
|
||||
|
||||
// don't notify (continue) for blocked conversations
|
||||
if (conversation.blocked) {
|
||||
Timber.v("no notifications for blocked")
|
||||
return Result.failure(inputData)
|
||||
}
|
||||
|
||||
// unarchive conversation if necessary
|
||||
if (conversation.archived) {
|
||||
Timber.v("conversation unarchived")
|
||||
conversationRepo.markUnarchived(listOf(conversation.id))
|
||||
}
|
||||
|
||||
// update/create notification
|
||||
Timber.v("update/create notification")
|
||||
notificationManager.update(conversation.id)
|
||||
|
||||
// update shortcuts
|
||||
Timber.v("update shortcuts")
|
||||
shortcutManager.updateShortcuts()
|
||||
shortcutManager.reportShortcutUsed(conversation.id)
|
||||
|
||||
// update the badge and widget
|
||||
Timber.v("update badge and widget")
|
||||
updateBadge.execute(Unit)
|
||||
|
||||
Timber.v("finished")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user