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 "device_event_monitor.h"
17
18 #include "input_event_handler.h"
19
20 #undef MMI_LOG_TAG
21 #define MMI_LOG_TAG "DeviceEventMonitor"
22
23 namespace OHOS {
24 namespace MMI {
25 const char* SOS_PAGE_CHANGE_EVENTS = "emergencycommunication.event.SOS_EMERGENCY_CALL_ABILITY_PAGE_CHANGE";
DeviceEventMonitor()26 DeviceEventMonitor::DeviceEventMonitor() {}
~DeviceEventMonitor()27 DeviceEventMonitor::~DeviceEventMonitor() {}
28
29 class DeviceChangedReceiver : public EventFwk::CommonEventSubscriber {
30 public:
DeviceChangedReceiver(const OHOS::EventFwk::CommonEventSubscribeInfo & subscribeInfo)31 explicit DeviceChangedReceiver(const OHOS::EventFwk::CommonEventSubscribeInfo& subscribeInfo)
32 : OHOS::EventFwk::CommonEventSubscriber(subscribeInfo)
33 {
34 MMI_HILOGD("DeviceEventMonitor register");
35 }
36
37 virtual ~DeviceChangedReceiver() = default;
38 __attribute__((no_sanitize("cfi")))
39
OnReceiveEvent(const EventFwk::CommonEventData & eventData)40 void OnReceiveEvent(const EventFwk::CommonEventData &eventData)
41 {
42 CALL_DEBUG_ENTER;
43 std::string action = eventData.GetWant().GetAction();
44 if (action.empty()) {
45 MMI_HILOGE("The action is empty");
46 return;
47 }
48 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED) {
49 int32_t callState = 0;
50 DEVICE_MONITOR->SetCallState(eventData, callState);
51 } else if (action == SOS_PAGE_CHANGE_EVENTS) {
52 MMI_HILOGD("Display emergency call page change");
53 std::string pageName = eventData.GetWant().GetStringParam("pageName");
54 if (pageName.empty()) {
55 MMI_HILOGE("StringParam is empty");
56 return;
57 }
58 auto eventKeyCommandHandler = InputHandler->GetKeyCommandHandler();
59 CHKPV(eventKeyCommandHandler);
60 int32_t ret = eventKeyCommandHandler->SetIsFreezePowerKey(pageName);
61 if (ret != RET_OK) {
62 MMI_HILOGE("SetIsFreezePowerKey is failed in key command:%{public}d", ret);
63 return;
64 }
65 } else {
66 MMI_HILOGW("Device changed receiver event: unknown");
67 return;
68 }
69 }
70 };
71
InitCommonEventSubscriber()72 void DeviceEventMonitor::InitCommonEventSubscriber()
73 {
74 CALL_DEBUG_ENTER;
75 std::lock_guard<std::mutex> lock(commonEventMutex_);
76 if (hasInit_) {
77 MMI_HILOGE("Current common event has subscribered");
78 return;
79 }
80 EventFwk::MatchingSkills matchingSkills;
81 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED);
82 matchingSkills.AddEvent(SOS_PAGE_CHANGE_EVENTS);
83 EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
84 commonEventSubscribeInfo.SetPermission("ohos.permission.SET_TELEPHONY_STATE");
85 hasInit_ = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(
86 std::make_shared<DeviceChangedReceiver>(commonEventSubscribeInfo));
87 }
88
SetCallState(const EventFwk::CommonEventData & eventData,int32_t callState)89 void DeviceEventMonitor::SetCallState(const EventFwk::CommonEventData &eventData, int32_t callState)
90 {
91 CALL_DEBUG_ENTER;
92 std::lock_guard<std::mutex> lock(stateMutex_);
93 if (eventData.GetWant().GetIntParam("slotId", -1) != -1) {
94 int32_t state = eventData.GetWant().GetIntParam("state", -1);
95 if (hasHandleRingMute_ && (state == CALL_STATUS_INCOMING || state == CALL_STATUS_DISCONNECTED)) {
96 hasHandleRingMute_ = false;
97 }
98 return;
99 }
100 callState = eventData.GetWant().GetIntParam("state", -1);
101 MMI_HILOGI("The state %{public}d", callState);
102 if (hasHandleRingMute_ && (callState_ == CALL_STATUS_INCOMING || callState_ == CALL_STATUS_WAITING)) {
103 MMI_HILOGI("Mute reply success");
104 hasHandleRingMute_ = false;
105 }
106 callState_ = callState;
107 }
108
GetCallState()109 int32_t DeviceEventMonitor::GetCallState()
110 {
111 CALL_DEBUG_ENTER;
112 std::lock_guard<std::mutex> lock(stateMutex_);
113 return callState_;
114 }
115
SetHasHandleRingMute(bool hasHandleRingMute)116 void DeviceEventMonitor::SetHasHandleRingMute(bool hasHandleRingMute)
117 {
118 CALL_INFO_TRACE;
119 hasHandleRingMute_ = hasHandleRingMute;
120 }
121
GetHasHandleRingMute()122 bool DeviceEventMonitor::GetHasHandleRingMute()
123 {
124 CALL_INFO_TRACE;
125 return hasHandleRingMute_;
126 }
127 } // namespace MMI
128 } // namespace OHOS