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/preview/osal/resource_convertor.h" 17 18namespace OHOS::Ace { 19 20Global::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 42Global::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 54Global::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} 75 76std::shared_ptr<Global::Resource::ResConfig> ConvertConfigToGlobal(const ResourceConfiguration& config) 77{ 78 std::shared_ptr<Global::Resource::ResConfig> newResCfg(Global::Resource::CreateResConfig()); 79 newResCfg->SetLocaleInfo(AceApplicationInfo::GetInstance().GetLanguage().c_str(), 80 AceApplicationInfo::GetInstance().GetScript().c_str(), 81 AceApplicationInfo::GetInstance().GetCountryOrRegion().c_str()); 82 newResCfg->SetDeviceType(ConvertDeviceTypeToGlobal(config.GetDeviceType())); 83 newResCfg->SetDirection(ConvertDirectionToGlobal(config.GetOrientation())); 84 newResCfg->SetScreenDensity(config.GetDensity()); 85 return newResCfg; 86} 87 88DeviceType ConvertDeviceTypeToAce(Global::Resource::DeviceType type) 89{ 90 switch (type) { 91 case Global::Resource::DeviceType::DEVICE_PHONE: 92 return DeviceType::PHONE; 93 case Global::Resource::DeviceType::DEVICE_TV: 94 return DeviceType::TV; 95 case Global::Resource::DeviceType::DEVICE_WEARABLE: 96 return DeviceType::WATCH; 97 case Global::Resource::DeviceType::DEVICE_CAR: 98 return DeviceType::CAR; 99 case Global::Resource::DeviceType::DEVICE_TABLET: 100 return DeviceType::TABLET; 101 default: 102 return DeviceType::UNKNOWN; 103 } 104} 105 106DeviceOrientation ConvertDirectionToAce(Global::Resource::Direction orientation) 107{ 108 switch (orientation) { 109 case Global::Resource::Direction::DIRECTION_VERTICAL: 110 return DeviceOrientation::PORTRAIT; 111 case Global::Resource::Direction::DIRECTION_HORIZONTAL: 112 return DeviceOrientation::LANDSCAPE; 113 default: 114 return DeviceOrientation::ORIENTATION_UNDEFINED; 115 } 116} 117 118double ConvertDensityToAce(Global::Resource::ScreenDensity density) 119{ 120 switch (density) { 121 case Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI: 122 return 120.0 / DPI_BASE; 123 case Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI: 124 return 160.0 / DPI_BASE; 125 case Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI: 126 return 240.0 / DPI_BASE; 127 case Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI: 128 return 320.0 / DPI_BASE; 129 case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI: 130 return 480.0 / DPI_BASE; 131 case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI: 132 return 640.0 / DPI_BASE; 133 default: 134 return 0.0; 135 } 136} 137 138Global::Resource::ColorMode ConvertColorModeToGlobal(ColorMode colorMode) 139{ 140 switch (colorMode) { 141 case ColorMode::DARK: 142 return Global::Resource::ColorMode::DARK; 143 case ColorMode::LIGHT: 144 return Global::Resource::ColorMode::LIGHT; 145 default: 146 return Global::Resource::ColorMode::COLOR_MODE_NOT_SET; 147 } 148} 149} // namespace OHOS::Ace 150