Exclude starred Android contacts from Do Not Disturb
This commit is contained in:
@@ -36,6 +36,7 @@ import android.net.LinkProperties
|
|||||||
import android.net.Network
|
import android.net.Network
|
||||||
import android.net.NetworkCapabilities
|
import android.net.NetworkCapabilities
|
||||||
import android.net.NetworkRequest
|
import android.net.NetworkRequest
|
||||||
|
import android.net.Uri
|
||||||
import android.net.wifi.WifiManager
|
import android.net.wifi.WifiManager
|
||||||
import android.os.Build.VERSION
|
import android.os.Build.VERSION
|
||||||
import android.os.CountDownTimer
|
import android.os.CountDownTimer
|
||||||
@@ -885,7 +886,8 @@ class BaresipService: Service() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
val channelId = if (shouldVibrate()) MEDIUM_CHANNEL_ID else HIGH_CHANNEL_ID
|
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()
|
startRinging()
|
||||||
if (!Utils.isVisible()) {
|
if (!Utils.isVisible()) {
|
||||||
val piFlags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
|
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 currentFilter = nm.currentInterruptionFilter
|
||||||
val dndAllowsRinging = currentFilter <= NotificationManager.INTERRUPTION_FILTER_ALL
|
if (currentFilter <= NotificationManager.INTERRUPTION_FILTER_ALL)
|
||||||
if (dndAllowsRinging)
|
|
||||||
return true
|
return true
|
||||||
val channel = nm.getNotificationChannel(channelId)
|
val channel = nm.getNotificationChannel(channelId)
|
||||||
return channel != null && channel.canBypassDnd()
|
if (channel != null && channel.canBypassDnd())
|
||||||
|
return true
|
||||||
|
return isStarredContact(callerNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun shouldVibrate(): Boolean {
|
private fun shouldVibrate(): Boolean {
|
||||||
@@ -1422,6 +1425,31 @@ class BaresipService: Service() {
|
|||||||
false
|
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() {
|
private fun stopRinging() {
|
||||||
rt!!.stop()
|
rt!!.stop()
|
||||||
if (vbTimer != null) {
|
if (vbTimer != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user