123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021-2022 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_adapter_impl.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include <set>
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_ci#include "base/i18n/localization.h"
2123b3eb3cSopenharmony_ci#include "base/log/log.h"
2223b3eb3cSopenharmony_ci#include "base/utils/system_properties.h"
2323b3eb3cSopenharmony_ci#include "core/common/ace_application_info.h"
2423b3eb3cSopenharmony_ci#include "core/common/container.h"
2523b3eb3cSopenharmony_ci#include "core/components/common/layout/constants.h"
2623b3eb3cSopenharmony_ci#include "core/components/theme/theme_attributes.h"
2723b3eb3cSopenharmony_ci
2823b3eb3cSopenharmony_cinamespace OHOS::Ace {
2923b3eb3cSopenharmony_cinamespace {
3023b3eb3cSopenharmony_ci
3123b3eb3cSopenharmony_ciconstexpr char COLOR_VALUE_PREFIX[] = "$color:";
3223b3eb3cSopenharmony_ciconstexpr char PATTERN_NAME_KEY_WORD[] = "$pattern:";
3323b3eb3cSopenharmony_ciconstexpr char STATE_VALUE_KEY_WORD[] = ".sxml";
3423b3eb3cSopenharmony_ciconstexpr char REF_ATTR_VALUE_KEY_WORD[] = "?theme:";
3523b3eb3cSopenharmony_ciconstexpr char STATE_CONTAINER[] = "state-container";
3623b3eb3cSopenharmony_ciconstexpr char STATE_ELEMENT[] = "element";
3723b3eb3cSopenharmony_ciconstexpr uint32_t STATE_MAX = 128;
3823b3eb3cSopenharmony_ciconstexpr double DPI_BASE = 160.0;
3923b3eb3cSopenharmony_ciconstexpr uint32_t THEME_ID_LIGHT = 117440515;
4023b3eb3cSopenharmony_ciconstexpr uint32_t THEME_ID_DARK = 117440516;
4123b3eb3cSopenharmony_ci
4223b3eb3cSopenharmony_civoid CheckThemeId(int32_t& themeId)
4323b3eb3cSopenharmony_ci{
4423b3eb3cSopenharmony_ci    if (themeId >= 0) {
4523b3eb3cSopenharmony_ci        return;
4623b3eb3cSopenharmony_ci    }
4723b3eb3cSopenharmony_ci    auto deviceType = SystemProperties::GetDeviceType();
4823b3eb3cSopenharmony_ci    themeId = (deviceType == DeviceType::PHONE || deviceType == DeviceType::UNKNOWN || deviceType == DeviceType::CAR)
4923b3eb3cSopenharmony_ci                  ? THEME_ID_LIGHT
5023b3eb3cSopenharmony_ci                  : THEME_ID_DARK;
5123b3eb3cSopenharmony_ci}
5223b3eb3cSopenharmony_ci
5323b3eb3cSopenharmony_ciDimensionUnit ParseDimensionUnit(const std::string& unit)
5423b3eb3cSopenharmony_ci{
5523b3eb3cSopenharmony_ci    if (unit == "px") {
5623b3eb3cSopenharmony_ci        return DimensionUnit::PX;
5723b3eb3cSopenharmony_ci    } else if (unit == "fp") {
5823b3eb3cSopenharmony_ci        return DimensionUnit::FP;
5923b3eb3cSopenharmony_ci    } else if (unit == "lpx") {
6023b3eb3cSopenharmony_ci        return DimensionUnit::LPX;
6123b3eb3cSopenharmony_ci    } else if (unit == "%") {
6223b3eb3cSopenharmony_ci        return DimensionUnit::PERCENT;
6323b3eb3cSopenharmony_ci    } else {
6423b3eb3cSopenharmony_ci        return DimensionUnit::VP;
6523b3eb3cSopenharmony_ci    }
6623b3eb3cSopenharmony_ci};
6723b3eb3cSopenharmony_ci
6823b3eb3cSopenharmony_ciGlobal::Resource::ORIENTATION ConvertOrientation(DeviceOrientation orientation)
6923b3eb3cSopenharmony_ci{
7023b3eb3cSopenharmony_ci    return orientation == DeviceOrientation::PORTRAIT ? Global::Resource::ORIENTATION::ORIENTATION_PORTRAIT
7123b3eb3cSopenharmony_ci                                                      : Global::Resource::ORIENTATION::ORIENTATION_LANDSCAPE;
7223b3eb3cSopenharmony_ci}
7323b3eb3cSopenharmony_ci
7423b3eb3cSopenharmony_ciGlobal::Resource::RESOLUTION ConvertResolution(double density)
7523b3eb3cSopenharmony_ci{
7623b3eb3cSopenharmony_ci    static const std::vector<std::pair<double, Global::Resource::RESOLUTION>> resolutions = {
7723b3eb3cSopenharmony_ci        { 120.0, Global::Resource::RESOLUTION::RESOLUTION_LOW },
7823b3eb3cSopenharmony_ci        { 160.0, Global::Resource::RESOLUTION::RESOLUTION_MEDIUM },
7923b3eb3cSopenharmony_ci        { 240.0, Global::Resource::RESOLUTION::RESOLUTION_HIGH },
8023b3eb3cSopenharmony_ci        { 320.0, Global::Resource::RESOLUTION::RESOLUTION_XHIGH },
8123b3eb3cSopenharmony_ci        { 480.0, Global::Resource::RESOLUTION::RESOLUTION_XXHIGH },
8223b3eb3cSopenharmony_ci        { 640.0, Global::Resource::RESOLUTION::RESOLUTION_XXXHIGH },
8323b3eb3cSopenharmony_ci    };
8423b3eb3cSopenharmony_ci    double deviceDpi = density * DPI_BASE;
8523b3eb3cSopenharmony_ci    auto resolution = Global::Resource::RESOLUTION::RESOLUTION_LOW;
8623b3eb3cSopenharmony_ci    for (const auto& [dpi, value] : resolutions) {
8723b3eb3cSopenharmony_ci        resolution = value;
8823b3eb3cSopenharmony_ci        if (LessOrEqual(deviceDpi, dpi)) {
8923b3eb3cSopenharmony_ci            break;
9023b3eb3cSopenharmony_ci        }
9123b3eb3cSopenharmony_ci    }
9223b3eb3cSopenharmony_ci    return resolution;
9323b3eb3cSopenharmony_ci}
9423b3eb3cSopenharmony_ci
9523b3eb3cSopenharmony_ciGlobal::Resource::DEVICE_TYPE ConvertDeviceType(DeviceType type)
9623b3eb3cSopenharmony_ci{
9723b3eb3cSopenharmony_ci    switch (type) {
9823b3eb3cSopenharmony_ci        case DeviceType::PHONE:
9923b3eb3cSopenharmony_ci            return Global::Resource::DEVICE_TYPE::DEVICE_TYPE_PHONE;
10023b3eb3cSopenharmony_ci        case DeviceType::TV:
10123b3eb3cSopenharmony_ci            return Global::Resource::DEVICE_TYPE::DEVICE_TYPE_TV;
10223b3eb3cSopenharmony_ci        case DeviceType::WATCH:
10323b3eb3cSopenharmony_ci            return Global::Resource::DEVICE_TYPE::DEVICE_TYPE_WATCH;
10423b3eb3cSopenharmony_ci        case DeviceType::CAR:
10523b3eb3cSopenharmony_ci            return Global::Resource::DEVICE_TYPE::DEVICE_TYPE_CAR;
10623b3eb3cSopenharmony_ci        case DeviceType::TABLET:
10723b3eb3cSopenharmony_ci            return Global::Resource::DEVICE_TYPE::DEVICE_TYPE_TABLET;
10823b3eb3cSopenharmony_ci        default:
10923b3eb3cSopenharmony_ci            return Global::Resource::DEVICE_TYPE::DEVICE_TYPE_UNDEFINED;
11023b3eb3cSopenharmony_ci    }
11123b3eb3cSopenharmony_ci}
11223b3eb3cSopenharmony_ci
11323b3eb3cSopenharmony_ciGlobal::Resource::COLOR_MODE ConvertColorMode(ColorMode colorMode)
11423b3eb3cSopenharmony_ci{
11523b3eb3cSopenharmony_ci    return colorMode == ColorMode::LIGHT ? Global::Resource::COLOR_MODE::COLOR_MODE_LIGHT
11623b3eb3cSopenharmony_ci                                         : Global::Resource::COLOR_MODE::COLOR_MODE_DARK;
11723b3eb3cSopenharmony_ci}
11823b3eb3cSopenharmony_ci
11923b3eb3cSopenharmony_ciGlobal::Resource::Configuration ConvertConfig(const ResourceConfiguration& config)
12023b3eb3cSopenharmony_ci{
12123b3eb3cSopenharmony_ci    Global::Resource::Configuration::Locale locale(AceApplicationInfo::GetInstance().GetLanguage(),
12223b3eb3cSopenharmony_ci        AceApplicationInfo::GetInstance().GetCountryOrRegion(), AceApplicationInfo::GetInstance().GetScript());
12323b3eb3cSopenharmony_ci    Global::Resource::Configuration globalConfig = {
12423b3eb3cSopenharmony_ci        .locales_ = { locale },
12523b3eb3cSopenharmony_ci        .orientation_ = ConvertOrientation(config.GetOrientation()),
12623b3eb3cSopenharmony_ci        .resolution_ = ConvertResolution(config.GetDensity()),
12723b3eb3cSopenharmony_ci        .deviceType_ = ConvertDeviceType(config.GetDeviceType()),
12823b3eb3cSopenharmony_ci        .fontRatio_ = config.GetFontRatio(),
12923b3eb3cSopenharmony_ci        .colorMode_ = ConvertColorMode(config.GetColorMode()),
13023b3eb3cSopenharmony_ci    };
13123b3eb3cSopenharmony_ci    return globalConfig;
13223b3eb3cSopenharmony_ci}
13323b3eb3cSopenharmony_ci
13423b3eb3cSopenharmony_ciRefPtr<StateResource> ParseStateResource(const std::string& styleName, const std::string& attrName,
13523b3eb3cSopenharmony_ci    std::unique_ptr<Global::Resource::SolidXmlWrapper> xmlWrapper)
13623b3eb3cSopenharmony_ci{
13723b3eb3cSopenharmony_ci    auto rootNode = xmlWrapper->GetRoot();
13823b3eb3cSopenharmony_ci    if (!rootNode) {
13923b3eb3cSopenharmony_ci        LOGE("Parse %{public}s state resource %{public}s error! No root!", styleName.c_str(), attrName.c_str());
14023b3eb3cSopenharmony_ci        return nullptr;
14123b3eb3cSopenharmony_ci    }
14223b3eb3cSopenharmony_ci    if (rootNode->GetNodeName() != STATE_CONTAINER) {
14323b3eb3cSopenharmony_ci        return nullptr;
14423b3eb3cSopenharmony_ci    }
14523b3eb3cSopenharmony_ci    auto stateResource = AceType::MakeRefPtr<StateResource>();
14623b3eb3cSopenharmony_ci    auto node = rootNode->GetChild();
14723b3eb3cSopenharmony_ci    uint32_t stateCount = 0;
14823b3eb3cSopenharmony_ci    while (node && ++stateCount < STATE_MAX) {
14923b3eb3cSopenharmony_ci        // Parse each item
15023b3eb3cSopenharmony_ci        auto nodeAttrs = node->GetAttributes();
15123b3eb3cSopenharmony_ci        auto valueFindIter = nodeAttrs.find(STATE_ELEMENT);
15223b3eb3cSopenharmony_ci        if (valueFindIter == nodeAttrs.end()) {
15323b3eb3cSopenharmony_ci            continue;
15423b3eb3cSopenharmony_ci        }
15523b3eb3cSopenharmony_ci        auto stateColor = Color(valueFindIter->second.GetColorValue());
15623b3eb3cSopenharmony_ci        uint32_t state = STATE_NORMAL;
15723b3eb3cSopenharmony_ci        static const std::unordered_map<std::string, uint32_t> stateMap = {
15823b3eb3cSopenharmony_ci            { "state_pressed", STATE_PRESSED },
15923b3eb3cSopenharmony_ci            { "state_focus", STATE_FOCUS },
16023b3eb3cSopenharmony_ci            { "state_checked", STATE_CHECKED },
16123b3eb3cSopenharmony_ci            { "state_disabled", STATE_DISABLED },
16223b3eb3cSopenharmony_ci            { "state_waiting", STATE_WAITING },
16323b3eb3cSopenharmony_ci            { "state_hovered", STATE_HOVERED },
16423b3eb3cSopenharmony_ci        };
16523b3eb3cSopenharmony_ci        for (auto& [stateKey, stateValue] : nodeAttrs) {
16623b3eb3cSopenharmony_ci            auto stateFindIter = stateMap.find(stateKey);
16723b3eb3cSopenharmony_ci            if (stateFindIter == stateMap.end()) {
16823b3eb3cSopenharmony_ci                continue;
16923b3eb3cSopenharmony_ci            }
17023b3eb3cSopenharmony_ci            if (stateValue.GetStringValue() != "true") {
17123b3eb3cSopenharmony_ci                continue;
17223b3eb3cSopenharmony_ci            }
17323b3eb3cSopenharmony_ci            state |= stateFindIter->second;
17423b3eb3cSopenharmony_ci        }
17523b3eb3cSopenharmony_ci        stateResource->SetStateValue(state, { .type = ThemeConstantsType::COLOR, .value = stateColor });
17623b3eb3cSopenharmony_ci        node = node->GetSibling();
17723b3eb3cSopenharmony_ci    }
17823b3eb3cSopenharmony_ci    return stateResource;
17923b3eb3cSopenharmony_ci}
18023b3eb3cSopenharmony_ci} // namespace
18123b3eb3cSopenharmony_ci
18223b3eb3cSopenharmony_ciclass RawThemeStyle : public ThemeStyle {
18323b3eb3cSopenharmony_ci    DECLARE_ACE_TYPE(RawThemeStyle, ThemeStyle);
18423b3eb3cSopenharmony_ci
18523b3eb3cSopenharmony_cipublic:
18623b3eb3cSopenharmony_ci    friend class ResourceAdapterImpl;
18723b3eb3cSopenharmony_ci    using RawAttrMap = std::unordered_map<std::string, std::unique_ptr<Global::Resource::TypeAttribute>>;
18823b3eb3cSopenharmony_ci
18923b3eb3cSopenharmony_ci    explicit RawThemeStyle(RefPtr<ResourceAdapter> resAdapter) : resAdapter_(resAdapter) {}
19023b3eb3cSopenharmony_ci    ~RawThemeStyle() override = default;
19123b3eb3cSopenharmony_ci
19223b3eb3cSopenharmony_ci    void ParseContent() override;
19323b3eb3cSopenharmony_ci
19423b3eb3cSopenharmony_ciprivate:
19523b3eb3cSopenharmony_ci    RawAttrMap rawAttrs_; // key and value read from global resource api.
19623b3eb3cSopenharmony_ci    RefPtr<ResourceAdapter> resAdapter_;
19723b3eb3cSopenharmony_ci};
19823b3eb3cSopenharmony_ci
19923b3eb3cSopenharmony_civoid RawThemeStyle::ParseContent()
20023b3eb3cSopenharmony_ci{
20123b3eb3cSopenharmony_ci    static const std::set<std::string> stringAttrs = {
20223b3eb3cSopenharmony_ci        "attr_text_font_family_regular",
20323b3eb3cSopenharmony_ci        "attr_text_font_family_medium"
20423b3eb3cSopenharmony_ci    };
20523b3eb3cSopenharmony_ci    for (auto& [attrName, attrValue] : rawAttrs_) {
20623b3eb3cSopenharmony_ci        if (!attrValue) {
20723b3eb3cSopenharmony_ci            continue;
20823b3eb3cSopenharmony_ci        }
20923b3eb3cSopenharmony_ci        auto rawString = attrValue->GetOriginalValue();
21023b3eb3cSopenharmony_ci        if (rawString.size() == 0) {
21123b3eb3cSopenharmony_ci            continue;
21223b3eb3cSopenharmony_ci        }
21323b3eb3cSopenharmony_ci        if (rawString.front() == '#' || rawString.find(COLOR_VALUE_PREFIX) != std::string::npos) {
21423b3eb3cSopenharmony_ci            // color
21523b3eb3cSopenharmony_ci            attributes_[attrName] = { .type = ThemeConstantsType::COLOR, .value = Color(attrValue->GetColorValue()) };
21623b3eb3cSopenharmony_ci        } else if (stringAttrs.find(attrName) != stringAttrs.end()) {
21723b3eb3cSopenharmony_ci            // string
21823b3eb3cSopenharmony_ci            attributes_[attrName] = { .type = ThemeConstantsType::STRING, .value = rawString };
21923b3eb3cSopenharmony_ci        } else if (rawString.find(PATTERN_NAME_KEY_WORD) != std::string::npos) {
22023b3eb3cSopenharmony_ci            // pattern
22123b3eb3cSopenharmony_ci            auto patternStyle = AceType::MakeRefPtr<RawThemeStyle>(resAdapter_);
22223b3eb3cSopenharmony_ci            patternStyle->SetName(attrName);
22323b3eb3cSopenharmony_ci            patternStyle->parentStyle_ = AceType::WeakClaim(this);
22423b3eb3cSopenharmony_ci            patternStyle->rawAttrs_ = attrValue->GetPattern();
22523b3eb3cSopenharmony_ci            patternStyle->ParseContent();
22623b3eb3cSopenharmony_ci            attributes_[attrName] = { .type = ThemeConstantsType::PATTERN,
22723b3eb3cSopenharmony_ci                .value = RefPtr<ThemeStyle>(std::move(patternStyle)) };
22823b3eb3cSopenharmony_ci        } else if (rawString.rfind(STATE_VALUE_KEY_WORD) != std::string::npos) {
22923b3eb3cSopenharmony_ci            // state graphic value
23023b3eb3cSopenharmony_ci            auto xmlWrapper = attrValue->GetLayoutValue();
23123b3eb3cSopenharmony_ci            if (!xmlWrapper) {
23223b3eb3cSopenharmony_ci                LOGW("Parse %{public}s state resource %{public}s error! xml is null!", name_.c_str(), attrName.c_str());
23323b3eb3cSopenharmony_ci                continue;
23423b3eb3cSopenharmony_ci            }
23523b3eb3cSopenharmony_ci            auto stateResource = ParseStateResource(name_, attrName, std::move(xmlWrapper));
23623b3eb3cSopenharmony_ci            if (!stateResource) {
23723b3eb3cSopenharmony_ci                continue;
23823b3eb3cSopenharmony_ci            }
23923b3eb3cSopenharmony_ci            attributes_[attrName] = { .type = ThemeConstantsType::STATE_RESOURCE, .value = stateResource };
24023b3eb3cSopenharmony_ci        } else if (rawString.find(REF_ATTR_VALUE_KEY_WORD) != std::string::npos) {
24123b3eb3cSopenharmony_ci            attributes_[attrName] = { .type = ThemeConstantsType::REFERENCE_ATTR, .value = rawString };
24223b3eb3cSopenharmony_ci        } else {
24323b3eb3cSopenharmony_ci            // double & dimension
24423b3eb3cSopenharmony_ci            std::string unit = "";
24523b3eb3cSopenharmony_ci            auto doubleValue = static_cast<double>(attrValue->GetFloat(unit));
24623b3eb3cSopenharmony_ci            if (unit.empty()) {
24723b3eb3cSopenharmony_ci                attributes_[attrName] = { .type = ThemeConstantsType::DOUBLE, .value = doubleValue };
24823b3eb3cSopenharmony_ci            } else {
24923b3eb3cSopenharmony_ci                attributes_[attrName] = { .type = ThemeConstantsType::DIMENSION,
25023b3eb3cSopenharmony_ci                    .value = Dimension(doubleValue, ParseDimensionUnit(unit)) };
25123b3eb3cSopenharmony_ci            }
25223b3eb3cSopenharmony_ci        }
25323b3eb3cSopenharmony_ci    }
25423b3eb3cSopenharmony_ci}
25523b3eb3cSopenharmony_ci
25623b3eb3cSopenharmony_ciRefPtr<ResourceAdapter> ResourceAdapter::Create()
25723b3eb3cSopenharmony_ci{
25823b3eb3cSopenharmony_ci    auto deviceType = SystemProperties::GetDeviceType();
25923b3eb3cSopenharmony_ci    if (deviceType == DeviceType::PHONE || deviceType == DeviceType::CAR || deviceType == DeviceType::TABLET ||
26023b3eb3cSopenharmony_ci        deviceType == DeviceType::TWO_IN_ONE) {
26123b3eb3cSopenharmony_ci        return AceType::MakeRefPtr<ResourceAdapterImpl>();
26223b3eb3cSopenharmony_ci    }
26323b3eb3cSopenharmony_ci    return RefPtr<ResourceAdapter>();
26423b3eb3cSopenharmony_ci}
26523b3eb3cSopenharmony_ci
26623b3eb3cSopenharmony_ciRefPtr<ResourceAdapter> ResourceAdapter::CreateNewResourceAdapter(
26723b3eb3cSopenharmony_ci    const std::string& bundleName, const std::string& moduleName)
26823b3eb3cSopenharmony_ci{
26923b3eb3cSopenharmony_ci    TAG_LOGW(AceLogTag::ACE_RESOURCE,
27023b3eb3cSopenharmony_ci        "Cannot preview the component from the %{public}s module, because it contains a resource reference. Preview it "
27123b3eb3cSopenharmony_ci        "in the %{public}s module instead.",
27223b3eb3cSopenharmony_ci        moduleName.c_str(), moduleName.c_str());
27323b3eb3cSopenharmony_ci    return nullptr;
27423b3eb3cSopenharmony_ci}
27523b3eb3cSopenharmony_ci
27623b3eb3cSopenharmony_ciResourceAdapterImpl::ResourceAdapterImpl(std::shared_ptr<Global::Resource::ResourceManager> resourceManager)
27723b3eb3cSopenharmony_ci{
27823b3eb3cSopenharmony_ci    resourceManager_ = resourceManager;
27923b3eb3cSopenharmony_ci    sysResourceManager_ = resourceManager;
28023b3eb3cSopenharmony_ci}
28123b3eb3cSopenharmony_ci
28223b3eb3cSopenharmony_civoid ResourceAdapterImpl::Init(const ResourceInfo& resourceInfo)
28323b3eb3cSopenharmony_ci{
28423b3eb3cSopenharmony_ci    std::vector<std::string> hapFiles;
28523b3eb3cSopenharmony_ci    hapFiles.emplace_back(resourceInfo.GetPackagePath());
28623b3eb3cSopenharmony_ci    auto configuration = ConvertConfig(resourceInfo.GetResourceConfiguration());
28723b3eb3cSopenharmony_ci    auto handlers = resourceInfo.GetResourceHandlers();
28823b3eb3cSopenharmony_ci    bool initRet = false;
28923b3eb3cSopenharmony_ci    if (handlers.empty()) {
29023b3eb3cSopenharmony_ci        initRet = resourceManger_.InitMock(hapFiles, resourceInfo.GetSystemPackagePath(), configuration);
29123b3eb3cSopenharmony_ci    } else {
29223b3eb3cSopenharmony_ci        initRet = resourceManger_.Init(hapFiles, handlers);
29323b3eb3cSopenharmony_ci        resourceManger_.UpdateConfig(configuration);
29423b3eb3cSopenharmony_ci    }
29523b3eb3cSopenharmony_ci    LOGI("Init result=%{public}d, handle=%{public}zu, ori=%{public}d, dpi=%{public}f, device=%{public}d, "
29623b3eb3cSopenharmony_ci         "font=%{public}f, color=%{public}d",
29723b3eb3cSopenharmony_ci        initRet, handlers.size(), configuration.orientation_, configuration.resolution_, configuration.deviceType_,
29823b3eb3cSopenharmony_ci        configuration.fontRatio_, configuration.colorMode_);
29923b3eb3cSopenharmony_ci}
30023b3eb3cSopenharmony_ci
30123b3eb3cSopenharmony_civoid ResourceAdapterImpl::UpdateConfig(const ResourceConfiguration& config, bool themeFlag)
30223b3eb3cSopenharmony_ci{
30323b3eb3cSopenharmony_ci    auto configuration = ConvertConfig(config);
30423b3eb3cSopenharmony_ci    LOGI("UpdateConfig ori=%{public}d, dpi=%{public}f, device=%{public}d, font=%{public}f, color=%{public}d",
30523b3eb3cSopenharmony_ci        configuration.orientation_, configuration.resolution_, configuration.deviceType_, configuration.fontRatio_,
30623b3eb3cSopenharmony_ci        configuration.colorMode_);
30723b3eb3cSopenharmony_ci    resourceManger_.UpdateConfig(configuration, themeFlag);
30823b3eb3cSopenharmony_ci}
30923b3eb3cSopenharmony_ci
31023b3eb3cSopenharmony_ciRefPtr<ThemeStyle> ResourceAdapterImpl::GetTheme(int32_t themeId)
31123b3eb3cSopenharmony_ci{
31223b3eb3cSopenharmony_ci    CheckThemeId(themeId);
31323b3eb3cSopenharmony_ci    auto theme = AceType::MakeRefPtr<RawThemeStyle>(AceType::Claim(this));
31423b3eb3cSopenharmony_ci    auto& attrMap = theme->rawAttrs_;
31523b3eb3cSopenharmony_ci    auto ret = resourceManger_.GetTheme(themeId, attrMap);
31623b3eb3cSopenharmony_ci    LOGI("GetTheme themeId=%{public}d, ret=%{public}d, attr size=%{public}zu", themeId, ret, attrMap.size());
31723b3eb3cSopenharmony_ci    auto iter = attrMap.find(THEME_ATTR_BG_COLOR);
31823b3eb3cSopenharmony_ci    if (iter != attrMap.end()) {
31923b3eb3cSopenharmony_ci        auto& attribute = iter->second;
32023b3eb3cSopenharmony_ci        if (attribute) {
32123b3eb3cSopenharmony_ci            Color bgColor(attribute->GetColorValue());
32223b3eb3cSopenharmony_ci            theme->SetAttr(THEME_ATTR_BG_COLOR, { .type = ThemeConstantsType::COLOR, .value = bgColor });
32323b3eb3cSopenharmony_ci        }
32423b3eb3cSopenharmony_ci    }
32523b3eb3cSopenharmony_ci    return theme;
32623b3eb3cSopenharmony_ci}
32723b3eb3cSopenharmony_ci
32823b3eb3cSopenharmony_ciColor ResourceAdapterImpl::GetColor(uint32_t resId)
32923b3eb3cSopenharmony_ci{
33023b3eb3cSopenharmony_ci    uint32_t result = 0;
33123b3eb3cSopenharmony_ci    auto ret = resourceManger_.GetColor(static_cast<int32_t>(resId), result);
33223b3eb3cSopenharmony_ci    if (!ret) {
33323b3eb3cSopenharmony_ci        LOGW("GetColor error, id=%{public}u", resId);
33423b3eb3cSopenharmony_ci    }
33523b3eb3cSopenharmony_ci    return Color(result);
33623b3eb3cSopenharmony_ci}
33723b3eb3cSopenharmony_ci
33823b3eb3cSopenharmony_ciDimension ResourceAdapterImpl::GetDimension(uint32_t resId)
33923b3eb3cSopenharmony_ci{
34023b3eb3cSopenharmony_ci    float result = 0;
34123b3eb3cSopenharmony_ci    std::string unit = "";
34223b3eb3cSopenharmony_ci    auto ret = resourceManger_.GetFloat(static_cast<int32_t>(resId), result, unit);
34323b3eb3cSopenharmony_ci    if (!ret) {
34423b3eb3cSopenharmony_ci        LOGW("GetDimension error, id=%{public}u", resId);
34523b3eb3cSopenharmony_ci    }
34623b3eb3cSopenharmony_ci    return Dimension(result, ParseDimensionUnit(unit));
34723b3eb3cSopenharmony_ci}
34823b3eb3cSopenharmony_ci
34923b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetString(uint32_t resId)
35023b3eb3cSopenharmony_ci{
35123b3eb3cSopenharmony_ci    std::string strResult = "";
35223b3eb3cSopenharmony_ci    auto ret = resourceManger_.GetString(static_cast<int32_t>(resId), strResult);
35323b3eb3cSopenharmony_ci    if (!ret) {
35423b3eb3cSopenharmony_ci        LOGW("GetString error, id=%{public}u", resId);
35523b3eb3cSopenharmony_ci    }
35623b3eb3cSopenharmony_ci    return strResult;
35723b3eb3cSopenharmony_ci}
35823b3eb3cSopenharmony_ci
35923b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetPluralString(uint32_t resId, int quantity)
36023b3eb3cSopenharmony_ci{
36123b3eb3cSopenharmony_ci    std::vector<std::string> pluralResults;
36223b3eb3cSopenharmony_ci    auto ret = resourceManger_.GetStringArray(static_cast<int32_t>(resId), pluralResults);
36323b3eb3cSopenharmony_ci    if (!ret) {
36423b3eb3cSopenharmony_ci        LOGW("GetPluralString error, id=%{public}u", resId);
36523b3eb3cSopenharmony_ci    }
36623b3eb3cSopenharmony_ci
36723b3eb3cSopenharmony_ci    auto pluralChoice = Localization::GetInstance()->PluralRulesFormat(quantity);
36823b3eb3cSopenharmony_ci    auto iter = std::find(pluralResults.begin(), pluralResults.end(), pluralChoice);
36923b3eb3cSopenharmony_ci    std::string originStr;
37023b3eb3cSopenharmony_ci    if (iter != pluralResults.end() && iter + 1 != pluralResults.end()) {
37123b3eb3cSopenharmony_ci        iter++;
37223b3eb3cSopenharmony_ci        originStr = *iter;
37323b3eb3cSopenharmony_ci    }
37423b3eb3cSopenharmony_ci    return originStr;
37523b3eb3cSopenharmony_ci}
37623b3eb3cSopenharmony_ci
37723b3eb3cSopenharmony_cistd::vector<std::string> ResourceAdapterImpl::GetStringArray(uint32_t resId) const
37823b3eb3cSopenharmony_ci{
37923b3eb3cSopenharmony_ci    std::vector<std::string> strResults;
38023b3eb3cSopenharmony_ci    auto ret = resourceManger_.GetStringArray(static_cast<int32_t>(resId), strResults);
38123b3eb3cSopenharmony_ci    if (!ret) {
38223b3eb3cSopenharmony_ci        LOGW("GetStringArray error, id=%{public}u", resId);
38323b3eb3cSopenharmony_ci    }
38423b3eb3cSopenharmony_ci    return strResults;
38523b3eb3cSopenharmony_ci}
38623b3eb3cSopenharmony_ci
38723b3eb3cSopenharmony_cidouble ResourceAdapterImpl::GetDouble(uint32_t resId)
38823b3eb3cSopenharmony_ci{
38923b3eb3cSopenharmony_ci    float result = 0.0f;
39023b3eb3cSopenharmony_ci    auto ret = resourceManger_.GetFloat(static_cast<int32_t>(resId), result);
39123b3eb3cSopenharmony_ci    if (!ret) {
39223b3eb3cSopenharmony_ci        LOGW("GetDouble error, id=%{public}u", resId);
39323b3eb3cSopenharmony_ci    }
39423b3eb3cSopenharmony_ci    return static_cast<double>(result);
39523b3eb3cSopenharmony_ci}
39623b3eb3cSopenharmony_ci
39723b3eb3cSopenharmony_ciint32_t ResourceAdapterImpl::GetInt(uint32_t resId)
39823b3eb3cSopenharmony_ci{
39923b3eb3cSopenharmony_ci    int32_t result = 0;
40023b3eb3cSopenharmony_ci    auto ret = resourceManger_.GetInt(static_cast<int32_t>(resId), result);
40123b3eb3cSopenharmony_ci    if (!ret) {
40223b3eb3cSopenharmony_ci        LOGW("GetInt error, id=%{public}u", resId);
40323b3eb3cSopenharmony_ci    }
40423b3eb3cSopenharmony_ci    return result;
40523b3eb3cSopenharmony_ci}
40623b3eb3cSopenharmony_ci
40723b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetResource(uint32_t resId, std::ostream& dest) const
40823b3eb3cSopenharmony_ci{
40923b3eb3cSopenharmony_ci    return resourceManger_.GetResource(static_cast<int32_t>(resId), dest);
41023b3eb3cSopenharmony_ci}
41123b3eb3cSopenharmony_ci
41223b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetResource(const std::string& path, std::ostream& dest) const
41323b3eb3cSopenharmony_ci{
41423b3eb3cSopenharmony_ci    return resourceManger_.GetResource(path, dest);
41523b3eb3cSopenharmony_ci}
41623b3eb3cSopenharmony_ci
41723b3eb3cSopenharmony_cibool ResourceAdapterImpl::ConvertToGlobalResourceType(
41823b3eb3cSopenharmony_ci    const std::string& resTypeName, Global::Resource::ResourceType& resType) const
41923b3eb3cSopenharmony_ci{
42023b3eb3cSopenharmony_ci    if (resTypeName == "color") {
42123b3eb3cSopenharmony_ci        resType = Global::Resource::ResourceType::COLOR;
42223b3eb3cSopenharmony_ci        return true;
42323b3eb3cSopenharmony_ci    }
42423b3eb3cSopenharmony_ci    if (resTypeName == "float") {
42523b3eb3cSopenharmony_ci        resType = Global::Resource::ResourceType::FLOAT;
42623b3eb3cSopenharmony_ci        return true;
42723b3eb3cSopenharmony_ci    }
42823b3eb3cSopenharmony_ci    if (resTypeName == "string") {
42923b3eb3cSopenharmony_ci        resType = Global::Resource::ResourceType::STRING;
43023b3eb3cSopenharmony_ci        return true;
43123b3eb3cSopenharmony_ci    }
43223b3eb3cSopenharmony_ci    if (resTypeName == "media") {
43323b3eb3cSopenharmony_ci        resType = Global::Resource::ResourceType::MEDIA;
43423b3eb3cSopenharmony_ci        return true;
43523b3eb3cSopenharmony_ci    }
43623b3eb3cSopenharmony_ci    LOGE("unsupported resource type(=%{public}s)", resTypeName.c_str());
43723b3eb3cSopenharmony_ci    return false;
43823b3eb3cSopenharmony_ci}
43923b3eb3cSopenharmony_ci
44023b3eb3cSopenharmony_cibool ResourceAdapterImpl::GetIdByName(const std::string& resName, const std::string& resType, uint32_t& resId) const
44123b3eb3cSopenharmony_ci{
44223b3eb3cSopenharmony_ci    Global::Resource::ResourceType globalResType;
44323b3eb3cSopenharmony_ci    if (!ConvertToGlobalResourceType(resType, globalResType)) {
44423b3eb3cSopenharmony_ci        return false;
44523b3eb3cSopenharmony_ci    }
44623b3eb3cSopenharmony_ci    int32_t globalResId = 0;
44723b3eb3cSopenharmony_ci    if (!resourceManger_.GetIdByName("", resName, globalResType, globalResId)) {
44823b3eb3cSopenharmony_ci        LOGW("get resource id failed.(name=%{public}s, type=%{public}s)", resName.c_str(), resType.c_str());
44923b3eb3cSopenharmony_ci        return false;
45023b3eb3cSopenharmony_ci    }
45123b3eb3cSopenharmony_ci    resId = static_cast<uint32_t>(globalResId);
45223b3eb3cSopenharmony_ci    return true;
45323b3eb3cSopenharmony_ci}
45423b3eb3cSopenharmony_ci
45523b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetMediaPath(uint32_t resId)
45623b3eb3cSopenharmony_ci{
45723b3eb3cSopenharmony_ci    std::string mediaPath = "";
45823b3eb3cSopenharmony_ci    auto ret = resourceManger_.GetString(static_cast<int32_t>(resId), mediaPath);
45923b3eb3cSopenharmony_ci    if (!ret) {
46023b3eb3cSopenharmony_ci        LOGW("GetMediaPath error, id=%{public}u", resId);
46123b3eb3cSopenharmony_ci        return "";
46223b3eb3cSopenharmony_ci    }
46323b3eb3cSopenharmony_ci    return "resource://" + mediaPath.substr(0, mediaPath.find_last_of("/")) + "/" +
46423b3eb3cSopenharmony_ci            std::to_string(resId) + mediaPath.substr(mediaPath.find_last_of("."));
46523b3eb3cSopenharmony_ci}
46623b3eb3cSopenharmony_ci
46723b3eb3cSopenharmony_cistd::string ResourceAdapterImpl::GetRawfile(const std::string& fileName)
46823b3eb3cSopenharmony_ci{
46923b3eb3cSopenharmony_ci    auto container = Container::Current();
47023b3eb3cSopenharmony_ci    if (!container) {
47123b3eb3cSopenharmony_ci        LOGW("container is null");
47223b3eb3cSopenharmony_ci        return "";
47323b3eb3cSopenharmony_ci    }
47423b3eb3cSopenharmony_ci    auto moduleName = container->GetModuleName();
47523b3eb3cSopenharmony_ci#if defined(PREVIEW)
47623b3eb3cSopenharmony_ci    return "resource://RAWFILE/" + moduleName + "/resources/rawfile/" + fileName;
47723b3eb3cSopenharmony_ci#else
47823b3eb3cSopenharmony_ci    return "resource://RAWFILE/assets/" + moduleName + "/resources/rawfile/" + fileName;
47923b3eb3cSopenharmony_ci#endif
48023b3eb3cSopenharmony_ci}
48123b3eb3cSopenharmony_ci
48223b3eb3cSopenharmony_ciuint32_t ResourceAdapterImpl::GetSymbolById(uint32_t resId) const
48323b3eb3cSopenharmony_ci{
48423b3eb3cSopenharmony_ci    uint32_t result = 0;
48523b3eb3cSopenharmony_ci    resourceManger_->GetSymbolById(resId, result);
48623b3eb3cSopenharmony_ci    return result;
48723b3eb3cSopenharmony_ci}
48823b3eb3cSopenharmony_ci} // namespace OHOS::Ace
489