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_{};
OnKeyboardStatus(bool isShow)31 void InputMethodEngineListenerImpl::OnKeyboardStatus(bool isShow)
32 {
33 IMSA_HILOGI("InputMethodEngineListenerImpl::OnKeyboardStatus %{public}s", isShow ? "show" : "hide");
34 keyboardState_ = isShow;
35 }
OnSecurityChange(int32_t security)36 void InputMethodEngineListenerImpl::OnSecurityChange(int32_t security)
37 {
38 IMSA_HILOGI("InputMethodEngineListenerImpl::OnSecurityChange %{public}d", security);
39 }
OnInputStart()40 void InputMethodEngineListenerImpl::OnInputStart()
41 {
42 IMSA_HILOGI("InputMethodEngineListenerImpl::OnInputStart");
43 isInputStart_ = true;
44 imeListenerCv_.notify_one();
45 }
OnInputStop()46 int32_t InputMethodEngineListenerImpl::OnInputStop()
47 {
48 IMSA_HILOGI("InputMethodEngineListenerImpl::OnInputStop");
49 return ErrorCode::NO_ERROR;
50 }
OnSetCallingWindow(uint32_t windowId)51 void InputMethodEngineListenerImpl::OnSetCallingWindow(uint32_t windowId)
52 {
53 IMSA_HILOGI("InputMethodEngineListenerImpl::OnSetCallingWindow %{public}d", windowId);
54 windowId_ = windowId;
55 imeListenerCv_.notify_one();
56 }
OnSetSubtype(const SubProperty & property)57 void InputMethodEngineListenerImpl::OnSetSubtype(const SubProperty &property)
58 {
59 IMSA_HILOGI("InputMethodEngineListenerImpl::OnSetSubtype");
60 }
OnInputFinish()61 void InputMethodEngineListenerImpl::OnInputFinish()
62 {
63 IMSA_HILOGI("OnInputFinish");
64 isInputFinish_ = true;
65 imeListenerCv_.notify_one();
66 }
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)67 void InputMethodEngineListenerImpl::ReceivePrivateCommand(
68 const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
69 {
70 IMSA_HILOGI("ReceivePrivateCommand");
71 privateCommand_ = privateCommand;
72 imeListenerCv_.notify_one();
73 }
IsEnable()74 bool InputMethodEngineListenerImpl::IsEnable()
75 {
76 IMSA_HILOGD("test::isEnable: %{public}d", isEnable_);
77 return isEnable_;
78 }
ResetParam()79 void InputMethodEngineListenerImpl::ResetParam()
80 {
81 isInputStart_ = false;
82 isInputFinish_ = false;
83 windowId_ = 0;
84 privateCommand_.clear();
85 }
WaitInputStart()86 bool InputMethodEngineListenerImpl::WaitInputStart()
87 {
88 std::unique_lock<std::mutex> lock(imeListenerMutex_);
89 imeListenerCv_.wait_for(lock, std::chrono::seconds(1), []() { return isInputStart_; });
90 return isInputStart_;
91 }
WaitInputFinish()92 bool InputMethodEngineListenerImpl::WaitInputFinish()
93 {
94 std::unique_lock<std::mutex> lock(imeListenerMutex_);
95 imeListenerCv_.wait_for(lock, std::chrono::seconds(1), []() { return isInputFinish_; });
96 return isInputFinish_;
97 }
WaitSetCallingWindow(uint32_t windowId)98 bool InputMethodEngineListenerImpl::WaitSetCallingWindow(uint32_t windowId)
99 {
100 std::unique_lock<std::mutex> lock(imeListenerMutex_);
101 imeListenerCv_.wait_for(lock, std::chrono::seconds(1), [&windowId]() { return windowId_ == windowId; });
102 return windowId_ == windowId;
103 }
WaitSendPrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)104 bool InputMethodEngineListenerImpl::WaitSendPrivateCommand(
105 const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
106 {
107 std::unique_lock<std::mutex> lock(imeListenerMutex_);
108 imeListenerCv_.wait_for(
109 lock, std::chrono::seconds(1), [&privateCommand]() { return privateCommand_ == privateCommand; });
110
111 return privateCommand_ == privateCommand;
112 }
113 } // namespace MiscServices
114 } // namespace OHOS
115