mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-11-28 16:58:41 +01:00
fix(accessibility): don't lose key events to the overlay engine
flutter_overlay_window starts a second Flutter engine when the activity attaches, which registers this plugin again. Its handlers replaced the app engine's in the static Observable, so every key the accessibility service forwarded went to an engine nobody listens on: HID keyboards, remotes and gamepads did nothing while the service swallowed their keys. Keep one receiver per engine and send events to all of them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -40,12 +40,14 @@ class AccessibilityPlugin: FlutterPlugin, Accessibility {
|
||||
Accessibility.setUp(flutterPluginBinding.binaryMessenger, this)
|
||||
StreamEventsStreamHandler.register(flutterPluginBinding.binaryMessenger, windowEventHandler)
|
||||
HidKeyPressedStreamHandler.register(flutterPluginBinding.binaryMessenger, hidEventHandler)
|
||||
Observable.fromServiceWindow = windowEventHandler
|
||||
Observable.fromServiceKeys = hidEventHandler
|
||||
Observable.fromServiceWindow.add(windowEventHandler)
|
||||
Observable.fromServiceKeys.add(hidEventHandler)
|
||||
}
|
||||
|
||||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
|
||||
channel.setMethodCallHandler(null)
|
||||
Observable.fromServiceWindow.remove(windowEventHandler)
|
||||
Observable.fromServiceKeys.remove(hidEventHandler)
|
||||
}
|
||||
|
||||
override fun hasPermission(): Boolean {
|
||||
|
||||
@@ -43,7 +43,7 @@ class AccessibilityService : AccessibilityService(), Listener {
|
||||
}
|
||||
val currentPackageName = event.packageName.toString()
|
||||
val windowSize = getWindowSize()
|
||||
Observable.fromServiceWindow?.onChange(packageName = currentPackageName, window = windowSize)
|
||||
Observable.fromServiceWindow.forEach { it.onChange(packageName = currentPackageName, window = windowSize) }
|
||||
}
|
||||
|
||||
private fun getWindowSize(): Rect {
|
||||
@@ -81,7 +81,7 @@ class AccessibilityService : AccessibilityService(), Listener {
|
||||
)
|
||||
|
||||
// Forward key events to the plugin (Flutter) and swallow them so they don't propagate.
|
||||
Observable.fromServiceKeys?.onKeyEvent(event)
|
||||
Observable.fromServiceKeys.forEach { it.onKeyEvent(event) }
|
||||
// Return true to indicate we've handled the event and it should be swallowed.
|
||||
return true
|
||||
} else {
|
||||
|
||||
@@ -4,11 +4,14 @@ import android.graphics.Rect
|
||||
import android.view.KeyEvent
|
||||
import GlobalAction
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.CopyOnWriteArraySet
|
||||
|
||||
object Observable {
|
||||
var toService: Listener? = null
|
||||
var fromServiceWindow: Receiver? = null
|
||||
var fromServiceKeys: Receiver? = null
|
||||
// One per Flutter engine: the overlay engine registers this plugin too,
|
||||
// and must not take the events away from the app's engine.
|
||||
val fromServiceWindow: MutableSet<Receiver> = CopyOnWriteArraySet()
|
||||
val fromServiceKeys: MutableSet<Receiver> = CopyOnWriteArraySet()
|
||||
var ignoreHidDevices: Boolean = false
|
||||
// Use concurrent set for thread-safe access from AccessibilityService and plugin
|
||||
var handledKeys: Set<String> = ConcurrentHashMap.newKeySet()
|
||||
|
||||
Reference in New Issue
Block a user