Feature: Add activity history to the Android client

get_activity_history was gated to Windows and Linux. The Android client
now answers COMMAND_QUERY_ACTIVITY with a TOKEN_REPORT_ACTIVITY snapshot
on the main connection, the same RPC shape as the other platforms; the
server takes the first packet without CancelIO, so no sub-connection or
request correlation is involved.

The sampler reuses linux/LinuxActivityHistory.h, whose ActivityHistory
class and formatters are platform-neutral, and is fed by a probe that
asks the new ControlService.currentActivityTitle() once per second over
JNI. The accessibility service already tracks the foreground package
from TYPE_WINDOW_STATE_CHANGED; the title is "label (package)" with the
label resolved once and cached, "Home" for the launcher, and idle for
lock or screen-off, so no permission beyond the service users already
enable is needed. Without the service the report says so instead of
recording placeholder "Android" entries.

Also scope the connection teardown to its own generation: a
ConnectionThread that exits late (CaptureService restart) no longer
clears the new run's g_running or g_pMainClient, and the report thread
takes one local snapshot of the main client pointer.

Verified on the emulator against a local debug server: five records
(Settings, Chrome, launcher, Home) came back newest-first with dwell
times, and disabling accessibility produced the permission hint.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 13:14:46 +01:00
1 parent d365a4f948
commit 5ed1a07dc4
4 files changed
+158 -9

No files matched your search

@@ -40,6 +40,37 @@ class ControlService : AccessibilityService() {
@JvmStatic
@Volatile var activeWindow: String = "Android"
// ── 历史活动采样用的前台应用标题(C++ ActivityHistory 的 probe 每秒调一次)──
// 辅助功能服务没启用时返回 null,让「历史活动」报告能说清是缺权限,而不是
// 记一堆默认值 "Android"。包名 → 应用名只查一次(缓存)。
private val labelCache = HashMap<String, String>()
@JvmStatic
fun currentActivityTitle(): String? {
val svc = instance ?: return null
val pkg = activeWindow
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) }
}
}
}
// 应用名 + 包名(应用名可能重名,包名才能唯一定位)。
// 非 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' }
}
// ── Screen geometry (set by CaptureService) ──────────────────────
// Server sends coords in encoding space [0, encW) × [0, encH).
// We map them to physical screen space before dispatching.