1 /*
2 * Copyright (c) 2021-2023 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 "system_event_observer.h"
17
18 #include "advanced_notification_service.h"
19 #include "ans_const_define.h"
20 #include "bundle_constants.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "notification_preferences.h"
24 #include "notification_clone_manager.h"
25 #ifdef ALL_SCENARIO_COLLABORATION
26 #include "distributed_device_manager.h"
27 #endif
28
29 namespace OHOS {
30 namespace Notification {
SystemEventObserver(const ISystemEvent & callbacks)31 SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callbacks_(callbacks)
32 {
33 EventFwk::MatchingSkills matchingSkills;
34 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
35 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
36 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
37 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
38 #endif
39 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
40 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
41 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
42 #endif
43 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
44 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
45 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED);
46 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED);
47 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
48 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
49 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
50 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_RESTORE_START);
51 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_KIOSK_MODE_ON);
52 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_KIOSK_MODE_OFF);
53 EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
54 commonEventSubscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
55
56 subscriber_ = std::make_shared<SystemEventSubscriber>(
57 commonEventSubscribeInfo, std::bind(&SystemEventObserver::OnReceiveEvent, this, std::placeholders::_1));
58
59 EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
60 }
61
~SystemEventObserver()62 SystemEventObserver::~SystemEventObserver()
63 {
64 EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
65 }
66
GetBundleOption(AAFwk::Want want)67 sptr<NotificationBundleOption> SystemEventObserver::GetBundleOption(AAFwk::Want want)
68 {
69 auto element = want.GetElement();
70 std::string bundleName = element.GetBundleName();
71 int32_t appIndex = want.GetIntParam("appIndex", -1);
72 int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
73 sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
74 if (bundleOption == nullptr) {
75 ANS_LOGE("Failed to create bundleOption.");
76 return nullptr;
77 }
78 bundleOption->SetAppIndex(appIndex);
79 return bundleOption;
80 }
81
GetBundleOptionDataCleared(AAFwk::Want want)82 sptr<NotificationBundleOption> SystemEventObserver::GetBundleOptionDataCleared(AAFwk::Want want)
83 {
84 auto element = want.GetElement();
85 std::string bundleName = element.GetBundleName();
86 int32_t appIndex = want.GetIntParam("appIndex", -1);
87 // 元能力提供的UID,该UID获取的是want信息中bundleName对应的UID。
88 int32_t uid = want.GetIntParam("ohos.aafwk.param.targetUid", -1);
89 sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
90 if (bundleOption == nullptr) {
91 ANS_LOGE("Failed to create bundleOption.");
92 return nullptr;
93 }
94 bundleOption->SetAppIndex(appIndex);
95 return bundleOption;
96 }
97
OnReceiveEvent(const EventFwk::CommonEventData & data)98 void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data)
99 {
100 auto want = data.GetWant();
101 std::string action = want.GetAction();
102 ANS_LOGD("OnReceiveEvent action is %{public}s.", action.c_str());
103 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
104 if (callbacks_.onBundleRemoved != nullptr) {
105 sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
106 if (bundleOption != nullptr) {
107 callbacks_.onBundleRemoved(bundleOption);
108 }
109 }
110 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
111 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
112 if (callbacks_.onScreenOn != nullptr) {
113 callbacks_.onScreenOn();
114 }
115 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
116 if (callbacks_.onScreenOff != nullptr) {
117 callbacks_.onScreenOff();
118 }
119 #endif
120 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
121 int32_t userId = data.GetCode();
122 if (userId <= SUBSCRIBE_USER_INIT) {
123 ANS_LOGE("Illegal userId, userId[%{public}d].", userId);
124 return;
125 }
126
127 NotificationPreferences::GetInstance()->InitSettingFromDisturbDB(userId);
128 AdvancedNotificationService::GetInstance()->RecoverLiveViewFromDb(userId);
129 NotificationCloneManager::GetInstance().OnUserSwitch(userId);
130 #ifdef ALL_SCENARIO_COLLABORATION
131 DistributedDeviceManager::GetInstance().InitTrustList();
132 #endif
133 return;
134 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
135 int32_t userId = data.GetCode();
136 if (userId <= SUBSCRIBE_USER_INIT) {
137 ANS_LOGE("Illegal userId, userId[%{public}d].", userId);
138 return;
139 }
140 if (callbacks_.onResourceRemove != nullptr) {
141 callbacks_.onResourceRemove(userId);
142 }
143 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED) {
144 int32_t userId = data.GetCode();
145 if (userId <= SUBSCRIBE_USER_INIT) {
146 ANS_LOGE("Illegal userId, userId[%{public}d].", userId);
147 return;
148 }
149 if (callbacks_.OnUserStopped != nullptr) {
150 callbacks_.OnUserStopped(userId);
151 }
152 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
153 if (callbacks_.onBundleDataCleared != nullptr) {
154 sptr<NotificationBundleOption> bundleOption = GetBundleOptionDataCleared(want);
155 if (bundleOption != nullptr) {
156 callbacks_.onBundleDataCleared(bundleOption);
157 }
158 }
159 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_RESTORE_START) {
160 NotificationCloneManager::GetInstance().OnRestoreStart(want);
161 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_KIOSK_MODE_ON) {
162 NotificationPreferences::GetInstance()->SetKioskModeStatus(true);
163 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_KIOSK_MODE_OFF) {
164 NotificationPreferences::GetInstance()->SetKioskModeStatus(false);
165 } else {
166 OnReceiveEventInner(data);
167 }
168 }
169
OnReceiveEventInner(const EventFwk::CommonEventData & data)170 void SystemEventObserver::OnReceiveEventInner(const EventFwk::CommonEventData &data)
171 {
172 std::string action = data.GetWant().GetAction();
173 if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) == 0) {
174 return OnBundleAddEventInner(data);
175 }
176 if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) == 0) {
177 return OnBundleUpdateEventInner(data);
178 }
179 if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) == 0) {
180 return OnBootSystemCompletedEventInner(data);
181 }
182 }
183
OnBundleAddEventInner(const EventFwk::CommonEventData & data)184 void SystemEventObserver::OnBundleAddEventInner(const EventFwk::CommonEventData &data)
185 {
186 if (callbacks_.onBundleAdd != nullptr) {
187 sptr<NotificationBundleOption> bundleOption = GetBundleOption(data.GetWant());
188 if (bundleOption != nullptr) {
189 callbacks_.onBundleAdd(bundleOption);
190 }
191 }
192 }
193
OnBundleUpdateEventInner(const EventFwk::CommonEventData & data)194 void SystemEventObserver::OnBundleUpdateEventInner(const EventFwk::CommonEventData &data)
195 {
196 if (callbacks_.onBundleUpdate != nullptr) {
197 sptr<NotificationBundleOption> bundleOption = GetBundleOption(data.GetWant());
198 if (bundleOption != nullptr) {
199 callbacks_.onBundleUpdate(bundleOption);
200 }
201 }
202 }
203
OnBootSystemCompletedEventInner(const EventFwk::CommonEventData & data)204 void SystemEventObserver::OnBootSystemCompletedEventInner(const EventFwk::CommonEventData &data)
205 {
206 if (callbacks_.onBootSystemCompleted != nullptr) {
207 callbacks_.onBootSystemCompleted();
208 }
209 }
210 } // namespace Notification
211 } // namespace OHOS
212