Update keyboard on preference changes

This commit is contained in:
Naveen 2023-01-31 23:25:32 +05:30
parent 8c5e422b3b
commit 6a6b70d414
2 changed files with 124 additions and 93 deletions

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.keyboard.services package com.simplemobiletools.keyboard.services
import android.content.SharedPreferences
import android.inputmethodservice.InputMethodService import android.inputmethodservice.InputMethodService
import android.text.InputType import android.text.InputType
import android.text.InputType.TYPE_CLASS_DATETIME import android.text.InputType.TYPE_CLASS_DATETIME
@ -14,6 +15,7 @@ import android.view.inputmethod.EditorInfo.IME_ACTION_NONE
import android.view.inputmethod.EditorInfo.IME_FLAG_NO_ENTER_ACTION import android.view.inputmethod.EditorInfo.IME_FLAG_NO_ENTER_ACTION
import android.view.inputmethod.EditorInfo.IME_MASK_ACTION import android.view.inputmethod.EditorInfo.IME_MASK_ACTION
import android.view.inputmethod.ExtractedTextRequest import android.view.inputmethod.ExtractedTextRequest
import com.simplemobiletools.commons.extensions.getSharedPrefs
import com.simplemobiletools.keyboard.R import com.simplemobiletools.keyboard.R
import com.simplemobiletools.keyboard.extensions.config import com.simplemobiletools.keyboard.extensions.config
import com.simplemobiletools.keyboard.helpers.* import com.simplemobiletools.keyboard.helpers.*
@ -21,7 +23,7 @@ import com.simplemobiletools.keyboard.views.MyKeyboardView
import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.* import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.*
// based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/ // based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/
class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionListener { class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionListener, SharedPreferences.OnSharedPreferenceChangeListener {
private var SHIFT_PERM_TOGGLE_SPEED = 500 // how quickly do we have to doubletap shift to enable permanent caps lock private var SHIFT_PERM_TOGGLE_SPEED = 500 // how quickly do we have to doubletap shift to enable permanent caps lock
private val KEYBOARD_LETTERS = 0 private val KEYBOARD_LETTERS = 0
private val KEYBOARD_SYMBOLS = 1 private val KEYBOARD_SYMBOLS = 1
@ -38,6 +40,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
override fun onInitializeInterface() { override fun onInitializeInterface() {
super.onInitializeInterface() super.onInitializeInterface()
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType) keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType)
getSharedPrefs().registerOnSharedPreferenceChangeListener(this)
} }
override fun onCreateInputView(): View { override fun onCreateInputView(): View {
@ -275,4 +278,8 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
else -> R.xml.keys_letters_english_qwerty else -> R.xml.keys_letters_english_qwerty
} }
} }
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
keyboardView?.setupKeyboard()
}
} }

View File

@ -104,6 +104,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private var mTextColor = 0 private var mTextColor = 0
private var mBackgroundColor = 0 private var mBackgroundColor = 0
private var mPrimaryColor = 0 private var mPrimaryColor = 0
private var mKeyColor = 0
private var mKeyColorPressed = 0
private var mPreviewText: TextView? = null private var mPreviewText: TextView? = null
private val mPreviewPopup: PopupWindow private val mPreviewPopup: PopupWindow
@ -155,6 +157,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private var ignoreTouches = false private var ignoreTouches = false
private var mKeyBackground: Drawable? = null private var mKeyBackground: Drawable? = null
private var mShowKeyBorders: Boolean = false
private var mUsingSystemTheme: Boolean = true
private var mToolbarHolder: View? = null private var mToolbarHolder: View? = null
private var mClipboardManagerHolder: View? = null private var mClipboardManagerHolder: View? = null
@ -272,61 +276,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
closeEmojiPalette() closeEmojiPalette()
if (visibility == VISIBLE) { if (visibility == VISIBLE) {
mTextColor = context.getProperTextColor() setupKeyboard(changedView)
mBackgroundColor = context.getProperBackgroundColor()
mPrimaryColor = context.getProperPrimaryColor()
val strokeColor = context.getStrokeColor()
val toolbarColor = getToolbarColor()
val darkerColor = getKeyboardBackgroundColor()
val miniKeyboardBackgroundColor = getToolbarColor(4)
if (changedView == mini_keyboard_view) {
val previewBackground = background as LayerDrawable
previewBackground.findDrawableByLayerId(R.id.button_background_shape).applyColorFilter(miniKeyboardBackgroundColor)
previewBackground.findDrawableByLayerId(R.id.button_background_stroke).applyColorFilter(strokeColor)
background = previewBackground
} else {
background.applyColorFilter(darkerColor)
}
val rippleBg = resources.getDrawable(R.drawable.clipboard_background, context.theme) as RippleDrawable
val layerDrawable = rippleBg.findDrawableByLayerId(R.id.clipboard_background_holder) as LayerDrawable
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_stroke).applyColorFilter(strokeColor)
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_shape).applyColorFilter(mBackgroundColor)
val wasDarkened = mBackgroundColor != mBackgroundColor.darkenColor()
mToolbarHolder?.apply {
top_keyboard_divider.beGoneIf(wasDarkened)
top_keyboard_divider.background = ColorDrawable(strokeColor)
background = ColorDrawable(toolbarColor)
clipboard_value.apply {
background = rippleBg
setTextColor(mTextColor)
setLinkTextColor(mTextColor)
}
settings_cog.applyColorFilter(mTextColor)
pinned_clipboard_items.applyColorFilter(mTextColor)
clipboard_clear.applyColorFilter(mTextColor)
}
mClipboardManagerHolder?.apply {
top_clipboard_divider.beGoneIf(wasDarkened)
top_clipboard_divider.background = ColorDrawable(strokeColor)
clipboard_manager_holder.background = ColorDrawable(toolbarColor)
clipboard_manager_close.applyColorFilter(mTextColor)
clipboard_manager_manage.applyColorFilter(mTextColor)
clipboard_manager_label.setTextColor(mTextColor)
clipboard_content_placeholder_1.setTextColor(mTextColor)
clipboard_content_placeholder_2.setTextColor(mTextColor)
}
setupEmojiPalette(toolbarColor = toolbarColor, backgroundColor = mBackgroundColor, textColor = mTextColor)
setupStoredClips()
} }
} }
@ -421,6 +371,75 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
emojiCompatMetadataVersion = editorInfo.extras?.getInt(EmojiCompat.EDITOR_INFO_METAVERSION_KEY, 0) ?: 0 emojiCompatMetadataVersion = editorInfo.extras?.getInt(EmojiCompat.EDITOR_INFO_METAVERSION_KEY, 0) ?: 0
} }
fun setupKeyboard(changedView: View? = null) {
mTextColor = context.getProperTextColor()
mBackgroundColor = context.getProperBackgroundColor()
mPrimaryColor = context.getProperPrimaryColor()
mShowKeyBorders = context.config.showKeyBorders
mUsingSystemTheme = context.config.isUsingSystemTheme
mKeyBackground = if (mShowKeyBorders) {
resources.getDrawable(R.drawable.keyboard_key_selector_outlined, context.theme)
} else {
resources.getDrawable(R.drawable.keyboard_key_selector, context.theme)
}
mKeyColor = getKeyColor()
mKeyColorPressed = mKeyColor.adjustAlpha(0.2f)
val strokeColor = context.getStrokeColor()
val toolbarColor = getToolbarColor()
val darkerColor = getKeyboardBackgroundColor()
val miniKeyboardBackgroundColor = getToolbarColor(4)
if (changedView != null && changedView == mini_keyboard_view) {
val previewBackground = background as LayerDrawable
previewBackground.findDrawableByLayerId(R.id.button_background_shape).applyColorFilter(miniKeyboardBackgroundColor)
previewBackground.findDrawableByLayerId(R.id.button_background_stroke).applyColorFilter(strokeColor)
background = previewBackground
} else {
background.applyColorFilter(darkerColor)
}
val rippleBg = resources.getDrawable(R.drawable.clipboard_background, context.theme) as RippleDrawable
val layerDrawable = rippleBg.findDrawableByLayerId(R.id.clipboard_background_holder) as LayerDrawable
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_stroke).applyColorFilter(strokeColor)
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_shape).applyColorFilter(mBackgroundColor)
val wasDarkened = mBackgroundColor != mBackgroundColor.darkenColor()
mToolbarHolder?.apply {
top_keyboard_divider.beGoneIf(wasDarkened)
top_keyboard_divider.background = ColorDrawable(strokeColor)
background = ColorDrawable(toolbarColor)
clipboard_value.apply {
background = rippleBg
setTextColor(mTextColor)
setLinkTextColor(mTextColor)
}
settings_cog.applyColorFilter(mTextColor)
pinned_clipboard_items.applyColorFilter(mTextColor)
clipboard_clear.applyColorFilter(mTextColor)
}
mClipboardManagerHolder?.apply {
top_clipboard_divider.beGoneIf(wasDarkened)
top_clipboard_divider.background = ColorDrawable(strokeColor)
clipboard_manager_holder.background = ColorDrawable(toolbarColor)
clipboard_manager_close.applyColorFilter(mTextColor)
clipboard_manager_manage.applyColorFilter(mTextColor)
clipboard_manager_label.setTextColor(mTextColor)
clipboard_content_placeholder_1.setTextColor(mTextColor)
clipboard_content_placeholder_2.setTextColor(mTextColor)
}
setupEmojiPalette(toolbarColor = toolbarColor, backgroundColor = mBackgroundColor, textColor = mTextColor)
setupStoredClips()
}
fun vibrateIfNeeded() { fun vibrateIfNeeded() {
if (context.config.vibrateOnKeypress) { if (context.config.vibrateOnKeypress) {
performHapticFeedback() performHapticFeedback()
@ -614,33 +633,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
} }
private fun setupKeyBackground(key: MyKeyboard.Key, keyCode: Int, canvas: Canvas) { private fun setupKeyBackground(key: MyKeyboard.Key, keyCode: Int, canvas: Canvas) {
val showKeyBorders = context.config.showKeyBorders val keyBackground = when (keyCode) {
val drawableId = when (keyCode) { KEYCODE_SPACE -> getSpaceKeyBackground()
KEYCODE_SPACE -> if (context.config.isUsingSystemTheme) { KEYCODE_ENTER -> getEnterKeyBackground()
if (showKeyBorders) { else -> mKeyBackground
R.drawable.keyboard_space_background_material_outlined
} else {
R.drawable.keyboard_space_background_material
}
} else {
if (showKeyBorders) {
R.drawable.keyboard_key_selector_outlined
} else {
R.drawable.keyboard_space_background
}
}
KEYCODE_ENTER -> if (showKeyBorders) {
R.drawable.keyboard_enter_background_outlined
} else {
R.drawable.keyboard_enter_background
}
else -> if (showKeyBorders) {
R.drawable.keyboard_key_selector_outlined
} else {
R.drawable.keyboard_key_selector
}
} }
val keyBackground = resources.getDrawable(drawableId, context.theme)
val bounds = keyBackground!!.bounds val bounds = keyBackground!!.bounds
if (key.width != bounds.right || key.height != bounds.bottom) { if (key.width != bounds.right || key.height != bounds.bottom) {
@ -660,15 +657,47 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
mPrimaryColor mPrimaryColor
} }
keyBackground.applyColorFilter(keyColor) keyBackground.applyColorFilter(keyColor)
} else if (showKeyBorders && drawableId == R.drawable.keyboard_key_selector_outlined) { } else if (mShowKeyBorders) {
val keyColor = getKeyColor(key.pressed) if (keyCode != KEYCODE_SPACE || !mUsingSystemTheme) {
keyBackground.applyColorFilter(keyColor) val keyColor = if (key.pressed) {
mKeyColorPressed
} else {
mKeyColor
}
keyBackground.applyColorFilter(keyColor)
}
} }
canvas.translate(key.x.toFloat(), key.y.toFloat()) canvas.translate(key.x.toFloat(), key.y.toFloat())
keyBackground.draw(canvas) keyBackground.draw(canvas)
} }
private fun getSpaceKeyBackground(): Drawable? {
val drawableId = if (mUsingSystemTheme) {
if (mShowKeyBorders) {
R.drawable.keyboard_space_background_material_outlined
} else {
R.drawable.keyboard_space_background_material
}
} else {
if (mShowKeyBorders) {
R.drawable.keyboard_key_selector_outlined
} else {
R.drawable.keyboard_space_background
}
}
return resources.getDrawable(drawableId, context.theme)
}
private fun getEnterKeyBackground(): Drawable? {
val drawableId = if (mShowKeyBorders) {
R.drawable.keyboard_enter_background_outlined
} else {
R.drawable.keyboard_enter_background
}
return resources.getDrawable(drawableId, context.theme)
}
private fun handleClipboard() { private fun handleClipboard() {
if (mToolbarHolder != null && mPopupParent.id != R.id.mini_keyboard_view) { if (mToolbarHolder != null && mPopupParent.id != R.id.mini_keyboard_view) {
val clipboardContent = context.getCurrentClip() val clipboardContent = context.getCurrentClip()
@ -1544,7 +1573,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
if (context.config.showKeyBorders && !context.isUsingSystemDarkTheme()) { if (context.config.showKeyBorders && !context.isUsingSystemDarkTheme()) {
val darkerColor = color.darkenColor(factor) val darkerColor = color.darkenColor(factor)
return if (darkerColor == Color.WHITE) { return if (darkerColor == Color.WHITE) {
resources.getColor(R.color.md_grey_200, context.theme).darkenColor(4) resources.getColor(R.color.md_grey_200, context.theme)
} else { } else {
darkerColor darkerColor
} }
@ -1570,7 +1599,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
return maybeDarkenColor(color, 6) return maybeDarkenColor(color, 6)
} }
private fun getKeyColor(pressed: Boolean): Int { private fun getKeyColor(): Int {
val backgroundColor = getKeyboardBackgroundColor() val backgroundColor = getKeyboardBackgroundColor()
val lighterColor = backgroundColor.lightenColor() val lighterColor = backgroundColor.lightenColor()
val keyColor = if (context.config.isUsingSystemTheme) { val keyColor = if (context.config.isUsingSystemTheme) {
@ -1582,11 +1611,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
lighterColor lighterColor
} }
} }
return keyColor
return if (pressed) {
keyColor.adjustAlpha(0.2f)
} else {
keyColor
}
} }
} }