1 /*
2 * Copyright (c) 2022 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 "normal_scene.h"
17
18 #include "tree_manager.h"
19
20 namespace OHOS {
21 namespace WuKong {
22 namespace {
23 const int MAXINPUTNUM = 2;
24 const int ONEHUNDRED = 100;
25 const int NEWPERCENT = 70;
26 const int OLDPERCENT = 20;
27 } // namespace
NormalScene()28 NormalScene::NormalScene()
29 {
30 }
31
~NormalScene()32 NormalScene::~NormalScene()
33 {
34 }
SetInputComponentList(std::vector<std::shared_ptr<ComponentTree>> & componentList)35 ErrCode NormalScene::SetInputComponentList(std::vector<std::shared_ptr<ComponentTree>> &componentList)
36 {
37 ErrCode result = OHOS::ERR_OK;
38 int randomNumber = rand() % ONEHUNDRED;
39 uint32_t count = 0;
40 DEBUG_LOG_STR("randomNumber: %d", randomNumber);
41 std::vector<uint32_t> indexList;
42 auto util = WuKongUtil::GetInstance();
43 std::vector<std::string> compIdBlock = util->GetCompIdBlockList();
44 std::vector<std::string> compTypeBlock = util->GetCompTypeBlockList();
45 if (randomNumber < NEWPERCENT) {
46 for (auto it = componentList.begin(); it != componentList.end(); it++) {
47 TRACK_LOG_STR("component inputcount: %d", (*it)->GetInputCount());
48 if ((*it)->GetInputCount() > MAXINPUTNUM || IsComponentBlock((*it), compIdBlock, compTypeBlock)) {
49 indexList.push_back((*it)->GetIndex());
50 TRACK_LOG_STR("index0: %d", distance(componentList.begin(), it));
51 }
52 }
53 } else if (randomNumber < (NEWPERCENT + OLDPERCENT)) {
54 for (auto it = componentList.begin(); it != componentList.end(); it++) {
55 TRACK_LOG_STR("component inputcount: %d", (*it)->GetInputCount());
56 if ((*it)->GetInputCount() <= MAXINPUTNUM || IsComponentBlock((*it), compIdBlock, compTypeBlock)) {
57 count++;
58 TRACK_LOG_STR("inputed count: %d, componentList size: %d", count, componentList.size());
59 indexList.push_back((*it)->GetIndex());
60 TRACK_LOG_STR("index: %d", distance(componentList.begin(), it));
61 }
62 }
63 }
64 if (count >= componentList.size()) {
65 if ((componentList.size() == 0) || (randomNumber < ONEHUNDRED && randomNumber >= (NEWPERCENT + OLDPERCENT))) {
66 isBack_ = true;
67 }
68 indexList.clear();
69 return OHOS::ERR_OK;
70 }
71 TRACK_LOG_STR("componentList size: %d", componentList.size());
72 TRACK_LOG_STR("indexList size: %d", indexList.size());
73 SubtractComponent(componentList, indexList);
74 if ((componentList.size() == 0) || (randomNumber < ONEHUNDRED && randomNumber >= (NEWPERCENT + OLDPERCENT))) {
75 isBack_ = true;
76 }
77 indexList.clear();
78 return result;
79 }
80
SetInputComponent(std::vector<std::shared_ptr<ComponentTree>> & componentList,std::shared_ptr<ComponentTree> & componentinfo)81 ErrCode NormalScene::SetInputComponent(std::vector<std::shared_ptr<ComponentTree>> &componentList,
82 std::shared_ptr<ComponentTree> &componentinfo)
83 {
84 auto util = WuKongUtil::GetInstance();
85 std::vector<std::string> compIdBlock = util->GetCompIdBlockList();
86 std::vector<std::string> compTypeBlock = util->GetCompTypeBlockList();
87 ErrCode result = OHOS::ERR_OK;
88 if (!componentList.empty()) {
89 auto treemanager = TreeManager::GetInstance();
90 bool hasDialog = treemanager->HasDialog();
91 bool meetDialog = false;
92 for (auto it : componentList) {
93 if (it->GetType() == "Dialog") {
94 meetDialog = true;
95 }
96 if (hasDialog && !meetDialog) {
97 continue;
98 }
99 DEBUG_LOG_STR("component inputcount: %d, node id: %016llX", it->GetInputCount(), it->GetNodeId());
100 if (it->GetInputCount() < 1 && !IsComponentBlock(it, compIdBlock, compTypeBlock)) {
101 componentinfo = it;
102 break;
103 }
104 }
105 }
106 if (componentinfo == nullptr) {
107 isBack_ = true;
108 }
109 return result;
110 }
111
SetInputComponentListForFocusInput(std::vector<std::shared_ptr<ComponentTree>> & componentList)112 ErrCode NormalScene::SetInputComponentListForFocusInput(std::vector<std::shared_ptr<ComponentTree>> &componentList)
113 {
114 ErrCode result = OHOS::ERR_OK;
115 uint32_t count = 0;
116 std::vector<uint32_t> indexList;
117 auto treemanager = TreeManager::GetInstance();
118 uint32_t focusNum = treemanager->GetFocusNum();
119 auto util = WuKongUtil::GetInstance();
120 std::vector<std::string> compIdBlock = util->GetCompIdBlockList();
121 std::vector<std::string> compTypeBlock = util->GetCompTypeBlockList();
122 for (auto it = componentList.begin(); it != componentList.end(); it++) {
123 TRACK_LOG_STR("component inputcount: %d", (*it)->GetInputCount());
124 std::string type = (*it)->GetType();
125 bool needFocus = treemanager->NeedFocus(type);
126 uint32_t limit = needFocus ? focusNum : MAXINPUTNUM;
127 if ((*it)->GetInputCount() >= limit || IsComponentBlock((*it), compIdBlock, compTypeBlock)) {
128 indexList.push_back((*it)->GetIndex());
129 count++;
130 TRACK_LOG_STR("index0: %d", distance(componentList.begin(), it));
131 }
132 }
133 if (count >= componentList.size()) {
134 isBack_ = true;
135 indexList.clear();
136 return OHOS::ERR_OK;
137 }
138 // just determine back or not, can not remove component because of locating component after
139 if (componentList.size() == 0) {
140 isBack_ = true;
141 }
142 indexList.clear();
143 return result;
144 }
145
SubtractComponent(std::vector<std::shared_ptr<ComponentTree>> & componentList,std::vector<uint32_t> & indexList)146 void NormalScene::SubtractComponent(std::vector<std::shared_ptr<ComponentTree>> &componentList,
147 std::vector<uint32_t> &indexList)
148 {
149 for (auto index : indexList) {
150 for (auto it = componentList.begin(); it != componentList.end(); it++) {
151 if ((*it)->GetIndex() == index) {
152 componentList.erase(it);
153 it--;
154 }
155 }
156 }
157 }
158
IsComponentBlock(std::shared_ptr<ComponentTree> & comp,std::vector<std::string> & compIdBlock,std::vector<std::string> & compTypeBlock)159 bool NormalScene::IsComponentBlock(std::shared_ptr<ComponentTree> &comp, std::vector<std::string> &compIdBlock,
160 std::vector<std::string> &compTypeBlock)
161 {
162 std::string id = comp->GetInspectorKey();
163 std::string type = comp->GetType();
164 auto util = WuKongUtil::GetInstance();
165 if (util->ContainsElement(compIdBlock, id)) {
166 DEBUG_LOG_STR("Block Id: %s", id.c_str());
167 return true;
168 }
169 if (util->ContainsElement(compTypeBlock, type)) {
170 DEBUG_LOG_STR("Block type: %s", type.c_str());
171 return true;
172 }
173 DEBUG_LOG_STR("UnBlock Id: %s, type: %s", id.c_str(), type.c_str());
174 return false;
175 }
176 } // namespace WuKong
177 } // namespace OHOS
178