• 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     customMatchingSkills.AddEvent(ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT);
45     CommonEventSubscribeInfo customSubscriberInfo(customMatchingSkills);
46     customSubscriberInfo.SetPermission("ohos.permission.GRANT_SENSITIVE_PERMISSIONS");
47     customSubscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
48     auto customSubscriber = std::make_shared<ReminderEventCustomSubscriber>(customSubscriberInfo, reminderDataManager);
49 
50     MatchingSkills matchingSkills;
51     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
52     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
53     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED);
54     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED);
55     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
56     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED);
57     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
58     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
59     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
60     subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
61     auto subscriber = std::make_shared<ReminderEventSubscriber>(subscriberInfo, reminderDataManager);
62 
63     std::string identity = IPCSkeleton::ResetCallingIdentity();
64     if (CommonEventManager::SubscribeCommonEvent(subscriber) &&
65         CommonEventManager::SubscribeCommonEvent(customSubscriber)) {
66         ANSR_LOGD("SubscribeCommonEvent ok");
67     } else {
68         ANSR_LOGD("SubscribeCommonEvent fail");
69     }
70     IPCSkeleton::SetCallingIdentity(identity);
71 
72     sptr<SystemAbilityStatusChangeListener> statusChangeListener
73         = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager);
74     if (statusChangeListener == nullptr) {
75         ANSR_LOGE("Failed to create statusChangeListener due to no memory.");
76         return;
77     }
78     // app mgr
79     sptr<SystemAbilityStatusChangeListener> appMgrStatusChangeListener
80         = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager);
81     if (appMgrStatusChangeListener == nullptr) {
82         ANSR_LOGE("Failed to create appMgrStatusChangeListener due to no memory.");
83         return;
84     }
85 
86     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
87     if (samgrProxy == nullptr) {
88         ANSR_LOGD("samgrProxy is null");
89         return;
90     }
91     int32_t ret = samgrProxy->SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, statusChangeListener);
92     if (ret != ERR_OK) {
93         ANSR_LOGE("subscribe system ability id: %{public}d failed", BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
94     }
95     ret = samgrProxy->SubscribeSystemAbility(APP_MGR_SERVICE_ID, appMgrStatusChangeListener);
96     if (ret != ERR_OK) {
97         ANSR_LOGE("subscribe system ability id: %{public}d failed", APP_MGR_SERVICE_ID);
98     }
99 }
100 
ReminderEventSubscriber(const CommonEventSubscribeInfo & subscriberInfo,std::shared_ptr<ReminderDataManager> & reminderDataManager)101 ReminderEventManager::ReminderEventSubscriber::ReminderEventSubscriber(
102     const CommonEventSubscribeInfo &subscriberInfo,
103     std::shared_ptr<ReminderDataManager> &reminderDataManager) : CommonEventSubscriber(subscriberInfo)
104 {
105     reminderDataManager_ = reminderDataManager;
106 }
107 
ReminderEventCustomSubscriber(const CommonEventSubscribeInfo & subscriberInfo,std::shared_ptr<ReminderDataManager> & reminderDataManager)108 ReminderEventManager::ReminderEventCustomSubscriber::ReminderEventCustomSubscriber(
109     const CommonEventSubscribeInfo &subscriberInfo,
110     std::shared_ptr<ReminderDataManager> &reminderDataManager) : CommonEventSubscriber(subscriberInfo)
111 {
112     reminderDataManager_ = reminderDataManager;
113 }
114 
OnReceiveEvent(const EventFwk::CommonEventData & data)115 void ReminderEventManager::ReminderEventCustomSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
116 {
117     Want want = data.GetWant();
118     std::string action = want.GetAction();
119     ANSR_LOGI("Recieved common event:%{public}s", action.c_str());
120     if (action == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) {
121         reminderDataManager_->ShowActiveReminder(want);
122         return;
123     }
124     if (action == ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT) {
125         reminderDataManager_->TerminateAlerting(want);
126         return;
127     }
128     if (action == ReminderRequest::REMINDER_EVENT_CLOSE_ALERT) {
129         reminderDataManager_->CloseReminder(want, true);
130         return;
131     }
132     if (action == ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT) {
133         reminderDataManager_->SnoozeReminder(want);
134         return;
135     }
136     if (action == ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT) {
137         reminderDataManager_->HandleCustomButtonClick(want);
138         return;
139     }
140     if (action == ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION) {
141         reminderDataManager_->CloseReminder(want, false);
142         return;
143     }
144 }
145 
OnReceiveEvent(const EventFwk::CommonEventData & data)146 void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
147 {
148     Want want = data.GetWant();
149     std::string action = want.GetAction();
150     ANSR_LOGD("Recieved common event:%{public}s", action.c_str());
151     if (action == CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) {
152         reminderDataManager_->Init(true);
153         return;
154     }
155     if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
156         HandlePackageRemove(want);
157         return;
158     }
159     if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
160         HandlePackageRemove(want);
161         return;
162     }
163     if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED) {
164         HandleProcessDied(want);
165         return;
166     }
167     if (action == CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) {
168         reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::TIME_ZONE_CHANGE);
169         return;
170     }
171     if (action == CommonEventSupport::COMMON_EVENT_TIME_CHANGED) {
172         reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE);
173         return;
174     }
175     if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
176         reminderDataManager_->OnUserSwitch(data.GetCode());
177         return;
178     }
179     if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
180         reminderDataManager_->OnUserRemove(data.GetCode());
181         return;
182     }
183 }
184 
HandlePackageRemove(const EventFwk::Want & want) const185 void ReminderEventManager::ReminderEventSubscriber::HandlePackageRemove(const 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     reminderDataManager_->CancelAllReminders(bundleName, userId);
191 }
192 
HandleProcessDied(const EventFwk::Want & want) const193 void ReminderEventManager::ReminderEventSubscriber::HandleProcessDied(const EventFwk::Want &want) const
194 {
195     sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
196     if (bundleOption == nullptr) {
197         ANSR_LOGE("Get bundle option error.");
198         return;
199     }
200     reminderDataManager_->OnProcessDiedLocked(bundleOption);
201 }
202 
GetBundleOption(const OHOS::EventFwk::Want & want) const203 sptr<NotificationBundleOption> ReminderEventManager::ReminderEventSubscriber::GetBundleOption(
204     const OHOS::EventFwk::Want &want) const
205 {
206     OHOS::AppExecFwk::ElementName ele = want.GetElement();
207     std::string bundleName = ele.GetBundleName();
208     int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1);
209     int32_t uid = ReminderRequest::GetUid(userId, bundleName);
210     ANSR_LOGD("bundleName=%{public}s, userId=%{public}d, uid=%{public}d", bundleName.c_str(), userId, uid);
211     sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
212     if (bundleOption == nullptr) {
213         ANSR_LOGE("new NotificationBundleOption fail due to no memory.");
214     }
215     return bundleOption;
216 }
217 
SystemAbilityStatusChangeListener(std::shared_ptr<ReminderDataManager> & reminderDataManager)218 ReminderEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
219     std::shared_ptr<ReminderDataManager> &reminderDataManager)
220 {
221     reminderDataManager_ = reminderDataManager;
222 }
223 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)224 void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
225     int32_t systemAbilityId, const std::string& deviceId)
226 {
227     ANSR_LOGD("OnAddSystemAbilityInner");
228     switch (systemAbilityId) {
229         case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
230             ANSR_LOGD("OnAddSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY");
231             reminderDataManager_->OnServiceStart();
232             break;
233         case APP_MGR_SERVICE_ID:
234             ANSR_LOGD("OnAddSystemAbilityInner: APP_MGR_SERVICE");
235             break;
236         default:
237             break;
238     }
239 }
240 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)241 void ReminderEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
242     int32_t systemAbilityId, const std::string& deviceId)
243 {
244     ANSR_LOGD("OnRemoveSystemAbilityInner");
245     switch (systemAbilityId) {
246         case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
247             ANSR_LOGD("OnRemoveSystemAbilityInner: BUNDLE_MGR_SERVICE_SYS_ABILITY");
248             break;
249         case APP_MGR_SERVICE_ID:
250             ANSR_LOGD("OnRemoveSystemAbilityInner: APP_MGR_SERVICE");
251             reminderDataManager_->OnRemoveAppMgr();
252             break;
253         default:
254             break;
255     }
256 }
257 }  // namespace OHOS
258 }  // namespace Notification
259