2021-12-30 08:33:17 +13:00
|
|
|
package io.heckel.ntfy.msg
|
|
|
|
|
|
|
|
import android.content.Context
|
2022-01-19 08:28:48 +13:00
|
|
|
import io.heckel.ntfy.db.Notification
|
|
|
|
import io.heckel.ntfy.db.Repository
|
|
|
|
import io.heckel.ntfy.db.Subscription
|
2022-02-10 10:20:24 +13:00
|
|
|
import io.heckel.ntfy.util.Log
|
2021-12-30 09:36:47 +13:00
|
|
|
import io.heckel.ntfy.up.Distributor
|
2022-03-14 08:58:19 +13:00
|
|
|
import io.heckel.ntfy.util.decodeBytesMessage
|
2021-12-30 13:05:32 +13:00
|
|
|
import io.heckel.ntfy.util.safeLet
|
2021-12-30 09:36:47 +13:00
|
|
|
|
2021-12-31 14:00:08 +13: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-30 09:36:47 +13: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() {
|
2022-12-01 23:13:20 +13:00
|
|
|
notifier.createDefaultNotificationChannels()
|
|
|
|
}
|
|
|
|
|
2021-12-30 08:33:17 +13:00
|
|
|
fun dispatch(subscription: Subscription, notification: Notification) {
|
2022-01-12 11:00:18 +13:00
|
|
|
Log.d(TAG, "Dispatching $notification for subscription $subscription")
|
|
|
|
|
2022-01-02 04:56:18 +13:00
|
|
|
val muted = getMuted(subscription)
|
|
|
|
val notify = shouldNotify(subscription, notification, muted)
|
|
|
|
val broadcast = shouldBroadcast(subscription)
|
|
|
|
val distribute = shouldDistribute(subscription)
|
2022-07-17 08:32:09 +12:00
|
|
|
val downloadAttachment = shouldDownloadAttachment(notification)
|
|
|
|
val downloadIcon = shouldDownloadIcon(notification)
|
2021-12-30 09:36:47 +13:00
|
|
|
if (notify) {
|
2022-01-04 12:54:18 +13:00
|
|
|
notifier.display(subscription, notification)
|
2021-12-30 09:36:47 +13:00
|
|
|
}
|
|
|
|
if (broadcast) {
|
2022-04-20 01:15:06 +12:00
|
|
|
broadcaster.sendMessage(subscription, notification, muted)
|
2021-12-30 09:36:47 +13:00
|
|
|
}
|
|
|
|
if (distribute) {
|
2021-12-30 13:05:32 +13:00
|
|
|
safeLet(subscription.upAppId, subscription.upConnectorToken) { appId, connectorToken ->
|
2022-03-14 08:58:19 +13:00
|
|
|
distributor.sendMessage(appId, connectorToken, decodeBytesMessage(notification))
|
2021-12-30 13:05:32 +13:00
|
|
|
}
|
2021-12-30 09:36:47 +13:00
|
|
|
}
|
2022-07-17 08:32:09 +12:00
|
|
|
if (downloadAttachment && downloadIcon) {
|
|
|
|
DownloadManager.enqueue(context, notification.id, userAction = false, type = DownloadType.BOTH)
|
|
|
|
} else if (downloadAttachment) {
|
|
|
|
DownloadManager.enqueue(context, notification.id, userAction = false, type = DownloadType.ATTACHMENT)
|
|
|
|
} else if (downloadIcon) {
|
|
|
|
DownloadManager.enqueue(context, notification.id, userAction = false, type = DownloadType.ICON)
|
2022-01-09 09:49:07 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 08:32:09 +12:00
|
|
|
private fun shouldDownloadAttachment(notification: Notification): Boolean {
|
2022-01-12 13:37:34 +13:00
|
|
|
if (notification.attachment == null) {
|
|
|
|
return false
|
|
|
|
}
|
2022-02-11 06:10:27 +13:00
|
|
|
val attachment = notification.attachment
|
|
|
|
if (attachment.expires != null && attachment.expires < System.currentTimeMillis()/1000) {
|
|
|
|
Log.d(TAG, "Attachment already expired at ${attachment.expires}, not downloading")
|
|
|
|
return false
|
|
|
|
}
|
2022-08-26 15:58:37 +12:00
|
|
|
when (val maxAutoDownloadSize = repository.getAutoDownloadMaxSize()) {
|
2022-01-12 13:37:34 +13:00
|
|
|
Repository.AUTO_DOWNLOAD_ALWAYS -> return true
|
|
|
|
Repository.AUTO_DOWNLOAD_NEVER -> return false
|
|
|
|
else -> {
|
2022-02-11 06:10:27 +13:00
|
|
|
if (attachment.size == null) {
|
2022-01-15 06:32:36 +13:00
|
|
|
return true // DownloadWorker will bail out if attachment is too large!
|
2022-01-12 13:37:34 +13:00
|
|
|
}
|
2022-02-11 06:10:27 +13:00
|
|
|
return attachment.size <= maxAutoDownloadSize
|
2022-01-12 13:37:34 +13:00
|
|
|
}
|
|
|
|
}
|
2021-12-30 09:36:47 +13:00
|
|
|
}
|
2022-07-17 08:32:09 +12:00
|
|
|
private fun shouldDownloadIcon(notification: Notification): Boolean {
|
2022-08-26 15:58:37 +12:00
|
|
|
return notification.icon != null
|
2022-07-17 08:32:09 +12:00
|
|
|
}
|
2021-12-30 09:36:47 +13:00
|
|
|
|
2022-01-02 04:56:18 +13:00
|
|
|
private fun shouldNotify(subscription: Subscription, notification: Notification, muted: Boolean): Boolean {
|
2021-12-31 05:00:27 +13:00
|
|
|
if (subscription.upAppId != null) {
|
2021-12-30 09:36:47 +13:00
|
|
|
return false
|
|
|
|
}
|
2022-01-02 04:56:18 +13:00
|
|
|
val priority = if (notification.priority > 0) notification.priority else 3
|
2022-05-06 08:56:06 +12:00
|
|
|
val minPriority = if (subscription.minPriority > 0) subscription.minPriority else repository.getMinPriority()
|
|
|
|
if (priority < minPriority) {
|
2022-01-02 04:56:18 +13:00
|
|
|
return false
|
|
|
|
}
|
2021-12-30 09:36:47 +13:00
|
|
|
val detailsVisible = repository.detailViewSubscriptionId.get() == notification.subscriptionId
|
|
|
|
return !detailsVisible && !muted
|
|
|
|
}
|
2021-12-30 08:33:17 +13:00
|
|
|
|
2022-01-02 04:56:18 +13: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-30 09:36:47 +13:00
|
|
|
if (repository.isGlobalMuted()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return subscription.mutedUntil == 1L || (subscription.mutedUntil > 1L && subscription.mutedUntil > System.currentTimeMillis()/1000)
|
2021-12-30 08:33:17 +13:00
|
|
|
}
|
2022-01-09 09:49:07 +13:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val TAG = "NtfyNotifDispatch"
|
|
|
|
}
|
2021-12-30 08:33:17 +13:00
|
|
|
}
|