d4bfb689d276f79aee5376734bf65ed661b2c46e
[platform/core/uifw/aurum.git] / libaurum / src / Accessibility / AccessibleNode.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17
18 #include "Aurum.h"
19
20 #include <string.h>
21 #include <iostream>
22 #include <vector>
23
24 #include "config.h"
25 #include <sstream>
26
27 using namespace Aurum;
28
29 AccessibleNode::~AccessibleNode()
30 {
31 }
32
33 AccessibleNode::AccessibleNode()
34 : mText{""}, mPkg{""}, mRole{""}, mId{""}, mType{""}, mStyle{""},
35   mScreenBoundingBox{0,0,0,0}, mWindowBoundingBox{0,0,0,0}, mSupportingIfaces(0), mFeatureProperty(0), mValid{true}, mLock{}
36 {
37 }
38
39 std::string AccessibleNode::description() {
40     std::stringstream ss{};
41
42     ss << "{";
43     ss << "\"mId\":\"" << this->mId << "\", ";
44     ss << "\"mAutomationId\":\"" << this->mAutomationId << "\", ";
45     ss << "\"mRole\":\"" << this->mRole << "\", ";
46     ss << "\"mText\":\"" << this->mText << "\", ";
47     ss << "\"mPkg\":\"" << this->mPkg << "\", ";
48     ss << "\"mType\":\"" << this->mType << "\", ";
49     ss << "\"mStyle\":\"" << this->mStyle << "\", ";
50     ss << "}";
51
52     return ss.str();
53 }
54
55 void AccessibleNode::notify(int type1, int type2, void *src)
56 {
57     void *handler = getRawHandler();
58
59     if ((EventType)type1 == EventType::Object && (ObjectEventType)type2 == ObjectEventType::ObjectStateDefunct) {
60         if (handler == src) invalidate();
61     }
62 }
63
64 void AccessibleNode::invalidate()
65 {
66     std::unique_lock<std::mutex> lock(mLock);
67     LOGI("object %p is now invalid", this);
68     mValid = false;
69 }
70
71 bool AccessibleNode::isValid() const
72 {
73     std::unique_lock<std::mutex> lock(mLock);
74     if (!getRawHandler() || !mValid) return false;
75     return true;
76 }
77
78 void AccessibleNode::print(int depth, int maxDepth)
79 {
80     if (maxDepth <= 0 || depth > maxDepth) return;
81
82     this->print(depth);
83     auto children = this->getChildren();
84     for ( auto &child : children ) {
85         if (child) child->print(depth +1, maxDepth);
86     }
87 }
88
89 void AccessibleNode::print(int d)
90 {
91     this->refresh();
92     LOGI("%s %s",std::string(d, ' ').c_str(), description().c_str());
93 }
94
95 bool AccessibleNode::isSupporting(AccessibleNodeInterface thisIface) const
96 {
97     return (mSupportingIfaces & static_cast<int>(thisIface)) != 0;
98 }
99
100 bool AccessibleNode::hasFeatureProperty(NodeFeatureProperties prop) const
101 {
102     return (mFeatureProperty & static_cast<int>(prop)) != 0;
103 }
104
105 void AccessibleNode::setFeatureProperty(NodeFeatureProperties prop, bool has)
106 {
107     if (has)
108         mFeatureProperty |= static_cast<int>(prop);
109     else
110         mFeatureProperty &= ~static_cast<int>(prop);
111 }
112
113 void AccessibleNode::resetFeatureProperty()
114 {
115     mFeatureProperty = 0;
116 }
117
118 std::string AccessibleNode::getText() const
119 {
120     return mText;
121 }
122
123 std::string AccessibleNode::getPkg() const
124 {
125     return mPkg;
126 }
127
128 std::string AccessibleNode::getId() const
129 {
130     return mId;
131 }
132
133 std::string AccessibleNode::getAutomationId() const
134 {
135     return mAutomationId;
136 }
137
138 std::string AccessibleNode::getRole() const
139 {
140     return mRole;
141 }
142
143 std::string AccessibleNode::getType() const
144 {
145     return mType;
146 }
147
148 std::string AccessibleNode::getStyle() const
149 {
150     return mStyle;
151 }
152
153 Rect<int> AccessibleNode::getScreenBoundingBox() const
154 {
155     return mScreenBoundingBox;
156 }
157
158 Rect<int> AccessibleNode::getWindowBoundingBox() const
159 {
160     return mWindowBoundingBox;
161 }
162
163 bool AccessibleNode::isCheckable() const
164 {
165     return hasFeatureProperty(NodeFeatureProperties::CHECKABLE);
166 }
167
168 bool AccessibleNode::isChecked() const
169 {
170     return hasFeatureProperty(NodeFeatureProperties::CHECKED);
171 }
172
173 bool AccessibleNode::isClickable() const
174 {
175     return hasFeatureProperty(NodeFeatureProperties::CLICKABLE);
176 }
177
178 bool AccessibleNode::isEnabled() const
179 {
180     return hasFeatureProperty(NodeFeatureProperties::ENABLED);
181 }
182
183 bool AccessibleNode::isFocusable() const
184 {
185     return hasFeatureProperty(NodeFeatureProperties::FOCUSABLE);
186 }
187
188 bool AccessibleNode::isFocused() const
189 {
190     return hasFeatureProperty(NodeFeatureProperties::FOCUSED);
191 }
192
193 bool AccessibleNode::isLongClickable() const
194 {
195     return hasFeatureProperty(NodeFeatureProperties::LONGCLICKABLE);
196 }
197
198 bool AccessibleNode::isScrollable() const
199 {
200     return hasFeatureProperty(NodeFeatureProperties::SCROLLABLE);
201 }
202
203 bool AccessibleNode::isSelectable() const
204 {
205     return hasFeatureProperty(NodeFeatureProperties::SELECTABLE);
206 }
207
208 bool AccessibleNode::isSelected() const
209 {
210     return hasFeatureProperty(NodeFeatureProperties::SELECTED);
211 }
212
213 bool AccessibleNode::isVisible() const
214 {
215     return hasFeatureProperty(NodeFeatureProperties::VISIBLE);
216 }
217
218 bool AccessibleNode::isShowing() const
219 {
220     return hasFeatureProperty(NodeFeatureProperties::SHOWING);
221 }
222
223 bool AccessibleNode::isActive() const
224 {
225     return hasFeatureProperty(NodeFeatureProperties::ACTIVE);
226 }