Files
SimpleRemoter/android/app/src/main/java/com/yama/client/ChatActivity.kt
T
yuanyuanxianganddeepseek-flash 915c587964 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
2026-11-11 21:58:44 +01:00

131 lines
5.0 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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("")
}
}