diff --git a/app/src/main/java/com/pilot51/voicenotify/Constants.kt b/app/src/main/java/com/pilot51/voicenotify/Constants.kt new file mode 100644 index 0000000..7432e4d --- /dev/null +++ b/app/src/main/java/com/pilot51/voicenotify/Constants.kt @@ -0,0 +1,5 @@ +package com.pilot51.voicenotify + +object Constants { + const val REGEX_PREFIX = "\$regex:" +} diff --git a/app/src/main/java/com/pilot51/voicenotify/NotificationInfo.kt b/app/src/main/java/com/pilot51/voicenotify/NotificationInfo.kt index 9ccc5aa..a7dfe81 100644 --- a/app/src/main/java/com/pilot51/voicenotify/NotificationInfo.kt +++ b/app/src/main/java/com/pilot51/voicenotify/NotificationInfo.kt @@ -132,8 +132,13 @@ data class NotificationInfo( val ttsTextReplace = if (isComposePreview) null else settings.ttsTextReplace val textReplaceList = PreferencesViewModel.convertTextReplaceStringToList(ttsTextReplace) for (pair in textReplaceList) { - ttsMessage = ttsMessage!!.replace( - "(?i)${Pattern.quote(pair.first)}".toRegex(), pair.second) + val pattern = + if (pair.first.startsWith(Constants.REGEX_PREFIX)) + Regex(pair.first.removePrefix(Constants.REGEX_PREFIX)) + else + "(?i)${Pattern.quote(pair.first)}".toRegex() + + ttsMessage = ttsMessage!!.replace(pattern, pair.second) } val maxLength = if (isComposePreview) 0 else { settings.ttsMaxLength ?: DEFAULT_MAX_LENGTH diff --git a/app/src/main/java/com/pilot51/voicenotify/Service.kt b/app/src/main/java/com/pilot51/voicenotify/Service.kt index 4c881c3..2c6f795 100644 --- a/app/src/main/java/com/pilot51/voicenotify/Service.kt +++ b/app/src/main/java/com/pilot51/voicenotify/Service.kt @@ -70,6 +70,7 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.launch import java.util.* +import java.util.regex.PatternSyntaxException class Service : NotificationListenerService() { private val appContext by ::applicationContext @@ -222,16 +223,29 @@ class Service : NotificationListenerService() { info.ignoreReasons.add(IgnoreReason.EMPTY_MSG) } if (ttsMsg != null) { + fun containsOrMatchesRegex(it: String): Boolean { + return if (it.startsWith(Constants.REGEX_PREFIX)) + try { + val pattern = it.removePrefix(Constants.REGEX_PREFIX) + Regex(pattern, RegexOption.IGNORE_CASE) + .containsMatchIn(ttsMsg) + } catch (e: PatternSyntaxException) { + false + } + else + ttsMsg.contains(it, true) + } + val requireStrings = settings.requireStrings?.split("\n") val stringRequired = requireStrings?.all { - it.isNotEmpty() && !ttsMsg.contains(it, true) + it.isNotEmpty() && !containsOrMatchesRegex(it) } ?: false if (stringRequired) { info.ignoreReasons.add(IgnoreReason.STRING_REQUIRED) } val ignoreStrings = settings.ignoreStrings?.split("\n") val stringIgnored = ignoreStrings?.any { - it.isNotEmpty() && ttsMsg.contains(it, true) + it.isNotEmpty() && containsOrMatchesRegex(it) } ?: false if (stringIgnored) { info.ignoreReasons.add(IgnoreReason.STRING_IGNORED)