From 6762c4e47998fa612870d873f82cd7afa321d5f1 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Wed, 8 Oct 2025 09:48:58 +0300 Subject: [PATCH] Exclude starred Android contacts from Do Not Disturb --- .../com/tutpro/baresip/BaresipService.kt | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 28e2be74..30606035 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -36,6 +36,7 @@ import android.net.LinkProperties import android.net.Network import android.net.NetworkCapabilities import android.net.NetworkRequest +import android.net.Uri import android.net.wifi.WifiManager import android.os.Build.VERSION import android.os.CountDownTimer @@ -885,7 +886,8 @@ class BaresipService: Service() { return } val channelId = if (shouldVibrate()) MEDIUM_CHANNEL_ID else HIGH_CHANNEL_ID - if (shouldStartRinging(channelId)) + val callerNumber = peerUri.split(":")[1].split("@")[0] + if (shouldStartRinging(channelId, callerNumber)) startRinging() if (!Utils.isVisible()) { val piFlags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT @@ -1399,13 +1401,14 @@ class BaresipService: Service() { } - private fun shouldStartRinging(channelId: String): Boolean { + private fun shouldStartRinging(channelId: String, callerNumber: String): Boolean { val currentFilter = nm.currentInterruptionFilter - val dndAllowsRinging = currentFilter <= NotificationManager.INTERRUPTION_FILTER_ALL - if (dndAllowsRinging) + if (currentFilter <= NotificationManager.INTERRUPTION_FILTER_ALL) return true val channel = nm.getNotificationChannel(channelId) - return channel != null && channel.canBypassDnd() + if (channel != null && channel.canBypassDnd()) + return true + return isStarredContact(callerNumber) } private fun shouldVibrate(): Boolean { @@ -1422,6 +1425,31 @@ class BaresipService: Service() { false } + private fun isStarredContact(callerNumber: String): Boolean { + if (contactsMode == "baresip" || callerNumber.isBlank()) + return false + val phoneUri = Uri.withAppendedPath( + ContactsContract.PhoneLookup.CONTENT_FILTER_URI, + Uri.encode(callerNumber) + ) + val projection = arrayOf(ContactsContract.PhoneLookup.STARRED) + try { + val cursor = contentResolver.query(phoneUri, projection, null, null, null) + cursor?.use { + if (it.moveToFirst()) { + val isStarred = it.getInt(0) == 1 + if (isStarred) { + Log.d(TAG, "Caller '$callerNumber' is a starred contact.") + } + return isStarred + } + } + } catch (e: Exception) { + Log.e(TAG, "Could not query contacts for starred status: ${e.message}") + } + return false + } + private fun stopRinging() { rt!!.stop() if (vbTimer != null) {