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 "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "lnn_async_callback_utils.h"
21 #include "lnn_log.h"
22 #include "lnn_ohos_account.h"
23 #include "power_mgr_client.h"
24 #include "softbus_error_code.h"
25 #include "lnn_init_monitor.h"
26
27 static const int32_t DELAY_LEN = 1000;
28 static const int32_t RETRY_MAX = 20;
29
30 namespace OHOS {
31 namespace EventFwk {
32 class CommonEventMonitor : public CommonEventSubscriber {
33 public:
34 explicit CommonEventMonitor(const CommonEventSubscribeInfo &subscriberInfo);
~CommonEventMonitor()35 virtual ~CommonEventMonitor() {}
36 virtual void OnReceiveEvent(const CommonEventData &data);
37 };
38
CommonEventMonitor(const CommonEventSubscribeInfo & subscriberInfo)39 CommonEventMonitor::CommonEventMonitor(const CommonEventSubscribeInfo &subscriberInfo)
40 :CommonEventSubscriber(subscriberInfo)
41 {
42 }
43
OnReceiveEvent(const CommonEventData & data)44 void CommonEventMonitor::OnReceiveEvent(const CommonEventData &data)
45 {
46 std::string action = data.GetWant().GetAction();
47 LNN_LOGI(LNN_EVENT, "notify common event=%{public}s", action.c_str());
48
49 if (action == CommonEventSupport::COMMON_EVENT_TIME_CHANGED) {
50 LnnNotifySysTimeChangeEvent();
51 }
52 if (action == CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) {
53 LnnNotifyDeviceRootStateChangeEvent();
54 }
55
56 SoftBusScreenState screenState = SOFTBUS_SCREEN_UNKNOWN;
57 if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
58 screenState = SOFTBUS_SCREEN_OFF;
59 } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
60 screenState = SOFTBUS_SCREEN_ON;
61 } else if (action == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
62 LnnNotifyScreenLockStateChangeEvent(SOFTBUS_USER_UNLOCK);
63 } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
64 LnnNotifyScreenLockStateChangeEvent(SOFTBUS_SCREEN_UNLOCK);
65 } else if (action == CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY) {
66 LnnNotifyDataShareStateChangeEvent(SOFTBUS_DATA_SHARE_READY);
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 const AAFwk::WantParams &wantParams = data.GetWant().GetParams();
77 int32_t eventUserId = -1;
78 int32_t activeUserId = GetActiveOsAccountIds();
79 std::string userIdKey = "userId";
80 eventUserId = wantParams.GetIntParam(userIdKey, -1);
81 LNN_LOGI(LNN_EVENT, "activeUserId=%{public}d, eventUserId=%{public}d", activeUserId, eventUserId);
82 if (eventUserId == activeUserId) {
83 state = SOFTBUS_ACCOUNT_LOG_OUT;
84 }
85 }
86 if (state != SOFTBUS_ACCOUNT_UNKNOWN) {
87 LnnNotifyAccountStateChangeEvent(state);
88 }
89
90 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
91 LnnNotifyUserSwitchEvent(SOFTBUS_USER_SWITCHED);
92 }
93 }
94
95 class SubscribeEvent {
96 public:
97 int32_t SubscribeCommonEvent();
98 private:
99 std::shared_ptr<CommonEventMonitor> subscriber_ = nullptr;
100 };
101
SubscribeCommonEvent()102 int32_t SubscribeEvent::SubscribeCommonEvent()
103 {
104 MatchingSkills matchingSkills;
105 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
106 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
107 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT);
108 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOFF);
109 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
110 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
111 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
112 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY);
113 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED);
114 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
115 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
116 subscriber_ = std::make_shared<CommonEventMonitor>(subscriberInfo);
117 if (!CommonEventManager::SubscribeCommonEvent(subscriber_)) {
118 LNN_LOGE(LNN_EVENT, "subscribe common event err");
119 return SOFTBUS_NETWORK_SUBSCRIBE_COMMON_EVENT_FAILED;
120 }
121 return SOFTBUS_OK;
122 }
123 } // namespace EventFwk
124 } // namespace OHOS
125
LnnQueryLocalScreenStatusOnce(bool notify)126 bool LnnQueryLocalScreenStatusOnce(bool notify)
127 {
128 bool isScreenOn = OHOS::PowerMgr::PowerMgrClient::GetInstance().IsScreenOn(true);
129 LNN_LOGI(LNN_EVENT, "query screen status is %{public}s", isScreenOn ? "on" : "off");
130 if (notify) {
131 SoftBusScreenState screenState = isScreenOn ? SOFTBUS_SCREEN_ON : SOFTBUS_SCREEN_OFF;
132 LnnNotifyScreenStateChangeEvent(screenState);
133 }
134 return isScreenOn;
135 }
136
LnnSubscribeCommonEvent(void)137 int32_t LnnSubscribeCommonEvent(void)
138 {
139 OHOS::EventFwk::SubscribeEvent *subscriberPtr = new OHOS::EventFwk::SubscribeEvent();
140 if (subscriberPtr == nullptr) {
141 LNN_LOGE(LNN_EVENT, "SubscribeEvent init fail");
142 return SOFTBUS_MEM_ERR;
143 }
144 if (subscriberPtr->SubscribeCommonEvent() != SOFTBUS_OK) {
145 delete subscriberPtr;
146 LNN_LOGE(LNN_EVENT, "subscribe common event fail");
147 return SOFTBUS_NETWORK_SUBSCRIBE_COMMON_EVENT_FAILED;
148 }
149 LNN_LOGI(LNN_EVENT, "subscribe common event success");
150 LnnUpdateOhosAccount(UPDATE_HEARTBEAT);
151 if (!LnnIsDefaultOhosAccount()) {
152 LnnNotifyAccountStateChangeEvent(SOFTBUS_ACCOUNT_LOG_IN);
153 }
154 (void)LnnQueryLocalScreenStatusOnce(true);
155 delete subscriberPtr;
156 return SOFTBUS_OK;
157 }
158
LnnInitCommonEventMonitorImpl(void)159 int32_t LnnInitCommonEventMonitorImpl(void)
160 {
161 int32_t ret = LnnInitModuleNotifyWithRetryAsync(INIT_DEPS_SCREEN_STATUS, LnnSubscribeCommonEvent, RETRY_MAX,
162 DELAY_LEN, false);
163 if (ret != SOFTBUS_OK) {
164 LNN_LOGE(LNN_INIT, "LnnAsyncCallbackHelper fail");
165 }
166 return ret;
167 }
168