1 /*
2 * Copyright (c) 2023 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
16 #include "configuration_utils.h"
17
18 #include "configuration_convertor.h"
19 #include "hilog_wrapper.h"
20
21 namespace OHOS {
22 namespace AbilityRuntime {
UpdateConfigToResourceManager(const AppExecFwk::Configuration & configuration,std::shared_ptr<Global::Resource::ResourceManager> resourceManager)23 void ConfigurationUtils::UpdateConfigToResourceManager(const AppExecFwk::Configuration &configuration,
24 std::shared_ptr<Global::Resource::ResourceManager> resourceManager)
25 {
26 HILOG_DEBUG("Enter");
27 if (resourceManager == nullptr) {
28 HILOG_ERROR("Resource manager is invalid.");
29 return;
30 }
31
32 std::string language;
33 std::string colormode;
34 std::string hasPointerDevice;
35 GetConfigurationProperties(configuration, language, colormode, hasPointerDevice);
36 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
37 if (resConfig == nullptr) {
38 HILOG_ERROR("Create resource config failed.");
39 return;
40 }
41 resourceManager->GetResConfig(*resConfig);
42
43 #ifdef SUPPORT_GRAPHICS
44 if (!language.empty()) {
45 UErrorCode status = U_ZERO_ERROR;
46 icu::Locale locale = icu::Locale::forLanguageTag(language, status);
47 HILOG_DEBUG("get Locale::forLanguageTag return[%{public}d].", static_cast<int>(status));
48 if (status == U_ZERO_ERROR) {
49 resConfig->SetLocaleInfo(locale);
50 }
51
52 const icu::Locale *localeInfo = resConfig->GetLocaleInfo();
53 if (localeInfo != nullptr) {
54 HILOG_DEBUG("Update config, language: %{public}s, script: %{public}s, region: %{public}s",
55 localeInfo->getLanguage(), localeInfo->getScript(), localeInfo->getCountry());
56 }
57 }
58 #endif
59
60 if (!colormode.empty()) {
61 resConfig->SetColorMode(AppExecFwk::ConvertColorMode(colormode));
62 HILOG_DEBUG("Update config, colorMode: %{public}d", resConfig->GetColorMode());
63 }
64
65 if (!hasPointerDevice.empty()) {
66 resConfig->SetInputDevice(AppExecFwk::ConvertHasPointerDevice(hasPointerDevice));
67 HILOG_DEBUG("Update config, hasPointerDevice: %{public}d", resConfig->GetInputDevice());
68 }
69
70 Global::Resource::RState ret = resourceManager->UpdateResConfig(*resConfig);
71 if (ret != Global::Resource::RState::SUCCESS) {
72 HILOG_ERROR("Update resource config failed with %{public}d.", static_cast<int>(ret));
73 return;
74 }
75
76 HILOG_DEBUG("Update resource config succeed.");
77 }
78
GetConfigurationProperties(const AppExecFwk::Configuration & configuration,std::string & language,std::string & colormode,std::string & hasPointerDevice)79 void ConfigurationUtils::GetConfigurationProperties(const AppExecFwk::Configuration &configuration,
80 std::string &language, std::string &colormode, std::string &hasPointerDevice)
81 {
82 language = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
83 colormode = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
84 hasPointerDevice = configuration.GetItem(AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE);
85 }
86 } // namespace AbilityRuntime
87 } // namespace OHOS
88