Skip to content

Commit

Permalink
refactor(util): fine tune ImeUtils (InputMethodUtils)
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck authored and Bambooin committed Oct 23, 2021
1 parent f5ca6bb commit da18332
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
47 changes: 47 additions & 0 deletions app/src/main/java/com/osfans/trime/common/InputMethodUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.osfans.trime.common

import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.view.inputmethod.InputMethodManager
import timber.log.Timber

object InputMethodUtils {
private const val IME_ID: String = "com.osfans.trime/.TrimeImeService"

fun checkIsTrimeEnabled(context: Context): Boolean {
val activeImeIds = Settings.Secure.getString(
context.contentResolver,
Settings.Secure.ENABLED_INPUT_METHODS
) ?: "(none)"
Timber.i("List of active IMEs: $activeImeIds")
return activeImeIds.split(":").contains(IME_ID)
}

fun checkisTrimeSelected(context: Context): Boolean {
val selectedImeIds = Settings.Secure.getString(
context.contentResolver,
Settings.Secure.DEFAULT_INPUT_METHOD
) ?: "(none)"
Timber.i("Selected IME: $selectedImeIds")
return selectedImeIds == IME_ID
}

fun showImeEnablerActivity(context: Context) {
val intent = Intent()
intent.action = Settings.ACTION_INPUT_METHOD_SETTINGS
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}

fun showImePicker(context: Context): Boolean {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
return if (imm != null) {
imm.showInputMethodPicker()
true
} else {
false
}
}
}
16 changes: 6 additions & 10 deletions app/src/main/java/com/osfans/trime/settings/PrefMainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -214,26 +214,22 @@ class PrefMainActivity :
class PrefFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.prefs, rootKey)
if (ImeUtils.checkIfImeIsEnabled(requireContext())) {
if (InputMethodUtils.checkIsTrimeEnabled(requireContext())) {
findPreference<Preference>("pref_enable")?.isVisible = false
}
if (ImeUtils.checkIfImeIsSelected(requireContext())) {
if (InputMethodUtils.checkisTrimeSelected(requireContext())) {
findPreference<Preference>("pref_select")?.isVisible = false
}
}

override fun onPreferenceTreeClick(preference: Preference?): Boolean {
return when (preference?.key) {
"pref_enable" -> { // 啓用
val intent = Intent()
intent.action = Settings.ACTION_INPUT_METHOD_SETTINGS
intent.addCategory(Intent.CATEGORY_DEFAULT)
startActivity(intent)
InputMethodUtils.showImeEnablerActivity(requireContext())
true
}
"pref_select" -> { // 切換
(activity as PrefMainActivity).imeManager.showInputMethodPicker()
true
InputMethodUtils.showImePicker(requireContext())
}
"pref_schemas" -> {
SchemaPickerDialog(requireContext()).show()
Expand All @@ -245,10 +241,10 @@ class PrefMainActivity :

override fun onResume() { // 如果同文已被启用/选用,则隐藏设置项
super.onResume()
if (ImeUtils.checkIfImeIsEnabled(requireContext())) {
if (InputMethodUtils.checkIsTrimeEnabled(requireContext())) {
findPreference<Preference>("pref_enable")?.isVisible = false
}
if (ImeUtils.checkIfImeIsSelected(requireContext())) {
if (InputMethodUtils.checkisTrimeSelected(requireContext())) {
findPreference<Preference>("pref_select")?.isVisible = false
}
}
Expand Down
27 changes: 0 additions & 27 deletions app/src/main/java/com/osfans/trime/util/ImeUtils.kt

This file was deleted.

0 comments on commit da18332

Please sign in to comment.