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_manager_listener.h"
17 #ifdef STANDBY_MULTIMODALINPUT_INPUT_ENABLE
18 #include "input_manager.h"
19 #endif
20 #include "istate_manager_adapter.h"
21 #include "standby_service_impl.h"
22 #include "standby_service_log.h"
23 #include "want.h"
24 
25 namespace OHOS {
26 namespace DevStandbyMgr {
InputManagerListener()27 InputManagerListener::InputManagerListener()
28 {
29     handler_ = StandbyServiceImpl::GetInstance()->GetHandler();
30 }
31 
StartListener()32 ErrCode WEAK_FUNC InputManagerListener::StartListener()
33 {
34     STANDBYSERVICE_LOGD("start input manager listener");
35     #ifdef STANDBY_MULTIMODALINPUT_INPUT_ENABLE
36     subscriberId_ = MMI::InputManager::GetInstance()->SubscribeSwitchEvent(
37         [listener = shared_from_this()] (std::shared_ptr<OHOS::MMI::SwitchEvent> switchEvent) {
38             listener->OnCallbackEvent(switchEvent->GetSwitchValue());
39             });
40     #endif
41     STANDBYSERVICE_LOGD("start input manager listner, result is %{public}d", subscriberId_);
42     return ERR_OK;
43 }
44 
StopListener()45 ErrCode WEAK_FUNC InputManagerListener::StopListener()
46 {
47     if (subscriberId_ < 0) {
48         return ERR_STANDBY_STOP_LISENER_FAILED;
49     }
50     #ifdef STANDBY_MULTIMODALINPUT_INPUT_ENABLE
51     MMI::InputManager::GetInstance()->UnsubscribeSwitchEvent(subscriberId_);
52     #endif
53     subscriberId_ = 0;
54     return ERR_OK;
55 }
56 
OnCallbackEvent(int32_t switchValue)57 void InputManagerListener::OnCallbackEvent(int32_t switchValue)
58 {
59     StandbyMessage msg {StandbyMessageType::COMMON_EVENT};
60     #ifdef STANDBY_MULTIMODALINPUT_INPUT_ENABLE
61     if (switchValue == MMI::SwitchEvent::SWITCH_OFF) {
62         STANDBYSERVICE_LOGI("lid close event received");
63         msg.action_ = LID_CLOSE;
64     } else {
65         STANDBYSERVICE_LOGI("lid open event received");
66         msg.action_ = LID_OPEN;
67     }
68     #else
69         msg.action_ = LID_OPEN;
70     #endif
71     StandbyServiceImpl::GetInstance()->DispatchEvent(msg);
72 }
73 }  // namespace DevStandbyMgr
74 }  // namespace OHOS