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