From 9268f81f8088715033c421ee915e5adac63b4f26 Mon Sep 17 00:00:00 2001 From: baozeyu Date: Sat, 24 Aug 2024 16:18:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=91=E5=B8=83=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E8=A7=92=E6=A0=87=E9=BB=98=E8=AE=A4=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: baozeyu Change-Id: I308b6cfcaa1807f4bc1935eb29c20c3efec77b00 --- .../mock/mock_bundle_manager_helper.cpp | 7 ++++++- services/ans/include/bundle_manager_helper.h | 11 ++++++++++- .../include/notification_preferences_database.h | 1 + .../src/advanced_notification_publish_service.cpp | 4 ++-- services/ans/src/bundle_manager_helper.cpp | 15 ++++++++++++--- .../ans/src/notification_preferences_database.cpp | 12 ++++++++++++ services/ans/test/unittest/BUILD.gn | 1 + .../unittest/mock/mock_bundle_manager_helper.cpp | 7 ++++++- .../mock/mock_bundle_manager_helper.cpp | 7 ++++++- .../mock/mock_bundle_manager_helper.cpp | 7 ++++++- 10 files changed, 62 insertions(+), 10 deletions(-) diff --git a/frameworks/test/moduletest/mock/mock_bundle_manager_helper.cpp b/frameworks/test/moduletest/mock/mock_bundle_manager_helper.cpp index 80609a098..178cc27a4 100644 --- a/frameworks/test/moduletest/mock/mock_bundle_manager_helper.cpp +++ b/frameworks/test/moduletest/mock/mock_bundle_manager_helper.cpp @@ -65,5 +65,10 @@ void BundleManagerHelper::Connect() void BundleManagerHelper::Disconnect() {} + +bool BundleManagerHelper::CheckApiCompatibility(const std::string &bundleName, const int32_t &uid) +{ + return true; +} } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/ans/include/bundle_manager_helper.h b/services/ans/include/bundle_manager_helper.h index ea71a8b83..669694027 100644 --- a/services/ans/include/bundle_manager_helper.h +++ b/services/ans/include/bundle_manager_helper.h @@ -108,6 +108,15 @@ public: bool GetBundleInfos( const AppExecFwk::BundleFlag flag, std::vector &bundleInfos, int32_t userId); + /** + * @brief Check API compatibility. + * + * @param bundleName Indicates the bundle name. + * @param uid Indicates the bundle uid. + * @return Returns the check result. + */ + bool CheckApiCompatibility(const std::string &bundleName, const int32_t &uid); + private: void Connect(); void Disconnect(); @@ -124,4 +133,4 @@ private: } // namespace Notification } // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_BUNDLE_MANAGER_HELPER_H \ No newline at end of file +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_BUNDLE_MANAGER_HELPER_H diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index aafa057b2..2c7b755dd 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -318,6 +318,7 @@ private: const std::string &findString, const int32_t &userId); void ExecuteDisturbeDB(sptr &slot, std::string &typeStr, std::string &valueStr, const std::string &findString, const int32_t &userId); + bool CheckApiCompatibility(const std::string &bundleName, const int32_t &uid); std::shared_ptr rdbDataManager_; }; } // namespace Notification diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index f30d7e6ae..03a1821ed 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -702,7 +702,7 @@ ErrCode AdvancedNotificationService::GetShowBadgeEnabledForBundle( result = NotificationPreferences::GetInstance()->IsShowBadge(bundle, enabled); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; - enabled = false; + enabled = true; } })); notificationSvrQueue_->wait(handler); @@ -728,7 +728,7 @@ ErrCode AdvancedNotificationService::GetShowBadgeEnabled(bool &enabled) result = NotificationPreferences::GetInstance()->IsShowBadge(bundleOption, enabled); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; - enabled = false; + enabled = true; } })); notificationSvrQueue_->wait(handler); diff --git a/services/ans/src/bundle_manager_helper.cpp b/services/ans/src/bundle_manager_helper.cpp index 71ff872a2..5ea086ca1 100644 --- a/services/ans/src/bundle_manager_helper.cpp +++ b/services/ans/src/bundle_manager_helper.cpp @@ -78,13 +78,22 @@ bool BundleManagerHelper::IsSystemApp(int32_t uid) } bool BundleManagerHelper::CheckApiCompatibility(const sptr &bundleOption) +{ + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption is nullptr"); + return false; + } + return CheckApiCompatibility(bundleOption->GetBundleName(), bundleOption->GetUid()); +} + +bool BundleManagerHelper::CheckApiCompatibility(const std::string &bundleName, const int32_t &uid) { AppExecFwk::BundleInfo bundleInfo; int32_t callingUserId; - AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), callingUserId); - if (!GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { + AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, callingUserId); + if (!GetBundleInfoByBundleName(bundleName, callingUserId, bundleInfo)) { ANS_LOGW("Failed to GetBundleInfoByBundleName, bundlename = %{public}s", - bundleOption->GetBundleName().c_str()); + bundleName.c_str()); return false; } diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 1b5629041..421ef1f5d 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -25,6 +25,7 @@ #include "hitrace_meter_adapter.h" #include "os_account_manager.h" #include "ipc_skeleton.h" +#include "bundle_manager_helper.h" #include "uri.h" namespace OHOS { @@ -595,6 +596,7 @@ bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, NotificationPreferencesInfo::BundleInfo bundleInfo; bundleInfo.SetBundleName(bundleName); bundleInfo.SetBundleUid(bundleUid); + bundleInfo.SetEnableNotification(CheckApiCompatibility(bundleName, bundleUid)); result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo); break; } @@ -1978,5 +1980,15 @@ std::string NotificationPreferencesDatabase::GetAdditionalConfig(const std::stri ANS_LOGD("The additional config key is :%{public}s, value is :%{public}s.", key.c_str(), configValue.c_str()); return configValue; } + +bool NotificationPreferencesDatabase::CheckApiCompatibility(const std::string &bundleName, const int32_t &uid) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager == nullptr) { + return false; + } + return bundleManager->CheckApiCompatibility(bundleName, uid); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 5eb12e218..e626898bf 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -1014,6 +1014,7 @@ ohos_unittest("notification_preferences_database_branch_test") { ] sources = [ + "${services_path}/ans/src/bundle_manager_helper.cpp", "${services_path}/ans/src/common/notification_config_parse.cpp", "${services_path}/ans/src/notification_preferences_database.cpp", "${services_path}/ans/src/notification_preferences_info.cpp", diff --git a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp index 501bf9e10..f03a0287d 100644 --- a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp +++ b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp @@ -73,5 +73,10 @@ bool BundleManagerHelper::GetDistributedNotificationEnabled(const std::string &b return true; } #endif + +bool BundleManagerHelper::CheckApiCompatibility(const std::string &bundleName, const int32_t &uid) +{ + return true; +} } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/test/moduletest/mock/mock_bundle_manager_helper.cpp b/services/test/moduletest/mock/mock_bundle_manager_helper.cpp index 080c28ec3..16c668789 100644 --- a/services/test/moduletest/mock/mock_bundle_manager_helper.cpp +++ b/services/test/moduletest/mock/mock_bundle_manager_helper.cpp @@ -65,5 +65,10 @@ void BundleManagerHelper::Connect() void BundleManagerHelper::Disconnect() {} + +bool BundleManagerHelper::CheckApiCompatibility(const std::string &bundleName, const int32_t &uid) +{ + return true; +} } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/test/bechmarktest/notification_services_test/mock/mock_bundle_manager_helper.cpp b/test/bechmarktest/notification_services_test/mock/mock_bundle_manager_helper.cpp index e0d31ddac..127129e1b 100644 --- a/test/bechmarktest/notification_services_test/mock/mock_bundle_manager_helper.cpp +++ b/test/bechmarktest/notification_services_test/mock/mock_bundle_manager_helper.cpp @@ -68,5 +68,10 @@ bool BundleManagerHelper::GetDistributedNotificationEnabled(const std::string &b return true; } #endif + +bool BundleManagerHelper::CheckApiCompatibility(const std::string &bundleName, const int32_t &uid) +{ + return true; +} } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS -- Gitee