diff --git a/interfaces/ipc/include/dinput_sa_manager.h b/interfaces/ipc/include/dinput_sa_manager.h index 8178d6e..6792393 100644 --- a/interfaces/ipc/include/dinput_sa_manager.h +++ b/interfaces/ipc/include/dinput_sa_manager.h @@ -18,6 +18,7 @@ #include #include +#include #include "i_distributed_source_input.h" #include "i_distributed_sink_input.h" @@ -46,6 +47,11 @@ public: bool HasDInputSinkProxy(); bool SetDInputSinkProxy(const sptr &remoteObject); void RegisterEventHandler(std::shared_ptr handler); + int32_t RestoreRegisterListenerAndCallback(); + void AddSimEventListenerToCache(sptr listener); + void RemoveSimEventListenerFromCache(sptr listener); + void AddSessionStateCbToCache(sptr callback); + void RemoveSessionStateCbFromCache(); public: class SystemAbilityListener : public SystemAbilityStatusChangeStub { @@ -54,6 +60,12 @@ public: void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; }; +private: + std::mutex simEventListenerCacheMtx_; + std::set> simEventListenerCache_; + std::mutex sessionStateCbCacheMtx_; + std::set> sessionStateCbCache_; + private: DInputSAManager() = default; ~DInputSAManager() = default; diff --git a/interfaces/ipc/src/dinput_sa_manager.cpp b/interfaces/ipc/src/dinput_sa_manager.cpp index 4f3969b..243ec7e 100644 --- a/interfaces/ipc/src/dinput_sa_manager.cpp +++ b/interfaces/ipc/src/dinput_sa_manager.cpp @@ -76,8 +76,8 @@ void DInputSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAb DHLOGI("SendEvent DINPUT_CLIENT_CHECK_SOURCE_CALLBACK_REGISTER_MSG"); AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(DINPUT_CLIENT_CHECK_SOURCE_CALLBACK_REGISTER_MSG, systemAbilityId); - DInputSAManager::GetInstance().eventHandler_->SendEvent(msgEvent, DINPUT_CLIENT_HANDLER_MSG_DELAY_TIME, - AppExecFwk::EventQueue::Priority::IMMEDIATE); + DInputSAManager::GetInstance().eventHandler_->SendEvent(msgEvent, + DINPUT_CLIENT_HANDLER_MSG_DELAY_TIME, AppExecFwk::EventQueue::Priority::IMMEDIATE); } } @@ -88,8 +88,8 @@ void DInputSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAb DHLOGI("SendEvent DINPUT_CLIENT_CHECK_SINK_CALLBACK_REGISTER_MSG"); AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get(DINPUT_CLIENT_CHECK_SINK_CALLBACK_REGISTER_MSG, systemAbilityId); - DInputSAManager::GetInstance().eventHandler_->SendEvent(msgEvent, DINPUT_CLIENT_HANDLER_MSG_DELAY_TIME, - AppExecFwk::EventQueue::Priority::IMMEDIATE); + DInputSAManager::GetInstance().eventHandler_->SendEvent(msgEvent, + DINPUT_CLIENT_HANDLER_MSG_DELAY_TIME, AppExecFwk::EventQueue::Priority::IMMEDIATE); } } DHLOGI("sa %d is added.", systemAbilityId); @@ -275,6 +275,77 @@ bool DInputSAManager::SetDInputSinkProxy(const sptr &remoteObject } return true; } + +int32_t DInputSAManager::RestoreRegisterListenerAndCallback() +{ + DHLOGI("Restore RegisterPublisherListener"); + if (!DInputSAManager::GetInstance().GetDInputSourceProxy()) { + DHLOGE("RestoreRegisterSimulationEventListener proxy error, client fail"); + return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; + } + + int32_t result = DH_SUCCESS; + { + std::lock_guard lock(simEventListenerCacheMtx_); + for (const auto& listener : simEventListenerCache_) { + if (listener == nullptr) { + DHLOGE("simEventListenerCache_ is nullptr"); + continue; + } + int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSimulationEventListener(listener); + if (ret != DH_SUCCESS) { + result = ret; + DHLOGE("SA execute RegisterSimulationEventListener fail, ret = %d", ret); + } + } + } + { + DHLOGI("Restore RegisterSessionStateCb"); + std::lock_guard lock(sessionStateCbCacheMtx_); + for (const auto& callback : sessionStateCbCache_) { + if (callback == nullptr) { + DHLOGE("sessionStateCbCache_ is nullptr"); + continue; + } + int32_t ret = DInputSAManager::GetInstance().dInputSourceProxy_->RegisterSessionStateCb(callback); + if (ret != DH_SUCCESS) { + result = ret; + DHLOGE("SA execute RegisterSessionStateCb fail, ret = %d", ret); + } + } + } + return result; +} + +void DInputSAManager::AddSimEventListenerToCache(sptr listener) +{ + std::lock_guard simEventListenerLock(simEventListenerCacheMtx_); + if (listener != nullptr) { + simEventListenerCache_.insert(listener); + } +} + +void DInputSAManager::RemoveSimEventListenerFromCache(sptr listener) +{ + std::lock_guard simEventListenerLock(simEventListenerCacheMtx_); + if (listener != nullptr) { + simEventListenerCache_.erase(listener); + } +} + +void DInputSAManager::AddSessionStateCbToCache(sptr callback) +{ + std::lock_guard sessionStateCbLock(sessionStateCbCacheMtx_); + if (callback != nullptr) { + sessionStateCbCache_.insert(callback); + } +} + +void DInputSAManager::RemoveSessionStateCbFromCache() +{ + std::lock_guard sessionStateCbLock(sessionStateCbCacheMtx_); + sessionStateCbCache_.clear(); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/ipc/src/distributed_input_client.cpp b/interfaces/ipc/src/distributed_input_client.cpp index 1cd468f..7f83766 100644 --- a/interfaces/ipc/src/distributed_input_client.cpp +++ b/interfaces/ipc/src/distributed_input_client.cpp @@ -28,6 +28,7 @@ #include "input_check_param.h" #include "softbus_bus_center.h" #include "white_list_util.h" +#include "dinput_sa_manager.h" namespace OHOS { namespace DistributedHardware { @@ -128,6 +129,10 @@ void DistributedInputClient::DInputClientEventHandler::ProcessEvent(const AppExe DHLOGI("DInputClientEventHandler ProcessEvent start eventId:%d.", eventId); if (eventId == DINPUT_CLIENT_CHECK_SOURCE_CALLBACK_REGISTER_MSG) { DistributedInputClient::GetInstance().CheckSourceRegisterCallback(); + int32_t result = DInputSAManager::GetInstance().RestoreRegisterListenerAndCallback(); + if (result != DH_SUCCESS) { + DHLOGE("source sa execute RestoreRegisterListenerAndCallback fail, result = %d", result); + } return; } @@ -583,6 +588,7 @@ int32_t DistributedInputClient::RegisterSimulationEventListener(sptrRegisterSimulationEventListener(listener); if (ret == DH_SUCCESS) { isSimulationEventCbReg = true; + DInputSAManager::GetInstance().AddSimEventListenerToCache(listener); } else { isSimulationEventCbReg = false; regSimulationEventListener_ = listener; @@ -607,6 +613,7 @@ int32_t DistributedInputClient::UnregisterSimulationEventListener(sptrRegisterSessionStateCb(callback); } @@ -758,6 +766,7 @@ int32_t DistributedInputClient::UnregisterSessionStateCb() DHLOGE("DinputStart client fail."); return ERR_DH_INPUT_CLIENT_GET_SOURCE_PROXY_FAIL; } + DInputSAManager::GetInstance().RemoveSessionStateCbFromCache(); return DInputSAManager::GetInstance().dInputSourceProxy_->UnregisterSessionStateCb(); } } // namespace DistributedInput diff --git a/interfaces/ipc/test/clientunittest/mock_samanager.cpp b/interfaces/ipc/test/clientunittest/mock_samanager.cpp index 7da39f4..25e1be5 100644 --- a/interfaces/ipc/test/clientunittest/mock_samanager.cpp +++ b/interfaces/ipc/test/clientunittest/mock_samanager.cpp @@ -97,6 +97,23 @@ bool DInputSAManager::SetDInputSinkProxy(const sptr &remoteObject } return true; } + +int32_t DInputSAManager::RestoreRegisterListenerAndCallback() +{ + return DH_SUCCESS; +} + +void DInputSAManager::AddSimEventListenerToCache(sptr listener) +{} + +void DInputSAManager::RemoveSimEventListenerFromCache(sptr listener) +{} + +void DInputSAManager::AddSessionStateCbToCache(const sptr callback) +{} + +void DInputSAManager::RemoveSessionStateCbFromCache() +{} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file