Skip to content

Commit

Permalink
refactor: still handle rime deploy message in RimeDaemon
Browse files Browse the repository at this point in the history
Then users can still receive notification or toast on deploy in the app even they don't set trime as default ime.
  • Loading branch information
WhiredPlanck committed Jan 12, 2025
1 parent 8ef1643 commit d0e54c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
31 changes: 19 additions & 12 deletions app/src/main/java/com/osfans/trime/daemon/RimeDaemon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package com.osfans.trime.daemon
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
Expand All @@ -26,6 +25,7 @@ import com.osfans.trime.util.logcat
import com.osfans.trime.util.toast
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -56,6 +56,8 @@ object RimeDaemon {

private val lock = ReentrantLock()

private var messageJob: Job? = null

private fun establish(name: String) =
object : RimeSession {
private inline fun <T> ensureEstablished(block: () -> T) =
Expand Down Expand Up @@ -150,19 +152,24 @@ object RimeDaemon {
.build()
.let { notificationManager.notify(id, it) }
realRime.finalize()
messageJob =
TrimeApplication.getInstance().coroutineScope.launch {
realRime.messageFlow.collect {
handleRimeMessage(it)
}
}
realRime.startup(fullCheck)
TrimeApplication.getInstance().coroutineScope.launch {
// cancel notification on ready
realRime.lifecycle.whenReady {
notificationManager.cancel(id)
messageJob?.cancel()
messageJob = null
}
}
}

suspend fun onRimeMessage(
ctx: Context,
it: RimeMessage<*>,
) {
private suspend fun handleRimeMessage(it: RimeMessage<*>) {
if (it is RimeMessage.DeployMessage) {
when (it.data) {
RimeMessage.DeployMessage.State.Start -> {
Expand All @@ -171,13 +178,13 @@ object RimeDaemon {
}
}
RimeMessage.DeployMessage.State.Success -> {
ContextCompat.getMainExecutor(ctx).execute {
ctx.toast(R.string.deploy_finish)
ContextCompat.getMainExecutor(appContext).execute {
appContext.toast(R.string.deploy_finish)
}
}
RimeMessage.DeployMessage.State.Failure -> {
val intent =
Intent(ctx, LogActivity::class.java).apply {
Intent(appContext, LogActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
val log =
withContext(Dispatchers.IO) {
Expand All @@ -191,13 +198,13 @@ object RimeDaemon {
putExtra(LogActivity.DEPLOY_FAILURE_TRACE, log)
}
NotificationCompat
.Builder(ctx, CHANNEL_ID)
.Builder(appContext, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_baseline_warning_24)
.setContentTitle(ctx.getString(R.string.rime_daemon))
.setContentText(ctx.getString(R.string.view_deploy_failure_log))
.setContentTitle(appContext.getString(R.string.rime_daemon))
.setContentText(appContext.getString(R.string.view_deploy_failure_log))
.setContentIntent(
PendingIntent.getActivity(
ctx,
appContext,
0,
intent,
PendingIntent.FLAG_ONE_SHOT or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}
}
}
else ->
lifecycleScope.launch {
RimeDaemon.onRimeMessage(this@TrimeInputMethodService, it)
}
else -> {}
}
}

Expand Down

0 comments on commit d0e54c9

Please sign in to comment.