add a Mark As Read notification item

This commit is contained in:
tibbi 2020-05-13 16:04:28 +02:00
parent 98a7095324
commit d4e9c22d13
6 changed files with 55 additions and 9 deletions

View File

@ -154,6 +154,15 @@
android:exported="true"
android:taskAffinity="${applicationId}.SMS_SENT" />
<receiver
android:name=".receivers.MarkAsReadReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.simplemobiletools.smsmessenger.action.mark_as_read" />
</intent-filter>
</receiver>
<activity-alias
android:name=".activities.SplashActivity.Red"
android:enabled="false"

View File

@ -22,9 +22,9 @@ import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.ThreadActivity
import com.simplemobiletools.smsmessenger.helpers.Config
import com.simplemobiletools.smsmessenger.helpers.THREAD_ID
import com.simplemobiletools.smsmessenger.helpers.*
import com.simplemobiletools.smsmessenger.models.*
import com.simplemobiletools.smsmessenger.receivers.MarkAsReadReceiver
import java.util.*
import kotlin.collections.ArrayList
@ -419,7 +419,7 @@ fun Context.getNameAndPhotoFromPhoneNumber(number: String): NamePhoto? {
return NamePhoto(number, null)
}
fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long, read: Int, threadId: Long, type: Int, subscriptionId: Int) {
fun Context.insertNewSMS(address: String, subject: String, body: String, date: Long, read: Int, threadId: Long, type: Int, subscriptionId: Int): Int {
val uri = Sms.CONTENT_URI
val contentValues = ContentValues().apply {
put(Sms.ADDRESS, address)
@ -432,7 +432,8 @@ fun Context.insertNewSMS(address: String, subject: String, body: String, date: L
put(Sms.SUBSCRIPTION_ID, subscriptionId)
}
contentResolver.insert(uri, contentValues)
val newUri = contentResolver.insert(uri, contentValues)
return newUri?.lastPathSegment?.toInt() ?: 0
}
fun Context.deleteConversation(id: Int) {
@ -492,7 +493,7 @@ fun Context.isNumberBlocked(number: String): Boolean {
}
@SuppressLint("NewApi")
fun Context.showReceivedMessageNotification(address: String, body: String, threadID: Int, bitmap: Bitmap? = null) {
fun Context.showReceivedMessageNotification(address: String, body: String, threadID: Int, bitmap: Bitmap?, messageId: Int, isMMS: Boolean) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val channelId = "simple_sms_messenger"
@ -522,6 +523,13 @@ fun Context.showReceivedMessageNotification(address: String, body: String, threa
val summaryText = getString(R.string.new_message)
val sender = getNameAndPhotoFromPhoneNumber(address)?.name ?: ""
val markAsReadIntent = Intent(this, MarkAsReadReceiver::class.java).apply {
action = MARK_AS_READ
putExtra(MESSAGE_ID, messageId)
putExtra(MESSAGE_IS_MMS, isMMS)
}
val markAsReadPendingIntent = PendingIntent.getBroadcast(this, 0, markAsReadIntent, PendingIntent.FLAG_CANCEL_CURRENT)
val largeIcon = bitmap ?: SimpleContactsHelper(this).getContactLetterIcon(sender)
val builder = NotificationCompat.Builder(this, channelId)
.setContentTitle(sender)
@ -535,7 +543,8 @@ fun Context.showReceivedMessageNotification(address: String, body: String, threa
.setCategory(Notification.CATEGORY_MESSAGE)
.setAutoCancel(true)
.setSound(soundUri, AudioManager.STREAM_NOTIFICATION)
.addAction(R.drawable.ic_check_vector, getString(R.string.mark_as_read), markAsReadPendingIntent)
.setChannelId(channelId)
notificationManager.notify(threadID, builder.build())
notificationManager.notify(messageId, builder.build())
}

View File

@ -11,6 +11,11 @@ const val THREAD_ATTACHMENT_URI = "thread_attachment_uri"
const val THREAD_ATTACHMENT_URIS = "thread_attachment_uris"
const val USE_SIM_ID_PREFIX = "use_sim_id_"
private const val PATH = "com.simplemobiletools.smsmessenger.action."
const val MARK_AS_READ = PATH + "mark_as_read"
const val MESSAGE_ID = "message_id"
const val MESSAGE_IS_MMS = "message_is_mms"
// view types for the thread list view
const val THREAD_DATE_TIME = 1
const val THREAD_RECEIVED_MESSAGE = 2

View File

@ -0,0 +1,23 @@
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.smsmessenger.extensions.markMessageRead
import com.simplemobiletools.smsmessenger.helpers.MARK_AS_READ
import com.simplemobiletools.smsmessenger.helpers.MESSAGE_ID
import com.simplemobiletools.smsmessenger.helpers.MESSAGE_IS_MMS
class MarkAsReadReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
MARK_AS_READ -> {
val messageId = intent.getIntExtra(MESSAGE_ID, 0)
val isMMS = intent.getBooleanExtra(MESSAGE_IS_MMS, false)
context.markMessageRead(messageId, isMMS)
context.notificationManager.cancel(messageId)
}
}
}
}

View File

@ -31,7 +31,7 @@ class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() {
null
}
context.showReceivedMessageNotification(address, mms.body, mms.thread, glideBitmap)
context.showReceivedMessageNotification(address, mms.body, mms.thread, glideBitmap, mms.id, true)
}
}

View File

@ -31,8 +31,8 @@ class SmsReceiver : BroadcastReceiver() {
}
if (!context.isNumberBlocked(address)) {
context.insertNewSMS(address, subject, body, date, read, threadId, type, subscriptionId)
context.showReceivedMessageNotification(address, body, threadId.toInt())
val messageId = context.insertNewSMS(address, subject, body, date, read, threadId, type, subscriptionId)
context.showReceivedMessageNotification(address, body, threadId.toInt(), null, messageId, false)
refreshMessages()
}
}