Fix(android): humanize lock status and transfer messages
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>
This commit is contained in:
1 parent
b7f87c958a
commit
db148c4a69
2 files changed
+99
-18
No files matched your search
@@ -3,6 +3,7 @@ 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
|
||||
@@ -21,6 +22,7 @@ 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
|
||||
@@ -45,8 +47,31 @@ class ControlService : AccessibilityService() {
|
||||
@Volatile var instance: ControlService? = null
|
||||
|
||||
// ── Active foreground window ─────────────────────────────────────
|
||||
@JvmStatic
|
||||
@Volatile var activeWindow: String = "Android"
|
||||
// 服务端心跳读的是这个属性的 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,让「历史活动」报告能说清是缺权限,而不是
|
||||
@@ -56,10 +81,11 @@ class ControlService : AccessibilityService() {
|
||||
@JvmStatic
|
||||
fun currentActivityTitle(): String? {
|
||||
val svc = instance ?: return null
|
||||
val pkg = activeWindow
|
||||
// 历史活动里锁屏仍按空闲处理(C++ 侧只认裸 "Locked"),不要带时长
|
||||
if (lockedAtElapsed != 0L || svc.isKeyguardLockedNow()) return "Locked"
|
||||
val pkg = foregroundPkg
|
||||
return when {
|
||||
pkg.isEmpty() -> null
|
||||
pkg == "Locked" -> pkg // 锁屏/息屏:C++ 侧当空闲处理,不记录
|
||||
pkg == "Android" -> svc.getString(R.string.activity_home)
|
||||
else -> synchronized(labelCache) {
|
||||
labelCache.getOrPut(pkg) { describeApp(svc, pkg) }
|
||||
@@ -177,12 +203,23 @@ class ControlService : AccessibilityService() {
|
||||
private val screenReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
when (intent?.action) {
|
||||
Intent.ACTION_SCREEN_OFF -> activeWindow = "Locked"
|
||||
Intent.ACTION_USER_PRESENT -> activeWindow = "Android"
|
||||
// 只记录起点;上报时由 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())
|
||||
|
||||
@@ -238,7 +275,11 @@ class ControlService : AccessibilityService() {
|
||||
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
|
||||
if (event?.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
|
||||
val pkg = event.packageName?.toString()
|
||||
if (!pkg.isNullOrEmpty()) activeWindow = pkg
|
||||
if (!pkg.isNullOrEmpty()) {
|
||||
// 只更新前台包名;解锁由 USER_PRESENT 与 activeWindow 的 getter 负责,
|
||||
// 避免「息屏时 KeyguardManager 误报未锁」把锁定起点清掉
|
||||
foregroundPkg = pkg
|
||||
}
|
||||
}
|
||||
}
|
||||
override fun onInterrupt() {}
|
||||
|
||||
Reference in new issue
Block a user