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 "bundle_resource_configuration.h"
17
18 #include "app_log_wrapper.h"
19 #ifdef GLOBAL_I18_ENABLE
20 #include "locale_config.h"
21 #include "locale_info.h"
22 #endif
23
24 namespace OHOS {
25 namespace AppExecFwk {
InitResourceGlobalConfig(std::shared_ptr<Global::Resource::ResourceManager> resourceManager)26 bool BundleResourceConfiguration::InitResourceGlobalConfig(
27 std::shared_ptr<Global::Resource::ResourceManager> resourceManager)
28 {
29 return InitResourceGlobalConfig("", resourceManager);
30 }
31
InitResourceGlobalConfig(const std::string & hapPath,std::shared_ptr<Global::Resource::ResourceManager> resourceManager)32 bool BundleResourceConfiguration::InitResourceGlobalConfig(
33 const std::string &hapPath,
34 std::shared_ptr<Global::Resource::ResourceManager> resourceManager)
35 {
36 if (resourceManager == nullptr) {
37 APP_LOGE("resourceManager is nullptr");
38 return false;
39 }
40 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
41 if (resConfig == nullptr) {
42 APP_LOGE("resConfig is nullptr");
43 return false;
44 }
45
46 #ifdef GLOBAL_I18_ENABLE
47 std::map<std::string, std::string> configs;
48 OHOS::Global::I18n::LocaleInfo locale(Global::I18n::LocaleConfig::GetSystemLocale(), configs);
49 resConfig->SetLocaleInfo(locale.GetLanguage().c_str(), locale.GetScript().c_str(), locale.GetRegion().c_str());
50 #endif
51 resConfig->SetScreenDensityDpi(Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI);
52
53 Global::Resource::RState ret = resourceManager->UpdateResConfig(*resConfig);
54 if (ret != Global::Resource::RState::SUCCESS) {
55 APP_LOGE("UpdateResConfig failed %{public}d", static_cast<int32_t>(ret));
56 return false;
57 }
58 if (!hapPath.empty() && !resourceManager->AddResource(hapPath.c_str(),
59 Global::Resource::SELECT_STRING | Global::Resource::SELECT_MEDIA)) {
60 APP_LOGE("AddResource failed, hapPath: %{private}s", hapPath.c_str());
61 return false;
62 }
63 return true;
64 }
65
InitResourceGlobalConfig(const std::string & hapPath,const std::vector<std::string> & overlayHaps,std::shared_ptr<Global::Resource::ResourceManager> resourceManager,bool needParseIcon,bool needParseLabel)66 bool BundleResourceConfiguration::InitResourceGlobalConfig(const std::string &hapPath,
67 const std::vector<std::string> &overlayHaps,
68 std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
69 bool needParseIcon, bool needParseLabel)
70 {
71 if (resourceManager == nullptr) {
72 APP_LOGE("resourceManager is nullptr");
73 return false;
74 }
75 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
76 if (resConfig == nullptr) {
77 APP_LOGE("resConfig is nullptr");
78 return false;
79 }
80
81 #ifdef GLOBAL_I18_ENABLE
82 std::map<std::string, std::string> configs;
83 OHOS::Global::I18n::LocaleInfo locale(Global::I18n::LocaleConfig::GetSystemLocale(), configs);
84 resConfig->SetLocaleInfo(locale.GetLanguage().c_str(), locale.GetScript().c_str(), locale.GetRegion().c_str());
85 #endif
86 resConfig->SetScreenDensityDpi(Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI);
87
88 Global::Resource::RState ret = resourceManager->UpdateResConfig(*resConfig);
89 if (ret != Global::Resource::RState::SUCCESS) {
90 APP_LOGE("UpdateResConfig failed %{public}d", static_cast<int32_t>(ret));
91 return false;
92 }
93 // adapt overlay
94 if (overlayHaps.empty()) {
95 uint32_t selectType = needParseIcon ? Global::Resource::SELECT_MEDIA : 0;
96 selectType = needParseLabel ? (selectType | Global::Resource::SELECT_STRING) : selectType;
97 if (!resourceManager->AddResource(hapPath.c_str(), selectType)) {
98 APP_LOGW("AddResource failed, hapPath: %{public}s", hapPath.c_str());
99 }
100 } else {
101 if (!resourceManager->AddResource(hapPath, overlayHaps)) {
102 APP_LOGW("AddResource overlay failed, hapPath: %{public}s", hapPath.c_str());
103 }
104 }
105 return true;
106 }
107 } // AppExecFwk
108 } // OHOS
109