• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "setting_listeners.h"
17 
18 #include "ime_event_monitor_manager_impl.h"
19 #include "cj_lambda.h"
20 #include "utils.h"
21 
22 namespace OHOS::MiscServices {
23 
24 std::mutex CJGetInputMethodSetting::msMutex_;
25 std::shared_ptr<CJGetInputMethodSetting> CJGetInputMethodSetting::inputMethod_{ nullptr };
26 
GetIMSInstance()27 std::shared_ptr<CJGetInputMethodSetting> CJGetInputMethodSetting::GetIMSInstance()
28 {
29     if (inputMethod_ == nullptr) {
30         std::lock_guard<std::mutex> lock(msMutex_);
31         if (inputMethod_ == nullptr) {
32             inputMethod_ = std::make_shared<CJGetInputMethodSetting>();
33         }
34     }
35     return inputMethod_;
36 }
37 
Subscribe(uint32_t type,void (* func)(CInputMethodProperty,CInputMethodSubtype))38 int32_t CJGetInputMethodSetting::Subscribe(uint32_t type, void (*func)(CInputMethodProperty, CInputMethodSubtype))
39 {
40     auto engine = CJGetInputMethodSetting::GetIMSInstance();
41     if (engine == nullptr) {
42         return 0;
43     }
44 
45     auto ret = ImeEventMonitorManagerImpl::GetInstance().RegisterImeEventListener(type, inputMethod_);
46     if (ret == ErrorCode::NO_ERROR) {
47         callback = CJLambda::Create(func);
48     }
49     return ret;
50 }
51 
UnSubscribe(uint32_t type)52 int32_t CJGetInputMethodSetting::UnSubscribe(uint32_t type)
53 {
54     auto engine = CJGetInputMethodSetting::GetIMSInstance();
55     if (engine == nullptr) {
56         return 0;
57     }
58     auto ret = ImeEventMonitorManagerImpl::GetInstance().UnRegisterImeEventListener(type, inputMethod_);
59     IMSA_HILOGI("UpdateListenEventFlag, ret: %{public}d, type: %{public}d.", ret, type);
60     return ret;
61 }
62 
OnImeChange(const Property & property,const SubProperty & subProperty)63 void CJGetInputMethodSetting::OnImeChange(const Property &property, const SubProperty &subProperty)
64 {
65     IMSA_HILOGD("start");
66     CInputMethodProperty prop;
67     CInputMethodSubtype subProp;
68     Utils::InputMethodSubProperty2C(&subProp, subProperty);
69     Utils::InputMethodProperty2C(&prop, property);
70     callback(prop, subProp);
71 }
72 }