Fix anchored notification visibility exceptions
This commit is contained in:
@@ -93,19 +93,23 @@ class CompiledVisibilityPolicy(policy: VisibilityPolicy) {
|
||||
fun isBlocked(notification: VisibilityNotification, surface: NotificationSurface): Boolean {
|
||||
if (!enabled) return false
|
||||
val policy = apps[notification.packageName] ?: return false
|
||||
val matchText = notification.matchText()
|
||||
val exception = policy.exceptions.firstOrNull { it.pattern.matcher(matchText).find() }
|
||||
val matchTexts = notification.matchTexts()
|
||||
val exception = policy.exceptions.firstOrNull { rule ->
|
||||
matchTexts.any { rule.pattern.matcher(it).find() }
|
||||
}
|
||||
return surface in (exception?.blockedSurfaces ?: policy.blockedSurfaces)
|
||||
}
|
||||
|
||||
fun hasExceptions(packageName: String) = apps[packageName]?.exceptions?.isNotEmpty() == true
|
||||
|
||||
private fun VisibilityNotification.matchText() = buildList {
|
||||
private fun VisibilityNotification.matchTexts() = buildList {
|
||||
add(packageName)
|
||||
add(key)
|
||||
listOf(title, text, bigText, subtext, channelId).filterNotNullTo(this)
|
||||
addAll(messages)
|
||||
}.joinToString("\n")
|
||||
}.map(String::trim).filter(String::isNotEmpty).let { fields ->
|
||||
fields + fields.joinToString("\n")
|
||||
}
|
||||
|
||||
private data class CompiledAppPolicy(
|
||||
val blockedSurfaces: Set<NotificationSurface>,
|
||||
|
||||
@@ -61,6 +61,14 @@ class VisibilityPolicyTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun `anchors apply to each notification field`() {
|
||||
val policy = compiled(exceptions = listOf(
|
||||
VisibilityExceptionRule("^Lunch tomorrow\\?$", setOf(NotificationSurface.AOD)),
|
||||
))
|
||||
|
||||
assertTrue(policy.isBlocked(notification, NotificationSurface.AOD))
|
||||
}
|
||||
|
||||
@Test fun `invalid regular expressions can be rejected by an editor`() {
|
||||
assertFalse(VisibilityExceptionRule("(", emptySet()).hasValidPattern())
|
||||
assertTrue(VisibilityExceptionRule("valid.*", emptySet()).hasValidPattern())
|
||||
|
||||
Reference in New Issue
Block a user