summaryrefslogtreecommitdiff
path: root/app/src/main/java/sh/lajo/buddy/BuddyNotificationService.kt
blob: 51076b93450b528c7297c06784ff3b21bebff8aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package sh.lajo.buddy

import android.content.Intent
import android.provider.ContactsContract
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.util.Log

class BuddyNotificationService : NotificationListenerService() {
    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        sbn ?: return

        val config = ConfigManager.getConfig(this)
        if (config.disableBuddy) {
            Log.d("BuddyNotificationService", "Buddy is disabled, skipping notification")
            return
        }


        val notification = sbn.notification
        val title = notification.extras.getCharSequence(android.app.Notification.EXTRA_TITLE)?.toString() ?: ""
        val text = notification.extras.getCharSequence(android.app.Notification.EXTRA_TEXT)?.toString() ?: ""
        val packageName = sbn.packageName

        val allowedPackages = arrayOf(
            "com.whatsapp",
            "com.discord",
            "org.thoughtcrime.securesms", // Signal
            "network.loki.messenger", // Session
            "chat.simplex.app", // SimpleX
        )

        if (!allowedPackages.contains(packageName)) {
            return
        }

        val contacts = mutableListOf<String>()
        val cursor = contentResolver.query(
            ContactsContract.Contacts.CONTENT_URI,
            arrayOf(ContactsContract.Contacts.DISPLAY_NAME),
            null, null, null
        )
        cursor?.use {
            while (it.moveToNext()) {
                val name = it.getString(it.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))
                contacts.add(name)
            }
        }

        for (contactName in contacts) {
            if (title.contains(contactName, ignoreCase = true)) {
                return
            }
        }

        val intent = Intent(this, WebSocketService::class.java).apply {
            action = WebSocketService.ACTION_SEND_NOTIFICATION
            putExtra(WebSocketService.EXTRA_NOTIFICATION_TITLE, title)
            putExtra(WebSocketService.EXTRA_NOTIFICATION_TEXT, text)
            putExtra(WebSocketService.EXTRA_NOTIFICATION_PACKAGE, packageName)
        }

        startForegroundService(intent)
    }
}