Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@
</intent-filter>
</receiver>

<receiver
android:name=".receivers.DeleteSmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.simplemobiletools.smsmessenger.action.delete" />
</intent-filter>
</receiver>

<receiver
android:name=".receivers.ScheduledMessageReceiver"
android:exported="false" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,13 @@ fun Context.getThreadId(addresses: Set<String>): Long {
}
}

fun Context.showReceivedMessageNotification(address: String, body: String, threadId: Long, bitmap: Bitmap?) {
fun Context.showReceivedMessageNotification(messageId: Long, address: String, body: String, threadId: Long, bitmap: Bitmap?) {
val privateCursor = getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true)
ensureBackgroundThread {
val senderName = getNameFromAddress(address, privateCursor)

Handler(Looper.getMainLooper()).post {
notificationHelper.showMessageNotification(address, body, threadId, bitmap, senderName)
notificationHelper.showMessageNotification(messageId, address, body, threadId, bitmap, senderName)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const val WAS_DB_CLEARED = "was_db_cleared_2"
const val EXTRA_VCARD_URI = "vcard"
const val SCHEDULED_MESSAGE_ID = "scheduled_message_id"
const val SOFT_KEYBOARD_HEIGHT = "soft_keyboard_height"
const val IS_MMS = "is_mms"
const val MESSAGE_ID = "message_id"

private const val PATH = "com.simplemobiletools.smsmessenger.action."
const val MARK_AS_READ = PATH + "mark_as_read"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.ThreadActivity
import com.simplemobiletools.smsmessenger.extensions.config
import com.simplemobiletools.smsmessenger.messaging.isShortCodeWithLetters
import com.simplemobiletools.smsmessenger.receivers.DeleteSmsReceiver
import com.simplemobiletools.smsmessenger.receivers.DirectReplyReceiver
import com.simplemobiletools.smsmessenger.receivers.MarkAsReadReceiver

Expand All @@ -35,7 +36,7 @@ class NotificationHelper(private val context: Context) {
.build()

@SuppressLint("NewApi")
fun showMessageNotification(address: String, body: String, threadId: Long, bitmap: Bitmap?, sender: String?, alertOnlyOnce: Boolean = false) {
fun showMessageNotification(messageId: Long, address: String, body: String, threadId: Long, bitmap: Bitmap?, sender: String?, alertOnlyOnce: Boolean = false) {
maybeCreateChannel(name = context.getString(R.string.channel_received_sms))

val notificationId = threadId.hashCode()
Expand All @@ -52,8 +53,16 @@ class NotificationHelper(private val context: Context) {
val markAsReadPendingIntent =
PendingIntent.getBroadcast(context, notificationId, markAsReadIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)

val deleteSmsIntent = Intent(context, DeleteSmsReceiver::class.java).apply {
putExtra(THREAD_ID, threadId)
putExtra(MESSAGE_ID, messageId)
}
val deleteSmsPendingIntent =
PendingIntent.getBroadcast(context, notificationId, deleteSmsIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)

var replyAction: NotificationCompat.Action? = null
if (isNougatPlus() && !isShortCodeWithLetters(address)) {
val isNoReplySms = isShortCodeWithLetters(address)
if (isNougatPlus() && !isNoReplySms) {
val replyLabel = context.getString(R.string.reply)
val remoteInput = RemoteInput.Builder(REPLY)
.setLabel(replyLabel)
Expand Down Expand Up @@ -112,7 +121,10 @@ class NotificationHelper(private val context: Context) {

builder.addAction(R.drawable.ic_check_vector, context.getString(R.string.mark_as_read), markAsReadPendingIntent)
.setChannelId(NOTIFICATION_CHANNEL)

if (isNoReplySms) {
builder.addAction(R.drawable.ic_delete_vector, context.getString(R.string.delete), deleteSmsPendingIntent)
.setChannelId(NOTIFICATION_CHANNEL)
}
notificationManager.notify(notificationId, builder.build())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.simplemobiletools.smsmessenger.receivers

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.simplemobiletools.commons.extensions.notificationManager
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.smsmessenger.extensions.conversationsDB
import com.simplemobiletools.smsmessenger.extensions.deleteMessage
import com.simplemobiletools.smsmessenger.extensions.updateLastConversationMessage
import com.simplemobiletools.smsmessenger.extensions.updateUnreadCountBadge
import com.simplemobiletools.smsmessenger.helpers.IS_MMS
import com.simplemobiletools.smsmessenger.helpers.MESSAGE_ID
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.helpers.refreshMessages

class DeleteSmsReceiver: BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
val threadId = intent.getLongExtra(THREAD_ID, 0L)
val messageId = intent.getLongExtra(MESSAGE_ID, 0L)
val isMms = intent.getBooleanExtra(IS_MMS, false)
context.notificationManager.cancel(threadId.hashCode())
ensureBackgroundThread {
context.deleteMessage(messageId, isMms)
context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations())
context.updateLastConversationMessage(threadId)
refreshMessages()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ class DirectReplyReceiver : BroadcastReceiver() {
}

ensureBackgroundThread {
var messageId: Long = 0L
try {
context.sendMessageCompat(body, listOf(address), subscriptionId, emptyList())
val message = context.getMessages(threadId, getImageResolutions = false, includeScheduledMessages = false, limit = 1).lastOrNull()
if (message != null) {
context.messagesDB.insertOrUpdate(message)
messageId = message.id
}
} catch (e: Exception) {
context.showErrorToast(e)
Expand All @@ -50,7 +52,7 @@ class DirectReplyReceiver : BroadcastReceiver() {
val photoUri = SimpleContactsHelper(context).getPhotoUriFromPhoneNumber(address)
val bitmap = context.getNotificationBitmap(photoUri)
Handler(Looper.getMainLooper()).post {
context.notificationHelper.showMessageNotification(address, body, threadId, bitmap, sender = null, alertOnlyOnce = true)
context.notificationHelper.showMessageNotification(messageId, address, body, threadId, bitmap, sender = null, alertOnlyOnce = true)
}

context.markThreadMessagesRead(threadId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() {
}

Handler(Looper.getMainLooper()).post {
context.showReceivedMessageNotification(address, mms.body, mms.threadId, glideBitmap)
context.showReceivedMessageNotification(mms.id, address, mms.body, mms.threadId, glideBitmap)
val conversation = context.getConversations(mms.threadId).firstOrNull() ?: return@post
ensureBackgroundThread {
context.insertOrUpdateConversation(conversation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ class SmsReceiver : BroadcastReceiver() {
Message(newMessageId, body, type, status, participants, messageDate, false, threadId, false, null, senderName, photoUri, subscriptionId)
context.messagesDB.insertOrUpdate(message)
refreshMessages()
context.showReceivedMessageNotification(newMessageId, address, body, threadId, bitmap)
}

context.showReceivedMessageNotification(address, body, threadId, bitmap)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<string name="mark_as_read">Mark as Read</string>
<string name="mark_as_unread">Mark as Unread</string>
<string name="me">Me</string>
<string name="delete">Delete</string>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to add this string at all, it comes from Simple Commons. Just try referencing it in the code and you will see that it works.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Didn`t know, fixed it

<!-- Confirmation dialog -->
<string name="delete_whole_conversation_confirmation">Are you sure you want to delete all messages of this conversation?</string>
<!-- Are you sure you want to delete 5 conversations? -->
Expand Down