2021-12-29 20:33:17 +01:00
|
|
|
package io.heckel.ntfy.msg
|
|
|
|
|
|
|
|
import android.content.Context
|
2022-01-08 21:49:07 +01:00
|
|
|
import android.util.Log
|
2021-12-29 20:33:17 +01:00
|
|
|
import io.heckel.ntfy.data.Notification
|
2021-12-29 21:36:47 +01:00
|
|
|
import io.heckel.ntfy.data.Repository
|
2021-12-29 20:33:17 +01:00
|
|
|
import io.heckel.ntfy.data.Subscription
|
2021-12-29 21:36:47 +01:00
|
|
|
import io.heckel.ntfy.up.Distributor
|
2021-12-30 01:05:32 +01:00
|
|
|
import io.heckel.ntfy.util.safeLet
|
2021-12-29 21:36:47 +01:00
|
|
|
|
2021-12-31 02:00:08 +01:00
|
|
|
/**
|
|
|
|
* The notification dispatcher figures out what to do with a notification.
|
|
|
|
* It may display a notification, send out a broadcast, or forward via UnifiedPush.
|
|
|
|
*/
|
2021-12-29 21:36:47 +01:00
|
|
|
class NotificationDispatcher(val context: Context, val repository: Repository) {
|
|
|
|
private val notifier = NotificationService(context)
|
|
|
|
private val broadcaster = BroadcastService(context)
|
|
|
|
private val distributor = Distributor(context)
|
|
|
|
|
|
|
|
fun init() {
|
|
|
|
notifier.createNotificationChannels()
|
|
|
|
}
|
2021-12-29 20:33:17 +01:00
|
|
|
|
|
|
|
fun dispatch(subscription: Subscription, notification: Notification) {
|
2022-01-11 23:00:18 +01:00
|
|
|
Log.d(TAG, "Dispatching $notification for subscription $subscription")
|
|
|
|
|
2022-01-01 16:56:18 +01:00
|
|
|
val muted = getMuted(subscription)
|
|
|
|
val notify = shouldNotify(subscription, notification, muted)
|
|
|
|
val broadcast = shouldBroadcast(subscription)
|
|
|
|
val distribute = shouldDistribute(subscription)
|
2022-01-12 01:37:34 +01:00
|
|
|
val download = shouldDownload(notification)
|
2021-12-29 21:36:47 +01:00
|
|
|
if (notify) {
|
2022-01-04 00:54:18 +01:00
|
|
|
notifier.display(subscription, notification)
|
2021-12-29 21:36:47 +01:00
|
|
|
}
|
|
|
|
if (broadcast) {
|
|
|
|
broadcaster.send(subscription, notification, muted)
|
|
|
|
}
|
|
|
|
if (distribute) {
|
2021-12-30 01:05:32 +01:00
|
|
|
safeLet(subscription.upAppId, subscription.upConnectorToken) { appId, connectorToken ->
|
|
|
|
distributor.sendMessage(appId, connectorToken, notification.message)
|
|
|
|
}
|
2021-12-29 21:36:47 +01:00
|
|
|
}
|
2022-01-08 21:49:07 +01:00
|
|
|
if (download) {
|
2022-01-14 18:32:36 +01:00
|
|
|
DownloadManager.enqueue(context, notification.id, userAction = false)
|
2022-01-08 21:49:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 01:37:34 +01:00
|
|
|
private fun shouldDownload(notification: Notification): Boolean {
|
|
|
|
if (notification.attachment == null) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
val maxAutoDownloadSize = repository.getAutoDownloadMaxSize()
|
|
|
|
when (maxAutoDownloadSize) {
|
|
|
|
Repository.AUTO_DOWNLOAD_ALWAYS -> return true
|
|
|
|
Repository.AUTO_DOWNLOAD_NEVER -> return false
|
|
|
|
else -> {
|
|
|
|
if (notification.attachment.size == null) {
|
2022-01-14 18:32:36 +01:00
|
|
|
return true // DownloadWorker will bail out if attachment is too large!
|
2022-01-12 01:37:34 +01:00
|
|
|
}
|
|
|
|
return notification.attachment.size <= maxAutoDownloadSize
|
|
|
|
}
|
|
|
|
}
|
2021-12-29 21:36:47 +01:00
|
|
|
}
|
|
|
|
|
2022-01-01 16:56:18 +01:00
|
|
|
private fun shouldNotify(subscription: Subscription, notification: Notification, muted: Boolean): Boolean {
|
2021-12-30 17:00:27 +01:00
|
|
|
if (subscription.upAppId != null) {
|
2021-12-29 21:36:47 +01:00
|
|
|
return false
|
|
|
|
}
|
2022-01-01 16:56:18 +01:00
|
|
|
val priority = if (notification.priority > 0) notification.priority else 3
|
|
|
|
if (priority < repository.getMinPriority()) {
|
|
|
|
return false
|
|
|
|
}
|
2021-12-29 21:36:47 +01:00
|
|
|
val detailsVisible = repository.detailViewSubscriptionId.get() == notification.subscriptionId
|
|
|
|
return !detailsVisible && !muted
|
|
|
|
}
|
2021-12-29 20:33:17 +01:00
|
|
|
|
2022-01-01 16:56:18 +01:00
|
|
|
private fun shouldBroadcast(subscription: Subscription): Boolean {
|
|
|
|
if (subscription.upAppId != null) { // Never broadcast for UnifiedPush subscriptions
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return repository.getBroadcastEnabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun shouldDistribute(subscription: Subscription): Boolean {
|
|
|
|
return subscription.upAppId != null // Only distribute for UnifiedPush subscriptions
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getMuted(subscription: Subscription): Boolean {
|
2021-12-29 21:36:47 +01:00
|
|
|
if (repository.isGlobalMuted()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return subscription.mutedUntil == 1L || (subscription.mutedUntil > 1L && subscription.mutedUntil > System.currentTimeMillis()/1000)
|
2021-12-29 20:33:17 +01:00
|
|
|
}
|
2022-01-08 21:49:07 +01:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val TAG = "NtfyNotifDispatch"
|
|
|
|
}
|
2021-12-29 20:33:17 +01:00
|
|
|
}
|