From 5ed1a07dc4bf254f137c59460078dc22d145a5e8 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Wed, 11 Nov 2026 13:14:46 +0100 Subject: [PATCH] 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 --- android/app/src/main/cpp/main.cpp | 124 +++++++++++++++++- .../java/com/yama/client/ControlService.kt | 31 +++++ android/app/src/main/res/values/strings.xml | 2 + server/2015Remote/McpServer.cpp | 10 +- 4 files changed, 158 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/cpp/main.cpp b/android/app/src/main/cpp/main.cpp index 92d54a4..d8c1e45 100644 --- a/android/app/src/main/cpp/main.cpp +++ b/android/app/src/main/cpp/main.cpp @@ -28,6 +28,10 @@ #include "common/xxhash.h" #include "common/logger.h" #include "common/ClientLogHandler.h" +// 历史活动:复用 Linux 端的采样机。linux/LinuxActivityHistory.h 里的 ActivityHistory +// 类与格式化函数都是平台无关的(X11 采样在 linux/main.cpp,不在头文件里),Android +// 只需注入自己的 probe(JNI 问 ControlService 要前台应用)。 +#include "linux/LinuxActivityHistory.h" #define LOG_TAG "YAMA" // LOGI/LOGE 走 android_log_tag(logger.h):既进 logcat,也进 Logger 的内存 ring, @@ -214,6 +218,7 @@ static JavaVM* g_jvm = nullptr; static jclass g_ctrlClass = nullptr; static jmethodID g_ctrlMethod = nullptr; static jmethodID g_getActiveWindowMid = nullptr; +static jmethodID g_activityTitleMid = nullptr; // ControlService.currentActivityTitle() // CaptureService.requestIdr() / forceFirstFrame() — ScreenSpyThread 子连接建立后触发 static jclass g_captureClass = nullptr; static jmethodID g_requestIdrMid = nullptr; @@ -278,6 +283,53 @@ static std::string GetActiveWindowFromJava() return result; } +// ---- 历史活动采样 ---- +// ControlService.currentActivityTitle():null = 无障碍服务没启用(或 JNI 未就绪), +// "Locked" = 锁屏/息屏,其余是「应用名 (包名)」或桌面。 +static bool GetActivityTitleFromJava(std::string& out) +{ + out.clear(); + if (!g_jvm || !g_ctrlClass || !g_activityTitleMid) return false; + JNIEnv* jenv = nullptr; + bool attached = false; + jint st = g_jvm->GetEnv((void**)&jenv, JNI_VERSION_1_6); + if (st == JNI_EDETACHED) { + if (g_jvm->AttachCurrentThread(&jenv, nullptr) != JNI_OK || !jenv) return false; + attached = true; + } else if (st != JNI_OK || !jenv) { + return false; + } + jstring js = (jstring)jenv->CallStaticObjectMethod(g_ctrlClass, g_activityTitleMid); + bool ok = false; + if (js) { + if (!jenv->ExceptionCheck()) { + const char* c = jenv->GetStringUTFChars(js, nullptr); + if (c) { out = c; jenv->ReleaseStringUTFChars(js, c); ok = true; } + } + jenv->DeleteLocalRef(js); + } + if (jenv->ExceptionCheck()) jenv->ExceptionClear(); + if (attached) g_jvm->DetachCurrentThread(); + return ok; +} + +// 最近一次成功问到前台应用的时间:决定「历史活动」空报告里给的是权限提示还是 +// 「暂无记录」。用时间而不是布尔:无障碍服务被关掉后要能重新给出权限提示。 +static std::atomic g_lastActivityProbeOkMs{0}; + +// ActivityHistory 采样线程每秒调一次;winId=0 表示空闲(锁屏、服务未启用、取不到标题)。 +static LinuxActivity::ProbeResult ProbeActivityFromJava() +{ + LinuxActivity::ProbeResult r; + std::string title; + if (!GetActivityTitleFromJava(title)) return r; + g_lastActivityProbeOkMs.store(GetUnixMs(), std::memory_order_relaxed); + if (title.empty() || title == "Locked") return r; + r.winId = (unsigned long)XXH64(title.data(), title.size(), 0); + r.title = std::move(title); + return r; +} + // 服务器连接参数 static std::string g_serverIp; static int g_serverPort = 443; @@ -515,6 +567,37 @@ static void ClientLogThread() LOGI("ClientLogThread exit"); } +// ------------------------------------------------------------------ 历史活动上报 +// 主连接一次性快照(与 Windows/Linux 一致,服务端 TOKEN_REPORT_ACTIVITY 分支不 CancelIO)。 +// 采样机每秒问一次 ControlService 的前台应用;这里只取 Dump(),几十微秒的活。 +// 同一时刻只跑一份:服务端每主机单飞行,连点也只是重复请求。 +static std::atomic g_activityReportsInFlight(0); + +static void ActivityReportThread() +{ + const uint64_t lastOk = g_lastActivityProbeOkMs.load(std::memory_order_relaxed); + const bool svcOn = lastOk != 0 && GetUnixMs() - lastOk < 5000; // 采样线程每秒跑一次 + std::string text = LinuxActivity::ActivityHistory::Instance().Dump(); + if (text.empty()) { + text = svcOn + ? std::string(u8"[来源] 前台应用活动:暂无记录(没有停留超过 5 秒的前台应用)\n") + : std::string(u8"[来源] 需要开启无障碍服务(设置 → 无障碍 → YAMA)才能采集前台应用\n"); + } + + std::vector pkt(1 + text.size()); + pkt[0] = TOKEN_REPORT_ACTIVITY; + memcpy(pkt.data() + 1, text.data(), text.size()); + // 指针只取一次快照(与 sendClientMsg 同一套路):主连接随时可能被 ConnectionThread + // 收掉,反复解引用全局指针等于多一次悬空窗口 + IOCPClient* mc = g_pMainClient; + bool sent = false; + if (mc && mc->IsConnected() && g_bExit == S_CLIENT_NORMAL) + sent = mc->Send2Server((char*)pkt.data(), (ULONG)pkt.size()) != FALSE; + LOGI("[Activity] report %zu bytes (%s)%s", text.size(), svcOn ? "foreground" : "no-accessibility", + sent ? "" : " FAILED"); + g_activityReportsInFlight.fetch_sub(1, std::memory_order_relaxed); +} + // ------------------------------------------------------------------ 在线升级 // 载荷布局:[COMMAND_UPDATE:1][APK 大小:8 LE][APK:size][xxh64:8 LE] // 校验通过后写到 filesDir/update.apk,交给 Kotlin 的 PackageInstaller 会话去装 @@ -752,6 +835,22 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength) std::thread(ClientLogThread).detach(); break; + case COMMAND_QUERY_ACTIVITY: + // MCP get_activity_history / 主控「历史活动」窗口:主连接回一次快照 + if (g_activityReportsInFlight.fetch_add(1) >= 1) { + g_activityReportsInFlight.fetch_sub(1); // 已有报表在飞,重复请求丢弃 + break; + } + LOGI("cmd: COMMAND_QUERY_ACTIVITY → main conn snapshot"); + try { + std::thread(ActivityReportThread).detach(); + } catch (const std::exception& e) { + // 线程起不来要把在途计数还回去,否则之后的历史活动请求全被当成「忙」丢掉 + g_activityReportsInFlight.fetch_sub(1); + LOGE("Activity report thread failed: %s", e.what()); + } + break; + case COMMAND_SHELL: LOGI("COMMAND_SHELL (not implemented)"); break; @@ -808,7 +907,12 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength) // CLOCK_MONOTONIC,不受系统时间调整影响;库加载即开始计时。 static const auto g_processStartMono = std::chrono::steady_clock::now(); -static void ConnectionThread() +// 每轮 nativeInit 生成一个代号:旧连接线程可能晚于新一轮才走到收尾代码, +// 只有当前代号的主线程才能清 g_running / g_pMainClient,否则会把新一轮连接 +// 的状态清掉(服务重启 CaptureService 时真会撞上)。 +static std::atomic g_connGeneration{0}; + +static void ConnectionThread(uint64_t gen) { LOGI("ConnectionThread → %s:%d", g_serverIp.c_str(), g_serverPort); @@ -976,9 +1080,12 @@ static void ConnectionThread() std::this_thread::sleep_for(std::chrono::seconds(2)); } - g_running.store(false); - g_pMainClient = nullptr; // client 马上析构,别让别的线程拿到悬空指针 - LOGI("ConnectionThread exit"); + if (g_connGeneration.load(std::memory_order_acquire) == gen) { + g_running.store(false); + // client 马上析构,别让别的线程拿到悬空指针;只清自己的那一个 + if (g_pMainClient == client.get()) g_pMainClient = nullptr; + } + LOGI("ConnectionThread exit (gen=%" PRIu64 ")", gen); if (g_bExit == S_CLIENT_EXIT && g_jvm && g_captureClass && g_nativeExitMid) { JNIEnv* jenv = nullptr; @@ -1053,6 +1160,9 @@ Java_com_yama_client_YamaBridge_nativeInit( g_getActiveWindowMid = env->GetStaticMethodID(g_ctrlClass, "getActiveWindow", "()Ljava/lang/String;"); if (env->ExceptionCheck()) { env->ExceptionClear(); g_getActiveWindowMid = nullptr; } if (!g_getActiveWindowMid) LOGE("ControlService.getActiveWindow not found"); + g_activityTitleMid = env->GetStaticMethodID(g_ctrlClass, "currentActivityTitle", "()Ljava/lang/String;"); + if (env->ExceptionCheck()) { env->ExceptionClear(); g_activityTitleMid = nullptr; } + if (!g_activityTitleMid) LOGE("ControlService.currentActivityTitle not found"); } else { LOGE("ControlService class not found"); } @@ -1085,8 +1195,12 @@ Java_com_yama_client_YamaBridge_nativeInit( } g_bExit = S_CLIENT_NORMAL; + // 历史活动采样:每秒问一次前台应用。Start 幂等(服务重启 nativeStop→nativeInit + // 不会重复起线程);采样与连接无关,断线期间照样记,随进程一起结束。 + LinuxActivity::ActivityHistory::Instance().Start(ProbeActivityFromJava); // g_running 已在 CAS 中设为 true,不重复 store - std::thread(ConnectionThread).detach(); + const uint64_t gen = g_connGeneration.fetch_add(1, std::memory_order_acq_rel) + 1; + std::thread(ConnectionThread, gen).detach(); LOGI("nativeInit OK server=%s:%d", g_serverIp.c_str(), g_serverPort); return 0; } diff --git a/android/app/src/main/java/com/yama/client/ControlService.kt b/android/app/src/main/java/com/yama/client/ControlService.kt index 923e04a..6ab92f1 100644 --- a/android/app/src/main/java/com/yama/client/ControlService.kt +++ b/android/app/src/main/java/com/yama/client/ControlService.kt @@ -40,6 +40,37 @@ class ControlService : AccessibilityService() { @JvmStatic @Volatile var activeWindow: String = "Android" + // ── 历史活动采样用的前台应用标题(C++ ActivityHistory 的 probe 每秒调一次)── + // 辅助功能服务没启用时返回 null,让「历史活动」报告能说清是缺权限,而不是 + // 记一堆默认值 "Android"。包名 → 应用名只查一次(缓存)。 + private val labelCache = HashMap() + + @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. diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index c835da1..b7466f9 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -2,4 +2,6 @@ YAMA YAMA remote control service + + Home diff --git a/server/2015Remote/McpServer.cpp b/server/2015Remote/McpServer.cpp index 51f362f..b67a546 100644 --- a/server/2015Remote/McpServer.cpp +++ b/server/2015Remote/McpServer.cpp @@ -344,7 +344,8 @@ const char* kServerInstructions = u8"(客户端有 PTY 实现,但服务端本轮未开放)、kill_process;" u8"文件传输、进程列表、日志可用。\n" u8"apk(APK):支持主机列表(list_online_hosts / list_all_hosts)/ 详情 / 搜索 / " - u8"审计日志 / 服务状态 / 会话管理、运行日志(get_client_log),以及 " + u8"审计日志 / 服务状态 / 会话管理、运行日志(get_client_log)、活动历史" + u8"(get_activity_history,前台应用停留记录),以及 " u8"host_set_remark / host_set_watch_level 这两个只改服务端本地数据的工具。其余一律不支持" u8"(客户端未实现 SYSTEM 与 SHELL)。\n" u8"copy_files_c2c 是唯一有方向性的工具:源端 src_id 必须是 win,目标端 dst_id 可以是 " @@ -2184,7 +2185,7 @@ std::string BuildToolsListResult(const Json::Value& id) { { Json::Value tool(Json::objectValue); tool["name"] = "get_activity_history"; - tool["description"] = u8"获取指定在线主机的历史活动记录。Windows 与有桌面的 Linux 客户端返回前台窗口驻留记录,每行一条「[时间] [标题] 时长」;无桌面的 Linux 客户端返回登录记录与 shell 命令历史,每行带「[登录]」或「[命令]」标签(首行为「[来源]」说明)。主连接 RPC。结果按 limit/offset 分页(默认 100 条、上限 1000),新在前:offset=0 就是最近 limit 条,想看更早的再往后翻。total 为本机现有记录总数,hasMore 表示还有更早的记录;窗口后端只保留最近 500 条,所以 total 最多约 501(含进行中的当前窗口)。"; + tool["description"] = u8"获取指定在线主机的历史活动记录。Windows 与有桌面的 Linux 客户端返回前台窗口驻留记录,每行一条「[时间] [标题] 时长」;Android 客户端返回前台应用驻留记录(标题为「应用名 (包名)」,需开启无障碍服务);无桌面的 Linux 客户端返回登录记录与 shell 命令历史,每行带「[登录]」或「[命令]」标签(首行为「[来源]」说明)。主连接 RPC。结果按 limit/offset 分页(默认 100 条、上限 1000),新在前:offset=0 就是最近 limit 条,想看更早的再往后翻。total 为本机现有记录总数,hasMore 表示还有更早的记录;窗口后端只保留最近 500 条,所以 total 最多约 501(含进行中的当前窗口)。"; tool["inputSchema"] = BuildPagedHostListInputSchema(); // { id | ids } + limit/offset tool["outputSchema"] = BuildActivityHistoryOutputSchema(); @@ -9966,8 +9967,9 @@ const ToolPlatform kToolPlatforms[] = { // 那是给 SystemDlg 窗口管理用的,不是 MCP 这条路径。 { "list_windows", kPlatWin }, // Linux 客户端已实现 COMMAND_QUERY_ACTIVITY → TOKEN_REPORT_ACTIVITY(linux/LinuxActivityHistory.h, - // 有桌面走 X11 窗口采样,无桌面走 last + shell 历史),与 Windows 同为主连接 RPC - { "get_activity_history", kPlatWin|kPlatLnx }, + // 有桌面走 X11 窗口采样,无桌面走 last + shell 历史),Android 复用同一套采样机、 + // 前台应用由无障碍服务提供,与 Windows 同为主连接 RPC + { "get_activity_history", kPlatWin|kPlatLnx|kPlatApk }, { "list_services", kPlatWin }, { "get_keylog", kPlatWin }, { "list_registry", kPlatWin },