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     newResCfg->SetLocaleInfo(AceApplicationInfo::GetInstance().GetLanguage().c_str(),
97         AceApplicationInfo::GetInstance().GetScript().c_str(),
98         AceApplicationInfo::GetInstance().GetCountryOrRegion().c_str());
99     newResCfg->SetDeviceType(ConvertDeviceTypeToGlobal(config.GetDeviceType()));
100     newResCfg->SetDirection(ConvertDirectionToGlobal(config.GetOrientation()));
101     newResCfg->SetScreenDensity(config.GetDensity());
102     newResCfg->SetColorMode(ConvertColorModeToGlobal(config.GetColorMode()));
103     newResCfg->SetInputDevice(ConvertInputDevice(config.GetDeviceAccess()));
104     newResCfg->SetAppColorMode(config.GetColorModeIsSetByApp());
105     newResCfg->SetMcc(config.GetMcc());
106     newResCfg->SetMnc(config.GetMnc());
107     icu::Locale locale(config.GetPreferredLanguage().c_str());
108     newResCfg->SetPreferredLocaleInfo(locale);
109     return newResCfg;
110 }
111 
ConvertDeviceTypeToAce(Global::Resource::DeviceType type)112 DeviceType ConvertDeviceTypeToAce(Global::Resource::DeviceType type)
113 {
114     switch (type) {
115         case Global::Resource::DeviceType::DEVICE_PHONE:
116             return DeviceType::PHONE;
117         case Global::Resource::DeviceType::DEVICE_TV:
118             return DeviceType::TV;
119         case Global::Resource::DeviceType::DEVICE_WEARABLE:
120             return DeviceType::WATCH;
121         case Global::Resource::DeviceType::DEVICE_CAR:
122             return DeviceType::CAR;
123         case Global::Resource::DeviceType::DEVICE_TABLET:
124             return DeviceType::TABLET;
125         default:
126             return DeviceType::UNKNOWN;
127     }
128 }
129 
ConvertDirectionToAce(Global::Resource::Direction orientation)130 DeviceOrientation ConvertDirectionToAce(Global::Resource::Direction orientation)
131 {
132     switch (orientation) {
133         case Global::Resource::Direction::DIRECTION_VERTICAL:
134             return DeviceOrientation::PORTRAIT;
135         case Global::Resource::Direction::DIRECTION_HORIZONTAL:
136             return DeviceOrientation::LANDSCAPE;
137         default:
138             return DeviceOrientation::ORIENTATION_UNDEFINED;
139     }
140 }
141 
ConvertDensityToAce(Global::Resource::ScreenDensity density)142 double ConvertDensityToAce(Global::Resource::ScreenDensity density)
143 {
144     switch (density) {
145         case Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI:
146             return 120.0 / DPI_BASE;
147         case Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI:
148             return 160.0 / DPI_BASE;
149         case Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI:
150             return 240.0 / DPI_BASE;
151         case Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI:
152             return 320.0 / DPI_BASE;
153         case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI:
154             return 480.0 / DPI_BASE;
155         case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI:
156             return 640.0 / DPI_BASE;
157         default:
158             return 0.0;
159     }
160 }
161 
162 } // namespace OHOS::Ace