• 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_extension_context.h"
17 #include "accessibility_element_info.h"
18 #include "accessible_ability_client_stub_impl.h"
19 #include "accessible_ability_manager_service_proxy.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 
23 using namespace std;
24 
25 namespace OHOS {
26 namespace Accessibility {
GetFocusElementInfo(uint32_t focusType,std::optional<AccessibilityElementInfo> & elementInfo)27 bool AccessibilityExtensionContext::GetFocusElementInfo(
28     uint32_t focusType, std::optional<AccessibilityElementInfo>& elementInfo)
29 {
30     HILOG_DEBUG("start.");
31     if ((focusType != FOCUS_TYPE_INPUT) && (focusType != FOCUS_TYPE_ACCESSIBILITY)) {
32         HILOG_DEBUG("focusType is not allowed.");
33         return false;
34     }
35 
36     AccessibilityElementInfo info {};
37     bool result = AccessibilityOperator::GetInstance().FindFocusedElementInfo(channelId_,
38         ANY_WINDOW_ID, ROOT_NODE_ID, focusType, info);
39     elementInfo = info;
40     return result;
41 }
42 
GestureSimulate(uint32_t sequence,const std::vector<GesturePathDefine> & gesturePathDefineList,const std::shared_ptr<GestureResultListener> & listener)43 bool AccessibilityExtensionContext::GestureSimulate(uint32_t sequence,
44     const std::vector<GesturePathDefine>& gesturePathDefineList,
45     const std::shared_ptr<GestureResultListener>& listener)
46 {
47     HILOG_DEBUG("start.");
48 
49     if (gesturePathDefineList.size() > gesturePathDefineList.front().GetMaxStrokes() ||
50         gesturePathDefineList.size() <= 0) {
51         HILOG_ERROR("The number of gesturePathDefine : [%{public}d] is not allowed.", gesturePathDefineList.size());
52         return false;
53     }
54 
55     int64_t totalDurationTime = 0;
56     for (auto gesturePath : gesturePathDefineList) {
57         totalDurationTime += gesturePath.GetDurationTime();
58     }
59     HILOG_DEBUG("The total duration time is %{public}lld.", totalDurationTime);
60 
61     if (totalDurationTime > gesturePathDefineList.front().GetMaxStrokeDuration()) {
62         HILOG_ERROR("The total duration time : [%{public}lld] is not allowed.", totalDurationTime);
63         return false;
64     }
65 
66     if (listener) {
67         gestureResultListenerInfos_.insert(make_pair(sequence, listener));
68     }
69 
70     AccessibilityOperator::GetInstance().SendSimulateGesture(channelId_, sequence, gesturePathDefineList);
71     return true;
72 }
73 
GetDisplayResizeController()74 std::shared_ptr<DisplayResizeController>& AccessibilityExtensionContext::GetDisplayResizeController()
75 {
76     HILOG_DEBUG("start.");
77 
78     // this is a temp deal: To make sure the id of DEFAULT_DISPALY.
79     uint32_t DEFAULT_DISPALY = 0;
80     return GetDisplayResizeController(DEFAULT_DISPALY);
81 }
82 
GetDisplayResizeController(uint32_t displayId)83 std::shared_ptr<DisplayResizeController>& AccessibilityExtensionContext::GetDisplayResizeController(uint32_t displayId)
84 {
85     HILOG_DEBUG("start.");
86 
87     auto it = displayResizeControllers_.find(displayId);
88     if (it != displayResizeControllers_.end()) {
89         return displayResizeControllers_[displayId];
90     } else {
91         HILOG_DEBUG("Have no DisplayResizeController and make a new one.");
92         displayResizeControllers_.insert(make_pair(displayId,
93             make_shared<DisplayResizeController>(channelId_, displayId)));
94         return displayResizeControllers_[displayId];
95     }
96 }
97 
GetRootElementInfo(std::optional<AccessibilityElementInfo> & elementInfo)98 bool AccessibilityExtensionContext::GetRootElementInfo(std::optional<AccessibilityElementInfo>& elementInfo)
99 {
100     HILOG_DEBUG("start.");
101     AccessibilityElementInfo info {};
102     bool result = AccessibilityOperator::GetInstance().GetRoot(channelId_, info);
103     elementInfo = info;
104     return result;
105 }
106 
GetWindows()107 std::vector<AccessibilityWindowInfo>& AccessibilityExtensionContext::GetWindows()
108 {
109     HILOG_DEBUG("start.");
110     accessibilityWindow_ = AccessibilityOperator::GetInstance().GetWindows(channelId_);
111     return accessibilityWindow_;
112 }
113 
ExecuteCommonAction(uint32_t action)114 bool AccessibilityExtensionContext::ExecuteCommonAction(uint32_t action)
115 {
116     HILOG_DEBUG("start.");
117     if (!CheckCommonAction(action)) {
118         HILOG_ERROR("action is not allowed.");
119         return false;
120     }
121 
122     return AccessibilityOperator::GetInstance().ExecuteCommonAction(channelId_, action);
123 }
124 
DispatchOnSimulationGestureResult(uint32_t sequence,bool result)125 void AccessibilityExtensionContext::DispatchOnSimulationGestureResult(uint32_t sequence, bool result)
126 {
127     HILOG_DEBUG("start.");
128 
129     if (gestureResultListenerInfos_.empty()) {
130         HILOG_ERROR("There is no information of gestureResultListener");
131         return;
132     }
133 
134     shared_ptr<GestureResultListener> gestureResultListener = nullptr;
135     auto it = gestureResultListenerInfos_.find(sequence);
136     if (it != gestureResultListenerInfos_.end()) {
137         HILOG_DEBUG("gestureResultListenerInfo has been found.");
138         gestureResultListener = gestureResultListenerInfos_[sequence];
139     }
140 
141     if (!gestureResultListener) {
142         HILOG_ERROR("There is no gestureResultListenerInfo in gestureResultListenerInfos_[%{public}d", sequence);
143         return;
144     }
145 
146     HILOG_DEBUG("GestureInject result is %{public}d", result);
147     gestureResultListener->OnGestureInjectResult(sequence, result);
148 }
149 
SetChannelId(uint32_t channelId)150 void AccessibilityExtensionContext::SetChannelId(uint32_t channelId)
151 {
152     HILOG_DEBUG("start.");
153     channelId_ = channelId;
154 }
155 
CheckCommonAction(uint32_t action)156 bool AccessibilityExtensionContext::CheckCommonAction(uint32_t action)
157 {
158     HILOG_DEBUG("start.");
159     if ((action != GLOBAL_ACTION_BACK) && (action != GLOBAL_ACTION_HOME) &&
160         (action != GLOBAL_ACTION_RECENT) && (action != GLOBAL_ACTION_NOTIFICATION) &&
161         (action != GLOBAL_ACTION_POP_UP_POWER_DIALOG) && (action != GLOBAL_ACTION_DIVIDE_SCREEN) &&
162         (action != GLOBAL_ACTION_LOCK_SCREEN) && (action != GLOBAL_ACTION_CAPTURE_SCREEN)) {
163         return false;
164     } else {
165         return true;
166     }
167 }
168 } // namespace Accessibility
169 } // namespace OHOS