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
GetColormode()34 std::string ResourceConfigHelper::GetColormode()
35 {
36 return colormode_;
37 }
SetColormode(std::string colormode)38 void ResourceConfigHelper::SetColormode(std::string colormode)
39 {
40 colormode_ = colormode;
41 }
GetHasPointerDevice()42 std::string ResourceConfigHelper::GetHasPointerDevice()
43 {
44 return hasPointerDevice_;
45 }
SetHasPointerDevice(std::string hasPointerDevice)46 void ResourceConfigHelper::SetHasPointerDevice(std::string hasPointerDevice)
47 {
48 hasPointerDevice_ = hasPointerDevice;
49 }
GetMcc()50 std::string ResourceConfigHelper::GetMcc()
51 {
52 return mcc_;
53 }
SetMcc(std::string mcc)54 void ResourceConfigHelper::SetMcc(std::string mcc)
55 {
56 mcc_ = mcc;
57 }
GetMnc()58 std::string ResourceConfigHelper::GetMnc()
59 {
60 return mnc_;
61 }
SetMnc(std::string mnc)62 void ResourceConfigHelper::SetMnc(std::string mnc)
63 {
64 mnc_ = mnc;
65 }
SetThemeId(std::string themeId)66 void ResourceConfigHelper::SetThemeId(std::string themeId)
67 {
68 themeId_ = themeId;
69 }
SetThemeIcon(std::string themeIcon)70 void ResourceConfigHelper::SetThemeIcon(std::string themeIcon)
71 {
72 themeIcon_ = themeIcon;
73 }
74
SetISAbilityColor(bool isSetColorMode)75 void ResourceConfigHelper::SetISAbilityColor(bool isSetColorMode)
76 {
77 isSetColorMode_ = isSetColorMode;
78 }
79
UpdateResConfig(const AppExecFwk::Configuration & configuration,std::shared_ptr<Global::Resource::ResourceManager> resourceManager)80 void ResourceConfigHelper::UpdateResConfig(
81 const AppExecFwk::Configuration &configuration, std::shared_ptr<Global::Resource::ResourceManager> resourceManager)
82 {
83 if (resourceManager == nullptr) {
84 TAG_LOGE(AAFwkTag::ABILITY, "null resourceManager");
85 return;
86 }
87 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
88 if (resConfig == nullptr) {
89 TAG_LOGE(AAFwkTag::ABILITY, "null resConfig");
90 return;
91 }
92 resourceManager->GetResConfig(*resConfig);
93 #ifdef SUPPORT_GRAPHICS
94 if (!language_.empty()) {
95 UErrorCode status = U_ZERO_ERROR;
96 icu::Locale locale = icu::Locale::forLanguageTag(language_, status);
97 TAG_LOGD(AAFwkTag::ABILITY, "get forLanguageTag return[%{public}d]", static_cast<int>(status));
98 SetLevel curSetLevel = ApplicationConfigurationManager::GetInstance().GetLanguageSetLevel();
99 if (status == U_ZERO_ERROR && curSetLevel < SetLevel::Application) {
100 resConfig->SetLocaleInfo(locale);
101 } else if (status == U_ZERO_ERROR) {
102 resConfig->SetPreferredLocaleInfo(locale);
103 }
104 const icu::Locale *localeInfo = resConfig->GetLocaleInfo();
105 if (localeInfo != nullptr) {
106 TAG_LOGD(AAFwkTag::ABILITY, "update config, language: %{public}s, script: %{public}s,"
107 " region: %{public}s", localeInfo->getLanguage(), localeInfo->getScript(), localeInfo->getCountry());
108 }
109 }
110 #endif
111 UpdateResConfig(resConfig);
112 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "resourceManager->UpdateResConfig");
113 Global::Resource::RState ret = resourceManager->UpdateResConfig(*resConfig);
114 if (ret != Global::Resource::RState::SUCCESS) {
115 TAG_LOGE(AAFwkTag::ABILITY, "update resConfig failed:%{public}d", static_cast<int>(ret));
116 return;
117 }
118 TAG_LOGD(AAFwkTag::ABILITY, "current colorMode: %{public}d, hasPointerDevice: %{public}d",
119 resConfig->GetColorMode(), resConfig->GetInputDevice());
120 }
121
UpdateResConfig(std::unique_ptr<Global::Resource::ResConfig> & resConfig)122 void ResourceConfigHelper::UpdateResConfig(std::unique_ptr<Global::Resource::ResConfig> &resConfig)
123 {
124 if (resConfig == nullptr) {
125 TAG_LOGE(AAFwkTag::ABILITY, "null resConfig");
126 return;
127 }
128 if (!colormode_.empty()) {
129 resConfig->SetColorMode(AppExecFwk::ConvertColorMode(colormode_));
130 }
131 if (!hasPointerDevice_.empty()) {
132 resConfig->SetInputDevice(AppExecFwk::ConvertHasPointerDevice(hasPointerDevice_));
133 }
134 if (ApplicationConfigurationManager::GetInstance().ColorModeHasSetByApplication() || isSetColorMode_) {
135 TAG_LOGD(AAFwkTag::ABILITY, "set app true");
136 resConfig->SetAppColorMode(true);
137 }
138 if (!mcc_.empty()) {
139 uint32_t mccNum = 0;
140 if (ConvertStringToUint32(mcc_, mccNum)) {
141 resConfig->SetMcc(mccNum);
142 TAG_LOGD(AAFwkTag::ABILITY, "set mcc: %{public}u", resConfig->GetMcc());
143 }
144 }
145 if (!mnc_.empty()) {
146 uint32_t mncNum = 0;
147 if (ConvertStringToUint32(mnc_, mncNum)) {
148 resConfig->SetMnc(mncNum);
149 TAG_LOGD(AAFwkTag::ABILITY, "set mnc: %{public}u", resConfig->GetMnc());
150 }
151 }
152 if (!themeId_.empty()) {
153 uint32_t themeId = 0;
154 if (ConvertStringToUint32(themeId_, themeId)) {
155 resConfig->SetThemeId(themeId);
156 TAG_LOGD(AAFwkTag::ABILITY, "set themeId: %{public}u", resConfig->GetThemeId());
157 }
158 }
159 if (!themeIcon_.empty()) {
160 uint32_t themeIcon = 0;
161 if (ConvertStringToUint32(themeIcon_, themeIcon)) {
162 resConfig->SetThemeIcon(themeIcon);
163 TAG_LOGD(AAFwkTag::ABILITY, "set themeIcon: %{public}u", themeIcon);
164 }
165 }
166 }
167
ConvertStringToUint32(std::string source,uint32_t & result)168 bool ResourceConfigHelper::ConvertStringToUint32(std::string source, uint32_t &result)
169 {
170 try {
171 result = static_cast<uint32_t>(std::stoi(source));
172 } catch (...) {
173 TAG_LOGW(AAFwkTag::ABILITY, "invalid source:%{public}s", source.c_str());
174 return false;
175 }
176 return true;
177 }
178 } // namespace AbilityRuntime
179 } // namespace OHOS