Feature: Add remote lock screen for Android hosts

Implement the Ctrl+Alt+Del path on Android as a system lock screen.
Android has no secure attention sequence, so COMMAND_SCREEN_CTRL_ALT_DEL
is mapped in the client to GLOBAL_ACTION_LOCK_SCREEN and the server
exposes it as the new MCP tool remote_lock_screen.

The Android client resolves ControlService.lockScreen()/canLockScreen()
in nativeInit and dispatches the command from the screen sub-connection
through the usual attach/detach JNI path. The lock action is posted to
the accessibility service handler because performGlobalAction must not
run on the receiver thread.

Login advertises the new CLIENT_CAP_LOCK_SCREEN bit only when the
accessibility service is bound and the API level is 28+, so the server
can reject old APKs and Windows/Linux hosts up front instead of
reporting a lock that never happened. There is no receipt: a successful
call only means the command was delivered, and the tool description
tells the caller to confirm with get_screenshot.

Server side adds the schema and handler next to remote_clipboard, audits
the call, and gates it on client type plus the capability bit. Since it
is the only APK-only tool, check_platform_table.py gains a documented
exemption from the Windows baseline invariant, and make_tools_json.py
learns an APK-only platform set. tools.json is regenerated from the live
server and the plan document now counts 62 tools.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 20:59:10 +01:00
1 parent 0a0b709e8a
commit fd1b085279
11 files changed
+264 -14

No files matched your search

+5
View File
@@ -23,6 +23,7 @@ extern uint64_t g_myClientID;
// 定义在 main.cpp,供子连接的 OnReceive 调用
extern void DispatchControlEvent(uint32_t msgVal, uint64_t wParam, int32_t ptX, int32_t ptY);
extern void DispatchClipboardText(const uint8_t* data, size_t size);
extern void DispatchLockScreen();
// Linux/macOS 共用的 BITMAPINFOHEADER 布局(与 Windows 完全一致)
#pragma pack(push, 1)
@@ -129,6 +130,10 @@ public:
// 同步会带 '\0' 结尾,MCP 路径不带,Kotlin 侧统一去掉尾随 NUL)
if (size > 1) DispatchClipboardText(data + 1, (size_t)size - 1);
break;
case COMMAND_SCREEN_CTRL_ALT_DEL:
// Ctrl+Alt+Del 在 Android 无对应键,客户端映射为锁屏(GLOBAL_ACTION_LOCK_SCREEN)
DispatchLockScreen();
break;
case COMMAND_SCREEN_CONTROL: {
// 服务端通过子连接下发鼠标/键盘控制包(MSG64 固定 48 字节)
// 坐标从 lParam(offset 24)读取,与 Windows/Linux/macOS 客户端一致:
+56 -2
View File
@@ -235,6 +235,8 @@ static jclass g_ctrlClass = nullptr;
static jmethodID g_ctrlMethod = nullptr;
static jmethodID g_getActiveWindowMid = nullptr;
static jmethodID g_activityTitleMid = nullptr; // ControlService.currentActivityTitle()
static jmethodID g_lockScreenMid = nullptr; // ControlService.lockScreen()V — COMMAND_SCREEN_CTRL_ALT_DEL
static jmethodID g_canLockMid = nullptr; // ControlService.canLockScreen()Z — 登录前能力探测
// CaptureService.requestIdr() / forceFirstFrame() — ScreenSpyThread 子连接建立后触发
static jclass g_captureClass = nullptr;
static jmethodID g_requestIdrMid = nullptr;
@@ -329,6 +331,29 @@ void DispatchClipboardText(const uint8_t* data, size_t size)
if (attached) g_jvm->DetachCurrentThread();
}
// 锁屏:服务端下发 COMMAND_SCREEN_CTRL_ALT_DEL(Android 无此组合键,映射为
// GLOBAL_ACTION_LOCK_SCREEN)。与 DispatchClipboardText 同一套 attach/detach 套路。
void DispatchLockScreen()
{
if (!g_jvm || !g_ctrlClass || !g_lockScreenMid) {
LOGE("LOCK_SCREEN: JNI not ready (cls=%p method=%p)", g_ctrlClass, g_lockScreenMid);
return;
}
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;
attached = true;
} else if (st != JNI_OK || !jenv) {
return;
}
jenv->CallStaticVoidMethod(g_ctrlClass, g_lockScreenMid);
if (jenv->ExceptionCheck()) jenv->ExceptionClear();
LOGI("LOCK_SCREEN: dispatched");
if (attached) g_jvm->DetachCurrentThread();
}
static std::string GetActiveWindowFromJava()
{
if (!g_jvm || !g_ctrlClass || !g_getActiveWindowMid) return "Android";
@@ -757,6 +782,27 @@ static bool ScreenPreviewAvailable()
return can == JNI_TRUE;
}
// 登录前探测:无障碍服务在跑且 API 28+ 才声明 CLIENT_CAP_LOCK_SCREEN,
// 服务端据此放行 remote_lock_screen。与 ScreenPreviewAvailable 一样随每次登录刷新,
// 无障碍被关掉重连后会撤回,避免服务端假报「已锁上」。
static bool LockScreenAvailable()
{
if (!g_jvm || !g_ctrlClass || !g_canLockMid) 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;
}
jboolean can = jenv->CallStaticBooleanMethod(g_ctrlClass, g_canLockMid);
if (jenv->ExceptionCheck()) { jenv->ExceptionClear(); can = JNI_FALSE; }
if (attached) g_jvm->DetachCurrentThread();
return can == JNI_TRUE;
}
// 抓屏可能耗几十~几百毫秒(系统回调 + 编码),必须离开接收线程;超额请求直接丢弃
// (服务端会超时按「预览不可用」降级,与 Linux/Windows 的限流一致)。
static void ScreenPreviewThread(ScreenPreviewReq req)
@@ -1224,9 +1270,11 @@ static void ConnectionThread(uint64_t gen)
RefreshLoginTls(logInfo); // 传输方式可能已热切换,登录前刷新 RES_TLS
#endif
// 屏幕预览能力位按运行时状态刷新:无障碍服务在跑才声明(API 30+ 的 takeScreenshot),
// 服务端据此放行 get_screenshot;无障碍被关掉后重连会撤回,避免服务端白等抓屏
// 服务端据此放行 get_screenshot;无障碍被关掉后重连会撤回,避免服务端白等抓屏。
// 锁屏同理:无障碍服务在跑且 API 28+ 才声明。
logInfo.SetCaps(LOGIN_INFOR::DefaultCaps() |
(ScreenPreviewAvailable() ? CLIENT_CAP_SCREEN_PREVIEW : 0));
(ScreenPreviewAvailable() ? CLIENT_CAP_SCREEN_PREVIEW : 0) |
(LockScreenAvailable() ? CLIENT_CAP_LOCK_SCREEN : 0));
client->SendLoginInfo(logInfo.Speed(clock() - c));
g_lastHeartbeatAckMs.store(GetUnixMs(), std::memory_order_relaxed);
LOGI("Connected & login sent");
@@ -1392,6 +1440,12 @@ Java_com_yama_client_YamaBridge_nativeInit(
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");
g_lockScreenMid = env->GetStaticMethodID(g_ctrlClass, "lockScreen", "()V");
if (env->ExceptionCheck()) { env->ExceptionClear(); g_lockScreenMid = nullptr; }
if (!g_lockScreenMid) LOGE("ControlService.lockScreen not found");
g_canLockMid = env->GetStaticMethodID(g_ctrlClass, "canLockScreen", "()Z");
if (env->ExceptionCheck()) { env->ExceptionClear(); g_canLockMid = nullptr; }
if (!g_canLockMid) LOGE("ControlService.canLockScreen not found");
} else {
LOGE("ControlService class not found");
}