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 "i_input_control_channel.h"
25  #include "i_input_data_channel.h"
26  #include "i_input_method_agent.h"
27  #include "i_input_method_core.h"
28  #include "i_input_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 "iremote_object.h"
35  #include "keyboard_listener.h"
36  #include "keyevent_consumer_proxy.h"
37  #include "message.h"
38  #include "message_handler.h"
39  #include "private_command_interface.h"
40  #include "system_cmd_channel_proxy.h"
41  #include "input_method_types.h"
42  
43  namespace OHOS {
44  namespace MiscServices {
45  class MessageHandler;
46  class InputMethodAbility : public RefBase, public PrivateCommandInterface {
47  public:
48      InputMethodAbility();
49      ~InputMethodAbility();
50      static sptr<InputMethodAbility> GetInstance();
51      int32_t SetCoreAndAgent();
52      void SetCoreAndAgentAsync();
53      int32_t InitConnect();
54      int32_t UnRegisteredProxyIme(UnRegisteredType type);
55      int32_t InsertText(const std::string text);
56      void SetImeListener(std::shared_ptr<InputMethodEngineListener> imeListener);
57      std::shared_ptr<InputMethodEngineListener> GetImeListener();
58      void SetKdListener(std::shared_ptr<KeyboardListener> kdListener);
59      int32_t DeleteForward(int32_t length);
60      int32_t DeleteBackward(int32_t length);
61      int32_t HideKeyboardSelf();
62      int32_t StartInput(const InputClientInfo &clientInfo, bool isBindFromClient);
63      int32_t StopInput(const sptr<IRemoteObject> &channelObject);
64      int32_t ShowKeyboard();
65      int32_t HideKeyboard();
66      int32_t SendExtendAction(int32_t action);
67      int32_t GetTextBeforeCursor(int32_t number, std::u16string &text);
68      int32_t GetTextAfterCursor(int32_t number, std::u16string &text);
69      int32_t SendFunctionKey(int32_t funcKey);
70      int32_t MoveCursor(int32_t keyCode);
71      int32_t SelectByRange(int32_t start, int32_t end);
72      int32_t SelectByMovement(int32_t direction);
73      int32_t DispatchKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer);
74      void SetCallingWindow(uint32_t windowId);
75      int32_t GetEnterKeyType(int32_t &keyType);
76      int32_t GetInputPattern(int32_t &inputPattern);
77      int32_t GetTextIndexAtCursor(int32_t &index);
78      int32_t GetTextConfig(TextTotalConfig &textConfig);
79      int32_t CreatePanel(const std::shared_ptr<AbilityRuntime::Context> &context, const PanelInfo &panelInfo,
80          std::shared_ptr<InputMethodPanel> &inputMethodPanel);
81      int32_t DestroyPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel);
82      int32_t ShowPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel);
83      int32_t HidePanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel);
84      bool IsCurrentIme();
85      bool IsEnable();
86      int32_t ExitCurrentInputType();
87      int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown);
88      int32_t GetSecurityMode(int32_t &security);
89      int32_t OnSecurityChange(int32_t security);
90      int32_t OnConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent);
91      void OnClientInactive(const sptr<IRemoteObject> &channel);
92      void NotifyKeyboardHeight(uint32_t panelHeight, PanelFlag panelFlag);
93      int32_t SendPrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override;
94      int32_t ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override;
95      bool IsDefaultIme();
96      int32_t GetCallingWindowInfo(CallingWindowInfo &windowInfo);
97      int32_t SetPreviewText(const std::string &text, const Range &range);
98      int32_t FinishTextPreview(bool isAsync);
99      int32_t NotifyPanelStatus(const std::shared_ptr<InputMethodPanel> &inputMethodPanel,
100          SysPanelStatus &sysPanelStatus);
101      InputAttribute GetInputAttribute();
102      int32_t OnStopInputService(bool isTerminateIme);
103  
104  private:
105      std::thread workThreadHandler;
106      MessageHandler *msgHandler_;
107      bool stop_ = false;
108  
109      std::mutex controlChannelLock_;
110      std::shared_ptr<InputControlChannelProxy> controlChannel_ = nullptr;
111  
112      std::mutex dataChannelLock_;
113      sptr<IRemoteObject> dataChannelObject_ = nullptr;
114      std::shared_ptr<InputDataChannelProxy> dataChannelProxy_ = nullptr;
115  
116      std::mutex systemCmdChannelLock_;
117      sptr<SystemCmdChannelProxy> systemCmdChannelProxy_ = nullptr;
118  
119      std::shared_ptr<InputMethodEngineListener> imeListener_;
120      std::shared_ptr<KeyboardListener> kdListener_;
121  
122      static std::mutex instanceLock_;
123      static sptr<InputMethodAbility> instance_;
124      std::mutex abilityLock_;
125      sptr<IInputMethodSystemAbility> abilityManager_{ nullptr };
126      sptr<InputDeathRecipient> deathRecipient_{ nullptr };
127      sptr<IInputMethodSystemAbility> GetImsaProxy();
128      void OnRemoteSaDied(const wptr<IRemoteObject> &object);
129  
130      sptr<SystemCmdChannelProxy> GetSystemCmdChannelProxy();
131      void ClearSystemCmdChannel();
132  
133      void SetInputDataChannel(const sptr<IRemoteObject> &object);
134      std::shared_ptr<InputDataChannelProxy> GetInputDataChannelProxy();
135      void ClearDataChannel(const sptr<IRemoteObject> &channel);
136      void SetInputControlChannel(sptr<IRemoteObject> &object);
137      void ClearInputControlChannel();
138      std::shared_ptr<InputControlChannelProxy> GetInputControlChannel();
139  
140      void Initialize();
141      void WorkThread();
142      void QuitWorkThread();
143  
144      void OnInitInputControlChannel(Message *msg);
145      void OnSetSubtype(Message *msg);
146      int32_t InvokeStartInputCallback(bool isNotifyInputStart);
147      int32_t InvokeStartInputCallback(const TextTotalConfig &textConfig, bool isNotifyInputStart);
148      void OnCursorUpdate(Message *msg);
149      void OnSelectionChange(Message *msg);
150      void OnAttributeChange(Message *msg);
151  
152      int32_t HideKeyboard(Trigger trigger);
153      std::shared_ptr<InputMethodPanel> GetSoftKeyboardPanel();
154      /* param flag: ShowPanel is async, show/hide softkeyboard in alphabet keyboard attached,
155         flag will be changed before finishing show/hide */
156      int32_t ShowPanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel, PanelFlag flag, Trigger trigger);
157      int32_t HidePanel(const std::shared_ptr<InputMethodPanel> &inputMethodPanel, PanelFlag flag, Trigger trigger);
158      void SetInputAttribute(const InputAttribute &inputAttribute);
159      void ClearInputAttribute();
160      void NotifyPanelStatusInfo(const PanelStatusInfo &info);
161      int32_t HideKeyboardImplWithoutLock(int32_t cmdId);
162      int32_t ShowKeyboardImplWithLock(int32_t cmdId);
163      int32_t ShowKeyboardImplWithoutLock(int32_t cmdId);
164      void NotifyPanelStatusInfo(const PanelStatusInfo &info, std::shared_ptr<InputDataChannelProxy> &channelProxy);
165  
166      ConcurrentMap<PanelType, std::shared_ptr<InputMethodPanel>> panels_{};
167      std::atomic_bool isBound_{ false };
168  
169      sptr<IInputMethodCore> coreStub_{ nullptr };
170      sptr<IInputMethodAgent> agentStub_{ nullptr };
171      sptr<IInputMethodAgent> systemAgentStub_{ nullptr };
172  
173      bool isPendingShowKeyboard_ = false;
174      double positionY_ = 0;
175      double height_ = 0;
176  
177      std::mutex imeCheckMutex_;
178      bool isCurrentIme_ = false;
179  
180      std::mutex defaultImeCheckMutex_;
181      bool isDefaultIme_ = false;
182      std::mutex inputAttrLock_;
183      InputAttribute inputAttribute_{};
184      std::recursive_mutex keyboardCmdLock_;
185      int32_t cmdId_ = 0;
186      std::atomic<bool> isImeTerminating_ = false;
187  };
188  } // namespace MiscServices
189  } // namespace OHOS
190  #endif // FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H
191