• 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 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::action_ = -1;
32 KeyboardStatus TextListener::keyboardStatus_ = { KeyboardStatus::NONE };
33 bool TextListener::isTimeout_ = { false };
34 
TextListener()35 TextListener::TextListener()
36 {
37     std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("TextListenerNotifier");
38     serviceHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
39 }
40 
~TextListener()41 TextListener::~TextListener() {}
42 
InsertText(const std::u16string & text)43 void TextListener::InsertText(const std::u16string &text)
44 {
45     insertText_ = text;
46     textListenerCv_.notify_one();
47 }
48 
DeleteForward(int32_t length)49 void TextListener::DeleteForward(int32_t length)
50 {
51     deleteForwardLength_ = length;
52     textListenerCv_.notify_one();
53     IMSA_HILOGI("TextChangeListener: DeleteForward, length is: %{public}d", length);
54 }
55 
DeleteBackward(int32_t length)56 void TextListener::DeleteBackward(int32_t length)
57 {
58     deleteBackwardLength_ = length;
59     textListenerCv_.notify_one();
60     IMSA_HILOGI("TextChangeListener: DeleteBackward, direction is: %{public}d", length);
61 }
62 
SendKeyEventFromInputMethod(const KeyEvent & event)63 void TextListener::SendKeyEventFromInputMethod(const KeyEvent &event) {}
64 
SendKeyboardStatus(const KeyboardStatus & keyboardStatus)65 void TextListener::SendKeyboardStatus(const KeyboardStatus &keyboardStatus)
66 {
67     IMSA_HILOGD("TextListener::SendKeyboardStatus %{public}d", static_cast<int>(keyboardStatus));
68     constexpr int32_t interval = 20;
69     {
70         std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
71         IMSA_HILOGD("TextListener::SendKeyboardStatus lock");
72         keyboardStatus_ = keyboardStatus;
73     }
74     serviceHandler_->PostTask(
75         [this]() {
76             textListenerCv_.notify_all();
77         },
78         interval);
79     IMSA_HILOGD("TextListener::SendKeyboardStatus notify_all");
80 }
81 
SendFunctionKey(const FunctionKey & functionKey)82 void TextListener::SendFunctionKey(const FunctionKey &functionKey)
83 {
84     EnterKeyType enterKeyType = functionKey.GetEnterKeyType();
85     key_ = static_cast<int32_t>(enterKeyType);
86     textListenerCv_.notify_one();
87 }
88 
SetKeyboardStatus(bool status)89 void TextListener::SetKeyboardStatus(bool status)
90 {
91     status_ = status;
92 }
93 
MoveCursor(const Direction direction)94 void TextListener::MoveCursor(const Direction direction)
95 {
96     direction_ = static_cast<int32_t>(direction);
97     textListenerCv_.notify_one();
98     IMSA_HILOGI("TextChangeListener: MoveCursor, direction is: %{public}d", direction);
99 }
100 
HandleSetSelection(int32_t start,int32_t end)101 void TextListener::HandleSetSelection(int32_t start, int32_t end)
102 {
103     selectionStart_ = start;
104     selectionEnd_ = end;
105     textListenerCv_.notify_one();
106     IMSA_HILOGI(
107         "TextChangeListener, selectionStart_: %{public}d, selectionEnd_: %{public}d", selectionStart_, selectionEnd_);
108 }
109 
HandleExtendAction(int32_t action)110 void TextListener::HandleExtendAction(int32_t action)
111 {
112     action_ = action;
113     textListenerCv_.notify_one();
114     IMSA_HILOGI("HandleExtendAction, action_: %{public}d", action_);
115 }
116 
HandleSelect(int32_t keyCode,int32_t cursorMoveSkip)117 void TextListener::HandleSelect(int32_t keyCode, int32_t cursorMoveSkip)
118 {
119     selectionDirection_ = keyCode;
120     textListenerCv_.notify_one();
121     IMSA_HILOGI("TextChangeListener, selectionDirection_: %{public}d", selectionDirection_);
122 }
GetLeftTextOfCursor(int32_t number)123 std::u16string TextListener::GetLeftTextOfCursor(int32_t number)
124 {
125     if (isTimeout_) {
126         usleep(MAX_TIMEOUT);
127     }
128     return Str8ToStr16(TEXT_BEFORE_CURSOR);
129 }
GetRightTextOfCursor(int32_t number)130 std::u16string TextListener::GetRightTextOfCursor(int32_t number)
131 {
132     if (isTimeout_) {
133         usleep(MAX_TIMEOUT);
134     }
135     return Str8ToStr16(TEXT_AFTER_CURSOR);
136 }
GetTextIndexAtCursor()137 int32_t TextListener::GetTextIndexAtCursor()
138 {
139     if (isTimeout_) {
140         usleep(MAX_TIMEOUT);
141     }
142     return TEXT_INDEX;
143 }
setTimeout(bool isTimeout)144 void TextListener::setTimeout(bool isTimeout)
145 {
146     isTimeout_ = isTimeout;
147 }
ResetParam()148 void TextListener::ResetParam()
149 {
150     direction_ = -1;
151     deleteForwardLength_ = -1;
152     deleteBackwardLength_ = -1;
153     insertText_ = u"";
154     key_ = -1;
155     status_ = false;
156     selectionStart_ = -1;
157     selectionEnd_ = -1;
158     selectionDirection_ = -1;
159     action_ = -1;
160     keyboardStatus_ = KeyboardStatus::NONE;
161     isTimeout_ = false;
162 }
WaitIMACallback()163 bool TextListener::WaitIMACallback()
164 {
165     std::unique_lock<std::mutex> lock(textListenerCallbackLock_);
166     return TextListener::textListenerCv_.wait_for(lock, std::chrono::seconds(1)) != std::cv_status::timeout;
167 }
168 } // namespace MiscServices
169 } // namespace OHOS