OnKeyboardActionListener extracted to a different file

This commit is contained in:
merkost 2023-06-09 10:31:08 +10:00
parent ca53db62c1
commit 29c8c763c0
3 changed files with 46 additions and 44 deletions

View File

@ -0,0 +1,41 @@
package com.simplemobiletools.keyboard.interfaces
interface OnKeyboardActionListener {
/**
* Called when the user presses a key. This is sent before the [.onKey] is called. For keys that repeat, this is only called once.
* @param primaryCode the unicode of the key being pressed. If the touch is not on a valid key, the value will be zero.
*/
fun onPress(primaryCode: Int)
/**
* Send a key press to the listener.
* @param code this is the key that was pressed
*/
fun onKey(code: Int)
/**
* Called when the finger has been lifted after pressing a key
*/
fun onActionUp()
/**
* Called when the user long presses Space and moves to the left
*/
fun moveCursorLeft()
/**
* Called when the user long presses Space and moves to the right
*/
fun moveCursorRight()
/**
* Sends a sequence of characters to the listener.
* @param text the string to be displayed.
*/
fun onText(text: String)
/**
* Called to force the KeyboardView to reload the keyboard
*/
fun reloadKeyboard()
}

View File

@ -19,11 +19,12 @@ 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.*
import com.simplemobiletools.keyboard.interfaces.OnKeyboardActionListener
import com.simplemobiletools.keyboard.views.MyKeyboardView 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, SharedPreferences.OnSharedPreferenceChangeListener { class SimpleKeyboardIME : InputMethodService(), 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

View File

@ -47,6 +47,7 @@ import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_ENTER
import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_MODE_CHANGE import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_MODE_CHANGE
import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SHIFT import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SHIFT
import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SPACE import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SPACE
import com.simplemobiletools.keyboard.interfaces.OnKeyboardActionListener
import com.simplemobiletools.keyboard.interfaces.RefreshClipsListener import com.simplemobiletools.keyboard.interfaces.RefreshClipsListener
import com.simplemobiletools.keyboard.models.Clip import com.simplemobiletools.keyboard.models.Clip
import com.simplemobiletools.keyboard.models.ClipsSectionLabel import com.simplemobiletools.keyboard.models.ClipsSectionLabel
@ -55,50 +56,10 @@ import kotlinx.android.synthetic.main.keyboard_popup_keyboard.view.*
import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.* import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.*
import java.util.* import java.util.*
@SuppressLint("UseCompatLoadingForDrawables", "ClickableViewAccessibility") @SuppressLint("UseCompatLoadingForDrawables")
class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int = 0) : class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int = 0) :
View(context, attrs, defStyleRes) { View(context, attrs, defStyleRes) {
interface OnKeyboardActionListener {
/**
* Called when the user presses a key. This is sent before the [.onKey] is called. For keys that repeat, this is only called once.
* @param primaryCode the unicode of the key being pressed. If the touch is not on a valid key, the value will be zero.
*/
fun onPress(primaryCode: Int)
/**
* Send a key press to the listener.
* @param code this is the key that was pressed
*/
fun onKey(code: Int)
/**
* Called when the finger has been lifted after pressing a key
*/
fun onActionUp()
/**
* Called when the user long presses Space and moves to the left
*/
fun moveCursorLeft()
/**
* Called when the user long presses Space and moves to the right
*/
fun moveCursorRight()
/**
* Sends a sequence of characters to the listener.
* @param text the string to be displayed.
*/
fun onText(text: String)
/**
* Called to force the KeyboardView to reload the keyboard
*/
fun reloadKeyboard()
}
override fun dispatchHoverEvent(event: MotionEvent): Boolean { override fun dispatchHoverEvent(event: MotionEvent): Boolean {
return if (accessHelper.dispatchHoverEvent(event)) { return if (accessHelper.dispatchHoverEvent(event)) {
true true
@ -109,7 +70,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private val accessHelper = AccessHelper() private val accessHelper = AccessHelper()
inner class AccessHelper() : ExploreByTouchHelper(this) { inner class AccessHelper() : ExploreByTouchHelper(this) {
/** /**
@ -560,7 +520,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private fun adjustCase(label: CharSequence): CharSequence? { private fun adjustCase(label: CharSequence): CharSequence? {
var newLabel: CharSequence? = label var newLabel: CharSequence? = label
if (newLabel != null && newLabel.isNotEmpty() && mKeyboard!!.mShiftState > SHIFT_OFF && newLabel.length < 3 && Character.isLowerCase(newLabel[0])) { if (!newLabel.isNullOrEmpty() && mKeyboard!!.mShiftState > SHIFT_OFF && newLabel.length < 3 && Character.isLowerCase(newLabel[0])) {
newLabel = newLabel.toString().uppercase(Locale.getDefault()) newLabel = newLabel.toString().uppercase(Locale.getDefault())
} }
return newLabel return newLabel