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 "input_control_channel_stub.h"
17 
18 #include "ipc_skeleton.h"
19 #include "message_handler.h"
20 #include "message_parcel.h"
21 #include "os_account_adapter.h"
22 
23 namespace OHOS {
24 namespace MiscServices {
InputControlChannelStub(int32_t userId)25 InputControlChannelStub::InputControlChannelStub(int32_t userId)
26 {
27     userId_ = userId;
28 }
29 
~InputControlChannelStub()30 InputControlChannelStub::~InputControlChannelStub()
31 {
32 }
33 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t InputControlChannelStub::OnRemoteRequest(
35     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
36 {
37     IMSA_HILOGD("InputControlChannelStub, code = %{public}u, callingPid: %{public}d, callingUid: %{public}d.", code,
38         IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
39     auto descriptorToken = data.ReadInterfaceToken();
40     if (descriptorToken != GetDescriptor()) {
41         return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
42     }
43     switch (code) {
44         case HIDE_KEYBOARD_SELF: {
45             reply.WriteInt32(HideKeyboardSelf());
46             break;
47         }
48         default: {
49             return IRemoteStub::OnRemoteRequest(code, data, reply, option);
50         }
51     }
52     return NO_ERROR;
53 }
54 
HideKeyboardSelf()55 int32_t InputControlChannelStub::HideKeyboardSelf()
56 {
57     auto userId = OsAccountAdapter::GetOsAccountLocalIdFromUid(IPCSkeleton::GetCallingUid());
58     if (userId == OsAccountAdapter::INVALID_USER_ID) {
59         return ErrorCode::ERROR_EX_ILLEGAL_STATE;
60     }
61     MessageParcel *parcel = new (std::nothrow) MessageParcel();
62     if (parcel == nullptr) {
63         return ErrorCode::ERROR_NULL_POINTER;
64     }
65     parcel->WriteInt32(userId);
66     Message *msg = new (std::nothrow) Message(MessageID::MSG_ID_HIDE_KEYBOARD_SELF, parcel);
67     if (msg == nullptr) {
68         delete parcel;
69         return ErrorCode::ERROR_NULL_POINTER;
70     }
71     MessageHandler::Instance()->SendMessage(msg);
72     return ErrorCode::NO_ERROR;
73 }
74 } // namespace MiscServices
75 } // namespace OHOS
76