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 #include "input_method_core_stub.h" 16 17 #include <chrono> 18 #include <cstdint> 19 #include "message_handler.h" 20 #include "i_input_data_channel.h" 21 #include "input_channel.h" 22 #include "platform.h" 23 #include "message_parcel.h" 24 #include "input_control_channel_proxy.h" 25 #include "input_method_ability.h" 26 #include <string_ex.h> 27 28 namespace OHOS { 29 namespace MiscServices { 30 using namespace MessageID; 31 /** 32 * param userId the id of the user to whom the object is linking 33 * @param userId 34 */ InputMethodCoreStub(int userId)35 InputMethodCoreStub::InputMethodCoreStub(int userId) 36 { 37 userId_ = userId; 38 msgHandler_ = nullptr; 39 } 40 ~InputMethodCoreStub()41 InputMethodCoreStub::~InputMethodCoreStub() 42 { 43 } 44 OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)45 int32_t InputMethodCoreStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, 46 MessageOption& option) 47 { 48 IMSA_HILOGI("InputMethodCoreStub::OnRemoteRequest"); 49 auto descriptorToken = data.ReadInterfaceToken(); 50 if (descriptorToken != GetDescriptor()) { 51 IMSA_HILOGI("InputMethodCoreStub::OnRemoteRequest descriptorToken is invalid"); 52 return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION; 53 } 54 switch (code) { 55 case INITIALIZE_INPUT: { 56 sptr<IRemoteObject> startInputToken = nullptr; 57 int32_t displayId = data.ReadInt32(); 58 IMSA_HILOGI("InputMethodCoreStub::OnRemoteRequest displayId = %{public}d", displayId); 59 sptr<IRemoteObject> channelObject = data.ReadRemoteObject(); 60 if (channelObject == nullptr) { 61 IMSA_HILOGI("InputMethodCoreStub::OnRemoteRequest channelObject is nullptr"); 62 } 63 sptr<IInputControlChannel> inputControlChannel = new InputControlChannelProxy(channelObject); 64 if (inputControlChannel == nullptr) { 65 IMSA_HILOGI("InputMethodCoreStub::OnRemoteRequest inputControlChannel is nullptr"); 66 } 67 68 initializeInput(startInputToken, displayId, inputControlChannel); 69 reply.WriteNoException(); 70 break; 71 } 72 case INIT_INPUT_CONTROL_CHANNEL: { 73 sptr<IRemoteObject> channelObject = data.ReadRemoteObject(); 74 if (channelObject == nullptr) { 75 IMSA_HILOGI("InputMethodCoreStub::OnRemoteRequest channelObject is nullptr"); 76 } 77 sptr<IInputControlChannel> inputControlChannel = new InputControlChannelProxy(channelObject); 78 if (inputControlChannel == nullptr) { 79 IMSA_HILOGI("InputMethodCoreStub::OnRemoteRequest inputControlChannel is nullptr"); 80 } 81 82 InitInputControlChannel(inputControlChannel); 83 reply.WriteNoException(); 84 break; 85 } 86 case START_INPUT: { 87 sptr<IInputDataChannel> inputDataChannel = iface_cast<IInputDataChannel>(data.ReadRemoteObject()); 88 InputAttribute *editorAttribute = data.ReadParcelable<InputAttribute>(); 89 bool supportPhysicalKbd = data.ReadBool(); 90 91 if (inputDataChannel == nullptr) { 92 IMSA_HILOGI("InputMethodCoreStub::OnRemoteRequest START_INPUT inputDataChannel is nulltpr"); 93 } 94 startInput(inputDataChannel, *editorAttribute, supportPhysicalKbd); 95 delete editorAttribute; 96 reply.WriteNoException(); 97 break; 98 } 99 case SET_CLIENT_STATE: { 100 bool state = data.ReadBool(); 101 SetClientState(state); 102 break; 103 } 104 case STOP_INPUT: { 105 stopInput(); 106 reply.WriteNoException(); 107 break; 108 } 109 case SHOW_KEYBOARD: { 110 sptr<IInputDataChannel> inputDataChannel = iface_cast<IInputDataChannel>(data.ReadRemoteObject()); 111 showKeyboard(inputDataChannel); 112 reply.WriteNoException(); 113 break; 114 } 115 case HIDE_KEYBOARD: { 116 int32_t flags = data.ReadInt32(); 117 hideKeyboard(flags); 118 reply.WriteNoException(); 119 break; 120 } 121 case SET_KEYBOARD_TYPE: { 122 KeyboardType *type = data.ReadParcelable<KeyboardType>(); 123 setKeyboardType(*type); 124 delete type; 125 reply.WriteNoException(); 126 break; 127 } 128 case GET_KEYBOARD_WINDOW_HEIGHT: { 129 int32_t retHeight = 0; 130 getKeyboardWindowHeight(retHeight); 131 reply.WriteNoException(); 132 break; 133 } 134 case STOP_INPUT_SERVICE: { 135 std::string imeId = Str16ToStr8(data.ReadString16()); 136 StopInputService(imeId); 137 reply.WriteNoException(); 138 break; 139 } 140 default: { 141 return IRemoteStub::OnRemoteRequest(code, data, reply, option); 142 } 143 } 144 return NO_ERROR; 145 } 146 initializeInput(sptr<IRemoteObject> & startInputToken,int32_t displayId,sptr<IInputControlChannel> & inputControlChannel)147 int32_t InputMethodCoreStub::initializeInput(sptr<IRemoteObject>& startInputToken, int32_t displayId, 148 sptr<IInputControlChannel>& inputControlChannel) 149 { 150 IMSA_HILOGI("InputMethodCoreStub::initializeInput"); 151 if (msgHandler_ == nullptr) { 152 return ErrorCode::ERROR_NULL_POINTER; 153 } 154 155 if (startInputToken == nullptr) { 156 IMSA_HILOGI("InputMethodCoreStub::initializeInput startInputToken is nullptr"); 157 } 158 159 MessageParcel *data = new MessageParcel(); 160 data->WriteInt32(displayId); 161 if (inputControlChannel != nullptr) { 162 IMSA_HILOGI("InputMethodCoreStub::initializeInput. inputControlChannel is not nullptr"); 163 data->WriteRemoteObject(inputControlChannel->AsObject()); 164 } 165 Message *msg = new Message(MessageID::MSG_ID_INITIALIZE_INPUT, data); 166 msgHandler_->SendMessage(msg); 167 return ErrorCode::NO_ERROR; 168 } 169 InitInputControlChannel(sptr<IInputControlChannel> & inputControlChannel)170 int32_t InputMethodCoreStub::InitInputControlChannel(sptr<IInputControlChannel>& inputControlChannel) 171 { 172 IMSA_HILOGI("InputMethodCoreStub::initializeInput"); 173 if (msgHandler_ == nullptr) { 174 return ErrorCode::ERROR_NULL_POINTER; 175 } 176 177 MessageParcel *data = new MessageParcel(); 178 if (inputControlChannel != nullptr) { 179 IMSA_HILOGI("InputMethodCoreStub::initializeInput. inputControlChannel is not nullptr"); 180 data->WriteRemoteObject(inputControlChannel->AsObject()); 181 } 182 Message *msg = new Message(MessageID::MSG_ID_INIT_INPUT_CONTROL_CHANNEL, data); 183 msgHandler_->SendMessage(msg); 184 return ErrorCode::NO_ERROR; 185 } 186 startInput(const sptr<IInputDataChannel> & inputDataChannel,const InputAttribute & editorAttribute,bool supportPhysicalKbd)187 bool InputMethodCoreStub::startInput(const sptr<IInputDataChannel>& inputDataChannel, 188 const InputAttribute& editorAttribute, bool supportPhysicalKbd) 189 { 190 IMSA_HILOGI("InputMethodCoreStub::startInput"); 191 if (msgHandler_ == nullptr) { 192 return ErrorCode::ERROR_NULL_POINTER; 193 } 194 MessageParcel *data = new MessageParcel(); 195 if (inputDataChannel != nullptr) { 196 IMSA_HILOGI("InputMethodCoreStub::startInput inputDataChannel is not nullptr"); 197 data->WriteRemoteObject(inputDataChannel->AsObject()); 198 } 199 data->WriteParcelable(&editorAttribute); 200 data->WriteBool(supportPhysicalKbd); 201 Message *msg = new Message(MessageID::MSG_ID_START_INPUT, data); 202 msgHandler_->SendMessage(msg); 203 return true; 204 } 205 stopInput()206 int32_t InputMethodCoreStub::stopInput() 207 { 208 IMSA_HILOGI("InputMethodCoreStub::stopInput"); 209 if (msgHandler_ == nullptr) { 210 return ErrorCode::ERROR_NULL_POINTER; 211 } 212 MessageParcel *data = new MessageParcel(); 213 Message *msg = new Message(MessageID::MSG_ID_STOP_INPUT, data); 214 msgHandler_->SendMessage(msg); 215 return ErrorCode::NO_ERROR; 216 } 217 SetClientState(bool state)218 void InputMethodCoreStub::SetClientState(bool state) 219 { 220 IMSA_HILOGI("InputMethodCoreStub::SetClientState"); 221 if (msgHandler_ == nullptr) { 222 return; 223 } 224 MessageParcel *data = new MessageParcel(); 225 226 data->WriteBool(state); 227 Message *msg = new Message(MessageID::MSG_ID_SET_CLIENT_STATE, data); 228 msgHandler_->SendMessage(msg); 229 } 230 showKeyboard(const sptr<IInputDataChannel> & inputDataChannel)231 bool InputMethodCoreStub::showKeyboard(const sptr<IInputDataChannel>& inputDataChannel) 232 { 233 IMSA_HILOGI("InputMethodCoreStub::showKeyboard"); 234 if (msgHandler_ == nullptr) { 235 return false; 236 } 237 MessageParcel *data = new MessageParcel(); 238 if (inputDataChannel != nullptr) { 239 IMSA_HILOGI("InputMethodCoreStub::showKeyboard inputDataChannel is not nullptr"); 240 data->WriteRemoteObject(inputDataChannel->AsObject()); 241 } 242 243 Message *msg = new Message(MessageID::MSG_ID_SHOW_KEYBOARD, data); 244 msgHandler_->SendMessage(msg); 245 return true; 246 } 247 hideKeyboard(int32_t flags)248 bool InputMethodCoreStub::hideKeyboard(int32_t flags) 249 { 250 IMSA_HILOGI("InputMethodCoreStub::hideKeyboard"); 251 if (msgHandler_ == nullptr) { 252 return ErrorCode::ERROR_NULL_POINTER; 253 } 254 MessageParcel *data = new MessageParcel(); 255 data->WriteInt32(userId_); 256 data->WriteInt32(flags); 257 258 Message *msg = new Message(MessageID::MSG_ID_HIDE_KEYBOARD, data); 259 msgHandler_->SendMessage(msg); 260 return true; 261 } 262 setKeyboardType(const KeyboardType & type)263 int32_t InputMethodCoreStub::setKeyboardType(const KeyboardType& type) 264 { 265 IMSA_HILOGI("InputMethodCoreStub::setKeyboardType"); 266 if (msgHandler_ == nullptr) { 267 return ErrorCode::ERROR_NULL_POINTER; 268 } 269 MessageParcel *data = new MessageParcel(); 270 data->WriteParcelable(&type); 271 272 Message *msg = new Message(MessageID::MSG_ID_SET_KEYBOARD_TYPE, data); 273 msgHandler_->SendMessage(msg); 274 return ErrorCode::NO_ERROR; 275 } 276 StopInputService(std::string imeId)277 void InputMethodCoreStub::StopInputService(std::string imeId) 278 { 279 IMSA_HILOGI("InputMethodCoreStub::StopInputService"); 280 if (msgHandler_ == nullptr) { 281 return; 282 } 283 MessageParcel *data = new MessageParcel(); 284 data->WriteString16(Str8ToStr16(imeId)); 285 286 Message *msg = new Message(MessageID::MSG_ID_STOP_INPUT_SERVICE, data); 287 msgHandler_->SendMessage(msg); 288 } 289 getKeyboardWindowHeight(int32_t retHeight)290 int32_t InputMethodCoreStub::getKeyboardWindowHeight(int32_t retHeight) 291 { 292 IMSA_HILOGI("InputMethodCoreStub::getKeyboardWindowHeight"); 293 if (msgHandler_ == nullptr) { 294 return ErrorCode::ERROR_NULL_POINTER; 295 } 296 MessageParcel *data = new MessageParcel(); 297 298 Message *msg = new Message(MessageID::MSG_ID_GET_KEYBOARD_WINDOW_HEIGHT, data); 299 msgHandler_->SendMessage(msg); 300 return ErrorCode::NO_ERROR; 301 } 302 SetMessageHandler(MessageHandler * msgHandler)303 void InputMethodCoreStub::SetMessageHandler(MessageHandler *msgHandler) 304 { 305 msgHandler_ = msgHandler; 306 } 307 } 308 }