From 98e201b9a95db15dc4eb6683e09475c3e5d0b0d3 Mon Sep 17 00:00:00 2001 From: WhiredPlanck Date: Mon, 26 Feb 2024 01:02:39 +0800 Subject: [PATCH] fix: avoid reading ENABLED_INPUT_METHODS on 34+ fix: crash on Android 14 devices --- .../com/osfans/trime/util/InputMethodUtils.kt | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/osfans/trime/util/InputMethodUtils.kt b/app/src/main/java/com/osfans/trime/util/InputMethodUtils.kt index ac6015641b..4c35acf06b 100644 --- a/app/src/main/java/com/osfans/trime/util/InputMethodUtils.kt +++ b/app/src/main/java/com/osfans/trime/util/InputMethodUtils.kt @@ -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))