Skip to content

Commit

Permalink
fix: avoid reading ENABLED_INPUT_METHODS on 34+
Browse files Browse the repository at this point in the history
fix: crash on Android 14 devices
  • Loading branch information
WhiredPlanck committed Feb 26, 2024
1 parent 97e83c1 commit 98e201b
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions app/src/main/java/com/osfans/trime/util/InputMethodUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,45 @@ package com.osfans.trime.util
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import com.osfans.trime.BuildConfig
import com.osfans.trime.ime.core.TrimeInputMethodService
import splitties.systemservices.inputMethodManager
import timber.log.Timber

object InputMethodUtils {
private val serviceName =
private val serviceName = TrimeInputMethodService::class.java.name
private val componentName =
ComponentName(appContext, TrimeInputMethodService::class.java).flattenToShortString()

private fun getSecureSettings(name: String) = Settings.Secure.getString(appContext.contentResolver, name)

fun checkIsTrimeEnabled(): Boolean {
val activeImeIds = getSecureSettings(Settings.Secure.ENABLED_INPUT_METHODS) ?: "(none)"
Timber.i("List of active IMEs: $activeImeIds")
return activeImeIds.split(":").contains(serviceName)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
inputMethodManager.enabledInputMethodList.also {
Timber.i("List of active IMEs: $it")
}.any {
it.packageName == BuildConfig.APPLICATION_ID && it.serviceName == serviceName
}
} else {
val activeImeIds = getSecureSettings(Settings.Secure.ENABLED_INPUT_METHODS) ?: "(none)"
Timber.i("List of active IMEs: $activeImeIds")
activeImeIds.split(":").contains(componentName)
}
}

fun checkIsTrimeSelected(): Boolean {
val selectedImeIds = getSecureSettings(Settings.Secure.DEFAULT_INPUT_METHOD) ?: "(none)"
Timber.i("Selected IME: $selectedImeIds")
return selectedImeIds == serviceName
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
inputMethodManager.currentInputMethodInfo?.let {
Timber.i("Selected IME: ${it.serviceName}")
it.packageName == BuildConfig.APPLICATION_ID && it.serviceName == serviceName
} ?: false
} else {
val selectedImeIds = getSecureSettings(Settings.Secure.DEFAULT_INPUT_METHOD) ?: "(none)"
Timber.i("Selected IME: $selectedImeIds")
selectedImeIds == componentName
}
}

fun showImeEnablerActivity(context: Context) = context.startActivity(Intent(Settings.ACTION_INPUT_METHOD_SETTINGS))
Expand Down

0 comments on commit 98e201b

Please sign in to comment.