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