/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "multimodal_input_connect_manager.h" #include #include #include "iservice_registry.h" #include "mmi_log.h" #include "multimodal_input_connect_death_recipient.h" #include "multimodal_input_connect_define.h" #include "system_ability_definition.h" #include "util.h" namespace OHOS { namespace MMI { namespace { std::shared_ptr g_instance; constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, MMI_LOG_DOMAIN, "MultimodalInputConnectManager"}; } // namespace std::shared_ptr MultimodalInputConnectManager::GetInstance() { static std::once_flag flag; std::call_once(flag, [&]() { g_instance.reset(new MultimodalInputConnectManager()); }); if (g_instance != nullptr) { g_instance->ConnectMultimodalInputService(); } return g_instance; } int32_t MultimodalInputConnectManager::AllocSocketPair(const int32_t moduleType) { MMI_LOGD("enter"); std::lock_guard guard(lock_); if (multimodalInputConnectService_ == nullptr) { MMI_LOGE("client has not connect server"); return RET_ERR; } const std::string programName(OHOS::MMI::GetProgramName()); int32_t result = multimodalInputConnectService_->AllocSocketFd(programName, moduleType, socketFd_); if (result != RET_OK) { MMI_LOGE("AllocSocketFd has error:%{public}d", result); return RET_ERR; } MMI_LOGI("AllocSocketPair success. socketFd_:%{public}d", socketFd_); return RET_OK; } int32_t MultimodalInputConnectManager::GetClientSocketFdOfAllocedSocketPair() const { MMI_LOGD("enter"); return socketFd_; } int32_t MultimodalInputConnectManager::AddInputEventFilter(sptr filter) { std::lock_guard guard(lock_); if (multimodalInputConnectService_ == nullptr) { MMI_LOGE("multimodalInputConnectService_ is nullptr"); return RET_ERR; } return multimodalInputConnectService_->AddInputEventFilter(filter); } bool MultimodalInputConnectManager::ConnectMultimodalInputService() { MMI_LOGD("enter"); std::lock_guard guard(lock_); if (multimodalInputConnectService_ != nullptr) { return true; } auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (sm == nullptr) { MMI_LOGE("get registry fail"); return false; } auto sa = sm->GetSystemAbility(IMultimodalInputConnect::MULTIMODAL_INPUT_CONNECT_SERVICE_ID); if (sa == nullptr) { MMI_LOGE("get sa fail"); return false; } std::weak_ptr weakPtr = shared_from_this(); auto deathCallback = [weakPtr](const wptr &object) { auto sharedPtr = weakPtr.lock(); if (sharedPtr != nullptr) { sharedPtr->OnDeath(); } }; multimodalInputConnectRecipient_ = new MultimodalInputConnectDeathRecipient(deathCallback); sa->AddDeathRecipient(multimodalInputConnectRecipient_); multimodalInputConnectService_ = iface_cast(sa); if (multimodalInputConnectService_ == nullptr) { MMI_LOGE("get multimodal input connect service fail"); return false; } MMI_LOGI("get multimodal input connect service successful"); return true; } void MultimodalInputConnectManager::OnDeath() { MMI_LOGD("enter"); Clean(); NotifyDeath(); MMI_LOGD("leave"); } void MultimodalInputConnectManager::Clean() { MMI_LOGD("enter"); std::lock_guard guard(lock_); if (multimodalInputConnectService_ != nullptr) { multimodalInputConnectService_.clear(); multimodalInputConnectService_ = nullptr; } if (multimodalInputConnectRecipient_ != nullptr) { multimodalInputConnectRecipient_.clear(); multimodalInputConnectRecipient_ = nullptr; } MMI_LOGD("leave"); } void MultimodalInputConnectManager::NotifyDeath() { MMI_LOGD("enter"); int32_t retryCount = 50; do { std::this_thread::sleep_for(std::chrono::seconds(1)); if (ConnectMultimodalInputService()) { MMI_LOGD("connect multimodal input connect service successful"); return; } } while (--retryCount > 0); MMI_LOGI("leave"); } } // namespace MMI } // namespace OHOS