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_manager_state_observer_proxy.h"
17 #include "accessibility_ipc_interface_code.h"
18 #include "hilog_wrapper.h"
19 
20 namespace OHOS {
21 namespace Accessibility {
AccessibleAbilityManagerStateObserverProxy(const sptr<IRemoteObject> & impl)22 AccessibleAbilityManagerStateObserverProxy::AccessibleAbilityManagerStateObserverProxy(
23     const sptr<IRemoteObject> &impl) : IRemoteProxy<IAccessibleAbilityManagerStateObserver>(impl)
24 {}
25 
~AccessibleAbilityManagerStateObserverProxy()26 AccessibleAbilityManagerStateObserverProxy::~AccessibleAbilityManagerStateObserverProxy()
27 {}
28 
WriteInterfaceToken(MessageParcel & data)29 bool AccessibleAbilityManagerStateObserverProxy::WriteInterfaceToken(MessageParcel &data)
30 {
31     HILOG_DEBUG();
32     if (!data.WriteInterfaceToken(AccessibleAbilityManagerStateObserverProxy::GetDescriptor())) {
33         HILOG_ERROR("write interface token failed");
34         return false;
35     }
36     return true;
37 }
38 
OnStateChanged(const uint32_t stateType)39 void AccessibleAbilityManagerStateObserverProxy::OnStateChanged(const uint32_t stateType)
40 {
41     HILOG_DEBUG();
42 
43     int error = NO_ERROR;
44     MessageParcel data;
45     MessageParcel reply;
46     MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
47 
48     if (!WriteInterfaceToken(data)) {
49         HILOG_ERROR("fail, connection write Token");
50         return;
51     }
52 
53     if (!data.WriteUint32(stateType)) {
54         HILOG_ERROR("fail, connection write stateType error");
55         return;
56     }
57 
58     sptr<IRemoteObject> remote = Remote();
59     if (remote == nullptr) {
60         HILOG_ERROR("fail to send transact cmd due to remote object");
61         return;
62     }
63 
64     error = remote->SendRequest(
65         static_cast<uint32_t>(AccessibilityInterfaceCode::ON_STATE_CHANGED),
66         data, reply, option);
67     if (error != NO_ERROR) {
68         HILOG_ERROR("OnStateChanged fail, error: %{public}d", error);
69         return;
70     }
71 }
72 } // namespace Accessibility
73 } // namespace OHOS