Fix implicit broadcast usage for SMS intents

This commit is contained in:
Moez Bhatti
2018-08-01 12:07:13 -04:00
parent 4834d548da
commit 4f8afcf7df
9 changed files with 51 additions and 56 deletions
@@ -22,16 +22,14 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import com.klinker.android.send_message.BroadcastUtils
import com.moez.QKSMS.service.SendScheduledMessageService
import javax.inject.Inject
class AlarmManagerImpl @Inject constructor(private val context: Context) : AlarmManager {
override fun getScheduledMessageIntent(id: Long): PendingIntent {
val action = "com.moez.QKSMS.SEND_SCHEDULED_MESSAGE"
val intent = Intent(action).putExtra("id", id)
BroadcastUtils.addClassName(context, intent, action)
return PendingIntent.getBroadcast(context, id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
val intent = Intent(context, SendScheduledMessageService::class.java).putExtra("id", id)
return PendingIntent.getService(context, id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
override fun setAlarm(date: Long, intent: PendingIntent) {
@@ -29,11 +29,16 @@ import javax.inject.Inject
class SmsDeliveredReceiver : BroadcastReceiver() {
companion object {
const val ACTION = "com.moez.QKSMS.SMS_DELIVERED"
}
@Inject lateinit var markDelivered: MarkDelivered
@Inject lateinit var markDeliveryFailed: MarkDeliveryFailed
override fun onReceive(context: Context, intent: Intent) {
AndroidInjection.inject(this, context)
context.unregisterReceiver(this)
val id = intent.getLongExtra("id", 0L)
@@ -30,11 +30,16 @@ import javax.inject.Inject
class SmsSentReceiver : BroadcastReceiver() {
companion object {
const val ACTION = "com.moez.QKSMS.SMS_SENT"
}
@Inject lateinit var markSent: MarkSent
@Inject lateinit var markFailed: MarkFailed
override fun onReceive(context: Context, intent: Intent) {
AndroidInjection.inject(this, context)
context.unregisterReceiver(this)
val id = intent.getLongExtra("id", 0L)
@@ -24,13 +24,13 @@ import android.content.ContentUris
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.provider.Telephony
import android.telephony.PhoneNumberUtils
import android.telephony.SmsManager
import com.google.android.mms.ContentType
import com.google.android.mms.MMSPart
import com.klinker.android.send_message.BroadcastUtils
import com.klinker.android.send_message.SmsManagerFactory
import com.klinker.android.send_message.StripAccents
import com.klinker.android.send_message.Transaction
@@ -41,6 +41,9 @@ import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.Message
import com.moez.QKSMS.model.MmsPart
import com.moez.QKSMS.receiver.SmsDeliveredReceiver
import com.moez.QKSMS.receiver.SmsSentReceiver
import com.moez.QKSMS.service.SendSmsService
import com.moez.QKSMS.util.ImageUtils
import com.moez.QKSMS.util.Preferences
import com.moez.QKSMS.util.tryOrNull
@@ -265,16 +268,14 @@ class MessageRepositoryImpl @Inject constructor(
?: arrayListOf()
val sentIntents = parts.map {
val action = "com.moez.QKSMS.SMS_SENT"
val intent = Intent(action).putExtra("id", message.id)
BroadcastUtils.addClassName(context, intent, action)
context.registerReceiver(SmsSentReceiver(), IntentFilter(SmsSentReceiver.ACTION))
val intent = Intent(SmsSentReceiver.ACTION).putExtra("id", message.id)
PendingIntent.getBroadcast(context, message.id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
val deliveredIntents = parts.map {
val action = "com.moez.QKSMS.SMS_DELIVERED"
val intent = Intent(action).putExtra("id", message.id)
BroadcastUtils.addClassName(context, intent, action)
context.registerReceiver(SmsDeliveredReceiver(), IntentFilter(SmsDeliveredReceiver.ACTION))
val intent = Intent(SmsDeliveredReceiver.ACTION).putExtra("id", message.id)
val pendingIntent = PendingIntent.getBroadcast(context, message.id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
if (prefs.delivery.get()) pendingIntent else null
}
@@ -288,10 +289,8 @@ class MessageRepositoryImpl @Inject constructor(
}
private fun getIntentForDelayedSms(id: Long): PendingIntent {
val action = "com.moez.QKSMS.SEND_SMS"
val intent = Intent(action).putExtra("id", id)
BroadcastUtils.addClassName(context, intent, action)
return PendingIntent.getBroadcast(context, id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
val intent = Intent(context, SendSmsService::class.java).putExtra("id", id)
return PendingIntent.getService(context, id.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
override fun insertSentSms(subId: Int, threadId: Long, address: String, body: String, date: Long): Message {
@@ -16,27 +16,25 @@
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.receiver
package com.moez.QKSMS.service
import android.content.BroadcastReceiver
import android.content.Context
import android.app.IntentService
import android.content.Intent
import com.moez.QKSMS.interactor.SendScheduledMessage
import com.moez.QKSMS.repository.MessageRepository
import dagger.android.AndroidInjection
import javax.inject.Inject
class SendScheduledMessageReceiver : BroadcastReceiver() {
class SendScheduledMessageService : IntentService(null) {
@Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var sendScheduledMessage: SendScheduledMessage
override fun onReceive(context: Context, intent: Intent) {
AndroidInjection.inject(this, context)
override fun onHandleIntent(intent: Intent) {
AndroidInjection.inject(this)
intent.getLongExtra("id", -1L).takeIf { it >= 0 }?.let { id ->
val result = goAsync()
sendScheduledMessage.execute(id) { result.finish() }
sendScheduledMessage.buildObservable(id).blockingSubscribe()
}
}
@@ -16,28 +16,26 @@
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.receiver
package com.moez.QKSMS.service
import android.content.BroadcastReceiver
import android.content.Context
import android.app.IntentService
import android.content.Intent
import com.moez.QKSMS.interactor.RetrySending
import com.moez.QKSMS.repository.MessageRepository
import dagger.android.AndroidInjection
import javax.inject.Inject
class SendSmsReceiver : BroadcastReceiver() {
class SendSmsService : IntentService(null) {
@Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var retrySending: RetrySending
override fun onReceive(context: Context, intent: Intent) {
AndroidInjection.inject(this, context)
override fun onHandleIntent(intent: Intent) {
AndroidInjection.inject(this)
val id = intent.getLongExtra("id", 0L)
messageRepo.getMessage(id)?.let { message ->
val result = goAsync()
retrySending.execute(message) { result.finish() }
retrySending.buildObservable(message).blockingSubscribe()
}
}