From 035daae00b0a5e611888c66d50b46af595de2648 Mon Sep 17 00:00:00 2001 From: Janne Koschinski <janne@kuschku.de> Date: Thu, 14 Jun 2018 01:57:07 +0200 Subject: [PATCH] Fixes #44 --- .../service/QuasselNotificationBackend.kt | 50 ++++++++++++++----- .../quasseldroid/service/QuasselService.kt | 1 + .../QuasseldroidNotificationManager.kt | 23 +++++---- .../quassel/syncables/RpcHandler.kt | 2 +- .../libquassel/session/NotificationManager.kt | 2 +- .../persistence/QuasselDatabase.kt | 3 ++ 6 files changed, 56 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/de/kuschku/quasseldroid/service/QuasselNotificationBackend.kt b/app/src/main/java/de/kuschku/quasseldroid/service/QuasselNotificationBackend.kt index 9dadcec95..f954dbfac 100644 --- a/app/src/main/java/de/kuschku/quasseldroid/service/QuasselNotificationBackend.kt +++ b/app/src/main/java/de/kuschku/quasseldroid/service/QuasselNotificationBackend.kt @@ -126,7 +126,7 @@ class QuasselNotificationBackend @Inject constructor( Message_Type.Notice).toInt(), 0 ) { - processMessages(session, *it.toTypedArray()) + processMessages(session, false, *it.toTypedArray()) false } } @@ -140,7 +140,7 @@ class QuasselNotificationBackend @Inject constructor( Message_Type.Notice).toInt(), Message_Flag.of(Message_Flag.Highlight).toInt() ) { - processMessages(session, *it.toTypedArray()) + processMessages(session, false, *it.toTypedArray()) false } } @@ -150,6 +150,15 @@ class QuasselNotificationBackend @Inject constructor( } } } + + // Cleanup + val removedBuffers = database.notifications().buffers().minus(buffers.map(BufferInfo::bufferId)) + for (removedBuffer in removedBuffers) { + database.notifications().markRead(removedBuffer, MsgId.MAX_VALUE) + } + + // Update notifications to have actions + showConnectedNotifications() } } @@ -178,7 +187,7 @@ class QuasselNotificationBackend @Inject constructor( } @Synchronized - override fun processMessages(session: Session, vararg messages: Message) { + override fun processMessages(session: Session, show: Boolean, vararg messages: Message) { val now = Instant.now() val results = messages.filter { val level = it.bufferInfo.type.let { @@ -229,18 +238,33 @@ class QuasselNotificationBackend @Inject constructor( ) } database.notifications().save(*results.toTypedArray()) - executor.schedule( - { - results.map(QuasselDatabase.NotificationData::bufferId).distinct() - .forEach(this::showNotification) - }, - session.lag.value * 2, - TimeUnit.MILLISECONDS - ) + if (show) { + executor.schedule( + { + results.map(QuasselDatabase.NotificationData::bufferId).distinct().forEach { buffer -> + this.showNotification(buffer) + } + }, + session.lag.value * 2, + TimeUnit.MILLISECONDS + ) + } + } + + fun showConnectedNotifications() { + database.notifications().buffers().forEach { buffer -> + this.showNotification(buffer, true) + } + } + + fun showDisconnectedNotifications() { + database.notifications().buffers().forEach { buffer -> + this.showNotification(buffer, false) + } } @Synchronized - private fun showNotification(buffer: BufferId) { + private fun showNotification(buffer: BufferId, isConnected: Boolean = true) { val data = database.notifications().all(buffer) data.lastOrNull()?.let { // Only send a loud notification if it has any new messages @@ -310,7 +334,7 @@ class QuasselNotificationBackend @Inject constructor( ) } val notification = notificationHandler.notificationMessage( - notificationSettings, bufferInfo, notificationData, isLoud + notificationSettings, bufferInfo, notificationData, isLoud, isConnected ) notificationHandler.notify(notification) } ?: notificationHandler.remove(buffer) diff --git a/app/src/main/java/de/kuschku/quasseldroid/service/QuasselService.kt b/app/src/main/java/de/kuschku/quasseldroid/service/QuasselService.kt index f6d7af6fc..a2b53fbd5 100644 --- a/app/src/main/java/de/kuschku/quasseldroid/service/QuasselService.kt +++ b/app/src/main/java/de/kuschku/quasseldroid/service/QuasselService.kt @@ -366,6 +366,7 @@ class QuasselService : DaggerLifecycleService(), if (it == ConnectionState.CLOSED) { scheduleReconnect() + notificationBackend.showDisconnectedNotifications() } } }) diff --git a/app/src/main/java/de/kuschku/quasseldroid/service/QuasseldroidNotificationManager.kt b/app/src/main/java/de/kuschku/quasseldroid/service/QuasseldroidNotificationManager.kt index 6ee03952e..27add767e 100644 --- a/app/src/main/java/de/kuschku/quasseldroid/service/QuasseldroidNotificationManager.kt +++ b/app/src/main/java/de/kuschku/quasseldroid/service/QuasseldroidNotificationManager.kt @@ -108,7 +108,8 @@ class QuasseldroidNotificationManager @Inject constructor(private val context: C } fun notificationMessage(notificationSettings: NotificationSettings, bufferInfo: BufferInfo, - notifications: List<NotificationMessage>, isLoud: Boolean): Handle { + notifications: List<NotificationMessage>, isLoud: Boolean, + isConnected: Boolean): Handle { val pendingIntentOpen = PendingIntent.getActivity( context.applicationContext, System.currentTimeMillis().toInt(), @@ -198,15 +199,17 @@ class QuasseldroidNotificationManager @Inject constructor(private val context: C } } ) - .addAction(0, translatedLocale.getString(R.string.label_mark_read), markReadPendingIntent) - .letIf(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - it.addAction( - NotificationCompat.Action.Builder( - 0, - translatedLocale.getString(R.string.label_reply), - replyPendingIntent - ).addRemoteInput(remoteInput).build() - ) + .letIf(isConnected) { + it.addAction(0, translatedLocale.getString(R.string.label_mark_read), markReadPendingIntent) + .letIf(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + it.addAction( + NotificationCompat.Action.Builder( + 0, + translatedLocale.getString(R.string.label_reply), + replyPendingIntent + ).addRemoteInput(remoteInput).build() + ) + } } .setWhen(notifications.last().time.toEpochMilli()) .apply { diff --git a/lib/src/main/java/de/kuschku/libquassel/quassel/syncables/RpcHandler.kt b/lib/src/main/java/de/kuschku/libquassel/quassel/syncables/RpcHandler.kt index a3ab7adc7..fb1dbf081 100644 --- a/lib/src/main/java/de/kuschku/libquassel/quassel/syncables/RpcHandler.kt +++ b/lib/src/main/java/de/kuschku/libquassel/quassel/syncables/RpcHandler.kt @@ -60,7 +60,7 @@ class RpcHandler( override fun displayMsg(message: Message) { session.bufferSyncer.bufferInfoUpdated(message.bufferInfo) backlogStorage.storeMessages(session, message) - notificationManager?.processMessages(session, message) + notificationManager?.processMessages(session, true, message) } override fun createIdentity(identity: Identity, additional: QVariantMap) = diff --git a/lib/src/main/java/de/kuschku/libquassel/session/NotificationManager.kt b/lib/src/main/java/de/kuschku/libquassel/session/NotificationManager.kt index 982505d80..128a270fe 100644 --- a/lib/src/main/java/de/kuschku/libquassel/session/NotificationManager.kt +++ b/lib/src/main/java/de/kuschku/libquassel/session/NotificationManager.kt @@ -25,6 +25,6 @@ import de.kuschku.libquassel.protocol.MsgId interface NotificationManager { fun init(session: Session) - fun processMessages(session: Session, vararg messages: Message) + fun processMessages(session: Session, show: Boolean, vararg messages: Message) fun clear(buffer: BufferId, lastRead: MsgId = MsgId.MAX_VALUE) } diff --git a/persistence/src/main/java/de/kuschku/quasseldroid/persistence/QuasselDatabase.kt b/persistence/src/main/java/de/kuschku/quasseldroid/persistence/QuasselDatabase.kt index aff4e8aba..0ecccd20a 100644 --- a/persistence/src/main/java/de/kuschku/quasseldroid/persistence/QuasselDatabase.kt +++ b/persistence/src/main/java/de/kuschku/quasseldroid/persistence/QuasselDatabase.kt @@ -216,6 +216,9 @@ abstract class QuasselDatabase : RoomDatabase() { @Insert(onConflict = OnConflictStrategy.IGNORE) fun save(vararg entities: NotificationData) + @Query("SELECT DISTINCT bufferId FROM notification") + fun buffers(): List<BufferId> + @Query("SELECT * FROM notification ORDER BY time ASC") fun all(): List<NotificationData> -- GitLab