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 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
79 if (samgrProxy == nullptr) {
80 ANSR_LOGD("samgrProxy is null");
81 return;
82 }
83 int32_t ret = samgrProxy->SubscribeSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, statusChangeListener);
84 if (ret != ERR_OK) {
85 ANSR_LOGE("subscribe system ability id: %{public}d failed", BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
86 }
87 }
88
ReminderEventSubscriber(const CommonEventSubscribeInfo & subscriberInfo,std::shared_ptr<ReminderDataManager> & reminderDataManager)89 ReminderEventManager::ReminderEventSubscriber::ReminderEventSubscriber(
90 const CommonEventSubscribeInfo &subscriberInfo,
91 std::shared_ptr<ReminderDataManager> &reminderDataManager) : CommonEventSubscriber(subscriberInfo)
92 {
93 reminderDataManager_ = reminderDataManager;
94 }
95
ReminderEventCustomSubscriber(const CommonEventSubscribeInfo & subscriberInfo,std::shared_ptr<ReminderDataManager> & reminderDataManager)96 ReminderEventManager::ReminderEventCustomSubscriber::ReminderEventCustomSubscriber(
97 const CommonEventSubscribeInfo &subscriberInfo,
98 std::shared_ptr<ReminderDataManager> &reminderDataManager) : CommonEventSubscriber(subscriberInfo)
99 {
100 reminderDataManager_ = reminderDataManager;
101 }
102
OnReceiveEvent(const EventFwk::CommonEventData & data)103 void ReminderEventManager::ReminderEventCustomSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
104 {
105 Want want = data.GetWant();
106 std::string action = want.GetAction();
107 ANSR_LOGI("Recieved common event:%{public}s", action.c_str());
108 if (action == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) {
109 reminderDataManager_->ShowActiveReminder(want);
110 return;
111 }
112 if (action == ReminderRequest::REMINDER_EVENT_ALERT_TIMEOUT) {
113 reminderDataManager_->TerminateAlerting(want);
114 return;
115 }
116 if (action == ReminderRequest::REMINDER_EVENT_CLOSE_ALERT) {
117 reminderDataManager_->CloseReminder(want, true);
118 return;
119 }
120 if (action == ReminderRequest::REMINDER_EVENT_SNOOZE_ALERT) {
121 reminderDataManager_->SnoozeReminder(want);
122 return;
123 }
124 if (action == ReminderRequest::REMINDER_EVENT_CUSTOM_ALERT) {
125 reminderDataManager_->HandleCustomButtonClick(want);
126 return;
127 }
128 if (action == ReminderRequest::REMINDER_EVENT_REMOVE_NOTIFICATION) {
129 reminderDataManager_->CloseReminder(want, false);
130 return;
131 }
132 }
133
OnReceiveEvent(const EventFwk::CommonEventData & data)134 void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
135 {
136 Want want = data.GetWant();
137 std::string action = want.GetAction();
138 ANSR_LOGI("Recieved common event:%{public}s", action.c_str());
139 if (action == CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED) {
140 reminderDataManager_->Init(true);
141 return;
142 }
143 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
144 HandlePackageRemove(want);
145 return;
146 }
147 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
148 HandlePackageRemove(want);
149 return;
150 }
151 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED) {
152 HandleProcessDied(want);
153 return;
154 }
155 if (action == CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) {
156 reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::TIME_ZONE_CHANGE);
157 return;
158 }
159 if (action == CommonEventSupport::COMMON_EVENT_TIME_CHANGED) {
160 reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE);
161 return;
162 }
163 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
164 reminderDataManager_->OnUserSwitch(data.GetCode());
165 return;
166 }
167 if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
168 reminderDataManager_->OnUserRemove(data.GetCode());
169 return;
170 }
171 }
172
HandlePackageRemove(const EventFwk::Want & want) const173 void ReminderEventManager::ReminderEventSubscriber::HandlePackageRemove(const EventFwk::Want &want) const
174 {
175 OHOS::AppExecFwk::ElementName ele = want.GetElement();
176 std::string bundleName = ele.GetBundleName();
177 int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1);
178 reminderDataManager_->CancelAllReminders(bundleName, userId);
179 }
180
HandleProcessDied(const EventFwk::Want & want) const181 void ReminderEventManager::ReminderEventSubscriber::HandleProcessDied(const EventFwk::Want &want) const
182 {
183 sptr<NotificationBundleOption> bundleOption = GetBundleOption(want);
184 if (bundleOption == nullptr) {
185 ANSR_LOGE("Get bundle option error.");
186 return;
187 }
188 reminderDataManager_->OnProcessDiedLocked(bundleOption);
189 }
190
GetBundleOption(const OHOS::EventFwk::Want & want) const191 sptr<NotificationBundleOption> ReminderEventManager::ReminderEventSubscriber::GetBundleOption(
192 const OHOS::EventFwk::Want &want) const
193 {
194 OHOS::AppExecFwk::ElementName ele = want.GetElement();
195 std::string bundleName = ele.GetBundleName();
196 int32_t userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1);
197 int32_t uid = ReminderRequest::GetUid(userId, bundleName);
198 ANSR_LOGD("bundleName=%{public}s, userId=%{public}d, uid=%{public}d", bundleName.c_str(), userId, uid);
199 sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid);
200 if (bundleOption == nullptr) {
201 ANSR_LOGE("new NotificationBundleOption fail due to no memory.");
202 }
203 return bundleOption;
204 }
205
SystemAbilityStatusChangeListener(std::shared_ptr<ReminderDataManager> & reminderDataManager)206 ReminderEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
207 std::shared_ptr<ReminderDataManager> &reminderDataManager)
208 {
209 reminderDataManager_ = reminderDataManager;
210 }
211
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)212 void ReminderEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
213 int32_t systemAbilityId, const std::string& deviceId)
214 {
215 ANSR_LOGD("OnAddSystemAbilityInner");
216 reminderDataManager_->OnServiceStart();
217 }
218
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)219 void ReminderEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
220 int32_t systemAbilityId, const std::string& deviceId)
221 {
222 ANSR_LOGD("OnRemoveSystemAbilityInner");
223 }
224 } // namespace OHOS
225 } // namespace Notification
226