fix(shortcuts): move the accessibility service into an optional companion so Play Protect stops blocking the install (#2654)

* fix(shortcuts): move the accessibility service into an optional companion so Play Protect stops blocking the install

* fix(shortcuts): bind only same-signer relays, recheck enablement per call, drop timed-out keys
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-15 09:55:30 -05:00
1 parent 5dd973587f
commit de5f0fe6df
52 files changed
+750 -296

No files matched your search

@@ -1,10 +1,8 @@
package com.droiddeck.launcher.input
import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.AccessibilityServiceInfo
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Rect
import android.hardware.display.DisplayManager
import android.os.Build
import android.os.Handler
@@ -12,9 +10,9 @@ import android.os.HandlerThread
import android.os.SystemClock
import android.util.Log
import android.view.Display
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityWindowInfo
import android.view.inputmethod.InputMethodManager
import com.droiddeck.shortcuts.Relay
import java.util.concurrent.ConcurrentHashMap
object BottomScreenGuests {
@@ -45,7 +43,7 @@ object BottomScreenGuests {
sdk >= 33 && DualScreen.yieldsBottom(model)
}
class BottomScreenWatch(private val service: AccessibilityService) {
class BottomScreenWatch(private val service: Context) {
private class Arrival(val pkg: String, val at: Long)
private val thread = HandlerThread("bottom-screen").apply { start() }
@@ -57,33 +55,22 @@ class BottomScreenWatch(private val service: AccessibilityService) {
private var holders: Map<Int, String?> = emptyMap()
private var ownWindows: Map<Int, Set<Int>> = emptyMap()
private val ownShownAt = mutableMapOf<Int, Long>()
private val check = Runnable { evaluate() }
fun start() {
current = this
service.serviceInfo = service.serviceInfo?.apply {
eventTypes = eventTypes or AccessibilityEvent.TYPE_WINDOWS_CHANGED or AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
flags = flags or AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS
}
handler.post {
homes = homePackages()
keyboards = keyboardPackages()
evaluate()
}
}
fun onEvent(event: AccessibilityEvent) {
when (event.eventType) {
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED -> {
val pkg = event.packageName?.toString() ?: return
if (event.displayId == Display.DEFAULT_DISPLAY || pkg == own || pkg == SYSTEM_UI) return
arrivals[event.displayId] = Arrival(pkg, event.eventTime)
}
AccessibilityEvent.TYPE_WINDOWS_CHANGED -> Unit
else -> return
}
handler.removeCallbacks(check)
handler.postDelayed(check, 150)
fun onArrival(displayId: Int, pkg: String, at: Long) {
if (displayId == Display.DEFAULT_DISPLAY || pkg == own || pkg == SYSTEM_UI) return
arrivals[displayId] = Arrival(pkg, at)
}
fun onWindows(all: Map<Int, List<Relay.Window>>) {
handler.post { evaluate(all) }
}
fun stop() {
@@ -97,25 +84,22 @@ class BottomScreenWatch(private val service: AccessibilityService) {
thread.quitSafely()
}
private fun evaluate() {
private fun evaluate(all: Map<Int, List<Relay.Window>>) {
val seen = HashMap(arrivals)
val all = runCatching { service.windowsOnAllDisplays }.getOrNull() ?: return
val displays = service.getSystemService(DisplayManager::class.java) ?: return
val taken = mutableMapOf<Int, String?>()
val shown = mutableMapOf<Int, Set<Int>>()
val now = SystemClock.uptimeMillis()
for (i in 0 until all.size()) {
val id = all.keyAt(i)
for ((id, list) in all) {
if (id == Display.DEFAULT_DISPLAY) continue
val display = displays.getDisplay(id)?.takeIf(DualScreen::builtIn) ?: continue
val windows = all.valueAt(i)
val windows = list
.filter { !BottomScreenGuests.ignorable(it.type) && cover(it, display) >= BottomScreenGuests.MIN_COVER }
.sortedByDescending { it.layer }
.map { it to packageOf(it) }
val ours = windows.filter { it.second == own }.map { it.first.id }.toSet()
val ours = windows.filter { it.pkg == own }.map { it.id }.toSet()
if (!ownWindows[id].orEmpty().containsAll(ours)) ownShownAt[id] = now
shown[id] = ours
val topPkg = windows.firstNotNullOfOrNull { it.second?.takeUnless { pkg -> pkg == own } }
val topPkg = windows.firstNotNullOfOrNull { it.pkg?.takeUnless { pkg -> pkg == own } }
val arrival = seen[id]?.takeIf { BottomScreenGuests.current(it.at, ownShownAt[id]) }
seen[id]?.takeIf { topPkg != null || arrival == null }?.let { arrivals.remove(id, it) }
BottomScreenGuests.holder(topPkg, arrival?.pkg?.takeUnless { it in keyboards }, homes)?.let { taken[id] = it }
@@ -127,15 +111,11 @@ class BottomScreenWatch(private val service: AccessibilityService) {
DualScreen.setTaken(taken.keys)
}
private fun cover(window: AccessibilityWindowInfo, display: Display): Float {
val bounds = Rect().also(window::getBoundsInScreen)
private fun cover(window: Relay.Window, display: Display): Float {
val area = display.mode.physicalWidth.toFloat() * display.mode.physicalHeight
return if (area <= 0f) 0f else bounds.width().toFloat() * bounds.height() / area
return if (area <= 0f) 0f else window.bounds.width().toFloat() * window.bounds.height() / area
}
private fun packageOf(window: AccessibilityWindowInfo): String? =
runCatching { window.getRoot(0)?.packageName?.toString() }.getOrNull()
private fun homePackages(): Set<String> {
val pm = service.packageManager
return listOf(Intent.CATEGORY_HOME, Intent.CATEGORY_SECONDARY_HOME).flatMap { category ->
@@ -154,6 +134,7 @@ class BottomScreenWatch(private val service: AccessibilityService) {
private companion object {
@Volatile var current: BottomScreenWatch? = null
const val TAG = "BottomScreen"
const val SYSTEM_UI = "com.android.systemui"
}
@@ -1,14 +1,17 @@
package com.droiddeck.launcher.input
import android.accessibilityservice.AccessibilityService
import android.app.Service
import android.content.Context
import android.content.Intent
import android.hardware.input.InputManager
import android.os.Binder
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.os.Parcel
import android.view.KeyEvent
import android.view.accessibility.AccessibilityEvent
import com.droiddeck.launcher.HomeApp
import com.droiddeck.shortcuts.Relay
object HomeButtonShortcuts {
private var owner: Any? = null
@@ -101,38 +104,78 @@ object SystemKeyCapture {
}
}
class HomeButtonService : AccessibilityService(), InputManager.InputDeviceListener {
class ShortcutsRelay : Service(), InputManager.InputDeviceListener {
private val main = Handler(Looper.getMainLooper())
private val trusted = java.util.concurrent.ConcurrentHashMap.newKeySet<Int>()
private var bottomScreen: BottomScreenWatch? = null
override fun onServiceConnected() {
getSystemService(InputManager::class.java).registerInputDeviceListener(this, Handler(Looper.getMainLooper()))
if (bottomScreen == null && BottomScreenGuests.supported()) bottomScreen = BottomScreenWatch(this).also { it.start() }
private val binder = object : Binder() {
override fun onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int): Boolean {
if (code !in Relay.CONFIG..Relay.WINDOWS) return super.onTransact(code, data, reply, flags)
data.enforceInterface(Relay.DESCRIPTOR)
if (!trusts(Binder.getCallingUid())) return false
when (code) {
Relay.CONFIG -> reply?.writeInt(if (bottomScreen != null) Relay.WANTS_WINDOWS else 0)
Relay.KEY -> reply?.writeInt(if (onMain(KeyEvent.CREATOR.createFromParcel(data))) 1 else 0)
Relay.INTERRUPT -> main.post { HomeButtonShortcuts.release() }
Relay.ARRIVAL -> bottomScreen?.onArrival(data.readInt(), data.readString() ?: return true, data.readLong())
Relay.WINDOWS -> bottomScreen?.onWindows(Relay.decode(data.createByteArray() ?: return true))
}
return true
}
}
private fun stopBottomScreen() {
bottomScreen?.stop()
bottomScreen = null
private fun trusts(uid: Int): Boolean {
if (uid !in trusted) {
if (!packageManager.getPackagesForUid(uid).orEmpty().contains(Relay.PACKAGE)) return false
trusted += uid
}
return com.droiddeck.launcher.core.SteamIntegration.shortcutsEnabled(this)
}
private fun onMain(event: KeyEvent): Boolean {
// A key the companion already gave up on must not run later and leave a press without its release.
val claimed = java.util.concurrent.atomic.AtomicBoolean(false)
val task = java.util.concurrent.FutureTask {
claimed.compareAndSet(false, true) &&
(SystemKeyCapture.onKeyEvent(event) || HomeButtonShortcuts.onKeyEvent(this, event))
}
main.postAtFrontOfQueue(task)
return try {
task.get(KEY_TIMEOUT_MS, java.util.concurrent.TimeUnit.MILLISECONDS)
} catch (e: java.util.concurrent.TimeoutException) {
if (claimed.compareAndSet(false, true)) false else runCatching { task.get() }.getOrDefault(false)
} catch (e: Exception) {
false
}
}
override fun onCreate() {
super.onCreate()
getSystemService(InputManager::class.java).registerInputDeviceListener(this, main)
if (BottomScreenGuests.supported()) bottomScreen = BottomScreenWatch(this).also { it.start() }
}
override fun onBind(intent: Intent?): IBinder = binder
override fun onUnbind(intent: Intent?): Boolean {
HomeButtonShortcuts.release()
return super.onUnbind(intent)
}
override fun onKeyEvent(event: KeyEvent) =
SystemKeyCapture.onKeyEvent(event) || HomeButtonShortcuts.onKeyEvent(this, event)
override fun onAccessibilityEvent(event: AccessibilityEvent?) { event?.let { bottomScreen?.onEvent(it) } }
override fun onInterrupt() { HomeButtonShortcuts.release() }
override fun onInputDeviceAdded(deviceId: Int) {}
override fun onInputDeviceChanged(deviceId: Int) {}
override fun onInputDeviceRemoved(deviceId: Int) { HomeButtonShortcuts.onDeviceRemoved(deviceId) }
override fun onUnbind(intent: Intent?): Boolean {
HomeButtonShortcuts.release()
stopBottomScreen()
com.droiddeck.launcher.core.SteamIntegration.syncShortcutsListing(this)
return super.onUnbind(intent)
}
override fun onDestroy() {
HomeButtonShortcuts.release()
stopBottomScreen()
bottomScreen?.stop()
bottomScreen = null
getSystemService(InputManager::class.java).unregisterInputDeviceListener(this)
super.onDestroy()
}
private companion object {
const val KEY_TIMEOUT_MS = 200L
}
}