Skip to content

Commit

Permalink
Fix to keyboard scan state
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenMcGirr committed Mar 7, 2024
1 parent c232b14 commit 677a5fd
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,42 @@ object KeyboardInfo {
var keyboardHeight = 0

// Track last scan state to go back to it after keyboard is dismissed
var lastScanState: ScanReceiver.ReceiverState = ScanReceiver.ReceiverState.CURSOR
private var lastScanState: ScanReceiver.ReceiverState = ScanReceiver.state

// Track last update time to prevent multiple updates in a short time
private var lastUpdateTime: Long = 0

fun updateKeyboardState(windows: List<AccessibilityWindowInfo>) {
if (System.currentTimeMillis() - lastUpdateTime < 50) {
return
}
lastUpdateTime = System.currentTimeMillis()
val keyboardWindow = windows.firstOrNull { window ->
window.type == AccessibilityWindowInfo.TYPE_INPUT_METHOD
}
if (keyboardWindow != null) {
if (!isKeyboardVisible) {
// Go to cursor as keyboard keys don't report AccessibilityNodeInfo
if (ScanReceiver.state == ScanReceiver.ReceiverState.ITEM_SCAN) {
lastScanState = ScanReceiver.ReceiverState.ITEM_SCAN
ScanReceiver.state = ScanReceiver.ReceiverState.CURSOR
} else {
lastScanState = ScanReceiver.state
}
}

isKeyboardVisible = true
val rect = Rect()
keyboardWindow.getBoundsInScreen(rect)
keyboardHeight = rect.height()

// Go to cursor as keyboard keys don't report AccessibilityNodeInfo
if (ScanReceiver.state == ScanReceiver.ReceiverState.ITEM_SCAN) {
lastScanState = ScanReceiver.state
ScanReceiver.state = ScanReceiver.ReceiverState.CURSOR
}
} else {
isKeyboardVisible = false
keyboardHeight = 0

// Go back to last scan state
if (ScanReceiver.state == ScanReceiver.ReceiverState.CURSOR) {
if (isKeyboardVisible) {
// Go back to last scan state
ScanReceiver.state = lastScanState
}

isKeyboardVisible = false
keyboardHeight = 0
}
Log.d("KeyboardInfo", "isKeyboardVisible: $isKeyboardVisible window count: ${windows.size}")
}
Expand Down

0 comments on commit 677a5fd

Please sign in to comment.