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