1 /*
2 * Copyright (c) 2021-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 "core/accessibility/accessibility_node.h"
17
18 #include "base/json/json_util.h"
19 #include "base/log/ace_trace.h"
20 #include "base/log/log.h"
21 #include "base/utils/linear_map.h"
22 #include "base/utils/utils.h"
23 #include "core/accessibility/accessibility_utils.h"
24 #include "core/common/container.h"
25 #include "core/event/ace_event_helper.h"
26
27 namespace OHOS::Ace {
28 namespace {
29
30 const char ACCESSIBILITY_VALUE[] = "value";
31 const char ACCESSIBILITY_TYPE[] = "type";
32 const char ACCESSIBILITY_DISABLED[] = "disabled";
33 const char ACCESSIBILITY_GROUP[] = "accessibilitygroup";
34 const char ACCESSIBILITY_TEXT[] = "accessibilitytext";
35 const char ACCESSIBILITY_DESCRIPTION[] = "accessibilitydescription";
36 const char ACCESSIBILITY_IMPORTANCE[] = "accessibilityimportance";
37 const char ACCESSIBILITY_SHOW[] = "show";
38 const char ID[] = "id";
39
40 const char INPUT_TYPE_CHECKBOX[] = "checkbox";
41 const char INPUT_TYPE_RADIO[] = "radio";
42 const char INPUT_TYPE_PASSWORD[] = "password";
43
44 // common event definition
45 const char ACCESSIBILITY_EVENT[] = "accessibility";
46 const char CLICK[] = "click";
47 const char LONG_PRESS[] = "longpress";
48 const char FOCUS[] = "focus";
49 const char BLUR[] = "blur";
50
StringToBool(const std::string & value)51 inline bool StringToBool(const std::string& value)
52 {
53 return value == "true";
54 }
55
MergeItems(const std::vector<std::pair<std::string,std::string>> & newItems,std::vector<std::pair<std::string,std::string>> & items)56 void MergeItems(const std::vector<std::pair<std::string, std::string>>& newItems,
57 std::vector<std::pair<std::string, std::string>>& items)
58 {
59 if (items.empty()) {
60 items = newItems;
61 } else {
62 std::unordered_map<std::string, std::string> originItems;
63 std::vector<std::pair<std::string, std::string>> sameVec;
64 std::vector<std::pair<std::string, std::string>> diffVec;
65 for (const auto& item: items) {
66 originItems[item.first] = item.second;
67 }
68
69 for (const auto& item: newItems) {
70 originItems[item.first] = item.second;
71 }
72
73 // find same element
74 for (auto item = items.begin(); item != items.end(); item++) {
75 const auto iter = originItems.find(item->first);
76 if (iter != originItems.end()) {
77 sameVec.emplace_back(iter->first, iter->second);
78 }
79 }
80
81 // find different element
82 for (auto newItem = newItems.begin(); newItem != newItems.end(); newItem++) {
83 size_t diffFlagCount = 0;
84 for (auto item = items.begin(); item != items.end(); item++) {
85 if (newItem->first != item->first) {
86 diffFlagCount++;
87 }
88 }
89 if (diffFlagCount == items.size()) {
90 diffVec.emplace_back(newItem->first, newItem->second);
91 }
92 }
93 sameVec.insert(sameVec.end(), diffVec.begin(), diffVec.end());
94 items.clear();
95 items = sameVec;
96 }
97 }
98
99 } // namespace
100
AccessibilityNode(NodeId nodeId,const std::string & nodeName)101 AccessibilityNode::AccessibilityNode(NodeId nodeId, const std::string& nodeName) : nodeId_(nodeId), tag_(nodeName)
102 {
103 // Initialize member variable in bitfield
104 isEnabled_ = true;
105 visible_ = true;
106 shown_ = true;
107 }
108
SetActionClickImpl(const ActionClickImpl & actionClickImpl)109 void AccessibilityNode::SetActionClickImpl(const ActionClickImpl& actionClickImpl)
110 {
111 actionClickImpl_ = actionClickImpl;
112 }
113
ActionClick()114 bool AccessibilityNode::ActionClick()
115 {
116 if (actionClickImpl_) {
117 actionClickImpl_();
118 return true;
119 }
120 return false;
121 }
122
SetActionLongClickImpl(const ActionLongClickImpl & actionLongClickImpl)123 void AccessibilityNode::SetActionLongClickImpl(const ActionLongClickImpl& actionLongClickImpl)
124 {
125 actionLongClickImpl_ = actionLongClickImpl;
126 }
127
ActionLongClick()128 bool AccessibilityNode::ActionLongClick()
129 {
130 if (actionLongClickImpl_) {
131 actionLongClickImpl_();
132 return true;
133 }
134 return false;
135 }
136
SetActionSetTextImpl(const ActionSetTextImpl & actionSetTextImpl)137 void AccessibilityNode::SetActionSetTextImpl(const ActionSetTextImpl& actionSetTextImpl)
138 {
139 actionSetTextImpl_ = actionSetTextImpl;
140 }
141
ActionSetText(const std::string & text)142 bool AccessibilityNode::ActionSetText(const std::string& text)
143 {
144 if (actionSetTextImpl_) {
145 actionSetTextImpl_(text);
146 return true;
147 }
148 return false;
149 }
150
SetActionScrollForward(const ActionScrollForwardImpl & actionScrollForwardImpl)151 void AccessibilityNode::SetActionScrollForward(const ActionScrollForwardImpl& actionScrollForwardImpl)
152 {
153 actionScrollForwardImpl_ = actionScrollForwardImpl;
154 }
155
ActionScrollForward()156 bool AccessibilityNode::ActionScrollForward()
157 {
158 if (actionScrollForwardImpl_) {
159 return actionScrollForwardImpl_();
160 }
161 return false;
162 }
163
SetActionScrollBackward(const ActionScrollBackwardImpl & actionScrollBackwardImpl)164 void AccessibilityNode::SetActionScrollBackward(const ActionScrollBackwardImpl& actionScrollBackwardImpl)
165 {
166 actionScrollBackwardImpl_ = actionScrollBackwardImpl;
167 }
168
ActionScrollBackward()169 bool AccessibilityNode::ActionScrollBackward()
170 {
171 if (actionScrollBackwardImpl_) {
172 return actionScrollBackwardImpl_();
173 }
174 return false;
175 }
176
SetActionAccessibilityFocusImpl(const ActionAccessibilityFocusImpl & actionAccessibilityFocusImpl)177 void AccessibilityNode::SetActionAccessibilityFocusImpl(
178 const ActionAccessibilityFocusImpl& actionAccessibilityFocusImpl)
179 {
180 actionAccessibilityFocusIdsImpl_ = actionAccessibilityFocusImpl;
181 }
182
ActionAccessibilityFocus(bool isFocus)183 bool AccessibilityNode::ActionAccessibilityFocus(bool isFocus)
184 {
185 if (actionAccessibilityFocusIdsImpl_) {
186 actionAccessibilityFocusIdsImpl_(isFocus);
187 return true;
188 }
189 return false;
190 }
191
SetActionFocusImpl(const ActionFocusImpl & actionFocusImpl)192 void AccessibilityNode::SetActionFocusImpl(const ActionFocusImpl& actionFocusImpl)
193 {
194 actionFocusImpl_ = actionFocusImpl;
195 }
196
ActionFocus()197 bool AccessibilityNode::ActionFocus()
198 {
199 if (actionFocusImpl_) {
200 actionFocusImpl_();
201 return true;
202 }
203 return false;
204 }
205
SetActionUpdateIdsImpl(const ActionUpdateIdsImpl & actionUpdateIdsImpl)206 void AccessibilityNode::SetActionUpdateIdsImpl(const ActionUpdateIdsImpl& actionUpdateIdsImpl)
207 {
208 actionUpdateIdsImpl_ = actionUpdateIdsImpl;
209 }
210
ActionUpdateIds()211 void AccessibilityNode::ActionUpdateIds()
212 {
213 if (actionUpdateIdsImpl_) {
214 actionUpdateIdsImpl_();
215 }
216 }
217
SetParentNode(const RefPtr<AccessibilityNode> & parentNode)218 void AccessibilityNode::SetParentNode(const RefPtr<AccessibilityNode>& parentNode)
219 {
220 parentNode_ = parentNode;
221 }
222
SetPositionInfo(const PositionInfo & positionInfo)223 void AccessibilityNode::SetPositionInfo(const PositionInfo& positionInfo)
224 {
225 rect_.SetRect(positionInfo.left, positionInfo.top, positionInfo.width, positionInfo.height);
226 }
227
SetAttr(const std::vector<std::pair<std::string,std::string>> & attrs)228 void AccessibilityNode::SetAttr(const std::vector<std::pair<std::string, std::string>>& attrs)
229 {
230 MergeItems(attrs, attrs_);
231
232 for (const auto& attr : attrs) {
233 if (attr.first == ACCESSIBILITY_VALUE) {
234 text_ = attr.second;
235 if (tag_ == ACCESSIBILITY_TAG_TEXT && parentNode_.Upgrade() &&
236 parentNode_.Upgrade()->GetTag() == ACCESSIBILITY_TAG_POPUP) {
237 auto spParent = parentNode_.Upgrade();
238 auto parentText = spParent->GetText() + text_;
239 spParent->SetText(parentText);
240 }
241 } else if (attr.first == ACCESSIBILITY_DISABLED) {
242 isEnabled_ = !StringToBool(attr.second);
243 } else if (attr.first == ACCESSIBILITY_TYPE) {
244 inputType_ = attr.second;
245 } else if (attr.first == ACCESSIBILITY_GROUP) {
246 accessible_ = StringToBool(attr.second);
247 } else if (attr.first == ACCESSIBILITY_TEXT) {
248 accessibilityLabel_ = attr.second;
249 } else if (attr.first == ACCESSIBILITY_DESCRIPTION) {
250 accessibilityHint_ = attr.second;
251 } else if (attr.first == ACCESSIBILITY_IMPORTANCE) {
252 importantForAccessibility_ = attr.second;
253 } else if (attr.first == ID) {
254 jsComponentId_ = attr.second;
255 } else if (attr.first == ACCESSIBILITY_SHOW) {
256 shown_ = attr.second == "true";
257 }
258 }
259 SetOperableInfo();
260 }
261
SetStyle(const std::vector<std::pair<std::string,std::string>> & styles)262 void AccessibilityNode::SetStyle(const std::vector<std::pair<std::string, std::string>>& styles)
263 {
264 MergeItems(styles, styles_);
265 }
266
AddEvent(int32_t pageId,const std::vector<std::string> & events)267 void AccessibilityNode::AddEvent(int32_t pageId, const std::vector<std::string>& events)
268 {
269 for (const auto& event : events) {
270 if (event == ACCESSIBILITY_EVENT) {
271 onAccessibilityEventId_ = EventMarker(std::to_string(nodeId_), event, pageId);
272 } else if (event == CLICK) {
273 onClickId_ = EventMarker(std::to_string(nodeId_), event, pageId);
274 SetClickableState(true);
275 } else if (event == LONG_PRESS) {
276 onLongPressId_ = EventMarker(std::to_string(nodeId_), event, pageId);
277 SetLongClickableState(true);
278 } else if (event == FOCUS) {
279 onFocusId_ = EventMarker(std::to_string(nodeId_), event, pageId);
280 } else if (event == BLUR) {
281 onBlurId_ = EventMarker(std::to_string(nodeId_), event, pageId);
282 }
283 }
284 AddSupportAction(AceAction::CUSTOM_ACTION);
285 AddSupportAction(AceAction::GLOBAL_ACTION_BACK);
286 }
287
AddNode(const RefPtr<AccessibilityNode> & node,int32_t slot)288 void AccessibilityNode::AddNode(const RefPtr<AccessibilityNode>& node, int32_t slot)
289 {
290 CHECK_NULL_VOID(node);
291 auto isExist = std::find_if(children_.begin(), children_.end(),
292 [node](const RefPtr<AccessibilityNode>& child) { return child->GetNodeId() == node->GetNodeId(); });
293 if (isExist != children_.end()) {
294 return;
295 }
296 auto pos = children_.begin();
297 std::advance(pos, slot);
298 children_.insert(pos, node);
299 }
300
RemoveNode(const RefPtr<AccessibilityNode> & node)301 void AccessibilityNode::RemoveNode(const RefPtr<AccessibilityNode>& node)
302 {
303 CHECK_NULL_VOID(node);
304 children_.remove_if(
305 [node](const RefPtr<AccessibilityNode>& child) { return node->GetNodeId() == child->GetNodeId(); });
306 }
307
Mount(int32_t slot)308 void AccessibilityNode::Mount(int32_t slot)
309 {
310 auto parentNode = parentNode_.Upgrade();
311 CHECK_NULL_VOID(parentNode);
312 parentNode->AddNode(AceType::Claim(this), slot);
313 }
314
AddOffsetForChildren(const Offset & offset)315 void AccessibilityNode::AddOffsetForChildren(const Offset& offset)
316 {
317 SetLeft(GetLeft() + offset.GetX());
318 SetTop(GetTop() + offset.GetY());
319 for (const auto& child : GetChildList()) {
320 child->AddOffsetForChildren(offset);
321 }
322 }
323
SetOperableInfo()324 void AccessibilityNode::SetOperableInfo()
325 {
326 static const LinearMapNode<OperableInfo> nodeOperatorMap[] = {
327 { ACCESSIBILITY_TAG_BUTTON,
328 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
329 { ACCESSIBILITY_TAG_CALENDAR,
330 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
331 { ACCESSIBILITY_TAG_CANVAS,
332 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
333 { ACCESSIBILITY_TAG_CHART,
334 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
335 { ACCESSIBILITY_TAG_CLOCK,
336 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
337 { ACCESSIBILITY_TAG_DIALOG,
338 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
339 { ACCESSIBILITY_TAG_DIV,
340 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
341 { ACCESSIBILITY_TAG_DIVIDER, { .checkable = false, .clickable = false, .scrollable = false,
342 .longClickable = false, .focusable = false } },
343 { ACCESSIBILITY_TAG_IMAGE,
344 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
345 { ACCESSIBILITY_TAG_INPUT,
346 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
347 { ACCESSIBILITY_TAG_LABEL,
348 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
349 { ACCESSIBILITY_TAG_LIST,
350 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
351 { ACCESSIBILITY_TAG_LIST_ITEM,
352 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
353 { ACCESSIBILITY_TAG_LIST_ITEM_GROUP,
354 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
355 { ACCESSIBILITY_TAG_MARQUEE,
356 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
357 { ACCESSIBILITY_TAG_NAVIGATION_BAR,
358 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
359 { ACCESSIBILITY_TAG_OPTION,
360 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
361 { ACCESSIBILITY_TAG_POPUP,
362 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
363 { ACCESSIBILITY_TAG_PROGRESS,
364 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
365 { ACCESSIBILITY_TAG_RATING,
366 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
367 { ACCESSIBILITY_TAG_REFRESH,
368 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
369 { ACCESSIBILITY_TAG_SELECT,
370 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
371 { ACCESSIBILITY_TAG_SLIDER,
372 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
373 { ACCESSIBILITY_TAG_SPAN,
374 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
375 { ACCESSIBILITY_TAG_STACK,
376 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
377 { ACCESSIBILITY_TAG_SWIPER,
378 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
379 { ACCESSIBILITY_TAG_SWITCH,
380 { .checkable = true, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
381 { ACCESSIBILITY_TAG_TAB_BAR,
382 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
383 { ACCESSIBILITY_TAG_TAB_CONTENT,
384 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
385 { ACCESSIBILITY_TAG_TABS,
386 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
387 { ACCESSIBILITY_TAG_TEXT,
388 { .checkable = false, .clickable = true, .scrollable = true, .longClickable = true, .focusable = true } },
389 { ACCESSIBILITY_TAG_TEXTAREA,
390 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
391 { ACCESSIBILITY_TAG_VIDEO,
392 { .checkable = false, .clickable = true, .scrollable = false, .longClickable = true, .focusable = true } },
393 };
394
395 // set node operable info
396 int64_t operateIter = BinarySearchFindIndex(nodeOperatorMap, ArraySize(nodeOperatorMap), tag_.c_str());
397 if (operateIter != -1) {
398 isCheckable_ = nodeOperatorMap[operateIter].value.checkable;
399 isScrollable_ = nodeOperatorMap[operateIter].value.scrollable;
400 isFocusable_ = nodeOperatorMap[operateIter].value.focusable;
401 if (isFocusable_) {
402 AddSupportAction(AceAction::ACTION_FOCUS);
403 }
404 } else {
405 isCheckable_ = false;
406 isClickable_ = false;
407 isScrollable_ = false;
408 isLongClickable_ = false;
409 isFocusable_ = false;
410 }
411
412 if (tag_ == ACCESSIBILITY_TAG_INPUT) {
413 if (inputType_ == INPUT_TYPE_CHECKBOX || inputType_ == INPUT_TYPE_RADIO) {
414 isCheckable_ = true;
415 } else if (inputType_ == INPUT_TYPE_PASSWORD) {
416 isPassword_ = true;
417 }
418 }
419 }
420
GetSupportAction(uint64_t enableActions) const421 std::unordered_set<AceAction> AccessibilityNode::GetSupportAction(uint64_t enableActions) const
422 {
423 static const AceAction allActions[] = {
424 AceAction::ACTION_NONE, AceAction::GLOBAL_ACTION_BACK, AceAction::CUSTOM_ACTION, AceAction::ACTION_CLICK,
425 AceAction::ACTION_LONG_CLICK, AceAction::ACTION_SCROLL_FORWARD, AceAction::ACTION_SCROLL_BACKWARD,
426 AceAction::ACTION_FOCUS, AceAction::ACTION_ACCESSIBILITY_FOCUS, AceAction::ACTION_CLEAR_ACCESSIBILITY_FOCUS,
427 AceAction::ACTION_NEXT_AT_MOVEMENT_GRANULARITY, AceAction::ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
428 AceAction::ACTION_SET_TEXT,
429 };
430
431 std::unordered_set<AceAction> supportActions;
432 if (supportActions_ == 0) {
433 return supportActions;
434 }
435
436 auto finalSupportActions = supportActions_ & enableActions;
437 for (auto action : allActions) {
438 if ((finalSupportActions & (1UL << static_cast<uint32_t>(action))) != 0) {
439 supportActions.emplace(action);
440 }
441 }
442 return supportActions;
443 }
444
SetFocusChangeEventMarker(const EventMarker & eventId)445 void AccessibilityNode::SetFocusChangeEventMarker(const EventMarker& eventId)
446 {
447 if (eventId.IsEmpty()) {
448 return;
449 }
450
451 auto container = Container::Current();
452 CHECK_NULL_VOID(container);
453 #ifndef NG_BUILD
454 auto pipelineContext = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
455 CHECK_NULL_VOID(pipelineContext);
456 focusChangeEventId_ =
457 AceAsyncEvent<void(const std::string&)>::Create(eventId, pipelineContext);
458 #endif
459 }
460
OnFocusChange(bool isFocus)461 void AccessibilityNode::OnFocusChange(bool isFocus)
462 {
463 CHECK_NULL_VOID(focusChangeEventId_);
464 auto json = JsonUtil::Create(true);
465 json->Put("eventType", isFocused_ ? "1" : "2");
466 focusChangeEventId_(json->ToString());
467 }
468
469 } // namespace OHOS::Ace
470