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 }
70 
UpdateResConfig(const AppExecFwk::Configuration & configuration,std::shared_ptr<Global::Resource::ResourceManager> resourceManager)71 void ResourceConfigHelper::UpdateResConfig(
72     const AppExecFwk::Configuration &configuration, std::shared_ptr<Global::Resource::ResourceManager> resourceManager)
73 {
74     if (resourceManager == nullptr) {
75         TAG_LOGE(AAFwkTag::ABILITY, "invalid Resource manager");
76         return;
77     }
78     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
79     if (resConfig == nullptr) {
80         TAG_LOGE(AAFwkTag::ABILITY, "create resConfig failed");
81         return;
82     }
83     resourceManager->GetResConfig(*resConfig);
84 #ifdef SUPPORT_GRAPHICS
85     if (!language_.empty()) {
86         UErrorCode status = U_ZERO_ERROR;
87         icu::Locale locale = icu::Locale::forLanguageTag(language_, status);
88         TAG_LOGD(AAFwkTag::ABILITY, "get forLanguageTag return[%{public}d]", static_cast<int>(status));
89         SetLevel curSetLevel = ApplicationConfigurationManager::GetInstance().GetLanguageSetLevel();
90         if (status == U_ZERO_ERROR && curSetLevel < SetLevel::Application) {
91             resConfig->SetLocaleInfo(locale);
92         } else if (status == U_ZERO_ERROR) {
93             resConfig->SetPreferredLocaleInfo(locale);
94         }
95         const icu::Locale *localeInfo = resConfig->GetLocaleInfo();
96         if (localeInfo != nullptr) {
97             TAG_LOGD(AAFwkTag::ABILITY, "update config, language: %{public}s, script: %{public}s,"
98             " region: %{public}s", localeInfo->getLanguage(), localeInfo->getScript(), localeInfo->getCountry());
99         }
100     }
101 #endif
102     UpdateResConfig(resConfig);
103     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "resourceManager->UpdateResConfig");
104     Global::Resource::RState ret = resourceManager->UpdateResConfig(*resConfig);
105     if (ret != Global::Resource::RState::SUCCESS) {
106         TAG_LOGE(AAFwkTag::ABILITY, "update resConfig failed:%{public}d", static_cast<int>(ret));
107         return;
108     }
109     TAG_LOGD(AAFwkTag::ABILITY, "current colorMode: %{public}d, hasPointerDevice: %{public}d",
110              resConfig->GetColorMode(), resConfig->GetInputDevice());
111 }
112 
UpdateResConfig(std::unique_ptr<Global::Resource::ResConfig> & resConfig)113 void ResourceConfigHelper::UpdateResConfig(std::unique_ptr<Global::Resource::ResConfig> &resConfig)
114 {
115     if (resConfig == nullptr) {
116         TAG_LOGE(AAFwkTag::ABILITY, "null resConfig");
117         return;
118     }
119     if (!colormode_.empty()) {
120         resConfig->SetColorMode(AppExecFwk::ConvertColorMode(colormode_));
121     }
122     if (!hasPointerDevice_.empty()) {
123         resConfig->SetInputDevice(AppExecFwk::ConvertHasPointerDevice(hasPointerDevice_));
124     }
125     if (ApplicationConfigurationManager::GetInstance().ColorModeHasSetByApplication()) {
126         TAG_LOGD(AAFwkTag::ABILITY, "set app true");
127         resConfig->SetAppColorMode(true);
128     }
129     if (!mcc_.empty()) {
130         uint32_t mccNum = 0;
131         if (ConvertStringToUint32(mcc_, mccNum)) {
132             resConfig->SetMcc(mccNum);
133             TAG_LOGD(AAFwkTag::ABILITY, "set mcc: %{public}u", resConfig->GetMcc());
134         }
135     }
136     if (!mnc_.empty()) {
137         uint32_t mncNum = 0;
138         if (ConvertStringToUint32(mnc_, mncNum)) {
139             resConfig->SetMnc(mncNum);
140             TAG_LOGD(AAFwkTag::ABILITY, "set mnc: %{public}u", resConfig->GetMnc());
141         }
142     }
143     if (!themeId_.empty()) {
144         uint32_t themeId = 0;
145         if (ConvertStringToUint32(themeId_, themeId)) {
146             resConfig->SetThemeId(themeId);
147             TAG_LOGD(AAFwkTag::ABILITY, "set themeId: %{public}u", resConfig->GetThemeId());
148         }
149     }
150 }
151 
ConvertStringToUint32(std::string source,uint32_t & result)152 bool ResourceConfigHelper::ConvertStringToUint32(std::string source, uint32_t &result)
153 {
154     try {
155         result = static_cast<uint32_t>(std::stoi(source));
156     } catch (...) {
157         TAG_LOGW(AAFwkTag::ABILITY, "invalid source:%{public}s", source.c_str());
158         return false;
159     }
160     return true;
161 }
162 } // namespace AbilityRuntime
163 } // namespace OHOS