From c77a198a550b4453aafd8fa0dc961959b6b26626 Mon Sep 17 00:00:00 2001 From: xinking129 Date: Mon, 30 Oct 2023 20:16:30 +0800 Subject: [PATCH] change code Signed-off-by: xinking129 --- .../include/advanced_notification_service.h | 7 ++ services/ans/include/interface_system_event.h | 1 + services/ans/include/system_event_observer.h | 10 +- .../ans/src/advanced_notification_service.cpp | 118 +++++++++--------- services/ans/src/reminder_data_manager.cpp | 5 - services/ans/src/system_event_observer.cpp | 68 ++++++++-- 6 files changed, 132 insertions(+), 77 deletions(-) diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 6849f01dd..70511cfe7 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -715,6 +715,11 @@ public: */ void OnBundleDataUpdate(const sptr &bundleOption); + /** + * @brief Boot system completed event callback. + */ + void OnBootSystemCompleted(); + // Distributed KvStore /** @@ -882,6 +887,8 @@ private: // Might fail if ces subscribe failed, if failed, dialogManager_ will be set nullptr bool CreateDialogManager(); + static bool GetBundleInfoByNotificationBundleOption( + const sptr &bundleOption, AppExecFwk::BundleInfo &bundleInfo); private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/include/interface_system_event.h b/services/ans/include/interface_system_event.h index ad2dbac19..71cf783da 100644 --- a/services/ans/include/interface_system_event.h +++ b/services/ans/include/interface_system_event.h @@ -33,6 +33,7 @@ struct ISystemEvent { std::function &)> onBundleDataCleared; std::function &)> onBundleAdd; std::function &)> onBundleUpdate; + std::function onBootSystemCompleted; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/system_event_observer.h b/services/ans/include/system_event_observer.h index 4d99c03b8..7be639803 100644 --- a/services/ans/include/system_event_observer.h +++ b/services/ans/include/system_event_observer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -42,11 +42,19 @@ public: private: void OnReceiveEvent(const EventFwk::CommonEventData &data); + void OnReceiveEventInner(const EventFwk::CommonEventData &data); sptr GetBundleOption(AAFwk::Want want); + void InitEventList(); + + void OnBundleUpdateEventInner(const EventFwk::CommonEventData &data); + void OnBundleAddEventInner(const EventFwk::CommonEventData &data); + void OnBootSystemCompletedEventInner(const EventFwk::CommonEventData &data); private: std::shared_ptr subscriber_ = nullptr; ISystemEvent callbacks_; + using SystemEventObserverFunc = void (SystemEventObserver::*)(const EventFwk::CommonEventData &data); + std::map memberFuncMap_; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 400f27955..49a8faf87 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -61,6 +61,18 @@ #include "distributed_screen_status_manager.h" #endif +#define CHECK_BUNDLE_OPTION_IS_INVALID(option) \ + if (option == nullptr || option->GetBundleName().empty()) { \ + ANS_LOGE("Bundle option sptr is null or bundle name is empty!"); \ + return; \ + } + +#define CHECK_BUNDLE_OPTION_IS_INVALID_WITH_RETURN(option, retVal) \ + if (option == nullptr || option->GetBundleName().empty()) { \ + ANS_LOGE("Bundle option sptr is null or bundle name is empty!"); \ + return retVal; \ + } + namespace OHOS { namespace Notification { namespace { @@ -305,6 +317,7 @@ AdvancedNotificationService::AdvancedNotificationService() std::bind(&AdvancedNotificationService::OnBundleDataCleared, this, std::placeholders::_1), std::bind(&AdvancedNotificationService::OnBundleDataAdd, this, std::placeholders::_1), std::bind(&AdvancedNotificationService::OnBundleDataUpdate, this, std::placeholders::_1), + std::bind(&AdvancedNotificationService::OnBootSystemCompleted, this), }; systemEventObserver_ = std::make_shared(iSystemEvent); @@ -593,14 +606,10 @@ ErrCode AdvancedNotificationService::Publish(const std::string &label, const spt } do { - bool notificationEnable = false; + if (request->GetReceiverUserId() != SUBSCRIBE_USER_INIT) { + bool notificationEnable = false; result = CheckNotificationEnableStatus(notificationEnable); - if (result != ERR_OK) { - result = ERR_ANS_INVALID_BUNDLE; - ANS_LOGE("Bundle notification enable not found!"); - break; - } if (notificationEnable) { result = PublishPreparedNotificationInner(request); break; @@ -2520,38 +2529,21 @@ void AdvancedNotificationService::OnBundleRemoved(const sptr &bundleOption) { - if (bundleOption == nullptr) { - ANS_LOGE("Bundle option sptr is null!"); - return; - } - if (bundleOption->GetBundleName().empty()) { - ANS_LOGE("Bundle name empty!"); - return; - } - + CHECK_BUNDLE_OPTION_IS_INVALID(bundleOption) auto bundleInstall = [bundleOption]() { - if (bundleOption == nullptr) { - ANS_LOGE("Bundle option sptr is null!"); - return; - } - + CHECK_BUNDLE_OPTION_IS_INVALID(bundleOption) AppExecFwk::BundleInfo bundleInfo; - int32_t callingUserId = -1; - AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), callingUserId); - auto bundleMgr = BundleManagerHelper::GetInstance(); - if (bundleMgr == nullptr) { - ANS_LOGE("bundleMgr instance error!"); - return; - } - if (!bundleMgr->GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { - ANS_LOGE("Get bundle info error!"); + if (!GetBundleInfoByNotificationBundleOption(bundleOption, bundleInfo)) { + ANS_LOGE("Failed to get BundleInfo using NotificationBundleOption."); return; } - auto errCode = NotificationPreferences::GetInstance().SetNotificationsEnabledForBundle( - bundleOption, bundleInfo.applicationInfo.allowEnableNotification); - if (errCode != ERR_OK) { - ANS_LOGE("Set notification enable error! code: %{public}d", errCode); + // In order to adapt to the publish reminder interface, currently only the input from the whitelist is written + if (bundleInfo.applicationInfo.allowEnableNotification) { + auto errCode = NotificationPreferences::GetInstance().SetNotificationsEnabledForBundle(bundleOption, true); + if (errCode != ERR_OK) { + ANS_LOGE("Set notification enable error! code: %{public}d", errCode); + } } }; @@ -2560,31 +2552,17 @@ void AdvancedNotificationService::OnBundleDataAdd(const sptr &bundleOption) { - if (bundleOption == nullptr) { - ANS_LOGE("Bundle option sptr is null!"); - return; - } - if (bundleOption->GetBundleName().empty()) { - ANS_LOGE("Bundle name empty!"); - return; - } - + CHECK_BUNDLE_OPTION_IS_INVALID(bundleOption) auto bundleUpdate = [bundleOption]() { - if (bundleOption == nullptr) { - ANS_LOGE("Bundle option sptr is null!"); - return; - } - + CHECK_BUNDLE_OPTION_IS_INVALID(bundleOption) AppExecFwk::BundleInfo bundleInfo; - int32_t callingUserId = -1; - AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), callingUserId); - auto bundleMgr = BundleManagerHelper::GetInstance(); - if (bundleMgr == nullptr) { - ANS_LOGE("bundleMgr instance error!"); + if (!GetBundleInfoByNotificationBundleOption(bundleOption, bundleInfo)) { + ANS_LOGE("Failed to get BundleInfo using NotificationBundleOption."); return; } - if (!bundleMgr->GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { - ANS_LOGE("Get bundle info error!"); + + if (!bundleInfo.applicationInfo.allowEnableNotification) { + ANS_LOGE("Current application allowEnableNotification is false, do not record."); return; } @@ -2615,6 +2593,12 @@ void AdvancedNotificationService::OnBundleDataUpdate(const sptrsubmit(bundleUpdate) : bundleUpdate(); } +void AdvancedNotificationService::OnBootSystemCompleted() +{ + ANS_LOGI("Called."); + InitNotificationEnableList(); +} + #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED void AdvancedNotificationService::OnScreenOn() { @@ -4940,7 +4924,6 @@ void AdvancedNotificationService::InitNotificationEnableList() ANS_LOGE("Get bundle mgr error!"); return; } - std::vector activeUserId; AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); if (activeUserId.empty()) { @@ -4956,10 +4939,13 @@ void AdvancedNotificationService::InitNotificationEnableList() } bundleInfos.insert(bundleInfos.end(), infos.begin(), infos.end()); } - bool notificationEnable = false; ErrCode saveRef = ERR_OK; for (const auto &bundleInfo : bundleInfos) { + // Currently only the input from the whitelist is written + if (!bundleInfo.applicationInfo.allowEnableNotification) { + continue; + } sptr bundleOption = new (std::nothrow) NotificationBundleOption( bundleInfo.applicationInfo.bundleName, bundleInfo.uid); if (bundleOption == nullptr) { @@ -4967,14 +4953,12 @@ void AdvancedNotificationService::InitNotificationEnableList() bundleInfo.applicationInfo.bundleName.c_str()); continue; } - saveRef = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle( bundleOption, notificationEnable); // record already exists if (saveRef == ERR_OK) { continue; } - saveRef = NotificationPreferences::GetInstance().SetNotificationsEnabledForBundle( bundleOption, bundleInfo.applicationInfo.allowEnableNotification); if (saveRef != ERR_OK) { @@ -4982,7 +4966,6 @@ void AdvancedNotificationService::InitNotificationEnableList() } } }; - notificationSvrQueue_ != nullptr ? notificationSvrQueue_->submit(task) : task(); } @@ -5039,6 +5022,23 @@ bool AdvancedNotificationService::CreateDialogManager() } return true; } +bool AdvancedNotificationService::GetBundleInfoByNotificationBundleOption( + const sptr &bundleOption, AppExecFwk::BundleInfo &bundleInfo) +{ + CHECK_BUNDLE_OPTION_IS_INVALID_WITH_RETURN(bundleOption, false) + int32_t callingUserId = -1; + AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), callingUserId); + auto bundleMgr = BundleManagerHelper::GetInstance(); + if (bundleMgr == nullptr) { + ANS_LOGE("bundleMgr instance error!"); + return false; + } + if (!bundleMgr->GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { + ANS_LOGE("Get bundle info error!"); + return false; + } + return true; +} void PushCallbackRecipient::OnRemoteDied(const wptr &remote) { diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index 200a4eb39..54026ad07 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -1270,11 +1270,6 @@ void ReminderDataManager::HandleSameNotificationIdShowing(const sptrInitNotificationEnableList(); - } - if (IsReminderAgentReady()) { return; } diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index dc6ad1072..2cc27feed 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -37,6 +37,7 @@ SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callba matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED); + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED); EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills); commonEventSubscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON); @@ -44,6 +45,7 @@ SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callba commonEventSubscribeInfo, std::bind(&SystemEventObserver::OnReceiveEvent, this, std::placeholders::_1)); EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_); + InitEventList(); } SystemEventObserver::~SystemEventObserver() @@ -104,21 +106,63 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) callbacks_.onBundleDataCleared(bundleOption); } } - } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) { - if (callbacks_.onBundleAdd != nullptr) { - sptr bundleOption = GetBundleOption(want); - if (bundleOption != nullptr) { - callbacks_.onBundleAdd(bundleOption); - } + } else { + OnReceiveEventInner(data); + } +} + +void SystemEventObserver::InitEventList() +{ + memberFuncMap_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED] = + &SystemEventObserver::OnBundleAddEventInner; + memberFuncMap_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED] = + &SystemEventObserver::OnBundleUpdateEventInner; + memberFuncMap_[EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED] = + &SystemEventObserver::OnBootSystemCompletedEventInner; +} + +void SystemEventObserver::OnReceiveEventInner(const EventFwk::CommonEventData &data) +{ + std::string action = data.GetWant().GetAction(); + auto itFunc = memberFuncMap_.find(action); + if (itFunc == memberFuncMap_.end()) { + ANS_LOGE("Action %{public}s callback is not found.", action.c_str()); + return; + } + + if (itFunc->second == nullptr) { + ANS_LOGE("Action [%{public}s] callback is nullptr.", action.c_str()); + return; + } + + (this->*(itFunc->second))(data); +} + +void SystemEventObserver::OnBundleAddEventInner(const EventFwk::CommonEventData &data) +{ + if (callbacks_.onBundleAdd != nullptr) { + sptr bundleOption = GetBundleOption(data.GetWant()); + if (bundleOption != nullptr) { + callbacks_.onBundleAdd(bundleOption); } - } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) { - if (callbacks_.onBundleUpdate != nullptr) { - sptr bundleOption = GetBundleOption(want); - if (bundleOption != nullptr) { - callbacks_.onBundleUpdate(bundleOption); - } + } +} + +void SystemEventObserver::OnBundleUpdateEventInner(const EventFwk::CommonEventData &data) +{ + if (callbacks_.onBundleUpdate != nullptr) { + sptr bundleOption = GetBundleOption(data.GetWant()); + if (bundleOption != nullptr) { + callbacks_.onBundleUpdate(bundleOption); } } } + +void SystemEventObserver::OnBootSystemCompletedEventInner(const EventFwk::CommonEventData &data) +{ + if (callbacks_.onBootSystemCompleted != nullptr) { + callbacks_.onBootSystemCompleted(); + } +} } // namespace Notification } // namespace OHOS -- Gitee