1) 锁屏时活动窗口与 Windows/macOS 对齐:由裸 "Locked" 改为 "Locked: HH:MM:SS"(已锁时长)。起点取 ACTION_SCREEN_OFF,解锁由 ACTION_USER_PRESENT 与 KeyguardManager 双重判定;历史活动仍上报裸 "Locked" 当空闲处理,不记录。 2) 转移主机的提示/日志不再出现大整数:到期时刻格式化为 "YYYY-MM-DD HH:MM:SS +ZZZZ",剩余有效期格式化为 "2h 05m"/"15m 03s"。 原 "Host is already transferred until 1763..., ignored" 现在是 "Host is already transferred until 2026-11-17 21:15:12 +0100 (2h 00m left), ignored"。 验证:arm64/armeabi 编译通过;模拟器开无障碍后锁屏,服务端 list_hosts 收到 "Locked: 00:00:06",解锁后恢复 "Android";格式化函数单独跑过 2h/15m/42s/负数用例。 Model: deepseek-flash Co-Authored-By: Claude Code <noreply@anthropic.com>
724 lines
35 KiB
Kotlin
724 lines
35 KiB
Kotlin
package com.yama.client
|
||
|
||
import android.accessibilityservice.AccessibilityService
|
||
import android.accessibilityservice.GestureDescription
|
||
import android.annotation.SuppressLint
|
||
import android.app.KeyguardManager
|
||
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
|
||
import java.util.Locale
|
||
|
||
/**
|
||
* AccessibilityService that injects touch gestures and global key actions
|
||
* dispatched by the server (COMMAND_SCREEN_CONTROL / MSG64).
|
||
*
|
||
* Requires API 24+ for dispatchGesture(); guarded at runtime so the APK
|
||
* installs on API 21+ but gesture injection only runs on 24+.
|
||
*
|
||
* Activation: Settings → Accessibility → YAMA → enable.
|
||
*/
|
||
@SuppressLint("NewApi")
|
||
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
|
||
|
||
// ── Active foreground window ─────────────────────────────────────
|
||
// 服务端心跳读的是这个属性的 Java getter(C++ 按 getActiveWindow 查找)。
|
||
// 锁屏时与其他平台对齐返回 "Locked: HH:MM:SS"(已锁时长),否则返回前台包名。
|
||
@Volatile private var foregroundPkg: String = "Android"
|
||
@Volatile private var lockedAtElapsed: Long = 0L
|
||
|
||
@get:JvmStatic
|
||
val activeWindow: String
|
||
get() {
|
||
val svc = instance
|
||
val now = SystemClock.elapsedRealtime()
|
||
val locked = lockedAtElapsed != 0L || (svc?.isKeyguardLockedNow() == true)
|
||
if (locked) {
|
||
if (lockedAtElapsed == 0L) lockedAtElapsed = now
|
||
return "Locked: " + formatElapsed(now - lockedAtElapsed)
|
||
}
|
||
lockedAtElapsed = 0L
|
||
return foregroundPkg
|
||
}
|
||
|
||
// 锁屏时长,格式与 Windows/macOS 的 "Locked: HH:MM:SS" 一致
|
||
private fun formatElapsed(ms: Long): String {
|
||
val total = (ms / 1000).coerceAtLeast(0)
|
||
return String.format(Locale.US, "%02d:%02d:%02d",
|
||
total / 3600, (total % 3600) / 60, total % 60)
|
||
}
|
||
|
||
// ── 历史活动采样用的前台应用标题(C++ ActivityHistory 的 probe 每秒调一次)──
|
||
// 辅助功能服务没启用时返回 null,让「历史活动」报告能说清是缺权限,而不是
|
||
// 记一堆默认值 "Android"。包名 → 应用名只查一次(缓存)。
|
||
private val labelCache = HashMap<String, String>()
|
||
|
||
@JvmStatic
|
||
fun currentActivityTitle(): String? {
|
||
val svc = instance ?: return null
|
||
// 历史活动里锁屏仍按空闲处理(C++ 侧只认裸 "Locked"),不要带时长
|
||
if (lockedAtElapsed != 0L || svc.isKeyguardLockedNow()) return "Locked"
|
||
val pkg = foregroundPkg
|
||
return when {
|
||
pkg.isEmpty() -> null
|
||
pkg == "Android" -> svc.getString(R.string.activity_home)
|
||
else -> synchronized(labelCache) {
|
||
labelCache.getOrPut(pkg) { describeApp(svc, pkg) }
|
||
}
|
||
}
|
||
}
|
||
|
||
// 应用名 + 包名(应用名可能重名,包名才能唯一定位)。
|
||
// 非 BMP 字符经 JNI 变成 CESU-8,服务端按 UTF-8 解析会拿到非法序列
|
||
// (Json 序列化直接抛异常),所以只保留 BMP 字符。
|
||
private fun describeApp(svc: ControlService, pkg: String): String {
|
||
val label = runCatching {
|
||
svc.packageManager.getApplicationLabel(
|
||
svc.packageManager.getApplicationInfo(pkg, 0)).toString()
|
||
}.getOrDefault(pkg)
|
||
val text = if (label == pkg) pkg else "$label ($pkg)"
|
||
return text.filterNot { it in '\uD800'..'\uDFFF' }
|
||
}
|
||
|
||
/**
|
||
* 登录前探测(C++ 侧据此决定是否声明 CLIENT_CAP_LOCK_SCREEN):锁屏依赖无障碍
|
||
* 服务在跑(performGlobalAction)与 API 28+,两者缺一就如实不声明,免得服务端
|
||
* 把「命令已下发」当成「已锁上」。
|
||
*/
|
||
@JvmStatic
|
||
fun canLockScreen(): Boolean =
|
||
instance != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
|
||
|
||
/**
|
||
* 服务端 COMMAND_SCREEN_CTRL_ALT_DEL:Android 没有 Ctrl+Alt+Del 这个组合键,
|
||
* 客户端映射成「锁屏」(GLOBAL_ACTION_LOCK_SCREEN,API 28+)。调用方是屏幕
|
||
* 子连接的 OnReceive 线程,performGlobalAction 必须在主线程,故 post 过去。
|
||
*/
|
||
@JvmStatic
|
||
fun lockScreen() {
|
||
val svc = instance ?: run {
|
||
Log.w(TAG, "lockScreen: accessibility service inactive, command dropped")
|
||
return
|
||
}
|
||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
|
||
Log.w(TAG, "lockScreen: GLOBAL_ACTION_LOCK_SCREEN needs API 28+")
|
||
return
|
||
}
|
||
svc.handler.post {
|
||
if (!svc.performGlobalAction(GLOBAL_ACTION_LOCK_SCREEN))
|
||
Log.w(TAG, "lockScreen: performGlobalAction rejected by the system")
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 登录前探测(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.
|
||
@Volatile var physW = 1080
|
||
@Volatile var physH = 1920
|
||
@Volatile var encW = 1080
|
||
@Volatile var encH = 1920
|
||
|
||
// ── Windows message constants ────────────────────────────────────
|
||
const val WM_MOUSEMOVE = 0x0200
|
||
const val WM_LBUTTONDOWN = 0x0201
|
||
const val WM_LBUTTONUP = 0x0202
|
||
const val WM_LBUTTONDBLCLK = 0x0203
|
||
const val WM_RBUTTONDOWN = 0x0204
|
||
const val WM_MBUTTONDOWN = 0x0207
|
||
const val WM_MOUSEWHEEL = 0x020A
|
||
const val WM_KEYDOWN = 0x0100
|
||
const val WM_SYSKEYDOWN = 0x0104
|
||
|
||
/**
|
||
* Called from C++ DataProcess() via JNI on the IO thread.
|
||
* Parameters:
|
||
* message – Windows WM_* constant (low 16 bits of MSG64.message)
|
||
* wParam – MSG64.wParam (key/button flags; high word of MOUSEWHEEL = delta)
|
||
* ptX/ptY – MSG64.pt in encoding-space pixels
|
||
*/
|
||
@JvmStatic
|
||
fun onControlEvent(message: Int, wParam: Long, ptX: Int, ptY: Int) {
|
||
val svc = instance ?: run {
|
||
Log.w(TAG, "ControlService inactive — event 0x${message.toString(16)} dropped")
|
||
return
|
||
}
|
||
// All gesture state lives on the main thread; post there.
|
||
svc.handler.post { svc.handleEvent(message, wParam, ptX, ptY) }
|
||
}
|
||
}
|
||
|
||
// ── Screen state receiver ─────────────────────────────────────────────
|
||
private val screenReceiver = object : BroadcastReceiver() {
|
||
override fun onReceive(context: Context?, intent: Intent?) {
|
||
when (intent?.action) {
|
||
// 只记录起点;上报时由 activeWindow 的 getter 算 "Locked: HH:MM:SS"
|
||
Intent.ACTION_SCREEN_OFF -> lockedAtElapsed = SystemClock.elapsedRealtime()
|
||
Intent.ACTION_USER_PRESENT -> {
|
||
lockedAtElapsed = 0L
|
||
foregroundPkg = "Android"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 锁屏判断:KeyguardManager 在「屏幕亮着但未解锁」时也返回 true;
|
||
// 少数机型息屏时它返回 false,所以调用方还会叠加 lockedAtElapsed。
|
||
private fun isKeyguardLockedNow(): Boolean {
|
||
val km = getSystemService(Context.KEYGUARD_SERVICE) as? KeyguardManager ?: return false
|
||
return km.isKeyguardLocked
|
||
}
|
||
|
||
// ── Main-thread handler ───────────────────────────────────────────────
|
||
private val handler = Handler(Looper.getMainLooper())
|
||
|
||
// ── TV (Google TV / Android TV) D-pad mode ───────────────────────────
|
||
// Activated when FEATURE_LEANBACK is present. Mouse events are translated
|
||
// to accessibility focus navigation — no touchscreen or root required.
|
||
private var isTV = false
|
||
private var tvLastEncX = -1f
|
||
private var tvLastEncY = -1f
|
||
private var tvAccumDX = 0f
|
||
private var tvAccumDY = 0f
|
||
private val TV_STEP = 40f // encoding-space pixels per D-pad step
|
||
// Touch-drag disambiguation: fire click on LBUTTONUP only if finger barely moved
|
||
private var tvTouchDown = false
|
||
private var tvTouchDownX = 0f
|
||
private var tvTouchDownY = 0f
|
||
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
|
||
// Pairs of (millisecond offset from gesture start, screen point)
|
||
private val strokePoints = mutableListOf<Pair<Long, PointF>>()
|
||
|
||
// ─────────────────────────────────────────────────────────────────────
|
||
// AccessibilityService lifecycle
|
||
|
||
override fun onServiceConnected() {
|
||
super.onServiceConnected()
|
||
instance = this
|
||
isTV = packageManager.hasSystemFeature("android.software.leanback")
|
||
if (isTV) Log.i(TAG, "TV mode: D-pad navigation enabled")
|
||
val filter = IntentFilter().apply {
|
||
addAction(Intent.ACTION_SCREEN_OFF)
|
||
addAction(Intent.ACTION_USER_PRESENT)
|
||
}
|
||
registerReceiver(screenReceiver, filter)
|
||
Log.i(TAG, "ControlService connected phys=${physW}x${physH} enc=${encW}x${encH}")
|
||
}
|
||
|
||
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
|
||
if (event?.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
|
||
val pkg = event.packageName?.toString()
|
||
if (!pkg.isNullOrEmpty()) {
|
||
// 只更新前台包名;解锁由 USER_PRESENT 与 activeWindow 的 getter 负责,
|
||
// 避免「息屏时 KeyguardManager 误报未锁」把锁定起点清掉
|
||
foregroundPkg = pkg
|
||
}
|
||
}
|
||
}
|
||
override fun onInterrupt() {}
|
||
|
||
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)
|
||
|
||
private fun handleEvent(message: Int, wParam: Long, ptX: Int, ptY: Int) {
|
||
if (isTV) { handleEventTV(message, wParam, ptX, ptY); return }
|
||
// Scale encoding-space coords to physical screen
|
||
val x = if (encW > 0) ptX.toFloat() * physW / encW else ptX.toFloat()
|
||
val y = if (encH > 0) ptY.toFloat() * physH / encH else ptY.toFloat()
|
||
if (message != WM_MOUSEMOVE)
|
||
Log.d(TAG, "event 0x${message.toString(16)} enc=($ptX,$ptY) phys=(${x.toInt()},${y.toInt()})")
|
||
|
||
when (message) {
|
||
|
||
// ── Left button: accumulate drag path, dispatch on up ─────────
|
||
WM_LBUTTONDOWN -> {
|
||
lbuttonDown = true
|
||
gestureStart = SystemClock.uptimeMillis()
|
||
strokePoints.clear()
|
||
strokePoints.add(0L to PointF(x, y))
|
||
}
|
||
WM_MOUSEMOVE -> {
|
||
if (lbuttonDown) {
|
||
strokePoints.add((SystemClock.uptimeMillis() - gestureStart) to PointF(x, y))
|
||
}
|
||
}
|
||
WM_LBUTTONUP -> {
|
||
if (!lbuttonDown) return
|
||
lbuttonDown = false
|
||
val elapsed = SystemClock.uptimeMillis() - gestureStart
|
||
strokePoints.add(elapsed to PointF(x, y))
|
||
val down = strokePoints[0].second
|
||
val dx = x - down.x
|
||
val dy = y - down.y
|
||
|
||
// ── System edge-gesture detection ────────────────────────────
|
||
// dispatchGesture() is unreliable for system gestures; use performGlobalAction.
|
||
val hEdge = physH * 0.08f // 8% of screen height = top/bottom trigger zone
|
||
val wEdge = physW * 0.08f // 8% of screen width = left/right trigger zone
|
||
val minH = physH * 0.15f // minimum vertical travel
|
||
val minW = physW * 0.20f // minimum horizontal travel
|
||
|
||
val fromLeft = down.x < wEdge && dx > minW && Math.abs(dx) > Math.abs(dy) * 1.5f && elapsed < 600L
|
||
val fromRight = down.x > physW - wEdge && dx < -minW && Math.abs(dx) > Math.abs(dy) * 1.5f && elapsed < 600L
|
||
val fromTop = down.y < hEdge && dy > minH && Math.abs(dy) > Math.abs(dx)
|
||
val fromBottom = down.y > physH - hEdge && dy < -minH && Math.abs(dy) > Math.abs(dx)
|
||
|
||
when {
|
||
fromLeft || fromRight -> {
|
||
Log.d(TAG, "Edge-swipe back (fromLeft=$fromLeft)")
|
||
performGlobalAction(GLOBAL_ACTION_BACK)
|
||
}
|
||
fromTop -> {
|
||
// Right half of screen → quick settings; left half → notifications
|
||
if (down.x > physW * 0.5f) {
|
||
Log.d(TAG, "Top-swipe quick settings")
|
||
performGlobalAction(GLOBAL_ACTION_QUICK_SETTINGS)
|
||
} else {
|
||
Log.d(TAG, "Top-swipe notifications")
|
||
performGlobalAction(GLOBAL_ACTION_NOTIFICATIONS)
|
||
}
|
||
}
|
||
fromBottom -> {
|
||
// Slow swipe (≥400 ms) or very long travel → Recents; quick swipe → Home
|
||
if (elapsed >= 400L || Math.abs(dy) > physH * 0.40f) {
|
||
Log.d(TAG, "Bottom-swipe recents (elapsed=${elapsed}ms)")
|
||
performGlobalAction(GLOBAL_ACTION_RECENTS)
|
||
} else {
|
||
Log.d(TAG, "Bottom-swipe home (elapsed=${elapsed}ms)")
|
||
performGlobalAction(GLOBAL_ACTION_HOME)
|
||
}
|
||
}
|
||
dx * dx + dy * dy < 100f -> // 移动 < 10px → 单击
|
||
dispatchTap(down.x, down.y)
|
||
else ->
|
||
dispatchTouchGesture(strokePoints)
|
||
}
|
||
// 手势执行后立即请求关键帧,让 decoder 尽快看到屏幕变化
|
||
CaptureService.requestIdr()
|
||
strokePoints.clear()
|
||
}
|
||
|
||
// ── Double-click → two rapid taps ────────────────────────────
|
||
WM_LBUTTONDBLCLK -> {
|
||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return
|
||
val p = singlePointPath(x, y)
|
||
val gesture = GestureDescription.Builder()
|
||
.addStroke(GestureDescription.StrokeDescription(p, 0L, 80L))
|
||
.addStroke(GestureDescription.StrokeDescription(p, 200L, 80L))
|
||
.build()
|
||
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)
|
||
sendGesture(GestureDescription.Builder().addStroke(stroke).build())
|
||
}
|
||
|
||
// ── Scroll wheel → vertical swipe ─────────────────────────────
|
||
WM_MOUSEWHEEL -> {
|
||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return
|
||
// High 16 bits of wParam hold the signed delta (+120 = up, -120 = down)
|
||
val delta = (wParam.toInt() ushr 16).toShort().toInt()
|
||
// Wheel-up (+delta) = user wants content above = finger swipes DOWN (+y)
|
||
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)
|
||
sendGesture(GestureDescription.Builder().addStroke(stroke).build())
|
||
CaptureService.requestIdr()
|
||
}
|
||
|
||
// ── Keyboard → global actions only ───────────────────────────
|
||
WM_KEYDOWN, WM_SYSKEYDOWN -> handleKeyDown(wParam.toInt() and 0xFFFF)
|
||
}
|
||
}
|
||
|
||
// ─────────────────────────────────────────────────────────────────────
|
||
// Helpers
|
||
|
||
private fun dispatchTouchGesture(points: List<Pair<Long, PointF>>) {
|
||
if (points.isEmpty()) return
|
||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return
|
||
|
||
val path = Path()
|
||
path.moveTo(points[0].second.x, points[0].second.y)
|
||
for (i in 1 until points.size) {
|
||
path.lineTo(points[i].second.x, points[i].second.y)
|
||
}
|
||
// 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 = 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") }
|
||
}
|
||
)
|
||
if (!ok) Log.e(TAG, "dispatchGesture returned false (service not ready?)")
|
||
}
|
||
|
||
// ─────────────────────────────────────────────────────────────────────
|
||
// TV D-pad mode — translates mouse events to accessibility focus actions
|
||
|
||
private fun handleEventTV(message: Int, wParam: Long, ptX: Int, ptY: Int) {
|
||
when (message) {
|
||
WM_MOUSEMOVE -> {
|
||
if (tvLastEncX >= 0) {
|
||
tvAccumDX += ptX - tvLastEncX
|
||
tvAccumDY += ptY - tvLastEncY
|
||
while (tvAccumDX > TV_STEP) { tvMoveFocus(View.FOCUS_RIGHT); tvAccumDX -= TV_STEP }
|
||
while (tvAccumDX < -TV_STEP) { tvMoveFocus(View.FOCUS_LEFT); tvAccumDX += TV_STEP }
|
||
while (tvAccumDY > TV_STEP) { tvMoveFocus(View.FOCUS_DOWN); tvAccumDY -= TV_STEP }
|
||
while (tvAccumDY < -TV_STEP) { tvMoveFocus(View.FOCUS_UP); tvAccumDY += TV_STEP }
|
||
}
|
||
if (tvTouchDown) {
|
||
val dx = ptX - tvTouchDownX
|
||
val dy = ptY - tvTouchDownY
|
||
if (dx * dx + dy * dy > TV_TAP_THRESHOLD * TV_TAP_THRESHOLD) tvTouchMoved = true
|
||
}
|
||
tvLastEncX = ptX.toFloat()
|
||
tvLastEncY = ptY.toFloat()
|
||
}
|
||
WM_LBUTTONDOWN -> {
|
||
tvTouchDown = true
|
||
tvTouchDownX = ptX.toFloat()
|
||
tvTouchDownY = ptY.toFloat()
|
||
tvTouchMoved = false
|
||
tvAccumDX = 0f
|
||
tvAccumDY = 0f
|
||
tvLastEncX = ptX.toFloat()
|
||
tvLastEncY = ptY.toFloat()
|
||
}
|
||
WM_LBUTTONUP -> {
|
||
if (tvTouchDown && !tvTouchMoved) tvClick()
|
||
tvTouchDown = false
|
||
}
|
||
WM_LBUTTONDBLCLK -> tvClick()
|
||
WM_RBUTTONDOWN -> performGlobalAction(GLOBAL_ACTION_BACK)
|
||
WM_MOUSEWHEEL -> {
|
||
val delta = (wParam.toInt() ushr 16).toShort().toInt()
|
||
tvScroll(delta < 0)
|
||
}
|
||
WM_KEYDOWN, WM_SYSKEYDOWN -> {
|
||
val vk = wParam.toInt() and 0xFFFF
|
||
when (vk) {
|
||
0x25 -> tvMoveFocus(View.FOCUS_LEFT) // VK_LEFT
|
||
0x26 -> tvMoveFocus(View.FOCUS_UP) // VK_UP
|
||
0x27 -> tvMoveFocus(View.FOCUS_RIGHT) // VK_RIGHT
|
||
0x28 -> tvMoveFocus(View.FOCUS_DOWN) // VK_DOWN
|
||
0x0D -> tvClick() // VK_RETURN
|
||
else -> handleKeyDown(vk)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
@Suppress("DEPRECATION")
|
||
private fun tvMoveFocus(direction: Int) {
|
||
val root = rootInActiveWindow ?: return
|
||
val focused = root.findFocus(AccessibilityNodeInfo.FOCUS_INPUT)
|
||
?: root.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY)
|
||
if (focused == null) {
|
||
val first = findFirstFocusable(root)
|
||
first?.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
|
||
first?.recycle()
|
||
} else {
|
||
val next = focused.focusSearch(direction)
|
||
next?.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
|
||
focused.recycle()
|
||
next?.recycle()
|
||
}
|
||
root.recycle()
|
||
}
|
||
|
||
@Suppress("DEPRECATION")
|
||
private fun tvClick() {
|
||
val root = rootInActiveWindow ?: return
|
||
val focused = root.findFocus(AccessibilityNodeInfo.FOCUS_INPUT)
|
||
?: root.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY)
|
||
if (focused != null) {
|
||
focused.performAction(AccessibilityNodeInfo.ACTION_CLICK)
|
||
focused.recycle()
|
||
} else {
|
||
Log.w(TAG, "tvClick: no focused node")
|
||
}
|
||
root.recycle()
|
||
}
|
||
|
||
@Suppress("DEPRECATION")
|
||
private fun tvScroll(forward: Boolean) {
|
||
val root = rootInActiveWindow ?: return
|
||
val action = if (forward) AccessibilityNodeInfo.ACTION_SCROLL_FORWARD
|
||
else AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD
|
||
val focused = root.findFocus(AccessibilityNodeInfo.FOCUS_INPUT)
|
||
?: root.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY)
|
||
if (focused == null || !focused.performAction(action)) root.performAction(action)
|
||
focused?.recycle()
|
||
root.recycle()
|
||
}
|
||
|
||
// Walk accessibility tree depth-first, return first visible focusable node (caller must recycle)
|
||
@Suppress("DEPRECATION")
|
||
private fun findFirstFocusable(node: AccessibilityNodeInfo): AccessibilityNodeInfo? {
|
||
if (node.isFocusable && node.isVisibleToUser) return AccessibilityNodeInfo.obtain(node)
|
||
for (i in 0 until node.childCount) {
|
||
val child = node.getChild(i) ?: continue
|
||
val hit = findFirstFocusable(child)
|
||
child.recycle()
|
||
if (hit != null) return hit
|
||
}
|
||
return null
|
||
}
|
||
|
||
private fun handleKeyDown(vk: Int) {
|
||
when (vk) {
|
||
0x08, 0x1B -> performGlobalAction(GLOBAL_ACTION_BACK) // Backspace / Esc
|
||
0x24 -> performGlobalAction(GLOBAL_ACTION_HOME) // VK_HOME
|
||
0x5D -> performGlobalAction(GLOBAL_ACTION_RECENTS) // VK_APPS
|
||
0x2C -> performGlobalAction(GLOBAL_ACTION_TAKE_SCREENSHOT) // PrtSc
|
||
0x56 -> pasteIntoFocused() // VK_V → 粘贴
|
||
}
|
||
}
|
||
|
||
// 远程剪贴板的粘贴动作。Android 没有 Ctrl+V 组合键,而且本客户端不注入文本,
|
||
// 所以把 VK_V 映射成输入框的 ACTION_PASTE:由目标应用自己读剪贴板,
|
||
// 不受 Android 10+「后台应用不能读剪贴板」的限制。
|
||
@Suppress("DEPRECATION")
|
||
private fun pasteIntoFocused() {
|
||
val root = rootInActiveWindow ?: run {
|
||
Log.w(TAG, "paste: no active window")
|
||
return
|
||
}
|
||
// 焦点链:输入焦点 → 无障碍焦点 → 整棵树里第一个可编辑节点。部分应用只置
|
||
// FOCUS_ACCESSIBILITY;也不拿 isEditable 当门槛(WebView / 自绘输入框常常不置位
|
||
// 但照样响应 ACTION_PASTE),真正的判据是 performAction 的返回值。
|
||
val target = root.findFocus(AccessibilityNodeInfo.FOCUS_INPUT)
|
||
?: root.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY)
|
||
?: findFirstEditable(root)
|
||
if (target == null) {
|
||
Log.w(TAG, "paste: no focused or editable node")
|
||
} else {
|
||
if (!target.performAction(AccessibilityNodeInfo.ACTION_PASTE))
|
||
Log.w(TAG, "paste: ACTION_PASTE rejected by ${target.packageName}")
|
||
target.recycle()
|
||
}
|
||
root.recycle()
|
||
}
|
||
|
||
// 找不到焦点时的兜底:返回整棵树里第一个可见的可编辑节点(调用方负责 recycle)
|
||
@Suppress("DEPRECATION")
|
||
private fun findFirstEditable(node: AccessibilityNodeInfo): AccessibilityNodeInfo? {
|
||
if (node.isEditable && node.isVisibleToUser) return AccessibilityNodeInfo.obtain(node)
|
||
for (i in 0 until node.childCount) {
|
||
val child = node.getChild(i) ?: continue
|
||
val hit = findFirstEditable(child)
|
||
child.recycle()
|
||
if (hit != null) return hit
|
||
}
|
||
return null
|
||
}
|
||
|
||
private fun dispatchTap(x: Float, y: Float) {
|
||
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 = 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") }
|
||
})
|
||
if (!ok) Log.e(TAG, "dispatchTap returned false")
|
||
}
|
||
|
||
private fun singlePointPath(x: Float, y: Float) = Path().apply { moveTo(x, y); lineTo(x + 1f, y) }
|
||
}
|