Feature: Support remote chat on Android hosts

The chat tools were Windows-only because the Android client had no
COMMAND_CHAT handler: the server asked for a chat sub-connection and the
command was silently dropped, leaving chat_open to time out. This adds
the full path on the client and the matching server gating.

Client: COMMAND_CHAT on the main connection spawns a chat thread that
opens an authenticated sub-connection, sends TOKEN_CHAT_START and waits
for the server to bind it. Incoming GBK lines are decoded and dispatched
to ChatSession, which drives an in-app chat activity; when accessibility
is available the activity is pulled to the front, otherwise a tappable
notification is posted. Outgoing text is strictly GBK encoded so emoji
and unmappable characters are rejected instead of becoming '?'. When the
server ends the session the activity closes itself, matching the Windows
client's DestroyWindow, and a new session starts from an empty log.

Server: a new CLIENT_CAP_REMOTE_CHAT bit lets the server tell new APKs
from old ones. ResolveHost now accepts Windows and Android hosts and
rejects older APKs up front with -32005 rather than waiting for the
command to expire. Tool platform masks, instructions, the docs matrix
and mcp/tools.json follow the same change.

Verified end to end on the Android 15 emulator: chat_open pops the
activity, Chinese text from the server renders, a device reply reaches
chat_wait, and ending the session from the master closes the activity
instead of leaving a dead window. The committed ghost.apk is stale and
must be rebuilt before release.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 21:58:44 +01:00
1 parent f33a205fd9
commit 915c587964
14 files changed
+695 -29

No files matched your search

+6
View File
@@ -31,6 +31,12 @@
</intent-filter>
</activity>
<!-- 远程交谈(COMMAND_CHAT):单实例,会话进行中重复拉起只走 onNewIntent -->
<activity
android:name=".ChatActivity"
android:exported="false"
android:launchMode="singleTop" />
<service
android:name=".CaptureService"
android:foregroundServiceType="dataSync|mediaProjection"
+66
View File
@@ -0,0 +1,66 @@
#pragma once
// Android 远程交谈(COMMAND_CHAT)子连接。
//
// 协议与 Windows 客户端完全一致(见 client/ChatManager.cpp、server/2015Remote/McpChat.cpp):
// 主连接 [COMMAND_CHAT] → 本线程开子连接(ConnAuth)→ 首包单字节 [TOKEN_CHAT_START]
// → 服务端绑定后回单字节 [COMMAND_NEXT_CHAT](0)→ 弹出聊天界面
// → 双向文本 [GBK 字节][NUL];服务端单字节 [COMMAND_CHAT_CLOSE](1)结束会话。
// 单字节包都是控制/心跳,不是文本(客户端发文本至少带尾部 NUL)。
#include "common/commands.h"
#include "client/IOCPClient.h"
#include <vector>
#include <cstring>
// 定义在 main.cpp:把会话事件交给 Kotlin(ChatSession.onNativeEvent)。
// kind: 1 = 服务端已绑定(可以显示聊天界面),2 = 会话结束,3 = 一条对方文本
// (data 为 GBK 字节,不含结尾 NUL)。必须从非 UI 线程调用,Kotlin 侧自己 post 到主线程。
extern void DispatchChatEvent(int kind, const uint8_t* data, size_t size);
class AndroidChatHandler : public IOCPManager {
public:
explicit AndroidChatHandler(IOCPClient* client) : m_client(client) {
BYTE bToken = TOKEN_CHAT_START;
m_client->Send2Server((char*)&bToken, 1);
}
// Java 侧点「发送」:GBK 字节 + 结尾 NUL(与 Windows 客户端 Send() 的载荷一致)。
bool SendText(const uint8_t* gbk, size_t size) {
if (!m_client || !m_client->IsConnected() || !gbk || size == 0) return false;
std::vector<char> pkt(size + 1);
memcpy(pkt.data(), gbk, size);
pkt[size] = '\0';
return m_client->Send2Server(pkt.data(), (ULONG)pkt.size()) != FALSE;
}
virtual VOID OnReceive(PBYTE data, ULONG size) override {
if (!data || !size) return;
if (size == 1) {
switch (data[0]) {
case COMMAND_NEXT_CHAT:
DispatchChatEvent(1, nullptr, 0);
return;
case COMMAND_CHAT_CLOSE:
DispatchChatEvent(2, nullptr, 0);
return;
case COMMAND_CHAT_SCREEN_LOCK:
case COMMAND_CHAT_SCREEN_UNLOCK:
// 服务端手动交谈时的「锁定屏幕/屏蔽功能键」在 Android 上没有对应语义
// (Windows 是 ClipCursor + 隐藏任务栏)。忽略,不锁设备。
return;
default:
// 单字节心跳等:聊天通道不承载,丢弃
break;
}
}
// GBK 文本,NUL 结尾;上限与 Windows 输入缓冲一致,防异常对端撑爆内存
const size_t kMaxIncoming = 8u * 1024u;
size_t n = 0;
while (n < size && n < kMaxIncoming && data[n] != 0) ++n;
if (n) DispatchChatEvent(3, data, n);
}
private:
IOCPClient* m_client;
};
+162
View File
@@ -25,6 +25,7 @@
#include "common/rtt_estimator.h"
#include "client/IOCPClient.h"
#include "ScreenHandler.h"
#include "ChatHandler.h"
#define XXH_INLINE_ALL
#include "common/xxhash.h"
#include "common/logger.h"
@@ -246,6 +247,9 @@ static jmethodID g_nativeExitMid = nullptr; // CaptureService.onNativeEx
static jmethodID g_updateReceivedMid = nullptr; // CaptureService.onUpdateReceived(String) — 升级 APK 已落盘
static jmethodID g_clipboardMid = nullptr; // CaptureService.onClipboardText([B)V — 远程剪贴板
static jmethodID g_uninstallMid = nullptr; // CaptureService.onUninstallRequested()V — TOKEN_UNINSTALL
// ChatSession(远程交谈界面:帧入站文本、发送按钮)
static jclass g_chatClass = nullptr;
static jmethodID g_chatEventMid = nullptr; // ChatSession.onNativeEvent(I[B)V
// ScreenShot(无障碍 takeScreenshot 抓屏,MCP get_screenshot 用)
static jclass g_shotClass = nullptr;
static jmethodID g_shotCanMid = nullptr; // ScreenShot.canCapture()Z
@@ -332,6 +336,50 @@ void DispatchClipboardText(const uint8_t* data, size_t size)
if (attached) g_jvm->DetachCurrentThread();
}
// 远程交谈:native 子连接 → ChatSession.onNativeEvent(kind, data)。
// kind: 1 = 服务端已绑定,2 = 会话结束,3 = 一条 GBK 文本。data 只在 kind=3 时非空。
// 与 DispatchClipboardText 同一套 attach/detach 套路,供 AndroidChatHandler::OnReceive 调用。
void DispatchChatEvent(int kind, const uint8_t* data, size_t size)
{
if (!g_jvm || !g_chatClass || !g_chatEventMid) {
LOGE("CHAT: JNI not ready (cls=%p method=%p)", g_chatClass, g_chatEventMid);
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;
}
jbyteArray arr = nullptr;
if (data && size) {
arr = jenv->NewByteArray((jsize)size);
if (!arr) {
if (jenv->ExceptionOccurred()) jenv->ExceptionClear();
if (attached) g_jvm->DetachCurrentThread();
LOGE("CHAT: NewByteArray(%zu) failed", size);
return;
}
jenv->SetByteArrayRegion(arr, 0, (jsize)size, (const jbyte*)data);
if (jenv->ExceptionCheck()) {
jenv->ExceptionClear();
jenv->DeleteLocalRef(arr);
if (attached) g_jvm->DetachCurrentThread();
return;
}
}
jenv->CallStaticVoidMethod(g_chatClass, g_chatEventMid, (jint)kind, arr);
if (jenv->ExceptionCheck()) {
jenv->ExceptionClear();
LOGE("CHAT: onNativeEvent(%d) threw", kind);
}
if (arr) jenv->DeleteLocalRef(arr);
if (attached) g_jvm->DetachCurrentThread();
}
// 锁屏:服务端下发 COMMAND_SCREEN_CTRL_ALT_DEL(Android 无此组合键,映射为
// GLOBAL_ACTION_LOCK_SCREEN)。与 DispatchClipboardText 同一套 attach/detach 套路。
void DispatchLockScreen()
@@ -668,6 +716,71 @@ static void ClientLogThread()
LOGI("ClientLogThread exit");
}
// ------------------------------------------------------------------ 远程交谈
// 服务端 COMMAND_CHAT → 开子连接 → 首包 TOKEN_CHAT_START → 服务端绑定后回 [0]。
// 同一时刻只允许一个交谈线程:服务端可能重复下发(重连后手动再点一次「远程交谈」),
// 第二个子连接会被服务端当重复子连接吸收,这里直接忽略。
static std::atomic<bool> g_chatInFlight{false};
static std::mutex g_chatMx;
static AndroidChatHandler* g_chatHandler = nullptr;
static void ChatThread()
{
// detached 线程:异常逃出去会 std::terminate 整个客户端;在途标志也必须无条件还回去,
// 否则之后所有 COMMAND_CHAT 都被当成「已有交谈在跑」丢掉
struct InFlightGuard {
~InFlightGuard() { g_chatInFlight.store(false); }
} guard;
LOGI("ChatThread start → %s:%d", g_serverIp.c_str(), g_serverPort);
for (int attempt = 1; attempt <= 3 && S_CLIENT_NORMAL == g_bExit && g_running.load(); ++attempt) {
std::unique_ptr<IOCPClient> client;
std::unique_ptr<AndroidChatHandler> handler;
try {
client = std::make_unique<IOCPClient>(g_bExit, true);
client->EnableSubConnAuth(true, g_myClientID);
// 与屏幕/日志子连接相同:必须继承配置的传输方式,否则 WS/WSS 部署下连不上反代
client->SetDefaultProto((int)(unsigned char)g_SETTINGS.protoType);
if (!client->ConnectServer(g_serverIp.c_str(), g_serverPort)) {
LOGE("ChatThread: connect failed (attempt %d/3)", attempt);
Sleep(1000);
continue;
}
// 构造即发 TOKEN_CHAT_START;服务端绑定后回单字节 [0] → Kotlin 弹聊天界面
handler = std::make_unique<AndroidChatHandler>(client.get());
client->setManagerCallBack(handler.get(),
IOCPManager::DataProcess,
IOCPManager::ReconnectProcess);
{
std::lock_guard<std::mutex> lk(g_chatMx);
g_chatHandler = handler.get();
}
while (client->IsRunning() && client->IsConnected() && S_CLIENT_NORMAL == g_bExit)
Sleep(200);
{
// 先摘掉 Java 可见的指针,再让 handler 析构:nativeChatSend 持同一把锁,
// 不会拿到正在销毁的对象
std::lock_guard<std::mutex> lk(g_chatMx);
g_chatHandler = nullptr;
}
client->setManagerCallBack(nullptr, nullptr, nullptr);
LOGI("ChatThread: sub-conn closed (attempt %d)", attempt);
break; // 会话结束/对端断开不重连:服务端会话已随子连接失效,需要新的 chat_open
} catch (const std::exception& e) {
LOGE("ChatThread exception (attempt %d): %s", attempt, e.what());
{
std::lock_guard<std::mutex> lk(g_chatMx);
g_chatHandler = nullptr;
}
if (client) client->setManagerCallBack(nullptr, nullptr, nullptr);
Sleep(1000);
}
}
// 会话结束(含一直没连上):Kotlin 侧未打开过界面时会直接忽略
DispatchChatEvent(2, nullptr, 0);
LOGI("ChatThread exit");
}
// ------------------------------------------------------------------ 历史活动上报
// 主连接一次性快照(与 Windows/Linux 一致,服务端 TOKEN_REPORT_ACTIVITY 分支不 CancelIO)。
// 采样机每秒问一次 ControlService 的前台应用;这里只取 Dump(),几十微秒的活。
@@ -1057,6 +1170,22 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength)
break;
}
case COMMAND_CHAT: {
// 远程交谈(主控右键「远程交谈」/ MCP chat_open):子连接 + 应用内聊天界面
if (g_chatInFlight.exchange(true)) {
LOGI("cmd: COMMAND_CHAT ignored (chat already in flight)");
break;
}
LOGI("cmd: COMMAND_CHAT → sub-conn");
try {
std::thread(ChatThread).detach();
} catch (const std::exception& e) {
g_chatInFlight.store(false);
LOGE("Chat thread failed: %s", e.what());
}
break;
}
case COMMAND_SCREEN_CONTROL: {
if (ulLength < 1 + 48u) { LOGI("SCREEN_CONTROL(main): too short %u", ulLength); break; }
const uint8_t* p = szBuffer + 1;
@@ -1523,6 +1652,17 @@ Java_com_yama_client_YamaBridge_nativeInit(
} else {
LOGE("ScreenShot class not found");
}
// 远程交谈:ChatSession.onNativeEvent(int, byte[]) —— 入站事件与文本
jclass chatCls = env->FindClass("com/yama/client/ChatSession");
if (env->ExceptionCheck()) { env->ExceptionClear(); chatCls = nullptr; }
if (chatCls) {
g_chatClass = (jclass)env->NewGlobalRef(chatCls);
g_chatEventMid = env->GetStaticMethodID(g_chatClass, "onNativeEvent", "(I[B)V");
if (env->ExceptionCheck()) { env->ExceptionClear(); g_chatEventMid = nullptr; }
if (!g_chatEventMid) LOGE("ChatSession.onNativeEvent not found");
} else {
LOGE("ChatSession class not found");
}
}
}
@@ -1561,6 +1701,28 @@ Java_com_yama_client_YamaBridge_nativeReportMessage(JNIEnv* env, jobject, jstrin
env->ReleaseStringUTFChars(msg, c);
}
// 由 ChatSession 在用户点「发送」时调用。入参已是 GBK 字节(Kotlin 侧严格编码,
// 表示不了的字符不会落到这里);native 只补结尾 NUL 并写入交谈子连接。
JNIEXPORT jboolean JNICALL
Java_com_yama_client_YamaBridge_nativeChatSend(JNIEnv* env, jobject, jbyteArray data)
{
if (!data) return JNI_FALSE;
const jsize len = env->GetArrayLength(data);
// 与 Windows 客户端输入缓冲一致:4000 GBK 字节上限(4096 缓冲留出 NUL 与余量)
if (len <= 0 || len > 4000) return JNI_FALSE;
std::vector<uint8_t> buf((size_t)len);
env->GetByteArrayRegion(data, 0, len, (jbyte*)buf.data());
if (env->ExceptionCheck()) { env->ExceptionClear(); return JNI_FALSE; }
std::lock_guard<std::mutex> lk(g_chatMx);
if (!g_chatHandler) {
LOGI("CHAT: send dropped, no active session");
return JNI_FALSE;
}
const bool ok = g_chatHandler->SendText(buf.data(), buf.size());
LOGI("CHAT: send %d bytes -> %d", (int)len, ok ? 1 : 0);
return ok ? JNI_TRUE : JNI_FALSE;
}
// 由 CaptureService 在 MediaCodec 配置完成后调用,告知 C++ 侧实际捕获尺寸
JNIEXPORT void JNICALL
Java_com_yama_client_YamaBridge_nativeSetScreenSize(JNIEnv*, jobject, jint width, jint height)
@@ -0,0 +1,130 @@
package com.yama.client
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.Button
import android.widget.EditText
import android.widget.ScrollView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
/**
* 远程交谈界面(服务端 COMMAND_CHAT → 子连接 → COMMAND_NEXT_CHAT 后由 ChatSession 拉起)。
*
* 关闭窗口只是停止显示:会话仍在,服务端再来消息会重新拉起本界面(与 Windows 客户端的
* SW_RESTORE 语义一致)。真正结束由服务端 COMMAND_CHAT_CLOSE 或子连接断开触发,此时与
* Windows 客户端一致直接关闭本界面(Windows 是 DestroyWindow),不留下只能看不能发的空窗。
*/
class ChatActivity : AppCompatActivity() {
private lateinit var logView: TextView
private lateinit var scroll: ScrollView
private lateinit var input: EditText
private lateinit var sendBtn: Button
private lateinit var statusView: TextView
private val onLine: (ChatSession.Line) -> Unit = { line -> appendLine(line) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
logView = findViewById(R.id.chat_log)
scroll = findViewById(R.id.chat_scroll)
input = findViewById(R.id.chat_input)
sendBtn = findViewById(R.id.chat_send)
statusView = findViewById(R.id.chat_status)
applySystemBarInsets()
sendBtn.setOnClickListener { doSend() }
input.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEND) {
doSend()
true
} else false
}
// attach 返回历史快照:界面被关掉又拉起来时,先前的对话仍在
ChatSession.attach(onLine) { updateState() }.forEach { appendLine(it) }
updateState()
}
override fun onResume() {
super.onResume()
ChatSession.uiVisible = true
ChatSession.cancelNotification()
updateState()
}
override fun onPause() {
super.onPause()
ChatSession.uiVisible = false
}
override fun onDestroy() {
super.onDestroy()
ChatSession.detach(onLine)
// 不在这里清 uiVisible:onPause 已经清过,而新界面的 onResume 可能早于本实例的
// onDestroy(服务端结束会话后立刻开新会话),这里再清会误伤新界面。
}
/**
* targetSdk 35 在 Android 15 上强制 edge-to-edge,fitsSystemWindows 不再自动让出
* 状态栏/导航栏;不处理的话标题会压到状态栏下面、输入框会被导航条挡住。
* 手动把系统栏(外加输入法)高度加进根布局的内边距。
*/
private fun applySystemBarInsets() {
WindowCompat.getInsetsController(window, window.decorView)
.isAppearanceLightStatusBars = true
val root = findViewById<View>(R.id.chat_root)
val base = (resources.displayMetrics.density * 12).toInt()
ViewCompat.setOnApplyWindowInsetsListener(root) { v, insets ->
val bars = insets.getInsets(
WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.ime())
v.setPadding(base + bars.left, base + bars.top,
base + bars.right, base + bars.bottom)
insets
}
}
private fun appendLine(line: ChatSession.Line) {
logView.append((if (line.mine) "我:" else "对方:") + line.text + "\n")
// 等布局完成再滚到底,否则最后一条贴不到底
scroll.post { scroll.fullScroll(View.FOCUS_DOWN) }
}
private fun updateState() {
// 会话已结束(服务端 COMMAND_CHAT_CLOSE 或子连接断开):与 Windows 客户端一致
// 直接关窗,而不是把输入框置灰后把窗口留在屏幕上。
if (!ChatSession.active) {
finish()
return
}
input.isEnabled = true
sendBtn.isEnabled = true
statusView.text = "已连接(消息按 GBK 中文编码发送,不支持 emoji)"
}
private fun doSend() {
if (!ChatSession.active) {
Toast.makeText(this, "交谈已结束", Toast.LENGTH_SHORT).show()
return
}
val text = input.text.toString()
if (text.isBlank()) return
if (!ChatSession.send(text)) {
// 最常见的原因是输入了 emoji / GBK 表示不了的字符,或超过 4000 字节
Toast.makeText(this,
"发送失败:只能发送 GBK 能表示的字符(不支持 emoji),且不超过 4000 字节",
Toast.LENGTH_LONG).show()
return
}
input.setText("")
}
}
@@ -0,0 +1,202 @@
package com.yama.client
import android.Manifest
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
import java.nio.CharBuffer
import java.nio.charset.CharacterCodingException
import java.nio.charset.Charset
import java.nio.charset.CodingErrorAction
/**
* 远程交谈(COMMAND_CHAT)的 Android 会话层。
*
* native 侧(android/.../cpp/ChatHandler.h + ChatThread)负责子连接与协议,这里只管界面:
* - onNativeEvent 由 native 子线程回调,统一切到主线程再分发;
* - 消息按 GBK 解码(协议是 CP936,与 Windows 客户端一致),发出去前严格编码,
* emoji / GBK 表示不了的字符直接拒绝并提示,绝不静默变成 '?';
* - 聊天界面用 ControlService(无障碍)在后台拉起,没有无障碍时退一条通知,
* 用户点开后由通知进入 ChatActivity —— 与升级安装框/卸载确认框同一条路。
*/
object ChatSession {
private const val TAG = "YAMA_CHAT"
private const val CHANNEL_ID = "yama_chat"
private const val NOTIF_ID = 104
data class Line(val mine: Boolean, val text: String)
private val gate = Any()
private val history = ArrayDeque<Line>()
private var lineListener: ((Line) -> Unit)? = null
private var stateListener: (() -> Unit)? = null
private val main = Handler(Looper.getMainLooper())
private val gbk = Charset.forName("GBK")
/** 服务端已绑定子连接(收到 COMMAND_NEXT_CHAT)且会话尚未结束 */
@Volatile
var active = false
private set
/** ChatActivity 在前台:避免每条消息都 startActivity 打扰正在输入的用户 */
@Volatile
var uiVisible = false
/** native 回调入口。kind: 1 = 已绑定,2 = 会话结束,3 = 一条 GBK 文本 */
@JvmStatic
fun onNativeEvent(kind: Int, data: ByteArray?) {
main.post { handle(kind, data) }
}
private fun handle(kind: Int, data: ByteArray?) {
when (kind) {
1 -> {
Log.i(TAG, "session opened")
// 新会话:上一条会话的记录不带过来(Windows 客户端每次都是全新对话框)。
// 同一会话内关掉界面再打开时历史仍在,由 attach() 的注释描述。
synchronized(gate) { history.clear() }
active = true
notifyState()
showUi()
}
2 -> {
// 一直没连上就结束(connect 失败)也会收到 2:从未打开过会话时不打扰用户
if (!active) {
Log.i(TAG, "closed event without an open session, ignored")
return
}
Log.i(TAG, "session closed")
active = false
// 界面收到 state 回调后会自己 finish();这里先清掉可见标志,
// 免得服务端紧接着又开一个新会话时 showUi() 被旧窗口挡住不弹。
uiVisible = false
notifyState()
cancelNotification()
}
3 -> {
if (data == null || data.isEmpty()) return
// 去尾随 NUL(Windows 客户端发送带 NUL,MFC 路径同样)
val text = String(data, gbk).trimEnd('\u0000')
if (text.isEmpty()) return
active = true
append(Line(false, text))
showUi() // 用户关掉窗口后再来消息 → 重新拉起(与 Windows SW_RESTORE 一致)
}
}
}
/** ChatActivity 打开时调用:注册监听并取回历史(在锁内拷贝,回调始终在主线程) */
fun attach(onLine: (Line) -> Unit, onState: () -> Unit): List<Line> = synchronized(gate) {
lineListener = onLine
stateListener = onState
history.toList()
}
fun detach(onLine: (Line) -> Unit) = synchronized(gate) {
if (lineListener === onLine) lineListener = null
stateListener = null
}
/**
* 发送一条文本。返回 false 表示没有会话、超出 4000 GBK 字节,或含 GBK 表示不了的字符
* (调用方据此提示,不要只说「发送失败」)。
*/
fun send(text: String): Boolean {
if (text.isEmpty() || !active) return false
val encoder = gbk.newEncoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT)
val bytes = try {
val bb = encoder.encode(CharBuffer.wrap(text))
ByteArray(bb.remaining()).also { bb.get(it) }
} catch (e: CharacterCodingException) {
Log.w(TAG, "send rejected: character not representable in GBK")
return false
}
if (bytes.isEmpty() || bytes.size > 4000) return false
if (!YamaBridge.nativeChatSend(bytes)) {
Log.w(TAG, "send rejected by native (${bytes.size} bytes, no active sub-connection)")
return false
}
append(Line(true, text))
return true
}
fun cancelNotification() {
val ctx = CaptureService.instance ?: CaptureService.appContext ?: return
val nm = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager ?: return
runCatching { nm.cancel(NOTIF_ID) }
}
private fun append(line: Line) {
val cb = synchronized(gate) {
history.addLast(line)
while (history.size > 200) history.removeFirst()
lineListener
}
cb?.invoke(line)
}
private fun notifyState() {
val cb = synchronized(gate) { stateListener }
cb?.invoke()
}
// ---------------------------------------------------------------- 拉起界面
private fun showUi() {
if (uiVisible) return
val ctx = CaptureService.instance ?: CaptureService.appContext ?: return
val intent = Intent(ctx, ChatActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val acc = ControlService.instance
if (acc != null) {
val ok = runCatching { acc.startActivity(intent) }.isSuccess
if (ok) {
Log.i(TAG, "chat UI opened via accessibility")
return
}
Log.w(TAG, "accessibility startActivity failed, falling back to notification")
} else {
Log.w(TAG, "no accessibility, posting notification")
}
notifyUser(ctx)
}
private fun notifyUser(ctx: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
ctx.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) !=
PackageManager.PERMISSION_GRANTED) {
Log.w(TAG, "notification skipped: POST_NOTIFICATIONS not granted")
return
}
val nm = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager ?: return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
nm.createNotificationChannel(
NotificationChannel(CHANNEL_ID, "YAMA 远程交谈", NotificationManager.IMPORTANCE_HIGH))
}
var flags = PendingIntent.FLAG_UPDATE_CURRENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
flags = flags or PendingIntent.FLAG_IMMUTABLE
val pi = PendingIntent.getActivity(ctx, NOTIF_ID,
Intent(ctx, ChatActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), flags)
val b = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
Notification.Builder(ctx, CHANNEL_ID)
else
@Suppress("DEPRECATION") Notification.Builder(ctx)
b.setSmallIcon(android.R.drawable.sym_action_chat)
.setContentTitle("YAMA 远程交谈")
.setContentText("有人在与你交谈,点按查看并回复。")
.setAutoCancel(true)
.setContentIntent(pi)
runCatching { nm.notify(NOTIF_ID, b.build()) }
}
}
@@ -29,4 +29,10 @@ object YamaBridge {
/** 把一行文本回报到服务端消息栏(TOKEN_CLIENT_MSG),用于升级流程的成败说明 */
external fun nativeReportMessage(msg: String)
/**
* 远程交谈:发送一条已按 GBK 编码的消息(不含结尾 NUL,native 补)。
* 没有活跃的交谈子连接时返回 false。
*/
external fun nativeChatSend(data: ByteArray): Boolean
}
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="12dp">
<TextView
android:id="@+id/chat_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="远程交谈"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/chat_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="6dp"
android:text="正在连接…"
android:textSize="12sp" />
<ScrollView
android:id="@+id/chat_scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#11000000"
android:padding="8dp">
<TextView
android:id="@+id/chat_log"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:textSize="15sp" />
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<EditText
android:id="@+id/chat_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="输入消息…"
android:imeOptions="actionSend"
android:inputType="text"
android:maxLines="3" />
<Button
android:id="@+id/chat_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="发送" />
</LinearLayout>
</LinearLayout>
+3
View File
@@ -147,6 +147,7 @@ inline int isValid_10s()
#define CLIENT_CAP_SCREEN_CLIPBOARD 0x0800 // 屏幕子连接处理 COMMAND_SCREEN_SET_CLIPBOARD,并把 VK_V 映射成 ACTION_PASTE(仅新版 Android)
#define CLIENT_CAP_LOCK_SCREEN 0x1000 // 屏幕子连接处理 COMMAND_SCREEN_CTRL_ALT_DEL(Android 映射为锁屏 GLOBAL_ACTION_LOCK_SCREEN)
#define CLIENT_CAP_UNINSTALL 0x2000 // 主连接处理 TOKEN_UNINSTALL(Android 拉起系统卸载确认框;Windows 客户端一直支持但不声明)
#define CLIENT_CAP_REMOTE_CHAT 0x4000 // 主连接处理 COMMAND_CHAT(远程交谈子连接;Windows 一直支持但不声明,目前只有新版 Android 声明)
#define TALK_DLG_MAXLEN 1024 // 最大输入字符长度
@@ -1311,6 +1312,8 @@ typedef struct LOGIN_INFOR {
// 卸载主机(TOKEN_UNINSTALL):命令处理是编译进来的,老 APK 收到只会静默丢弃,
// 服务端据此提前拒绝,而不是发一条没人接的命令后假报 ok。
caps |= CLIENT_CAP_UNINSTALL;
// 远程交谈(COMMAND_CHAT → 子连接 + 应用内聊天窗口):同上,老 APK 不认识这条命令。
caps |= CLIENT_CAP_REMOTE_CHAT;
#endif
return caps;
}
+1 -1
View File
@@ -131,7 +131,7 @@
| 客户端日志 | `COMMAND_QUERY_LOG` | ✅ `get_client_log` | — |
| 电源操作 | `TOKEN_MACHINE_MANAGE` | ✅ `power_action` | — |
| 卸载主机(被控程序) | `TOKEN_UNINSTALL` | ✅ `uninstall_host`(win + apk) | Android 新版拉起系统卸载确认框,需人工确认 |
| 远程交谈 | `COMMAND_CHAT` | ✅ `chat_*` | — |
| 远程交谈 | `COMMAND_CHAT` | ✅ `chat_*`(win + apk) | Android 新版弹应用内聊天窗口(能力位 `CLIENT_CAP_REMOTE_CHAT`),老 APK 返回 `-32005` |
| **客户端升级** | `COMMAND_UPDATE` | ❌ | 零客户端改动,需独立评审(见 §4.4) |
| **上传执行 / 下载执行** | `COMMAND_UPLOAD_EXEC` / `COMMAND_DOWN_EXEC` | ❌ | 零客户端改动,需独立评审(见 §4.4) |
| **代理映射** | `COMMAND_PROXY` | ❌ | 需独立设计(见 §4.3) |
+10 -6
View File
@@ -6066,10 +6066,11 @@
},
{
"name": "chat_open",
"description": "在指定在线 Windows 主机上弹出「远程交谈」窗口,建立一次多轮文字对话。返回 session_id,后续 chat_send / chat_wait / chat_close 都要带上。客户端使用者会直接看到聊天窗口(无确认框)。同一主机同一时刻只允许一个会话;会话在客户端断开或空闲 10 分钟后自动失效。会话建立后应先用 chat_send 发送问候,再用 chat_wait 等待对方回复。",
"description": "在指定在线 Windows / Android 主机上弹出「远程交谈」窗口,建立一次多轮文字对话。返回 session_id,后续 chat_send / chat_wait / chat_close 都要带上。客户端使用者会直接看到聊天窗口(无确认框;Android 上是应用内聊天界面,无障碍服务可用时会自动拉到前台,否则发一条可点开的通知)。同一主机同一时刻只允许一个会话;会话在客户端断开或空闲 10 分钟后自动失效。会话建立后应先用 chat_send 发送问候,再用 chat_wait 等待对方回复。",
"gate": "chat",
"platforms": [
"win"
"win",
"apk"
],
"impl": "mfc",
"inputSchema": {
@@ -6110,10 +6111,11 @@
},
{
"name": "chat_send",
"description": "向会话对端发送一条文本。消息以客户端本地代码页(简体中文为 GBK/CP936)显示;客户端不保证非中文系统能正确显示。发送成功只代表已写入子连接,不代表对方已读。请勿在同一条消息里塞入多段对话;需要等待回复时调用 chat_wait。",
"description": "向会话对端发送一条文本。消息以客户端本地代码页(简体中文为 GBK/CP936)显示(Windows 客户端按 UTF-16 渲染,Android 客户端按 GBK 解码);客户端不保证非中文系统能正确显示。发送成功只代表已写入子连接,不代表对方已读。请勿在同一条消息里塞入多段对话;需要等待回复时调用 chat_wait。",
"gate": "chat",
"platforms": [
"win"
"win",
"apk"
],
"impl": "mfc",
"inputSchema": {
@@ -6166,7 +6168,8 @@
"description": "等待客户端使用者发来的消息。有消息立即返回;没有消息则阻塞到 timeout_ms 或会话关闭。返回 messages(按时间顺序,text 为 UTF-8)、timed_out、closed、dropped(因缓冲区溢出丢弃的最旧消息数)。timed_out=true 只表示这段时间对方没说话,会话仍然有效;closed=true 表示客户端已断开,需要重新 chat_open。一般回复后立刻调用 chat_wait 形成对话循环。",
"gate": "chat",
"platforms": [
"win"
"win",
"apk"
],
"impl": "mfc",
"inputSchema": {
@@ -6243,7 +6246,8 @@
"description": "结束会话并关闭客户端上的聊天窗口。主机在线且会话已不存在时按幂等处理为成功;主机已离线时返回 -32002(此时会话已随断线失效)。关闭后旧的 session_id 不能再用;可立即重新 chat_open。",
"gate": "chat",
"platforms": [
"win"
"win",
"apk"
],
"impl": "mfc",
"inputSchema": {
+22 -11
View File
@@ -85,7 +85,7 @@ struct HostTool {
};
bool ResolveHost(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent,
HostTool& ht, bool requireWindows_, std::string& errJson) {
HostTool& ht, bool requireChatSupported_, std::string& errJson) {
uint64_t dev = 0;
std::string err;
if (!ParseHostIdArg(args, dev, err)) {
@@ -97,10 +97,21 @@ bool ResolveHost(const Json::Value& id, const Json::Value& args, CMy2015RemoteDl
errJson = BuildError(id, kErrOffline, "Host not found or offline: " + std::to_string(dev));
return false;
}
if (requireWindows_ && !IsWindowsClient(ctx)) {
errJson = BuildError(id, kErrUnsupported,
"Remote chat is only supported on Windows hosts");
return false;
// Windows 客户端一直有 COMMAND_CHAT 分支;Android 从支持 CLIENT_CAP_REMOTE_CHAT 的版本
// 才开始弹应用内聊天窗口,老 APK 收到会静默丢弃,提前拒绝比等 15s 超时友好。
if (requireChatSupported_) {
const CString clientType = ctx->GetAdditionalData(RES_CLIENT_TYPE);
if (clientType == "LNX" || clientType == "MAC") {
errJson = BuildError(id, kErrUnsupported,
"Remote chat is only supported on Windows and Android hosts");
return false;
}
if (clientType == "APK" && !ctx->SupportsChat()) {
errJson = BuildError(id, kErrUnsupported,
"Android client is too old for remote chat: "
"the APK has no COMMAND_CHAT handler, upgrade the client first");
return false;
}
}
ht.dev = dev;
ht.mainCtx = ctx;
@@ -237,7 +248,7 @@ void AppendTools(Json::Value& tools) {
props["id"] = HostIdProp();
props["timeout_ms"] = PropInt(u8"等待客户端弹出聊天窗口的超时,默认 15000,范围 2000-60000");
AddTool(tools, "chat_open",
u8"在指定在线 Windows 主机上弹出「远程交谈」窗口,建立一次多轮文字对话。返回 session_id,后续 chat_send / chat_wait / chat_close 都要带上。客户端使用者会直接看到聊天窗口(无确认框)。同一主机同一时刻只允许一个会话;会话在客户端断开或空闲 10 分钟后自动失效。会话建立后应先用 chat_send 发送问候,再用 chat_wait 等待对方回复。",
u8"在指定在线 Windows / Android 主机上弹出「远程交谈」窗口,建立一次多轮文字对话。返回 session_id,后续 chat_send / chat_wait / chat_close 都要带上。客户端使用者会直接看到聊天窗口(无确认框;Android 上是应用内聊天界面,无障碍服务可用时会自动拉到前台,否则发一条可点开的通知)。同一主机同一时刻只允许一个会话;会话在客户端断开或空闲 10 分钟后自动失效。会话建立后应先用 chat_send 发送问候,再用 chat_wait 等待对方回复。",
MakeSchema(props, { "id" }),
OutputSchema([] {
Json::Value p(Json::objectValue);
@@ -255,7 +266,7 @@ void AppendTools(Json::Value& tools) {
props["session_id"] = SessionIdProp();
props["message"] = PropStr(u8"要发给客户端使用者的纯文本(UTF-8)。不能为空、不能含换行/控制字符、不能超过 4000 个 GBK 字节(约 2000 个汉字);emoji 等 GBK 无法表示的字符会被拒绝");
AddTool(tools, "chat_send",
u8"向会话对端发送一条文本。消息以客户端本地代码页(简体中文为 GBK/CP936)显示;客户端不保证非中文系统能正确显示。发送成功只代表已写入子连接,不代表对方已读。请勿在同一条消息里塞入多段对话;需要等待回复时调用 chat_wait。",
u8"向会话对端发送一条文本。消息以客户端本地代码页(简体中文为 GBK/CP936)显示(Windows 客户端按 UTF-16 渲染,Android 客户端按 GBK 解码);客户端不保证非中文系统能正确显示。发送成功只代表已写入子连接,不代表对方已读。请勿在同一条消息里塞入多段对话;需要等待回复时调用 chat_wait。",
MakeSchema(props, { "id", "session_id", "message" }),
OutputSchema([] {
Json::Value p(Json::objectValue);
@@ -322,7 +333,7 @@ static std::string BuildChatOpen(const Json::Value& id, const Json::Value& args,
CMy2015RemoteDlg* parent) {
HostTool ht;
std::string errJson;
if (!ResolveHost(id, args, parent, ht, /*requireWindows_=*/true, errJson)) return errJson;
if (!ResolveHost(id, args, parent, ht, /*requireChatSupported_=*/true, errJson)) return errJson;
int timeoutMs = kOpenDefaultMs;
if (!GetOptIntArg(id, args, "timeout_ms", kOpenMinMs, kOpenMaxMs, timeoutMs, errJson))
@@ -383,7 +394,7 @@ static std::string BuildChatSend(const Json::Value& id, const Json::Value& args,
CMy2015RemoteDlg* parent) {
HostTool ht;
std::string errJson;
if (!ResolveHost(id, args, parent, ht, /*requireWindows_=*/true, errJson)) return errJson;
if (!ResolveHost(id, args, parent, ht, /*requireChatSupported_=*/true, errJson)) return errJson;
const std::string sessionId = OptStr(args, "session_id");
if (sessionId.empty())
@@ -457,7 +468,7 @@ static std::string BuildChatWait(const Json::Value& id, const Json::Value& args,
CMy2015RemoteDlg* parent) {
HostTool ht;
std::string errJson;
if (!ResolveHost(id, args, parent, ht, /*requireWindows_=*/true, errJson)) return errJson;
if (!ResolveHost(id, args, parent, ht, /*requireChatSupported_=*/true, errJson)) return errJson;
const std::string sessionId = OptStr(args, "session_id");
if (sessionId.empty())
@@ -535,7 +546,7 @@ static std::string BuildChatClose(const Json::Value& id, const Json::Value& args
CMy2015RemoteDlg* parent) {
HostTool ht;
std::string errJson;
if (!ResolveHost(id, args, parent, ht, /*requireWindows_=*/true, errJson)) return errJson;
if (!ResolveHost(id, args, parent, ht, /*requireChatSupported_=*/true, errJson)) return errJson;
const std::string sessionId = OptStr(args, "session_id");
if (sessionId.empty())
+7 -5
View File
@@ -351,7 +351,9 @@ const char* kServerInstructions =
u8"输入被翻译成触摸手势与 ESC(返回)/ HOME(桌面)/ APPS(任务列表)/ PRINTSCREEN(截屏)"
u8"等全局动作,只支持整屏目标;remote_lock_screen 用系统的锁屏动作;注入依赖"
u8"无障碍服务、抓屏依赖已授权录屏,两者都没有能力位可查,命令下发成功不代表生效),"
u8"卸载主机(uninstall_host,会拉起系统的卸载确认框,需要有人在设备上点确认),以及 "
u8"卸载主机(uninstall_host,会拉起系统的卸载确认框,需要有人在设备上点确认)、"
u8"远程交谈(chat_open / chat_send / chat_wait / chat_close,聊天界面在 App 内,"
u8"需要声明 CLIENT_CAP_REMOTE_CHAT 的新版 APK,老 APK 返回 -32005),以及 "
u8"host_set_remark / host_set_watch_level 这两个只改服务端本地数据的工具。其余一律不支持"
u8"(客户端未实现 SYSTEM 与 SHELL)。\n"
u8"copy_files_c2c 是唯一有方向性的工具:源端 src_id 必须是 win,目标端 dst_id 可以是 "
@@ -10299,10 +10301,10 @@ const ToolPlatform kToolPlatforms[] = {
{ "host_control_window", kPlatWin },
{ "host_control_service", kPlatWin },
{ "host_control_task", kPlatWin },
{ "chat_open", kPlatWin },
{ "chat_send", kPlatWin },
{ "chat_wait", kPlatWin },
{ "chat_close", kPlatWin },
{ "chat_open", kPlatWin|kPlatApk },
{ "chat_send", kPlatWin|kPlatApk },
{ "chat_wait", kPlatWin|kPlatApk },
{ "chat_close", kPlatWin|kPlatApk },
};
// 未知的 clientType 一律按 Windows:EXE/DLL/MEXE/SC/MDLL 都走这个分支,
+8
View File
@@ -155,6 +155,14 @@ public:
return (strtoul(caps.GetString(), nullptr, 16) & CLIENT_CAP_UNINSTALL) != 0;
}
// 检查客户端是否处理 COMMAND_CHAT(远程交谈子连接)。Windows 客户端一直有该分支但
// 不声明能力位,调用方按平台区分;只有新版 Android 声明,老 APK 收到会静默丢弃。
bool SupportsChat() const {
CString caps = GetClientData(ONLINELIST_CAPABILITIES);
if (caps.IsEmpty()) return false;
return (strtoul(caps.GetString(), nullptr, 16) & CLIENT_CAP_REMOTE_CHAT) != 0;
}
// 检查客户端是否支持远程协议切换(COMMAND_SET_PROTO)。老客户端没有这个命令,
// 收到也会走 default 丢弃,服务端据此把「协议切换」菜单置灰。
bool SupportsProtoSwitch() const {
+6 -6
View File
@@ -115,7 +115,7 @@ GATE = {
# win/lnx/mac —— client/、linux/、macos/ 都有该命令的处理分支,且服务端不拦
# win/lnx —— 远程控制只实现于 Windows/Linux(McpServer.cpp:2728);
# 以及 macOS 客户端有实现但服务端未开放的 exec_command / terminal_* / kill_process
# 仅 win —— 服务/注册表/键日志/活动历史/文件写/块输入/host_* /chat_* /power_action
# 仅 win —— 服务/注册表/键日志/活动历史/文件写/块输入/host_* /power_action
# (power_action 实际走 TOKEN_MACHINE_MANAGE,不是通用电源命令)
#
# 支持面是产品定位的阶梯:win 100% > lnx 46% > mac 28% ≈ apk 16%。
@@ -222,11 +222,11 @@ PLATFORMS = {
"host_control_service": _WIN,
"host_control_task": _WIN,
# McpChat 族(COMMAND_TALK 仅 Windows,代码里另有 requireWindows_ 硬校验)
"chat_open": _WIN,
"chat_send": _WIN,
"chat_wait": _WIN,
"chat_close": _WIN,
# McpChat 族(Windows 一直支持;新版 Android 声明 CLIENT_CAP_REMOTE_CHAT 后也可用)
"chat_open": _WINAPK,
"chat_send": _WINAPK,
"chat_wait": _WINAPK,
"chat_close": _WINAPK,
}
# roles: 多主机工具的方向性要求。