@@ -16,14 +16,13 @@ import android.os.Message
1616import android.util.AttributeSet
1717import android.util.TypedValue
1818import android.view.*
19- import android.view.accessibility.AccessibilityEvent
20- import android.view.accessibility.AccessibilityManager
2119import android.view.animation.AccelerateInterpolator
2220import android.view.inputmethod.EditorInfo
2321import android.widget.PopupWindow
2422import android.widget.TextView
2523import androidx.core.animation.doOnEnd
2624import androidx.core.animation.doOnStart
25+ import androidx.core.view.ViewCompat
2726import androidx.emoji2.text.EmojiCompat
2827import androidx.emoji2.text.EmojiCompat.EMOJI_SUPPORTED
2928import com.simplemobiletools.commons.extensions.*
@@ -43,6 +42,7 @@ import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_ENTER
4342import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_MODE_CHANGE
4443import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SHIFT
4544import com.simplemobiletools.keyboard.helpers.MyKeyboard.Companion.KEYCODE_SPACE
45+ import com.simplemobiletools.keyboard.interfaces.OnKeyboardActionListener
4646import com.simplemobiletools.keyboard.interfaces.RefreshClipsListener
4747import com.simplemobiletools.keyboard.models.Clip
4848import com.simplemobiletools.keyboard.models.ClipsSectionLabel
@@ -52,49 +52,18 @@ import kotlinx.android.synthetic.main.keyboard_view_keyboard.view.*
5252import java.util.*
5353
5454@SuppressLint(" UseCompatLoadingForDrawables" , " ClickableViewAccessibility" )
55- class MyKeyboardView @JvmOverloads constructor(context : Context , attrs : AttributeSet ? , defStyleRes : Int = 0 ) :
56- View (context, attrs, defStyleRes) {
57-
58- interface OnKeyboardActionListener {
59- /* *
60- * Called when the user presses a key. This is sent before the [.onKey] is called. For keys that repeat, this is only called once.
61- * @param primaryCode the unicode of the key being pressed. If the touch is not on a valid key, the value will be zero.
62- */
63- fun onPress (primaryCode : Int )
64-
65- /* *
66- * Send a key press to the listener.
67- * @param code this is the key that was pressed
68- */
69- fun onKey (code : Int )
70-
71- /* *
72- * Called when the finger has been lifted after pressing a key
73- */
74- fun onActionUp ()
75-
76- /* *
77- * Called when the user long presses Space and moves to the left
78- */
79- fun moveCursorLeft ()
80-
81- /* *
82- * Called when the user long presses Space and moves to the right
83- */
84- fun moveCursorRight ()
85-
86- /* *
87- * Sends a sequence of characters to the listener.
88- * @param text the string to be displayed.
89- */
90- fun onText (text : String )
91-
92- /* *
93- * Called to force the KeyboardView to reload the keyboard
94- */
95- fun reloadKeyboard ()
55+ class MyKeyboardView @JvmOverloads constructor(context : Context , attrs : AttributeSet ? , defStyleRes : Int = 0 ) : View(context, attrs, defStyleRes) {
56+
57+ override fun dispatchHoverEvent (event : MotionEvent ): Boolean {
58+ return if (accessHelper?.dispatchHoverEvent(event) == true ) {
59+ true
60+ } else {
61+ super .dispatchHoverEvent(event)
62+ }
9663 }
9764
65+ private var accessHelper: AccessHelper ? = null
66+
9867 private var mKeyboard: MyKeyboard ? = null
9968 private var mCurrentKeyIndex: Int = NOT_A_KEY
10069
@@ -184,9 +153,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
184153 /* * The canvas for the above mutable keyboard bitmap */
185154 private var mCanvas: Canvas ? = null
186155
187- /* * The accessibility manager for accessibility support */
188- private val mAccessibilityManager: AccessibilityManager
189-
190156 private var mHandler: Handler ? = null
191157
192158 companion object {
@@ -245,7 +211,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
245211 mPaint.textAlign = Align .CENTER
246212 mPaint.alpha = 255
247213 mMiniKeyboardCache = HashMap ()
248- mAccessibilityManager = (context.getSystemService(Context .ACCESSIBILITY_SERVICE ) as AccessibilityManager )
249214 mPopupMaxMoveDistance = resources.getDimension(R .dimen.popup_max_move_distance)
250215 mTopSmallNumberSize = resources.getDimension(R .dimen.small_text_size)
251216 mTopSmallNumberMarginWidth = resources.getDimension(R .dimen.top_small_number_margin_width)
@@ -301,6 +266,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
301266 invalidateAllKeys()
302267 computeProximityThreshold(keyboard)
303268 mMiniKeyboardCache.clear()
269+
270+ accessHelper = AccessHelper (this , mKeyboard?.mKeys.orEmpty())
271+ ViewCompat .setAccessibilityDelegate(this , accessHelper)
272+
304273 // Not really necessary to do every time, but will free up views
305274 // Switching to a different keyboard should abort any pending keys so that the key up
306275 // doesn't get delivered to the old or new keyboard
@@ -479,7 +448,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
479448
480449 private fun adjustCase (label : CharSequence ): CharSequence? {
481450 var newLabel: CharSequence? = label
482- if (newLabel != null && newLabel.isNotEmpty () && mKeyboard!! .mShiftState != ShiftState .OFF && newLabel.length < 3 && Character .isLowerCase(newLabel[0 ])) {
451+ if (! newLabel.isNullOrEmpty () && mKeyboard!! .mShiftState != ShiftState .OFF && newLabel.length < 3 && Character .isLowerCase(newLabel[0 ])) {
483452 newLabel = newLabel.toString().uppercase(Locale .getDefault())
484453 }
485454 return newLabel
@@ -644,10 +613,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
644613 val secondaryIconBottom = secondaryIconTop + secondaryIconHeight
645614
646615 secondaryIcon.setBounds(
647- secondaryIconLeft,
648- secondaryIconTop,
649- secondaryIconRight,
650- secondaryIconBottom
616+ secondaryIconLeft, secondaryIconTop, secondaryIconRight, secondaryIconBottom
651617 )
652618 secondaryIcon.draw(canvas)
653619
@@ -833,29 +799,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
833799 val oldKeyIndex = mCurrentKeyIndex
834800 val previewPopup = mPreviewPopup
835801 mCurrentKeyIndex = keyIndex
836- // Release the old key and press the new key
837- val keys = mKeys
838- if (oldKeyIndex != mCurrentKeyIndex) {
839- if (oldKeyIndex != NOT_A_KEY && keys.size > oldKeyIndex) {
840- val oldKey = keys[oldKeyIndex]
841- oldKey.pressed = false
842- invalidateKey(oldKeyIndex)
843- val keyCode = oldKey.code
844- sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent .TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED , keyCode)
845- }
846-
847- if (mCurrentKeyIndex != NOT_A_KEY && keys.size > mCurrentKeyIndex) {
848- val newKey = keys[mCurrentKeyIndex]
849-
850- val code = newKey.code
851- if (context.config.showKeyBorders || (code == KEYCODE_SHIFT || code == KEYCODE_MODE_CHANGE || code == KEYCODE_DELETE || code == KEYCODE_ENTER || code == KEYCODE_SPACE )) {
852- newKey.pressed = true
853- }
854-
855- invalidateKey(mCurrentKeyIndex)
856- sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent .TYPE_VIEW_ACCESSIBILITY_FOCUSED , code)
857- }
858- }
859802
860803 if (! context.config.showPopupOnKeypress) {
861804 return
@@ -866,8 +809,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
866809 if (previewPopup.isShowing) {
867810 if (keyIndex == NOT_A_KEY ) {
868811 mHandler!! .sendMessageDelayed(
869- mHandler!! .obtainMessage(MSG_REMOVE_PREVIEW ),
870- DELAY_AFTER_PREVIEW .toLong()
812+ mHandler!! .obtainMessage(MSG_REMOVE_PREVIEW ), DELAY_AFTER_PREVIEW .toLong()
871813 )
872814 }
873815 }
@@ -960,22 +902,6 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
960902 }
961903 }
962904
963- private fun sendAccessibilityEventForUnicodeCharacter (eventType : Int , code : Int ) {
964- if (mAccessibilityManager.isEnabled) {
965- val event = AccessibilityEvent .obtain(eventType)
966- onInitializeAccessibilityEvent(event)
967- val text: String = when (code) {
968- KEYCODE_DELETE -> context.getString(R .string.keycode_delete)
969- KEYCODE_ENTER -> context.getString(R .string.keycode_enter)
970- KEYCODE_MODE_CHANGE -> context.getString(R .string.keycode_mode_change)
971- KEYCODE_SHIFT -> context.getString(R .string.keycode_shift)
972- else -> code.toChar().toString()
973- }
974- event.text.add(text)
975- mAccessibilityManager.sendAccessibilityEvent(event)
976- }
977- }
978-
979905 /* *
980906 * Requests a redraw of the entire keyboard. Calling [.invalidate] is not sufficient because the keyboard renders the keys to an off-screen buffer and
981907 * an invalidate() only draws the cached buffer.
@@ -1090,8 +1016,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
10901016 mMiniKeyboard!! .setKeyboard(keyboard)
10911017 mPopupParent = this
10921018 mMiniKeyboardContainer!! .measure(
1093- MeasureSpec .makeMeasureSpec(width, MeasureSpec .AT_MOST ),
1094- MeasureSpec .makeMeasureSpec(height, MeasureSpec .AT_MOST )
1019+ MeasureSpec .makeMeasureSpec(width, MeasureSpec .AT_MOST ), MeasureSpec .makeMeasureSpec(height, MeasureSpec .AT_MOST )
10951020 )
10961021 mMiniKeyboardCache[popupKey] = mMiniKeyboardContainer
10971022 } else {
0 commit comments