forked from upstream-mirrors/quik
Manage notifications for each conversation independently
This commit is contained in:
@@ -45,12 +45,12 @@ class NotificationManager @Inject constructor(
|
|||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val prefs: Preferences,
|
private val prefs: Preferences,
|
||||||
private val colors: Colors,
|
private val colors: Colors,
|
||||||
|
private val messageRepo: MessageRepository,
|
||||||
private val markUnarchived: MarkUnarchived
|
private val markUnarchived: MarkUnarchived
|
||||||
) {
|
) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val DEFAULT_CHANNEL_ID = "channel_1"
|
val DEFAULT_CHANNEL_ID = "channel_1"
|
||||||
|
|
||||||
val VIBRATE_PATTERN = longArrayOf(0, 200, 0, 200)
|
val VIBRATE_PATTERN = longArrayOf(0, 200, 0, 200)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,56 +75,64 @@ class NotificationManager @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun update(messageRepo: MessageRepository) {
|
/**
|
||||||
if (!prefs.notifications.get()) return
|
* Updates the notification for a particular conversation
|
||||||
|
*/
|
||||||
|
fun update(threadId: Long) {
|
||||||
|
// If notifications are disabled, don't do anything
|
||||||
|
if (!prefs.notifications.get()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
messageRepo.getUnreadUnseenMessages()
|
val messages = messageRepo.getUnreadUnseenMessages(threadId)
|
||||||
.groupBy { message -> message.threadId }
|
|
||||||
.forEach { group ->
|
|
||||||
val threadId = group.key
|
|
||||||
val messages = group.value
|
|
||||||
val conversation = messageRepo.getConversation(threadId) ?: return
|
|
||||||
|
|
||||||
if (conversation.archived) {
|
// If there are no messages to be displayed, make sure that the notification is dismissed
|
||||||
markUnarchived.execute(threadId)
|
if (messages.isEmpty()) {
|
||||||
}
|
notificationManager.cancel(threadId.toInt())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
val style = NotificationCompat.MessagingStyle("Me")
|
val conversation = messageRepo.getConversation(threadId) ?: return
|
||||||
messages.forEach { message ->
|
|
||||||
val name = if (message.isMe()) null else conversation.getTitle()
|
|
||||||
style.addMessage(message.body, message.date, name)
|
|
||||||
}
|
|
||||||
|
|
||||||
val contentIntent = Intent(context, ComposeActivity::class.java).putExtra("threadId", threadId)
|
if (conversation.archived) {
|
||||||
val taskStackBuilder = TaskStackBuilder.create(context)
|
markUnarchived.execute(threadId)
|
||||||
taskStackBuilder.addParentStack(ComposeActivity::class.java)
|
}
|
||||||
taskStackBuilder.addNextIntent(contentIntent)
|
|
||||||
val contentPI = taskStackBuilder.getPendingIntent(threadId.toInt() + 10000, PendingIntent.FLAG_UPDATE_CURRENT)
|
|
||||||
|
|
||||||
val seenIntent = Intent(context, MarkSeenReceiver::class.java).putExtra("threadId", threadId)
|
val style = NotificationCompat.MessagingStyle("Me")
|
||||||
val seenPI = PendingIntent.getBroadcast(context, threadId.toInt() + 20000, seenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
messages.forEach { message ->
|
||||||
|
val name = if (message.isMe()) null else conversation.getTitle()
|
||||||
|
style.addMessage(message.body, message.date, name)
|
||||||
|
}
|
||||||
|
|
||||||
val readIntent = Intent(context, MarkReadReceiver::class.java).putExtra("threadId", threadId)
|
val contentIntent = Intent(context, ComposeActivity::class.java).putExtra("threadId", threadId)
|
||||||
val readPI = PendingIntent.getBroadcast(context, threadId.toInt() + 30000, readIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
val taskStackBuilder = TaskStackBuilder.create(context)
|
||||||
val readAction = NotificationCompat.Action(R.drawable.ic_done_black_24dp, context.getString(R.string.notification_read), readPI)
|
taskStackBuilder.addParentStack(ComposeActivity::class.java)
|
||||||
|
taskStackBuilder.addNextIntent(contentIntent)
|
||||||
|
val contentPI = taskStackBuilder.getPendingIntent(threadId.toInt() + 10000, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||||
|
|
||||||
val notification = NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID)
|
val seenIntent = Intent(context, MarkSeenReceiver::class.java).putExtra("threadId", threadId)
|
||||||
.setColor(colors.theme.blockingFirst())
|
val seenPI = PendingIntent.getBroadcast(context, threadId.toInt() + 20000, seenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||||
.setSmallIcon(R.mipmap.ic_launcher)
|
|
||||||
.setNumber(messages.size)
|
|
||||||
.setAutoCancel(true)
|
|
||||||
.setContentIntent(contentPI)
|
|
||||||
.setDeleteIntent(seenPI)
|
|
||||||
.addAction(readAction)
|
|
||||||
.setStyle(style)
|
|
||||||
.setVibrate(if (prefs.vibration.get()) VIBRATE_PATTERN else longArrayOf(0))
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= 24) {
|
val readIntent = Intent(context, MarkReadReceiver::class.java).putExtra("threadId", threadId)
|
||||||
notification.addAction(getReplyAction(conversation.recipients[0]?.address.orEmpty(), conversation.id))
|
val readPI = PendingIntent.getBroadcast(context, threadId.toInt() + 30000, readIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||||
}
|
val readAction = NotificationCompat.Action(R.drawable.ic_done_black_24dp, context.getString(R.string.notification_read), readPI)
|
||||||
|
|
||||||
notificationManager.notify(threadId.toInt(), notification.build())
|
val notification = NotificationCompat.Builder(context, DEFAULT_CHANNEL_ID)
|
||||||
}
|
.setColor(colors.theme.blockingFirst())
|
||||||
|
.setSmallIcon(R.mipmap.ic_launcher)
|
||||||
|
.setNumber(messages.size)
|
||||||
|
.setAutoCancel(true)
|
||||||
|
.setContentIntent(contentPI)
|
||||||
|
.setDeleteIntent(seenPI)
|
||||||
|
.addAction(readAction)
|
||||||
|
.setStyle(style)
|
||||||
|
.setVibrate(if (prefs.vibration.get()) VIBRATE_PATTERN else longArrayOf(0))
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= 24) {
|
||||||
|
notification.addAction(getReplyAction(conversation.recipients[0]?.address.orEmpty(), conversation.id))
|
||||||
|
}
|
||||||
|
|
||||||
|
notificationManager.notify(threadId.toInt(), notification.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getReplyAction(address: String, threadId: Long): NotificationCompat.Action {
|
private fun getReplyAction(address: String, threadId: Long): NotificationCompat.Action {
|
||||||
|
|||||||
@@ -139,11 +139,16 @@ class MessageRepository @Inject constructor(
|
|||||||
.findAllSorted("date")
|
.findAllSorted("date")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getUnreadUnseenMessages(): RealmResults<Message> {
|
/**
|
||||||
|
* Retrieves the list of messages which should be shown in the notification
|
||||||
|
* for a given conversation
|
||||||
|
*/
|
||||||
|
fun getUnreadUnseenMessages(threadId: Long): RealmResults<Message> {
|
||||||
return Realm.getDefaultInstance()
|
return Realm.getDefaultInstance()
|
||||||
.where(Message::class.java)
|
.where(Message::class.java)
|
||||||
.equalTo("seen", false)
|
.equalTo("seen", false)
|
||||||
.equalTo("read", false)
|
.equalTo("read", false)
|
||||||
|
.equalTo("threadId", threadId)
|
||||||
.findAllSorted("date")
|
.findAllSorted("date")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,20 +324,15 @@ class MessageRepository @Inject constructor(
|
|||||||
.subscribe { addMessageFromUri(uri) }
|
.subscribe { addMessageFromUri(uri) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addMessageFromUri(uri: Uri) {
|
fun addMessageFromUri(uri: Uri): Flowable<Message> {
|
||||||
val cursor = context.contentResolver.query(uri, null, null, null, "date DESC")
|
val cursor = context.contentResolver.query(uri, null, null, null, "date DESC")
|
||||||
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
||||||
|
|
||||||
if (cursor.moveToFirst()) {
|
// Map the cursor to a message
|
||||||
val message = cursorToMessage.map(Pair(cursor, columnsMap))
|
return cursor.asFlowable()
|
||||||
|
.map { cursorToMessage.map(Pair(it, columnsMap)) }
|
||||||
// If the conversation isn't in Realm, add it
|
.doOnNext { message -> getConversation(message.threadId) ?: getConversationFromCp(message.threadId) }
|
||||||
getConversation(message.threadId) ?: getConversationFromCp(message.threadId)
|
.doOnNext { message -> message.insertOrUpdate() }
|
||||||
|
|
||||||
message.insertOrUpdate()
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -18,15 +18,20 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.domain.interactor
|
package com.moez.QKSMS.domain.interactor
|
||||||
|
|
||||||
|
import com.moez.QKSMS.common.util.NotificationManager
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class MarkRead @Inject constructor(private val messageRepo: MessageRepository) : Interactor<Long, Unit>() {
|
class MarkRead @Inject constructor(
|
||||||
|
private val messageRepo: MessageRepository,
|
||||||
|
private val notificationManager: NotificationManager
|
||||||
|
) : Interactor<Long, Unit>() {
|
||||||
|
|
||||||
override fun buildObservable(params: Long): Flowable<Unit> {
|
override fun buildObservable(params: Long): Flowable<Unit> {
|
||||||
return Flowable.just(Unit)
|
return Flowable.just(Unit)
|
||||||
.doOnNext { messageRepo.markRead(params) }
|
.doOnNext { messageRepo.markRead(params) }
|
||||||
|
.doOnNext { notificationManager.update(params) }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -45,8 +45,9 @@ class ReceiveMessage @Inject constructor(
|
|||||||
val contentResolver = context.contentResolver
|
val contentResolver = context.contentResolver
|
||||||
return Flowable.just(values)
|
return Flowable.just(values)
|
||||||
.map { contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) }
|
.map { contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) }
|
||||||
.doOnNext { uri -> messageRepo.addMessageFromUri(uri) }
|
.flatMap { uri -> messageRepo.addMessageFromUri(uri) }
|
||||||
.doOnNext { notificationManager.update(messageRepo) }
|
.map { message -> message.threadId }
|
||||||
|
.doOnNext { threadId -> notificationManager.update(threadId) }
|
||||||
.map { Unit }
|
.map { Unit }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user