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/ohos/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_ciGlobal::Resource::ColorMode ConvertColorModeToGlobal(ColorMode colorMode) 7623b3eb3cSopenharmony_ci{ 7723b3eb3cSopenharmony_ci switch (colorMode) { 7823b3eb3cSopenharmony_ci case ColorMode::DARK: 7923b3eb3cSopenharmony_ci return Global::Resource::ColorMode::DARK; 8023b3eb3cSopenharmony_ci case ColorMode::LIGHT: 8123b3eb3cSopenharmony_ci return Global::Resource::ColorMode::LIGHT; 8223b3eb3cSopenharmony_ci default: 8323b3eb3cSopenharmony_ci return Global::Resource::ColorMode::COLOR_MODE_NOT_SET; 8423b3eb3cSopenharmony_ci } 8523b3eb3cSopenharmony_ci} 8623b3eb3cSopenharmony_ci 8723b3eb3cSopenharmony_ciGlobal::Resource::InputDevice ConvertInputDevice(bool deviceAccess) 8823b3eb3cSopenharmony_ci{ 8923b3eb3cSopenharmony_ci return deviceAccess ? Global::Resource::InputDevice::INPUTDEVICE_POINTINGDEVICE : 9023b3eb3cSopenharmony_ci Global::Resource::InputDevice::INPUTDEVICE_NOT_SET; 9123b3eb3cSopenharmony_ci} 9223b3eb3cSopenharmony_ci 9323b3eb3cSopenharmony_cistd::shared_ptr<Global::Resource::ResConfig> ConvertConfigToGlobal(const ResourceConfiguration& config) 9423b3eb3cSopenharmony_ci{ 9523b3eb3cSopenharmony_ci std::shared_ptr<Global::Resource::ResConfig> newResCfg(Global::Resource::CreateResConfig()); 9623b3eb3cSopenharmony_ci newResCfg->SetLocaleInfo(AceApplicationInfo::GetInstance().GetLanguage().c_str(), 9723b3eb3cSopenharmony_ci AceApplicationInfo::GetInstance().GetScript().c_str(), 9823b3eb3cSopenharmony_ci AceApplicationInfo::GetInstance().GetCountryOrRegion().c_str()); 9923b3eb3cSopenharmony_ci newResCfg->SetDeviceType(ConvertDeviceTypeToGlobal(config.GetDeviceType())); 10023b3eb3cSopenharmony_ci newResCfg->SetDirection(ConvertDirectionToGlobal(config.GetOrientation())); 10123b3eb3cSopenharmony_ci newResCfg->SetScreenDensity(config.GetDensity()); 10223b3eb3cSopenharmony_ci newResCfg->SetColorMode(ConvertColorModeToGlobal(config.GetColorMode())); 10323b3eb3cSopenharmony_ci newResCfg->SetInputDevice(ConvertInputDevice(config.GetDeviceAccess())); 10423b3eb3cSopenharmony_ci newResCfg->SetAppColorMode(config.GetColorModeIsSetByApp()); 10523b3eb3cSopenharmony_ci newResCfg->SetMcc(config.GetMcc()); 10623b3eb3cSopenharmony_ci newResCfg->SetMnc(config.GetMnc()); 10723b3eb3cSopenharmony_ci icu::Locale locale(config.GetPreferredLanguage().c_str()); 10823b3eb3cSopenharmony_ci newResCfg->SetPreferredLocaleInfo(locale); 10923b3eb3cSopenharmony_ci return newResCfg; 11023b3eb3cSopenharmony_ci} 11123b3eb3cSopenharmony_ci 11223b3eb3cSopenharmony_ciDeviceType ConvertDeviceTypeToAce(Global::Resource::DeviceType type) 11323b3eb3cSopenharmony_ci{ 11423b3eb3cSopenharmony_ci switch (type) { 11523b3eb3cSopenharmony_ci case Global::Resource::DeviceType::DEVICE_PHONE: 11623b3eb3cSopenharmony_ci return DeviceType::PHONE; 11723b3eb3cSopenharmony_ci case Global::Resource::DeviceType::DEVICE_TV: 11823b3eb3cSopenharmony_ci return DeviceType::TV; 11923b3eb3cSopenharmony_ci case Global::Resource::DeviceType::DEVICE_WEARABLE: 12023b3eb3cSopenharmony_ci return DeviceType::WATCH; 12123b3eb3cSopenharmony_ci case Global::Resource::DeviceType::DEVICE_CAR: 12223b3eb3cSopenharmony_ci return DeviceType::CAR; 12323b3eb3cSopenharmony_ci case Global::Resource::DeviceType::DEVICE_TABLET: 12423b3eb3cSopenharmony_ci return DeviceType::TABLET; 12523b3eb3cSopenharmony_ci default: 12623b3eb3cSopenharmony_ci return DeviceType::UNKNOWN; 12723b3eb3cSopenharmony_ci } 12823b3eb3cSopenharmony_ci} 12923b3eb3cSopenharmony_ci 13023b3eb3cSopenharmony_ciDeviceOrientation ConvertDirectionToAce(Global::Resource::Direction orientation) 13123b3eb3cSopenharmony_ci{ 13223b3eb3cSopenharmony_ci switch (orientation) { 13323b3eb3cSopenharmony_ci case Global::Resource::Direction::DIRECTION_VERTICAL: 13423b3eb3cSopenharmony_ci return DeviceOrientation::PORTRAIT; 13523b3eb3cSopenharmony_ci case Global::Resource::Direction::DIRECTION_HORIZONTAL: 13623b3eb3cSopenharmony_ci return DeviceOrientation::LANDSCAPE; 13723b3eb3cSopenharmony_ci default: 13823b3eb3cSopenharmony_ci return DeviceOrientation::ORIENTATION_UNDEFINED; 13923b3eb3cSopenharmony_ci } 14023b3eb3cSopenharmony_ci} 14123b3eb3cSopenharmony_ci 14223b3eb3cSopenharmony_cidouble ConvertDensityToAce(Global::Resource::ScreenDensity density) 14323b3eb3cSopenharmony_ci{ 14423b3eb3cSopenharmony_ci switch (density) { 14523b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_SDPI: 14623b3eb3cSopenharmony_ci return 120.0 / DPI_BASE; 14723b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_MDPI: 14823b3eb3cSopenharmony_ci return 160.0 / DPI_BASE; 14923b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_LDPI: 15023b3eb3cSopenharmony_ci return 240.0 / DPI_BASE; 15123b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_XLDPI: 15223b3eb3cSopenharmony_ci return 320.0 / DPI_BASE; 15323b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXLDPI: 15423b3eb3cSopenharmony_ci return 480.0 / DPI_BASE; 15523b3eb3cSopenharmony_ci case Global::Resource::ScreenDensity::SCREEN_DENSITY_XXXLDPI: 15623b3eb3cSopenharmony_ci return 640.0 / DPI_BASE; 15723b3eb3cSopenharmony_ci default: 15823b3eb3cSopenharmony_ci return 0.0; 15923b3eb3cSopenharmony_ci } 16023b3eb3cSopenharmony_ci} 16123b3eb3cSopenharmony_ci 16223b3eb3cSopenharmony_ci} // namespace OHOS::Ace