forked from upstream-mirrors/quik
Add ability to Mark read or Reply from notification
This commit is contained in:
@@ -64,6 +64,8 @@
|
||||
<receiver android:name=".receiver.MessageSentReceiver" />
|
||||
<receiver android:name=".receiver.MessageDeliveredReceiver" />
|
||||
<receiver android:name=".receiver.MarkSeenReceiver" />
|
||||
<receiver android:name=".receiver.MarkReadReceiver" />
|
||||
<receiver android:name=".receiver.RemoteMessagingReceiver" />
|
||||
|
||||
<service
|
||||
android:name=".service.HeadlessSmsSendService"
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/>
|
||||
</vector>
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M10,9V5l-7,7 7,7v-4.1c5,0 8.5,1.6 11,5.1 -1,-5 -4,-10 -11,-11z"/>
|
||||
</vector>
|
||||
@@ -54,7 +54,7 @@
|
||||
android:layout_weight="1"
|
||||
android:background="@null"
|
||||
android:gravity="center_vertical"
|
||||
android:hint="@string/message_hint"
|
||||
android:hint="@string/compose_hint"
|
||||
android:minHeight="48dp" />
|
||||
|
||||
<ImageView
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
<string name="app_name">QKSMS</string>
|
||||
|
||||
<string name="message_hint">Write a message</string>
|
||||
<string name="compose_hint">Write a message</string>
|
||||
|
||||
<string name="notification_reply">Reply</string>
|
||||
<string name="notification_read">Mark read</string>
|
||||
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user