forked from upstream-mirrors/quik
Add ability to Mark read or Reply from notification
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package com.moez.QKSMS.dagger
|
||||
|
||||
import com.moez.QKSMS.receiver.MarkSeenReceiver
|
||||
import com.moez.QKSMS.receiver.MessageSentReceiver
|
||||
import com.moez.QKSMS.receiver.SmsReceiver
|
||||
import com.moez.QKSMS.receiver.*
|
||||
import com.moez.QKSMS.ui.conversations.ConversationAdapter
|
||||
import com.moez.QKSMS.ui.conversations.ConversationListViewModel
|
||||
import com.moez.QKSMS.ui.messages.MessageAdapter
|
||||
@@ -19,6 +17,8 @@ interface AppComponent {
|
||||
fun inject(receiver: SmsReceiver)
|
||||
fun inject(receiver: MessageSentReceiver)
|
||||
fun inject(receiver: MarkSeenReceiver)
|
||||
fun inject(receiver: MarkReadReceiver)
|
||||
fun inject(receiver: RemoteMessagingReceiver)
|
||||
|
||||
fun inject(activity: MessageListActivity)
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.moez.QKSMS.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.moez.QKSMS.dagger.AppComponentManager
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
class MarkReadReceiver : BroadcastReceiver() {
|
||||
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
AppComponentManager.appComponent.inject(this)
|
||||
|
||||
val threadId = intent.getLongExtra("threadId", 0)
|
||||
messageRepo.markRead(threadId)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.moez.QKSMS.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.support.v4.app.RemoteInput
|
||||
import com.moez.QKSMS.dagger.AppComponentManager
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.util.NotificationManager
|
||||
import javax.inject.Inject
|
||||
|
||||
class RemoteMessagingReceiver : BroadcastReceiver() {
|
||||
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
@Inject lateinit var notificationManager: NotificationManager
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
AppComponentManager.appComponent.inject(this)
|
||||
|
||||
val remoteInput = RemoteInput.getResultsFromIntent(intent)
|
||||
val bundle = intent.extras
|
||||
if (remoteInput != null && bundle != null) {
|
||||
val address = bundle.getString("address")
|
||||
val threadId = bundle.getLong("threadId")
|
||||
val body = remoteInput.getCharSequence("body").toString()
|
||||
messageRepo.markRead(threadId)
|
||||
messageRepo.sendMessage(threadId, address, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,12 @@ import android.graphics.Color
|
||||
import android.os.Build
|
||||
import android.support.v4.app.NotificationCompat
|
||||
import android.support.v4.app.NotificationManagerCompat
|
||||
import android.support.v4.app.RemoteInput
|
||||
import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.receiver.MarkReadReceiver
|
||||
import com.moez.QKSMS.receiver.MarkSeenReceiver
|
||||
import com.moez.QKSMS.receiver.RemoteMessagingReceiver
|
||||
import com.moez.QKSMS.ui.conversations.ConversationListActivity
|
||||
|
||||
|
||||
@@ -39,7 +42,6 @@ class NotificationManager(private val context: Context, private val themeManager
|
||||
}
|
||||
}
|
||||
|
||||
// https://developer.android.com/guide/topics/ui/notifiers/notifications.html
|
||||
fun update(messageRepo: MessageRepository) {
|
||||
messageRepo.getUnreadUnseenMessages()
|
||||
.groupBy { message -> message.threadId }
|
||||
@@ -60,6 +62,10 @@ class NotificationManager(private val context: Context, private val themeManager
|
||||
val seenIntent = Intent(context, MarkSeenReceiver::class.java).putExtra("threadId", threadId)
|
||||
val seenPI = PendingIntent.getBroadcast(context, threadId.toInt() + 20000, seenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val readIntent = Intent(context, MarkReadReceiver::class.java).putExtra("threadId", threadId)
|
||||
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)
|
||||
|
||||
val notification = NotificationCompat.Builder(context, "channel_1")
|
||||
.setColor(themeManager.color)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
@@ -67,10 +73,34 @@ class NotificationManager(private val context: Context, private val themeManager
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(contentPI)
|
||||
.setDeleteIntent(seenPI)
|
||||
.addAction(readAction)
|
||||
.setStyle(style)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 24 && conversation != null) {
|
||||
notification.addAction(getReplyAction(conversation.contacts[0].address, conversation.id))
|
||||
}
|
||||
|
||||
notificationManager.notify(threadId.toInt(), notification.build())
|
||||
}
|
||||
}
|
||||
|
||||
private fun getReplyAction(address: String, threadId: Long): NotificationCompat.Action {
|
||||
val replyIntent = Intent(context, RemoteMessagingReceiver::class.java)
|
||||
.putExtra("address", address)
|
||||
.putExtra("threadId", threadId)
|
||||
val replyPI = PendingIntent.getBroadcast(context, threadId.toInt() + 40000, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
val responseSet = arrayListOf("Hello", "Hey").toTypedArray().sortedArray()
|
||||
val remoteInput = RemoteInput.Builder("body")
|
||||
.setLabel(context.getString(R.string.notification_reply))
|
||||
.setChoices(responseSet)
|
||||
.build()
|
||||
|
||||
return NotificationCompat.Action.Builder(
|
||||
R.drawable.ic_reply_black_24dp,
|
||||
context.getString(R.string.notification_reply), replyPI)
|
||||
.addRemoteInput(remoteInput)
|
||||
.build()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user