diff --git a/frameworks/ans/native/src/reminder_request.cpp b/frameworks/ans/native/src/reminder_request.cpp index a1d134f8dbcad17ca8b5f85383d092925b0d1166..cd1a49e8c812ddb2ba03d8b5ffd56508d0098563 100644 --- a/frameworks/ans/native/src/reminder_request.cpp +++ b/frameworks/ans/native/src/reminder_request.cpp @@ -1540,6 +1540,7 @@ int32_t ReminderRequest::GetUid(const int &userId, const std::string &bundleName sptr bundleMgr = iface_cast(remoteObject); bundleMgr->GetApplicationInfo(bundleName, AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO, static_cast(userId), info); + ANSR_LOGD("uid=%{public}d", info.uid); return static_cast(info.uid); } @@ -1547,6 +1548,7 @@ int ReminderRequest::GetUserId(const int &uid) { int userId = -1; AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId); + ANSR_LOGD("userId=%{public}d", userId); return userId; } diff --git a/frameworks/ans/native/src/reminder_request_calendar.cpp b/frameworks/ans/native/src/reminder_request_calendar.cpp index b2490eb647b877e2b2aae4770c266a71babd8f66..751c600fafc90eadc6f325c7609fa4b399c1b25d 100644 --- a/frameworks/ans/native/src/reminder_request_calendar.cpp +++ b/frameworks/ans/native/src/reminder_request_calendar.cpp @@ -450,7 +450,7 @@ bool ReminderRequestCalendar::Marshalling(Parcel &parcel) const ReminderRequestCalendar *ReminderRequestCalendar::Unmarshalling(Parcel &parcel) { ANSR_LOGD("New calendar"); - auto objptr = new ReminderRequestCalendar(); + auto objptr = new (std::nothrow) ReminderRequestCalendar(); if (objptr == nullptr) { ANS_LOGE("Failed to create reminder calendar due to no memory."); return objptr; diff --git a/frameworks/ans/native/src/reminder_store.cpp b/frameworks/ans/native/src/reminder_store.cpp index 1b51a5a9f33c1f9a3d7bbe31da8d9034d9929bb4..9a6ea8275de6687b5bb0550bf4468c9af4752b44 100644 --- a/frameworks/ans/native/src/reminder_store.cpp +++ b/frameworks/ans/native/src/reminder_store.cpp @@ -143,7 +143,7 @@ int32_t ReminderStore::DeleteUser(int32_t userId) int32_t ReminderStore::Delete(const std::string &pkg, int32_t userId) { - std::string deleteCondition = ReminderRequest::PKG_NAME + " = " + pkg + " and " + std::string deleteCondition = ReminderRequest::PKG_NAME + " = \"" + pkg + "\" and " + ReminderRequest::USER_ID + " = " + std::to_string(userId); return ReminderStore::Delete(deleteCondition); } @@ -342,15 +342,15 @@ sptr ReminderStore::BuildReminder(const std::shared_ptr reminder = nullptr; switch (reminderType) { case (static_cast(ReminderRequest::ReminderType::TIMER)): { - reminder = new ReminderRequestTimer(reminderId); + reminder = new (std::nothrow) ReminderRequestTimer(reminderId); break; } case (static_cast(ReminderRequest::ReminderType::CALENDAR)): { - reminder = new ReminderRequestCalendar(reminderId); + reminder = new (std::nothrow) ReminderRequestCalendar(reminderId); break; } case (static_cast(ReminderRequest::ReminderType::ALARM)): { - reminder = new ReminderRequestAlarm(reminderId); + reminder = new (std::nothrow) ReminderRequestAlarm(reminderId); break; } default: { diff --git a/services/ans/include/reminder_data_manager.h b/services/ans/include/reminder_data_manager.h index bd69270eb4970ed0e6d2dc4458e34accd3595fc1..acd73e3669bae943a9c9e989626e72fb4d74bd02 100644 --- a/services/ans/include/reminder_data_manager.h +++ b/services/ans/include/reminder_data_manager.h @@ -41,10 +41,10 @@ public: /** * @brief Cancels all the reminders relative to the bundle option. * - * @param bundleOption Indicates the bundle option. + * @param packageName Indicates the package name. * @param userId Indicates the user id which the bundle belong to. */ - void CancelAllReminders(const sptr &bundleOption, int userId); + void CancelAllReminders(const std::string &packageName, const int &userId); /** * @brief Cancels the target reminder relative to the reminder id and bundle option. @@ -95,8 +95,14 @@ public: */ void Init(bool isFromBootComplete); + void InitUserId(); + + void OnRemoveUser(const int& userId); + void OnServiceStart(); + void OnSwitchUser(const int& userId); + /** * @brief Triggered when third party application died. * @@ -126,6 +132,8 @@ public: */ void SetService(AdvancedNotificationService *advancedNotificationService); + bool ShouldAlert(const sptr &reminder) const; + /** * @brief Show the reminder. * @@ -185,6 +193,16 @@ private: */ void AddToShowedReminders(const sptr &reminder); + void CancelAllReminders(const int &userId); + + /** + * @brief Cancels all the reminders of the target bundle or user. + * + * @param packageName Indicates the packageName need to cancel. + * @param userId Indicates the userId to cancel. + */ + void CancelRemindersImplLocked(const std::string &packageName, const int &userId); + /** * Cancels the notification relative to the reminder. * @@ -198,7 +216,7 @@ private: * @param bundleName Indicates the target bundle. * @return true if number limit is exceeded. */ - bool CheckReminderLimitExceededLocked(const std::string &bundleName) const; + bool CheckReminderLimitExceededLocked(const sptr &bundleOption) const; void CloseReminder(const sptr &reminder, bool cancelNotification); /** @@ -269,18 +287,29 @@ private: bool HandleSysTimeChange(const sptr reminder) const; - bool IsReminderAgentReady() const; - /** - * Judge the two reminders is belong to the same application or not. + * @brief Judge the two reminders is belong to the same application or not. * - * @param reminder Indicates the first reminder. - * @param otherPkgName Indicates the package name of second reminder. - * @param otherUserId Indicates the user id of second reminder. + * @param bundleOption Indicates the bundleOption of first reminder. + * @param other Indicates the bundleOption of second reminder. * @return true if the two reminders belong to the same application. */ - bool IsBelongToSameApp( - const sptr reminder, const std::string &otherPkgName, const int otherUserId); + bool IsBelongToSameApp(const sptr &bundleOption, + const sptr &other) const; + + /** + * @brief Judges whether the reminder is matched with the bundleOption or userId. + * + * @param reminder Indicates the target reminder. + * @param packageName Indicates the package name. + * @param userId Indicates the user id. + * @return true If the reminder is matched with the bundleOption or userId. + */ + bool IsMatched(const sptr &reminder, const std::string &packageName, const int &userId) const; + + bool IsAllowedNotify(const sptr &reminder) const; + + bool IsReminderAgentReady() const; void LoadReminderFromDb(); @@ -462,6 +491,7 @@ private: * Indicates the total count of reminders in system. */ int16_t totalCount_ {0}; + int currentUserId_ {0}; sptr advancedNotificationService_ = nullptr; std::shared_ptr store_ = nullptr; }; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index edf337dd5e41cf53770f3cf8a978453ec2c84bd3..4217cb849f1d99804dc0818c77fcfd11be44ae5c 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1802,7 +1802,7 @@ ErrCode AdvancedNotificationService::CancelAllReminders() if (rdm == nullptr) { return ERR_NO_INIT; } - rdm->CancelAllReminders(bundleOption, userId); + rdm->CancelAllReminders(bundleOption->GetBundleName(), userId); return ERR_OK; } diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index b08745ff58c7a326c6a1b256eddc23216febb95a..7a768a494d607a1c90e7b70604383dbf61f70303 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -20,12 +20,19 @@ #include "common_event_support.h" #include "ipc_skeleton.h" #include "notification_slot.h" +#include "os_account_manager.h" #include "reminder_event_manager.h" #include "time_service_client.h" #include "singleton.h" namespace OHOS { namespace Notification { +namespace +{ +const std::string ALL_PACKAGES = "allPackages"; +const int MAIN_USER_ID = 100; +} + const int16_t ReminderDataManager::MAX_NUM_REMINDER_LIMIT_SYSTEM = 2000; const int16_t ReminderDataManager::MAX_NUM_REMINDER_LIMIT_APP = 30; const uint8_t ReminderDataManager::TIME_ZONE_CHANGE = 0; @@ -39,7 +46,7 @@ std::mutex ReminderDataManager::TIMER_MUTEX; void ReminderDataManager::PublishReminder(const sptr &reminder, const sptr &bundleOption) { - if (CheckReminderLimitExceededLocked(bundleOption->GetBundleName())) { + if (CheckReminderLimitExceededLocked(bundleOption)) { return; } UpdateAndSaveReminderLocked(reminder, bundleOption); @@ -69,67 +76,46 @@ void ReminderDataManager::CancelReminder( StartRecentReminder(); } -void ReminderDataManager::CancelNotification(const sptr &reminder) const +void ReminderDataManager::CancelAllReminders(const std::string &packageName, const int &userId) { - if (!(reminder->IsShowing())) { - ANSR_LOGD("No need to cancel notification"); - return; - } - sptr notification = reminder->GetNotificationRequest(); - if (notification == nullptr) { - ANSR_LOGW("Cancel notification fail"); - return; - } - ANSR_LOGD("Cancel notification"); - if (advancedNotificationService_ == nullptr) { - ANSR_LOGE("Cancel notification fail"); - return; - } - sptr bundleOption = FindNotificationBundleOption(reminder->GetReminderId()); - advancedNotificationService_->CancelPreparedNotification( - notification->GetNotificationId(), ReminderRequest::NOTIFICATION_LABEL, bundleOption); + ANSR_LOGD("CancelAllReminders, userId=%{public}d, pkgName=%{public}s", + userId, packageName.c_str()); + CancelRemindersImplLocked(packageName, userId); } -bool ReminderDataManager::CheckReminderLimitExceededLocked(const std::string &bundleName) const +void ReminderDataManager::GetValidReminders( + const sptr bundleOption, std::vector> &reminders) { std::lock_guard lock(ReminderDataManager::MUTEX); - if (totalCount_ >= ReminderDataManager::MAX_NUM_REMINDER_LIMIT_SYSTEM) { - ANSR_LOGW("The number of validate reminders exceeds the system upper limit:%{public}d, \ - and new reminder can not be published", MAX_NUM_REMINDER_LIMIT_SYSTEM); - return true; - } - int8_t count = 0; for (auto it = reminderVector_.begin(); it != reminderVector_.end(); ++it) { if ((*it)->IsExpired()) { continue; } - auto mit = notificationBundleOptionMap_.find((*it)->GetReminderId()); + int32_t reminderId = (*it)->GetReminderId(); + auto mit = notificationBundleOptionMap_.find(reminderId); if (mit == notificationBundleOptionMap_.end()) { - ANSR_LOGE("Error occur when get bundle option, reminderId=%{public}d", (*it)->GetReminderId()); + ANSR_LOGE("Get bundle option occur error, reminderId=%{public}d", reminderId); } else { - if (mit->second->GetBundleName() == bundleName) { - count++; + if (IsBelongToSameApp(mit->second, bundleOption)) { + reminders.push_back(*it); } } } - if (count >= ReminderDataManager::MAX_NUM_REMINDER_LIMIT_APP) { - ANSR_LOGW("The number of validate reminders exceeds the application upper limit:%{public}d, and new \ - reminder can not be published", MAX_NUM_REMINDER_LIMIT_APP); - return true; - } - return false; } -void ReminderDataManager::CancelAllReminders(const sptr &bundleOption, int userId) +void ReminderDataManager::CancelAllReminders(const int &userId) +{ + ANSR_LOGD("CancelAllReminders, userId=%{public}d", userId); + CancelRemindersImplLocked(ALL_PACKAGES, userId); +} + +void ReminderDataManager::CancelRemindersImplLocked(const std::string &packageName, const int &userId) { MUTEX.lock(); - auto it = notificationBundleOptionMap_.find(activeReminderId_); - if (it != notificationBundleOptionMap_.end()) { - if (it->second->GetBundleName() == bundleOption->GetBundleName()) { - activeReminder_->OnStop(); - StopTimer(TimerType::TRIGGER_TIMER); - ANSR_LOGD("Stop active reminder, reminderId=%{public}d", activeReminderId_); - } + if (activeReminderId_ != -1 && IsMatched(activeReminder_, packageName, userId)) { + activeReminder_->OnStop(); + StopTimer(TimerType::TRIGGER_TIMER); + ANSR_LOGD("Stop active reminder, reminderId=%{public}d", activeReminderId_); } for (auto vit = reminderVector_.begin(); vit != reminderVector_.end();) { int32_t reminderId = (*vit)->GetReminderId(); @@ -139,8 +125,7 @@ void ReminderDataManager::CancelAllReminders(const sptrsecond->GetBundleName() == bundleOption->GetBundleName()) { - ANSR_LOGD("currently, userId is not supported. userId=%{public}d", userId); + if (IsMatched(*vit, packageName, userId)) { if ((*vit)->IsAlerting()) { StopAlertingReminder(*vit); } @@ -150,33 +135,88 @@ void ReminderDataManager::CancelAllReminders(const sptrDelete(reminderId); continue; } ++vit; } + if (packageName == ALL_PACKAGES) { + store_->DeleteUser(userId); + } else { + store_->Delete(packageName, userId); + } MUTEX.unlock(); StartRecentReminder(); } -void ReminderDataManager::GetValidReminders( - const sptr bundleOption, std::vector> &reminders) +bool ReminderDataManager::IsMatched(const sptr &reminder, + const std::string &packageName, const int &userId) const +{ + auto mit = notificationBundleOptionMap_.find(reminder->GetReminderId()); + if (mit == notificationBundleOptionMap_.end()) { + ANS_LOGE("Failed to get bundle information. reminderId=%{public}d", reminder->GetReminderId()); + return true; + } + if (ReminderRequest::GetUserId(mit->second->GetUid()) != userId) { + return false; + } + if (packageName == ALL_PACKAGES) { + return true; + } + if (mit->second->GetBundleName() == packageName) { + return true; + } + return false; +} + +void ReminderDataManager::CancelNotification(const sptr &reminder) const +{ + if (!(reminder->IsShowing())) { + ANSR_LOGD("No need to cancel notification"); + return; + } + sptr notification = reminder->GetNotificationRequest(); + if (notification == nullptr) { + ANSR_LOGW("Cancel notification fail"); + return; + } + ANSR_LOGD("Cancel notification"); + if (advancedNotificationService_ == nullptr) { + ANSR_LOGE("Cancel notification fail"); + return; + } + sptr bundleOption = FindNotificationBundleOption(reminder->GetReminderId()); + advancedNotificationService_->CancelPreparedNotification( + notification->GetNotificationId(), ReminderRequest::NOTIFICATION_LABEL, bundleOption); +} + +bool ReminderDataManager::CheckReminderLimitExceededLocked(const sptr &bundleOption) const { std::lock_guard lock(ReminderDataManager::MUTEX); + if (totalCount_ >= ReminderDataManager::MAX_NUM_REMINDER_LIMIT_SYSTEM) { + ANSR_LOGW("The number of validate reminders exceeds the system upper limit:%{public}d, \ + and new reminder can not be published", MAX_NUM_REMINDER_LIMIT_SYSTEM); + return true; + } + int8_t count = 0; for (auto it = reminderVector_.begin(); it != reminderVector_.end(); ++it) { if ((*it)->IsExpired()) { continue; } - int32_t reminderId = (*it)->GetReminderId(); - auto mit = notificationBundleOptionMap_.find(reminderId); + auto mit = notificationBundleOptionMap_.find((*it)->GetReminderId()); if (mit == notificationBundleOptionMap_.end()) { - ANSR_LOGE("Get bundle option occur error, reminderId=%{public}d", reminderId); + ANSR_LOGE("Error occur when get bundle option, reminderId=%{public}d", (*it)->GetReminderId()); } else { - if (mit->second->GetBundleName() == bundleOption->GetBundleName()) { - reminders.push_back(*it); + if (IsBelongToSameApp(mit->second, bundleOption)) { + count++; } } } + if (count >= ReminderDataManager::MAX_NUM_REMINDER_LIMIT_APP) { + ANSR_LOGW("The number of validate reminders exceeds the application upper limit:%{public}d, and new \ + reminder can not be published", MAX_NUM_REMINDER_LIMIT_APP); + return true; + } + return false; } void ReminderDataManager::AddToShowedReminders(const sptr &reminder) @@ -192,6 +232,16 @@ void ReminderDataManager::AddToShowedReminders(const sptr &remi showedReminderVector_.push_back(reminder); } +void ReminderDataManager::OnRemoveUser(const int& userId) +{ + ANSR_LOGD("Remove user id: %{public}d", userId); + if (!IsReminderAgentReady()) { + ANSR_LOGW("Give up to remove user id: %{public}d for reminderAgent is not ready", userId); + return; + } + CancelAllReminders(userId); +} + void ReminderDataManager::OnServiceStart() { std::vector> immediatelyShowReminders; @@ -201,6 +251,16 @@ void ReminderDataManager::OnServiceStart() StartRecentReminder(); } +void ReminderDataManager::OnSwitchUser(const int& userId) +{ + ANSR_LOGD("Switch user id from %{public}d to %{public}d", currentUserId_, userId); + currentUserId_ = userId; + std::lock_guard lock(ReminderDataManager::MUTEX); + if ((alertingReminderId_ != -1) && IsReminderAgentReady()) { + TerminateAlerting(alertingReminder_, "OnSwitchUser"); + } +} + void ReminderDataManager::OnProcessDiedLocked(const sptr bundleOption) { std::string bundleName = bundleOption->GetBundleName(); @@ -327,6 +387,7 @@ sptr ReminderDataManager::FindNotificationBundleOption { auto it = notificationBundleOptionMap_.find(reminderId); if (it == notificationBundleOptionMap_.end()) { + ANSR_LOGW("Failed to get bundle option."); return nullptr; } else { return it->second; @@ -472,6 +533,52 @@ void ReminderDataManager::SetService(AdvancedNotificationService *advancedNotifi advancedNotificationService_ = advancedNotificationService; } +bool ReminderDataManager::ShouldAlert(const sptr &reminder) const +{ + if (reminder == nullptr) { + return false; + } + int32_t reminderId = reminder->GetReminderId(); + sptr bundleOption = FindNotificationBundleOption(reminderId); + if (bundleOption == nullptr) { + ANSR_LOGD("The reminder (reminderId=%{public}d) is silent", reminderId); + return false; + } + int userId = ReminderRequest::GetUserId(bundleOption->GetUid()); + if (currentUserId_ != userId) { + ANSR_LOGD("The reminder (reminderId=%{public}d) is silent for not in active user, " \ + "current user id: %{public}d, reminder user id: %{public}d", reminderId, currentUserId_, userId); + return false; + } + + sptr date; + ErrCode errCode = advancedNotificationService_->GetDoNotDisturbDate(date); + if (errCode != ERR_OK) { + ANSR_LOGE("The reminder (reminderId=%{public}d) is silent for get disturbDate error", reminderId); + return false; + } + if (date->GetDoNotDisturbType() == NotificationConstant::DoNotDisturbType::NONE) { + return true; + } + std::vector> slots; + errCode = advancedNotificationService_->GetSlotsByBundle(bundleOption, slots); + if (errCode != ERR_OK) { + ANSR_LOGE("The reminder (reminderId=%{public}d) is silent for get slots error", reminderId); + return false; + } + for (auto slot : slots) { + if (slot->GetType() != reminder->GetSlotType()) { + continue; + } + if (slot->IsEnableBypassDnd()) { + ANSR_LOGD("Not silent for enable by pass Dnd, reminderId=%{public}d", reminderId); + return true; + } + } + ANSR_LOGD("The reminder (reminderId=%{public}d) is silent for Dnd", reminderId); + return false; +} + void ReminderDataManager::ShowActiveReminder(const EventFwk::Want &want) { int32_t reminderId = static_cast(want.GetIntParam(ReminderRequest::PARAM_REMINDER_ID, -1)); @@ -567,9 +674,17 @@ void ReminderDataManager::ShowReminder(const sptr &reminder, co if (advancedNotificationService_ == nullptr) { ANSR_LOGE("ShowReminder fail"); reminder->OnShow(false, isSysTimeChanged, false); + store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); + return; + } + if (!IsAllowedNotify(reminder)) { + ANSR_LOGD("Not allow to notify."); + reminder->OnShow(false, isSysTimeChanged, false); + store_->UpdateOrInsert(reminder, FindNotificationBundleOption(reminder->GetReminderId())); return; } - reminder->OnShow(isNeedToPlaySound, isSysTimeChanged, true); + bool toPlaySound = isNeedToPlaySound && ShouldAlert(reminder) ? true : false; + reminder->OnShow(toPlaySound, isSysTimeChanged, true); AddToShowedReminders(reminder); UpdateNotification(reminder); // this should be called after OnShow ANSR_LOGD("publish notification.(reminderId=%{public}d)", reminder->GetReminderId()); @@ -578,7 +693,7 @@ void ReminderDataManager::ShowReminder(const sptr &reminder, co reminder->OnShowFail(); RemoveFromShowedReminders(reminder); } else { - if (isNeedToPlaySound) { + if (toPlaySound) { PlaySoundAndVibration(reminder); // play sound and vibration if (needScheduleTimeout) { StartTimer(reminder, TimerType::ALERTING_TIMER); @@ -816,6 +931,12 @@ void ReminderDataManager::HandleSameNotificationIdShowing(const sptrGetNotificationId(); ANSR_LOGD("HandleSameNotificationIdShowing notificationId=%{public}d", notificationId); int32_t curReminderId = reminder->GetReminderId(); + auto mit = notificationBundleOptionMap_.find(curReminderId); + if (mit == notificationBundleOptionMap_.end()) { + ANSR_LOGE("Error occur when get bundle option, reminderId=%{public}d", curReminderId); + return; + } + for (auto it = reminderVector_.begin(); it != reminderVector_.end(); ++it) { int32_t tmpId = (*it)->GetReminderId(); if (tmpId == curReminderId) { @@ -829,8 +950,7 @@ void ReminderDataManager::HandleSameNotificationIdShowing(const sptrGetNotificationId() && - IsBelongToSameApp(reminder, bundleOption->GetBundleName(), 0)) { + if (notificationId == (*it)->GetNotificationId() && IsBelongToSameApp(bundleOption, mit->second)) { if ((*it)->IsAlerting()) { StopAlertingReminder(*it); } @@ -855,10 +975,24 @@ void ReminderDataManager::Init(bool isFromBootComplete) return; } LoadReminderFromDb(); + InitUserId(); isReminderAgentReady_ = true; ANSR_LOGD("ReminderAgent is ready."); } +void ReminderDataManager::InitUserId() +{ + std::vector activeUserId; + AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); + if (activeUserId.size() > 0) { + currentUserId_ = activeUserId[0]; + ANSR_LOGD("Init user id=%{public}d", currentUserId_); + } else { + currentUserId_ = MAIN_USER_ID; + ANSR_LOGE("Failed to get active user id."); + } +} + void ReminderDataManager::GetImmediatelyShowRemindersLocked(std::vector> &reminders) const { std::lock_guard lock(ReminderDataManager::MUTEX); @@ -873,25 +1007,37 @@ void ReminderDataManager::GetImmediatelyShowRemindersLocked(std::vector &reminder) const +{ + if (reminder == nullptr) { + return false; + } + int32_t reminderId = reminder->GetReminderId(); + auto mit = notificationBundleOptionMap_.find(reminderId); + if (mit == notificationBundleOptionMap_.end()) { + ANSR_LOGE("Get bundle option occur error, reminderId=%{public}d", reminderId); + return false; + } + bool isAllowed = false; + ErrCode errCode = advancedNotificationService_->IsSpecialBundleAllowedNotify(mit->second, isAllowed); + if (errCode != ERR_OK) { + ANSR_LOGE("Failed to call IsSpecialBundleAllowedNotify, errCode=%{public}d", errCode); + return false; + } + return isAllowed; +} + bool ReminderDataManager::IsReminderAgentReady() const { return isReminderAgentReady_; } -bool ReminderDataManager::IsBelongToSameApp( - const sptr reminder, const std::string &otherPkgName, const int otherUserId) +bool ReminderDataManager::IsBelongToSameApp(const sptr &bundleOption, + const sptr &other) const { - ANSR_LOGD("otherUserId=%{public}d, (currently, userId not support)", otherUserId); - int32_t reminderId = reminder->GetReminderId(); - sptr bundleOption = FindNotificationBundleOption(reminderId); - if (bundleOption == nullptr) { - ANSR_LOGW("IsBelongToSameApp get notificationBundleOption(reminderId=%{public}d) fail", reminderId); - return false; - } - if (bundleOption->GetBundleName() == otherPkgName) { - return true; - } - return false; + int userIdSrc = ReminderRequest::GetUserId(bundleOption->GetUid()); + int userIdTar = ReminderRequest::GetUserId(other->GetUid()); + return ((bundleOption->GetBundleName() == other->GetBundleName()) && (userIdSrc == userIdTar)) ? true : false; } void ReminderDataManager::LoadReminderFromDb() @@ -901,7 +1047,11 @@ void ReminderDataManager::LoadReminderFromDb() reminderVector_ = existReminders; ANSR_LOGD("LoadReminderFromDb, reminder size=%{public}d", reminderVector_.size()); for (auto it = reminderVector_.begin(); it != reminderVector_.end(); ++it) { - sptr bundleOption = new NotificationBundleOption(); + sptr bundleOption = new (std::nothrow) NotificationBundleOption(); + if (bundleOption == nullptr) { + ANS_LOGE("Failed to create bundle option due to low memory."); + return; + } int32_t reminderId = (*it)->GetReminderId(); if (!(store_->GetBundleOption(reminderId, bundleOption))) { ANSR_LOGE("Get bundle option fail, reminderId=%{public}d", reminderId); @@ -948,6 +1098,7 @@ void ReminderDataManager::PlaySoundAndVibration(const sptr &rem std::string ReminderDataManager::GetSoundUri(const sptr &reminder) { + // Gets the slots of the bundle of current active user sptr bundle = FindNotificationBundleOption(reminder->GetReminderId()); std::vector> slots; ErrCode errCode = advancedNotificationService_->GetSlotsByBundle(bundle, slots); diff --git a/services/ans/src/reminder_event_manager.cpp b/services/ans/src/reminder_event_manager.cpp index 747a9a788277bec542230e2defe84f75af1b43fc..5c9a848f6074940efc1c25b53f0016b1c7783eb0 100644 --- a/services/ans/src/reminder_event_manager.cpp +++ b/services/ans/src/reminder_event_manager.cpp @@ -47,6 +47,8 @@ void ReminderEventManager::init(std::shared_ptr &reminderDa matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED); CommonEventSubscribeInfo subscriberInfo(matchingSkills); auto subscriber = std::make_shared(subscriberInfo, reminderDataManager); @@ -59,7 +61,11 @@ void ReminderEventManager::init(std::shared_ptr &reminderDa IPCSkeleton::SetCallingIdentity(identity); sptr statusChangeListener - = new SystemAbilityStatusChangeListener(reminderDataManager); + = new (std::nothrow) SystemAbilityStatusChangeListener(reminderDataManager); + if (statusChangeListener == nullptr) { + ANS_LOGE("Failed to create statusChangeListener due to no memory."); + return; + } sptr samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgrProxy == nullptr) { ANSR_LOGD("samgrProxy is null"); @@ -127,6 +133,14 @@ void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFw reminderDataManager_->RefreshRemindersDueToSysTimeChange(ReminderDataManager::DATE_TIME_CHANGE); return; } + if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + reminderDataManager_->OnSwitchUser(data.GetCode()); + return; + } + if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) { + reminderDataManager_->OnRemoveUser(data.GetCode()); + return; + } } void ReminderEventManager::ReminderEventSubscriber::HandlePackageRemove(const EventFwk::Want &want) const @@ -134,12 +148,7 @@ void ReminderEventManager::ReminderEventSubscriber::HandlePackageRemove(const Ev OHOS::AppExecFwk::ElementName ele = want.GetElement(); std::string bundleName = ele.GetBundleName(); int userId = want.GetIntParam(OHOS::AppExecFwk::Constants::USER_ID, -1); - sptr bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, -1); - if (bundleOption == nullptr) { - ANSR_LOGE("new NotificationBundleOption fail due to no memory."); - return; - } - reminderDataManager_->CancelAllReminders(bundleOption, userId); + reminderDataManager_->CancelAllReminders(bundleName, userId); } void ReminderEventManager::ReminderEventSubscriber::HandleProcessDied(const EventFwk::Want &want) const