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;
|
||||
|
||||
|
||||
+37
-1
@@ -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 码分类
|
||||
|
||||
Reference in new issue
Block a user