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 #include "mmi_log.h"
20 #include "want.h"
21
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "DeviceEventMonitor"
24
25 namespace OHOS {
26 namespace MMI {
27 const std::string SOS_PAGE_CHANGE_EVENTS = "emergencycommunication.event.SOS_EMERGENCY_CALL_ABILITY_PAGE_CHANGE";
DeviceEventMonitor()28 DeviceEventMonitor::DeviceEventMonitor() {}
~DeviceEventMonitor()29 DeviceEventMonitor::~DeviceEventMonitor() {}
30
31 class DeviceChangedReceiver : public EventFwk::CommonEventSubscriber {
32 public:
DeviceChangedReceiver(const OHOS::EventFwk::CommonEventSubscribeInfo & subscribeInfo)33 explicit DeviceChangedReceiver(const OHOS::EventFwk::CommonEventSubscribeInfo& subscribeInfo)
34 : OHOS::EventFwk::CommonEventSubscriber(subscribeInfo)
35 {
36 MMI_HILOGD("DeviceEventMonitor register");
37 }
38
39 virtual ~DeviceChangedReceiver() = default;
40 __attribute__((no_sanitize("cfi")))
41
OnReceiveEvent(const EventFwk::CommonEventData & eventData)42 void OnReceiveEvent(const EventFwk::CommonEventData &eventData)
43 {
44 CALL_DEBUG_ENTER;
45 std::string action = eventData.GetWant().GetAction();
46 if (action.empty()) {
47 MMI_HILOGE("action is empty");
48 return;
49 }
50 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED) {
51 int32_t callState = 0;
52 DEVICE_MONITOR->SetCallState(eventData, callState);
53 } else if (action == SOS_PAGE_CHANGE_EVENTS) {
54 MMI_HILOGD("Display emergency call page change");
55 std::string pageName = eventData.GetWant().GetStringParam("pageName");
56 if (pageName.empty()) {
57 MMI_HILOGE("StringParam is empty");
58 return;
59 }
60 auto eventKeyCommandHandler = InputHandler->GetKeyCommandHandler();
61 int32_t ret = eventKeyCommandHandler->SetIsFreezePowerKey(pageName);
62 if (ret != RET_OK) {
63 MMI_HILOGE("SetIsFreezePowerKey is failed in key command:%{public}d", ret);
64 return;
65 }
66 } else {
67 MMI_HILOGW("Device changed receiver event: unknown");
68 return;
69 }
70 }
71 };
72
InitCommonEventSubscriber()73 void DeviceEventMonitor::InitCommonEventSubscriber()
74 {
75 CALL_DEBUG_ENTER;
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("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