diff --git a/frameworks/native/src/core_manager_inner.cpp b/frameworks/native/src/core_manager_inner.cpp index b546387e3..ee81cced0 100644 --- a/frameworks/native/src/core_manager_inner.cpp +++ b/frameworks/native/src/core_manager_inner.cpp @@ -215,13 +215,13 @@ int32_t CoreManagerInner::RegisterSimAccountCallback( return simManager_->RegisterSimAccountCallback(tokenId, callback); } -int32_t CoreManagerInner::UnregisterSimAccountCallback(const int32_t tokenId) +int32_t CoreManagerInner::UnregisterSimAccountCallback(const sptr &callback) { if (simManager_ == nullptr) { TELEPHONY_LOGE("simManager_ is null"); return TELEPHONY_ERR_LOCAL_PTR_NULL; } - return simManager_->UnregisterSimAccountCallback(tokenId); + return simManager_->UnregisterSimAccountCallback(callback); } /******************** telRilManager start *******************/ diff --git a/interfaces/innerkits/include/core_manager_inner.h b/interfaces/innerkits/include/core_manager_inner.h index df07487ef..f1f963e33 100644 --- a/interfaces/innerkits/include/core_manager_inner.h +++ b/interfaces/innerkits/include/core_manager_inner.h @@ -50,7 +50,7 @@ public: int32_t GetDefaultSlotId(void); int32_t GetMaxSimCount(void); int32_t RegisterSimAccountCallback(const int32_t tokenId, const sptr &callback); - int32_t UnregisterSimAccountCallback(const int32_t tokenId); + int32_t UnregisterSimAccountCallback(const sptr &callback); /******************** telRilManager start *******************/ int32_t SetRadioState( diff --git a/interfaces/innerkits/include/i_sim_manager.h b/interfaces/innerkits/include/i_sim_manager.h index 672d6d5e1..80a48f303 100644 --- a/interfaces/innerkits/include/i_sim_manager.h +++ b/interfaces/innerkits/include/i_sim_manager.h @@ -79,7 +79,7 @@ public: virtual int32_t GetDefaultCellularDataSimId(int32_t &simId) = 0; virtual int32_t RegisterSimAccountCallback( const int32_t tokenId, const sptr &callback) = 0; - virtual int32_t UnregisterSimAccountCallback(const int32_t tokenId) = 0; + virtual int32_t UnregisterSimAccountCallback(const sptr &callback) = 0; virtual int32_t GetPrimarySlotId(int32_t &slotId) = 0; virtual int32_t GetShowNumber(int32_t slotId, std::u16string &showNumber) = 0; virtual int32_t GetShowName(int32_t slotId, std::u16string &showName) = 0; diff --git a/services/sim/include/multi_sim_monitor.h b/services/sim/include/multi_sim_monitor.h index 983d050ac..6e826cfff 100644 --- a/services/sim/include/multi_sim_monitor.h +++ b/services/sim/include/multi_sim_monitor.h @@ -47,7 +47,7 @@ public: std::shared_ptr simFileManager); void RegisterCoreNotify(int32_t slotId, const std::shared_ptr &handler, int what); int32_t RegisterSimAccountCallback(const int32_t tokenId, const sptr &callback); - int32_t UnregisterSimAccountCallback(const int32_t tokenId); + int32_t UnregisterSimAccountCallback(const sptr &callback); void NotifySimAccountChanged(); void RegisterSimNotify(); void RegisterSimNotify(int32_t slotId); diff --git a/services/sim/include/sim_manager.h b/services/sim/include/sim_manager.h index 20b8fe31c..8a0858bf3 100644 --- a/services/sim/include/sim_manager.h +++ b/services/sim/include/sim_manager.h @@ -148,7 +148,7 @@ public: int32_t QueryImsSwitch(int32_t, int32_t &imsSwitchValue) override; int32_t RegisterSimAccountCallback( const int32_t tokenId, const sptr &callback) override; - int32_t UnregisterSimAccountCallback(const int32_t tokenId) override; + int32_t UnregisterSimAccountCallback(const sptr &callback) override; bool IsSetActiveSimInProgress(int32_t slotId) override; bool IsSetPrimarySlotIdInProgress() override; diff --git a/services/sim/src/multi_sim_monitor.cpp b/services/sim/src/multi_sim_monitor.cpp index 7fcca0a03..9f494e01a 100644 --- a/services/sim/src/multi_sim_monitor.cpp +++ b/services/sim/src/multi_sim_monitor.cpp @@ -426,14 +426,16 @@ int32_t MultiSimMonitor::RegisterSimAccountCallback( const int32_t tokenId, const sptr &callback) { if (callback == nullptr) { - TELEPHONY_LOGE(" callback is nullptr"); + TELEPHONY_LOGE("callback is nullptr"); return TELEPHONY_ERR_ARGUMENT_NULL; } std::lock_guard lock(mutexInner_); bool isExisted = false; for (auto &iter : listSimAccountCallbackRecord_) { - if (iter.tokenId == tokenId) { - iter.simAccountCallback = callback; + if (iter.simAccountCallback == nullptr) { + continue; + } + if (iter.simAccountCallback->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) { isExisted = true; break; } @@ -451,13 +453,21 @@ int32_t MultiSimMonitor::RegisterSimAccountCallback( return TELEPHONY_SUCCESS; } -int32_t MultiSimMonitor::UnregisterSimAccountCallback(const int32_t tokenId) +int32_t MultiSimMonitor::UnregisterSimAccountCallback(const sptr &callback) { + if (callback == nullptr) { + TELEPHONY_LOGE("callback is nullptr"); + return TELEPHONY_ERR_ARGUMENT_NULL; + } std::lock_guard lock(mutexInner_); bool isSuccess = false; auto iter = listSimAccountCallbackRecord_.begin(); for (; iter != listSimAccountCallbackRecord_.end();) { - if (iter->tokenId == tokenId) { + if (iter->simAccountCallback == nullptr) { + iter++; + continue; + } + if (iter->simAccountCallback->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) { iter = listSimAccountCallbackRecord_.erase(iter); isSuccess = true; break; diff --git a/services/sim/src/sim_manager.cpp b/services/sim/src/sim_manager.cpp index 9b6fe52c0..b12644750 100644 --- a/services/sim/src/sim_manager.cpp +++ b/services/sim/src/sim_manager.cpp @@ -1202,13 +1202,13 @@ int32_t SimManager::RegisterSimAccountCallback(const int32_t tokenId, const sptr return multiSimMonitor_->RegisterSimAccountCallback(tokenId, callback); } -int32_t SimManager::UnregisterSimAccountCallback(const int32_t tokenId) +int32_t SimManager::UnregisterSimAccountCallback(const sptr &callback) { if (multiSimMonitor_ == nullptr) { TELEPHONY_LOGE("multiSimMonitor is null"); return TELEPHONY_ERR_LOCAL_PTR_NULL; } - return multiSimMonitor_->UnregisterSimAccountCallback(tokenId); + return multiSimMonitor_->UnregisterSimAccountCallback(callback); } bool SimManager::IsSetActiveSimInProgress(int32_t slotId) diff --git a/test/unittest/core_service_gtest/core_service_native_branch_test.cpp b/test/unittest/core_service_gtest/core_service_native_branch_test.cpp index 93b229a95..861910d09 100644 --- a/test/unittest/core_service_gtest/core_service_native_branch_test.cpp +++ b/test/unittest/core_service_gtest/core_service_native_branch_test.cpp @@ -287,7 +287,7 @@ HWTEST_F(CoreServiceNativeBranchTest, Telephony_CoreManagerInner_002, Function | mInner.simManager_ = simManager; sptr simAccountCallback; EXPECT_EQ(mInner.RegisterSimAccountCallback(-1, simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); - EXPECT_EQ(mInner.UnregisterSimAccountCallback(-1), TELEPHONY_ERR_LOCAL_PTR_NULL); + EXPECT_EQ(mInner.UnregisterSimAccountCallback(simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); mInner.telRilManager_ = nullptr; std::shared_ptr handler; @@ -502,7 +502,7 @@ HWTEST_F(CoreServiceNativeBranchTest, Telephony_CoreManagerInner_008, Function | mInner.simManager_ = simManager; sptr simAccountCallback; EXPECT_EQ(mInner.RegisterSimAccountCallback(-1, simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); - EXPECT_EQ(mInner.UnregisterSimAccountCallback(-1), TELEPHONY_ERR_LOCAL_PTR_NULL); + EXPECT_EQ(mInner.UnregisterSimAccountCallback(simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); mInner.telRilManager_ = nullptr; std::shared_ptr handler; std::string testStr = ""; @@ -531,7 +531,7 @@ HWTEST_F(CoreServiceNativeBranchTest, Telephony_CoreManagerInner_009, Function | mInner.simManager_ = simManager; sptr simAccountCallback; EXPECT_EQ(mInner.RegisterSimAccountCallback(-1, simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); - EXPECT_EQ(mInner.UnregisterSimAccountCallback(-1), TELEPHONY_ERR_LOCAL_PTR_NULL); + EXPECT_EQ(mInner.UnregisterSimAccountCallback(simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); mInner.telRilManager_ = nullptr; std::shared_ptr handler; @@ -558,7 +558,7 @@ HWTEST_F(CoreServiceNativeBranchTest, Telephony_CoreManagerInner_0010, Function mInner.simManager_ = simManager; sptr simAccountCallback; EXPECT_EQ(mInner.RegisterSimAccountCallback(-1, simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); - EXPECT_EQ(mInner.UnregisterSimAccountCallback(-1), TELEPHONY_ERR_LOCAL_PTR_NULL); + EXPECT_EQ(mInner.UnregisterSimAccountCallback(simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); mInner.telRilManager_ = nullptr; std::shared_ptr handler; DtmfParam dtmfParam; @@ -594,7 +594,7 @@ HWTEST_F(CoreServiceNativeBranchTest, Telephony_CoreManagerInner_0011, Function mInner.simManager_ = simManager; sptr simAccountCallback; EXPECT_EQ(mInner.RegisterSimAccountCallback(-1, simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); - EXPECT_EQ(mInner.UnregisterSimAccountCallback(-1), TELEPHONY_ERR_LOCAL_PTR_NULL); + EXPECT_EQ(mInner.UnregisterSimAccountCallback(simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); mInner.telRilManager_ = nullptr; std::shared_ptr handler; diff --git a/test/unittest/core_service_gtest/zero_branch_test.cpp b/test/unittest/core_service_gtest/zero_branch_test.cpp index d2f2398dc..900b8e4e3 100644 --- a/test/unittest/core_service_gtest/zero_branch_test.cpp +++ b/test/unittest/core_service_gtest/zero_branch_test.cpp @@ -1216,7 +1216,7 @@ HWTEST_F(BranchTest, Telephony_CoreManagerInner_006, Function | MediumTest | Lev sptr simAccountCallback; int32_t imsSwitchValue = 1; EXPECT_EQ(mInner.RegisterSimAccountCallback(tokenId, simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); - EXPECT_EQ(mInner.UnregisterSimAccountCallback(tokenId), TELEPHONY_ERR_LOCAL_PTR_NULL); + EXPECT_EQ(mInner.UnregisterSimAccountCallback(simAccountCallback), TELEPHONY_ERR_LOCAL_PTR_NULL); EXPECT_EQ(mInner.GetSmscAddr(INVALID_SLOTID, 1, handler), TELEPHONY_ERR_LOCAL_PTR_NULL); EXPECT_EQ(mInner.QueryImsSwitch(INVALID_SLOTID, imsSwitchValue), TELEPHONY_ERROR); EXPECT_GT(mInner.SetVoNRSwitch(0, 0, 0, nullptr), TELEPHONY_ERR_SUCCESS); @@ -1588,7 +1588,7 @@ HWTEST_F(BranchTest, Telephony_SimManager_004, Function | MediumTest | Level1) EXPECT_NE(simManager->QueryImsSwitch(0, imsSwitchValue), TELEPHONY_ERR_SUCCESS); EXPECT_NE(simManager->QueryImsSwitch(INVALID_SLOTID, imsSwitchValue), TELEPHONY_ERR_SUCCESS); EXPECT_NE(simManager->RegisterSimAccountCallback(tokenId, nullptr), TELEPHONY_ERR_SUCCESS); - EXPECT_NE(simManager->UnregisterSimAccountCallback(tokenId), TELEPHONY_ERR_SUCCESS); + EXPECT_NE(simManager->UnregisterSimAccountCallback(nullptr), TELEPHONY_ERR_SUCCESS); int32_t dsdsMode = INVALID_VALUE; simManager->GetDsdsMode(dsdsMode); simManager->SetDsdsMode(0); @@ -3084,7 +3084,7 @@ HWTEST_F(BranchTest, Telephony_MultiSimMonitor_001, Function | MediumTest | Leve int32_t tokenId = 123456789; sptr callback = nullptr; EXPECT_GT(multiSimMonitor->RegisterSimAccountCallback(tokenId, callback), TELEPHONY_ERROR); - EXPECT_EQ(multiSimMonitor->UnregisterSimAccountCallback(tokenId), TELEPHONY_ERROR); + EXPECT_GT(multiSimMonitor->UnregisterSimAccountCallback(callback), TELEPHONY_ERROR); } /**