Feature: add file management to the Android client

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>
This commit is contained in:
yuanyuanxiangandClaude Code committed 2026-11-16 18:34:46 +01:00
1 parent 7172ab376d
commit 28c0c9a2a6
9 files changed
+1001 -19

No files matched your search

+37 -1
View File
@@ -24,7 +24,9 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#ifndef __ANDROID__
#include <iconv.h>
#endif
#include <cstring>
#include <cctype>
#include <cerrno>
@@ -35,8 +37,26 @@
#include <set>
#include <mutex>
#include <fstream>
#include <functional>
#include <thread>
namespace filetransferv2_path {
// 路径映射钩子:Android 的存储是盘符模型(I:\...),不能套 UnixPath 的 "/:" 语义。
// 用 inline 函数的局部 static,保证多个 TU 共享同一个实例(C++17)。
inline std::function<std::string(const std::string&)>& Mapper()
{
static std::function<std::string(const std::string&)> mapper;
return mapper;
}
} // namespace filetransferv2_path
#ifdef __ANDROID__
// Android NDK 的 iconv 从 API 28 才提供(客户端 minSdk 21),GBK 转换走 Java Charset。
// 实现在 android/app/src/main/cpp/main.cpp;失败时原样返回输入(与 iconv 版一致)。
std::string AndroidGbkToUtf8(const std::string& input);
std::string AndroidUtf8ToGbk(const std::string& input);
#endif
class FileTransferV2
{
public:
@@ -56,6 +76,9 @@ public:
static std::string gbkToUtf8(const std::string& gbk)
{
if (gbk.empty()) return gbk;
#ifdef __ANDROID__
return AndroidGbkToUtf8(gbk);
#else
iconv_t cd = iconv_open("UTF-8", "GBK");
if (cd == (iconv_t)-1) return gbk;
@@ -72,11 +95,15 @@ public:
if (ret == (size_t)-1) return gbk;
output.resize(output.size() - outLeft);
return output;
#endif
}
static std::string utf8ToGbk(const std::string& utf8)
{
if (utf8.empty()) return utf8;
#ifdef __ANDROID__
return AndroidUtf8ToGbk(utf8);
#else
iconv_t cd = iconv_open("GBK", "UTF-8");
if (cd == (iconv_t)-1) return utf8;
@@ -93,6 +120,7 @@ public:
if (ret == (size_t)-1) return utf8;
output.resize(output.size() - outLeft);
return output;
#endif
}
// 递归创建目录
@@ -430,11 +458,19 @@ public:
closedir(d);
}
// 平台路径映射钩子:由客户端启动时注入(Android 用盘符映射;其它平台不设,
// 保持 UnixPath 的原有语义)。只在单线程初始化阶段调用,不要求线程安全。
static void SetPathMapper(std::function<std::string(const std::string&)> mapper)
{
filetransferv2_path::Mapper() = std::move(mapper);
}
// Windows 路径转 Linux 路径。服务端对话框的快捷按钮(桌面/下载/用户主页)下发的是
// %USERPROFILE%\Desktop\ 这类 Windows 变量路径,必须先展开(见 UnixPath.h)。
static std::string WinPathToLinux(const std::string& winPath)
{
return UnixPath::ToUnixPath(winPath);
auto& mapper = filetransferv2_path::Mapper();
return mapper ? mapper(winPath) : UnixPath::ToUnixPath(winPath);
}
// errno -> Win32 错误码:服务端 TOKEN_DOWNLOAD_ERROR 按 Win32 码分类