diff --git a/bundle.json b/bundle.json index b0686c2bce991e4d94b3468fdc43340a1cb211a8..3e44277c06756ab038f2df60d65bcd4d5e228340 100644 --- a/bundle.json +++ b/bundle.json @@ -26,6 +26,7 @@ "ability_runtime", "app_file_service", "ipc", + "init", "samgr", "safwk", "napi", @@ -34,6 +35,7 @@ "hilog", "hitrace", "access_token", + "os_account", "c_utils", "image_framework", "common_event_service" diff --git a/interfaces/inner_api/file_access/BUILD.gn b/interfaces/inner_api/file_access/BUILD.gn index e5b3a3378cd631f385e02a485a9eb722c8c1519f..d459c3fafc36040344bad365f9ae71cf250ea8b2 100644 --- a/interfaces/inner_api/file_access/BUILD.gn +++ b/interfaces/inner_api/file_access/BUILD.gn @@ -96,8 +96,10 @@ ohos_shared_library("file_access_extension_ability_kit") { "hilog:libhilog", "hitrace:hitrace_meter", "image_framework:image_native", + "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", + "os_account:os_account_innerkits", "samgr:samgr_proxy", ] diff --git a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp index d399f10aeb33c9e3624773fd4b8b885707321bf6..ce7949b6d66c129b448031a7f0333e8e530db27c 100644 --- a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp @@ -36,6 +36,8 @@ #include "napi_common_util.h" #include "napi_common_want.h" #include "napi_remote_object.h" +#include "os_account_manager.h" +#include "parameter.h" #include "system_ability_definition.h" #include "user_access_tracer.h" @@ -1046,6 +1048,44 @@ int JsFileAccessExtAbility::ScanFile(const FileInfo &fileInfo, const int64_t off return ERR_OK; } +bool GetDeviceType(std::string deviceType) +{ + char deviceTypeChar[PARAM_CONST_VALUE_LEN_MAX]; + int32_t ret = GetParameter("const.product.devicetype", "0", deviceTypeChar, PARAM_CONST_VALUE_LEN_MAX); + if(ret < 0) { + HILOG_ERROR("Get deviceType fail. %{public}d", ret); + return false; + } + deviceType = deviceTypeChar; + return true; +} + +bool GetUserName(std::string userName) +{ + ErrCode errCode = OHOS::AccountSA::OsAccountManager::GetOsAccountShortName(userName); + if (errCode != ERR_OK) { + return false; + } + return true; +} + +void ChangeCurrentDir(RootInfo &rootInfo) +{ + // 获取用户名 + std::string userName; + if(!GetUserName(userName)) { + return ; + } + if(rootInfo.uri.rfind("file://docs/storage/Users/currentUser") == 0) { + rootInfo.uri = "file://docs/storage/Users/" + userName; + HILOG_INFO("rootInfo.uri: %{public}s", rootInfo.uri.c_str()); + } + if(rootInfo.relativePath.rfind("/storage/Users/currentUser") == 0) { + rootInfo.relativePath = "/storage/Users/" + userName; + HILOG_INFO("rootInfo.relativePath: %{public}s", rootInfo.relativePath.c_str()); + } +} + bool JsFileAccessExtAbility::ParserGetRootsJsResult(napi_env &env, napi_value nativeValue, Value> &result) { @@ -1070,6 +1110,9 @@ bool JsFileAccessExtAbility::ParserGetRootsJsResult(napi_env &env, napi_value na return false; } + std::string deviceType; + bool deviceResult = GetDeviceType(deviceType); + HILOG_INFO("deviceType: %{public}s", deviceType.c_str()); for (uint32_t i = 0; i < length; i++) { napi_value nativeRootInfo = nullptr; napi_get_element(env, nativeArray, i, &nativeRootInfo); @@ -1084,6 +1127,9 @@ bool JsFileAccessExtAbility::ParserGetRootsJsResult(napi_env &env, napi_value na return false; } + if (deviceResult && deviceType == "2in1") { + ChangeCurrentDir(rootInfo); + } result.data.emplace_back(std::move(rootInfo)); } return true;