other_icons下图标支持缓存

Signed-off-by: liule <liule21@huawei.com>
This commit is contained in:
liule 2024-08-22 15:51:31 +08:00
parent fdd5f5f5f4
commit 89c54d9fca
10 changed files with 258 additions and 0 deletions

View File

@ -648,6 +648,7 @@ public:
* @param foregroundInfo the foreground info
* @param backgroundInfo the background info
* @param density the drawable density
* @param abilityName the hap abilityName
* @return SUCCESS if resource exist, else not found
*/
virtual RState GetThemeIcons(uint32_t resId, std::pair<std::unique_ptr<uint8_t[]>, size_t> &foregroundInfo,
@ -661,6 +662,26 @@ public:
*/
virtual std::string GetThemeMask();
/**
* Whether an icon exists in the theme
*
* @param bundleName the hap bundleName
* @return true if icon exists, else no exists
*/
virtual bool HasIconInTheme(const std::string &bundleName);
/**
* Get icons info in other icons by icon name
*
* @param iconName the icon name
* @param outValue the obtain resource wirte to
* @param len the data len wirte to
* @param isGlobalMask true if the global mask, else other icons
* @return SUCCESS if the theme icon get success, else failed
*/
virtual RState GetOtherIconsInfo(const std::string &iconName,
std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask);
/**
* Whether this raw resource is a directory
*

View File

@ -101,6 +101,7 @@ public:
*
* @param bundleInfo which contains bundlename, modulename
* @param iconName the icon resource name
* @param abilityName the hap abilityName
* @return the best resource or empty
*/
const std::string FindThemeIconResource(const std::pair<std::string, std::string> &bundleInfo,
@ -116,6 +117,36 @@ public:
bool UpdateThemeId(uint32_t newThemeId);
bool IsFirstLoadResource();
/**
* Whether an icon exists in the theme
*
* @param bundleName the hap bundleName
* @return true if icon exists, else no exists
*/
bool HasIconInTheme(const std::string &bundleName);
/**
* Get icons info in other icons by icon name
*
* @param iconName the icon name
* @param outValue the obtain resource wirte to
* @param len the data len wirte to
* @param isGlobalMask true if the global mask, else other icons
* @return SUCCESS if the theme icon get success, else failed
*/
RState GetOtherIconsInfo(const std::string &iconName,
std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask);
/**
* Get the theme icon from cache
*
* @param iconTag the tag of icon info
* @param outValue the obtain resource wirte to
* @param len the data len wirte to
* @return SUCCESS if the theme icon get success, else failed
*/
RState GetThemeIconFromCache(const std::string &iconTag, std::unique_ptr<uint8_t[]> &outValue, size_t &len);
private:
ThemePackManager();
std::string themeMask;
@ -123,6 +154,7 @@ private:
void ClearIconResource();
std::vector<std::shared_ptr<ThemeResource>> skinResource_;
std::vector<std::shared_ptr<ThemeResource>> iconResource_;
std::vector<std::tuple<std::string, std::unique_ptr<uint8_t[]>, size_t>> iconMaskValues_;
std::vector<std::shared_ptr<ThemeResource::ThemeValue> > GetThemeResourceList(
const std::pair<std::string, std::string> &bundInfo, const ResType &resType, const std::string &resName);
@ -138,6 +170,7 @@ private:
Lock lockSkin_;
Lock lockIcon_;
Lock lockThemeId_;
Lock lockIconValue_;
uint32_t themeId_{0};
bool isFirstCreate = true;
};

View File

@ -120,6 +120,7 @@ public:
*
* @param bundleInfo which contains bundlename, modulename
* @param name the icon name
* @param abilityName the hap abilityName
* @return the icon path
*/
const std::string GetThemeAppIcon(const std::pair<std::string, std::string> &bundleInfo, const std::string &name,
@ -135,6 +136,14 @@ public:
*/
std::string GetThemeResBundleName(const std::string &themePath);
/**
* Whether an icon exists in the theme
*
* @param bundleName the hap bundleName
* @return true if icon exists, else no exists
*/
bool HasIconInTheme(const std::string &bundleName);
inline std::string GetThemePath()
{
return themePath_;

View File

@ -1668,6 +1668,27 @@ std::string ResourceManagerImpl::GetThemeMask()
return ThemePackManager::GetThemePackManager()->GetMask();
}
bool ResourceManagerImpl::HasIconInTheme(const std::string &bundleName)
{
return ThemePackManager::GetThemePackManager()->HasIconInTheme(bundleName);
}
RState ResourceManagerImpl::GetOtherIconsInfo(const std::string &iconName,
std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask)
{
std::string iconTag;
if (iconName.find("icon_mask") != std::string::npos && isGlobalMask) {
iconTag = "global_" + iconName;
} else {
iconTag = "other_icons_" + iconName;
}
RState result = ThemePackManager::GetThemePackManager()->GetThemeIconFromCache(iconTag, outValue, len);
if (result == SUCCESS) {
return SUCCESS;
}
return ThemePackManager::GetThemePackManager()->GetOtherIconsInfo(iconName, outValue, len, isGlobalMask);
}
RState ResourceManagerImpl::IsRawDirFromHap(const std::string &pathName, bool &outValue)
{
return hapManager_->IsRawDirFromHap(pathName, outValue);

View File

@ -21,11 +21,15 @@
#include <cstring>
#include "hilog_wrapper.h"
#include "theme_pack_resource.h"
#include <securec.h>
#include "utils/utils.h"
namespace OHOS {
namespace Global {
namespace Resource {
constexpr int FIRST_ELEMENT = 0;
constexpr int SECOND_ELEMENT = 1;
constexpr int THIRED_ELEMENT = 2;
static std::shared_ptr<ThemePackManager> themeMgr = nullptr;
static std::once_flag themeMgrFlag;
const std::string sysResIdPreFix = "125";
@ -48,6 +52,7 @@ ThemePackManager::~ThemePackManager()
{
skinResource_.clear();
iconResource_.clear();
iconMaskValues_.clear();
}
std::shared_ptr<ThemePackManager> ThemePackManager::GetThemePackManager()
@ -281,6 +286,7 @@ void ThemePackManager::ClearIconResource()
++it;
}
}
iconMaskValues_.clear();
}
void ThemePackManager::LoadThemeIconsResource(const std::string &bundleName, const std::string &moduleName,
@ -349,6 +355,86 @@ bool ThemePackManager::IsFirstLoadResource()
}
return false;
}
bool ThemePackManager::HasIconInTheme(const std::string &bundleName)
{
AutoMutex mutex(this->lockIcon_);
bool result = false;
for (size_t i = 0; i < iconResource_.size(); i++) {
auto pThemeResource = iconResource_[i];
if (pThemeResource == nullptr) {
continue;
}
result = pThemeResource->HasIconInTheme(bundleName);
if (result) {
break;
}
}
return result;
}
RState ThemePackManager::GetOtherIconsInfo(const std::string &iconName,
std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask)
{
AutoMutex mutex(this->lockIconValue_);
std::string iconPath;
std::string iconTag;
if (iconName.find("icon_mask") != std::string::npos && isGlobalMask) {
iconPath = themeMask;
iconTag = "global_" + iconName;
} else {
std::pair<std::string, std::string> bundleInfo;
bundleInfo.first = "other_icons";
iconPath = FindThemeIconResource(bundleInfo, iconName);
iconTag = "other_icons_" + iconName;
}
if (iconPath.empty()) {
RESMGR_HILOGE(RESMGR_TAG, "no found, iconName = %{public}s", iconName.c_str());
return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
}
outValue = Utils::LoadResourceFile(iconPath, len);
if (outValue != nullptr && len != 0) {
auto tmpInfo = std::make_unique<uint8_t[]>(len);
errno_t ret = memcpy_s(tmpInfo.get(), len, outValue.get(), len);
if (ret != 0) {
RESMGR_HILOGE(RESMGR_TAG, "save fail, iconName = %{public}s, ret = %{public}d", iconName.c_str(), ret);
return SUCCESS;
}
iconMaskValues_.emplace_back(std::make_tuple(iconTag, std::move(tmpInfo), len));
return SUCCESS;
}
return ERROR_CODE_RES_NOT_FOUND_BY_NAME;
}
RState ThemePackManager::GetThemeIconFromCache(
const std::string &iconTag, std::unique_ptr<uint8_t[]> &outValue, size_t &len)
{
AutoMutex mutex(this->lockIconValue_);
if (iconMaskValues_.empty()) {
return NOT_FOUND;
}
for (const auto &iconValue : iconMaskValues_) {
std::string tag = std::get<FIRST_ELEMENT>(iconValue);
if (iconTag != tag) {
continue;
}
size_t length = std::get<THIRED_ELEMENT>(iconValue);
auto iconInfo = std::make_unique<uint8_t[]>(length);
auto tmpInfo = std::get<SECOND_ELEMENT>(iconValue).get();
errno_t ret = memcpy_s(iconInfo.get(), length, tmpInfo, length);
if (ret != 0) {
RESMGR_HILOGE(RESMGR_TAG, "get icon info fail, ret = %{public}d", ret);
continue;
}
len = length;
outValue = std::move(iconInfo);
return SUCCESS;
}
return NOT_FOUND;
}
} // namespace Resource
} // namespace Global
} // namespace OHOS

View File

@ -412,6 +412,16 @@ const std::string ThemeResource::GetThemeAppIconByAbilityName(const std::pair<st
}
return std::string("");
}
bool ThemeResource::HasIconInTheme(const std::string &bundleName)
{
for (size_t i = 0; i < iconValues_.size(); i++) {
if (iconValues_[i].first.moduleName == bundleName || iconValues_[i].first.bundleName == bundleName) {
return true;
}
}
return false;
}
} // namespace Resource
} // namespace Global
} // namespace OHOS

View File

@ -189,4 +189,72 @@ HWTEST_F(ThemeManagerTest, ThemeManagerTestLoadThemeIconsResourceTest003, TestSi
EXPECT_EQ(state, SUCCESS);
tm->LoadThemeIconsResource("ohos.global.test.all", "entry", {}, userId);
}
/*
* @tc.name: ThemeManagerTestHasIconInThemeTest001
* @tc.desc: Test HasIconInTheme function, file case.
* @tc.type: FUNC
*/
HWTEST_F(ThemeManagerTest, ThemeManagerTestHasIconInThemeTest001, TestSize.Level1)
{
bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
ASSERT_TRUE(ret);
std::vector<std::string> rootDirs;
std::string rootDir = "/data/test/theme/icons/ohos.global.test.all";
rootDirs.emplace_back(rootDir);
int32_t userId = 100; // userId is 100
tm->LoadThemeIconsResource("ohos.global.test.all", "entry", rootDirs, userId);
bool result = rm->HasIconInTheme("ohos.global.test");
EXPECT_TRUE(result == false);
result = rm->HasIconInTheme("ohos.global.test.all");
EXPECT_TRUE(result == true);
}
/*
* @tc.name: ThemeManagerTestGetOtherIconsInfoTest001
* @tc.desc: Test GetOtherIconsInfo function, file case.
* @tc.type: FUNC
*/
HWTEST_F(ThemeManagerTest, ThemeManagerTestGetOtherIconsInfoTest001, TestSize.Level1)
{
bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
ASSERT_TRUE(ret);
std::vector<std::string> rootDirs;
std::string rootDir = "/data/test/theme/icons/other_icons";
rootDirs.emplace_back(rootDir);
int32_t userId = 100; // userId is 100
tm->LoadThemeIconsResource("other_icons", "", rootDirs, userId);
std::unique_ptr<uint8_t[]> outValue;
size_t len;
RState state = rm->GetOtherIconsInfo("other_icons", outValue, len, true);
EXPECT_EQ(state, ERROR_CODE_RES_NOT_FOUND_BY_NAME);
state = rm->GetOtherIconsInfo("foreground", outValue, len, false);
EXPECT_EQ(state, SUCCESS);
}
/*
* @tc.name: ThemeManagerTestGetThemeIconFromCacheTest001
* @tc.desc: Test GetThemeIconFromCache function, file case.
* @tc.type: FUNC
*/
HWTEST_F(ThemeManagerTest, ThemeManagerTestGetThemeIconFromCacheTest001, TestSize.Level1)
{
bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
ASSERT_TRUE(ret);
std::vector<std::string> rootDirs;
std::string rootDir = "/data/test/theme/icons/other_icons";
rootDirs.emplace_back(rootDir);
int32_t userId = 100; // userId is 100
tm->LoadThemeIconsResource("other_icons", "", rootDirs, userId);
std::unique_ptr<uint8_t[]> outValue;
size_t len;
RState state = tm->GetThemeIconFromCache("other_icons_background", outValue, len);
EXPECT_EQ(state, NOT_FOUND);
state = tm->GetOtherIconsInfo("background", outValue, len, false);
EXPECT_EQ(state, SUCCESS);
state = tm->GetThemeIconFromCache("other_icons_background", outValue, len);
EXPECT_EQ(state, SUCCESS);
}
}

View File

@ -22,6 +22,9 @@ namespace Resource {
int ThemeManagerTestLoadThemeSkinResourceTest001(void);
int ThemeManagerTestLoadThemeSkinResourceTest002(void);
int ThemeManagerTestLoadThemeIconsResourceTest001(void);
int ThemeManagerTestHasIconInThemeTest001(void);
int ThemeManagerTestGetOtherIconsInfoTest001(void);
int ThemeManagerTestGetThemeIconFromCacheTest001(void);
} // namespace Resource
} // namespace Global
} // namespace OHOS

View File

@ -216,6 +216,11 @@ public:
virtual std::string GetThemeMask() = 0;
virtual bool HasIconInTheme(const std::string &bundleName) = 0;
virtual RState GetOtherIconsInfo(const std::string &iconName,
std::unique_ptr<uint8_t[]> &outValue, size_t &len, bool isGlobalMask) = 0;
virtual RState IsRawDirFromHap(const std::string &pathName, bool &outValue) = 0;
virtual std::shared_ptr<ResourceManager> GetOverrideResourceManager(

View File

@ -34,6 +34,8 @@
<option name="push" value="data/theme/base/media/background.png -> /data/test/theme/icons/dynamic_icons/ohos.global.test.all/" src="res"/>
<option name="push" value="data/theme/base/media/foreground.png -> /data/test/theme/icons/ohos.global.test.all/entry/ohos.global.test.all.EntryAbility/" src="res"/>
<option name="push" value="data/theme/base/media/background.png -> /data/test/theme/icons/ohos.global.test.all/entry/ohos.global.test.all.EntryAbility/" src="res"/>
<option name="push" value="data/theme/base/media/foreground.png -> /data/test/theme/icons/other_icons/" src="res"/>
<option name="push" value="data/theme/base/media/background.png -> /data/test/theme/icons/other_icons/" src="res"/>
</preparer>
</target>
</configuration>