Skip to content

Commit c5c4ef0

Browse files
committed
refactor(daemon): reduce the frequency of annoying notifications
1 parent 071ce38 commit c5c4ef0

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

app/src/main/java/com/osfans/trime/daemon/RimeDaemon.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,18 @@ object RimeDaemon {
113113
private const val MESSAGE_ID = 2331
114114
private var restartId = 0
115115

116+
private const val NOTIFICATION_TIMEOUT = 60000L // 60secs
117+
private const val SILENCE_DURATION = 30L // 30ms
118+
119+
private var silenceNotificationUntil = 0L
120+
private var allowNotificationUntil = 0L
121+
116122
init {
117123
createNotificationChannel(
118124
CHANNEL_ID,
119125
appContext.getString(R.string.rime_daemon),
120126
)
127+
allowNotification()
121128
TrimeApplication.getInstance().coroutineScope.launch {
122129
realRime.messageFlow.collect {
123130
handleRimeMessage(it)
@@ -137,6 +144,10 @@ object RimeDaemon {
137144
builder.build().let { notificationManager.notify(id, it) }
138145
}
139146

147+
private fun allowNotification() {
148+
allowNotificationUntil = System.currentTimeMillis() + NOTIFICATION_TIMEOUT
149+
}
150+
140151
/**
141152
* Restart Rime instance to deploy while keep the session
142153
*/
@@ -153,6 +164,7 @@ object RimeDaemon {
153164
}
154165
}
155166
realRime.finalize()
167+
allowNotification()
156168
realRime.startup(fullCheck)
157169
TrimeApplication.getInstance().coroutineScope.launch {
158170
// cancel notification on ready
@@ -164,9 +176,11 @@ object RimeDaemon {
164176

165177
private suspend fun handleRimeMessage(it: RimeMessage<*>) {
166178
if (it is RimeMessage.DeployMessage) {
179+
val buildNotification: NotificationCompat.Builder.() -> Unit
180+
var blockMessage = false
167181
when (it.data) {
168182
RimeMessage.DeployMessage.State.Start -> {
169-
sendNotification(MESSAGE_ID) {
183+
buildNotification = {
170184
setSmallIcon(R.drawable.ic_baseline_refresh_reversed_24)
171185
setContentText(appContext.getString(R.string.deploy_progress))
172186
setProgress(0, 0, true)
@@ -177,7 +191,7 @@ object RimeDaemon {
177191
withContext(Dispatchers.IO) { subprocess("logcat", "--clear") }
178192
}
179193
RimeMessage.DeployMessage.State.Success -> {
180-
sendNotification(MESSAGE_ID) {
194+
buildNotification = {
181195
setSmallIcon(R.drawable.ic_baseline_refresh_reversed_24)
182196
setColor(Color.GREEN)
183197
setContentText(appContext.getString(R.string.deploy_finish))
@@ -186,6 +200,7 @@ object RimeDaemon {
186200
setAutoCancel(true)
187201
setPriority(NotificationCompat.PRIORITY_DEFAULT)
188202
}
203+
blockMessage = true
189204
}
190205
RimeMessage.DeployMessage.State.Failure -> {
191206
val intent =
@@ -197,7 +212,7 @@ object RimeDaemon {
197212
putExtra(LogActivity.FROM_DEPLOY, true)
198213
putExtra(LogActivity.DEPLOY_FAILURE_TRACE, log)
199214
}
200-
sendNotification(MESSAGE_ID) {
215+
buildNotification = {
201216
setSmallIcon(R.drawable.ic_baseline_warning_24)
202217
setColor(Color.YELLOW)
203218
setContentText(appContext.getString(R.string.view_deploy_failure_log))
@@ -214,8 +229,17 @@ object RimeDaemon {
214229
setAutoCancel(true)
215230
setPriority(NotificationCompat.PRIORITY_HIGH)
216231
}
232+
blockMessage = true
217233
}
218234
}
235+
val current = System.currentTimeMillis()
236+
if (current > silenceNotificationUntil && current < allowNotificationUntil) {
237+
sendNotification(MESSAGE_ID, buildNotification)
238+
}
239+
240+
if (blockMessage) {
241+
silenceNotificationUntil = current + SILENCE_DURATION
242+
}
219243
}
220244
}
221245
}

0 commit comments

Comments
 (0)