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