From ad396bcb290ef5a24dd938372fad5e4dbea3fc5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A6=E5=9B=BD=E5=BA=86?= Date: Fri, 8 Nov 2024 09:48:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E7=BB=AD=E8=BF=87=E7=A8=8B=E4=B8=AD?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E5=8F=96=E6=B6=88=E6=AD=A3=E5=9C=A8=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=9A=84=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 韦国庆 --- .../mission/dms_continue_recv_manager.h | 3 + .../src/mission/dms_continue_recv_manager.cpp | 87 ++++++++++--------- .../dtbschedmgr/src/multi_user_manager.cpp | 8 +- .../mission/dms_continue_manager_test.cpp | 53 +++++++++++ 4 files changed, 108 insertions(+), 43 deletions(-) diff --git a/services/dtbschedmgr/include/mission/dms_continue_recv_manager.h b/services/dtbschedmgr/include/mission/dms_continue_recv_manager.h index 490ef286..242c9970 100644 --- a/services/dtbschedmgr/include/mission/dms_continue_recv_manager.h +++ b/services/dtbschedmgr/include/mission/dms_continue_recv_manager.h @@ -85,6 +85,7 @@ public: void NotifyPackageRemoved(const std::string& sinkBundleName); void OnDeviceScreenOff(); void OnContinueSwitchOff(); + void OnUserSwitch(); std::string GetContinueType(const std::string& bundleName); bool CheckRegSoftbusListener(); @@ -106,6 +107,8 @@ private: bool IsBundleContinuable(const AppExecFwk::BundleInfo& bundleInfo, const std::string &srcAbilityName, const std::string &srcContinueType, bool isSameBundle); std::string ContinueTypeFormat(const std::string &continueType); + void FindToNotifyRecvBroadcast(const std::string& senderNetworkId, const std::string& bundleName, + const std::string& continueType); private: currentIconInfo iconInfo_; sptr missionDiedListener_; diff --git a/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp b/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp index 105eb6cc..35539618 100644 --- a/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp +++ b/services/dtbschedmgr/src/mission/dms_continue_recv_manager.cpp @@ -508,6 +508,23 @@ void DMSContinueRecvMgr::OnDeviceScreenOff() } #endif +void DMSContinueRecvMgr::FindToNotifyRecvBroadcast(const std::string& senderNetworkId, const std::string& bundleName, + const std::string& continueType) +{ + std::lock_guard registerOnListenerMapLock(eventMutex_); + auto iterItem = registerOnListener_.find(onType_); + if (iterItem == registerOnListener_.end()) { + HILOGI("Get iterItem failed from registerOnListener_, nobody registed"); + return; + } + std::vector> objs = iterItem->second; + for (auto iter : objs) { + NotifyRecvBroadcast(iter, + currentIconInfo(senderNetworkId, iconInfo_.sourceBundleName, bundleName, continueType), + INACTIVE); + } +} + void DMSContinueRecvMgr::OnContinueSwitchOff() { auto func = [this]() { @@ -529,20 +546,7 @@ void DMSContinueRecvMgr::OnContinueSwitchOff() } HILOGI("Saved iconInfo cleared, networkId: %{public}s, bundleName: %{public}s.", GetAnonymStr(senderNetworkId).c_str(), bundleName.c_str()); - { - std::lock_guard registerOnListenerMapLock(eventMutex_); - auto iterItem = registerOnListener_.find(onType_); - if (iterItem == registerOnListener_.end()) { - HILOGI("Get iterItem failed from registerOnListener_, nobody registed"); - return; - } - std::vector> objs = iterItem->second; - for (auto iter : objs) { - NotifyRecvBroadcast(iter, - currentIconInfo(senderNetworkId, iconInfo_.sourceBundleName, bundleName, continueType), - INACTIVE); - } - } + FindToNotifyRecvBroadcast(senderNetworkId, bundleName, continueType); }; if (eventHandler_ == nullptr) { HILOGE("eventHandler_ is nullptr"); @@ -551,6 +555,31 @@ void DMSContinueRecvMgr::OnContinueSwitchOff() eventHandler_->PostTask(func); } +void DMSContinueRecvMgr::OnUserSwitch() +{ + HILOGI("OnUserSwitch start."); + std::string senderNetworkId; + std::string bundleName; + std::string continueType; + { + std::lock_guard currentIconLock(iconMutex_); + if (iconInfo_.isEmpty()) { + HILOGW("Saved iconInfo has already been cleared, task abort."); + return; + } + senderNetworkId = iconInfo_.senderNetworkId; + bundleName = iconInfo_.bundleName; + continueType = iconInfo_.continueType; + iconInfo_.senderNetworkId = ""; + iconInfo_.bundleName = ""; + iconInfo_.continueType = ""; + } + HILOGI("Saved iconInfo cleared, networkId: %{public}s, bundleName: %{public}s.", + GetAnonymStr(senderNetworkId).c_str(), bundleName.c_str()); + FindToNotifyRecvBroadcast(senderNetworkId, bundleName, continueType); + HILOGI("OnUserSwitch end."); +} + void DMSContinueRecvMgr::NotifyDeviceOffline(const std::string& networkId) { if (networkId.empty()) { @@ -581,20 +610,7 @@ void DMSContinueRecvMgr::NotifyDeviceOffline(const std::string& networkId) } HILOGI("Saved iconInfo cleared, networkId: %{public}s, bundleName: %{public}s.", GetAnonymStr(senderNetworkId).c_str(), bundleName.c_str()); - { - std::lock_guard registerOnListenerMapLock(eventMutex_); - auto iterItem = registerOnListener_.find(onType_); - if (iterItem == registerOnListener_.end()) { - HILOGI("Get iterItem failed from registerOnListener_, nobody registed"); - return; - } - std::vector> objs = iterItem->second; - for (auto iter : objs) { - NotifyRecvBroadcast(iter, - currentIconInfo(senderNetworkId, iconInfo_.sourceBundleName, bundleName, continueType), - INACTIVE); - } - } + FindToNotifyRecvBroadcast(senderNetworkId, bundleName, continueType); HILOGI("NotifyDeviceOffline end"); } @@ -623,20 +639,7 @@ void DMSContinueRecvMgr::NotifyPackageRemoved(const std::string& sinkBundleName) iconInfo_.continueType = ""; } HILOGI("Saved iconInfo cleared, sinkBundleName: %{public}s.", bundleName.c_str()); - { - std::lock_guard registerOnListenerMapLock(eventMutex_); - auto iterItem = registerOnListener_.find(onType_); - if (iterItem == registerOnListener_.end()) { - HILOGI("Get iterItem failed from registerOnListener_, nobody registed"); - return; - } - std::vector> objs = iterItem->second; - for (auto iter : objs) { - NotifyRecvBroadcast(iter, - currentIconInfo(senderNetworkId, iconInfo_.sourceBundleName, bundleName, continueType), - INACTIVE); - } - } + FindToNotifyRecvBroadcast(senderNetworkId, bundleName, continueType); HILOGI("NotifyPackageRemoved end"); } diff --git a/services/dtbschedmgr/src/multi_user_manager.cpp b/services/dtbschedmgr/src/multi_user_manager.cpp index 40e18131..99eec7ee 100644 --- a/services/dtbschedmgr/src/multi_user_manager.cpp +++ b/services/dtbschedmgr/src/multi_user_manager.cpp @@ -100,6 +100,12 @@ void MultiUserManager::UnInit() void MultiUserManager::OnUserSwitched(int32_t accountId) { HILOGI("UserSwitched start"); + auto recvMgr = GetCurrentRecvMgr(); + if (recvMgr == nullptr) { + HILOGI("GetRecvMgr failed."); + return; + } + recvMgr->OnUserSwitch(); auto sendMgr = GetCurrentSendMgr(); if (sendMgr == nullptr) { HILOGI("GetSendMgr failed."); @@ -125,7 +131,7 @@ void MultiUserManager::OnUserSwitched(int32_t accountId) return; } sendMgr->Init(); - auto recvMgr = GetCurrentRecvMgr(); + recvMgr = GetCurrentRecvMgr(); if (recvMgr == nullptr) { HILOGI("GetRecvMgr failed."); return; diff --git a/services/dtbschedmgr/test/unittest/mission/dms_continue_manager_test.cpp b/services/dtbschedmgr/test/unittest/mission/dms_continue_manager_test.cpp index 271c7a87..881b21a2 100644 --- a/services/dtbschedmgr/test/unittest/mission/dms_continue_manager_test.cpp +++ b/services/dtbschedmgr/test/unittest/mission/dms_continue_manager_test.cpp @@ -1488,5 +1488,58 @@ HWTEST_F(DMSContinueManagerTest, OnContinueSwitchOff_001, TestSize.Level1) EXPECT_EQ(recvMgr->registerOnListener_.empty(), true); DTEST_LOG << "DMSContinueManagerTest OnContinueSwitchOff_001 end" << std::endl; } + +/** + * @tc.name: OnUserSwitch_001 + * @tc.desc: test OnUserSwitch + * @tc.type: FUNC + */ +HWTEST_F(DMSContinueManagerTest, OnUserSwitch_001, TestSize.Level1) +{ + DTEST_LOG << "DMSContinueManagerTest OnUserSwitch_001 start" << std::endl; + auto recvMgr = MultiUserManager::GetInstance().GetCurrentRecvMgr(); + if (recvMgr == nullptr) { + DTEST_LOG << "GetRecvMgr failed." << std::endl; + return; + } + recvMgr->iconInfo_.senderNetworkId = ""; + recvMgr->iconInfo_.bundleName = ""; + recvMgr->iconInfo_.continueType = ""; + recvMgr->OnUserSwitch(); + EXPECT_EQ(recvMgr->iconInfo_.isEmpty(), true); + + recvMgr->iconInfo_.senderNetworkId = "senderNetworkId"; + recvMgr->iconInfo_.bundleName = "bundleName"; + recvMgr->iconInfo_.continueType = "continueType"; + recvMgr->registerOnListener_.clear(); + recvMgr->OnUserSwitch(); + EXPECT_EQ(recvMgr->registerOnListener_.empty(), true); + DTEST_LOG << "DMSContinueManagerTest OnUserSwitch_001 end" << std::endl; +} + +/** + * @tc.name: FindToNotifyRecvBroadcast_001 + * @tc.desc: test FindToNotifyRecvBroadcast + * @tc.type: FUNC + */ +HWTEST_F(DMSContinueManagerTest, FindToNotifyRecvBroadcast_001, TestSize.Level1) +{ + DTEST_LOG << "DMSContinueManagerTest FindToNotifyRecvBroadcast_001 start" << std::endl; + auto recvMgr = MultiUserManager::GetInstance().GetCurrentRecvMgr(); + if (recvMgr == nullptr) { + DTEST_LOG << "GetRecvMgr failed." << std::endl; + return; + } + std::string senderNetworkId = "senderNetworkId"; + std::string bundleName = "bundleName"; + std::string continueType = "senderNetworkId"; + recvMgr->iconInfo_.senderNetworkId = senderNetworkId; + recvMgr->iconInfo_.bundleName = bundleName; + recvMgr->iconInfo_.continueType = continueType; + recvMgr->registerOnListener_.clear(); + recvMgr->FindToNotifyRecvBroadcast(senderNetworkId, bundleName, continueType); + EXPECT_EQ(recvMgr->registerOnListener_.empty(), true); + DTEST_LOG << "DMSContinueManagerTest FindToNotifyRecvBroadcast_001 end" << std::endl; +} } // namespace DistributedSchedule } // namespace OHOS