1 /*
2 * Copyright (c) 2025 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 "resource_manager_helper.h"
17
18 #include "hilog_tag_wrapper.h"
19 #include "js_data_converter.h"
20
21 namespace OHOS {
22 namespace AbilityRuntime {
23 const std::string RESOURCES_DIR_NAME = "resources";
24 const std::string RESOURCES_FILE_NAME = "resources.index";
25
26 #if defined(WINDOWS_PLATFORM)
27 constexpr char DELIMITER[] = "\\";
28 #elif defined(MAC_PLATFORM)
29 constexpr char DELIMITER[] = "/";
30 #else
31 #error "Unsupported platform"
32 #endif
33
GetInstance()34 ResourceManagerHelper &ResourceManagerHelper::GetInstance()
35 {
36 static ResourceManagerHelper instance;
37 return instance;
38 }
39
Init(const Options & options)40 void ResourceManagerHelper::Init(const Options &options)
41 {
42 options_ = options;
43 }
44
GetResConfig(Global::Resource::ResConfig & resConfig,bool isCreateModuleContext)45 void ResourceManagerHelper::GetResConfig(Global::Resource::ResConfig &resConfig, bool isCreateModuleContext)
46 {
47 resConfig.SetLocaleInfo(options_.language.c_str(), options_.script.c_str(), options_.region.c_str());
48 auto deviceType = ConvertDeviceType(options_.deviceConfig.deviceType);
49 resConfig.SetDeviceType(deviceType);
50 if (!isCreateModuleContext) {
51 auto direction = ConvertDirection(options_.deviceConfig.orientation);
52 resConfig.SetDirection(direction);
53 resConfig.SetScreenDensity(options_.deviceConfig.density);
54 auto colorMode = ConvertColorMode(options_.deviceConfig.colorMode);
55 resConfig.SetColorMode(colorMode);
56 resConfig.SetThemeId(options_.themeId);
57 }
58 }
59
AddSystemResource(std::shared_ptr<Global::Resource::ResourceManager> & resMgr)60 void ResourceManagerHelper::AddSystemResource(std::shared_ptr<Global::Resource::ResourceManager> &resMgr)
61 {
62 std::string sysResIndexPath = options_.systemResourcePath + DELIMITER + RESOURCES_FILE_NAME;
63 if (!resMgr->AddResource(sysResIndexPath.c_str())) {
64 TAG_LOGE(AAFwkTag::ABILITY_SIM, "Add system resource failed");
65 }
66 std::string hmsResIndexPath = options_.containerSdkPath + DELIMITER + RESOURCES_DIR_NAME + DELIMITER +
67 RESOURCES_DIR_NAME + DELIMITER + RESOURCES_FILE_NAME;
68 if (!resMgr->AddResource(hmsResIndexPath.c_str())) {
69 TAG_LOGE(AAFwkTag::ABILITY_SIM, "Add hms resource failed");
70 }
71 }
72 } // namespace AbilityRuntime
73 } // namespace OHOS
74