• 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 "if_system_ability_manager.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 
28 using namespace OHOS::EventFwk;
29 namespace OHOS {
30 namespace Notification {
ReminderEventManager(std::shared_ptr<ReminderDataManager> & reminderDataManager)31 ReminderEventManager::ReminderEventManager(std::shared_ptr<ReminderDataManager> &reminderDataManager)
32 {
33     init(reminderDataManager);
34 }
35 
init(std::shared_ptr<ReminderDataManager> & reminderDataManager) const36 void ReminderEventManager::init(std::shared_ptr<ReminderDataManager> &reminderDataManager) const
37 {
38     MatchingSkills customMatchingSkills;
39     customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALARM_ALERT);
40     customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT);
41     customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CLOSE_ALERT);
42     customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT);
43     customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION);
44     CommonEventSubscribeInfo customSubscriberInfo(customMatchingSkills);
45     customSubscriberInfo.SetPermission("ohos.permission.GRANT_SENSITIVE_PERMISSIONS");
46     auto customSubscriber = std::make_shared<ReminderEventCustomSubscriber>(customSubscriberInfo, reminderDataManager);
47 
48     MatchingSkills matchingSkills;
49     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
50     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
51     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED);
52     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED);
53     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
54     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED);
55     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
56     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
57     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
58     auto subscriber = std::make_shared<ReminderEventSubscriber>(subscriberInfo, reminderDataManager);
59 
60     std::string identity = IPCSkeleton::ResetCallingIdentity();
61     if (CommonEventManager::SubscribeCommonEvent(subscriber) &&
62         CommonEventManager::SubscribeCommonEvent(customSubscriber)) {
63         ANSR_LOGD("SubscribeCommonEvent ok");
64     } else {
65         ANSR_LOGD("SubscribeCommonEvent fail");
66     }
67     IPCSkeleton::SetCallingIdentity(identity);
68 
69     sptr<SystemAbilityStatusChangeListener> statusChangeListener
70         = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager);
71     if (statusChangeListener == nullptr) {
72         ANSR_LOGE("Failed to create statusChangeListener due to no memory.");
73         return;
74     }
75     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
76     if (samgrProxy == nullptr) {
77         ANSR_LOGD("samgrProxy is null");
78         return;
79     }
80     int32_t ret = samgrProxy->SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, statusChangeListener);
81     if (ret != ERR_OK) {
82         ANSR_LOGE("subscribe system ability id: %{public}d failed", BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
83     }
84 }
85 
ReminderEventSubscriber(const CommonEventSubscribeInfo & subscriberInfo,std::shared_ptr<ReminderDataManager> & reminderDataManager)86 ReminderEventManager::ReminderEventSubscriber::ReminderEventSubscriber(
87     const CommonEventSubscribeInfo &subscriberInfo,
88     std::shared_ptr<ReminderDataManager> &reminderDataManager) : CommonEventSubscriber(subscriberInfo)
89 {
90     reminderDataManager_ = reminderDataManager;
91 }
92 
ReminderEventCustomSubscriber(const CommonEventSubscribeInfo & subscriberInfo,std::shared_ptr<ReminderDataManager> & reminderDataManager)93 ReminderEventManager::ReminderEventCustomSubscriber::ReminderEventCustomSubscriber(
94     const CommonEventSubscribeInfo &subscriberInfo,
95     std::shared_ptr<ReminderDataManager> &reminderDataManager) : CommonEventSubscriber(subscriberInfo)
96 {
97     reminderDataManager_ = reminderDataManager;
98 }
99 
OnReceiveEvent(const EventFwk::CommonEventData & data)100 void ReminderEventManager::ReminderEventCustomSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
101 {
102     Want want = data.GetWant();
103     std::string action = want.GetAction();
104     ANSR_LOGI("Recieved common event:%{public}s", action.c_str());
105     if (action == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) {
106         reminderDataManager_->ShowActiveReminder(want);
107         return;
108     }
109     if (action == ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT) {
110         reminderDataManager_->TerminateAlerting(want);
111         return;
112     }
113     if (action == ReminderRequest::REMINDER_EVENT_CLOSE_ALERT) {
114         reminderDataManager_->CloseReminder(want, true);
115         return;
116     }
117     if (action == ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT) {
118         reminderDataManager_->SnoozeReminder(want);
119         return;
120     }
121     if (action == ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION) {
122         reminderDataManager_->CloseReminder(want, false);
123         return;
124     }
125 }
126 
OnReceiveEvent(const EventFwk::CommonEventData & data)127 void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
128 {
129     Want want = data.GetWant();
130     std::string action = want.GetAction();
131     ANSR_LOGI("Recieved common event:%{public}s", action.c_str());
132     if (action == CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) {
133         reminderDataManager_->Init(true);
134         return;
135     }
136     if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
137         HandlePackageRemove(want);
138         return;
139     }
140     if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
141         HandlePackageRemove(want);
142         return;
143     }
144     if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED) {
145         HandleProcessDied(want);
146         return;
147     }
148     if (action == CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) {
149         reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::TIME_ZONE_CHANGE);
150         return;
151     }
152     if (action == CommonEventSupport::COMMON_EVENT_TIME_CHANGED) {
153         reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE);
154         return;
155     }
156     if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
157         reminderDataManager_->OnUserSwitch(data.GetCode());
158         return;
159     }
160     if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
161         reminderDataManager_->OnUserRemove(data.GetCode());
162         return;
163     }
164 }
165 
HandlePackageRemove(const EventFwk::Want & want) const166 void ReminderEventManager::ReminderEventSubscriber::HandlePackageRemove(const EventFwk::Want &want) const
167 {
168     OHOS::AppExecFwk::ElementName ele = want.GetElement();
169     std::string bundleName = ele.GetBundleName();
170     int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1);
171     reminderDataManager_->CancelAllReminders(bundleName, userId);
172 }
173 
HandleProcessDied(const EventFwk::Want & want) const174 void ReminderEventManager::ReminderEventSubscriber::HandleProcessDied(const EventFwk::Want &want) const
175 {
176     sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
177     if (bundleOption == nullptr) {
178         ANSR_LOGE("Get bundle option error.");
179         return;
180     }
181     reminderDataManager_->OnProcessDiedLocked(bundleOption);
182 }
183 
GetBundleOption(const OHOS::EventFwk::Want & want) const184 sptr<NotificationBundleOption> ReminderEventManager::ReminderEventSubscriber::GetBundleOption(
185     const OHOS::EventFwk::Want &want) const
186 {
187     OHOS::AppExecFwk::ElementName ele = want.GetElement();
188     std::string bundleName = ele.GetBundleName();
189     int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1);
190     int32_t uid = ReminderRequest::GetUid(userId, bundleName);
191     ANSR_LOGD("bundleName=%{public}s, userId=%{public}d, uid=%{public}d", bundleName.c_str(), userId, uid);
192     sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
193     if (bundleOption == nullptr) {
194         ANSR_LOGE("new NotificationBundleOption fail due to no memory.");
195     }
196     return bundleOption;
197 }
198 
SystemAbilityStatusChangeListener(std::shared_ptr<ReminderDataManager> & reminderDataManager)199 ReminderEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
200     std::shared_ptr<ReminderDataManager> &reminderDataManager)
201 {
202     reminderDataManager_ = reminderDataManager;
203 }
204 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)205 void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
206     int32_t systemAbilityId, const std::string& deviceId)
207 {
208     ANSR_LOGD("OnAddSystemAbilityInner");
209     reminderDataManager_->OnServiceStart();
210 }
211 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)212 void ReminderEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
213     int32_t systemAbilityId, const std::string& deviceId)
214 {
215     ANSR_LOGD("OnRemoveSystemAbilityInner");
216 }
217 }  // namespace OHOS
218 }  // namespace Notification
219