1 /*
2 * Copyright (c) 2025 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 <sstream>
17 #include "uifirst_param_parse.h"
18
19 namespace OHOS::Rosen {
20
GetUIFirstSwitchType(const std::string & input)21 UIFirstSwitchType UIFirstParamParse::GetUIFirstSwitchType(const std::string& input)
22 {
23 static const std::map<std::string, UIFirstSwitchType> uifirstSwitchTypeMap = {
24 {"UIFirstEnabled", UIFirstSwitchType::UIFIRST_ENABLED},
25 {"CardUIFirstEnabled", UIFirstSwitchType::CARD_UIFIRST_ENABLED},
26 {"CacheOptimizeRotateEnabled", UIFirstSwitchType::ROTATE_ENABLED},
27 {"FreeMultiWindowEnabled", UIFirstSwitchType::FREE_MULTI_WINDOW_ENABLED}
28 };
29
30 auto it = uifirstSwitchTypeMap.find(input);
31 if (it != uifirstSwitchTypeMap.end()) {
32 return it->second;
33 }
34 return UIFirstSwitchType::UNKNOWN;
35 }
36
ParseFeatureParam(FeatureParamMapType & featureMap,xmlNode & node)37 int32_t UIFirstParamParse::ParseFeatureParam(FeatureParamMapType &featureMap, xmlNode &node)
38 {
39 xmlNode *currNode = &node;
40 if (currNode->xmlChildrenNode == nullptr) {
41 RS_LOGD("UIFirstParamParse stop parsing, no children nodes");
42 return PARSE_GET_CHILD_FAIL;
43 }
44
45 currNode = currNode->xmlChildrenNode;
46 for (; currNode; currNode = currNode->next) {
47 if (currNode->type != XML_ELEMENT_NODE) {
48 continue;
49 }
50 ParseUIFirstInternal(*currNode);
51 }
52 return PARSE_EXEC_SUCCESS;
53 }
54
ParseUIFirstInternal(xmlNode & node)55 int32_t UIFirstParamParse::ParseUIFirstInternal(xmlNode &node)
56 {
57 // Start Parse Feature Params
58 xmlNode *currNode = &node;
59 int xmlParamType = GetXmlNodeAsInt(*currNode);
60 auto name = ExtractPropertyValue("name", *currNode);
61 auto val = ExtractPropertyValue("value", *currNode);
62 if (xmlParamType == PARSE_XML_FEATURE_SWITCH) {
63 bool isEnabled = ParseFeatureSwitch(val);
64 switch (GetUIFirstSwitchType(name)) {
65 case UIFirstSwitchType::UIFIRST_ENABLED:
66 UIFirstParam::SetUIFirstEnable(isEnabled);
67 RS_LOGI("UIFirstParamParse parse UIFirstEnabled %{public}d", UIFirstParam::IsUIFirstEnable());
68 break;
69 case UIFirstSwitchType::CARD_UIFIRST_ENABLED:
70 UIFirstParam::SetCardUIFirstEnable(isEnabled);
71 RS_LOGI("UIFirstParamParse parse CardUIFirstEnabled %{public}d",
72 UIFirstParam::IsCardUIFirstEnable());
73 break;
74 case UIFirstSwitchType::ROTATE_ENABLED:
75 UIFirstParam::SetCacheOptimizeRotateEnable(isEnabled);
76 RS_LOGI("UIFirstParamParse parse CacheOptimizeRotateEnabled %{public}d",
77 UIFirstParam::IsCacheOptimizeRotateEnable());
78 break;
79 case UIFirstSwitchType::FREE_MULTI_WINDOW_ENABLED:
80 UIFirstParam::SetFreeMultiWindowEnable(isEnabled);
81 RS_LOGI("UIFirstParamParse parse FreeMultiWindowEnabled %{public}d",
82 UIFirstParam::IsFreeMultiWindowEnable());
83 break;
84 default:
85 RS_LOGE("UIFirstParamParse %{public}s is not support!", name.c_str());
86 break;
87 }
88 } else if (xmlParamType == PARSE_XML_FEATURE_SINGLEPARAM) {
89 return ParseUIFirstSingleParam(name, val);
90 }
91
92 return PARSE_EXEC_SUCCESS;
93 }
94
ParseUIFirstSingleParam(const std::string & name,const std::string & value)95 int32_t UIFirstParamParse::ParseUIFirstSingleParam(const std::string& name, const std::string& value)
96 {
97 if (name == "UIFirstEnableWindowThreshold" && IsNumber(value)) {
98 int num;
99 std::istringstream iss(value);
100 if (iss >> num) {
101 UIFirstParam::SetUIFirstEnableWindowThreshold(num);
102 RS_LOGI("UIFirstParamParse parse UIFirstEnableWindowThreshold %{public}d",
103 UIFirstParam::GetUIFirstEnableWindowThreshold());
104 }
105 } else if (name == "UIFirstType" && IsNumber(value)) {
106 int num;
107 std::istringstream iss(value);
108 if (iss >> num) {
109 UIFirstParam::SetUIFirstType(num);
110 RS_LOGI("UIFirstParamParse parse UIFirstType %{public}d", UIFirstParam::GetUIFirstType());
111 }
112 }
113 return PARSE_EXEC_SUCCESS;
114 }
115 } // namespace OHOS::Rosen