diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 4e7890bf750373b7de7df5c41fd66417be1fbffb..8e5428d2b209b2994657120b1554b2bb60ec074f 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1256,7 +1256,7 @@ private: void AdjustDateForDndTypeOnce(int64_t &beginDate, int64_t &endDate); ErrCode PrepareNotificationRequest(const sptr &request); ErrCode PrepareContinuousTaskNotificationRequest(const sptr &request, const int32_t &uid); - static bool GetActiveUserId(int& userId); + void TriggerRemoveWantAgent(const sptr &request); bool CheckApiCompatibility(const sptr &bundleOption); ErrCode IsAllowedNotifySelf(const sptr &bundleOption, bool &allowed); diff --git a/services/ans/include/notification_dialog.h b/services/ans/include/notification_dialog.h index ea37aa5ae1d12164ae7777ff503a34d3a77795e6..941e7f47f44ae5fc3e3283be2b694ec3bd47fa85 100644 --- a/services/ans/include/notification_dialog.h +++ b/services/ans/include/notification_dialog.h @@ -36,7 +36,6 @@ public: std::string appBundleName, const sptr &callerToken); - static int32_t GetActiveUserId(); static int32_t GetUidByBundleName(const std::string &bundleName); }; } // namespace Notification diff --git a/services/ans/src/advanced_datashare_helper_ext.cpp b/services/ans/src/advanced_datashare_helper_ext.cpp index 347f9dd032be762cbdceeeb1ee65743bb9551850..48415abff7317c471c889ee67b593df97e72731e 100644 --- a/services/ans/src/advanced_datashare_helper_ext.cpp +++ b/services/ans/src/advanced_datashare_helper_ext.cpp @@ -20,8 +20,10 @@ #include "iservice_registry.h" #include "message_parcel.h" #include "os_account_manager.h" +#include "os_account_manager_helper.h" #include "singleton.h" #include "system_ability_definition.h" +#include namespace OHOS { namespace Notification { @@ -88,13 +90,9 @@ bool AdvancedDatashareHelperExt::Query(Uri &uri, const std::string &key, std::st std::string AdvancedDatashareHelperExt::GetUnifiedGroupEnableUri() const { - std::vector accountIds; - OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountIds); - std::string userId = "100"; - if (!accountIds.empty()) { - userId = std::to_string(accountIds[0]); - } - return USER_SETTINGS_DATA_SECURE_URI + userId + UNIFIED_GROUP_ENABLE_URI; + int32_t userId = 100; + OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId); + return USER_SETTINGS_DATA_SECURE_URI + std::to_string(userId) + UNIFIED_GROUP_ENABLE_URI; } } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index 35e5ea9ca0cfcff9fb056b7d304c952350ab37ee..0d1c6fb05d149d32933551c8a0b6df12e87f390c 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -37,6 +37,7 @@ #include "common_event_manager.h" #include "common_event_support.h" #include "common_event_publish_info.h" +#include "os_account_manager_helper.h" #include "want_params_wrapper.h" #include "ans_convert_enum.h" #include "notification_analytics_util.h" @@ -682,7 +683,7 @@ ErrCode AdvancedNotificationService::DeleteAll() ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { ANS_LOGD("ffrt enter!"); int32_t activeUserId = SUBSCRIBE_USER_INIT; - if (!GetActiveUserId(activeUserId)) { + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(activeUserId) != ERR_OK) { return; } std::vector keys = GetNotificationKeys(nullptr); @@ -947,7 +948,7 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForAllBundles(const } int32_t userId = SUBSCRIBE_USER_INIT; - if (!GetActiveUserId(userId)) { + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { return ERR_ANS_GET_ACTIVE_USER_FAILED; } @@ -1054,7 +1055,7 @@ ErrCode AdvancedNotificationService::IsAllowedNotify(bool &allowed) } int32_t userId = SUBSCRIBE_USER_INIT; - if (!GetActiveUserId(userId)) { + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { return ERR_ANS_GET_ACTIVE_USER_FAILED; } @@ -1185,7 +1186,7 @@ ErrCode AdvancedNotificationService::IsAllowedNotifySelf(const sptrGetOwnerUid() == DEFAULT_UID) { - GetActiveUserId(userId); + OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId); uid = bundleManager->GetDefaultUidByBundleName(request->GetOwnerBundleName(), userId); } else { uid = request->GetOwnerUid(); diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 22f669fc12b372f76efd5cd571ecbbdda19c555f..311d03163429648a429c995e0b944e6d9bba3d96 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -32,6 +32,7 @@ #include "os_account_manager.h" #include "notification_preferences.h" #include "distributed_database.h" +#include "os_account_manager_helper.h" #include "singleton.h" #include "want_agent_helper.h" #include "hitrace_meter.h" @@ -137,7 +138,7 @@ sptr AdvancedNotificationService::GenerateValidBundleO std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); if (bundleManager != nullptr) { int32_t activeUserId = -1; - if (!GetActiveUserId(activeUserId)) { + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(activeUserId) != ERR_OK) { ANS_LOGE("Failed to get active user id!"); return validBundleOption; } @@ -322,7 +323,7 @@ void AdvancedNotificationService::SetAgentNotification(sptr { auto bundleManager = BundleManagerHelper::GetInstance(); int32_t activeUserId = -1; - if (!GetActiveUserId(activeUserId)) { + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(activeUserId) != ERR_OK) { ANSR_LOGW("Failed to get active user id!"); return; } @@ -772,7 +773,7 @@ ErrCode AdvancedNotificationService::SetDoNotDisturbDate(const sptrsubmit(std::bind([this, deviceId, bundleName, label, id]() { ANS_LOGD("ffrt enter!"); int32_t activeUserId = -1; - if (!GetActiveUserId(activeUserId)) { + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(activeUserId) != ERR_OK) { ANS_LOGE("Failed to get active user id!"); return; } @@ -1277,18 +1278,6 @@ ErrCode AdvancedNotificationService::IsSupportTemplate(const std::string& templa return result; } -bool AdvancedNotificationService::GetActiveUserId(int& userId) -{ - std::vector activeUserId; - OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); - if (activeUserId.size() > 0) { - userId = activeUserId[0]; - ANS_LOGD("Return active userId=%{public}d", userId); - return true; - } - return false; -} - void AdvancedNotificationService::TriggerRemoveWantAgent(const sptr &request) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); @@ -1907,7 +1896,7 @@ ErrCode AdvancedNotificationService::CheckBundleOptionValid(sptr AdvancedNotificationService::GetBundlesOfAct } std::vector activeUserId; - AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); + OsAccountManagerHelper::GetInstance().GetAllActiveOsAccount(activeUserId); if (activeUserId.empty()) { activeUserId.push_back(MAIN_USER_ID); } diff --git a/services/ans/src/clone/notification_clone_util.cpp b/services/ans/src/clone/notification_clone_util.cpp index eeb074b799ea929195c79b00da59c6dbdc31b53f..38590b55d87f0f60baaf9757f74e1a563e07d264 100644 --- a/services/ans/src/clone/notification_clone_util.cpp +++ b/services/ans/src/clone/notification_clone_util.cpp @@ -17,6 +17,7 @@ #include "bundle_manager_helper.h" #include "os_account_manager.h" +#include "os_account_manager_helper.h" namespace OHOS { namespace Notification { @@ -24,12 +25,9 @@ namespace Notification { constexpr int32_t MAIN_USER_ID = 100; int32_t NotificationCloneUtil::GetActiveUserId() { - std::vector activeUserId; - OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); - if (activeUserId.size() > 0) { - return activeUserId[0]; - } - return MAIN_USER_ID; + int32_t userId = MAIN_USER_ID; + OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId); + return userId; } int32_t NotificationCloneUtil::GetBundleUid(const std::string bundleName, int32_t userId, int32_t appIndex) diff --git a/services/ans/src/notification_dialog.cpp b/services/ans/src/notification_dialog.cpp index cd01f5b963f95fe02a7db0c7bef3fcf22b7691c8..8b635cf9f1112fcf978d5d55f6f5d89b59519d9b 100644 --- a/services/ans/src/notification_dialog.cpp +++ b/services/ans/src/notification_dialog.cpp @@ -21,32 +21,18 @@ #include "bundle_manager_helper.h" #include "in_process_call_wrapper.h" #include "os_account_manager.h" +#include "os_account_manager_helper.h" #include "system_dialog_connect_stb.h" #include "extension_manager_client.h" namespace OHOS { namespace Notification { constexpr int32_t DEFAULT_VALUE = -1; -int32_t NotificationDialog::GetActiveUserId() -{ - std::vector activeUserId; - auto errCode = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); - if (errCode != ERR_OK) { - ANS_LOGE("Query active accountIds failed with %{public}d.", errCode); - return AppExecFwk::Constants::ANY_USERID; - } - - if (activeUserId.empty()) { - ANS_LOGE("Active accountIds is empty."); - return AppExecFwk::Constants::ANY_USERID; - } - - return activeUserId.front(); -} int32_t NotificationDialog::GetUidByBundleName(const std::string &bundleName) { - auto userId = NotificationDialog::GetActiveUserId(); + int32_t userId = AppExecFwk::Constants::ANY_USERID; + OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId); return IN_PROCESS_CALL(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, userId)); } diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 3e1cdcb4db19db1c8bd2f269992c90e43f42ca68..1f34ea962c98ee25aac381f7cbc03d2ec5723669 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -832,9 +832,9 @@ bool NotificationPreferencesDatabase::HandleDataBaseMap( { std::regex matchBundlenamePattern("^ans_bundle_(.*)_name$"); std::smatch match; - std::vector ids; + int32_t currentUserId = SUBSCRIBE_USER_INIT; ErrCode result = ERR_OK; - result = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids); + result = OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(currentUserId); if (result != ERR_OK) { ANS_LOGE("Get account id fail"); return false; @@ -859,13 +859,12 @@ bool NotificationPreferencesDatabase::HandleDataBaseMap( continue; } int userid = -1; - constexpr int FIRST_USERID = 0; result = - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(StringToInt(uidItem->second), userid); + OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(StringToInt(uidItem->second), userid); if (result != ERR_OK) { return false; } - if (userid != ids[FIRST_USERID]) { + if (userid != currentUserId) { continue; } NotificationBundleOption obj(value, StringToInt(uidItem->second)); diff --git a/services/ans/src/os_account_manager_helper.cpp b/services/ans/src/os_account_manager_helper.cpp index fa92124aa162fee16f2c00855a3f982562f4b465..1355bfec8b98ffdc81db235e1d4af51444868dee 100644 --- a/services/ans/src/os_account_manager_helper.cpp +++ b/services/ans/src/os_account_manager_helper.cpp @@ -57,10 +57,12 @@ ErrCode OsAccountManagerHelper::GetCurrentCallingUserId(int32_t &userId) ErrCode OsAccountManagerHelper::GetCurrentActiveUserId(int32_t &id) { - std::vector activeUserId; - int32_t ret = GetAllActiveOsAccount(activeUserId); - if (activeUserId.size() > 0) { - id = activeUserId[0]; + int32_t ret = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(id); + if (ret != ERR_OK) { + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_4) + .Message("Get foreground os account failed ret " + std::to_string(ret)); + NotificationAnalyticsUtil::ReportModifyEvent(message); + ANS_LOGE("Failed to call OsAccountManager::GetForegroundOsAccountLocalId, code is %{public}d", ret); } return ret; } diff --git a/services/ans/src/reminder_data_manager.cpp b/services/ans/src/reminder_data_manager.cpp index 87c0556f89bd50ae4dcaba498a1b6d2bf153928e..4494bb21c8672bb4869b71bc5df2fef88e91773b 100644 --- a/services/ans/src/reminder_data_manager.cpp +++ b/services/ans/src/reminder_data_manager.cpp @@ -1425,15 +1425,8 @@ void ReminderDataManager::CheckReminderTime(std::vector>& void ReminderDataManager::InitUserId() { - std::vector activeUserId; - AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); - if (activeUserId.size() > 0) { - currentUserId_ = activeUserId[0]; - ANSR_LOGD("Init user id=%{private}d", currentUserId_); - } else { - currentUserId_ = MAIN_USER_ID; - ANSR_LOGE("Failed to get active user id."); - } + currentUserId_ = MAIN_USER_ID; + OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(currentUserId_); } bool ReminderDataManager::RegisterConfigurationObserver() diff --git a/services/ans/test/unittest/notification_dialog_test/mock_os_account_manager_annex.cpp b/services/ans/test/unittest/notification_dialog_test/mock_os_account_manager_annex.cpp index 067b2eff5163b000d67eabaefd5fc9dc0ec5740c..8be5780b35d8bdd2c36a8f184568184b77c273e4 100644 --- a/services/ans/test/unittest/notification_dialog_test/mock_os_account_manager_annex.cpp +++ b/services/ans/test/unittest/notification_dialog_test/mock_os_account_manager_annex.cpp @@ -17,15 +17,15 @@ namespace { int32_t g_mockId = 100; // default id when there is no os_account part -bool g_mockQueryActiveOsAccountIdsRet = true; +bool g_mockQueryForgroundOsAccountRet = true; bool g_mockGetOsAccountLocalIdFromUidRet = true; int32_t g_mockIdForGetOsAccountLocalIdFromUid = 100; bool g_mockOsAccountExists = true; } -void MockQueryActiveOsAccountIds(bool mockRet, uint8_t mockCase) +void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase) { - g_mockQueryActiveOsAccountIdsRet = mockRet; + g_mockQueryForgroundOsAccountRet = mockRet; switch (mockCase) { case 1: { g_mockId = 101; // 101 mockcase1 @@ -46,7 +46,7 @@ void MockIsOsAccountExists(bool mockRet) void ResetAccountMock() { g_mockId = 100; // 100 mockId - g_mockQueryActiveOsAccountIdsRet = true; + g_mockQueryForgroundOsAccountRet = true; g_mockGetOsAccountLocalIdFromUidRet = true; g_mockIdForGetOsAccountLocalIdFromUid = 100; g_mockOsAccountExists = true; @@ -73,12 +73,12 @@ void MockGetOsAccountLocalIdFromUid(bool mockRet, uint8_t mockCase = 0) namespace OHOS { namespace AccountSA { -ErrCode OsAccountManager::QueryActiveOsAccountIds(std::vector& ids) +ErrCode OsAccountManager::GetForegroundOsAccountLocalId(int32_t &id) { - if (!g_mockQueryActiveOsAccountIdsRet) { + if (!g_mockQueryForgroundOsAccountRet) { return ERR_INVALID_OPERATION; } - ids.emplace_back(g_mockId); + id = g_mockId; return ERR_OK; } diff --git a/services/ans/test/unittest/notification_dialog_test/notification_dialog_test.cpp b/services/ans/test/unittest/notification_dialog_test/notification_dialog_test.cpp index fc2cde99a4e3f55f1aefac09181119605852011e..a43ffabdb159d0409b6eb5cd96facf61330cdbc9 100644 --- a/services/ans/test/unittest/notification_dialog_test/notification_dialog_test.cpp +++ b/services/ans/test/unittest/notification_dialog_test/notification_dialog_test.cpp @@ -24,7 +24,7 @@ #undef protected #include "ans_inner_errors.h" -extern void MockQueryActiveOsAccountIds(bool mockRet, uint8_t mockCase); +extern void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase); using namespace testing::ext; @@ -38,30 +38,14 @@ public: void TearDown() {}; }; -/** - * @tc.name : NotificationDialog_00100 - * @tc.number : - * @tc.desc : test GetActiveUserId function and QueryActiveOsAccountIds is ERR_OK - */ -HWTEST_F(NotificationDialogTest, NotificationDialog_00100, Function | SmallTest | Level1) -{ - MockQueryActiveOsAccountIds(true, 0); - int32_t result = NotificationDialog::GetActiveUserId(); - int32_t activeUserId = 100; - ASSERT_EQ(result, activeUserId); -} - /** * @tc.name : NotificationDialog_00200 * @tc.number : - * @tc.desc : test GetActiveUserId function and QueryActiveOsAccountIds is ERR_INVALID_OPERATION + * @tc.desc : test QueryActiveOsAccountIds is ERR_INVALID_OPERATION */ HWTEST_F(NotificationDialogTest, NotificationDialog_00200, Function | SmallTest | Level1) { - MockQueryActiveOsAccountIds(false, 1); - int32_t result = NotificationDialog::GetActiveUserId(); - int32_t activeUserId = -4; - ASSERT_EQ(result, activeUserId); + MockQueryForgroundOsAccountId(false, 1); std::string bundleName = "BundleName"; int32_t result2 = NotificationDialog::GetUidByBundleName(bundleName); @@ -76,10 +60,7 @@ HWTEST_F(NotificationDialogTest, NotificationDialog_00200, Function | SmallTest */ HWTEST_F(NotificationDialogTest, NotificationDialog_00300, Function | SmallTest | Level1) { - MockQueryActiveOsAccountIds(false, 1); - int32_t result = NotificationDialog::GetActiveUserId(); - int32_t activeUserId = -4; - ASSERT_EQ(result, activeUserId); + MockQueryForgroundOsAccountId(false, 1); std::string bundleName = "BundleName"; int32_t result2 = NotificationDialog::GetUidByBundleName(bundleName); @@ -104,10 +85,7 @@ HWTEST_F(NotificationDialogTest, NotificationDialog_00300, Function | SmallTest */ HWTEST_F(NotificationDialogTest, NotificationDialog_00400, Function | SmallTest | Level1) { - MockQueryActiveOsAccountIds(false, 1); - int32_t result = NotificationDialog::GetActiveUserId(); - int32_t activeUserId = -4; - ASSERT_EQ(result, activeUserId); + MockQueryForgroundOsAccountId(false, 1); std::string bundleName = "BundleName"; int32_t result2 = NotificationDialog::GetUidByBundleName(bundleName); diff --git a/services/ans/test/unittest/notification_subscriber_manager_branch_test/notification_subscriber_manager_branch_test.cpp b/services/ans/test/unittest/notification_subscriber_manager_branch_test/notification_subscriber_manager_branch_test.cpp index 125426ef7e816be50d62fa67023796ce8f7167aa..5387aee08d41493d2c3a237a04d1664171beda76 100644 --- a/services/ans/test/unittest/notification_subscriber_manager_branch_test/notification_subscriber_manager_branch_test.cpp +++ b/services/ans/test/unittest/notification_subscriber_manager_branch_test/notification_subscriber_manager_branch_test.cpp @@ -30,7 +30,7 @@ extern void MockGetUserId(bool mockRet); extern void MockGetBundleName(bool mockRet); extern void MockGetNotificationSlotRet(bool mockRet); -extern void MockQueryActiveOsAccountIds(bool mockRet, uint8_t mockCase); +extern void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase); using namespace OHOS::Security::AccessToken; using namespace testing::ext; @@ -639,7 +639,7 @@ HWTEST_F(NotificationSubscriberManagerBranchTest, AdvancedNotificationService_02 std::string deviceId = ""; bool enabled = true; MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); - MockQueryActiveOsAccountIds(false, 1); + MockQueryForgroundOsAccountId(false, 1); AdvancedNotificationService advancedNotificationService; ASSERT_EQ(advancedNotificationService.SetNotificationsEnabledForAllBundles(deviceId, enabled), @@ -689,7 +689,7 @@ HWTEST_F(NotificationSubscriberManagerBranchTest, AdvancedNotificationService_03 bool enabled = true; MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); - MockQueryActiveOsAccountIds(false, 1); + MockQueryForgroundOsAccountId(false, 1); AdvancedNotificationService advancedNotificationService; ASSERT_EQ(advancedNotificationService.IsAllowedNotify(enabled), ERR_ANS_GET_ACTIVE_USER_FAILED); } @@ -735,7 +735,7 @@ HWTEST_F(NotificationSubscriberManagerBranchTest, AdvancedNotificationService_03 HWTEST_F(NotificationSubscriberManagerBranchTest, AdvancedNotificationService_03400, Function | SmallTest | Level1) { sptr bundleOption = new NotificationBundleOption(); - MockQueryActiveOsAccountIds(false, 1); + MockQueryForgroundOsAccountId(false, 1); bool allowed = true; int32_t uid = 2; @@ -923,7 +923,7 @@ HWTEST_F(NotificationSubscriberManagerBranchTest, AdvancedNotificationService_04 HWTEST_F(NotificationSubscriberManagerBranchTest, AdvancedNotificationService_04500, Function | SmallTest | Level1) { sptr date = nullptr; - MockQueryActiveOsAccountIds(false, 1); + MockQueryForgroundOsAccountId(false, 1); MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); AdvancedNotificationService advancedNotificationService; @@ -953,7 +953,7 @@ HWTEST_F(NotificationSubscriberManagerBranchTest, AdvancedNotificationService_04 HWTEST_F(NotificationSubscriberManagerBranchTest, AdvancedNotificationService_04700, Function | SmallTest | Level1) { sptr date = nullptr; - MockQueryActiveOsAccountIds(false, 1); + MockQueryForgroundOsAccountId(false, 1); MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE); AdvancedNotificationService advancedNotificationService;