• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "text_listener.h"
17 
18 #include "input_method_utils.h"
19 
20 namespace OHOS {
21 namespace MiscServices {
22 constexpr uint32_t KEYBOARD_STATUS_WAIT_TIME_OUT = 3;
23 std::mutex TextListener::textListenerCallbackLock_;
24 std::condition_variable TextListener::textListenerCv_;
25 int32_t TextListener::direction_ = -1;
26 int32_t TextListener::deleteForwardLength_ = -1;
27 int32_t TextListener::deleteBackwardLength_ = -1;
28 std::u16string TextListener::insertText_;
29 int32_t TextListener::key_ = -1;
30 bool TextListener::status_ = false;
31 int32_t TextListener::selectionStart_ = -1;
32 int32_t TextListener::selectionEnd_ = -1;
33 int32_t TextListener::selectionDirection_ = -1;
34 int32_t TextListener::selectionSkip_ = -1;
35 int32_t TextListener::action_ = -1;
36 uint32_t TextListener::height_ = 0;
37 KeyboardStatus TextListener::keyboardStatus_ = { KeyboardStatus::NONE };
38 PanelStatusInfo TextListener::info_ {};
39 std::unordered_map<std::string, PrivateDataValue> TextListener::privateCommand_ {};
40 std::string TextListener::previewText_;
41 Range TextListener::previewRange_ {};
42 bool TextListener::isFinishTextPreviewCalled_ { false };
TextListener()43 TextListener::TextListener()
44 {
45     std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("TextListenerNotifier");
46     serviceHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
47 }
48 
~TextListener()49 TextListener::~TextListener() { }
50 
InsertText(const std::u16string & text)51 void TextListener::InsertText(const std::u16string &text)
52 {
53     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
54     insertText_ = text;
55     textListenerCv_.notify_one();
56     IMSA_HILOGI("TextListener text: %{public}s", Str16ToStr8(text).c_str());
57 }
58 
DeleteForward(int32_t length)59 void TextListener::DeleteForward(int32_t length)
60 {
61     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
62     deleteForwardLength_ = length;
63     textListenerCv_.notify_one();
64     IMSA_HILOGI("TextListener: DeleteForward, length is: %{public}d", length);
65 }
66 
DeleteBackward(int32_t length)67 void TextListener::DeleteBackward(int32_t length)
68 {
69     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
70     deleteBackwardLength_ = length;
71     textListenerCv_.notify_one();
72     IMSA_HILOGI("TextListener: DeleteBackward, direction is: %{public}d", length);
73 }
74 
SendKeyEventFromInputMethod(const KeyEvent & event)75 void TextListener::SendKeyEventFromInputMethod(const KeyEvent &event) { }
76 
SendKeyboardStatus(const KeyboardStatus & keyboardStatus)77 void TextListener::SendKeyboardStatus(const KeyboardStatus &keyboardStatus)
78 {
79     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
80     IMSA_HILOGI("TextListener::SendKeyboardStatus %{public}d", static_cast<int>(keyboardStatus));
81     keyboardStatus_ = keyboardStatus;
82     textListenerCv_.notify_one();
83 }
84 
SendFunctionKey(const FunctionKey & functionKey)85 void TextListener::SendFunctionKey(const FunctionKey &functionKey)
86 {
87     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
88     EnterKeyType enterKeyType = functionKey.GetEnterKeyType();
89     key_ = static_cast<int32_t>(enterKeyType);
90     IMSA_HILOGI("TextListener functionKey: %{public}d", key_);
91     textListenerCv_.notify_one();
92 }
93 
SetKeyboardStatus(bool status)94 void TextListener::SetKeyboardStatus(bool status)
95 {
96     status_ = status;
97     IMSA_HILOGI("TextListener status: %{public}d", status);
98 }
99 
MoveCursor(const Direction direction)100 void TextListener::MoveCursor(const Direction direction)
101 {
102     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
103     direction_ = static_cast<int32_t>(direction);
104     textListenerCv_.notify_one();
105     IMSA_HILOGI("TextListener: MoveCursor, direction is: %{public}d", direction);
106 }
107 
HandleSetSelection(int32_t start,int32_t end)108 void TextListener::HandleSetSelection(int32_t start, int32_t end)
109 {
110     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
111     selectionStart_ = start;
112     selectionEnd_ = end;
113     textListenerCv_.notify_one();
114     IMSA_HILOGI("TextListener, selectionStart_: %{public}d, selectionEnd_: %{public}d", selectionStart_, selectionEnd_);
115 }
116 
HandleExtendAction(int32_t action)117 void TextListener::HandleExtendAction(int32_t action)
118 {
119     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
120     action_ = action;
121     textListenerCv_.notify_one();
122     IMSA_HILOGI("HandleExtendAction, action_: %{public}d", action_);
123 }
124 
HandleSelect(int32_t keyCode,int32_t cursorMoveSkip)125 void TextListener::HandleSelect(int32_t keyCode, int32_t cursorMoveSkip)
126 {
127     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
128     selectionDirection_ = keyCode;
129     selectionSkip_ = cursorMoveSkip;
130     textListenerCv_.notify_one();
131     IMSA_HILOGI("TextListener, selectionDirection_: %{public}d", selectionDirection_);
132 }
133 
GetLeftTextOfCursor(int32_t number)134 std::u16string TextListener::GetLeftTextOfCursor(int32_t number)
135 {
136     IMSA_HILOGI("TextListener number: %{public}d", number);
137     return Str8ToStr16(TEXT_BEFORE_CURSOR);
138 }
139 
GetRightTextOfCursor(int32_t number)140 std::u16string TextListener::GetRightTextOfCursor(int32_t number)
141 {
142     IMSA_HILOGI("TextListener number: %{public}d", number);
143     return Str8ToStr16(TEXT_AFTER_CURSOR);
144 }
145 
GetTextIndexAtCursor()146 int32_t TextListener::GetTextIndexAtCursor()
147 {
148     return TEXT_INDEX;
149 }
150 
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)151 int32_t TextListener::ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
152 {
153     privateCommand_ = privateCommand;
154     return 0;
155 }
156 
NotifyPanelStatusInfo(const PanelStatusInfo & info)157 void TextListener::NotifyPanelStatusInfo(const PanelStatusInfo &info)
158 {
159     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
160     IMSA_HILOGI("TextListener::type: %{public}d, flag: %{public}d, visible: %{public}d, trigger: %{public}d.",
161         static_cast<PanelType>(info.panelInfo.panelType), static_cast<PanelFlag>(info.panelInfo.panelFlag),
162         info.visible, static_cast<Trigger>(info.trigger));
163     info_ = info;
164     textListenerCv_.notify_one();
165 }
166 
NotifyKeyboardHeight(uint32_t height)167 void TextListener::NotifyKeyboardHeight(uint32_t height)
168 {
169     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
170     IMSA_HILOGI("keyboard height: %{public}u", height);
171     height_ = height;
172     textListenerCv_.notify_one();
173 }
174 
SetPreviewText(const std::u16string & text,const Range & range)175 int32_t TextListener::SetPreviewText(const std::u16string &text, const Range &range)
176 {
177     IMSA_HILOGI("TextListener, text: %{public}s, range[start, end]: [%{public}d, %{public}d]",
178         Str16ToStr8(text).c_str(), range.start, range.end);
179     previewText_ = Str16ToStr8(text);
180     previewRange_ = range;
181     return ErrorCode::NO_ERROR;
182 }
183 
FinishTextPreview()184 void TextListener::FinishTextPreview()
185 {
186     IMSA_HILOGI("TextListener in");
187     isFinishTextPreviewCalled_ = true;
188 }
189 
ResetParam()190 void TextListener::ResetParam()
191 {
192     direction_ = -1;
193     deleteForwardLength_ = -1;
194     deleteBackwardLength_ = -1;
195     insertText_ = u"";
196     key_ = -1;
197     status_ = false;
198     selectionStart_ = -1;
199     selectionEnd_ = -1;
200     selectionDirection_ = -1;
201     selectionSkip_ = -1;
202     action_ = -1;
203     keyboardStatus_ = KeyboardStatus::NONE;
204     info_ = {};
205     height_ = 0;
206     previewText_ = "";
207     previewRange_ = {};
208     isFinishTextPreviewCalled_ = false;
209 }
210 
WaitSendKeyboardStatusCallback(const KeyboardStatus & keyboardStatus)211 bool TextListener::WaitSendKeyboardStatusCallback(const KeyboardStatus &keyboardStatus)
212 {
213     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
214     textListenerCv_.wait_for(lock, std::chrono::seconds(KEYBOARD_STATUS_WAIT_TIME_OUT), [&keyboardStatus]() {
215         return keyboardStatus == keyboardStatus_;
216     });
217     return keyboardStatus == keyboardStatus_;
218 }
219 
WaitNotifyPanelStatusInfoCallback(const PanelStatusInfo & info)220 bool TextListener::WaitNotifyPanelStatusInfoCallback(const PanelStatusInfo &info)
221 {
222     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
223     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [info]() {
224         return info == info_;
225     });
226     return info == info_;
227 }
228 
WaitNotifyKeyboardHeightCallback(uint32_t height)229 bool TextListener::WaitNotifyKeyboardHeightCallback(uint32_t height)
230 {
231     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
232     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [height]() {
233         return height_ == height;
234     });
235     return height_ == height;
236 }
237 
WaitSendPrivateCommandCallback(std::unordered_map<std::string,PrivateDataValue> & privateCommand)238 bool TextListener::WaitSendPrivateCommandCallback(std::unordered_map<std::string, PrivateDataValue> &privateCommand)
239 {
240     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
241     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [privateCommand]() {
242         return privateCommand_ == privateCommand;
243     });
244     return privateCommand_ == privateCommand;
245 }
246 
WaitInsertText(const std::u16string & insertText)247 bool TextListener::WaitInsertText(const std::u16string &insertText)
248 {
249     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
250     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [insertText]() {
251         return insertText_ == insertText;
252     });
253     return insertText_ == insertText;
254 }
255 
WaitMoveCursor(int32_t direction)256 bool TextListener::WaitMoveCursor(int32_t direction)
257 {
258     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
259     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [direction]() {
260         return direction_ == direction;
261     });
262     return direction_ == direction;
263 }
264 
WaitDeleteForward(int32_t length)265 bool TextListener::WaitDeleteForward(int32_t length)
266 {
267     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
268     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [length]() {
269         return deleteForwardLength_ == length;
270     });
271     return deleteForwardLength_ == length;
272 }
273 
WaitDeleteBackward(int32_t length)274 bool TextListener::WaitDeleteBackward(int32_t length)
275 {
276     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
277     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [length]() { return deleteBackwardLength_ == length; });
278     return deleteBackwardLength_ == length;
279 }
280 
WaitSendFunctionKey(int32_t functionKey)281 bool TextListener::WaitSendFunctionKey(int32_t functionKey)
282 {
283     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
284     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [functionKey]() {
285         return key_ == functionKey;
286     });
287     return key_ == functionKey;
288 }
289 
WaitHandleExtendAction(int32_t action)290 bool TextListener::WaitHandleExtendAction(int32_t action)
291 {
292     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
293     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [action]() {
294         return action_ == action;
295     });
296     return action_ == action;
297 }
298 
WaitHandleSetSelection(int32_t start,int32_t end)299 bool TextListener::WaitHandleSetSelection(int32_t start, int32_t end)
300 {
301     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
302     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [start, end]() {
303         return selectionStart_ == start && selectionEnd_ == end;
304     });
305     return selectionStart_ == start && selectionEnd_ == end;
306 }
307 
WaitHandleSelect(int32_t keyCode,int32_t cursorMoveSkip)308 bool TextListener::WaitHandleSelect(int32_t keyCode, int32_t cursorMoveSkip)
309 {
310     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
311     textListenerCv_.wait_for(lock, std::chrono::seconds(1), [keyCode]() {
312         return selectionDirection_ == keyCode;
313     });
314     return selectionDirection_ == keyCode;
315 }
316 } // namespace MiscServices
317 } // namespace OHOS