Feature: Support input blocking and privacy overlay on Android hosts
remote_block_input rejected anything but Windows because the Android client had no COMMAND_SCREEN_BLOCK_INPUT handler. This adds the overlay on the client and the server gating, and wires up COMMAND_SCREEN_BLANK. Client: the accessibility service hangs a full-screen TYPE_ACCESSIBILITY_OVERLAY on itself. block=true makes it transparent and touchable so it swallows physical taps; black=true paints it black. Both are absolute states, so repeated commands are idempotent, and the handler clears the overlay when the screen sub-connection dies (Android has no "released when the calling thread exits" fallback). During a remote gesture the overlay is flipped to FLAG_NOT_TOUCHABLE, mirroring Windows' BlockInput(false) wrap; the flag change and the injection must be separated by a short delay or the gesture is swallowed. Server: CLIENT_CAP_PRIVACY_OVERLAY distinguishes new APKs from old ones; Linux/macOS and old APKs still get -32005 up front. Tool masks, instructions and mcp/tools.json follow the change. COMMAND_SCREEN_BLANK is implemented but not exposed as a tool: MediaProjection captures the overlay, so blacking out the screen blinds the operator too and cannot emulate Windows' WDA_EXCLUDEFROMCAPTURE. Verified on the Android 15 emulator: blocked taps are swallowed while remote clicks still land, the block re-arms after each gesture, the watchdog auto-unblocks at the deadline, a dropped sub-connection clears the overlay, and a black overlay shows up in both screencap and get_screenshot. The committed ghost.apk is stale; rebuild before release. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
445bd8bbb9
commit
a801a2999a
9 files changed
+306
-24
No files matched your search
@@ -7,14 +7,18 @@ import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.graphics.Color
|
||||
import android.graphics.Path
|
||||
import android.graphics.PixelFormat
|
||||
import android.graphics.PointF
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.os.SystemClock
|
||||
import android.util.Log
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import android.view.accessibility.AccessibilityEvent
|
||||
import android.view.accessibility.AccessibilityNodeInfo
|
||||
|
||||
@@ -33,6 +37,10 @@ class ControlService : AccessibilityService() {
|
||||
companion object {
|
||||
const val TAG = "YAMA_CTRL"
|
||||
|
||||
// 锁定时注入手势前的放行等待(毫秒):见 sendGesture 的说明。100ms 是实测能
|
||||
// 稳稳让 WMS 应用完 FLAG_NOT_TOUCHABLE 的最小值量级(官方没有回调可等)。
|
||||
private const val OVERLAY_BYPASS_DELAY_MS = 100
|
||||
|
||||
// ── Service instance ─────────────────────────────────────────────
|
||||
@Volatile var instance: ControlService? = null
|
||||
|
||||
@@ -101,6 +109,33 @@ class ControlService : AccessibilityService() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录前探测(C++ 侧据此决定是否声明 CLIENT_CAP_PRIVACY_OVERLAY):隐私 overlay
|
||||
* 用 TYPE_ACCESSIBILITY_OVERLAY 挂在无障碍服务上,服务没在跑就挂不了(我们不申请
|
||||
* SYSTEM_ALERT_WINDOW),API 22 以下没有这个窗口类型。缺任一条都如实不声明。
|
||||
*/
|
||||
@JvmStatic
|
||||
fun canOverlay(): Boolean =
|
||||
instance != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1
|
||||
|
||||
/**
|
||||
* 服务端 COMMAND_SCREEN_BLANK / COMMAND_SCREEN_BLOCK_INPUT 的处理入口。
|
||||
* black=true → 全屏不透明黑(COMMAND_SCREEN_BLANK)
|
||||
* block=true → 透明但吃掉本机触摸(COMMAND_SCREEN_BLOCK_INPUT)
|
||||
* 两个参数都是**绝对状态**不是开关:重复下发幂等,都传 false 就是撤掉 overlay。
|
||||
* 注意 Android 上黑色 overlay 同样会被 MediaProjection 抓到,主控端的画面也会
|
||||
* 变黑——黑屏是给本机旁观的真人看的,不是「主控隐身」。
|
||||
* 调用方是屏幕子连接 OnReceive 线程,WindowManager 的操作必须在主线程。
|
||||
*/
|
||||
@JvmStatic
|
||||
fun setPrivacyOverlay(black: Boolean, block: Boolean) {
|
||||
val svc = instance ?: run {
|
||||
Log.w(TAG, "setPrivacyOverlay: accessibility service inactive, command dropped")
|
||||
return
|
||||
}
|
||||
svc.handler.post { svc.applyPrivacyOverlay(black, block) }
|
||||
}
|
||||
|
||||
// ── Screen geometry (set by CaptureService) ──────────────────────
|
||||
// Server sends coords in encoding space [0, encW) × [0, encH).
|
||||
// We map them to physical screen space before dispatching.
|
||||
@@ -167,6 +202,17 @@ class ControlService : AccessibilityService() {
|
||||
private var tvTouchMoved = false
|
||||
private val TV_TAP_THRESHOLD = 15f // encoding pixels — below this = tap, above = drag
|
||||
|
||||
// ── Privacy overlay(COMMAND_SCREEN_BLANK / COMMAND_SCREEN_BLOCK_INPUT)──
|
||||
// 两种用途共用一个挂在本服务上的 View(TYPE_ACCESSIBILITY_OVERLAY):
|
||||
// 黑屏 → 不透明黑;输入锁 → 透明。两者都要吃掉触摸,因为黑屏之下让真人
|
||||
// 盲点等于让他对着看不见的界面乱按。
|
||||
private var overlayView: View? = null
|
||||
private var overlayParams: WindowManager.LayoutParams? = null
|
||||
private var overlayBlack = false
|
||||
private var overlayConsuming = false
|
||||
// 注入手势期间的临时放行计数(见 sendGesture):>0 时 overlay 置 FLAG_NOT_TOUCHABLE
|
||||
private var overlayBypass = 0
|
||||
|
||||
// ── Left-button drag accumulator ─────────────────────────────────────
|
||||
private var lbuttonDown = false
|
||||
private var gestureStart = 0L
|
||||
@@ -200,10 +246,123 @@ class ControlService : AccessibilityService() {
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
runCatching { unregisterReceiver(screenReceiver) }
|
||||
// 服务被关掉(用户关闭无障碍)时 overlay 必须一起撤掉,否则会留下一个
|
||||
// 没人能解除的全屏黑色/不可触摸窗口把用户锁在外面。
|
||||
removeOverlay()
|
||||
instance = null
|
||||
Log.i(TAG, "ControlService destroyed")
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
// Privacy overlay(主线程)
|
||||
|
||||
private fun applyPrivacyOverlay(black: Boolean, block: Boolean) {
|
||||
overlayBlack = black
|
||||
overlayConsuming = black || block
|
||||
if (!overlayConsuming) {
|
||||
removeOverlay()
|
||||
Log.i(TAG, "privacy overlay: off")
|
||||
return
|
||||
}
|
||||
val v = overlayView ?: createOverlay() ?: return
|
||||
v.setBackgroundColor(if (black) Color.BLACK else Color.TRANSPARENT)
|
||||
syncOverlayTouchability()
|
||||
Log.i(TAG, "privacy overlay: black=$black block=$block")
|
||||
}
|
||||
|
||||
private fun createOverlay(): View? {
|
||||
val wm = getSystemService(Context.WINDOW_SERVICE) as? WindowManager ?: return null
|
||||
val v = View(this)
|
||||
val lp = WindowManager.LayoutParams(
|
||||
WindowManager.LayoutParams.MATCH_PARENT,
|
||||
WindowManager.LayoutParams.MATCH_PARENT,
|
||||
WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
|
||||
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
|
||||
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
|
||||
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS or
|
||||
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
|
||||
PixelFormat.TRANSLUCENT)
|
||||
lp.gravity = Gravity.TOP or Gravity.START
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
|
||||
lp.layoutInDisplayCutoutMode =
|
||||
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
|
||||
v.setBackgroundColor(if (overlayBlack) Color.BLACK else Color.TRANSPARENT)
|
||||
// 吃掉触摸:不给下面的应用(是否可触摸由 FLAG_NOT_TOUCHABLE 控制)
|
||||
v.setOnTouchListener { _, _ -> true }
|
||||
return try {
|
||||
wm.addView(v, lp)
|
||||
overlayView = v
|
||||
overlayParams = lp
|
||||
Log.i(TAG, "privacy overlay added")
|
||||
v
|
||||
} catch (t: Throwable) {
|
||||
Log.e(TAG, "privacy overlay addView failed: ${t.message}")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun removeOverlay() {
|
||||
val v = overlayView ?: return
|
||||
val wm = getSystemService(Context.WINDOW_SERVICE) as? WindowManager
|
||||
runCatching { wm?.removeView(v) }
|
||||
overlayView = null
|
||||
overlayParams = null
|
||||
overlayBlack = false
|
||||
overlayConsuming = false
|
||||
overlayBypass = 0
|
||||
Log.i(TAG, "privacy overlay removed")
|
||||
}
|
||||
|
||||
// 触摸能否穿透由 overlay 上的 FLAG_NOT_TOUCHABLE 决定:
|
||||
// 正常锁定 → 可触摸(吃掉);有注入手势在飞 → 临时不可触摸(放行给下面的应用)。
|
||||
private fun syncOverlayTouchability() {
|
||||
val v = overlayView ?: return
|
||||
val lp = overlayParams ?: return
|
||||
val wm = getSystemService(Context.WINDOW_SERVICE) as? WindowManager ?: return
|
||||
val touchable = overlayConsuming && overlayBypass == 0
|
||||
val flag = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
|
||||
val flags = if (touchable) (lp.flags and flag.inv()) else (lp.flags or flag)
|
||||
if (flags == lp.flags) return
|
||||
lp.flags = flags
|
||||
runCatching { wm.updateViewLayout(v, lp) }
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入手势。锁定键鼠时 overlay 会把本服务自己注入的手势也吃掉——Windows 客户端
|
||||
* 的做法是 ProcessCommand 前后临时 BlockInput(false) 再恢复(client/ScreenManager.cpp
|
||||
* 的 COMMAND_SCREEN_CONTROL 分支),这里等价:手势开始前放行 overlay,结束回调里恢复。
|
||||
* 代价与 Windows 相同:手势在飞的那几十~几百毫秒里,真人触摸可以穿透。
|
||||
*
|
||||
* 放行后必须隔一小段时间再注入:改窗口属性(updateViewLayout)和注入触摸是两条
|
||||
* 异步路径,同一条消息里先改属性再 dispatchGesture,事件几乎必然仍打在被吞的
|
||||
* 那个窗口上(实测必现「Tap ok 但点了没反应」)。
|
||||
*/
|
||||
private fun sendGesture(gesture: GestureDescription,
|
||||
callback: GestureResultCallback? = null): Boolean {
|
||||
if (overlayView == null) return dispatchGesture(gesture, callback, null)
|
||||
overlayBypass++
|
||||
syncOverlayTouchability()
|
||||
handler.postDelayed({
|
||||
val restore = object : GestureResultCallback() {
|
||||
override fun onCompleted(g: GestureDescription?) { releaseBypass(); callback?.onCompleted(g) }
|
||||
override fun onCancelled(g: GestureDescription?) { releaseBypass(); callback?.onCancelled(g) }
|
||||
}
|
||||
val ok = try {
|
||||
dispatchGesture(gesture, restore, null)
|
||||
} catch (t: Throwable) {
|
||||
Log.e(TAG, "dispatchGesture threw: ${t.message}")
|
||||
false
|
||||
}
|
||||
if (!ok) releaseBypass() // 没派出去就不会有回调,立刻恢复,别把 overlay 留在放行状态
|
||||
}, OVERLAY_BYPASS_DELAY_MS.toLong())
|
||||
return true
|
||||
}
|
||||
|
||||
private fun releaseBypass() {
|
||||
if (overlayBypass > 0) overlayBypass--
|
||||
syncOverlayTouchability()
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
// Event routing (runs on main thread)
|
||||
|
||||
@@ -293,14 +452,14 @@ class ControlService : AccessibilityService() {
|
||||
.addStroke(GestureDescription.StrokeDescription(p, 0L, 80L))
|
||||
.addStroke(GestureDescription.StrokeDescription(p, 200L, 80L))
|
||||
.build()
|
||||
dispatchGesture(gesture, null, null)
|
||||
sendGesture(gesture)
|
||||
}
|
||||
|
||||
// ── Right-click → long press (600 ms) ────────────────────────
|
||||
WM_RBUTTONDOWN -> {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return
|
||||
val stroke = GestureDescription.StrokeDescription(singlePointPath(x, y), 0L, 600L)
|
||||
dispatchGesture(GestureDescription.Builder().addStroke(stroke).build(), null, null)
|
||||
sendGesture(GestureDescription.Builder().addStroke(stroke).build())
|
||||
}
|
||||
|
||||
// ── Scroll wheel → vertical swipe ─────────────────────────────
|
||||
@@ -312,7 +471,7 @@ class ControlService : AccessibilityService() {
|
||||
val dy = if (delta > 0) 400f else -400f
|
||||
val path = Path().apply { moveTo(x, y); lineTo(x, y + dy) }
|
||||
val stroke = GestureDescription.StrokeDescription(path, 0L, 300L)
|
||||
dispatchGesture(GestureDescription.Builder().addStroke(stroke).build(), null, null)
|
||||
sendGesture(GestureDescription.Builder().addStroke(stroke).build())
|
||||
CaptureService.requestIdr()
|
||||
}
|
||||
|
||||
@@ -336,13 +495,12 @@ class ControlService : AccessibilityService() {
|
||||
// Minimum 50 ms so Android registers the gesture; use actual elapsed for drags.
|
||||
val duration = maxOf(points.last().first, 50L)
|
||||
val stroke = GestureDescription.StrokeDescription(path, 0L, duration)
|
||||
val ok = dispatchGesture(
|
||||
val ok = sendGesture(
|
||||
GestureDescription.Builder().addStroke(stroke).build(),
|
||||
object : GestureResultCallback() {
|
||||
override fun onCompleted(g: GestureDescription?) { Log.d(TAG, "Gesture ok") }
|
||||
override fun onCancelled(g: GestureDescription?) { Log.w(TAG, "Gesture cancelled") }
|
||||
},
|
||||
null
|
||||
}
|
||||
)
|
||||
if (!ok) Log.e(TAG, "dispatchGesture returned false (service not ready?)")
|
||||
}
|
||||
@@ -512,11 +670,11 @@ class ControlService : AccessibilityService() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return
|
||||
val path = Path().apply { moveTo(x, y); lineTo(x + 1f, y) }
|
||||
val stroke = GestureDescription.StrokeDescription(path, 0L, 100L)
|
||||
val ok = dispatchGesture(GestureDescription.Builder().addStroke(stroke).build(),
|
||||
val ok = sendGesture(GestureDescription.Builder().addStroke(stroke).build(),
|
||||
object : GestureResultCallback() {
|
||||
override fun onCompleted(g: GestureDescription?) { Log.d(TAG, "Tap ok") }
|
||||
override fun onCancelled(g: GestureDescription?) { Log.w(TAG, "Tap cancelled") }
|
||||
}, null)
|
||||
})
|
||||
if (!ok) Log.e(TAG, "dispatchTap returned false")
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user