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:
1 parent
7172ab376d
commit
28c0c9a2a6
9 files changed
+1001
-19
No files matched your search
+35
-4
@@ -32,7 +32,15 @@
|
||||
#include <sys/mount.h> // macOS: statfs for filesystem type
|
||||
#endif
|
||||
|
||||
#ifdef __ANDROID__
|
||||
// Android NDK 的 iconv 从 API 28 才提供,而 Android 客户端的 minSdk 是 21。
|
||||
// GBK 转换改走 Java 的 Charset(JNI),实现在 android/app/src/main/cpp/main.cpp。
|
||||
// 失败时与原 iconv 版本一致:原样返回输入(服务端对 GBK 表示不了的字符会直传 UTF-8)。
|
||||
std::string AndroidGbkToUtf8(const std::string& input);
|
||||
std::string AndroidUtf8ToGbk(const std::string& input);
|
||||
#else
|
||||
#include <iconv.h> // Character encoding conversion (GBK <-> UTF-8)
|
||||
#endif
|
||||
|
||||
// FileTransferV2 is in the same directory (common/)
|
||||
#include "FileTransferV2.h"
|
||||
@@ -47,6 +55,12 @@ class FileManager : public IOCPManager
|
||||
{
|
||||
public:
|
||||
FileManager(IOCPClient* client)
|
||||
: FileManager(client, false) {}
|
||||
|
||||
// deferredStart=true:不在构造里发 TOKEN_DRIVE_LIST,由子类准备好平台数据后
|
||||
// 自己调用 SendDriveList()。Android 用它——存储根要先问 Java 要,而构造期
|
||||
// 的虚函数不会分派到子类,直接发出去的是 Linux 的 "/" 根。
|
||||
FileManager(IOCPClient* client, bool deferredStart)
|
||||
: m_client(client),
|
||||
m_nTransferMode(TRANSFER_MODE_NORMAL),
|
||||
m_nCurrentFileLength(0),
|
||||
@@ -54,7 +68,7 @@ public:
|
||||
{
|
||||
memset(m_strCurrentFileName, 0, sizeof(m_strCurrentFileName));
|
||||
memset(m_strCurrentUploadFile, 0, sizeof(m_strCurrentUploadFile));
|
||||
SendDriveList();
|
||||
if (!deferredStart) SendDriveList();
|
||||
}
|
||||
|
||||
~FileManager()
|
||||
@@ -148,6 +162,7 @@ private:
|
||||
m_client->Send2Server((char*)&token, 1);
|
||||
}
|
||||
|
||||
#ifndef __ANDROID__
|
||||
// ---- iconv encoding conversion ----
|
||||
static std::string iconvConvert(const std::string& input, const char* from, const char* to)
|
||||
{
|
||||
@@ -169,17 +184,28 @@ private:
|
||||
output.resize(output.size() - outLeft);
|
||||
return output;
|
||||
}
|
||||
#endif
|
||||
|
||||
protected:
|
||||
static std::string gbkToUtf8(const std::string& gbk)
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
return AndroidGbkToUtf8(gbk);
|
||||
#else
|
||||
return iconvConvert(gbk, "GBK", "UTF-8");
|
||||
#endif
|
||||
}
|
||||
|
||||
static std::string utf8ToGbk(const std::string& utf8)
|
||||
{
|
||||
#ifdef __ANDROID__
|
||||
return AndroidUtf8ToGbk(utf8);
|
||||
#else
|
||||
return iconvConvert(utf8, "UTF-8", "GBK");
|
||||
#endif
|
||||
}
|
||||
|
||||
protected:
|
||||
// ---- Windows path -> Linux path ----
|
||||
// Examples:
|
||||
// /:\home\user\file.txt -> /home/user/file.txt
|
||||
@@ -188,7 +214,8 @@ private:
|
||||
// /: -> /
|
||||
// C:\Users\file.txt -> /Users/file.txt
|
||||
// %USERPROFILE%\Desktop\ -> /home/user/Desktop/ (or the XDG desktop folder)
|
||||
static std::string winPathToLinux(const char* winPath)
|
||||
// Android 覆盖它,把驱动字母映射到 /storage/... 下的真实路径。
|
||||
virtual std::string winPathToLinux(const char* winPath)
|
||||
{
|
||||
// 展开 %USERPROFILE% 等变量并把路径风格转成 Unix(见 UnixPath.h)
|
||||
return UnixPath::ToUnixPath(winPath ? winPath : "");
|
||||
@@ -196,7 +223,7 @@ private:
|
||||
|
||||
// ---- Linux path -> Windows path (for sending to server) ----
|
||||
// Converts /home/user/file.txt to /:\home\user\file.txt
|
||||
static std::string linuxPathToWin(const std::string& linuxPath)
|
||||
virtual std::string linuxPathToWin(const std::string& linuxPath)
|
||||
{
|
||||
// Start with drive letter representation /: for Linux root
|
||||
// Linux path /home/user/file.txt should become /:\home\user\file.txt
|
||||
@@ -290,8 +317,12 @@ private:
|
||||
return true;
|
||||
}
|
||||
|
||||
// 子类(Android)覆盖 SendDriveList 时要用到连接对象;m_client 是私有的。
|
||||
IOCPClient* ClientObject() const { return m_client; }
|
||||
|
||||
// ---- Send drive list (Linux: root partition /) ----
|
||||
void SendDriveList()
|
||||
// Android 覆盖它:把内部存储 / SD 卡 / 应用目录当成带盘符的「驱动器」。
|
||||
virtual void SendDriveList()
|
||||
{
|
||||
if (!m_client) return;
|
||||
|
||||
|
||||
Reference in new issue
Block a user