Android 客户端此前没有文件管理:声明了 CLIENT_CAP_V2 却没有实现,主控的 文件管理对话框和 MCP 的 list_files/download_file/upload_file 对 APK 只会 超时。本次复用 Linux/macOS 已在用的 common/FileManager.h,只补平台差异: - 盘符模型:P=应用私有目录、I=内部存储、S/T/...=SD 卡与 U 盘;V2 上传 通过 FileTransferV2::SetPathMapper 复用同一套映射。 - 文件通道线上是 GBK。NDK 的 iconv 要 API 28,客户端 minSdk 是 21,改走 JNI -> Java Charset(NativeText.kt),严格模式失败原样透传。 - Android 11+ 引导开启「所有文件访问」;未授权时只挂 P: 并回一条提示。 API <=29 用运行时存储权限 + requestLegacyExternalStorage。 - 顺带实现 COMMAND_SEARCH_FILE(深度 32、1 万条),并让 MCP 的 list_files/download_file/upload_file/read_file 对 APK 开闸。 验证:arm64/armeabi 与模拟器 ABI 编译通过;模拟器上用 MCP 完成列盘、 列目录、下载、上传、中文文件名往返,GUI 文件管理器正常显示 P:/I:/S:。 Model: deepseek-flash Co-Authored-By: Claude Code <noreply@anthropic.com>
58 lines
2.2 KiB
Kotlin
58 lines
2.2 KiB
Kotlin
package com.yama.client
|
||
|
||
import java.nio.ByteBuffer
|
||
import java.nio.charset.Charset
|
||
import java.nio.charset.CodingErrorAction
|
||
|
||
/**
|
||
* 文件管理通道的 GBK ↔ UTF-8 转换。
|
||
*
|
||
* 线上协议(与服务端 FileManagerDlg / MCP 的文件通道一致)是 GBK/ANSI:服务端按
|
||
* CP936 下发路径、按 CP936 解析客户端上报的文件名。Android 的文件系统路径是
|
||
* UTF-8,所以两端都要转一次。
|
||
*
|
||
* 为什么不在 native 用 iconv:NDK 的 iconv 从 API 28 才提供,客户端 minSdk 是 21,
|
||
* 低版本设备上链接/加载会失败。
|
||
*
|
||
* 严格模式:GBK 转换失败(例如路径里含 GBK 表示不了的字符)时**原样返回输入**,
|
||
* 与 Linux/macOS 客户端的 iconvConvert 行为一致;服务端对这类路径本来就会直传
|
||
* UTF-8 字节(见 McpServer.cpp 里 list_files 的注释),两边才能对上。
|
||
*/
|
||
object NativeText {
|
||
private val GBK: Charset = Charset.forName("GBK")
|
||
|
||
@JvmStatic
|
||
fun gbkToUtf8(bytes: ByteArray): ByteArray {
|
||
if (bytes.isEmpty()) return bytes
|
||
return try {
|
||
val dec = GBK.newDecoder()
|
||
.onMalformedInput(CodingErrorAction.REPORT)
|
||
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
||
val text = dec.decode(ByteBuffer.wrap(bytes)).toString()
|
||
text.toByteArray(Charsets.UTF_8)
|
||
} catch (e: Exception) {
|
||
bytes
|
||
}
|
||
}
|
||
|
||
@JvmStatic
|
||
fun utf8ToGbk(bytes: ByteArray): ByteArray {
|
||
if (bytes.isEmpty()) return bytes
|
||
return try {
|
||
val dec = Charsets.UTF_8.newDecoder()
|
||
.onMalformedInput(CodingErrorAction.REPORT)
|
||
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
||
val text = dec.decode(ByteBuffer.wrap(bytes)).toString()
|
||
val enc = GBK.newEncoder()
|
||
.onMalformedInput(CodingErrorAction.REPORT)
|
||
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
||
val buf = enc.encode(java.nio.CharBuffer.wrap(text))
|
||
val out = ByteArray(buf.remaining())
|
||
buf.get(out)
|
||
out
|
||
} catch (e: Exception) {
|
||
bytes
|
||
}
|
||
}
|
||
}
|