From 915c58796473ab900babcc94879fe65b28ceccfd Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Wed, 11 Nov 2026 15:14:36 +0100 Subject: [PATCH] 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 --- android/app/src/main/AndroidManifest.xml | 6 + android/app/src/main/cpp/ChatHandler.h | 66 ++++++ android/app/src/main/cpp/main.cpp | 162 ++++++++++++++ .../main/java/com/yama/client/ChatActivity.kt | 130 +++++++++++ .../main/java/com/yama/client/ChatSession.kt | 202 ++++++++++++++++++ .../main/java/com/yama/client/YamaBridge.kt | 6 + .../app/src/main/res/layout/activity_chat.xml | 66 ++++++ common/commands.h | 3 + docs/Mcp_Next_Plan.md | 2 +- mcp/tools.json | 16 +- server/2015Remote/McpChat.cpp | 33 ++- server/2015Remote/McpServer.cpp | 12 +- server/2015Remote/context.h | 8 + tools/mcp_tools/make_tools_json.py | 12 +- 14 files changed, 695 insertions(+), 29 deletions(-) create mode 100644 android/app/src/main/cpp/ChatHandler.h create mode 100644 android/app/src/main/java/com/yama/client/ChatActivity.kt create mode 100644 android/app/src/main/java/com/yama/client/ChatSession.kt create mode 100644 android/app/src/main/res/layout/activity_chat.xml diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 5c563a5..7c0d415 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -31,6 +31,12 @@ + + + +#include + +// 定义在 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 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; +}; diff --git a/android/app/src/main/cpp/main.cpp b/android/app/src/main/cpp/main.cpp index 80aaa75..2c6cffc 100644 --- a/android/app/src/main/cpp/main.cpp +++ b/android/app/src/main/cpp/main.cpp @@ -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 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 client; + std::unique_ptr handler; + try { + client = std::make_unique(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(client.get()); + client->setManagerCallBack(handler.get(), + IOCPManager::DataProcess, + IOCPManager::ReconnectProcess); + { + std::lock_guard 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 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 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 buf((size_t)len); + env->GetByteArrayRegion(data, 0, len, (jbyte*)buf.data()); + if (env->ExceptionCheck()) { env->ExceptionClear(); return JNI_FALSE; } + std::lock_guard 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) diff --git a/android/app/src/main/java/com/yama/client/ChatActivity.kt b/android/app/src/main/java/com/yama/client/ChatActivity.kt new file mode 100644 index 0000000..1aeb893 --- /dev/null +++ b/android/app/src/main/java/com/yama/client/ChatActivity.kt @@ -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(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("") + } +} diff --git a/android/app/src/main/java/com/yama/client/ChatSession.kt b/android/app/src/main/java/com/yama/client/ChatSession.kt new file mode 100644 index 0000000..e73974d --- /dev/null +++ b/android/app/src/main/java/com/yama/client/ChatSession.kt @@ -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() + 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 = 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()) } + } +} diff --git a/android/app/src/main/java/com/yama/client/YamaBridge.kt b/android/app/src/main/java/com/yama/client/YamaBridge.kt index 283732f..710d361 100644 --- a/android/app/src/main/java/com/yama/client/YamaBridge.kt +++ b/android/app/src/main/java/com/yama/client/YamaBridge.kt @@ -29,4 +29,10 @@ object YamaBridge { /** 把一行文本回报到服务端消息栏(TOKEN_CLIENT_MSG),用于升级流程的成败说明 */ external fun nativeReportMessage(msg: String) + + /** + * 远程交谈:发送一条已按 GBK 编码的消息(不含结尾 NUL,native 补)。 + * 没有活跃的交谈子连接时返回 false。 + */ + external fun nativeChatSend(data: ByteArray): Boolean } diff --git a/android/app/src/main/res/layout/activity_chat.xml b/android/app/src/main/res/layout/activity_chat.xml new file mode 100644 index 0000000..d9b1837 --- /dev/null +++ b/android/app/src/main/res/layout/activity_chat.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + +