• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2023 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 #ifndef FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H
17 #define FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H
18 
19 #include <thread>
20 
21 #include "calling_window_info.h"
22 #include "concurrent_map.h"
23 #include "context.h"
24 #include "iinput_control_channel.h"
25 #include "iinput_data_channel.h"
26 #include "iinput_method_agent.h"
27 #include "iinput_method_core.h"
28 #include "iinput_method_system_ability.h"
29 #include "input_attribute.h"
30 #include "input_control_channel_proxy.h"
31 #include "input_data_channel_proxy.h"
32 #include "input_method_engine_listener.h"
33 #include "input_method_panel.h"
34 #include "input_method_types.h"
35 #include "input_method_utils.h"
36 #include "iremote_object.h"
37 #include "keyboard_listener.h"
38 #include "key_event_consumer_proxy.h"
39 #include "text_input_client_listener.h"
40 #include "msg_handler_callback_interface.h"
41 #include "private_command_interface.h"
42 #include "system_cmd_channel_proxy.h"
43 #include "inputmethod_message_handler.h"
44 #include "input_data_channel_proxy_wrap.h"
45 #include "ime_mirror_manager.h"
46 
47 namespace OHOS {
48 namespace MiscServices {
49 class InputMethodAbility : public RefBase, public PrivateCommandInterface {
50 public:
51     static InputMethodAbility &GetInstance();
52     int32_t SetCoreAndAgent();
53     int32_t InitConnect();
54     int32_t UnRegisteredProxyIme(UnRegisteredType type);
55     int32_t RegisterProxyIme(uint64_t displayId = DEFAULT_DISPLAY_ID);
56     int32_t UnregisterProxyIme(uint64_t displayId);
57     int32_t BindImeMirror();
58     int32_t UnbindImeMirror();
59     int32_t InsertText(const std::string &text, const AsyncIpcCallBack &callback = nullptr);
60     void SetImeListener(std::shared_ptr<InputMethodEngineListener> imeListener);
61     std::shared_ptr<InputMethodEngineListener> GetImeListener();
62     void SetKdListener(std::shared_ptr<KeyboardListener> kdListener);
63     void SetTextInputClientListener(std::shared_ptr<TextInputClientListener> textInputClientListener);
64     int32_t DeleteForward(int32_t length, const AsyncIpcCallBack &callback = nullptr);
65     int32_t DeleteBackward(int32_t length, const AsyncIpcCallBack &callback = nullptr);
66     int32_t HideKeyboardSelf();
67 
68     int32_t SendExtendAction(int32_t action, const AsyncIpcCallBack &callback = nullptr);
69     int32_t GetTextBeforeCursor(int32_t number, std::u16string &text, const AsyncIpcCallBack &callback = nullptr);
70     int32_t GetTextAfterCursor(int32_t number, std::u16string &text, const AsyncIpcCallBack &callback = nullptr);
71     int32_t SendFunctionKey(int32_t funcKey, const AsyncIpcCallBack &callback = nullptr);
72     int32_t MoveCursor(int32_t keyCode, const AsyncIpcCallBack &callback = nullptr);
73     int32_t SelectByRange(int32_t start, int32_t end, const AsyncIpcCallBack &callback = nullptr);
74     int32_t SelectByMovement(int32_t direction, const AsyncIpcCallBack &callback = nullptr);
75     int32_t DispatchKeyEvent(
76         const std::shared_ptr<MMI::KeyEvent> &keyEvent, uint64_t cbId, const sptr<IRemoteObject> &channelObject);
77     void SetCallingWindow(uint32_t windowId);
78     int32_t GetEnterKeyType(int32_t &keyType);
79     int32_t GetInputPattern(int32_t &inputPattern);
80     int32_t GetTextIndexAtCursor(int32_t &index, const AsyncIpcCallBack &callback = nullptr);
81     int32_t GetTextConfig(TextTotalConfig &textConfig);
82     int32_t AdjustKeyboard();
83     int32_t CreatePanel(const std::shared_ptr<AbilityRuntime::Context> &context, const PanelInfo &panelInfo,
84         std::shared_ptr<InputMethodPanel> &inputMethodPanel);
85     int32_t DestroyPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel);
86     int32_t ShowPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel);
87     int32_t HidePanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel);
88     bool IsCurrentIme();
89     bool IsEnable();
90     bool IsCallbackRegistered(const std::string &type);
91     bool IsSystemApp();
92     InputType GetInputType();
93     int32_t ExitCurrentInputType();
94     int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown);
95     int32_t GetSecurityMode(int32_t &security);
96     int32_t OnSecurityChange(int32_t security);
97     int32_t OnConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent);
98     void OnClientInactive(const sptr<IRemoteObject> &channel);
99     void NotifyKeyboardHeight(uint32_t panelHeight, PanelFlag panelFlag);
100     int32_t SendPrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override;
101     int32_t ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override;
102     bool IsDefaultIme();
103     int32_t GetCallingWindowInfo(CallingWindowInfo &windowInfo);
104     int32_t SetPreviewText(const std::string &text, const Range &range, const AsyncIpcCallBack &callback = nullptr);
105     int32_t FinishTextPreview(const AsyncIpcCallBack &callback = nullptr);
106     int32_t NotifyPanelStatus(bool isUseParameterFlag = false, PanelFlag panelFlag = FLG_FIXED);
107     InputAttribute GetInputAttribute();
108     void OnSetInputType(InputType inputType);
109     int32_t SendMessage(const ArrayBuffer &arrayBuffer);
110     int32_t RecvMessage(const ArrayBuffer &arrayBuffer);
111     int32_t RegisterMsgHandler(const std::shared_ptr<MsgHandlerCallbackInterface> &msgHandler = nullptr);
112     int32_t OnCallingDisplayIdChanged(uint64_t displayId);
113     int32_t OnSendPrivateData(const std::unordered_map<std::string, PrivateDataValue> &privateCommand);
114     bool HandleUnconsumedKey(const std::shared_ptr<MMI::KeyEvent> &keyEvent);
115     int32_t OnResponse(uint64_t msgId, int32_t code, const ResponseData &data);
116     int32_t IsCapacitySupport(int32_t capacity, bool &isSupport);
117     AttachOptions GetAttachOptions();
118     int32_t HandleKeyEventResult(uint64_t cbId, bool consumeResult, const sptr<IRemoteObject> &channelObject);
119 
120 public:
121     /* called from TaskManager worker thread */
122     int32_t StartInput(const InputClientInfo &clientInfo, bool isBindFromClient);
123     int32_t StopInput(sptr<IRemoteObject> channelObj, uint32_t sessionId);
124     int32_t ShowKeyboard(int32_t requestKeyboardReason);
125     int32_t HideKeyboard();
126     int32_t OnDiscardTypingText();
127     int32_t OnNotifyPreemption();
128 
129     void OnInitInputControlChannel(sptr<IRemoteObject> channelObj);
130     void OnSetSubtype(SubProperty subProperty);
131     void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height);
132     void OnSelectionChange(std::u16string text, int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEndg);
133     void OnAttributeChange(InputAttribute attribute);
134     void OnFunctionKey(int32_t funcKey);
135 
136     int32_t OnStopInputService(bool isTerminateIme);
137 private:
138     std::mutex controlChannelLock_;
139     std::shared_ptr<InputControlChannelProxy> controlChannel_ = nullptr;
140 
141     std::mutex dataChannelLock_;
142     sptr<IRemoteObject> dataChannelObject_ = nullptr;
143     std::shared_ptr<InputDataChannelProxyWrap> dataChannelProxyWrap_ = nullptr;
144 
145     std::mutex systemCmdChannelLock_;
146     sptr<SystemCmdChannelProxy> systemCmdChannelProxy_ = nullptr;
147 
148     std::shared_ptr<InputMethodEngineListener> imeListener_;
149     std::shared_ptr<KeyboardListener> kdListener_;
150     std::shared_ptr<TextInputClientListener> textInputClientListener_;
151 
152     std::mutex abilityLock_;
153     sptr<IInputMethodSystemAbility> abilityManager_ { nullptr };
154     sptr<InputDeathRecipient> deathRecipient_ { nullptr };
155     InputMethodAbility();
156     ~InputMethodAbility();
157     InputMethodAbility(const InputMethodAbility &) = delete;
158     InputMethodAbility &operator=(const InputMethodAbility &) = delete;
159     sptr<IInputMethodSystemAbility> GetImsaProxy();
160     void OnRemoteSaDied(const wptr<IRemoteObject> &object);
161 
162     sptr<SystemCmdChannelProxy> GetSystemCmdChannelProxy();
163     void ClearSystemCmdChannel();
164 
165     void SetInputDataChannel(const sptr<IRemoteObject> &object);
166     std::shared_ptr<InputDataChannelProxyWrap> GetInputDataChannelProxyWrap();
167     std::shared_ptr<InputDataChannelProxy> GetInputDataChannelProxy();
168     void ClearDataChannel(const sptr<IRemoteObject> &channel);
169     void SetInputControlChannel(sptr<IRemoteObject> &object);
170     void ClearInputControlChannel();
171     std::shared_ptr<InputControlChannelProxy> GetInputControlChannel();
172 
173     void Initialize();
174     int32_t InvokeStartInputCallbackWithInfoRestruct(const TextTotalConfig &textConfig, bool isNotifyInputStart);
175     int32_t InvokeStartInputCallback(const TextTotalConfig &textConfig, bool isNotifyInputStart);
176     void HandleRequestKeyboardReasonChanged(const RequestKeyboardReason &requestKeyboardReason);
177     void InvokeAttachOptionsCallback(const AttachOptions &options, bool isFirstNotify = false);
178     void SetAttachOptions(const AttachOptions &options);
179     void ClearAttachOptions();
180     int32_t HideKeyboard(Trigger trigger, uint32_t sessionId);
181     std::shared_ptr<InputMethodPanel> GetSoftKeyboardPanel();
182     /* param flag: ShowPanel is async, show/hide softkeyboard in alphabet keyboard attached,
183        flag will be changed before finishing show/hide */
184     int32_t ShowPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel, PanelFlag flag, Trigger trigger);
185     int32_t HidePanel(
186         const std::shared_ptr<InputMethodPanel> &inputMethodPanel, PanelFlag flag, Trigger trigger, uint32_t sessionId);
187     void SetInputAttribute(const InputAttribute &inputAttribute);
188     void ClearInputAttribute();
189     void NotifyPanelStatusInfo(const PanelStatusInfo &info);
190     int32_t HideKeyboardImplWithoutLock(int32_t cmdId, uint32_t sessionId);
191     int32_t ShowKeyboardImplWithLock(int32_t cmdId);
192     int32_t ShowKeyboardImplWithoutLock(int32_t cmdId);
193     void NotifyPanelStatusInfo(const PanelStatusInfo &info, std::shared_ptr<InputDataChannelProxy> &channelProxy);
194     void ClearInputType();
195     std::shared_ptr<MsgHandlerCallbackInterface> GetMsgHandlerCallback();
196     int32_t StartInputInner(const InputClientInfo &clientInfo, bool isBindFromClient);
197     int32_t InsertTextInner(const std::string &text, const AsyncIpcCallBack &callback = nullptr);
198     int32_t SetPreviewTextInner(
199         const std::string &text, const Range &range, const AsyncIpcCallBack &callback = nullptr);
200     int32_t DeleteForwardInner(int32_t length, const AsyncIpcCallBack &callback = nullptr);
201     int32_t DeleteBackwardInner(int32_t length, const AsyncIpcCallBack &callback = nullptr);
202     int32_t FinishTextPreviewInner(const AsyncIpcCallBack &callback = nullptr);
203     int32_t GetTextBeforeCursorInner(int32_t number, std::u16string &text, const AsyncIpcCallBack &callback = nullptr);
204     int32_t GetTextAfterCursorInner(int32_t number, std::u16string &text, const AsyncIpcCallBack &callback = nullptr);
205     int32_t GetTextIndexAtCursorInner(int32_t &index, const AsyncIpcCallBack &callback = nullptr);
206     bool NotifyInfoToWmsInStartInput(const TextTotalConfig &textConfig);
207     void SetBindClientInfo(const InputClientInfo &clientInfo);
208     HiSysEventClientInfo GetBindClientInfo();
209     void ClearBindClientInfo();
210     void ReportImeStartInput(int32_t eventCode, int32_t errCode, bool isShowKeyboard, int64_t consumeTime = -1);
211     void ReportBaseTextOperation(int32_t eventCode, int32_t errCode, int64_t consumeTime);
212     void ClearBindInfo(const sptr<IRemoteObject> &channel);
213 
214     ConcurrentMap<PanelType, std::shared_ptr<InputMethodPanel>> panels_ {};
215     std::atomic_bool isBound_ { false };
216     std::atomic_bool isProxyIme_{ false };
217 
218     sptr<IInputMethodCore> coreStub_ { nullptr };
219     sptr<IInputMethodAgent> agentStub_ { nullptr };
220     sptr<IInputMethodAgent> systemAgentStub_ { nullptr };
221 
222     double positionY_ = 0;
223     double height_ = 0;
224 
225     std::mutex imeCheckMutex_;
226     bool isCurrentIme_ = false;
227 
228     std::mutex defaultImeCheckMutex_;
229     bool isDefaultIme_ = false;
230     std::mutex inputAttrLock_;
231     InputAttribute inputAttribute_ {};
232     std::recursive_mutex keyboardCmdLock_;
233     int32_t cmdId_ = 0;
234     std::mutex attachOptionsLock_;
235     AttachOptions attachOptions_;  // isShowKeyboard not be maintained at present
236 
237     std::mutex inputTypeLock_;
238     InputType inputType_ = InputType::NONE;
239     std::atomic<bool> isImeTerminating_ = false;
240     std::atomic_bool isShowAfterCreate_ { false };
241     std::atomic<int32_t> securityMode_ = -1;
242     std::mutex msgHandlerMutex_;
243     std::shared_ptr<MsgHandlerCallbackInterface> msgHandler_;
244 
245     std::mutex systemAppCheckMutex_;
246     bool isSystemApp_ = false;
247 
248     std::mutex bindClientInfoLock_;
249     HiSysEventClientInfo bindClientInfo_;
250     bool isInputStartNotified_ = false;
251     ImeMirrorManager imeMirrorMgr_;
252 
253     bool IsDisplayChanged(uint64_t oldDisplayId, uint64_t newDisplayId);
254 };
255 } // namespace MiscServices
256 } // namespace OHOS
257 #endif // FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H
258