Skip to content

Commit

Permalink
refactor: re-register intent receiver for IMS
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 30, 2025
1 parent 076d1e2 commit 3d0a48f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
8 changes: 7 additions & 1 deletion app/src/main/java/com/osfans/trime/TrimeApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.osfans.trime

import android.app.Application
import android.content.Intent
import android.content.IntentFilter
import android.os.Process
import android.util.Log
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -35,10 +36,15 @@ class TrimeApplication : Application() {
private val rimeIntentReceiver = RimeIntentReceiver()

private fun registerBroadcastReceiver() {
val intentFilter =
IntentFilter().apply {
addAction(RimeIntentReceiver.ACTION_DEPLOY)
addAction(RimeIntentReceiver.ACTION_SYNC_USER_DATA)
}
ContextCompat.registerReceiver(
this,
rimeIntentReceiver,
RimeIntentReceiver.intentFilter,
intentFilter,
PERMISSION_TEST_INPUT_METHOD,
null,
ContextCompat.RECEIVER_EXPORTED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.app.AlarmManager
import android.app.Dialog
import android.app.PendingIntent
import android.content.Intent
import android.content.IntentFilter
import android.content.res.Configuration
import android.graphics.RectF
import android.inputmethodservice.InputMethodService
Expand Down Expand Up @@ -59,6 +60,7 @@ import com.osfans.trime.ime.composition.CandidatesView
import com.osfans.trime.ime.keyboard.InitializationUi
import com.osfans.trime.ime.keyboard.InputFeedbackManager
import com.osfans.trime.ime.keyboard.KeyboardSwitcher
import com.osfans.trime.receiver.RimeIntentReceiver
import com.osfans.trime.util.findSectionFrom
import com.osfans.trime.util.forceShowSelf
import com.osfans.trime.util.isNightMode
Expand Down Expand Up @@ -90,6 +92,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
private var inputView: InputView? = null
private var candidatesView: CandidatesView? = null
private val inputDeviceManager = InputDeviceManager()
private val rimeIntentReceiver = RimeIntentReceiver()
private var initializationUi: InitializationUi? = null
private val locales = Array(2) { Locale.getDefault() }

Expand Down Expand Up @@ -185,6 +188,20 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}
}

private fun registerReceiver() {
val intentFilter =
IntentFilter().apply {
addAction(RimeIntentReceiver.ACTION_DEPLOY)
addAction(RimeIntentReceiver.ACTION_SYNC_USER_DATA)
}
ContextCompat.registerReceiver(
this,
rimeIntentReceiver,
intentFilter,
ContextCompat.RECEIVER_NOT_EXPORTED,
)
}

override fun onCreate() {
rime = RimeDaemon.createSession(javaClass.name)
lifecycleScope.launch {
Expand Down Expand Up @@ -232,7 +249,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
else -> Locale.US
}
inlineSuggestionHandler = InlineSuggestionHandler(this@TrimeInputMethodService)
Timber.d("Trime.onCreate completed")
registerReceiver()
}
} catch (e: Exception) {
Timber.e(e)
Expand Down Expand Up @@ -337,6 +354,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
ThemeManager.removeOnChangedListener(onThemeChangeListener)
ColorManager.removeOnChangedListener(onColorChangeListener)
super.onDestroy()
unregisterReceiver(rimeIntentReceiver)
RimeDaemon.destroySession(javaClass.name)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package com.osfans.trime.receiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.osfans.trime.BuildConfig
import com.osfans.trime.daemon.RimeDaemon
import timber.log.Timber
Expand All @@ -34,13 +33,7 @@ class RimeIntentReceiver : BroadcastReceiver() {
}

companion object {
private const val ACTION_DEPLOY = "${BuildConfig.APPLICATION_ID}.action.DEPLOY"
private const val ACTION_SYNC_USER_DATA = "${BuildConfig.APPLICATION_ID}.action.SYNC_USER_DATA"

val intentFilter =
IntentFilter().apply {
addAction(ACTION_DEPLOY)
addAction(ACTION_SYNC_USER_DATA)
}
const val ACTION_DEPLOY = "${BuildConfig.APPLICATION_ID}.action.DEPLOY"
const val ACTION_SYNC_USER_DATA = "${BuildConfig.APPLICATION_ID}.action.SYNC_USER_DATA"
}
}

0 comments on commit 3d0a48f

Please sign in to comment.