From 5738657bfc2f4dd114de48882695ff90bcfc9324 Mon Sep 17 00:00:00 2001 From: Goofyz Leung Date: Thu, 21 Dec 2023 10:27:20 +0800 Subject: [PATCH] fix: deploy in background thread when triggered by broadcast --- .../trime/ime/broadcast/IntentReceiver.kt | 85 ++++++++++--------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/osfans/trime/ime/broadcast/IntentReceiver.kt b/app/src/main/java/com/osfans/trime/ime/broadcast/IntentReceiver.kt index 8c7c9a5a6c..bc52cd7e2a 100644 --- a/app/src/main/java/com/osfans/trime/ime/broadcast/IntentReceiver.kt +++ b/app/src/main/java/com/osfans/trime/ime/broadcast/IntentReceiver.kt @@ -36,7 +36,6 @@ import com.osfans.trime.ime.core.RimeWrapper import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.MainScope -import kotlinx.coroutines.async import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import timber.log.Timber @@ -54,51 +53,61 @@ class IntentReceiver : BroadcastReceiver(), CoroutineScope by MainScope() { when (command) { COMMAND_DEPLOY -> launch { - withContext(Dispatchers.Default) { + withContext(Dispatchers.IO) { RimeWrapper.deploy() } ToastUtils.showLong(R.string.deploy_finish) } COMMAND_SYNC -> - async { - Rime.syncRimeUserData() - RimeWrapper.deploy() + launch { + withContext(Dispatchers.IO) { + Rime.syncRimeUserData() + RimeWrapper.deploy() + } } COMMAND_TIMING_SYNC -> - async { - // 获取唤醒锁 - val powerManager = context.getSystemService(POWER_SERVICE) as PowerManager - val wakeLock = powerManager.newWakeLock(PARTIAL_WAKE_LOCK, "com.osfans.trime:WakeLock") - wakeLock.acquire(600000) // 10分钟超时 - val cal = Calendar.getInstance() - val triggerTime = cal.timeInMillis + TimeUnit.DAYS.toMillis(1) // 下次同步时间 - AppPrefs.defaultInstance().profile.timingSyncTriggerTime = triggerTime // 更新定时同步偏好值 - val alarmManager = - context.getSystemService(Context.ALARM_SERVICE) as AlarmManager - // 设置待发送的同步事件 - val pendingIntent = - PendingIntent.getBroadcast( - context, - 0, - Intent("com.osfans.trime.timing.sync"), - if (VERSION.SDK_INT >= VERSION_CODES.M) { - PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE - } else { - PendingIntent.FLAG_UPDATE_CURRENT - }, - ) - if (VERSION.SDK_INT >= VERSION_CODES.M) { // 根据SDK设置alarm任务 - alarmManager.setExactAndAllowWhileIdle( - AlarmManager.RTC_WAKEUP, - triggerTime, - pendingIntent, - ) - } else { - alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent) + launch { + withContext(Dispatchers.IO) { + // 获取唤醒锁 + val powerManager = context.getSystemService(POWER_SERVICE) as PowerManager + val wakeLock = + powerManager.newWakeLock(PARTIAL_WAKE_LOCK, "com.osfans.trime:WakeLock") + wakeLock.acquire(600000) // 10分钟超时 + val cal = Calendar.getInstance() + val triggerTime = cal.timeInMillis + TimeUnit.DAYS.toMillis(1) // 下次同步时间 + AppPrefs.defaultInstance().profile.timingSyncTriggerTime = + triggerTime // 更新定时同步偏好值 + val alarmManager = + context.getSystemService(Context.ALARM_SERVICE) as AlarmManager + // 设置待发送的同步事件 + val pendingIntent = + PendingIntent.getBroadcast( + context, + 0, + Intent("com.osfans.trime.timing.sync"), + if (VERSION.SDK_INT >= VERSION_CODES.M) { + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE + } else { + PendingIntent.FLAG_UPDATE_CURRENT + }, + ) + if (VERSION.SDK_INT >= VERSION_CODES.M) { // 根据SDK设置alarm任务 + alarmManager.setExactAndAllowWhileIdle( + AlarmManager.RTC_WAKEUP, + triggerTime, + pendingIntent, + ) + } else { + alarmManager.setExact( + AlarmManager.RTC_WAKEUP, + triggerTime, + pendingIntent, + ) + } + Rime.syncRimeUserData() + RimeWrapper.deploy() + wakeLock.release() // 释放唤醒锁 } - Rime.syncRimeUserData() - RimeWrapper.deploy() - wakeLock.release() // 释放唤醒锁 } else -> return }