From 03c6c8bc56f5a7b9fb6593b6dab5e63fc7921000 Mon Sep 17 00:00:00 2001 From: liqiang Date: Wed, 23 Feb 2022 16:26:18 +0800 Subject: [PATCH] IssueNo:https://gitee.com/openharmony/notification_ans_standard/issues/I4UYGI Description:fix ActsAnsEnableNotificationTest excute failed bug Sig: SIG_ApplicationFramework Feature or Bugfix:Bugfix Binary Source:No Signed-off-by: liqiang Change-Id: I4b7771b4a513f5d3de16752c1b4b3eed9d21b7d8 --- services/ans/include/bundle_manager_helper.h | 2 +- .../ans/src/advanced_notification_service.cpp | 27 ++++++++++++++----- services/ans/src/bundle_manager_helper.cpp | 4 +-- .../mock/mock_bundle_manager_helper.cpp | 2 +- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/services/ans/include/bundle_manager_helper.h b/services/ans/include/bundle_manager_helper.h index 9a060faca..1ddaf7918 100644 --- a/services/ans/include/bundle_manager_helper.h +++ b/services/ans/include/bundle_manager_helper.h @@ -33,7 +33,7 @@ class BundleManagerHelper : public DelayedSingleton { public: std::string GetBundleNameByUid(int uid); bool IsSystemApp(int uid); - int GetDefaultUidByBundleName(const std::string &bundle); + int GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId); bool GetBundleInfoByBundleName(const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED bool GetDistributedNotificationEnabled(const std::string &bundleName, const int userId); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 0a68398f5..897ec9b92 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -302,7 +302,12 @@ sptr AdvancedNotificationService::GenerateValidBundleO if (bundleOption->GetUid() <= 0) { std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); if (bundleManager != nullptr) { - int uid = bundleManager->GetDefaultUidByBundleName(bundleOption->GetBundleName()); + int activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return validBundleOption; + } + int uid = bundleManager->GetDefaultUidByBundleName(bundleOption->GetBundleName(), activeUserId); if (uid > 0) { validBundleOption = new NotificationBundleOption(bundleOption->GetBundleName(), uid); } @@ -1576,7 +1581,7 @@ ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(targetBundle, allowed); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; - allowed = CheckApiCompatibility(bundleOption); + allowed = CheckApiCompatibility(targetBundle); } } })); @@ -2737,8 +2742,12 @@ void AdvancedNotificationService::OnDistributedPublish( const std::string &deviceId, const std::string &bundleName, sptr &request) { ANS_LOGD("%{public}s", __FUNCTION__); - - request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName)); + int activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return; + } + request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, activeUserId)); handler_->PostTask(std::bind([this, deviceId, bundleName, request]() { if (!CheckDistributedNotificationType(request)) { @@ -2788,8 +2797,12 @@ void AdvancedNotificationService::OnDistributedUpdate( const std::string &deviceId, const std::string &bundleName, sptr &request) { ANS_LOGD("%{public}s", __FUNCTION__); - - request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName)); + int activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return; + } + request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, activeUserId)); handler_->PostTask(std::bind([this, deviceId, bundleName, request]() { if (!CheckDistributedNotificationType(request)) { @@ -3177,6 +3190,8 @@ bool AdvancedNotificationService::CheckApiCompatibility(const sptrGetUid(), callingUserId); if (bundleManager != nullptr) { if (!bundleManager->GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { + ANS_LOGW("Failed to GetBundleInfoByBundleName, bundlename = %{public}s", + bundleOption->GetBundleName().c_str()); return false; } } diff --git a/services/ans/src/bundle_manager_helper.cpp b/services/ans/src/bundle_manager_helper.cpp index 7afa5a38f..9e965edcb 100644 --- a/services/ans/src/bundle_manager_helper.cpp +++ b/services/ans/src/bundle_manager_helper.cpp @@ -111,7 +111,7 @@ void BundleManagerHelper::Disconnect() } } -int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle) +int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId) { int uid = -1; @@ -121,7 +121,7 @@ int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle) if (bundleMgr_ != nullptr) { AppExecFwk::BundleInfo bundleInfo; - if (bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo)) { + if (bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, userId)) { uid = bundleInfo.uid; } } 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 7528a5c37..6eba48ba6 100644 --- a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp +++ b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp @@ -36,7 +36,7 @@ std::string BundleManagerHelper::GetBundleNameByUid(int uid) return (uid == NON_BUNDLE_NAME_UID) ? "" : "bundleName"; } -int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle) +int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId) { return NON_SYSTEM_APP_UID; } -- Gitee