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 SoftBusScreenState screenState = SOFTBUS_SCREEN_UNKNOWN;
50 if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
51 screenState = SOFTBUS_SCREEN_OFF;
52 } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
53 screenState = SOFTBUS_SCREEN_ON;
54 } else if (action == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
55 LnnNotifyScreenLockStateChangeEvent(SOFTBUS_USER_UNLOCK);
56 } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) {
57 LnnNotifyScreenLockStateChangeEvent(SOFTBUS_SCREEN_UNLOCK);
58 } else if (action == CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY) {
59 LnnNotifyDataShareStateChangeEvent(SOFTBUS_DATA_SHARE_READY);
60 }
61 if (screenState != SOFTBUS_SCREEN_UNKNOWN) {
62 LnnNotifyScreenStateChangeEvent(screenState);
63 }
64
65 SoftBusAccountState state = SOFTBUS_ACCOUNT_UNKNOWN;
66
67 if (action == CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT ||
68 action == CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOFF) {
69 const AAFwk::WantParams &wantParams = data.GetWant().GetParams();
70 int32_t eventUserId = -1;
71 int32_t activeUserId = GetActiveOsAccountIds();
72 std::string userIdKey = "userId";
73 eventUserId = wantParams.GetIntParam(userIdKey, -1);
74 LNN_LOGI(LNN_EVENT, "activeUserId=%{public}d, eventUserId=%{public}d", activeUserId, eventUserId);
75 if (eventUserId == activeUserId) {
76 state = SOFTBUS_ACCOUNT_LOG_OUT;
77 }
78 }
79 if (state != SOFTBUS_ACCOUNT_UNKNOWN) {
80 LnnNotifyAccountStateChangeEvent(state);
81 }
82
83 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
84 LnnNotifyUserSwitchEvent(SOFTBUS_USER_SWITCHED);
85 }
86 }
87
88 class SubscribeEvent {
89 public:
90 int32_t SubscribeCommonEvent();
91 private:
92 std::shared_ptr<CommonEventMonitor> subscriber_ = nullptr;
93 };
94
SubscribeCommonEvent()95 int32_t SubscribeEvent::SubscribeCommonEvent()
96 {
97 MatchingSkills matchingSkills;
98 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
99 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
100 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT);
101 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOFF);
102 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
103 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
104 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
105 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY);
106 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
107 subscriber_ = std::make_shared<CommonEventMonitor>(subscriberInfo);
108 if (!CommonEventManager::SubscribeCommonEvent(subscriber_)) {
109 LNN_LOGE(LNN_EVENT, "subscribe common event err");
110 return SOFTBUS_NETWORK_SUBSCRIBE_COMMON_EVENT_FAILED;
111 }
112 return SOFTBUS_OK;
113 }
114 } // namespace EventFwk
115 } // namespace OHOS
116
LnnQueryLocalScreenStatusOnce(bool notify)117 bool LnnQueryLocalScreenStatusOnce(bool notify)
118 {
119 bool isScreenOn = OHOS::PowerMgr::PowerMgrClient::GetInstance().IsScreenOn(true);
120 LNN_LOGI(LNN_EVENT, "query screen status is %{public}s", isScreenOn ? "on" : "off");
121 if (notify) {
122 SoftBusScreenState screenState = isScreenOn ? SOFTBUS_SCREEN_ON : SOFTBUS_SCREEN_OFF;
123 LnnNotifyScreenStateChangeEvent(screenState);
124 }
125 return isScreenOn;
126 }
127
LnnSubscribeCommonEvent(void)128 int32_t LnnSubscribeCommonEvent(void)
129 {
130 OHOS::EventFwk::SubscribeEvent *subscriberPtr = new OHOS::EventFwk::SubscribeEvent();
131 if (subscriberPtr == nullptr) {
132 LNN_LOGE(LNN_EVENT, "SubscribeEvent init fail");
133 return SOFTBUS_MEM_ERR;
134 }
135 if (subscriberPtr->SubscribeCommonEvent() != SOFTBUS_OK) {
136 delete subscriberPtr;
137 LNN_LOGE(LNN_EVENT, "subscribe common event fail");
138 return SOFTBUS_NETWORK_SUBSCRIBE_COMMON_EVENT_FAILED;
139 }
140 LNN_LOGI(LNN_EVENT, "subscribe common event success");
141 LnnUpdateOhosAccount(UPDATE_HEARTBEAT);
142 if (!LnnIsDefaultOhosAccount()) {
143 LnnNotifyAccountStateChangeEvent(SOFTBUS_ACCOUNT_LOG_IN);
144 }
145 (void)LnnQueryLocalScreenStatusOnce(true);
146 delete subscriberPtr;
147 return SOFTBUS_OK;
148 }
149
LnnInitCommonEventMonitorImpl(void)150 int32_t LnnInitCommonEventMonitorImpl(void)
151 {
152 int32_t ret = LnnInitModuleNotifyWithRetryAsync(INIT_DEPS_SCREEN_STATUS, LnnSubscribeCommonEvent, RETRY_MAX,
153 DELAY_LEN, false);
154 if (ret != SOFTBUS_OK) {
155 LNN_LOGE(LNN_INIT, "LnnAsyncCallbackHelper fail");
156 }
157 return ret;
158 }
159