1 /*
2 * Copyright (c) 2021-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 "reminder_event_manager.h"
17
18 #include "ans_log_wrapper.h"
19 #include "bundle_constants.h"
20 #include "bundle_mgr_interface.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "bundle_manager_helper.h"
24 #include "if_system_ability_manager.h"
25 #include "ipc_skeleton.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28 #include "notification_helper.h"
29 #include "string_ex.h"
30
31 using namespace OHOS::EventFwk;
32 namespace OHOS {
33 namespace Notification {
34 static const std::string NOTIFICATION_LABEL = "REMINDER_AGENT";
35 std::shared_ptr<ReminderEventManager::ReminderNotificationSubscriber> ReminderEventManager::subscriber_
36 = nullptr;
37
ReminderEventManager(std::shared_ptr<ReminderDataManager> & reminderDataManager)38 ReminderEventManager::ReminderEventManager(std::shared_ptr<ReminderDataManager> &reminderDataManager)
39 {
40 init(reminderDataManager);
41 }
42
init(std::shared_ptr<ReminderDataManager> & reminderDataManager) const43 void ReminderEventManager::init(std::shared_ptr<ReminderDataManager> &reminderDataManager) const
44 {
45 MatchingSkills customMatchingSkills;
46 customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT);
47 customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT);
48 customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT);
49 customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT);
50 customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION);
51 customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT);
52 customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLICK_ALERT);
53 CommonEventSubscribeInfo customSubscriberInfo(customMatchingSkills);
54 customSubscriberInfo.SetPermission("ohos.permission.GRANT_SENSITIVE_PERMISSIONS");
55 customSubscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
56 auto customSubscriber = std::make_shared<ReminderEventCustomSubscriber>(customSubscriberInfo, reminderDataManager);
57
58 MatchingSkills matchingSkills;
59 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
60 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
61 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED);
62 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED);
63 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
64 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED);
65 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
66 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
67 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
68 subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
69 auto subscriber = std::make_shared<ReminderEventSubscriber>(subscriberInfo, reminderDataManager);
70
71 std::string identity = IPCSkeleton::ResetCallingIdentity();
72 if (CommonEventManager::SubscribeCommonEvent(subscriber) &&
73 CommonEventManager::SubscribeCommonEvent(customSubscriber)) {
74 ANSR_LOGD("SubscribeCommonEvent ok");
75 } else {
76 ANSR_LOGD("SubscribeCommonEvent fail");
77 }
78 IPCSkeleton::SetCallingIdentity(identity);
79
80 subscriber_ = std::make_shared<ReminderNotificationSubscriber>(reminderDataManager);
81 if (NotificationHelper::SubscribeNotification(*subscriber_) != ERR_OK) {
82 ANSR_LOGD("SubscribeNotification failed");
83 }
84
85 SubscribeSystemAbility(reminderDataManager);
86 }
87
SubscribeSystemAbility(std::shared_ptr<ReminderDataManager> & reminderDataManager) const88 void ReminderEventManager::SubscribeSystemAbility(std::shared_ptr<ReminderDataManager> &reminderDataManager) const
89 {
90 sptr<SystemAbilityStatusChangeListener> statusChangeListener
91 = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager);
92 if (statusChangeListener == nullptr) {
93 ANSR_LOGE("Failed to create statusChangeListener due to no memory.");
94 return;
95 }
96 // app mgr
97 sptr<SystemAbilityStatusChangeListener> appMgrStatusChangeListener
98 = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager);
99 if (appMgrStatusChangeListener == nullptr) {
100 ANSR_LOGE("Failed to create appMgrStatusChangeListener due to no memory.");
101 return;
102 }
103 // ability mgr
104 sptr<SystemAbilityStatusChangeListener> abilityMgrStatusListener
105 = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager);
106 if (abilityMgrStatusListener == nullptr) {
107 ANSR_LOGE("Failed to create abilityMgrStatusListener due to no memory.");
108 return;
109 }
110
111 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
112 if (samgrProxy == nullptr) {
113 ANSR_LOGD("samgrProxy is null");
114 return;
115 }
116 int32_t ret = samgrProxy->SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, statusChangeListener);
117 if (ret != ERR_OK) {
118 ANSR_LOGE("subscribe system ability id: %{public}d failed", BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
119 }
120 ret = samgrProxy->SubscribeSystemAbility(APP_MGR_SERVICE_ID, appMgrStatusChangeListener);
121 if (ret != ERR_OK) {
122 ANSR_LOGE("subscribe system ability id: %{public}d failed", APP_MGR_SERVICE_ID);
123 }
124 ret = samgrProxy->SubscribeSystemAbility(ABILITY_MGR_SERVICE_ID, abilityMgrStatusListener);
125 if (ret != ERR_OK) {
126 ANSR_LOGE("subscribe system ability id: %{public}d failed", ABILITY_MGR_SERVICE_ID);
127 }
128 }
129
ReminderEventSubscriber(const CommonEventSubscribeInfo & subscriberInfo,std::shared_ptr<ReminderDataManager> & reminderDataManager)130 ReminderEventManager::ReminderEventSubscriber::ReminderEventSubscriber(
131 const CommonEventSubscribeInfo &subscriberInfo,
132 std::shared_ptr<ReminderDataManager> &reminderDataManager) : CommonEventSubscriber(subscriberInfo)
133 {
134 reminderDataManager_ = reminderDataManager;
135 }
136
ReminderEventCustomSubscriber(const CommonEventSubscribeInfo & subscriberInfo,std::shared_ptr<ReminderDataManager> & reminderDataManager)137 ReminderEventManager::ReminderEventCustomSubscriber::ReminderEventCustomSubscriber(
138 const CommonEventSubscribeInfo &subscriberInfo,
139 std::shared_ptr<ReminderDataManager> &reminderDataManager) : CommonEventSubscriber(subscriberInfo)
140 {
141 reminderDataManager_ = reminderDataManager;
142 }
143
OnReceiveEvent(const EventFwk::CommonEventData & data)144 void ReminderEventManager::ReminderEventCustomSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
145 {
146 Want want = data.GetWant();
147 std::string action = want.GetAction();
148 ANSR_LOGI("Recieved common event:%{public}s", action.c_str());
149 if (action == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) {
150 reminderDataManager_->ShowActiveReminder(want);
151 return;
152 }
153 if (action == ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT) {
154 reminderDataManager_->TerminateAlerting(want);
155 return;
156 }
157 if (action == ReminderRequest::REMINDER_EVENT_CLOSE_ALERT) {
158 reminderDataManager_->CloseReminder(want, true);
159 return;
160 }
161 if (action == ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT) {
162 reminderDataManager_->SnoozeReminder(want);
163 return;
164 }
165 if (action == ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT) {
166 reminderDataManager_->HandleCustomButtonClick(want);
167 return;
168 }
169 if (action == ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION) {
170 reminderDataManager_->CloseReminder(want, false, false);
171 return;
172 }
173 if (action == ReminderRequest::REMINDER_EVENT_CLICK_ALERT) {
174 reminderDataManager_->ClickReminder(want);
175 return;
176 }
177 }
178
OnReceiveEvent(const EventFwk::CommonEventData & data)179 void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
180 {
181 Want want = data.GetWant();
182 std::string action = want.GetAction();
183 ANSR_LOGD("Recieved common event:%{public}s", action.c_str());
184 if (action == CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) {
185 reminderDataManager_->Init(true);
186 return;
187 }
188 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
189 HandlePackageRemove(want);
190 return;
191 }
192 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
193 HandlePackageRemove(want);
194 return;
195 }
196 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED) {
197 HandleProcessDied(want);
198 return;
199 }
200 if (action == CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) {
201 reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::TIME_ZONE_CHANGE);
202 return;
203 }
204 if (action == CommonEventSupport::COMMON_EVENT_TIME_CHANGED) {
205 reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE);
206 return;
207 }
208 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
209 reminderDataManager_->OnUserSwitch(data.GetCode());
210 return;
211 }
212 if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
213 reminderDataManager_->OnUserRemove(data.GetCode());
214 return;
215 }
216 }
217
HandlePackageRemove(const EventFwk::Want & want) const218 void ReminderEventManager::ReminderEventSubscriber::HandlePackageRemove(const EventFwk::Want &want) const
219 {
220 OHOS::AppExecFwk::ElementName ele = want.GetElement();
221 std::string bundleName = ele.GetBundleName();
222 int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1);
223 int32_t uid = want.GetIntParam(OHOS::AppExecFwk::Constants::UID, -1);
224 reminderDataManager_->CancelAllReminders(bundleName, userId, uid);
225 }
226
HandleProcessDied(const EventFwk::Want & want) const227 void ReminderEventManager::ReminderEventSubscriber::HandleProcessDied(const EventFwk::Want &want) const
228 {
229 sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
230 if (bundleOption == nullptr) {
231 ANSR_LOGE("Get bundle option error.");
232 return;
233 }
234 reminderDataManager_->OnProcessDiedLocked(bundleOption);
235 }
236
GetBundleOption(const OHOS::EventFwk::Want & want) const237 sptr<NotificationBundleOption> ReminderEventManager::ReminderEventSubscriber::GetBundleOption(
238 const OHOS::EventFwk::Want &want) const
239 {
240 OHOS::AppExecFwk::ElementName ele = want.GetElement();
241 std::string bundleName = ele.GetBundleName();
242 int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1);
243 int32_t uid = BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, userId);
244 ANSR_LOGD("bundleName=%{public}s, userId=%{private}d, uid=%{public}d", bundleName.c_str(), userId, uid);
245 sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
246 if (bundleOption == nullptr) {
247 ANSR_LOGE("new NotificationBundleOption fail due to no memory.");
248 }
249 return bundleOption;
250 }
251
SystemAbilityStatusChangeListener(std::shared_ptr<ReminderDataManager> & reminderDataManager)252 ReminderEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
253 std::shared_ptr<ReminderDataManager> &reminderDataManager)
254 {
255 reminderDataManager_ = reminderDataManager;
256 }
257
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)258 void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
259 int32_t systemAbilityId, const std::string& deviceId)
260 {
261 ANSR_LOGD("OnAddSystemAbilityInner");
262 switch (systemAbilityId) {
263 case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
264 ANSR_LOGD("OnAddSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY");
265 reminderDataManager_->OnBundleMgrServiceStart();
266 break;
267 case APP_MGR_SERVICE_ID:
268 ANSR_LOGD("OnAddSystemAbilityInner: APP_MGR_SERVICE");
269 break;
270 case ABILITY_MGR_SERVICE_ID:
271 ANSR_LOGD("OnAddSystemAbilityInner ABILITY_MGR_SERVICE_ID");
272 reminderDataManager_->OnAbilityMgrServiceStart();
273 break;
274 default:
275 break;
276 }
277 }
278
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)279 void ReminderEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
280 int32_t systemAbilityId, const std::string& deviceId)
281 {
282 ANSR_LOGD("OnRemoveSystemAbilityInner");
283 switch (systemAbilityId) {
284 case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
285 ANSR_LOGD("OnRemoveSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY");
286 break;
287 case APP_MGR_SERVICE_ID:
288 ANSR_LOGD("OnRemoveSystemAbilityInner: APP_MGR_SERVICE");
289 reminderDataManager_->OnRemoveAppMgr();
290 break;
291 case ABILITY_MGR_SERVICE_ID:
292 ANSR_LOGD("OnRemoveSystemAbilityInner ABILITY_MGR_SERVICE_ID");
293 break;
294 default:
295 break;
296 }
297 }
298
ReminderNotificationSubscriber(std::shared_ptr<ReminderDataManager> & reminderDataManager)299 ReminderEventManager::ReminderNotificationSubscriber::ReminderNotificationSubscriber(
300 std::shared_ptr<ReminderDataManager> &reminderDataManager)
301 {
302 reminderDataManager_ = reminderDataManager;
303 }
304
~ReminderNotificationSubscriber()305 ReminderEventManager::ReminderNotificationSubscriber::~ReminderNotificationSubscriber() {}
306
OnConnected()307 void ReminderEventManager::ReminderNotificationSubscriber::OnConnected() {}
308
OnDisconnected()309 void ReminderEventManager::ReminderNotificationSubscriber::OnDisconnected() {}
310
OnCanceled(const std::shared_ptr<Notification> & notification,const std::shared_ptr<NotificationSortingMap> & sortingMap,int deleteReason)311 void ReminderEventManager::ReminderNotificationSubscriber::OnCanceled(
312 const std::shared_ptr<Notification> ¬ification,
313 const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason)
314 {
315 // Note: Don't modify param notification
316 if (deleteReason != NotificationConstant::TRIGGER_AUTO_DELETE_REASON_DELETE) {
317 return;
318 }
319 if (notification == nullptr) {
320 return;
321 }
322 NotificationRequest request = notification->GetNotificationRequest();
323 std::string label = request.GetLabel();
324 int64_t autoDeletedTime = request.GetAutoDeletedTime();
325 if (autoDeletedTime <= 0 || label != NOTIFICATION_LABEL) {
326 return;
327 }
328
329 if (reminderDataManager_ == nullptr) {
330 return;
331 }
332 int32_t notificationId = request.GetNotificationId();
333 int32_t uid = request.GetOwnerUid() == 0 ? request.GetCreatorUid() : request.GetOwnerUid();
334 reminderDataManager_->HandleAutoDeleteReminder(notificationId, uid, autoDeletedTime);
335 }
336
OnConsumed(const std::shared_ptr<Notification> & notification,const std::shared_ptr<NotificationSortingMap> & sortingMap)337 void ReminderEventManager::ReminderNotificationSubscriber::OnConsumed(const std::shared_ptr<Notification> ¬ification,
338 const std::shared_ptr<NotificationSortingMap> &sortingMap) {}
339
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)340 void ReminderEventManager::ReminderNotificationSubscriber::OnUpdate(
341 const std::shared_ptr<NotificationSortingMap> &sortingMap) {}
342
OnDied()343 void ReminderEventManager::ReminderNotificationSubscriber::OnDied() {}
344
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)345 void ReminderEventManager::ReminderNotificationSubscriber::OnDoNotDisturbDateChange(
346 const std::shared_ptr<NotificationDoNotDisturbDate> &date) {}
347
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)348 void ReminderEventManager::ReminderNotificationSubscriber::OnEnabledNotificationChanged(
349 const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) {}
350
OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> & badgeData)351 void ReminderEventManager::ReminderNotificationSubscriber::OnBadgeChanged(
352 const std::shared_ptr<BadgeNumberCallbackData> &badgeData) {}
353
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)354 void ReminderEventManager::ReminderNotificationSubscriber::OnBadgeEnabledChanged(
355 const sptr<EnabledNotificationCallbackData> &callbackData) {}
356
OnBatchCanceled(const std::vector<std::shared_ptr<Notification>> & requestList,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)357 void ReminderEventManager::ReminderNotificationSubscriber::OnBatchCanceled(
358 const std::vector<std::shared_ptr<Notification>> &requestList,
359 const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) {}
360 } // namespace OHOS
361 } // namespace Notification
362