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 "accessible_ability_client_stub_impl.h"
17 #include "accessibility_extension_context.h"
18 #include "accessibility_ui_test_ability.h"
19 #include "display_resize_controller.h"
20
21 using namespace std;
22
23 namespace OHOS {
24 namespace Accessibility {
RegisterListenerImpl(const std::shared_ptr<AccessibilityExtension> & listener)25 void AccessibleAbilityClientStubImpl::RegisterListenerImpl(const std::shared_ptr<AccessibilityExtension> &listener)
26 {
27 HILOG_DEBUG("Register AccessibleAbilityClientStubImpl listener.");
28 if (listener_) {
29 HILOG_DEBUG("listener already exists.");
30 return;
31 }
32
33 listener_ = listener;
34 }
35
SetUITestEnabled()36 void AccessibleAbilityClientStubImpl::SetUITestEnabled()
37 {
38 HILOG_DEBUG("start.");
39 uiTestEnabled_ = true;
40 }
41
RegisterUITestAbilityListener(const std::shared_ptr<IAccessibleUITestAbilityListener> & listener)42 bool AccessibleAbilityClientStubImpl::RegisterUITestAbilityListener(
43 const std::shared_ptr<IAccessibleUITestAbilityListener> &listener)
44 {
45 HILOG_DEBUG("start.");
46 if (uiTestListener_) {
47 HILOG_DEBUG("listener already exists.");
48 return false;
49 }
50
51 uiTestListener_ = listener;
52 return true;
53 }
54
Init(const sptr<IAccessibleAbilityChannel> & channel,const int channelId)55 void AccessibleAbilityClientStubImpl::Init(const sptr<IAccessibleAbilityChannel> &channel, const int channelId)
56 {
57 HILOG_DEBUG("start.");
58 if (!uiTestEnabled_) {
59 if (!channel) {
60 HILOG_DEBUG("channel is nullptr.");
61 return;
62 }
63 if (!listener_ || !listener_->GetContext()) {
64 HILOG_ERROR("listener_ is nullptr or there is no context in listener_.");
65 return;
66 }
67
68 listener_->GetContext()->SetChannelId(channelId);
69 AccessibilityOperator::AddChannel(channelId, channel);
70 channelId_ = channelId;
71 channel_ = channel;
72
73 // Add death recipient
74 if (!deathRecipient_) {
75 deathRecipient_ = new AccessibleAbilityDeathRecipient(channelId_, channel_);
76 }
77
78 auto object = channel_->AsObject();
79 if (object) {
80 HILOG_DEBUG("Add death recipient");
81 object->AddDeathRecipient(deathRecipient_);
82 }
83
84 listener_->OnAbilityConnected();
85 } else {
86 if (!channel) {
87 HILOG_DEBUG("channel is nullptr.");
88 return;
89 }
90 if (!uiTestListener_) {
91 HILOG_ERROR("listener_ is nullptr.");
92 return;
93 }
94
95 AccessibilityUITestAbility::GetInstance()->SetChannelId(channelId);
96 AccessibilityOperator::AddChannel(channelId, channel);
97 channelId_ = channelId;
98 channel_ = channel;
99
100 // Add death recipient
101 if (!deathRecipient_) {
102 deathRecipient_ = new AccessibleAbilityDeathRecipient(channelId_, channel_);
103 }
104
105 auto object = channel_->AsObject();
106 if (object) {
107 HILOG_DEBUG("Add death recipient");
108 object->AddDeathRecipient(deathRecipient_);
109 }
110
111 uiTestListener_->OnAbilityConnected();
112 }
113 }
114
Disconnect(const int channelId)115 void AccessibleAbilityClientStubImpl::Disconnect(const int channelId)
116 {
117 HILOG_DEBUG("start.");
118
119 // Delete death recipient
120 if (channel_ && channel_->AsObject()) {
121 HILOG_ERROR("Remove death recipient");
122 channel_->AsObject()->RemoveDeathRecipient(deathRecipient_);
123 }
124
125 // Remove channel
126 AccessibilityOperator::RemoveChannel(channelId);
127 channelId_ = INVALID_CHANNEL_ID;
128 channel_ = nullptr;
129 if (!uiTestEnabled_) {
130 if (listener_ && listener_->GetContext()) {
131 HILOG_DEBUG("Clear extensionContext channelId.");
132 listener_->GetContext()->SetChannelId(channelId_);
133 listener_ = nullptr;
134 }
135 } else {
136 AccessibilityUITestAbility::GetInstance()->SetChannelId(channelId_);
137 uiTestListener_->OnAbilityDisconnected();
138 uiTestListener_ = nullptr;
139 }
140 }
141
OnAccessibilityEvent(const AccessibilityEventInfo & eventInfo)142 void AccessibleAbilityClientStubImpl::OnAccessibilityEvent(const AccessibilityEventInfo &eventInfo)
143 {
144 HILOG_DEBUG("start.");
145 if (channelId_ != INVALID_CHANNEL_ID) {
146 if (listener_) {
147 listener_->OnAccessibilityEvent(eventInfo);
148 }
149
150 if (uiTestListener_) {
151 uiTestListener_->OnAccessibilityEvent(eventInfo);
152 }
153 }
154 }
155
OnKeyPressEvent(const MMI::KeyEvent & keyEvent,const int sequence)156 void AccessibleAbilityClientStubImpl::OnKeyPressEvent(const MMI::KeyEvent &keyEvent, const int sequence)
157 {
158 HILOG_DEBUG("start.");
159 if (listener_) {
160 bool handled = listener_->OnKeyPressEvent(keyEvent);
161 AccessibilityOperator::GetInstance().SetOnKeyPressEventResult(channelId_, handled, sequence);
162 }
163
164 if (uiTestListener_) {
165 std::shared_ptr<MMI::KeyEvent> tmp = std::make_shared<MMI::KeyEvent>(keyEvent);
166 bool handled = uiTestListener_->OnKeyPressEvent(tmp, sequence);
167 AccessibilityOperator::GetInstance().SetOnKeyPressEventResult(channelId_, handled, sequence);
168 }
169 }
170
OnDisplayResized(const int displayId,const Rect & rect,const float scale,const float centerX,const float centerY)171 void AccessibleAbilityClientStubImpl::OnDisplayResized(const int displayId, const Rect &rect, const float scale,
172 const float centerX, const float centerY)
173 {
174 HILOG_DEBUG("start.");
175 if (channelId_ != INVALID_CHANNEL_ID) {
176 if (listener_ && listener_->GetContext()) {
177 HILOG_DEBUG("Get displayResize controller.");
178 shared_ptr<DisplayResizeController> displayResizeController =
179 listener_->GetContext()->GetDisplayResizeController(displayId);
180 if (!displayResizeController) {
181 HILOG_ERROR("There is no displayResizeController.");
182 return;
183 }
184
185 displayResizeController->DispatchOnDisplayResized(rect, scale, centerX, centerY);
186 }
187 }
188 }
189
OnGestureSimulateResult(const int sequence,const bool completedSuccessfully)190 void AccessibleAbilityClientStubImpl::OnGestureSimulateResult(const int sequence, const bool completedSuccessfully)
191 {
192 HILOG_DEBUG("start.");
193 if (channelId_ != INVALID_CHANNEL_ID) {
194 if (listener_ && listener_->GetContext()) {
195 HILOG_DEBUG("Dispatch the result of simulation gesture.");
196 listener_->GetContext()->DispatchOnSimulationGestureResult(sequence, completedSuccessfully);
197 }
198
199 if (uiTestEnabled_) {
200 HILOG_DEBUG("Dispatch the result of simulation gesture.");
201 AccessibilityUITestAbility::GetInstance()->DispatchOnSimulationGestureResult(
202 sequence, completedSuccessfully);
203 }
204 }
205 }
206
OnRemoteDied(const wptr<IRemoteObject> & remote)207 void AccessibleAbilityClientStubImpl::AccessibleAbilityDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
208 {
209 HILOG_DEBUG("start.");
210
211 // Delete death recipient
212 if (!remote.GetRefPtr()) {
213 HILOG_ERROR("remote is nullptr.");
214 return;
215 }
216
217 if (!recipientchannel_ && (recipientchannel_->AsObject() != remote)) {
218 HILOG_ERROR("recipientchannel_ is nullptr or remote is wrong.");
219 return;
220 }
221 remote->RemoveDeathRecipient(this);
222
223 // Remove channel
224 AccessibilityOperator::RemoveChannel(recipientChannelId_);
225 recipientChannelId_ = INVALID_CHANNEL_ID;
226 recipientchannel_ = nullptr;
227 }
228 } // namespace Accessibility
229 } // namespace OHOS