forked from upstream-mirrors/quik
Display notifications for incoming messages, mark seen when notification dismissed
This commit is contained in:
@@ -61,6 +61,7 @@
|
|||||||
</receiver>
|
</receiver>
|
||||||
<receiver android:name=".receiver.MessageSentReceiver" />
|
<receiver android:name=".receiver.MessageSentReceiver" />
|
||||||
<receiver android:name=".receiver.MessageDeliveredReceiver" />
|
<receiver android:name=".receiver.MessageDeliveredReceiver" />
|
||||||
|
<receiver android:name=".receiver.MarkSeenReceiver" />
|
||||||
|
|
||||||
<service android:name=".service.HeadlessSmsSendService"
|
<service android:name=".service.HeadlessSmsSendService"
|
||||||
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
|
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.moez.QKSMS.dagger
|
package com.moez.QKSMS.dagger
|
||||||
|
|
||||||
|
import com.moez.QKSMS.receiver.MarkSeenReceiver
|
||||||
import com.moez.QKSMS.receiver.MessageSentReceiver
|
import com.moez.QKSMS.receiver.MessageSentReceiver
|
||||||
import com.moez.QKSMS.receiver.SmsReceiver
|
import com.moez.QKSMS.receiver.SmsReceiver
|
||||||
import com.moez.QKSMS.ui.conversations.ConversationAdapter
|
import com.moez.QKSMS.ui.conversations.ConversationAdapter
|
||||||
@@ -17,6 +18,7 @@ interface AppComponent {
|
|||||||
|
|
||||||
fun inject(receiver: SmsReceiver)
|
fun inject(receiver: SmsReceiver)
|
||||||
fun inject(receiver: MessageSentReceiver)
|
fun inject(receiver: MessageSentReceiver)
|
||||||
|
fun inject(receiver: MarkSeenReceiver)
|
||||||
|
|
||||||
fun inject(activity: MessageListActivity)
|
fun inject(activity: MessageListActivity)
|
||||||
|
|
||||||
|
|||||||
@@ -41,14 +41,14 @@ class AppModule(var application: Application) {
|
|||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideNotificationHelper(context: Context, messageRepository: MessageRepository): NotificationManager {
|
fun provideNotificationHelper(context: Context, themeManager: ThemeManager): NotificationManager {
|
||||||
return NotificationManager(context, messageRepository)
|
return NotificationManager(context, themeManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Provides
|
@Provides
|
||||||
fun provideMessageRepository(context: Context): MessageRepository {
|
fun provideMessageRepository(context: Context, notificationManager: NotificationManager): MessageRepository {
|
||||||
return MessageRepository(context)
|
return MessageRepository(context, notificationManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import com.moez.QKSMS.data.sync.MessageColumns
|
|||||||
import com.moez.QKSMS.data.sync.SyncManager
|
import com.moez.QKSMS.data.sync.SyncManager
|
||||||
import com.moez.QKSMS.receiver.MessageDeliveredReceiver
|
import com.moez.QKSMS.receiver.MessageDeliveredReceiver
|
||||||
import com.moez.QKSMS.receiver.MessageSentReceiver
|
import com.moez.QKSMS.receiver.MessageSentReceiver
|
||||||
|
import com.moez.QKSMS.util.NotificationManager
|
||||||
import com.moez.QKSMS.util.extensions.asFlowable
|
import com.moez.QKSMS.util.extensions.asFlowable
|
||||||
import com.moez.QKSMS.util.extensions.insertOrUpdate
|
import com.moez.QKSMS.util.extensions.insertOrUpdate
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
@@ -22,7 +23,7 @@ import io.realm.Realm
|
|||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
import io.realm.Sort
|
import io.realm.Sort
|
||||||
|
|
||||||
class MessageRepository(val context: Context) {
|
class MessageRepository(val context: Context, val notificationManager: NotificationManager) {
|
||||||
|
|
||||||
fun getConversationMessagesAsync(): RealmResults<Message> {
|
fun getConversationMessagesAsync(): RealmResults<Message> {
|
||||||
return Realm.getDefaultInstance()
|
return Realm.getDefaultInstance()
|
||||||
@@ -70,7 +71,10 @@ class MessageRepository(val context: Context) {
|
|||||||
Flowable.just(values)
|
Flowable.just(values)
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.map { contentResolver.insert(Sms.Inbox.CONTENT_URI, values) }
|
.map { contentResolver.insert(Sms.Inbox.CONTENT_URI, values) }
|
||||||
.subscribe { uri -> copyMessageToRealm(uri) }
|
.subscribe { uri ->
|
||||||
|
copyMessageToRealm(uri)
|
||||||
|
notificationManager.update(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun markAllSeen() {
|
fun markAllSeen() {
|
||||||
@@ -100,6 +104,41 @@ class MessageRepository(val context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun markSeen(threadId: Long) {
|
||||||
|
// Messages in SMS ContentProvider
|
||||||
|
val projection = arrayOf(BaseColumns._ID)
|
||||||
|
val selection = "${Sms.THREAD_ID} = $threadId AND ${Sms.SEEN} = 0"
|
||||||
|
val contentResolver = context.contentResolver
|
||||||
|
contentResolver.query(Sms.Inbox.CONTENT_URI, projection, selection, null, null)
|
||||||
|
.asFlowable()
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.map { cursor -> cursor.getLong(0) }
|
||||||
|
.map { id -> Uri.withAppendedPath(Sms.CONTENT_URI, id.toString()) }
|
||||||
|
.subscribe { uri ->
|
||||||
|
val values = ContentValues()
|
||||||
|
values.put(Sms.SEEN, true)
|
||||||
|
contentResolver.update(uri, values, null, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO also need to mark MMS in ContentProvider as Seen
|
||||||
|
|
||||||
|
// Messages in Realm
|
||||||
|
Schedulers.io().scheduleDirect {
|
||||||
|
val realm = Realm.getDefaultInstance()
|
||||||
|
val messages = realm.where(Message::class.java)
|
||||||
|
.equalTo("threadId", threadId)
|
||||||
|
.equalTo("seen", false)
|
||||||
|
.findAll()
|
||||||
|
|
||||||
|
realm.executeTransaction {
|
||||||
|
messages.forEach { message ->
|
||||||
|
message.seen = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
realm.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun markRead(threadId: Long) {
|
fun markRead(threadId: Long) {
|
||||||
// Messages in SMS ContentProvider
|
// Messages in SMS ContentProvider
|
||||||
val projection = arrayOf(BaseColumns._ID)
|
val projection = arrayOf(BaseColumns._ID)
|
||||||
|
|||||||
@@ -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 MarkSeenReceiver : BroadcastReceiver() {
|
||||||
|
|
||||||
|
@Inject lateinit var messageRepo: MessageRepository
|
||||||
|
|
||||||
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
|
AppComponentManager.appComponent.inject(this)
|
||||||
|
|
||||||
|
val threadId = intent.getLongExtra("threadId", 0)
|
||||||
|
messageRepo.markSeen(threadId)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,26 +2,23 @@ package com.moez.QKSMS.util
|
|||||||
|
|
||||||
import android.app.NotificationChannel
|
import android.app.NotificationChannel
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
|
import android.app.PendingIntent
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.support.v4.app.NotificationCompat
|
import android.support.v4.app.NotificationCompat
|
||||||
import android.support.v4.app.NotificationManagerCompat
|
import android.support.v4.app.NotificationManagerCompat
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.moez.QKSMS.dagger.AppComponentManager
|
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
import javax.inject.Inject
|
import com.moez.QKSMS.receiver.MarkSeenReceiver
|
||||||
|
|
||||||
|
|
||||||
class NotificationManager(val context: Context, val messageRepo: MessageRepository) {
|
class NotificationManager(private val context: Context, private val themeManager: ThemeManager) {
|
||||||
|
|
||||||
@Inject lateinit var themeManager: ThemeManager
|
|
||||||
|
|
||||||
private val notificationManager = NotificationManagerCompat.from(context)
|
private val notificationManager = NotificationManagerCompat.from(context)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
AppComponentManager.appComponent.inject(this)
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= 26) {
|
if (Build.VERSION.SDK_INT >= 26) {
|
||||||
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
|
|
||||||
@@ -42,22 +39,28 @@ class NotificationManager(val context: Context, val messageRepo: MessageReposito
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://developer.android.com/guide/topics/ui/notifiers/notifications.html
|
// https://developer.android.com/guide/topics/ui/notifiers/notifications.html
|
||||||
fun update() {
|
fun update(messageRepo: MessageRepository) {
|
||||||
messageRepo.getUnreadUnseenMessages().groupBy { message -> message.threadId }.forEach { conversation ->
|
messageRepo.getUnreadUnseenMessages()
|
||||||
|
.groupBy { message -> message.threadId }
|
||||||
|
.forEach { conversation ->
|
||||||
|
|
||||||
val style = NotificationCompat.MessagingStyle("Me")
|
val style = NotificationCompat.MessagingStyle("Me")
|
||||||
conversation.value.forEach { message ->
|
conversation.value.forEach { message ->
|
||||||
val name = if (message.isMe()) null else "Person"
|
val name = if (message.isMe()) null else "Person"
|
||||||
style.addMessage(message.body, message.date, name)
|
style.addMessage(message.body, message.date, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
val notification = NotificationCompat.Builder(context, "channel_1")
|
val seenIntent = Intent(context, MarkSeenReceiver::class.java).putExtra("threadId", conversation.key)
|
||||||
.setColor(themeManager.color)
|
val seenPI = PendingIntent.getBroadcast(context, conversation.key.toInt(), seenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||||
.setSmallIcon(R.mipmap.ic_launcher)
|
|
||||||
.setStyle(style)
|
|
||||||
|
|
||||||
notificationManager.notify(conversation.key.toInt(), notification.build())
|
val notification = NotificationCompat.Builder(context, "channel_1")
|
||||||
}
|
.setColor(themeManager.color)
|
||||||
|
.setSmallIcon(R.mipmap.ic_launcher)
|
||||||
|
.setDeleteIntent(seenPI)
|
||||||
|
.setStyle(style)
|
||||||
|
|
||||||
|
notificationManager.notify(conversation.key.toInt(), notification.build())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user