• 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 #include "global.h"
18 #include "message_handler.h"
19 #include "message.h"
20 #include "input_method_ability.h"
21 
22 namespace OHOS {
23 namespace MiscServices {
24     using namespace MessageID;
25 
InputMethodAgentStub()26     InputMethodAgentStub::InputMethodAgentStub()
27     {
28         msgHandler_ = nullptr;
29     }
30 
~InputMethodAgentStub()31     InputMethodAgentStub::~InputMethodAgentStub()
32     {
33     }
34 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35     int32_t InputMethodAgentStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
36                                                   MessageParcel &reply, MessageOption &option)
37     {
38         IMSA_HILOGI("InputMethodAgentStub::OnRemoteRequest code = %{public}d", code);
39         auto descriptorToken = data.ReadInterfaceToken();
40         if (descriptorToken != GetDescriptor()) {
41             return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
42         }
43 
44         switch (code) {
45             case DISPATCH_KEY_EVENT: {
46                 MessageParcel *msgParcel = (MessageParcel*) &data;
47                 reply.WriteBool(DispatchKeyEvent(*msgParcel));
48                 break;
49             }
50             case SET_CALLING_WINDOW_ID: {
51                 uint32_t windowId = data.ReadUint32();
52                 SetCallingWindow(windowId);
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             default: {
74                 return IRemoteStub::OnRemoteRequest(code, data, reply, option);
75             }
76         }
77         return ErrorCode::NO_ERROR;
78     }
79 
DispatchKeyEvent(MessageParcel & data)80     bool InputMethodAgentStub::DispatchKeyEvent(MessageParcel& data)
81     {
82         IMSA_HILOGI("InputMethodAgentStub::DispatchKeyEvent");
83         if (!msgHandler_) {
84             return false;
85         }
86         return InputMethodAbility::GetInstance()->DispatchKeyEvent(data.ReadInt32(), data.ReadInt32());
87     }
88 
SetCallingWindow(uint32_t windowId)89     void InputMethodAgentStub::SetCallingWindow(uint32_t windowId)
90     {
91         IMSA_HILOGI("InputMethodAgentStub::SetCallingWindow");
92         if (!msgHandler_) {
93             return;
94         }
95         InputMethodAbility::GetInstance()->SetCallingWindow(windowId);
96         return;
97     }
98 
OnCursorUpdate(int32_t positionX,int32_t positionY,int height)99     void InputMethodAgentStub::OnCursorUpdate(int32_t positionX, int32_t positionY, int height)
100     {
101         IMSA_HILOGI("InputMethodAgentStub::OnCursorUpdate");
102         if (!msgHandler_) {
103             return;
104         }
105         MessageParcel *data = new MessageParcel();
106         data->WriteInt32(positionX);
107         data->WriteInt32(positionY);
108         data->WriteInt32(height);
109         Message *message = new Message(MessageID::MSG_ID_ON_CURSOR_UPDATE, data);
110         msgHandler_->SendMessage(message);
111     }
112 
OnSelectionChange(std::u16string text,int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)113     void InputMethodAgentStub::OnSelectionChange(std::u16string text, int32_t oldBegin, int32_t oldEnd,
114                                                  int32_t newBegin, int32_t newEnd)
115     {
116         IMSA_HILOGI("InputMethodAgentStub::OnSelectionChange");
117         if (!msgHandler_) {
118             return;
119         }
120         MessageParcel *data = new MessageParcel();
121         data->WriteString16(text);
122         data->WriteInt32(oldBegin);
123         data->WriteInt32(oldEnd);
124         data->WriteInt32(newBegin);
125         data->WriteInt32(newEnd);
126         Message *message = new Message(MessageID::MSG_ID_ON_SELECTION_CHANGE, data);
127         msgHandler_->SendMessage(message);
128     }
129 
SetMessageHandler(MessageHandler * msgHandler)130     void InputMethodAgentStub::SetMessageHandler(MessageHandler *msgHandler)
131     {
132         msgHandler_ = msgHandler;
133     }
134 } // namespace MiscServices
135 } // namespace OHOS
136