Skip to content

Commit 6a6b70d

Browse files
committed
Update keyboard on preference changes
1 parent 8c5e422 commit 6a6b70d

File tree

2 files changed

+124
-93
lines changed

2 files changed

+124
-93
lines changed

app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.simplemobiletools.keyboard.services
22

3+
import android.content.SharedPreferences
34
import android.inputmethodservice.InputMethodService
45
import android.text.InputType
56
import android.text.InputType.TYPE_CLASS_DATETIME
@@ -14,14 +15,15 @@ import android.view.inputmethod.EditorInfo.IME_ACTION_NONE
1415
import android.view.inputmethod.EditorInfo.IME_FLAG_NO_ENTER_ACTION
1516
import android.view.inputmethod.EditorInfo.IME_MASK_ACTION
1617
import android.view.inputmethod.ExtractedTextRequest
18+
import com.simplemobiletools.commons.extensions.getSharedPrefs
1719
import com.simplemobiletools.keyboard.R
1820
import com.simplemobiletools.keyboard.extensions.config
1921
import com.simplemobiletools.keyboard.helpers.*
2022
import com.simplemobiletools.keyboard.views.MyKeyboardView
2123
import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.*
2224

2325
// based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/
24-
class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionListener {
26+
class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionListener, SharedPreferences.OnSharedPreferenceChangeListener {
2527
private var SHIFT_PERM_TOGGLE_SPEED = 500 // how quickly do we have to doubletap shift to enable permanent caps lock
2628
private val KEYBOARD_LETTERS = 0
2729
private val KEYBOARD_SYMBOLS = 1
@@ -38,6 +40,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
3840
override fun onInitializeInterface() {
3941
super.onInitializeInterface()
4042
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType)
43+
getSharedPrefs().registerOnSharedPreferenceChangeListener(this)
4144
}
4245

4346
override fun onCreateInputView(): View {
@@ -275,4 +278,8 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
275278
else -> R.xml.keys_letters_english_qwerty
276279
}
277280
}
281+
282+
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
283+
keyboardView?.setupKeyboard()
284+
}
278285
}

app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt

Lines changed: 116 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
104104
private var mTextColor = 0
105105
private var mBackgroundColor = 0
106106
private var mPrimaryColor = 0
107+
private var mKeyColor = 0
108+
private var mKeyColorPressed = 0
107109

108110
private var mPreviewText: TextView? = null
109111
private val mPreviewPopup: PopupWindow
@@ -155,6 +157,8 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
155157
private var ignoreTouches = false
156158

157159
private var mKeyBackground: Drawable? = null
160+
private var mShowKeyBorders: Boolean = false
161+
private var mUsingSystemTheme: Boolean = true
158162

159163
private var mToolbarHolder: View? = null
160164
private var mClipboardManagerHolder: View? = null
@@ -272,61 +276,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
272276
closeEmojiPalette()
273277

274278
if (visibility == VISIBLE) {
275-
mTextColor = context.getProperTextColor()
276-
mBackgroundColor = context.getProperBackgroundColor()
277-
mPrimaryColor = context.getProperPrimaryColor()
278-
val strokeColor = context.getStrokeColor()
279-
280-
val toolbarColor = getToolbarColor()
281-
val darkerColor = getKeyboardBackgroundColor()
282-
val miniKeyboardBackgroundColor = getToolbarColor(4)
283-
284-
if (changedView == mini_keyboard_view) {
285-
val previewBackground = background as LayerDrawable
286-
previewBackground.findDrawableByLayerId(R.id.button_background_shape).applyColorFilter(miniKeyboardBackgroundColor)
287-
previewBackground.findDrawableByLayerId(R.id.button_background_stroke).applyColorFilter(strokeColor)
288-
background = previewBackground
289-
} else {
290-
background.applyColorFilter(darkerColor)
291-
}
292-
293-
val rippleBg = resources.getDrawable(R.drawable.clipboard_background, context.theme) as RippleDrawable
294-
val layerDrawable = rippleBg.findDrawableByLayerId(R.id.clipboard_background_holder) as LayerDrawable
295-
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_stroke).applyColorFilter(strokeColor)
296-
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_shape).applyColorFilter(mBackgroundColor)
297-
298-
val wasDarkened = mBackgroundColor != mBackgroundColor.darkenColor()
299-
mToolbarHolder?.apply {
300-
top_keyboard_divider.beGoneIf(wasDarkened)
301-
top_keyboard_divider.background = ColorDrawable(strokeColor)
302-
303-
background = ColorDrawable(toolbarColor)
304-
clipboard_value.apply {
305-
background = rippleBg
306-
setTextColor(mTextColor)
307-
setLinkTextColor(mTextColor)
308-
}
309-
310-
settings_cog.applyColorFilter(mTextColor)
311-
pinned_clipboard_items.applyColorFilter(mTextColor)
312-
clipboard_clear.applyColorFilter(mTextColor)
313-
}
314-
315-
mClipboardManagerHolder?.apply {
316-
top_clipboard_divider.beGoneIf(wasDarkened)
317-
top_clipboard_divider.background = ColorDrawable(strokeColor)
318-
clipboard_manager_holder.background = ColorDrawable(toolbarColor)
319-
320-
clipboard_manager_close.applyColorFilter(mTextColor)
321-
clipboard_manager_manage.applyColorFilter(mTextColor)
322-
323-
clipboard_manager_label.setTextColor(mTextColor)
324-
clipboard_content_placeholder_1.setTextColor(mTextColor)
325-
clipboard_content_placeholder_2.setTextColor(mTextColor)
326-
}
327-
328-
setupEmojiPalette(toolbarColor = toolbarColor, backgroundColor = mBackgroundColor, textColor = mTextColor)
329-
setupStoredClips()
279+
setupKeyboard(changedView)
330280
}
331281
}
332282

@@ -421,6 +371,75 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
421371
emojiCompatMetadataVersion = editorInfo.extras?.getInt(EmojiCompat.EDITOR_INFO_METAVERSION_KEY, 0) ?: 0
422372
}
423373

374+
fun setupKeyboard(changedView: View? = null) {
375+
mTextColor = context.getProperTextColor()
376+
mBackgroundColor = context.getProperBackgroundColor()
377+
mPrimaryColor = context.getProperPrimaryColor()
378+
379+
mShowKeyBorders = context.config.showKeyBorders
380+
mUsingSystemTheme = context.config.isUsingSystemTheme
381+
mKeyBackground = if (mShowKeyBorders) {
382+
resources.getDrawable(R.drawable.keyboard_key_selector_outlined, context.theme)
383+
} else {
384+
resources.getDrawable(R.drawable.keyboard_key_selector, context.theme)
385+
}
386+
mKeyColor = getKeyColor()
387+
mKeyColorPressed = mKeyColor.adjustAlpha(0.2f)
388+
389+
val strokeColor = context.getStrokeColor()
390+
391+
val toolbarColor = getToolbarColor()
392+
val darkerColor = getKeyboardBackgroundColor()
393+
val miniKeyboardBackgroundColor = getToolbarColor(4)
394+
395+
if (changedView != null && changedView == mini_keyboard_view) {
396+
val previewBackground = background as LayerDrawable
397+
previewBackground.findDrawableByLayerId(R.id.button_background_shape).applyColorFilter(miniKeyboardBackgroundColor)
398+
previewBackground.findDrawableByLayerId(R.id.button_background_stroke).applyColorFilter(strokeColor)
399+
background = previewBackground
400+
} else {
401+
background.applyColorFilter(darkerColor)
402+
}
403+
404+
val rippleBg = resources.getDrawable(R.drawable.clipboard_background, context.theme) as RippleDrawable
405+
val layerDrawable = rippleBg.findDrawableByLayerId(R.id.clipboard_background_holder) as LayerDrawable
406+
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_stroke).applyColorFilter(strokeColor)
407+
layerDrawable.findDrawableByLayerId(R.id.clipboard_background_shape).applyColorFilter(mBackgroundColor)
408+
409+
val wasDarkened = mBackgroundColor != mBackgroundColor.darkenColor()
410+
mToolbarHolder?.apply {
411+
top_keyboard_divider.beGoneIf(wasDarkened)
412+
top_keyboard_divider.background = ColorDrawable(strokeColor)
413+
414+
background = ColorDrawable(toolbarColor)
415+
clipboard_value.apply {
416+
background = rippleBg
417+
setTextColor(mTextColor)
418+
setLinkTextColor(mTextColor)
419+
}
420+
421+
settings_cog.applyColorFilter(mTextColor)
422+
pinned_clipboard_items.applyColorFilter(mTextColor)
423+
clipboard_clear.applyColorFilter(mTextColor)
424+
}
425+
426+
mClipboardManagerHolder?.apply {
427+
top_clipboard_divider.beGoneIf(wasDarkened)
428+
top_clipboard_divider.background = ColorDrawable(strokeColor)
429+
clipboard_manager_holder.background = ColorDrawable(toolbarColor)
430+
431+
clipboard_manager_close.applyColorFilter(mTextColor)
432+
clipboard_manager_manage.applyColorFilter(mTextColor)
433+
434+
clipboard_manager_label.setTextColor(mTextColor)
435+
clipboard_content_placeholder_1.setTextColor(mTextColor)
436+
clipboard_content_placeholder_2.setTextColor(mTextColor)
437+
}
438+
439+
setupEmojiPalette(toolbarColor = toolbarColor, backgroundColor = mBackgroundColor, textColor = mTextColor)
440+
setupStoredClips()
441+
}
442+
424443
fun vibrateIfNeeded() {
425444
if (context.config.vibrateOnKeypress) {
426445
performHapticFeedback()
@@ -614,33 +633,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
614633
}
615634

616635
private fun setupKeyBackground(key: MyKeyboard.Key, keyCode: Int, canvas: Canvas) {
617-
val showKeyBorders = context.config.showKeyBorders
618-
val drawableId = when (keyCode) {
619-
KEYCODE_SPACE -> if (context.config.isUsingSystemTheme) {
620-
if (showKeyBorders) {
621-
R.drawable.keyboard_space_background_material_outlined
622-
} else {
623-
R.drawable.keyboard_space_background_material
624-
}
625-
} else {
626-
if (showKeyBorders) {
627-
R.drawable.keyboard_key_selector_outlined
628-
} else {
629-
R.drawable.keyboard_space_background
630-
}
631-
}
632-
KEYCODE_ENTER -> if (showKeyBorders) {
633-
R.drawable.keyboard_enter_background_outlined
634-
} else {
635-
R.drawable.keyboard_enter_background
636-
}
637-
else -> if (showKeyBorders) {
638-
R.drawable.keyboard_key_selector_outlined
639-
} else {
640-
R.drawable.keyboard_key_selector
641-
}
636+
val keyBackground = when (keyCode) {
637+
KEYCODE_SPACE -> getSpaceKeyBackground()
638+
KEYCODE_ENTER -> getEnterKeyBackground()
639+
else -> mKeyBackground
642640
}
643-
val keyBackground = resources.getDrawable(drawableId, context.theme)
644641

645642
val bounds = keyBackground!!.bounds
646643
if (key.width != bounds.right || key.height != bounds.bottom) {
@@ -660,15 +657,47 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
660657
mPrimaryColor
661658
}
662659
keyBackground.applyColorFilter(keyColor)
663-
} else if (showKeyBorders && drawableId == R.drawable.keyboard_key_selector_outlined) {
664-
val keyColor = getKeyColor(key.pressed)
665-
keyBackground.applyColorFilter(keyColor)
660+
} else if (mShowKeyBorders) {
661+
if (keyCode != KEYCODE_SPACE || !mUsingSystemTheme) {
662+
val keyColor = if (key.pressed) {
663+
mKeyColorPressed
664+
} else {
665+
mKeyColor
666+
}
667+
keyBackground.applyColorFilter(keyColor)
668+
}
666669
}
667670

668671
canvas.translate(key.x.toFloat(), key.y.toFloat())
669672
keyBackground.draw(canvas)
670673
}
671674

675+
private fun getSpaceKeyBackground(): Drawable? {
676+
val drawableId = if (mUsingSystemTheme) {
677+
if (mShowKeyBorders) {
678+
R.drawable.keyboard_space_background_material_outlined
679+
} else {
680+
R.drawable.keyboard_space_background_material
681+
}
682+
} else {
683+
if (mShowKeyBorders) {
684+
R.drawable.keyboard_key_selector_outlined
685+
} else {
686+
R.drawable.keyboard_space_background
687+
}
688+
}
689+
return resources.getDrawable(drawableId, context.theme)
690+
}
691+
692+
private fun getEnterKeyBackground(): Drawable? {
693+
val drawableId = if (mShowKeyBorders) {
694+
R.drawable.keyboard_enter_background_outlined
695+
} else {
696+
R.drawable.keyboard_enter_background
697+
}
698+
return resources.getDrawable(drawableId, context.theme)
699+
}
700+
672701
private fun handleClipboard() {
673702
if (mToolbarHolder != null && mPopupParent.id != R.id.mini_keyboard_view) {
674703
val clipboardContent = context.getCurrentClip()
@@ -1544,7 +1573,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
15441573
if (context.config.showKeyBorders && !context.isUsingSystemDarkTheme()) {
15451574
val darkerColor = color.darkenColor(factor)
15461575
return if (darkerColor == Color.WHITE) {
1547-
resources.getColor(R.color.md_grey_200, context.theme).darkenColor(4)
1576+
resources.getColor(R.color.md_grey_200, context.theme)
15481577
} else {
15491578
darkerColor
15501579
}
@@ -1570,7 +1599,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
15701599
return maybeDarkenColor(color, 6)
15711600
}
15721601

1573-
private fun getKeyColor(pressed: Boolean): Int {
1602+
private fun getKeyColor(): Int {
15741603
val backgroundColor = getKeyboardBackgroundColor()
15751604
val lighterColor = backgroundColor.lightenColor()
15761605
val keyColor = if (context.config.isUsingSystemTheme) {
@@ -1582,11 +1611,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
15821611
lighterColor
15831612
}
15841613
}
1585-
1586-
return if (pressed) {
1587-
keyColor.adjustAlpha(0.2f)
1588-
} else {
1589-
keyColor
1590-
}
1614+
return keyColor
15911615
}
15921616
}

0 commit comments

Comments
 (0)