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 namespace OHOS {
19 namespace MiscServices {
20 std::mutex TextListener::textListenerCallbackLock_;
21 std::condition_variable TextListener::textListenerCv_;
22 int32_t TextListener::direction_ = -1;
23 int32_t TextListener::deleteForwardLength_ = -1;
24 int32_t TextListener::deleteBackwardLength_ = -1;
25 std::u16string TextListener::insertText_;
26 int32_t TextListener::key_ = -1;
27 bool TextListener::status_ = false;
28 int32_t TextListener::selectionStart_ = -1;
29 int32_t TextListener::selectionEnd_ = -1;
30 int32_t TextListener::selectionDirection_ = -1;
31 int32_t TextListener::selectionSkip_ = -1;
32 int32_t TextListener::action_ = -1;
33 uint32_t TextListener::height_ = 0;
34 KeyboardStatus TextListener::keyboardStatus_ = { KeyboardStatus::NONE };
35 PanelStatusInfo TextListener::info_{};
TextListener()36 TextListener::TextListener()
37 {
38 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("TextListenerNotifier");
39 serviceHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
40 }
41
~TextListener()42 TextListener::~TextListener()
43 {
44 }
45
InsertText(const std::u16string & text)46 void TextListener::InsertText(const std::u16string &text)
47 {
48 insertText_ = text;
49 textListenerCv_.notify_one();
50 }
51
DeleteForward(int32_t length)52 void TextListener::DeleteForward(int32_t length)
53 {
54 deleteForwardLength_ = length;
55 textListenerCv_.notify_one();
56 IMSA_HILOGI("TextChangeListener: DeleteForward, length is: %{public}d", length);
57 }
58
DeleteBackward(int32_t length)59 void TextListener::DeleteBackward(int32_t length)
60 {
61 deleteBackwardLength_ = length;
62 textListenerCv_.notify_one();
63 IMSA_HILOGI("TextChangeListener: DeleteBackward, direction is: %{public}d", length);
64 }
65
SendKeyEventFromInputMethod(const KeyEvent & event)66 void TextListener::SendKeyEventFromInputMethod(const KeyEvent &event)
67 {
68 }
69
SendKeyboardStatus(const KeyboardStatus & keyboardStatus)70 void TextListener::SendKeyboardStatus(const KeyboardStatus &keyboardStatus)
71 {
72 IMSA_HILOGD("TextListener::SendKeyboardStatus %{public}d", static_cast<int>(keyboardStatus));
73 keyboardStatus_ = keyboardStatus;
74 textListenerCv_.notify_one();
75 }
76
SendFunctionKey(const FunctionKey & functionKey)77 void TextListener::SendFunctionKey(const FunctionKey &functionKey)
78 {
79 EnterKeyType enterKeyType = functionKey.GetEnterKeyType();
80 key_ = static_cast<int32_t>(enterKeyType);
81 textListenerCv_.notify_one();
82 }
83
SetKeyboardStatus(bool status)84 void TextListener::SetKeyboardStatus(bool status)
85 {
86 status_ = status;
87 }
88
MoveCursor(const Direction direction)89 void TextListener::MoveCursor(const Direction direction)
90 {
91 direction_ = static_cast<int32_t>(direction);
92 textListenerCv_.notify_one();
93 IMSA_HILOGI("TextChangeListener: MoveCursor, direction is: %{public}d", direction);
94 }
95
HandleSetSelection(int32_t start,int32_t end)96 void TextListener::HandleSetSelection(int32_t start, int32_t end)
97 {
98 selectionStart_ = start;
99 selectionEnd_ = end;
100 textListenerCv_.notify_one();
101 IMSA_HILOGI(
102 "TextChangeListener, selectionStart_: %{public}d, selectionEnd_: %{public}d", selectionStart_, selectionEnd_);
103 }
104
HandleExtendAction(int32_t action)105 void TextListener::HandleExtendAction(int32_t action)
106 {
107 action_ = action;
108 textListenerCv_.notify_one();
109 IMSA_HILOGI("HandleExtendAction, action_: %{public}d", action_);
110 }
111
HandleSelect(int32_t keyCode,int32_t cursorMoveSkip)112 void TextListener::HandleSelect(int32_t keyCode, int32_t cursorMoveSkip)
113 {
114 selectionDirection_ = keyCode;
115 selectionSkip_ = cursorMoveSkip;
116 textListenerCv_.notify_one();
117 IMSA_HILOGI("TextChangeListener, selectionDirection_: %{public}d", selectionDirection_);
118 }
119
GetLeftTextOfCursor(int32_t number)120 std::u16string TextListener::GetLeftTextOfCursor(int32_t number)
121 {
122 return Str8ToStr16(TEXT_BEFORE_CURSOR);
123 }
124
GetRightTextOfCursor(int32_t number)125 std::u16string TextListener::GetRightTextOfCursor(int32_t number)
126 {
127 return Str8ToStr16(TEXT_AFTER_CURSOR);
128 }
129
GetTextIndexAtCursor()130 int32_t TextListener::GetTextIndexAtCursor()
131 {
132 return TEXT_INDEX;
133 }
134
NotifyPanelStatusInfo(const PanelStatusInfo & info)135 void TextListener::NotifyPanelStatusInfo(const PanelStatusInfo &info)
136 {
137 IMSA_HILOGD("TextListener::type: %{public}d, flag: %{public}d, visible: %{public}d, trigger: %{public}d.",
138 static_cast<PanelType>(info.panelInfo.panelType), static_cast<PanelFlag>(info.panelInfo.panelFlag),
139 info.visible, static_cast<Trigger>(info.trigger));
140 info_ = info;
141 textListenerCv_.notify_one();
142 }
143
NotifyKeyboardHeight(uint32_t height)144 void TextListener::NotifyKeyboardHeight(uint32_t height)
145 {
146 IMSA_HILOGD("keyboard height: %{public}u", height);
147 height_ = height;
148 textListenerCv_.notify_one();
149 }
150
ResetParam()151 void TextListener::ResetParam()
152 {
153 direction_ = -1;
154 deleteForwardLength_ = -1;
155 deleteBackwardLength_ = -1;
156 insertText_ = u"";
157 key_ = -1;
158 status_ = false;
159 selectionStart_ = -1;
160 selectionEnd_ = -1;
161 selectionDirection_ = -1;
162 selectionSkip_ = -1;
163 action_ = -1;
164 keyboardStatus_ = KeyboardStatus::NONE;
165 info_ = {};
166 height_ = 0;
167 }
168
WaitSendKeyboardStatusCallback(const KeyboardStatus & keyboardStatus)169 bool TextListener::WaitSendKeyboardStatusCallback(const KeyboardStatus &keyboardStatus)
170 {
171 std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
172 textListenerCv_.wait_for(
173 lock, std::chrono::seconds(1), [&keyboardStatus]() { return keyboardStatus == keyboardStatus_; });
174 return keyboardStatus == keyboardStatus_;
175 }
176
WaitNotifyPanelStatusInfoCallback(const PanelStatusInfo & info)177 bool TextListener::WaitNotifyPanelStatusInfoCallback(const PanelStatusInfo &info)
178 {
179 std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
180 textListenerCv_.wait_for(lock, std::chrono::seconds(1), [info]() { return info == info_; });
181 return info == info_;
182 }
183
WaitNotifyKeyboardHeightCallback(uint32_t height)184 bool TextListener::WaitNotifyKeyboardHeightCallback(uint32_t height)
185 {
186 std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
187 textListenerCv_.wait_for(lock, std::chrono::seconds(1), [height]() { return height_ == height; });
188 return height_ == height;
189 }
190 } // namespace MiscServices
191 } // namespace OHOS