From 0d3f9643969f74e255e7e7bcd24a2c18461528f2 Mon Sep 17 00:00:00 2001 From: rcy-hw Date: Sat, 9 Apr 2022 11:37:35 +0800 Subject: [PATCH 1/3] support multiuser Signed-off-by: rcy-hw Change-Id: Ic40f3479feecdb2c1be4fd3d10fb46566a79b8c8 --- .../ans/native/src/reminder_request.cpp | 2 + frameworks/ans/native/src/reminder_store.cpp | 2 +- services/ans/include/reminder_data_manager.h | 52 +++- .../ans/src/advanced_notification_service.cpp | 2 +- services/ans/src/reminder_data_manager.cpp | 292 +++++++++++++----- services/ans/src/reminder_event_manager.cpp | 17 +- 6 files changed, 275 insertions(+), 92 deletions(-) diff --git a/frameworks/ans/native/src/reminder_request.cpp b/frameworks/ans/native/src/reminder_request.cpp index 31e706c48..6e22958b5 100644 --- a/frameworks/ans/native/src/reminder_request.cpp +++ b/frameworks/ans/native/src/reminder_request.cpp @@ -1544,6 +1544,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); } @@ -1551,6 +1552,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_store.cpp b/frameworks/ans/native/src/reminder_store.cpp index b33fb9481..1cd8e95dc 100644 --- a/frameworks/ans/native/src/reminder_store.cpp +++ b/frameworks/ans/native/src/reminder_store.cpp @@ -147,7 +147,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); } diff --git a/services/ans/include/reminder_data_manager.h b/services/ans/include/reminder_data_manager.h index 606d4a3aa..a2e710ccd 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 972b32657..62f6afd45 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1799,7 +1799,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 06cfb845a..7ad43e29e 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; @@ -476,6 +537,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)); @@ -571,9 +678,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()); @@ -582,7 +697,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); @@ -824,6 +939,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) { @@ -837,8 +958,7 @@ void ReminderDataManager::HandleSameNotificationIdShowing(const sptrGetNotificationId() && - IsBelongToSameApp(reminder, bundleOption->GetBundleName(), 0)) { + if (notificationId == (*it)->GetNotificationId() && IsBelongToSameApp(bundleOption, mit->second)) { if ((*it)->IsAlerting()) { StopAlertingReminder(*it); } @@ -863,10 +983,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); @@ -881,25 +1015,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() @@ -911,8 +1057,8 @@ void ReminderDataManager::LoadReminderFromDb() for (auto it = reminderVector_.begin(); it != reminderVector_.end(); ++it) { sptr bundleOption = new (std::nothrow) NotificationBundleOption(); if (bundleOption == nullptr) { - ANSR_LOGE("Failed to create bundleOption due to no memory."); - break; + ANS_LOGE("Failed to create bundle option due to low memory."); + return; } int32_t reminderId = (*it)->GetReminderId(); if (!(store_->GetBundleOption(reminderId, bundleOption))) { diff --git a/services/ans/src/reminder_event_manager.cpp b/services/ans/src/reminder_event_manager.cpp index 54dbe2cf4..723916eba 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); @@ -131,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 @@ -138,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 -- Gitee From d5aae7b3ad96838781bcc4fb385a2281743965c4 Mon Sep 17 00:00:00 2001 From: rcy-hw Date: Sat, 9 Apr 2022 15:59:48 +0800 Subject: [PATCH 2/3] support multiuser Signed-off-by: rcy-hw Change-Id: I92048f7dec6cafbb5180547e67f5539e3057d93d --- services/ans/src/reminder_data_manager.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index 7ad43e29e..ed1f43312 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -27,8 +27,7 @@ namespace OHOS { namespace Notification { -namespace -{ +namespace { const std::string ALL_PACKAGES = "allPackages"; const int MAIN_USER_ID = 100; } @@ -769,8 +768,6 @@ void ReminderDataManager::SnoozeReminderImpl(sptr &reminder) StartRecentReminder(); } -// snoozeReminder(bool snoozeAll) - void ReminderDataManager::StartRecentReminder() { sptr reminder = GetRecentReminderLocked(); -- Gitee From 59fdee6d657ad9ac1b769759497fd953d887cc84 Mon Sep 17 00:00:00 2001 From: rcy-hw Date: Mon, 11 Apr 2022 19:38:16 +0800 Subject: [PATCH 3/3] support multiuser Signed-off-by: rcy-hw Change-Id: I1576e907c2cd60b7f95ec24953a02690e176cf2e --- services/ans/include/reminder_data_manager.h | 4 ++-- services/ans/src/reminder_data_manager.cpp | 6 +++--- services/ans/src/reminder_event_manager.cpp | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/services/ans/include/reminder_data_manager.h b/services/ans/include/reminder_data_manager.h index a2e710ccd..bb5cc0d0b 100644 --- a/services/ans/include/reminder_data_manager.h +++ b/services/ans/include/reminder_data_manager.h @@ -97,11 +97,11 @@ public: void InitUserId(); - void OnRemoveUser(const int& userId); + void OnUserRemove(const int& userId); void OnServiceStart(); - void OnSwitchUser(const int& userId); + void OnUserSwitch(const int& userId); /** * @brief Triggered when third party application died. diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index ed1f43312..0a00d2b70 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -231,7 +231,7 @@ void ReminderDataManager::AddToShowedReminders(const sptr &remi showedReminderVector_.push_back(reminder); } -void ReminderDataManager::OnRemoveUser(const int& userId) +void ReminderDataManager::OnUserRemove(const int& userId) { ANSR_LOGD("Remove user id: %{public}d", userId); if (!IsReminderAgentReady()) { @@ -250,13 +250,13 @@ void ReminderDataManager::OnServiceStart() StartRecentReminder(); } -void ReminderDataManager::OnSwitchUser(const int& userId) +void ReminderDataManager::OnUserSwitch(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"); + TerminateAlerting(alertingReminder_, "OnUserSwitch"); } } diff --git a/services/ans/src/reminder_event_manager.cpp b/services/ans/src/reminder_event_manager.cpp index 723916eba..69fae0614 100644 --- a/services/ans/src/reminder_event_manager.cpp +++ b/services/ans/src/reminder_event_manager.cpp @@ -134,11 +134,11 @@ void ReminderEventManager::ReminderEventSubscriber::OnReceiveEvent(const EventFw return; } if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { - reminderDataManager_->OnSwitchUser(data.GetCode()); + reminderDataManager_->OnUserSwitch(data.GetCode()); return; } if (action == CommonEventSupport::COMMON_EVENT_USER_REMOVED) { - reminderDataManager_->OnRemoveUser(data.GetCode()); + reminderDataManager_->OnUserRemove(data.GetCode()); return; } } -- Gitee