• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/utils/resource_configuration.h"
17 
18 #include "base/log/log.h"
19 #include "base/utils/utils.h"
20 
21 namespace OHOS::Ace {
22 namespace {
23 
24 const std::string CONFIGURATION_COLOR_MODE = "colorMode";
25 const std::string COLOR_MODE_LIGHT = "light";
26 const std::string COLOR_MODE_DARK = "dark";
27 const std::string CONFIGURATION_FONT_RATIO = "fontScale";
28 
AddFlag(uint32_t & flags,uint32_t flag)29 void AddFlag(uint32_t& flags, uint32_t flag)
30 {
31     flags |= flag;
32 }
33 
34 } // namespace
35 
ParseJsonColorMode(const std::unique_ptr<JsonValue> & jsonConfig,uint32_t & updateFlags)36 bool ResourceConfiguration::ParseJsonColorMode(const std::unique_ptr<JsonValue>& jsonConfig, uint32_t& updateFlags)
37 {
38     if (!jsonConfig->Contains(CONFIGURATION_COLOR_MODE)) {
39         return false;
40     }
41     auto jsonColorMode = jsonConfig->GetValue(CONFIGURATION_COLOR_MODE);
42     if (!jsonColorMode->IsString()) {
43         return false;
44     }
45     auto strColorMode = jsonColorMode->GetString();
46     if (strColorMode != COLOR_MODE_LIGHT && strColorMode != COLOR_MODE_DARK) {
47         return false;
48     }
49     ColorMode colorMode = ColorMode::LIGHT;
50     if (strColorMode == COLOR_MODE_DARK) {
51         colorMode = ColorMode::DARK;
52     }
53     if (colorMode != colorMode_) {
54         colorMode_ = colorMode;
55         AddFlag(updateFlags, ResourceConfiguration::COLOR_MODE_UPDATED_FLAG);
56     }
57     return true;
58 }
59 
ParseJsonFontRatio(const std::unique_ptr<JsonValue> & jsonConfig,uint32_t & updateFlags)60 bool ResourceConfiguration::ParseJsonFontRatio(const std::unique_ptr<JsonValue>& jsonConfig, uint32_t& updateFlags)
61 {
62     if (!jsonConfig->Contains(CONFIGURATION_FONT_RATIO)) {
63         return false;
64     }
65     auto jsonFontRatio = jsonConfig->GetValue(CONFIGURATION_FONT_RATIO);
66     if (!jsonFontRatio->IsNumber()) {
67         return false;
68     }
69     double fontRatio = jsonFontRatio->GetDouble();
70     if (!NearEqual(fontRatio, fontRatio_)) {
71         fontRatio_ = fontRatio;
72         AddFlag(updateFlags, ResourceConfiguration::FONT_RATIO_UPDATED_FLAG);
73     }
74     return true;
75 }
76 
UpdateFromJsonString(const std::string jsonStr,uint32_t & updateFlags)77 bool ResourceConfiguration::UpdateFromJsonString(const std::string jsonStr, uint32_t& updateFlags)
78 {
79     updateFlags = 0;
80     std::unique_ptr<JsonValue> jsonConfig = JsonUtil::ParseJsonString(jsonStr);
81     CHECK_NULL_RETURN(jsonConfig, false);
82     ParseJsonColorMode(jsonConfig, updateFlags);
83     ParseJsonFontRatio(jsonConfig, updateFlags);
84     return true;
85 }
86 
87 } // namespace OHOS::Ace
88