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