1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "resource_config_helper.h"
16 #include "hilog_tag_wrapper.h"
17 #include "configuration_convertor.h"
18 #include "hitrace_meter.h"
19 #include "application_configuration_manager.h"
20
21 namespace OHOS {
22 namespace AbilityRuntime {
23 using namespace AppExecFwk;
24
GetLanguage()25 std::string ResourceConfigHelper::GetLanguage()
26 {
27 return language_;
28 }
SetLanguage(std::string language)29 void ResourceConfigHelper::SetLanguage(std::string language)
30 {
31 language_ = language;
32 }
33
GetLocale()34 std::string ResourceConfigHelper::GetLocale()
35 {
36 return locale_;
37 }
38
SetLocale(const std::string & locale)39 void ResourceConfigHelper::SetLocale(const std::string& locale)
40 {
41 locale_ = locale;
42 }
43
GetColormode()44 std::string ResourceConfigHelper::GetColormode()
45 {
46 return colormode_;
47 }
SetColormode(std::string colormode)48 void ResourceConfigHelper::SetColormode(std::string colormode)
49 {
50 colormode_ = colormode;
51 }
GetHasPointerDevice()52 std::string ResourceConfigHelper::GetHasPointerDevice()
53 {
54 return hasPointerDevice_;
55 }
SetHasPointerDevice(std::string hasPointerDevice)56 void ResourceConfigHelper::SetHasPointerDevice(std::string hasPointerDevice)
57 {
58 hasPointerDevice_ = hasPointerDevice;
59 }
GetMcc()60 std::string ResourceConfigHelper::GetMcc()
61 {
62 return mcc_;
63 }
SetMcc(std::string mcc)64 void ResourceConfigHelper::SetMcc(std::string mcc)
65 {
66 mcc_ = mcc;
67 }
GetMnc()68 std::string ResourceConfigHelper::GetMnc()
69 {
70 return mnc_;
71 }
SetMnc(std::string mnc)72 void ResourceConfigHelper::SetMnc(std::string mnc)
73 {
74 mnc_ = mnc;
75 }
SetThemeId(std::string themeId)76 void ResourceConfigHelper::SetThemeId(std::string themeId)
77 {
78 themeId_ = themeId;
79 }
SetThemeIcon(std::string themeIcon)80 void ResourceConfigHelper::SetThemeIcon(std::string themeIcon)
81 {
82 themeIcon_ = themeIcon;
83 }
84
SetISAbilityColor(bool isSetColorMode)85 void ResourceConfigHelper::SetISAbilityColor(bool isSetColorMode)
86 {
87 isSetColorMode_ = isSetColorMode;
88 }
89
UpdateResConfig(const AppExecFwk::Configuration & configuration,std::shared_ptr<Global::Resource::ResourceManager> resourceManager)90 void ResourceConfigHelper::UpdateResConfig(
91 const AppExecFwk::Configuration &configuration, std::shared_ptr<Global::Resource::ResourceManager> resourceManager)
92 {
93 if (resourceManager == nullptr) {
94 TAG_LOGE(AAFwkTag::ABILITY, "null resourceManager");
95 return;
96 }
97 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
98 if (resConfig == nullptr) {
99 TAG_LOGE(AAFwkTag::ABILITY, "null resConfig");
100 return;
101 }
102 resourceManager->GetResConfig(*resConfig);
103 #ifdef SUPPORT_GRAPHICS
104 if (!language_.empty()) {
105 UErrorCode status = U_ZERO_ERROR;
106 icu::Locale locale = icu::Locale::forLanguageTag(language_, status);
107 TAG_LOGD(AAFwkTag::ABILITY, "get forLanguageTag return[%{public}d]", static_cast<int>(status));
108 SetLevel curSetLevel = ApplicationConfigurationManager::GetInstance().GetLanguageSetLevel();
109 if (status == U_ZERO_ERROR && curSetLevel < SetLevel::Application) {
110 resConfig->SetLocaleInfo(locale);
111 } else if (status == U_ZERO_ERROR) {
112 resConfig->SetPreferredLocaleInfo(locale);
113 }
114 const icu::Locale *localeInfo = resConfig->GetLocaleInfo();
115 if (localeInfo != nullptr) {
116 TAG_LOGD(AAFwkTag::ABILITY, "update config, language: %{public}s, script: %{public}s,"
117 " region: %{public}s", localeInfo->getLanguage(), localeInfo->getScript(), localeInfo->getCountry());
118 }
119 }
120 #endif
121 UpdateResConfig(resConfig);
122 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "resourceManager->UpdateResConfig");
123 Global::Resource::RState ret = resourceManager->UpdateResConfig(*resConfig);
124 if (ret != Global::Resource::RState::SUCCESS) {
125 TAG_LOGE(AAFwkTag::ABILITY, "update resConfig failed:%{public}d", static_cast<int>(ret));
126 return;
127 }
128 TAG_LOGD(AAFwkTag::ABILITY, "current colorMode: %{public}d, hasPointerDevice: %{public}d",
129 resConfig->GetColorMode(), resConfig->GetInputDevice());
130 }
131
UpdateResConfig(std::unique_ptr<Global::Resource::ResConfig> & resConfig)132 void ResourceConfigHelper::UpdateResConfig(std::unique_ptr<Global::Resource::ResConfig> &resConfig)
133 {
134 if (resConfig == nullptr) {
135 TAG_LOGE(AAFwkTag::ABILITY, "null resConfig");
136 return;
137 }
138 if (!colormode_.empty()) {
139 resConfig->SetColorMode(AppExecFwk::ConvertColorMode(colormode_));
140 }
141 if (!hasPointerDevice_.empty()) {
142 resConfig->SetInputDevice(AppExecFwk::ConvertHasPointerDevice(hasPointerDevice_));
143 }
144 if (ApplicationConfigurationManager::GetInstance().ColorModeHasSetByApplication() || isSetColorMode_) {
145 TAG_LOGD(AAFwkTag::ABILITY, "set app true");
146 resConfig->SetAppColorMode(true);
147 }
148 if (!mcc_.empty()) {
149 uint32_t mccNum = 0;
150 if (ConvertStringToUint32(mcc_, mccNum)) {
151 resConfig->SetMcc(mccNum);
152 TAG_LOGD(AAFwkTag::ABILITY, "set mcc: %{public}u", resConfig->GetMcc());
153 }
154 }
155 if (!mnc_.empty()) {
156 uint32_t mncNum = 0;
157 if (ConvertStringToUint32(mnc_, mncNum)) {
158 resConfig->SetMnc(mncNum);
159 TAG_LOGD(AAFwkTag::ABILITY, "set mnc: %{public}u", resConfig->GetMnc());
160 }
161 }
162 if (!themeId_.empty()) {
163 uint32_t themeId = 0;
164 if (ConvertStringToUint32(themeId_, themeId)) {
165 resConfig->SetThemeId(themeId);
166 TAG_LOGD(AAFwkTag::ABILITY, "set themeId: %{public}u", resConfig->GetThemeId());
167 }
168 }
169 if (!themeIcon_.empty()) {
170 uint32_t themeIcon = 0;
171 if (ConvertStringToUint32(themeIcon_, themeIcon)) {
172 resConfig->SetThemeIcon(themeIcon);
173 TAG_LOGD(AAFwkTag::ABILITY, "set themeIcon: %{public}u", themeIcon);
174 }
175 }
176 }
177
ConvertStringToUint32(std::string source,uint32_t & result)178 bool ResourceConfigHelper::ConvertStringToUint32(std::string source, uint32_t &result)
179 {
180 try {
181 result = static_cast<uint32_t>(std::stoi(source));
182 } catch (...) {
183 TAG_LOGW(AAFwkTag::ABILITY, "invalid source:%{public}s", source.c_str());
184 return false;
185 }
186 return true;
187 }
188 } // namespace AbilityRuntime
189 } // namespace OHOS