123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#include "adapter/preview/osal/resource_convertor.h" 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_cinamespace OHOS::Ace { 1923b3eb3cSopenharmony_ci 2023b3eb3cSopenharmony_ciGlobal::Resource::DeviceType ConvertDeviceTypeToGlobal(DeviceType type) 2123b3eb3cSopenharmony_ci{ 2223b3eb3cSopenharmony_ci switch (type) { 2323b3eb3cSopenharmony_ci case DeviceType::PHONE: 2423b3eb3cSopenharmony_ci return Global::Resource::DeviceType::DEVICE_PHONE; 2523b3eb3cSopenharmony_ci case DeviceType::TV: 2623b3eb3cSopenharmony_ci return Global::Resource::DeviceType::DEVICE_TV; 2723b3eb3cSopenharmony_ci case DeviceType::WATCH: 2823b3eb3cSopenharmony_ci return Global::Resource::DeviceType::DEVICE_WEARABLE; 2923b3eb3cSopenharmony_ci case DeviceType::CAR: 3023b3eb3cSopenharmony_ci return Global::Resource::DeviceType::DEVICE_CAR; 3123b3eb3cSopenharmony_ci case DeviceType::TABLET: 3223b3eb3cSopenharmony_ci return Global::Resource::DeviceType::DEVICE_TABLET; 3323b3eb3cSopenharmony_ci case DeviceType::TWO_IN_ONE: 3423b3eb3cSopenharmony_ci return Global::Resource::DeviceType::DEVICE_TWOINONE; 3523b3eb3cSopenharmony_ci case DeviceType::WEARABLE: 3623b3eb3cSopenharmony_ci return Global::Resource::DeviceType::DEVICE_WEARABLE; 3723b3eb3cSopenharmony_ci default: 3823b3eb3cSopenharmony_ci return Global::Resource::DeviceType::DEVICE_NOT_SET; 3923b3eb3cSopenharmony_ci } 4023b3eb3cSopenharmony_ci} 4123b3eb3cSopenharmony_ci 4223b3eb3cSopenharmony_ciGlobal::Resource::Direction ConvertDirectionToGlobal(DeviceOrientation orientation) 4323b3eb3cSopenharmony_ci{ 4423b3eb3cSopenharmony_ci switch (orientation) { 4523b3eb3cSopenharmony_ci case DeviceOrientation::PORTRAIT: 4623b3eb3cSopenharmony_ci return Global::Resource::Direction::DIRECTION_VERTICAL; 4723b3eb3cSopenharmony_ci case DeviceOrientation::LANDSCAPE: 4823b3eb3cSopenharmony_ci return Global::Resource::Direction::DIRECTION_HORIZONTAL; 4923b3eb3cSopenharmony_ci default: 5023b3eb3cSopenharmony_ci return Global::Resource::Direction::DIRECTION_NOT_SET; 5123b3eb3cSopenharmony_ci } 5223b3eb3cSopenharmony_ci} 5323b3eb3cSopenharmony_ci 5423b3eb3cSopenharmony_ciGlobal::Resource::ScreenDensity ConvertDensityToGlobal(double density) 5523b3eb3cSopenharmony_ci{ 5623b3eb3cSopenharmony_ci static const std::vector<std::pair<double, Global::Resource::ScreenDensity>> resolutions = { 5723b3eb3cSopenharmony_ci { 0.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET }, 5823b3eb3cSopenharmony_ci { 120.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI }, 5923b3eb3cSopenharmony_ci { 160.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI }, 6023b3eb3cSopenharmony_ci { 240.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI }, 6123b3eb3cSopenharmony_ci { 320.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI }, 6223b3eb3cSopenharmony_ci { 480.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI }, 6323b3eb3cSopenharmony_ci { 640.0, Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI }, 6423b3eb3cSopenharmony_ci }; 6523b3eb3cSopenharmony_ci double deviceDpi = density * DPI_BASE; 6623b3eb3cSopenharmony_ci auto resolution = Global::Resource::ScreenDensity::SCREEN_DENSITY_NOT_SET; 6723b3eb3cSopenharmony_ci for (const auto& [dpi, value] : resolutions) { 6823b3eb3cSopenharmony_ci resolution = value; 6923b3eb3cSopenharmony_ci if (LessOrEqual(deviceDpi, dpi)) { 7023b3eb3cSopenharmony_ci break; 7123b3eb3cSopenharmony_ci } 7223b3eb3cSopenharmony_ci } 7323b3eb3cSopenharmony_ci return resolution; 7423b3eb3cSopenharmony_ci} 7523b3eb3cSopenharmony_ci 7623b3eb3cSopenharmony_cistd::shared_ptr<Global::Resource::ResConfig> ConvertConfigToGlobal(const ResourceConfiguration& config) 7723b3eb3cSopenharmony_ci{ 7823b3eb3cSopenharmony_ci std::shared_ptr<Global::Resource::ResConfig> newResCfg(Global::Resource::CreateResConfig()); 7923b3eb3cSopenharmony_ci newResCfg->SetLocaleInfo(AceApplicationInfo::GetInstance().GetLanguage().c_str(), 8023b3eb3cSopenharmony_ci AceApplicationInfo::GetInstance().GetScript().c_str(), 8123b3eb3cSopenharmony_ci AceApplicationInfo::GetInstance().GetCountryOrRegion().c_str()); 8223b3eb3cSopenharmony_ci newResCfg->SetDeviceType(ConvertDeviceTypeToGlobal(config.GetDeviceType())); 8323b3eb3cSopenharmony_ci newResCfg->SetDirection(ConvertDirectionToGlobal(config.GetOrientation())); 8423b3eb3cSopenharmony_ci newResCfg->SetScreenDensity(config.GetDensity()); 8523b3eb3cSopenharmony_ci return newResCfg; 8623b3eb3cSopenharmony_ci} 8723b3eb3cSopenharmony_ci 8823b3eb3cSopenharmony_ciDeviceType ConvertDeviceTypeToAce(Global::Resource::DeviceType type) 8923b3eb3cSopenharmony_ci{ 9023b3eb3cSopenharmony_ci switch (type) { 9123b3eb3cSopenharmony_ci case Global::Resource::DeviceType::DEVICE_PHONE: 9223b3eb3cSopenharmony_ci return DeviceType::PHONE; 9323b3eb3cSopenharmony_ci case Global::Resource::DeviceType::DEVICE_TV: 9423b3eb3cSopenharmony_ci return DeviceType::TV; 9523b3eb3cSopenharmony_ci case Global::Resource::DeviceType::DEVICE_WEARABLE: 9623b3eb3cSopenharmony_ci return DeviceType::WATCH; 9723b3eb3cSopenharmony_ci case Global::Resource::DeviceType::DEVICE_CAR: 9823b3eb3cSopenharmony_ci return DeviceType::CAR; 9923b3eb3cSopenharmony_ci case Global::Resource::DeviceType::DEVICE_TABLET: 10023b3eb3cSopenharmony_ci return DeviceType::TABLET; 10123b3eb3cSopenharmony_ci default: 10223b3eb3cSopenharmony_ci return DeviceType::UNKNOWN; 10323b3eb3cSopenharmony_ci } 10423b3eb3cSopenharmony_ci} 10523b3eb3cSopenharmony_ci 10623b3eb3cSopenharmony_ciDeviceOrientation ConvertDirectionToAce(Global::Resource::Direction orientation) 10723b3eb3cSopenharmony_ci{ 10823b3eb3cSopenharmony_ci switch (orientation) { 10923b3eb3cSopenharmony_ci case Global::Resource::Direction::DIRECTION_VERTICAL: 11023b3eb3cSopenharmony_ci return DeviceOrientation::PORTRAIT; 11123b3eb3cSopenharmony_ci case Global::Resource::Direction::DIRECTION_HORIZONTAL: 11223b3eb3cSopenharmony_ci return DeviceOrientation::LANDSCAPE; 11323b3eb3cSopenharmony_ci default: 11423b3eb3cSopenharmony_ci return DeviceOrientation::ORIENTATION_UNDEFINED; 11523b3eb3cSopenharmony_ci } 11623b3eb3cSopenharmony_ci} 11723b3eb3cSopenharmony_ci 11823b3eb3cSopenharmony_cidouble ConvertDensityToAce(Global::Resource::ScreenDensity density) 11923b3eb3cSopenharmony_ci{ 12023b3eb3cSopenharmony_ci switch (density) { 12123b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI: 12223b3eb3cSopenharmony_ci return 120.0 / DPI_BASE; 12323b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI: 12423b3eb3cSopenharmony_ci return 160.0 / DPI_BASE; 12523b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI: 12623b3eb3cSopenharmony_ci return 240.0 / DPI_BASE; 12723b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI: 12823b3eb3cSopenharmony_ci return 320.0 / DPI_BASE; 12923b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI: 13023b3eb3cSopenharmony_ci return 480.0 / DPI_BASE; 13123b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI: 13223b3eb3cSopenharmony_ci return 640.0 / DPI_BASE; 13323b3eb3cSopenharmony_ci default: 13423b3eb3cSopenharmony_ci return 0.0; 13523b3eb3cSopenharmony_ci } 13623b3eb3cSopenharmony_ci} 13723b3eb3cSopenharmony_ci 13823b3eb3cSopenharmony_ciGlobal::Resource::ColorMode ConvertColorModeToGlobal(ColorMode colorMode) 13923b3eb3cSopenharmony_ci{ 14023b3eb3cSopenharmony_ci switch (colorMode) { 14123b3eb3cSopenharmony_ci case ColorMode::DARK: 14223b3eb3cSopenharmony_ci return Global::Resource::ColorMode::DARK; 14323b3eb3cSopenharmony_ci case ColorMode::LIGHT: 14423b3eb3cSopenharmony_ci return Global::Resource::ColorMode::LIGHT; 14523b3eb3cSopenharmony_ci default: 14623b3eb3cSopenharmony_ci return Global::Resource::ColorMode::COLOR_MODE_NOT_SET; 14723b3eb3cSopenharmony_ci } 14823b3eb3cSopenharmony_ci} 14923b3eb3cSopenharmony_ci} // namespace OHOS::Ace 150