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 "common_event_listener.h"
17
18 #include "bundle_constants.h"
19 #include "want.h"
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22
23 #include "standby_service_impl.h"
24 #include "istate_manager_adapter.h"
25 #include "istandby_service.h"
26 #include "call_manager_inner_type.h"
27 #include "wifi_p2p_msg.h"
28
29 namespace OHOS {
30 namespace DevStandbyMgr {
31 using namespace OHOS::Telephony;
32 using namespace OHOS::Wifi;
CommonEventListener(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)33 CommonEventListener::CommonEventListener(const EventFwk::CommonEventSubscribeInfo& subscribeInfo)
34 : EventFwk::CommonEventSubscriber(subscribeInfo)
35 {
36 handler_ = StandbyServiceImpl::GetInstance()->GetHandler();
37 standbyStateManager_ = StandbyServiceImpl::GetInstance()->GetStateManager();
38 }
39
StartListener()40 ErrCode WEAK_FUNC CommonEventListener::StartListener()
41 {
42 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(shared_from_this())) {
43 STANDBYSERVICE_LOGE("SubscribeCommonEvent occur exception");
44 return ERR_STANDBY_START_LISENER_FAILED;
45 }
46 return ERR_OK;
47 }
48
StopListener()49 ErrCode WEAK_FUNC CommonEventListener::StopListener()
50 {
51 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(shared_from_this())) {
52 STANDBYSERVICE_LOGE("UnsubscribeCommonEvent occur exception");
53 return ERR_STANDBY_STOP_LISENER_FAILED;
54 }
55 return ERR_OK;
56 }
57
HandleCallStateChanged(int32_t state)58 void WEAK_FUNC CommonEventListener::HandleCallStateChanged(int32_t state)
59 {
60 bool disabled = (state == static_cast<int32_t>(TelCallState::CALL_STATUS_UNKNOWN) ||
61 state == static_cast<int32_t>(TelCallState::CALL_STATUS_DISCONNECTED) ||
62 state == static_cast<int32_t>(TelCallState::CALL_STATUS_IDLE));
63 DeviceStateCache::GetInstance()->SetDeviceState(
64 static_cast<int32_t>(DeviceStateType::TELEPHONE_STATE_CHANGE), !disabled);
65 }
66
HandleP2PStateChanged(int32_t state)67 void WEAK_FUNC CommonEventListener::HandleP2PStateChanged(int32_t state)
68 {
69 bool disabled = (state == static_cast<int32_t>(P2pState::P2P_STATE_IDLE) ||
70 state == static_cast<int32_t>(P2pState::P2P_STATE_NONE) ||
71 state == static_cast<int32_t>(P2pState::P2P_STATE_CLOSED));
72 DeviceStateCache::GetInstance()->SetDeviceState(
73 static_cast<int32_t>(DeviceStateType::WIFI_P2P_CHANGE), !disabled);
74 }
75
OnReceiveEvent(const EventFwk::CommonEventData & eventData)76 void CommonEventListener::OnReceiveEvent(const EventFwk::CommonEventData& eventData)
77 {
78 AAFwk::Want want = eventData.GetWant();
79 std::string action = want.GetAction();
80
81 STANDBYSERVICE_LOGD("receive common event %{public}s", action.c_str());
82 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
83 StandbyServiceImpl::GetInstance()->DispatchEvent(StandbyMessage(StandbyMessageType::COMMON_EVENT, action));
84 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
85 StandbyServiceImpl::GetInstance()->DispatchEvent(StandbyMessage(StandbyMessageType::COMMON_EVENT, action));
86 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_CHARGING ||
87 action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED ||
88 action == EventFwk::CommonEventSupport::COMMON_EVENT_DISCHARGING ||
89 action == EventFwk::CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED) {
90 StandbyServiceImpl::GetInstance()->DispatchEvent(StandbyMessage(StandbyMessageType::COMMON_EVENT, action));
91 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED) {
92 int32_t state = want.GetIntParam("state", static_cast<int32_t>(TelCallState::CALL_STATUS_UNKNOWN));
93 HandleCallStateChanged(state);
94 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_P2P_STATE_CHANGED) {
95 HandleP2PStateChanged(eventData.GetCode());
96 }
97 }
98 } // namespace DevStandbyMgr
99 } // namespace OHOS