Skip to content

Commit

Permalink
fix: deploy in background thread when triggered by broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
goofyz authored and Bambooin committed Jan 1, 2024
1 parent 1a10a79 commit 5738657
Showing 1 changed file with 47 additions and 38 deletions.
85 changes: 47 additions & 38 deletions app/src/main/java/com/osfans/trime/ime/broadcast/IntentReceiver.kt
Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 5738657

Please sign in to comment.