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() 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) } }