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