diff --git a/frameworks/innerkits/file_access/BUILD.gn b/frameworks/innerkits/file_access/BUILD.gn index 78e238b3539209392d8e3e68e123e4625c966ddb..838f2cf703c3a0c3d0b99df2887aa8df848e30eb 100644 --- a/frameworks/innerkits/file_access/BUILD.gn +++ b/frameworks/innerkits/file_access/BUILD.gn @@ -92,6 +92,7 @@ ohos_shared_library("file_access_extension_ability_kit") { "hitrace_native:hitrace_meter", "ipc:ipc_core", "ipc_js:rpc", + "os_account:os_account_innerkits", "samgr:samgr_proxy", ] diff --git a/frameworks/innerkits/file_access/include/file_access_helper.h b/frameworks/innerkits/file_access/include/file_access_helper.h index 436ea33345714e1e457524e0d1f0bd0f062629f9..c592522be867e72b59e61fe286d35434344b4fa8 100644 --- a/frameworks/innerkits/file_access/include/file_access_helper.h +++ b/frameworks/innerkits/file_access/include/file_access_helper.h @@ -93,6 +93,7 @@ private: static std::unordered_map wantsMap_; static std::string GetKeyOfWantsMap(const AAFwk::Want &want); + static int GetUserId(int &userId); sptr callerDeathRecipient_ = nullptr; diff --git a/frameworks/innerkits/file_access/src/file_access_helper.cpp b/frameworks/innerkits/file_access/src/file_access_helper.cpp index feb430645041f8e25d30756d4cfe63f2eccade5d..adf3cc780c7be882bceab7c30a8972901575fa5a 100644 --- a/frameworks/innerkits/file_access/src/file_access_helper.cpp +++ b/frameworks/innerkits/file_access/src/file_access_helper.cpp @@ -22,12 +22,12 @@ #include "if_system_ability_manager.h" #include "ifile_access_ext_base.h" #include "iservice_registry.h" +#include "os_account_manager.h" #include "system_ability_definition.h" namespace OHOS { namespace FileAccessFwk { namespace { - static const int32_t DEFAULT_USERID = 100; static const std::string SCHEME_NAME = "datashare"; static const std::string MEDIA_BNUDLE_NAME_ALIAS = "media"; static const std::string MEDIA_BNUDLE_NAME = "com.ohos.medialibrary.medialibrarydata"; @@ -153,6 +153,23 @@ std::string FileAccessHelper::GetKeyOfWantsMap(const AAFwk::Want &want) return nullptr; } +int FileAccessHelper::GetUserId(int &userId) +{ + std::vector activeIds; + int ret = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeIds); + if (ret != ERR_OK) { + HILOG_ERROR("Failed to get UserID"); + return ERR_INVALID_PARAM; + } + + if (activeIds.size() == 0) { + return ERR_INVALID_PARAM; + } + + userId = activeIds[0]; + return ERR_OK; +} + void FileAccessHelper::InsertConnectInfo(const std::string &key, const AAFwk::Want &want, const sptr &fileAccessExtProxy, @@ -186,8 +203,14 @@ std::shared_ptr FileAccessHelper::Creator( FileAccessHelper::wantsMap_.clear(); std::unordered_map> cMap; std::vector extensionInfos; + + int userId = 0; + if (FileAccessHelper::GetUserId(userId) != ERR_OK) { + return nullptr; + } + bool ret = bm->QueryExtensionAbilityInfos( - AppExecFwk::ExtensionAbilityType::FILEACCESS_EXTENSION, DEFAULT_USERID, extensionInfos); + AppExecFwk::ExtensionAbilityType::FILEACCESS_EXTENSION, userId, extensionInfos); if (!ret) { HILOG_ERROR("FileAccessHelper::Creator QueryExtensionAbilityInfos failed"); return nullptr; @@ -589,8 +612,14 @@ std::vector FileAccessHelper::GetRegisterFileAccessExtAbilityInfo() std::vector wants; std::vector extensionInfos; sptr bm = FileAccessHelper::GetBundleMgrProxy(); + + int userId = 0; + if (FileAccessHelper::GetUserId(userId) != ERR_OK) { + return wants; + } + bool ret = bm->QueryExtensionAbilityInfos( - AppExecFwk::ExtensionAbilityType::FILEACCESS_EXTENSION, DEFAULT_USERID, extensionInfos); + AppExecFwk::ExtensionAbilityType::FILEACCESS_EXTENSION, userId, extensionInfos); if (!ret) { HILOG_ERROR("FileAccessHelper::GetRegisterFileAccessExtAbilityInfo QueryExtensionAbilityInfos error"); return wants;