1 /*
2 * Copyright (c) 2021 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 "adapter/ohos/osal/resource_convertor.h"
17
18 namespace OHOS::Ace {
19
ConvertDeviceTypeToGlobal(DeviceType type)20 Global::Resource::DeviceType ConvertDeviceTypeToGlobal(DeviceType type)
21 {
22 switch (type) {
23 case DeviceType::PHONE:
24 return Global::Resource::DeviceType::DEVICE_PHONE;
25 case DeviceType::TV:
26 return Global::Resource::DeviceType::DEVICE_TV;
27 case DeviceType::WATCH:
28 return Global::Resource::DeviceType::DEVICE_WEARABLE;
29 case DeviceType::CAR:
30 return Global::Resource::DeviceType::DEVICE_CAR;
31 case DeviceType::TABLET:
32 return Global::Resource::DeviceType::DEVICE_TABLET;
33 default:
34 return Global::Resource::DeviceType::DEVICE_NOT_SET;
35 }
36 }
37
ConvertDirectionToGlobal(DeviceOrientation orientation)38 Global::Resource::Direction ConvertDirectionToGlobal(DeviceOrientation orientation)
39 {
40 switch (orientation) {
41 case DeviceOrientation::PORTRAIT:
42 return Global::Resource::Direction::DIRECTION_VERTICAL;
43 case DeviceOrientation::LANDSCAPE:
44 return Global::Resource::Direction::DIRECTION_HORIZONTAL;
45 default:
46 return Global::Resource::Direction::DIRECTION_NOT_SET;
47 }
48 }
49
ConvertDensityToGlobal(double density)50 Global::Resource::ScreenDensity ConvertDensityToGlobal(double density)
51 {
52 static const std::vector<std::pair<double, Global::Resource::ScreenDensity>> resolutions = {
53 { 0.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET },
54 { 120.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI },
55 { 160.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI },
56 { 240.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI },
57 { 320.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI },
58 { 480.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI },
59 { 640.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI },
60 };
61 double deviceDpi = density * DPI_BASE;
62 auto resolution = Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET;
63 for (const auto& [dpi, value] : resolutions) {
64 resolution = value;
65 if (LessOrEqual(deviceDpi, dpi)) {
66 break;
67 }
68 }
69 return resolution;
70 }
ConvertColorModeToGlobal(ColorMode colorMode)71 Global::Resource::ColorMode ConvertColorModeToGlobal(ColorMode colorMode)
72 {
73 switch (colorMode) {
74 case ColorMode::DARK:
75 return Global::Resource::ColorMode::DARK;
76 case ColorMode::LIGHT:
77 return Global::Resource::ColorMode::LIGHT;
78 default:
79 return Global::Resource::ColorMode::COLOR_MODE_NOT_SET;
80 }
81 }
82
ConvertInputDevice(bool deviceAccess)83 Global::Resource::InputDevice ConvertInputDevice(bool deviceAccess)
84 {
85 return deviceAccess ? Global::Resource::InputDevice::INPUTDEVICE_POINTINGDEVICE :
86 Global::Resource::InputDevice::INPUTDEVICE_NOT_SET;
87 }
88
ConvertConfigToGlobal(const ResourceConfiguration & config)89 std::shared_ptr<Global::Resource::ResConfig> ConvertConfigToGlobal(const ResourceConfiguration& config)
90 {
91 std::shared_ptr<Global::Resource::ResConfig> newResCfg(Global::Resource::CreateResConfig());
92 newResCfg->SetLocaleInfo(AceApplicationInfo::GetInstance().GetLanguage().c_str(),
93 AceApplicationInfo::GetInstance().GetScript().c_str(),
94 AceApplicationInfo::GetInstance().GetCountryOrRegion().c_str());
95 newResCfg->SetDeviceType(ConvertDeviceTypeToGlobal(config.GetDeviceType()));
96 newResCfg->SetDirection(ConvertDirectionToGlobal(config.GetOrientation()));
97 newResCfg->SetScreenDensity(config.GetDensity());
98 newResCfg->SetColorMode(ConvertColorModeToGlobal(config.GetColorMode()));
99 newResCfg->SetInputDevice(ConvertInputDevice(config.GetDeviceAccess()));
100 return newResCfg;
101 }
102
ConvertDeviceTypeToAce(Global::Resource::DeviceType type)103 DeviceType ConvertDeviceTypeToAce(Global::Resource::DeviceType type)
104 {
105 switch (type) {
106 case Global::Resource::DeviceType::DEVICE_PHONE:
107 return DeviceType::PHONE;
108 case Global::Resource::DeviceType::DEVICE_TV:
109 return DeviceType::TV;
110 case Global::Resource::DeviceType::DEVICE_WEARABLE:
111 return DeviceType::WATCH;
112 case Global::Resource::DeviceType::DEVICE_CAR:
113 return DeviceType::CAR;
114 case Global::Resource::DeviceType::DEVICE_TABLET:
115 return DeviceType::TABLET;
116 default:
117 return DeviceType::UNKNOWN;
118 }
119 }
120
ConvertDirectionToAce(Global::Resource::Direction orientation)121 DeviceOrientation ConvertDirectionToAce(Global::Resource::Direction orientation)
122 {
123 switch (orientation) {
124 case Global::Resource::Direction::DIRECTION_VERTICAL:
125 return DeviceOrientation::PORTRAIT;
126 case Global::Resource::Direction::DIRECTION_HORIZONTAL:
127 return DeviceOrientation::LANDSCAPE;
128 default:
129 return DeviceOrientation::ORIENTATION_UNDEFINED;
130 }
131 }
132
ConvertDensityToAce(Global::Resource::ScreenDensity density)133 double ConvertDensityToAce(Global::Resource::ScreenDensity density)
134 {
135 switch (density) {
136 case Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI:
137 return 120.0 / DPI_BASE;
138 case Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI:
139 return 160.0 / DPI_BASE;
140 case Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI:
141 return 240.0 / DPI_BASE;
142 case Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI:
143 return 320.0 / DPI_BASE;
144 case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI:
145 return 480.0 / DPI_BASE;
146 case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI:
147 return 640.0 / DPI_BASE;
148 default:
149 return 0.0;
150 }
151 }
152
153 } // namespace OHOS::Ace