• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "accessibility_window_info.h"
17 #include "accessibility_operator.h"
18 #include "hilog_wrapper.h"
19 
20 namespace OHOS {
21 namespace Accessibility {
22 /* AccessibleAction     Parcel struct                   */
ReadFromParcel(Parcel & parcel)23 bool AccessibilityWindowInfo::ReadFromParcel(Parcel &parcel)
24 {
25     int windowType = TYPE_WINDOW_INVALID;
26     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowType);
27     windowType_ = static_cast<WindowType>(windowType);
28     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowLayer_);
29     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowId_);
30     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, parentId_);
31     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, windowTitle_);
32     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32Vector, parcel, &childIds_);
33     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, anchorId_);
34     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, childNum_);
35     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, channelId_);
36     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, active_);
37     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, focused_);
38     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, accessibilityFocused_);
39     sptr<Rect> boundsInScreen = parcel.ReadStrongParcelable<Rect>();
40     if (!boundsInScreen) {
41         HILOG_ERROR("ReadStrongParcelable boundsInScreen failed.");
42         return false;
43     }
44     boundsInScreen_ = *boundsInScreen;
45 
46     return true;
47 }
48 
Marshalling(Parcel & parcel) const49 bool AccessibilityWindowInfo::Marshalling(Parcel &parcel) const
50 {
51     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int>(windowType_));
52     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowLayer_);
53     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, windowId_);
54     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, parentId_);
55     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, windowTitle_);
56     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32Vector, parcel, childIds_);
57     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, anchorId_);
58     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, childNum_);
59     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, channelId_);
60     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, active_);
61     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, focused_);
62     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, accessibilityFocused_);
63     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &boundsInScreen_);
64 
65     return true;
66 };
67 
Unmarshalling(Parcel & parcel)68 AccessibilityWindowInfo* AccessibilityWindowInfo::Unmarshalling(Parcel& parcel)
69 {
70     AccessibilityWindowInfo *accessibilityWindow = new AccessibilityWindowInfo();
71     if (!accessibilityWindow->ReadFromParcel(parcel)) {
72         delete accessibilityWindow;
73         accessibilityWindow = nullptr;
74     }
75     return accessibilityWindow;
76 }
77 
GetWindowTitle() const78 std::string AccessibilityWindowInfo::GetWindowTitle() const
79 {
80     HILOG_DEBUG("windowTitle_[%{public}s]", windowTitle_.c_str());
81     return windowTitle_;
82 }
83 
SetWindowTitle(std::string & title)84 void AccessibilityWindowInfo::SetWindowTitle(std::string &title)
85 {
86     windowTitle_ = title;
87     HILOG_DEBUG("windowTitle_[%{public}s]", windowTitle_.c_str());
88 }
89 
GetWindowType() const90 WindowType AccessibilityWindowInfo::GetWindowType() const
91 {
92     HILOG_DEBUG("windowType_[%{public}d]", windowType_);
93     return windowType_;
94 }
95 
SetWindowType(const WindowType type)96 void AccessibilityWindowInfo::SetWindowType(const WindowType type)
97 {
98     windowType_ = type;
99     HILOG_DEBUG("windowType_[%{public}d]", windowType_);
100 }
101 
GetWindowLayer() const102 int AccessibilityWindowInfo::GetWindowLayer() const
103 {
104     HILOG_DEBUG("window(%{public}d)Layer_[%{public}d]", windowId_, windowLayer_);
105     return windowLayer_;
106 }
107 
SetWindowLayer(const int layer)108 void AccessibilityWindowInfo::SetWindowLayer(const int layer)
109 {
110     windowLayer_ = layer;
111     HILOG_DEBUG("window(%{public}d)Layer[%{public}d]", windowId_, windowLayer_);
112 }
113 
GetRootAccessibilityInfo(AccessibilityElementInfo & elementInfo)114 bool AccessibilityWindowInfo::GetRootAccessibilityInfo(AccessibilityElementInfo &elementInfo)
115 {
116     HILOG_INFO("channelId_[%{public}d], windowId_[%{public}d]", channelId_, windowId_);
117     AccessibilityOperator *instance = &AccessibilityOperator::GetInstance();
118 
119     std::vector<AccessibilityElementInfo> elementInfos {};
120     bool result = false;
121     if (instance != nullptr) {
122         result = instance->SearchElementInfosByAccessibilityId(channelId_, windowId_, -1, 0, elementInfos);
123         if (elementInfos.empty()) {
124             result = false;
125         } else {
126             elementInfo = elementInfos.front();
127         }
128     } else {
129         HILOG_INFO("Can't get AccessibilityOperator instance");
130     }
131     return result;
132 }
133 
SetAnchorId(const int anchorId)134 void AccessibilityWindowInfo::SetAnchorId(const int anchorId)
135 {
136     anchorId_ = anchorId;
137     HILOG_DEBUG("anchorId_[%{public}d]", anchorId_);
138 }
139 
GetAnchor(AccessibilityElementInfo & elementInfo)140 bool AccessibilityWindowInfo::GetAnchor(AccessibilityElementInfo &elementInfo)
141 {
142     HILOG_DEBUG("GetAnchor of windowInfo is not support!");
143     HILOG_INFO("channelId_[%{public}d], windowId_[%{public}d], anchorId_[%{public}d]",
144         channelId_, windowId_, anchorId_);
145     AccessibilityOperator *instance = &AccessibilityOperator::GetInstance();
146 
147     std::vector<AccessibilityElementInfo> elementInfos {};
148     bool result = false;
149     if (instance != nullptr) {
150         result = instance->SearchElementInfosByAccessibilityId(channelId_, windowId_, anchorId_, 0, elementInfos);
151         if (elementInfos.empty()) {
152             result = false;
153         } else {
154             elementInfo = elementInfos.front();
155         }
156     } else {
157         HILOG_INFO("Can't get AccessibilityOperator instance");
158     }
159     return result;
160 }
161 
GetParent()162 AccessibilityWindowInfo AccessibilityWindowInfo::GetParent()
163 {
164     HILOG_DEBUG("GetParent of windowInfo is not support!");
165     AccessibilityWindowInfo win {};
166     HILOG_INFO("channelId_[%{public}d], parentId_[%{public}d]",
167         channelId_, parentId_);
168     AccessibilityOperator *instance = &AccessibilityOperator::GetInstance();
169     if (instance != nullptr) {
170         for (auto window : instance->GetWindows(channelId_)) {
171             if (window.GetWindowId() == parentId_) {
172                 return window;
173             }
174         }
175     } else {
176         HILOG_INFO("Can't get AccessibilityOperator instance");
177     }
178     return win;
179 }
180 
SetParentId(const int parentId)181 void AccessibilityWindowInfo::SetParentId(const int parentId)
182 {
183     parentId_ = parentId;
184     HILOG_DEBUG("parentId_[%{public}d]", parentId_);
185 }
186 
GetParentId() const187 int AccessibilityWindowInfo::GetParentId() const
188 {
189     HILOG_DEBUG("parentId_[%{public}d]", parentId_);
190     return parentId_;
191 }
192 
GetChildIds() const193 std::vector<int>  AccessibilityWindowInfo::GetChildIds() const
194 {
195     return childIds_;
196 }
197 
GetAnchorId() const198 int AccessibilityWindowInfo::GetAnchorId() const
199 {
200     HILOG_DEBUG("anchorId_[%{public}d]", anchorId_);
201     return anchorId_;
202 }
GetWindowId() const203 int AccessibilityWindowInfo::GetWindowId() const
204 {
205     HILOG_DEBUG("windowId_[%{public}d]", windowId_);
206     return windowId_;
207 }
208 
SetWindowId(const int id)209 void AccessibilityWindowInfo::SetWindowId(const int id)
210 {
211     windowId_ = id;
212     HILOG_DEBUG("windowId_[%{public}d]", windowId_);
213 }
214 
SetChannelId(const int channelId)215 void AccessibilityWindowInfo::SetChannelId(const int channelId)
216 {
217     channelId_ = channelId;
218     HILOG_DEBUG("channelId_[%{public}d]", channelId_);
219 }
220 
GetChannelId() const221 int AccessibilityWindowInfo::GetChannelId() const
222 {
223     HILOG_DEBUG("channelId_[%{public}d]", channelId_);
224     return channelId_;
225 }
226 
GetRectInScreen() const227 Rect AccessibilityWindowInfo::GetRectInScreen() const
228 {
229     return boundsInScreen_;
230 }
231 
SetRectInScreen(const Rect & bounds)232 void AccessibilityWindowInfo::SetRectInScreen(const Rect &bounds)
233 {
234     boundsInScreen_.SetLeftTopScreenPostion(const_cast<Rect &>(bounds).GetLeftTopXScreenPostion(),
235         const_cast<Rect &>(bounds).GetLeftTopYScreenPostion());
236     boundsInScreen_.SetRightBottomScreenPostion(const_cast<Rect &>(bounds).GetRightBottomXScreenPostion(),
237         const_cast<Rect &>(bounds).GetRightBottomYScreenPostion());
238 }
239 
IsActive() const240 bool AccessibilityWindowInfo::IsActive() const
241 {
242     HILOG_DEBUG("active_[%{public}d]", active_);
243     return active_;
244 }
245 
SetActive(bool active)246 void AccessibilityWindowInfo::SetActive(bool active)
247 {
248     active_ = active;
249     HILOG_DEBUG("active_[%{public}d]", active_);
250 }
251 
IsFocused() const252 bool AccessibilityWindowInfo::IsFocused() const
253 {
254     HILOG_DEBUG("focused_[%{public}d]", focused_);
255     return focused_;
256 }
257 
SetFocused(bool focused)258 void AccessibilityWindowInfo::SetFocused(bool focused)
259 {
260     focused_ = focused;
261     HILOG_DEBUG("focused_[%{public}d]", focused_);
262 }
263 
IsAccessibilityFocused() const264 bool AccessibilityWindowInfo::IsAccessibilityFocused() const
265 {
266     HILOG_DEBUG("accessibilityFocused_[%{public}d]", accessibilityFocused_);
267     return accessibilityFocused_;
268 }
269 
SetAccessibilityFocused(const bool accessibilityfocused)270 void AccessibilityWindowInfo::SetAccessibilityFocused(const bool accessibilityfocused)
271 {
272     accessibilityFocused_ = accessibilityfocused;
273     HILOG_DEBUG("accessibilityFocused_[%{public}d]", accessibilityFocused_);
274 }
275 
GetChildNum() const276 int AccessibilityWindowInfo::GetChildNum() const
277 {
278     HILOG_DEBUG("childNum_[%{public}d]", childNum_);
279     return childNum_;
280 }
281 
GetChild(const int index)282 AccessibilityWindowInfo AccessibilityWindowInfo::GetChild(const int index)
283 {
284     HILOG_DEBUG("GetChild of windowInfo is not support!");
285     AccessibilityWindowInfo win {};
286     HILOG_INFO("channelId_[%{public}d], childNum_[%{public}d], index[%{public}d]",
287         channelId_, childNum_, index);
288     if (index >= childNum_ || index < 0) {
289         HILOG_ERROR("index[%{public}d] is invalid", index);
290         return win;
291     }
292     AccessibilityOperator *instance = &AccessibilityOperator::GetInstance();
293     if (instance != nullptr) {
294         for (auto window : instance->GetWindows(channelId_)) {
295         if (window.GetWindowId() == childIds_[index]) {
296             return window;
297         }
298     }
299     } else {
300         HILOG_INFO("Can't get AccessibilityOperator instance");
301     }
302     return win;
303 }
304 
AddChild(const int childId)305 void AccessibilityWindowInfo::AddChild(const int childId)
306 {
307     childIds_.push_back(childId);
308     childNum_++;
309 }
310 
AccessibilityWindowInfo()311 AccessibilityWindowInfo::AccessibilityWindowInfo()
312 {
313 }
314 } // namespace Accessibility
315 } // namespace OHOS