Feature: Support input blocking and privacy overlay on Android hosts

remote_block_input rejected anything but Windows because the Android
client had no COMMAND_SCREEN_BLOCK_INPUT handler. This adds the overlay
on the client and the server gating, and wires up COMMAND_SCREEN_BLANK.

Client: the accessibility service hangs a full-screen
TYPE_ACCESSIBILITY_OVERLAY on itself. block=true makes it transparent
and touchable so it swallows physical taps; black=true paints it black.
Both are absolute states, so repeated commands are idempotent, and the
handler clears the overlay when the screen sub-connection dies (Android
has no "released when the calling thread exits" fallback). During a
remote gesture the overlay is flipped to FLAG_NOT_TOUCHABLE, mirroring
Windows' BlockInput(false) wrap; the flag change and the injection must
be separated by a short delay or the gesture is swallowed.

Server: CLIENT_CAP_PRIVACY_OVERLAY distinguishes new APKs from old ones;
Linux/macOS and old APKs still get -32005 up front. Tool masks,
instructions and mcp/tools.json follow the change. COMMAND_SCREEN_BLANK
is implemented but not exposed as a tool: MediaProjection captures the
overlay, so blacking out the screen blinds the operator too and cannot
emulate Windows' WDA_EXCLUDEFROMCAPTURE.

Verified on the Android 15 emulator: blocked taps are swallowed while
remote clicks still land, the block re-arms after each gesture, the
watchdog auto-unblocks at the deadline, a dropped sub-connection clears
the overlay, and a black overlay shows up in both screencap and
get_screenshot. The committed ghost.apk is stale; rebuild before release.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 21:58:54 +01:00
1 parent 445bd8bbb9
commit a801a2999a
9 files changed
+306 -24

No files matched your search

+57 -1
View File
@@ -239,6 +239,8 @@ 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 — 登录前能力探测
static jmethodID g_overlayMid = nullptr; // ControlService.setPrivacyOverlay(ZZ)V — 黑屏 / 锁键鼠
static jmethodID g_canOverlayMid = nullptr; // ControlService.canOverlay()Z — 登录前能力探测
// CaptureService.requestIdr() / forceFirstFrame() — ScreenSpyThread 子连接建立后触发
static jclass g_captureClass = nullptr;
static jmethodID g_requestIdrMid = nullptr;
@@ -404,6 +406,31 @@ void DispatchLockScreen()
if (attached) g_jvm->DetachCurrentThread();
}
// 隐私 overlay:黑屏(COMMAND_SCREEN_BLANK)与锁键鼠(COMMAND_SCREEN_BLOCK_INPUT)
// 共用同一个无障碍 overlay,两个参数都是绝对状态(见 ScreenHandler.h 的 ApplyOverlay)。
void DispatchPrivacyOverlay(bool black, bool block)
{
if (!g_jvm || !g_ctrlClass || !g_overlayMid) {
LOGE("PRIVACY_OVERLAY: JNI not ready (cls=%p method=%p)", g_ctrlClass, g_overlayMid);
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_overlayMid,
black ? JNI_TRUE : JNI_FALSE,
block ? JNI_TRUE : JNI_FALSE);
if (jenv->ExceptionCheck()) jenv->ExceptionClear();
LOGI("PRIVACY_OVERLAY: dispatched black=%d block=%d", (int)black, (int)block);
if (attached) g_jvm->DetachCurrentThread();
}
static std::string GetActiveWindowFromJava()
{
if (!g_jvm || !g_ctrlClass || !g_getActiveWindowMid) return "Android";
@@ -961,6 +988,27 @@ static bool LockScreenAvailable()
return can == JNI_TRUE;
}
// 登录前探测:无障碍服务在跑且 API 22+(TYPE_ACCESSIBILITY_OVERLAY)才声明
// CLIENT_CAP_PRIVACY_OVERLAY,服务端据此放行 Android 的 remote_block_input。
// 与 LockScreenAvailable 一样随每次登录刷新:无障碍被关掉重连后撤回。
static bool OverlayAvailable()
{
if (!g_jvm || !g_ctrlClass || !g_canOverlayMid) 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_canOverlayMid);
if (jenv->ExceptionCheck()) { jenv->ExceptionClear(); can = JNI_FALSE; }
if (attached) g_jvm->DetachCurrentThread();
return can == JNI_TRUE;
}
// 抓屏可能耗几十~几百毫秒(系统回调 + 编码),必须离开接收线程;超额请求直接丢弃
// (服务端会超时按「预览不可用」降级,与 Linux/Windows 的限流一致)。
static void ScreenPreviewThread(ScreenPreviewReq req)
@@ -1475,9 +1523,11 @@ static void ConnectionThread(uint64_t gen)
// 屏幕预览能力位按运行时状态刷新:无障碍服务在跑才声明(API 30+ 的 takeScreenshot),
// 服务端据此放行 get_screenshot;无障碍被关掉后重连会撤回,避免服务端白等抓屏。
// 锁屏同理:无障碍服务在跑且 API 28+ 才声明。
// 隐私 overlay(黑屏 / 锁键鼠)同理:无障碍服务在跑且 API 22+ 才声明。
logInfo.SetCaps(LOGIN_INFOR::DefaultCaps() |
(ScreenPreviewAvailable() ? CLIENT_CAP_SCREEN_PREVIEW : 0) |
(LockScreenAvailable() ? CLIENT_CAP_LOCK_SCREEN : 0));
(LockScreenAvailable() ? CLIENT_CAP_LOCK_SCREEN : 0) |
(OverlayAvailable() ? CLIENT_CAP_PRIVACY_OVERLAY : 0));
client->SendLoginInfo(logInfo.Speed(clock() - c));
g_lastHeartbeatAckMs.store(GetUnixMs(), std::memory_order_relaxed);
LOGI("Connected & login sent");
@@ -1649,6 +1699,12 @@ Java_com_yama_client_YamaBridge_nativeInit(
g_canLockMid = env->GetStaticMethodID(g_ctrlClass, "canLockScreen", "()Z");
if (env->ExceptionCheck()) { env->ExceptionClear(); g_canLockMid = nullptr; }
if (!g_canLockMid) LOGE("ControlService.canLockScreen not found");
g_overlayMid = env->GetStaticMethodID(g_ctrlClass, "setPrivacyOverlay", "(ZZ)V");
if (env->ExceptionCheck()) { env->ExceptionClear(); g_overlayMid = nullptr; }
if (!g_overlayMid) LOGE("ControlService.setPrivacyOverlay not found");
g_canOverlayMid = env->GetStaticMethodID(g_ctrlClass, "canOverlay", "()Z");
if (env->ExceptionCheck()) { env->ExceptionClear(); g_canOverlayMid = nullptr; }
if (!g_canOverlayMid) LOGE("ControlService.canOverlay not found");
} else {
LOGE("ControlService class not found");
}