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 "input_method_engine_listener_impl.h"
17
18 #include "global.h"
19 #include "input_method_utils.h"
20
21 namespace OHOS {
22 namespace MiscServices {
23 bool InputMethodEngineListenerImpl::keyboardState_ = false;
24 bool InputMethodEngineListenerImpl::isInputStart_ = false;
25 uint32_t InputMethodEngineListenerImpl::windowId_ = 0;
26 std::mutex InputMethodEngineListenerImpl::imeListenerMutex_;
27 std::condition_variable InputMethodEngineListenerImpl::imeListenerCv_;
28 bool InputMethodEngineListenerImpl::isEnable_ { false };
29 bool InputMethodEngineListenerImpl::isInputFinish_ { false };
30 std::unordered_map<std::string, PrivateDataValue> InputMethodEngineListenerImpl::privateCommand_ {};
31 constexpr int32_t TIMEOUT_SECONDS = 2;
32
OnKeyboardStatus(bool isShow)33 void InputMethodEngineListenerImpl::OnKeyboardStatus(bool isShow)
34 {
35 IMSA_HILOGI("InputMethodEngineListenerImpl::OnKeyboardStatus %{public}s", isShow ? "show" : "hide");
36 keyboardState_ = isShow;
37 }
38
OnSecurityChange(int32_t security)39 void InputMethodEngineListenerImpl::OnSecurityChange(int32_t security)
40 {
41 IMSA_HILOGI("InputMethodEngineListenerImpl::OnSecurityChange %{public}d", security);
42 }
43
OnInputStart()44 void InputMethodEngineListenerImpl::OnInputStart()
45 {
46 IMSA_HILOGI("InputMethodEngineListenerImpl::OnInputStart");
47 isInputStart_ = true;
48 imeListenerCv_.notify_one();
49 }
50
OnInputStop()51 int32_t InputMethodEngineListenerImpl::OnInputStop()
52 {
53 IMSA_HILOGI("InputMethodEngineListenerImpl::OnInputStop");
54 return ErrorCode::NO_ERROR;
55 }
56
OnSetCallingWindow(uint32_t windowId)57 void InputMethodEngineListenerImpl::OnSetCallingWindow(uint32_t windowId)
58 {
59 IMSA_HILOGI("InputMethodEngineListenerImpl::OnSetCallingWindow %{public}d", windowId);
60 windowId_ = windowId;
61 imeListenerCv_.notify_one();
62 }
63
OnSetSubtype(const SubProperty & property)64 void InputMethodEngineListenerImpl::OnSetSubtype(const SubProperty &property)
65 {
66 IMSA_HILOGI("InputMethodEngineListenerImpl::OnSetSubtype");
67 }
68
OnInputFinish()69 void InputMethodEngineListenerImpl::OnInputFinish()
70 {
71 IMSA_HILOGI("OnInputFinish");
72 isInputFinish_ = true;
73 imeListenerCv_.notify_one();
74 }
75
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)76 void InputMethodEngineListenerImpl::ReceivePrivateCommand(
77 const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
78 {
79 IMSA_HILOGI("ReceivePrivateCommand");
80 privateCommand_ = privateCommand;
81 imeListenerCv_.notify_one();
82 }
83
IsEnable()84 bool InputMethodEngineListenerImpl::IsEnable()
85 {
86 IMSA_HILOGD("test::isEnable: %{public}d", isEnable_);
87 return isEnable_;
88 }
89
ResetParam()90 void InputMethodEngineListenerImpl::ResetParam()
91 {
92 isInputStart_ = false;
93 isInputFinish_ = false;
94 windowId_ = 0;
95 privateCommand_.clear();
96 }
97
WaitInputStart()98 bool InputMethodEngineListenerImpl::WaitInputStart()
99 {
100 std::unique_lock<std::mutex> lock(imeListenerMutex_);
101 imeListenerCv_.wait_for(lock, std::chrono::seconds(TIMEOUT_SECONDS), []() {
102 return isInputStart_;
103 });
104 return isInputStart_;
105 }
106
WaitInputFinish()107 bool InputMethodEngineListenerImpl::WaitInputFinish()
108 {
109 std::unique_lock<std::mutex> lock(imeListenerMutex_);
110 imeListenerCv_.wait_for(lock, std::chrono::seconds(TIMEOUT_SECONDS), []() {
111 return isInputFinish_;
112 });
113 return isInputFinish_;
114 }
115
WaitSetCallingWindow(uint32_t windowId)116 bool InputMethodEngineListenerImpl::WaitSetCallingWindow(uint32_t windowId)
117 {
118 std::unique_lock<std::mutex> lock(imeListenerMutex_);
119 imeListenerCv_.wait_for(lock, std::chrono::seconds(1), [&windowId]() {
120 return windowId_ == windowId;
121 });
122 return windowId_ == windowId;
123 }
124
WaitSendPrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)125 bool InputMethodEngineListenerImpl::WaitSendPrivateCommand(
126 const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
127 {
128 std::unique_lock<std::mutex> lock(imeListenerMutex_);
129 imeListenerCv_.wait_for(lock, std::chrono::seconds(1), [&privateCommand]() {
130 return privateCommand_ == privateCommand;
131 });
132
133 return privateCommand_ == privateCommand;
134 }
135
WaitKeyboardStatus(bool state)136 bool InputMethodEngineListenerImpl::WaitKeyboardStatus(bool state)
137 {
138 std::unique_lock<std::mutex> lock(imeListenerMutex_);
139 imeListenerCv_.wait_for(lock, std::chrono::seconds(1), [state]() {
140 return keyboardState_ == state;
141 });
142 return keyboardState_ == state;
143 }
144
PostTaskToEventHandler(std::function<void ()> task,const std::string & taskName)145 bool InputMethodEngineListenerImpl::PostTaskToEventHandler(std::function<void()> task, const std::string &taskName)
146 {
147 task();
148 return true;
149 }
150 } // namespace MiscServices
151 } // namespace OHOS
152