• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
71 
ConvertConfigToGlobal(const ResourceConfiguration & config)72 std::shared_ptr<Global::Resource::ResConfig> ConvertConfigToGlobal(const ResourceConfiguration& config)
73 {
74     std::shared_ptr<Global::Resource::ResConfig> newResCfg(Global::Resource::CreateResConfig());
75     newResCfg->SetLocaleInfo(AceApplicationInfo::GetInstance().GetLanguage().c_str(),
76         AceApplicationInfo::GetInstance().GetScript().c_str(),
77         AceApplicationInfo::GetInstance().GetCountryOrRegion().c_str());
78     newResCfg->SetDeviceType(ConvertDeviceTypeToGlobal(config.GetDeviceType()));
79     newResCfg->SetDirection(ConvertDirectionToGlobal(config.GetOrientation()));
80     newResCfg->SetScreenDensity(ConvertDensityToGlobal(config.GetDensity()));
81     return newResCfg;
82 }
83 
ConvertDeviceTypeToAce(Global::Resource::DeviceType type)84 DeviceType ConvertDeviceTypeToAce(Global::Resource::DeviceType type)
85 {
86     switch (type) {
87         case Global::Resource::DeviceType::DEVICE_PHONE:
88             return DeviceType::PHONE;
89         case Global::Resource::DeviceType::DEVICE_TV:
90             return DeviceType::TV;
91         case Global::Resource::DeviceType::DEVICE_WEARABLE:
92             return DeviceType::WATCH;
93         case Global::Resource::DeviceType::DEVICE_CAR:
94             return DeviceType::CAR;
95         case Global::Resource::DeviceType::DEVICE_TABLET:
96             return DeviceType::TABLET;
97         default:
98             return DeviceType::UNKNOWN;
99     }
100 }
101 
ConvertDirectionToAce(Global::Resource::Direction orientation)102 DeviceOrientation ConvertDirectionToAce(Global::Resource::Direction orientation)
103 {
104     switch (orientation) {
105         case Global::Resource::Direction::DIRECTION_VERTICAL:
106             return DeviceOrientation::PORTRAIT;
107         case Global::Resource::Direction::DIRECTION_HORIZONTAL:
108             return DeviceOrientation::LANDSCAPE;
109         default:
110             return DeviceOrientation::ORIENTATION_UNDEFINED;
111     }
112 }
113 
ConvertDensityToAce(Global::Resource::ScreenDensity density)114 double ConvertDensityToAce(Global::Resource::ScreenDensity density)
115 {
116     switch (density) {
117         case Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI:
118             return 120.0 / DPI_BASE;
119         case Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI:
120             return 160.0 / DPI_BASE;
121         case Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI:
122             return 240.0 / DPI_BASE;
123         case Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI:
124             return 320.0 / DPI_BASE;
125         case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI:
126             return 480.0 / DPI_BASE;
127         case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI:
128             return 640.0 / DPI_BASE;
129         default:
130             return 0.0;
131     }
132 }
133 
134 } // namespace OHOS::Ace