!7335 【资源缓存】适配themeIcon参数

Merge pull request !7335 from xuyicong/feat_AddThemeIcon
This commit is contained in:
openharmony_ci 2024-10-29 02:57:29 +00:00 committed by Gitee
commit 9ab7bd8cc3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 61 additions and 41 deletions

View File

@ -41,14 +41,14 @@ public:
const uint32_t type = static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE));
// for application theme changed
bool OnApplicationThemeChanged(const std::string &theme, const int32_t themeId = 0,
bool OnApplicationThemeChanged(const std::string &theme, const int32_t themeId = 0, const int32_t themeIcon = 0,
const uint32_t type = static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE));
// for overlay
bool OnOverlayStatusChanged(const std::string &bundleName, bool isEnabled, int32_t userId);
private:
bool SetThemeIdForThemeChanged(const int32_t themeId);
bool SetThemeParamForThemeChanged(const int32_t themeId, const int32_t themeIcon);
};
} // AppExecFwk
} // OHOS

View File

@ -34,11 +34,15 @@ public:
void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override;
private:
void ProcessResourceChangeByType(const std::string &language, const std::string &theme,
const int32_t id, const int32_t themeIcon, const uint32_t type);
static void OnSystemColorModeChanged(const std::string &colorMode, const uint32_t type);
static void OnSystemLanguageChange(const std::string &language, const uint32_t type);
static void OnApplicationThemeChanged(const std::string &theme, const int32_t themeId, const uint32_t type);
static void OnApplicationThemeChanged(const std::string &theme, const int32_t themeId,
const int32_t themeIcon, const uint32_t type);
};
#endif
} // AppExecFwk

View File

@ -86,7 +86,7 @@ bool BundleResourceCallback::OnSystemLanguageChange(const std::string &language,
}
bool BundleResourceCallback::OnApplicationThemeChanged(const std::string &theme,
const int32_t themeId, const uint32_t type)
const int32_t themeId, const int32_t themeIcon, const uint32_t type)
{
APP_LOGI("start, theme:%{public}s, themeId:%{public}d", theme.c_str(), themeId);
if (theme.empty()) {
@ -109,8 +109,8 @@ bool BundleResourceCallback::OnApplicationThemeChanged(const std::string &theme,
return false;
}
#ifdef GLOBAL_RESMGR_ENABLE
if (!SetThemeIdForThemeChanged(themeId)) {
APP_LOGE("set theme id failed, themeId %{public}d", themeId);
if (!SetThemeParamForThemeChanged(themeId, themeIcon)) {
APP_LOGE("set theme param failed, themeId %{public}d themeIcon %{public}d", themeId, themeIcon);
}
#endif
@ -166,7 +166,7 @@ bool BundleResourceCallback::OnOverlayStatusChanged(
return true;
}
bool BundleResourceCallback::SetThemeIdForThemeChanged(const int32_t themeId)
bool BundleResourceCallback::SetThemeParamForThemeChanged(const int32_t themeId, const int32_t themeIcon)
{
if (themeId <= 0) {
return false;
@ -174,7 +174,7 @@ bool BundleResourceCallback::SetThemeIdForThemeChanged(const int32_t themeId)
#ifdef GLOBAL_RESMGR_ENABLE
auto resourcePtr = std::shared_ptr<Global::Resource::ResourceManager>(Global::Resource::CreateResourceManager());
if (resourcePtr == nullptr) {
APP_LOGE("resource is nullptr, themeId %{public}d", themeId);
APP_LOGE("resource is nullptr, themeId %{public}d themeIcon %{public}d", themeId, themeIcon);
return false;
}
int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
@ -183,18 +183,21 @@ bool BundleResourceCallback::SetThemeIdForThemeChanged(const int32_t themeId)
currentUserId = Constants::START_USERID;
}
resourcePtr->userId = currentUserId;
APP_LOGI("start set themeId %{public}d userId %{public}d", themeId, currentUserId);
APP_LOGI("start set themeId %{public}d userId %{public}d themeIcon %{public}d",
themeId, currentUserId, themeIcon);
std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
if (resConfig == nullptr) {
APP_LOGE("resConfig is nullptr, themeId %{public}d", themeId);
APP_LOGE("resConfig is nullptr, themeId %{public}d themeIcon %{public}d", themeId, themeIcon);
return false;
}
resConfig->SetThemeId(themeId);
resConfig->SetThemeIcon(themeIcon > 0 ? true : false);
Global::Resource::RState ret = resourcePtr->UpdateResConfig(*resConfig); // no need to process ret
if (ret != Global::Resource::RState::SUCCESS) {
APP_LOGW("UpdateResConfig ret %{public}d, themeId %{public}d", static_cast<int32_t>(ret), themeId);
APP_LOGW("UpdateResConfig ret %{public}d, themeId %{public}d, themeIcon %{public}d",
static_cast<int32_t>(ret), themeId, themeIcon);
}
APP_LOGI("end set themeId %{public}d", themeId);
APP_LOGI("end set themeId %{public}d themeIcon %{public}d", themeId, themeIcon);
#endif
return true;
}

View File

@ -51,37 +51,22 @@ void BundleResourceObserver::OnConfigurationUpdated(const AppExecFwk::Configurat
}
std::string theme = configuration.GetItem(AAFwk::GlobalConfigurationKey::THEME);
int32_t id = 0;
int32_t themeIcon = 0;
if (!theme.empty()) {
std::string themeIconStr = configuration.GetItem(AAFwk::GlobalConfigurationKey::THEME_ICON);
std::string themeId = configuration.GetItem(AAFwk::GlobalConfigurationKey::THEME_ID);
APP_LOGI("theme change %{public}s, themeId %{public}s", theme.c_str(), themeId.c_str());
APP_LOGI("theme change %{public}s, themeId %{public}s, themeIcon %{public}s",
theme.c_str(), themeId.c_str(), themeIconStr.c_str());
if (!OHOS::StrToInt(themeId, id)) {
id = 0;
}
if (!OHOS::StrToInt(themeIconStr, themeIcon)) {
themeIcon = 0;
}
type = (type == 0) ? static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE) :
(type | static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE));
}
switch (type) {
case 0 : {
break;
}
case static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE) : {
std::thread systemLanguageChangedThread(OnSystemLanguageChange, language, type);
systemLanguageChangedThread.detach();
break;
}
case static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE) : {
std::thread applicationThemeChangedThread(OnApplicationThemeChanged, theme, id, type);
applicationThemeChangedThread.detach();
break;
}
default: {
BundleSystemState::GetInstance().SetSystemLanguage(language);
std::thread applicationThemeChangedThread(OnApplicationThemeChanged, theme, id, type);
applicationThemeChangedThread.detach();
break;
}
}
ProcessResourceChangeByType(language, theme, id, themeIcon, type);
APP_LOGI("end change type %{public}u", type);
}
@ -98,10 +83,37 @@ void BundleResourceObserver::OnSystemLanguageChange(const std::string &language,
}
void BundleResourceObserver::OnApplicationThemeChanged(const std::string &theme,
const int32_t themeId, const uint32_t type)
const int32_t themeId, const int32_t themeIcon, const uint32_t type)
{
BundleResourceCallback callback;
callback.OnApplicationThemeChanged(theme, themeId, type);
callback.OnApplicationThemeChanged(theme, themeId, themeIcon, type);
}
void BundleResourceObserver::ProcessResourceChangeByType(const std::string &language,
const std::string &theme, const int32_t id, const int32_t themeIcon, const uint32_t type)
{
switch (type) {
case static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE) : {
std::thread systemLanguageChangedThread(OnSystemLanguageChange, language, type);
systemLanguageChangedThread.detach();
break;
}
case static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE) : {
std::thread applicationThemeChangedThread(OnApplicationThemeChanged, theme, id, themeIcon, type);
applicationThemeChangedThread.detach();
break;
}
case (static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE) |
static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE)): {
BundleSystemState::GetInstance().SetSystemLanguage(language);
std::thread applicationThemeChangedThread(OnApplicationThemeChanged, theme, id, themeIcon, type);
applicationThemeChangedThread.detach();
break;
}
default: {
break;
}
}
}
#endif
} // AppExecFwk

View File

@ -3922,19 +3922,20 @@ HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0172, Function | SmallTest
/**
* @tc.number: BmsBundleResourceTest_0173
* Function: SetThemeIdForThemeChanged
* Function: SetThemeParamForThemeChanged
* @tc.name: test
* @tc.desc: 1. system running normally
* 2. test SetThemeIdForThemeChanged
* 2. test SetThemeParamForThemeChanged
*/
HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0173, Function | SmallTest | Level0)
{
int32_t themeId = 0;
int32_t themeIcon = 0;
BundleResourceCallback bundleResourceCallback;
bool ret = bundleResourceCallback.SetThemeIdForThemeChanged(themeId);
bool ret = bundleResourceCallback.SetThemeParamForThemeChanged(themeId, themeIcon);
EXPECT_FALSE(ret);
themeId = 1000;
ret = bundleResourceCallback.SetThemeIdForThemeChanged(themeId);
ret = bundleResourceCallback.SetThemeParamForThemeChanged(themeId, themeIcon);
EXPECT_TRUE(ret);
}