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
26 namespace OHOS {
27 namespace Notification {
SystemEventObserver(const ISystemEvent & callbacks)28 SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callbacks_(callbacks)
29 {
30 EventFwk::MatchingSkills matchingSkills;
31 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
32 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
33 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
34 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
35 #endif
36 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
37 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
38 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
39 #endif
40 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
41 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
42 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED);
43 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
44 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
45 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
46 EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
47 commonEventSubscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
48
49 subscriber_ = std::make_shared<SystemEventSubscriber>(
50 commonEventSubscribeInfo, std::bind(&SystemEventObserver::OnReceiveEvent, this, std::placeholders::_1));
51
52 EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
53 }
54
~SystemEventObserver()55 SystemEventObserver::~SystemEventObserver()
56 {
57 EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
58 }
59
GetBundleOption(AAFwk::Want want)60 sptr<NotificationBundleOption> SystemEventObserver::GetBundleOption(AAFwk::Want want)
61 {
62 auto element = want.GetElement();
63 std::string bundleName = element.GetBundleName();
64 int32_t appIndex = want.GetIntParam("appIndex", -1);
65 int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
66 sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
67 if (bundleOption == nullptr) {
68 ANS_LOGE("Failed to create bundleOption.");
69 return nullptr;
70 }
71 bundleOption->SetAppIndex(appIndex);
72 return bundleOption;
73 }
74
GetBundleOptionDataCleared(AAFwk::Want want)75 sptr<NotificationBundleOption> SystemEventObserver::GetBundleOptionDataCleared(AAFwk::Want want)
76 {
77 auto element = want.GetElement();
78 std::string bundleName = element.GetBundleName();
79 int32_t appIndex = want.GetIntParam("appIndex", -1);
80 int32_t uid = want.GetIntParam("ohos.aafwk.param.targetUid", -1);
81 sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
82 if (bundleOption == nullptr) {
83 ANS_LOGE("Failed to create bundleOption.");
84 return nullptr;
85 }
86 bundleOption->SetAppIndex(appIndex);
87 return bundleOption;
88 }
89
OnReceiveEvent(const EventFwk::CommonEventData & data)90 void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data)
91 {
92 auto want = data.GetWant();
93 std::string action = want.GetAction();
94 ANS_LOGD("OnReceiveEvent action is %{public}s.", action.c_str());
95 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
96 if (callbacks_.onBundleRemoved != nullptr) {
97 sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
98 if (bundleOption != nullptr) {
99 callbacks_.onBundleRemoved(bundleOption);
100 }
101 }
102 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
103 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
104 if (callbacks_.onScreenOn != nullptr) {
105 callbacks_.onScreenOn();
106 }
107 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
108 if (callbacks_.onScreenOff != nullptr) {
109 callbacks_.onScreenOff();
110 }
111 #endif
112 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
113 int32_t userId = data.GetCode();
114 if (userId <= SUBSCRIBE_USER_INIT) {
115 ANS_LOGE("Illegal userId, userId[%{public}d].", userId);
116 return;
117 }
118 NotificationPreferences::GetInstance()->InitSettingFromDisturbDB(userId);
119 AdvancedNotificationService::GetInstance()->RecoverLiveViewFromDb(userId);
120 NotificationCloneManager::GetInstance().OnUserSwitch(userId);
121 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
122 int32_t userId = data.GetCode();
123 if (userId <= SUBSCRIBE_USER_INIT) {
124 ANS_LOGE("Illegal userId, userId[%{public}d].", userId);
125 return;
126 }
127 if (callbacks_.onResourceRemove != nullptr) {
128 callbacks_.onResourceRemove(userId);
129 }
130 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
131 if (callbacks_.onBundleDataCleared != nullptr) {
132 sptr<NotificationBundleOption> bundleOption = GetBundleOptionDataCleared(want);
133 if (bundleOption != nullptr) {
134 callbacks_.onBundleDataCleared(bundleOption);
135 }
136 }
137 } else {
138 OnReceiveEventInner(data);
139 }
140 }
141
OnReceiveEventInner(const EventFwk::CommonEventData & data)142 void SystemEventObserver::OnReceiveEventInner(const EventFwk::CommonEventData &data)
143 {
144 std::string action = data.GetWant().GetAction();
145 if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) == 0) {
146 return OnBundleAddEventInner(data);
147 }
148 if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) == 0) {
149 return OnBundleUpdateEventInner(data);
150 }
151 if (action.compare(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) == 0) {
152 return OnBootSystemCompletedEventInner(data);
153 }
154 }
155
OnBundleAddEventInner(const EventFwk::CommonEventData & data)156 void SystemEventObserver::OnBundleAddEventInner(const EventFwk::CommonEventData &data)
157 {
158 if (callbacks_.onBundleAdd != nullptr) {
159 sptr<NotificationBundleOption> bundleOption = GetBundleOption(data.GetWant());
160 if (bundleOption != nullptr) {
161 callbacks_.onBundleAdd(bundleOption);
162 }
163 }
164 }
165
OnBundleUpdateEventInner(const EventFwk::CommonEventData & data)166 void SystemEventObserver::OnBundleUpdateEventInner(const EventFwk::CommonEventData &data)
167 {
168 if (callbacks_.onBundleUpdate != nullptr) {
169 sptr<NotificationBundleOption> bundleOption = GetBundleOption(data.GetWant());
170 if (bundleOption != nullptr) {
171 callbacks_.onBundleUpdate(bundleOption);
172 }
173 }
174 }
175
OnBootSystemCompletedEventInner(const EventFwk::CommonEventData & data)176 void SystemEventObserver::OnBootSystemCompletedEventInner(const EventFwk::CommonEventData &data)
177 {
178 if (callbacks_.onBootSystemCompleted != nullptr) {
179 callbacks_.onBootSystemCompleted();
180 }
181 }
182 } // namespace Notification
183 } // namespace OHOS
184