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