1 /*
2 * Copyright (C) 2021 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 "keyevent_consumer_proxy.h"
17
18 #include "global.h"
19 #include "ipc_types.h"
20 #include "itypes_util.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23
24 namespace OHOS {
25 namespace MiscServices {
KeyEventConsumerProxy(const sptr<IRemoteObject> & object)26 KeyEventConsumerProxy::KeyEventConsumerProxy(const sptr<IRemoteObject> &object)
27 : IRemoteProxy<IKeyEventConsumer>(object)
28 {
29 }
30
OnKeyEventResult(bool isConsumed)31 int32_t KeyEventConsumerProxy::OnKeyEventResult(bool isConsumed)
32 {
33 return SendRequest(
34 KEY_EVENT_RESULT,
35 [isConsumed](MessageParcel &parcel) {
36 return ITypesUtil::Marshal(parcel, isConsumed);
37 },
38 nullptr, MessageOption::TF_ASYNC);
39 }
40
OnKeyEventConsumeResult(bool isConsumed)41 void KeyEventConsumerProxy::OnKeyEventConsumeResult(bool isConsumed)
42 {
43 IMSA_HILOGI("result: %{public}d.", isConsumed);
44 keyEventConsume_ = true;
45 keyEventResult_ = isConsumed;
46 if (keyEventConsume_ && keyCodeConsume_) {
47 OnKeyEventResult(keyCodeResult_ || keyEventResult_);
48 }
49 }
50
OnKeyCodeConsumeResult(bool isConsumed)51 void KeyEventConsumerProxy::OnKeyCodeConsumeResult(bool isConsumed)
52 {
53 IMSA_HILOGI("result: %{public}d.", isConsumed);
54 keyCodeConsume_ = true;
55 keyCodeResult_ = isConsumed;
56 if (keyEventConsume_ && keyCodeConsume_) {
57 OnKeyEventResult(keyCodeResult_ || keyEventResult_);
58 }
59 }
60
SendRequest(int code,ParcelHandler input,ParcelHandler output,MessageOption option)61 int32_t KeyEventConsumerProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output, MessageOption option)
62 {
63 IMSA_HILOGD("KeyEventConsumerProxy run in, code = %{public}d.", code);
64 MessageParcel data;
65 MessageParcel reply;
66
67 if (!data.WriteInterfaceToken(GetDescriptor())) {
68 IMSA_HILOGE("write interface token failed!");
69 return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
70 }
71 if (input != nullptr && (!input(data))) {
72 IMSA_HILOGE("write data failed!");
73 return ErrorCode::ERROR_EX_PARCELABLE;
74 }
75 auto remote = Remote();
76 if (remote == nullptr) {
77 IMSA_HILOGE("KeyEventConsumerProxy remote is nullptr!");
78 return ErrorCode::ERROR_EX_NULL_POINTER;
79 }
80 auto ret = remote->SendRequest(code, data, reply, option);
81 if (ret != NO_ERROR) {
82 IMSA_HILOGE("send request failed, code: %{public}d ret %{public}d!", code, ret);
83 return ret;
84 }
85 if (option.GetFlags() == MessageOption::TF_ASYNC) {
86 return ErrorCode::NO_ERROR;
87 }
88 ret = reply.ReadInt32();
89 if (ret != NO_ERROR) {
90 IMSA_HILOGE("reply error, ret: %{public}d", ret);
91 return ret;
92 }
93 if (output != nullptr && (!output(reply))) {
94 IMSA_HILOGE("reply parcel error!");
95 return ErrorCode::ERROR_EX_PARCELABLE;
96 }
97 return ret;
98 }
99 } // namespace MiscServices
100 } // namespace OHOS