• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     IMSA_HILOGI("run in");
34     return SendRequest(
35         KEY_EVENT_RESULT, [isConsumed](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, isConsumed); });
36 }
37 
OnKeyEventConsumeResult(bool isConsumed)38 void KeyEventConsumerProxy::OnKeyEventConsumeResult(bool isConsumed)
39 {
40     IMSA_HILOGI("result: %{public}d", isConsumed);
41     keyEventConsume_ = true;
42     keyEventResult_ = isConsumed;
43     if (keyEventConsume_ && keyCodeConsume_) {
44         OnKeyEventResult(keyCodeResult_ || keyEventResult_);
45     }
46 }
47 
OnKeyCodeConsumeResult(bool isConsumed)48 void KeyEventConsumerProxy::OnKeyCodeConsumeResult(bool isConsumed)
49 {
50     IMSA_HILOGI("result: %{public}d", isConsumed);
51     keyCodeConsume_ = true;
52     keyCodeResult_ = isConsumed;
53     if (keyEventConsume_ && keyCodeConsume_) {
54         OnKeyEventResult(keyCodeResult_ || keyEventResult_);
55     }
56 }
57 
SendRequest(int code,ParcelHandler input,ParcelHandler output)58 int32_t KeyEventConsumerProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
59 {
60     IMSA_HILOGD("KeyEventConsumerProxy run in, code = %{public}d", code);
61     MessageParcel data;
62     MessageParcel reply;
63     MessageOption option = MessageOption::TF_SYNC;
64 
65     if (!data.WriteInterfaceToken(GetDescriptor())) {
66         IMSA_HILOGE("write interface token failed");
67         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
68     }
69     if (input != nullptr && (!input(data))) {
70         IMSA_HILOGE("write data failed");
71         return ErrorCode::ERROR_EX_PARCELABLE;
72     }
73     auto ret = Remote()->SendRequest(code, data, reply, option);
74     if (ret != NO_ERROR) {
75         IMSA_HILOGE("KeyEventConsumerProxy send request failed, code: %{public}d ret %{public}d", code, ret);
76         return ret;
77     }
78     if (option.GetFlags() == MessageOption::TF_ASYNC) {
79         return ErrorCode::NO_ERROR;
80     }
81     ret = reply.ReadInt32();
82     if (ret != NO_ERROR) {
83         IMSA_HILOGE("reply error, ret %{public}d", ret);
84         return ret;
85     }
86     if (output != nullptr && (!output(reply))) {
87         IMSA_HILOGE("reply parcel error");
88         return ErrorCode::ERROR_EX_PARCELABLE;
89     }
90     return ret;
91 }
92 } // namespace MiscServices
93 } // namespace OHOS
94