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_method_agent_stub.h"
17
18 #include "global.h"
19 #include "input_method_ability.h"
20 #include "ipc_skeleton.h"
21 #include "message.h"
22 #include "message_handler.h"
23
24 namespace OHOS {
25 namespace MiscServices {
26 using namespace MessageID;
27
InputMethodAgentStub()28 InputMethodAgentStub::InputMethodAgentStub()
29 {
30 msgHandler_ = nullptr;
31 }
32
~InputMethodAgentStub()33 InputMethodAgentStub::~InputMethodAgentStub()
34 {
35 }
36
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int32_t InputMethodAgentStub::OnRemoteRequest(
38 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
39 {
40 IMSA_HILOGI("InputMethodAgentStub, code = %{public}u, callingPid:%{public}d, callingUid:%{public}d", code,
41 IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
42 auto descriptorToken = data.ReadInterfaceToken();
43 if (descriptorToken != GetDescriptor()) {
44 return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
45 }
46
47 switch (code) {
48 case DISPATCH_KEY_EVENT: {
49 return DispatchKeyEventOnRemote(data, reply);
50 }
51 case SET_CALLING_WINDOW_ID: {
52 SetCallingWindow(data.ReadUint32());
53 break;
54 }
55 case ON_CURSOR_UPDATE: {
56 int32_t positionX = data.ReadInt32();
57 int32_t positionY = data.ReadInt32();
58 int32_t height = data.ReadInt32();
59 OnCursorUpdate(positionX, positionY, height);
60 reply.WriteNoException();
61 return ErrorCode::NO_ERROR;
62 }
63 case ON_SELECTION_CHANGE: {
64 std::u16string text = data.ReadString16();
65 int32_t oldBegin = data.ReadInt32();
66 int32_t oldEnd = data.ReadInt32();
67 int32_t newBegin = data.ReadInt32();
68 int32_t newEnd = data.ReadInt32();
69 OnSelectionChange(text, oldBegin, oldEnd, newBegin, newEnd);
70 reply.WriteNoException();
71 return ErrorCode::NO_ERROR;
72 }
73 case ON_CONFIGURATION_CHANGE: {
74 Configuration configuration;
75 configuration.SetEnterKeyType(EnterKeyType(data.ReadInt32()));
76 configuration.SetTextInputType(TextInputType(data.ReadInt32()));
77 OnConfigurationChange(configuration);
78 reply.WriteNoException();
79 return ErrorCode::NO_ERROR;
80 }
81 default: {
82 return IRemoteStub::OnRemoteRequest(code, data, reply, option);
83 }
84 }
85 return ErrorCode::NO_ERROR;
86 }
87
DispatchKeyEventOnRemote(MessageParcel & data,MessageParcel & reply)88 int32_t InputMethodAgentStub::DispatchKeyEventOnRemote(MessageParcel &data, MessageParcel &reply)
89 {
90 IMSA_HILOGD("run in");
91 std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
92 if (!keyEvent->ReadFromParcel(data)) {
93 IMSA_HILOGE("failed to read key event from parcel");
94 return ErrorCode::ERROR_EX_PARCELABLE;
95 }
96 bool isConsumed = InputMethodAbility::GetInstance()->DispatchKeyEvent(keyEvent);
97 return reply.WriteBool(isConsumed) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
98 }
99
DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)100 bool InputMethodAgentStub::DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent)
101 {
102 return false;
103 }
104
SetCallingWindow(uint32_t windowId)105 void InputMethodAgentStub::SetCallingWindow(uint32_t windowId)
106 {
107 IMSA_HILOGI("InputMethodAgentStub::SetCallingWindow");
108 InputMethodAbility::GetInstance()->SetCallingWindow(windowId);
109 }
110
OnCursorUpdate(int32_t positionX,int32_t positionY,int height)111 void InputMethodAgentStub::OnCursorUpdate(int32_t positionX, int32_t positionY, int height)
112 {
113 IMSA_HILOGI("InputMethodAgentStub::OnCursorUpdate");
114 if (msgHandler_ == nullptr) {
115 return;
116 }
117 MessageParcel *data = new MessageParcel();
118 data->WriteInt32(positionX);
119 data->WriteInt32(positionY);
120 data->WriteInt32(height);
121 Message *message = new Message(MessageID::MSG_ID_ON_CURSOR_UPDATE, data);
122 msgHandler_->SendMessage(message);
123 }
124
OnSelectionChange(std::u16string text,int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)125 void InputMethodAgentStub::OnSelectionChange(
126 std::u16string text, int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
127 {
128 IMSA_HILOGI("InputMethodAgentStub::OnSelectionChange");
129 if (msgHandler_ == nullptr) {
130 return;
131 }
132 MessageParcel *data = new MessageParcel();
133 data->WriteString16(text);
134 data->WriteInt32(oldBegin);
135 data->WriteInt32(oldEnd);
136 data->WriteInt32(newBegin);
137 data->WriteInt32(newEnd);
138 Message *message = new Message(MessageID::MSG_ID_ON_SELECTION_CHANGE, data);
139 msgHandler_->SendMessage(message);
140 }
141
OnConfigurationChange(const Configuration & config)142 void InputMethodAgentStub::OnConfigurationChange(const Configuration &config)
143 {
144 IMSA_HILOGI("InputMethodAgentStub in.");
145 if (msgHandler_ == nullptr) {
146 return;
147 }
148 MessageParcel *data = new MessageParcel();
149 data->WriteInt32(static_cast<int32_t>(config.GetEnterKeyType()));
150 data->WriteInt32(static_cast<int32_t>(config.GetTextInputType()));
151 Message *message = new Message(MessageID::MSG_ID_ON_CONFIGURATION_CHANGE, data);
152 msgHandler_->SendMessage(message);
153 }
154
SetMessageHandler(MessageHandler * msgHandler)155 void InputMethodAgentStub::SetMessageHandler(MessageHandler *msgHandler)
156 {
157 msgHandler_ = msgHandler;
158 }
159 } // namespace MiscServices
160 } // namespace OHOS
161