• 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 #ifndef FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H
17 #define FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H
18 
19 #include <thread>
20 #include "iremote_object.h"
21 #include "i_input_control_channel.h"
22 #include "i_input_method_core.h"
23 #include "i_input_data_channel.h"
24 #include "i_input_method_agent.h"
25 #include "input_method_core_stub.h"
26 #include "input_control_channel_proxy.h"
27 #include "input_data_channel_proxy.h"
28 #include "input_attribute.h"
29 #include "message_handler.h"
30 #include "input_channel.h"
31 #include "message.h"
32 #include "utils.h"
33 #include "input_method_system_ability_proxy.h"
34 #include "input_method_engine_listener.h"
35 #include "keyboard_listener.h"
36 #include "visibility.h"
37 
38 namespace OHOS {
39 namespace MiscServices {
40 struct InputStartNotifier {
41     bool isNotify{ false };
42     bool isShowKeyboard{};
43     SubProperty subProperty{};
44 };
45     class MessageHandler;
46     class InputMethodAbility : public RefBase {
47     public:
48         InputMethodAbility();
49         ~InputMethodAbility();
50         IMF_API static sptr<InputMethodAbility> GetInstance();
51         IMF_API int32_t InsertText(const std::string text);
52         IMF_API void setImeListener(std::shared_ptr<InputMethodEngineListener> imeListener);
53         IMF_API void setKdListener(std::shared_ptr<KeyboardListener> kdListener);
54         IMF_API int32_t DeleteForward(int32_t length);
55         IMF_API int32_t DeleteBackward(int32_t length);
56         IMF_API int32_t HideKeyboardSelf();
57         IMF_API int32_t GetTextBeforeCursor(int32_t number, std::u16string &text);
58         IMF_API int32_t GetTextAfterCursor(int32_t number, std::u16string &text);
59         IMF_API int32_t SendFunctionKey(int32_t funcKey);
60         IMF_API int32_t MoveCursor(int32_t keyCode);
61         IMF_API bool DispatchKeyEvent(int32_t keyCode, int32_t keyStatus);
62         IMF_API void SetCallingWindow(uint32_t windowId);
63         IMF_API int32_t GetEnterKeyType(int32_t &keyType);
64         IMF_API int32_t GetInputPattern(int32_t &inputPattern);
65         IMF_API void OnImeReady();
66 
67     private:
68         std::thread workThreadHandler;
69         MessageHandler *msgHandler;
70         InputAttribute editorAttribute;
71         InputChannel *writeInputChannel;
72         bool stop_;
73         int32_t KEYBOARD_HIDE = 1;
74         int32_t KEYBOARD_SHOW = 2;
75 
76         std::mutex controlChannelLock_;
77         std::shared_ptr<InputControlChannelProxy> controlChannel_ = nullptr;
78         void SetCoreAndAgent();
79 
80         std::mutex dataChannelLock_;
81         std::shared_ptr<InputDataChannelProxy> dataChannel_ = nullptr;
82         std::shared_ptr<InputMethodEngineListener> imeListener_;
83         std::shared_ptr<KeyboardListener> kdListener_;
84         static std::mutex instanceLock_;
85 
86         static sptr<InputMethodAbility> instance_;
87         sptr<InputMethodSystemAbilityProxy> mImms;
88         struct ServiceDeathRecipient : public IRemoteObject::DeathRecipient {
89             std::shared_ptr<InputMethodEngineListener> listener{ nullptr };
90             void OnRemoteDied(const wptr<IRemoteObject> &object) override;
91             std::string currentIme_;
92         };
93         sptr<ServiceDeathRecipient> deathRecipientPtr_{ nullptr };
94         sptr<InputMethodSystemAbilityProxy> GetImsaProxy();
95 
96         void SetInputDataChannel(sptr<IRemoteObject> &object);
97         std::shared_ptr<InputDataChannelProxy> GetInputDataChannel();
98         void SetInputControlChannel(sptr<IRemoteObject> &object);
99         std::shared_ptr<InputControlChannelProxy> GetInputControlChannel();
100 
101         void Initialize();
102         void WorkThread();
103         void QuitWorkThread();
104 
105         void OnShowKeyboard(Message *msg);
106         void OnHideKeyboard(Message *msg);
107         void OnInitInputControlChannel(Message *msg);
108         void OnSetSubtype(Message *msg);
109 
110         void OnCursorUpdate(Message *msg);
111         void OnSelectionChange(Message *msg);
112         void ShowInputWindow(bool isShowKeyboard, const SubProperty &subProperty);
113         void DissmissInputWindow();
114         bool isImeReady_{ false };
115         InputStartNotifier notifier_;
116     };
117 } // namespace MiscServices
118 } // namespace OHOS
119 #endif // FRAMEWORKS_INPUTMETHOD_ABILITY_INCLUDE_INPUT_METHOD_ABILITY_H
120