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 "base/utils/resource_configuration.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "base/utils/utils.h"
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_cinamespace OHOS::Ace {
2123b3eb3cSopenharmony_cinamespace {
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_ciconst std::string CONFIGURATION_COLOR_MODE = "colorMode";
2423b3eb3cSopenharmony_ciconst std::string COLOR_MODE_LIGHT = "light";
2523b3eb3cSopenharmony_ciconst std::string COLOR_MODE_DARK = "dark";
2623b3eb3cSopenharmony_ciconst std::string CONFIGURATION_FONT_RATIO = "fontScale";
2723b3eb3cSopenharmony_ci
2823b3eb3cSopenharmony_civoid AddFlag(uint32_t& flags, uint32_t flag)
2923b3eb3cSopenharmony_ci{
3023b3eb3cSopenharmony_ci    flags |= flag;
3123b3eb3cSopenharmony_ci}
3223b3eb3cSopenharmony_ci
3323b3eb3cSopenharmony_ci} // namespace
3423b3eb3cSopenharmony_ci
3523b3eb3cSopenharmony_cibool ResourceConfiguration::ParseJsonColorMode(const std::unique_ptr<JsonValue>& jsonConfig, uint32_t& updateFlags)
3623b3eb3cSopenharmony_ci{
3723b3eb3cSopenharmony_ci    if (!jsonConfig->Contains(CONFIGURATION_COLOR_MODE)) {
3823b3eb3cSopenharmony_ci        return false;
3923b3eb3cSopenharmony_ci    }
4023b3eb3cSopenharmony_ci    auto jsonColorMode = jsonConfig->GetValue(CONFIGURATION_COLOR_MODE);
4123b3eb3cSopenharmony_ci    if (!jsonColorMode->IsString()) {
4223b3eb3cSopenharmony_ci        return false;
4323b3eb3cSopenharmony_ci    }
4423b3eb3cSopenharmony_ci    auto strColorMode = jsonColorMode->GetString();
4523b3eb3cSopenharmony_ci    if (strColorMode != COLOR_MODE_LIGHT && strColorMode != COLOR_MODE_DARK) {
4623b3eb3cSopenharmony_ci        return false;
4723b3eb3cSopenharmony_ci    }
4823b3eb3cSopenharmony_ci    ColorMode colorMode = ColorMode::LIGHT;
4923b3eb3cSopenharmony_ci    if (strColorMode == COLOR_MODE_DARK) {
5023b3eb3cSopenharmony_ci        colorMode = ColorMode::DARK;
5123b3eb3cSopenharmony_ci    }
5223b3eb3cSopenharmony_ci    if (colorMode != colorMode_) {
5323b3eb3cSopenharmony_ci        colorMode_ = colorMode;
5423b3eb3cSopenharmony_ci        AddFlag(updateFlags, ResourceConfiguration::COLOR_MODE_UPDATED_FLAG);
5523b3eb3cSopenharmony_ci    }
5623b3eb3cSopenharmony_ci    return true;
5723b3eb3cSopenharmony_ci}
5823b3eb3cSopenharmony_ci
5923b3eb3cSopenharmony_cibool ResourceConfiguration::ParseJsonFontRatio(const std::unique_ptr<JsonValue>& jsonConfig, uint32_t& updateFlags)
6023b3eb3cSopenharmony_ci{
6123b3eb3cSopenharmony_ci    if (!jsonConfig->Contains(CONFIGURATION_FONT_RATIO)) {
6223b3eb3cSopenharmony_ci        return false;
6323b3eb3cSopenharmony_ci    }
6423b3eb3cSopenharmony_ci    auto jsonFontRatio = jsonConfig->GetValue(CONFIGURATION_FONT_RATIO);
6523b3eb3cSopenharmony_ci    if (!jsonFontRatio->IsNumber()) {
6623b3eb3cSopenharmony_ci        return false;
6723b3eb3cSopenharmony_ci    }
6823b3eb3cSopenharmony_ci    double fontRatio = jsonFontRatio->GetDouble();
6923b3eb3cSopenharmony_ci    if (!NearEqual(fontRatio, fontRatio_)) {
7023b3eb3cSopenharmony_ci        fontRatio_ = fontRatio;
7123b3eb3cSopenharmony_ci        AddFlag(updateFlags, ResourceConfiguration::FONT_RATIO_UPDATED_FLAG);
7223b3eb3cSopenharmony_ci    }
7323b3eb3cSopenharmony_ci    return true;
7423b3eb3cSopenharmony_ci}
7523b3eb3cSopenharmony_ci
7623b3eb3cSopenharmony_cibool ResourceConfiguration::UpdateFromJsonString(const std::string jsonStr, uint32_t& updateFlags)
7723b3eb3cSopenharmony_ci{
7823b3eb3cSopenharmony_ci    updateFlags = 0;
7923b3eb3cSopenharmony_ci    std::unique_ptr<JsonValue> jsonConfig = JsonUtil::ParseJsonString(jsonStr);
8023b3eb3cSopenharmony_ci
8123b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(jsonConfig, false);
8223b3eb3cSopenharmony_ci    ParseJsonColorMode(jsonConfig, updateFlags);
8323b3eb3cSopenharmony_ci    ParseJsonFontRatio(jsonConfig, updateFlags);
8423b3eb3cSopenharmony_ci    return true;
8523b3eb3cSopenharmony_ci}
8623b3eb3cSopenharmony_ci
8723b3eb3cSopenharmony_ci} // namespace OHOS::Ace
88