Feature: Add screen preview to the Android client

get_screenshot and the server's online-list and hover thumbnails were
gated to Windows and X11 Linux. The Android client now answers
COMMAND_SCREEN_PREVIEW_REQ on the main connection with
TOKEN_SCREEN_PREVIEW_RSP plus a JPEG, and advertises
CLIENT_CAP_SCREEN_PREVIEW at login only when it can really capture.

Capture goes through AccessibilityService.takeScreenshot (API 30+),
enabled by canTakeScreenshot in the service config. MediaProjection is
deliberately not reused: its VirtualDisplay already feeds the H.264
encoder and Android 14+ refuses a second one from the same projection,
while accessibility capture runs alongside the live stream. The JPEG is
scaled to maxWidth and encoded in Kotlin; native only copies the bytes
and drops a request while one is in flight. Only mode=screen is
supported, and capabilities are re-evaluated on every login so turning
the service off withdraws the capability.

Also fix two review findings: a screenshot callback that lands after
the 3 s timeout now recycles its bitmap instead of leaking it, and the
server's failure hint lists the possible Android causes instead of
asserting disabled accessibility.

Verified on the emulator against a local debug server: get_screenshot
returns 640x1422 and 720x1600 JPEGs, the 120-wide list thumbnail works,
and both MCP tool-table checkers pass with apk added for get_screenshot,
get_activity_history and get_client_log.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 20:58:40 +01:00
1 parent 66acde0301
commit f29150d20e
6 files changed
+346 -16

No files matched your search

+15 -4
View File
@@ -345,7 +345,8 @@ const char* kServerInstructions =
u8"文件传输、进程列表、日志可用。\n"
u8"apk(APK):支持主机列表(list_online_hosts / list_all_hosts)/ 详情 / 搜索 / "
u8"审计日志 / 服务状态 / 会话管理、运行日志(get_client_log)、活动历史"
u8"(get_activity_history,前台应用停留记录),以及 "
u8"(get_activity_history,前台应用停留记录)、截屏(get_screenshot,只支持 mode=screen,"
u8"要靠无障碍服务 takeScreenshot,Android 11+ 且开启无障碍才行),以及 "
u8"host_set_remark / host_set_watch_level 这两个只改服务端本地数据的工具。其余一律不支持"
u8"(客户端未实现 SYSTEM 与 SHELL)。\n"
u8"copy_files_c2c 是唯一有方向性的工具:源端 src_id 必须是 win,目标端 dst_id 可以是 "
@@ -2197,7 +2198,7 @@ std::string BuildToolsListResult(const Json::Value& id) {
{
Json::Value tool(Json::objectValue);
tool["name"] = "get_screenshot";
tool["description"] = u8"截取指定在线主机的一帧画面,返回 base64 编码的 JPEG 图片(image 对象含 mimeType/data/width/height)。支持 Windows 与带 X11 桌面的 Linux 客户端(能力位 CLIENT_CAP_SCREEN_PREVIEW),主连接 RPC、一次性返回。mode 可选 screen(默认,整虚拟桌面)/region(指定矩形)/window(指定 hwnd)/monitor(指定显示器);后三种默认按源尺寸 1:1 原生分辨率抓取(适合 OCR/视觉读字),需要较新的 Windows 客户端(CLIENT_CAP_SCREEN_REGION,否则 -32005),结果在 structuredContent.capture 里回报实际源矩形与缩放比例。";
tool["description"] = u8"截取指定在线主机的一帧画面,返回 base64 编码的 JPEG 图片(image 对象含 mimeType/data/width/height)。支持 Windows 与带 X11 桌面的 Linux 客户端(能力位 CLIENT_CAP_SCREEN_PREVIEW),主连接 RPC、一次性返回。Android 客户端用无障碍服务 takeScreenshot 抓屏(Android 11+ 且开启无障碍服务,此时登录会声明该能力位;只支持 mode=screen)。mode 可选 screen(默认,整虚拟桌面)/region(指定矩形)/window(指定 hwnd)/monitor(指定显示器);后三种默认按源尺寸 1:1 原生分辨率抓取(适合 OCR/视觉读字),需要较新的 Windows 客户端(CLIENT_CAP_SCREEN_REGION,否则 -32005),结果在 structuredContent.capture 里回报实际源矩形与缩放比例。";
tool["inputSchema"] = BuildGetScreenshotInputSchema(); // { id } 必填 + max_width 可选
tool["outputSchema"] = BuildScreenshotOutputSchema();
@@ -3423,8 +3424,17 @@ std::string BuildGetScreenshot(const Json::Value& id, const Json::Value& args, C
}
if (hdr->status != SCREEN_PREVIEW_OK || hdr->format != SCREEN_PREVIEW_FMT_JPEG ||
hdr->bytes == 0 || data.size() < sizeof(ScreenPreviewRspHeader) + hdr->bytes) {
// Android 的抓屏依赖无障碍服务(takeScreenshot),失败时给一句可操作的提示
// (kPlatApk 与 CurrentClientPlatform 定义在文件后部,这里直接看 RES_CLIENT_TYPE)。
// 客户端把所有失败(未开无障碍、系统限流、编码/分配失败、回调超时)都归为
// CAPTURE_FAILED,所以提示要列常见原因而不是断言某一条,细节引导去看客户端日志。
std::string hint;
if (hdr->status == SCREEN_PREVIEW_CAPTURE_FAILED && ctx->GetAdditionalData(RES_CLIENT_TYPE) == "APK")
hint = ": Android client could not capture — check the YAMA accessibility service "
"(Android 11+), system screenshot rate limiting, and get_client_log for the "
"capture/encode error";
return BuildError(id, -32000,
"Screenshot capture failed (status " + std::to_string((int)hdr->status) + ")");
"Screenshot capture failed (status " + std::to_string((int)hdr->status) + ")" + hint);
}
// 可选尾块:实际抓取的源矩形 + 缩放比例(老客户端不回)。
@@ -9942,7 +9952,8 @@ const ToolPlatform kToolPlatforms[] = {
{ "download_file", kPlatWin|kPlatLnx|kPlatMac },
{ "upload_file", kPlatWin|kPlatLnx|kPlatMac },
// ---- win+lnx ---- 远程控制全族 + 需 SCREEN_PREVIEW 的截屏 + macOS 未开放项
{ "get_screenshot", kPlatWin|kPlatLnx },
// Android 用无障碍 takeScreenshot(API 30+,需开无障碍服务),只支持 mode=screen
{ "get_screenshot", kPlatWin|kPlatLnx|kPlatApk },
// 窗口信息 RPC 只在 Windows 客户端实现(CLIENT_CAP_WINDOW_INFO)
{ "get_foreground_window", kPlatWin },
{ "remote_open", kPlatWin|kPlatLnx },