1 /*
2 * Copyright (c) 2022 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 "lnn_event_monitor_impl.h"
17
18 #include <securec.h>
19
20 #include "auth_interface.h"
21 #include "bus_center_event.h"
22 #include "common_event_data.h"
23 #include "common_event_manager.h"
24 #include "common_event_subscriber.h"
25 #include "common_event_support.h"
26 #include "lnn_async_callback_utils.h"
27 #include "lnn_log.h"
28 #include "lnn_ohos_account.h"
29 #include "lnn_heartbeat_strategy.h"
30 #include "want.h"
31
32 #include "power_mgr_client.h"
33 #include "softbus_adapter_mem.h"
34 #include "softbus_errcode.h"
35
36 static const int32_t DELAY_LEN = 1000;
37 static const int32_t RETRY_MAX = 20;
38
39 namespace OHOS {
40 namespace EventFwk {
41 class CommonEventMonitor : public CommonEventSubscriber {
42 public:
43 explicit CommonEventMonitor(const CommonEventSubscribeInfo &subscriberInfo);
~CommonEventMonitor()44 virtual ~CommonEventMonitor() {}
45 virtual void OnReceiveEvent(const CommonEventData &data);
46 };
47
CommonEventMonitor(const CommonEventSubscribeInfo & subscriberInfo)48 CommonEventMonitor::CommonEventMonitor(const CommonEventSubscribeInfo &subscriberInfo)
49 :CommonEventSubscriber(subscriberInfo)
50 {
51 }
52
OnReceiveEvent(const CommonEventData & data)53 void CommonEventMonitor::OnReceiveEvent(const CommonEventData &data)
54 {
55 std::string action = data.GetWant().GetAction();
56 LNN_LOGI(LNN_EVENT, "notify common event=%{public}s", action.c_str());
57
58 SoftBusScreenState screenState = SOFTBUS_SCREEN_UNKNOWN;
59 if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
60 screenState = SOFTBUS_SCREEN_OFF;
61 } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
62 screenState = SOFTBUS_SCREEN_ON;
63 } else if (action == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
64 LnnNotifyScreenLockStateChangeEvent(SOFTBUS_USER_UNLOCK);
65 } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
66 LnnNotifyScreenLockStateChangeEvent(SOFTBUS_SCREEN_UNLOCK);
67 }
68 if (screenState != SOFTBUS_SCREEN_UNKNOWN) {
69 LnnNotifyScreenStateChangeEvent(screenState);
70 }
71
72 SoftBusAccountState state = SOFTBUS_ACCOUNT_UNKNOWN;
73
74 if (action == CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT ||
75 action == CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOFF) {
76 state = SOFTBUS_ACCOUNT_LOG_OUT;
77 }
78 if (state != SOFTBUS_ACCOUNT_UNKNOWN) {
79 LnnNotifyAccountStateChangeEvent(state);
80 }
81
82 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
83 LnnNotifyUserSwitchEvent(SOFTBUS_USER_SWITCHED);
84 }
85 }
86
87 class SubscribeEvent {
88 public:
89 int32_t SubscribeCommonEvent();
90 private:
91 std::shared_ptr<CommonEventMonitor> subscriber_ = nullptr;
92 };
93
SubscribeCommonEvent()94 int32_t SubscribeEvent::SubscribeCommonEvent()
95 {
96 MatchingSkills matchingSkills;
97 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
98 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
99 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT);
100 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOFF);
101 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
102 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
103 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
104 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
105 subscriber_ = std::make_shared<CommonEventMonitor>(subscriberInfo);
106 if (!CommonEventManager::SubscribeCommonEvent(subscriber_)) {
107 LNN_LOGE(LNN_EVENT, "subscribe common event err");
108 return SOFTBUS_ERR;
109 }
110 return SOFTBUS_OK;
111 }
112 } // namespace EventFwk
113 } // namespace OHOS
114
LnnQueryLocalScreenStatusOnce(bool notify)115 bool LnnQueryLocalScreenStatusOnce(bool notify)
116 {
117 bool isScreenOn = OHOS::PowerMgr::PowerMgrClient::GetInstance().IsScreenOn(true);
118 LNN_LOGI(LNN_EVENT, "query screen status is %{public}s", isScreenOn ? "on" : "off");
119 if (notify) {
120 SoftBusScreenState screenState = isScreenOn ? SOFTBUS_SCREEN_ON : SOFTBUS_SCREEN_OFF;
121 LnnNotifyScreenStateChangeEvent(screenState);
122 }
123 return isScreenOn;
124 }
125
LnnSubscribeCommonEvent(void * para)126 static void LnnSubscribeCommonEvent(void *para)
127 {
128 (void)para;
129 static int32_t retry = 0;
130 if (retry > RETRY_MAX) {
131 LNN_LOGE(LNN_EVENT, "try subscribe common event max times");
132 return;
133 }
134 OHOS::EventFwk::SubscribeEvent *subscriberPtr = new OHOS::EventFwk::SubscribeEvent();
135 if (subscriberPtr == nullptr) {
136 LNN_LOGE(LNN_EVENT, "SubscribeEvent init fail");
137 return;
138 }
139 if (subscriberPtr->SubscribeCommonEvent() == SOFTBUS_OK) {
140 LNN_LOGI(LNN_EVENT, "subscribe common event success");
141 LnnUpdateOhosAccount(true);
142 if (!LnnIsDefaultOhosAccount()) {
143 LnnNotifyAccountStateChangeEvent(SOFTBUS_ACCOUNT_LOG_IN);
144 }
145 } else {
146 LNN_LOGE(LNN_EVENT, "subscribe common event fail");
147 retry++;
148 SoftBusLooper *looper = GetLooper(LOOP_TYPE_DEFAULT);
149 if (LnnAsyncCallbackDelayHelper(looper, LnnSubscribeCommonEvent, NULL, DELAY_LEN) != SOFTBUS_OK) {
150 LNN_LOGE(LNN_EVENT, "async call subscribe screen state fail");
151 }
152 }
153 delete subscriberPtr;
154 (void)LnnQueryLocalScreenStatusOnce(true);
155 }
156
LnnInitCommonEventMonitorImpl(void)157 int32_t LnnInitCommonEventMonitorImpl(void)
158 {
159 SoftBusLooper *looper = GetLooper(LOOP_TYPE_DEFAULT);
160 int32_t ret = LnnAsyncCallbackHelper(looper, LnnSubscribeCommonEvent, NULL);
161 if (ret != SOFTBUS_OK) {
162 LNN_LOGE(LNN_INIT, "LnnAsyncCallbackHelper fail");
163 }
164 return ret;
165 }
166