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