@@ -8,7 +8,6 @@ import android.app.PendingIntent
8
8
import android.content.Intent
9
9
import androidx.core.app.NotificationCompat
10
10
import androidx.core.app.NotificationManagerCompat
11
- import androidx.core.content.ContextCompat
12
11
import com.osfans.trime.R
13
12
import com.osfans.trime.TrimeApplication
14
13
import com.osfans.trime.core.Rime
@@ -22,10 +21,8 @@ import com.osfans.trime.util.appContext
22
21
import com.osfans.trime.util.createNotificationChannel
23
22
import com.osfans.trime.util.readText
24
23
import com.osfans.trime.util.subprocess
25
- import com.osfans.trime.util.toast
26
24
import kotlinx.coroutines.CoroutineScope
27
25
import kotlinx.coroutines.Dispatchers
28
- import kotlinx.coroutines.Job
29
26
import kotlinx.coroutines.launch
30
27
import kotlinx.coroutines.runBlocking
31
28
import kotlinx.coroutines.withContext
@@ -56,8 +53,6 @@ object RimeDaemon {
56
53
57
54
private val lock = ReentrantLock ()
58
55
59
- private var messageJob: Job ? = null
60
-
61
56
private fun establish (name : String ) =
62
57
object : RimeSession {
63
58
private inline fun <T > ensureEstablished (block : () -> T ) =
@@ -99,12 +94,6 @@ object RimeDaemon {
99
94
if (realRime.lifecycle.currentStateFlow.value == RimeLifecycle .State .STOPPED ) {
100
95
realRime.startup(false )
101
96
}
102
- messageJob =
103
- TrimeApplication .getInstance().coroutineScope.launch {
104
- realRime.messageFlow.collect {
105
- handleRimeMessage(it)
106
- }
107
- }
108
97
val session = establish(name)
109
98
sessions[name] = session
110
99
return @withLock session
@@ -118,8 +107,6 @@ object RimeDaemon {
118
107
sessions - = name
119
108
if (sessions.isEmpty()) {
120
109
realRime.finalize()
121
- messageJob?.cancel()
122
- messageJob = null
123
110
}
124
111
}
125
112
@@ -138,6 +125,23 @@ object RimeDaemon {
138
125
appContext.getString(R .string.rime_daemon),
139
126
NotificationManagerCompat .IMPORTANCE_HIGH ,
140
127
)
128
+ TrimeApplication .getInstance().coroutineScope.launch {
129
+ realRime.messageFlow.collect {
130
+ handleRimeMessage(it)
131
+ }
132
+ }
133
+ }
134
+
135
+ private inline fun sendNotification (
136
+ id : Int ,
137
+ buildAction : NotificationCompat .Builder .() -> Unit ,
138
+ ) {
139
+ val builder =
140
+ NotificationCompat
141
+ .Builder (appContext, CHANNEL_ID )
142
+ .setContentTitle(appContext.getString(R .string.rime_daemon))
143
+ builder.buildAction()
144
+ builder.build().let { notificationManager.notify(id, it) }
141
145
}
142
146
143
147
/* *
@@ -146,24 +150,22 @@ object RimeDaemon {
146
150
fun restartRime (fullCheck : Boolean = false) =
147
151
lock.withLock {
148
152
val id = restartId++
149
- NotificationCompat
150
- . Builder (appContext, CHANNEL_ID )
151
- . setSmallIcon(R .drawable.ic_baseline_sync_24)
152
- . setContentTitle(appContext.getString(R .string.rime_daemon))
153
- . setContentText(appContext.getString(R .string.restarting_rime))
154
- . setOngoing(true )
155
- . setProgress(100 , 0 , true )
156
- . setPriority(NotificationCompat .PRIORITY_HIGH )
157
- .build()
158
- . let { notificationManager.notify(id, it) }
153
+ if ( ! fullCheck) {
154
+ sendNotification(id) {
155
+ setSmallIcon(R .drawable.ic_baseline_sync_24)
156
+ setContentTitle(appContext.getString(R .string.rime_daemon))
157
+ setContentText(appContext.getString(R .string.restarting_rime))
158
+ setOngoing(true )
159
+ setProgress(100 , 0 , true )
160
+ setPriority(NotificationCompat .PRIORITY_HIGH )
161
+ }
162
+ }
159
163
realRime.finalize()
160
164
realRime.startup(fullCheck)
161
165
TrimeApplication .getInstance().coroutineScope.launch {
162
166
// cancel notification on ready
163
167
realRime.lifecycle.whenReady {
164
168
notificationManager.cancel(id)
165
- messageJob?.cancel()
166
- messageJob = null
167
169
}
168
170
}
169
171
}
@@ -172,11 +174,20 @@ object RimeDaemon {
172
174
if (it is RimeMessage .DeployMessage ) {
173
175
when (it.data) {
174
176
RimeMessage .DeployMessage .State .Start -> {
177
+ sendNotification(MESSAGE_ID ) {
178
+ setSmallIcon(R .drawable.ic_baseline_sync_24)
179
+ setContentText(appContext.getString(R .string.deploy_progress))
180
+ setPriority(NotificationCompat .PRIORITY_DEFAULT )
181
+ setTimeoutAfter(2000L )
182
+ }
175
183
withContext(Dispatchers .IO ) { subprocess(" logcat" , " --clear" ) }
176
184
}
177
185
RimeMessage .DeployMessage .State .Success -> {
178
- ContextCompat .getMainExecutor(appContext).execute {
179
- appContext.toast(R .string.deploy_finish)
186
+ sendNotification(MESSAGE_ID ) {
187
+ setSmallIcon(R .drawable.ic_baseline_sync_24)
188
+ setContentText(appContext.getString(R .string.deploy_finish))
189
+ setPriority(NotificationCompat .PRIORITY_DEFAULT )
190
+ setTimeoutAfter(2000L )
180
191
}
181
192
}
182
193
RimeMessage .DeployMessage .State .Failure -> {
@@ -189,24 +200,22 @@ object RimeDaemon {
189
200
putExtra(LogActivity .FROM_DEPLOY , true )
190
201
putExtra(LogActivity .DEPLOY_FAILURE_TRACE , log)
191
202
}
192
- NotificationCompat
193
- .Builder (appContext, CHANNEL_ID )
194
- .setSmallIcon(R .drawable.ic_baseline_warning_24)
195
- .setContentTitle(appContext.getString(R .string.rime_daemon))
196
- .setContentText(appContext.getString(R .string.view_deploy_failure_log))
197
- .setContentIntent(
203
+ sendNotification(MESSAGE_ID ) {
204
+ setSmallIcon(R .drawable.ic_baseline_warning_24)
205
+ setContentText(appContext.getString(R .string.view_deploy_failure_log))
206
+ setContentIntent(
198
207
PendingIntent .getActivity(
199
208
appContext,
200
209
0 ,
201
210
intent,
202
211
PendingIntent .FLAG_ONE_SHOT or
203
212
PendingIntent .FLAG_IMMUTABLE ,
204
213
),
205
- ).setOngoing( false )
206
- .setAutoCancel( true )
207
- .setPriority( NotificationCompat . PRIORITY_DEFAULT )
208
- .build( )
209
- . let { notificationManager.notify( MESSAGE_ID , it) }
214
+ )
215
+ setOngoing( false )
216
+ setAutoCancel( true )
217
+ setPriority( NotificationCompat . PRIORITY_HIGH )
218
+ }
210
219
}
211
220
}
212
221
}
0 commit comments