fix: set focus and FLAG_RETRIEVE_INTERACTIVE_WINDOWS

This commit is contained in:
mcfans 2023-11-01 00:06:45 +08:00
parent 9521ac6adb
commit 47d57ddf70

View File

@ -19,6 +19,7 @@ import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import android.accessibilityservice.AccessibilityServiceInfo
import android.accessibilityservice.AccessibilityServiceInfo.FLAG_INPUT_METHOD_EDITOR
import android.accessibilityservice.AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS
import android.view.inputmethod.EditorInfo
import androidx.annotation.RequiresApi
import java.util.*
@ -289,6 +290,8 @@ class InputService : AccessibilityService() {
}
}
Log.d(logTag, "onKeyEvent $keyEvent textToCommit:$textToCommit")
if (Build.VERSION.SDK_INT >= 33) {
getInputMethod()?.let { inputMethod ->
inputMethod.getCurrentInputConnection()?.let { inputConnection ->
@ -308,6 +311,7 @@ class InputService : AccessibilityService() {
handler.post {
KeyEventConverter.toAndroidKeyEvent(keyEvent)?.let { event ->
val possibleNodes = possibleAccessibiltyNodes()
Log.d(logTag, "possibleNodes:$possibleNodes")
for (item in possibleNodes) {
val success = trySendKeyEvent(event, item, textToCommit)
if (success) {
@ -374,6 +378,8 @@ class InputService : AccessibilityService() {
val rootInActiveWindow = getRootInActiveWindow()
Log.d(logTag, "focusInput:$focusInput focusAccessibilityInput:$focusAccessibilityInput rootInActiveWindow:$rootInActiveWindow")
if (focusInput != null) {
if (focusInput.isFocusable() && focusInput.isEditable()) {
insertAccessibilityNode(linkedList, focusInput)
@ -391,6 +397,7 @@ class InputService : AccessibilityService() {
}
val childFromFocusInput = findChildNode(focusInput)
Log.d(logTag, "childFromFocusInput:$childFromFocusInput")
if (childFromFocusInput != null) {
insertAccessibilityNode(linkedList, childFromFocusInput)
@ -400,6 +407,7 @@ class InputService : AccessibilityService() {
if (childFromFocusAccessibilityInput != null) {
insertAccessibilityNode(linkedList, childFromFocusAccessibilityInput)
}
Log.d(logTag, "childFromFocusAccessibilityInput:$childFromFocusAccessibilityInput")
if (rootInActiveWindow != null) {
insertAccessibilityNode(linkedList, rootInActiveWindow)
@ -439,7 +447,12 @@ class InputService : AccessibilityService() {
var success = false
Log.d(logTag, "existing text:$text textToCommit:$textToCommit textSelectionStart:$textSelectionStart textSelectionEnd:$textSelectionEnd")
val focused = this.fakeEditTextForTextStateCalculation?.isFocused
this.fakeEditTextForTextStateCalculation?.let {
it.requestFocus()
}
Log.d(logTag, "existing text:$text textToCommit:$textToCommit textSelectionStart:$textSelectionStart textSelectionEnd:$textSelectionEnd $focused")
if (textToCommit != null) {
var newText = ""
@ -511,6 +524,8 @@ class InputService : AccessibilityService() {
}
}
}
fakeEditTextForTextStateCalculation?.setFocusableInTouchMode(false);
fakeEditTextForTextStateCalculation?.setFocusable(false);
return success
}
@ -525,8 +540,11 @@ class InputService : AccessibilityService() {
if (Build.VERSION.SDK_INT >= 33) {
info.flags = FLAG_INPUT_METHOD_EDITOR
}
info.flags = FLAG_RETRIEVE_INTERACTIVE_WINDOWS
setServiceInfo(info)
fakeEditTextForTextStateCalculation = EditText(this)
fakeEditTextForTextStateCalculation?.setFocusable(false)
fakeEditTextForTextStateCalculation?.setFocusableInTouchMode(false)
Log.d(logTag, "onServiceConnected!")
}