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

@@ -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
}