@@ -7,7 +7,6 @@ package com.osfans.trime.daemon
7
7
import android.app.NotificationChannel
8
8
import android.app.NotificationManager
9
9
import android.app.PendingIntent
10
- import android.content.Context
11
10
import android.content.Intent
12
11
import android.os.Build
13
12
import androidx.core.app.NotificationCompat
@@ -26,6 +25,7 @@ import com.osfans.trime.util.logcat
26
25
import com.osfans.trime.util.toast
27
26
import kotlinx.coroutines.CoroutineScope
28
27
import kotlinx.coroutines.Dispatchers
28
+ import kotlinx.coroutines.Job
29
29
import kotlinx.coroutines.launch
30
30
import kotlinx.coroutines.runBlocking
31
31
import kotlinx.coroutines.withContext
@@ -56,6 +56,8 @@ object RimeDaemon {
56
56
57
57
private val lock = ReentrantLock ()
58
58
59
+ private var messageJob: Job ? = null
60
+
59
61
private fun establish (name : String ) =
60
62
object : RimeSession {
61
63
private inline fun <T > ensureEstablished (block : () -> T ) =
@@ -150,19 +152,24 @@ object RimeDaemon {
150
152
.build()
151
153
.let { notificationManager.notify(id, it) }
152
154
realRime.finalize()
155
+ messageJob =
156
+ TrimeApplication .getInstance().coroutineScope.launch {
157
+ realRime.messageFlow.collect {
158
+ handleRimeMessage(it)
159
+ }
160
+ }
153
161
realRime.startup(fullCheck)
154
162
TrimeApplication .getInstance().coroutineScope.launch {
155
163
// cancel notification on ready
156
164
realRime.lifecycle.whenReady {
157
165
notificationManager.cancel(id)
166
+ messageJob?.cancel()
167
+ messageJob = null
158
168
}
159
169
}
160
170
}
161
171
162
- suspend fun onRimeMessage (
163
- ctx : Context ,
164
- it : RimeMessage <* >,
165
- ) {
172
+ private suspend fun handleRimeMessage (it : RimeMessage <* >) {
166
173
if (it is RimeMessage .DeployMessage ) {
167
174
when (it.data) {
168
175
RimeMessage .DeployMessage .State .Start -> {
@@ -171,13 +178,13 @@ object RimeDaemon {
171
178
}
172
179
}
173
180
RimeMessage .DeployMessage .State .Success -> {
174
- ContextCompat .getMainExecutor(ctx ).execute {
175
- ctx .toast(R .string.deploy_finish)
181
+ ContextCompat .getMainExecutor(appContext ).execute {
182
+ appContext .toast(R .string.deploy_finish)
176
183
}
177
184
}
178
185
RimeMessage .DeployMessage .State .Failure -> {
179
186
val intent =
180
- Intent (ctx , LogActivity ::class .java).apply {
187
+ Intent (appContext , LogActivity ::class .java).apply {
181
188
flags = Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TASK
182
189
val log =
183
190
withContext(Dispatchers .IO ) {
@@ -191,13 +198,13 @@ object RimeDaemon {
191
198
putExtra(LogActivity .DEPLOY_FAILURE_TRACE , log)
192
199
}
193
200
NotificationCompat
194
- .Builder (ctx , CHANNEL_ID )
201
+ .Builder (appContext , CHANNEL_ID )
195
202
.setSmallIcon(R .drawable.ic_baseline_warning_24)
196
- .setContentTitle(ctx .getString(R .string.rime_daemon))
197
- .setContentText(ctx .getString(R .string.view_deploy_failure_log))
203
+ .setContentTitle(appContext .getString(R .string.rime_daemon))
204
+ .setContentText(appContext .getString(R .string.view_deploy_failure_log))
198
205
.setContentIntent(
199
206
PendingIntent .getActivity(
200
- ctx ,
207
+ appContext ,
201
208
0 ,
202
209
intent,
203
210
PendingIntent .FLAG_ONE_SHOT or
0 commit comments