• 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 "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_HILOGD("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_HILOGI("callingPid/Uid: %{public}d/%{public}d", IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
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     auto consumerObject = data.ReadRemoteObject();
97     if (consumerObject == nullptr) {
98         IMSA_HILOGE("consumerObject is nullptr");
99         return ErrorCode::ERROR_EX_PARCELABLE;
100     }
101     sptr<KeyEventConsumerProxy> consumer = new (std::nothrow) KeyEventConsumerProxy(consumerObject);
102     auto ret = InputMethodAbility::GetInstance()->DispatchKeyEvent(keyEvent, consumer);
103     return reply.WriteInt32(ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
104 }
105 
DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,sptr<IKeyEventConsumer> & consumer)106 int32_t InputMethodAgentStub::DispatchKeyEvent(
107     const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<IKeyEventConsumer> &consumer)
108 {
109     return false;
110 }
111 
SetCallingWindow(uint32_t windowId)112 void InputMethodAgentStub::SetCallingWindow(uint32_t windowId)
113 {
114     InputMethodAbility::GetInstance()->SetCallingWindow(windowId);
115 }
116 
OnCursorUpdate(int32_t positionX,int32_t positionY,int height)117 void InputMethodAgentStub::OnCursorUpdate(int32_t positionX, int32_t positionY, int height)
118 {
119     if (msgHandler_ == nullptr) {
120         return;
121     }
122     MessageParcel *data = new MessageParcel();
123     data->WriteInt32(positionX);
124     data->WriteInt32(positionY);
125     data->WriteInt32(height);
126     Message *message = new Message(MessageID::MSG_ID_ON_CURSOR_UPDATE, data);
127     msgHandler_->SendMessage(message);
128 }
129 
OnSelectionChange(std::u16string text,int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)130 void InputMethodAgentStub::OnSelectionChange(
131     std::u16string text, int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
132 {
133     if (msgHandler_ == nullptr) {
134         return;
135     }
136     MessageParcel *data = new MessageParcel();
137     data->WriteString16(text);
138     data->WriteInt32(oldBegin);
139     data->WriteInt32(oldEnd);
140     data->WriteInt32(newBegin);
141     data->WriteInt32(newEnd);
142     Message *message = new Message(MessageID::MSG_ID_ON_SELECTION_CHANGE, data);
143     msgHandler_->SendMessage(message);
144 }
145 
OnConfigurationChange(const Configuration & config)146 void InputMethodAgentStub::OnConfigurationChange(const Configuration &config)
147 {
148     if (msgHandler_ == nullptr) {
149         return;
150     }
151     MessageParcel *data = new MessageParcel();
152     data->WriteInt32(static_cast<int32_t>(config.GetEnterKeyType()));
153     data->WriteInt32(static_cast<int32_t>(config.GetTextInputType()));
154     Message *message = new Message(MessageID::MSG_ID_ON_CONFIGURATION_CHANGE, data);
155     msgHandler_->SendMessage(message);
156 }
157 
SetMessageHandler(MessageHandler * msgHandler)158 void InputMethodAgentStub::SetMessageHandler(MessageHandler *msgHandler)
159 {
160     msgHandler_ = msgHandler;
161 }
162 } // namespace MiscServices
163 } // namespace OHOS
164