1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "trigger_manager.h" 16 #include "trigger_service.h" 17 #include "intell_voice_log.h" 18 #include "memory_guard.h" 19 20 #define LOG_TAG "TriggerManager" 21 22 namespace OHOS { 23 namespace IntellVoiceTrigger { 24 std::mutex TriggerManager::instanceMutex_; 25 std::shared_ptr<TriggerManager> TriggerManager::instance_ = nullptr; 26 TriggerManager()27TriggerManager::TriggerManager() 28 { 29 OHOS::IntellVoiceUtils::MemoryGuard memoryGuard; 30 service_ = std::make_shared<TriggerService>(); 31 if (service_ == nullptr) { 32 INTELL_VOICE_LOG_ERROR("service_ is nullptr"); 33 } 34 } 35 ~TriggerManager()36TriggerManager::~TriggerManager() 37 { 38 service_ = nullptr; 39 } 40 GetInstance()41std::shared_ptr<TriggerManager> TriggerManager::GetInstance() 42 { 43 if (instance_ == nullptr) { 44 std::lock_guard<std::mutex> autoLock(instanceMutex_); 45 if (instance_ == nullptr) { 46 instance_ = std::shared_ptr<TriggerManager>(new TriggerManager()); 47 } 48 } 49 return instance_; 50 } 51 UpdateModel(std::shared_ptr<GenericTriggerModel> model)52void TriggerManager::UpdateModel(std::shared_ptr<GenericTriggerModel> model) 53 { 54 if (service_ == nullptr) { 55 INTELL_VOICE_LOG_ERROR("service_ is nullptr"); 56 return; 57 } 58 service_->UpdateGenericTriggerModel(model); 59 } 60 DeleteModel(int32_t uuid)61void TriggerManager::DeleteModel(int32_t uuid) 62 { 63 if (service_ == nullptr) { 64 INTELL_VOICE_LOG_ERROR("service_ is nullptr"); 65 return; 66 } 67 service_->DeleteGenericTriggerModel(uuid); 68 } 69 GetModel(int32_t uuid)70std::shared_ptr<GenericTriggerModel> TriggerManager::GetModel(int32_t uuid) 71 { 72 if (service_ == nullptr) { 73 INTELL_VOICE_LOG_ERROR("service_ is nullptr"); 74 return nullptr; 75 } 76 return service_->GetGenericTriggerModel(uuid); 77 } 78 CreateTriggerDetector(int32_t uuid,std::shared_ptr<IIntellVoiceTriggerDetectorCallback> callback)79std::shared_ptr<TriggerDetector> TriggerManager::CreateTriggerDetector( 80 int32_t uuid, std::shared_ptr<IIntellVoiceTriggerDetectorCallback> callback) 81 { 82 OHOS::IntellVoiceUtils::MemoryGuard memoryGuard; 83 std::shared_ptr<TriggerDetector> detector = std::make_shared<TriggerDetector>(uuid, service_, callback); 84 if (detector == nullptr) { 85 INTELL_VOICE_LOG_ERROR("detector is nullptr"); 86 return nullptr; 87 } 88 89 detectors_[uuid] = detector; 90 return detector; 91 } 92 ReleaseTriggerDetector(int32_t uuid)93void TriggerManager::ReleaseTriggerDetector(int32_t uuid) 94 { 95 OHOS::IntellVoiceUtils::MemoryGuard memoryGuard; 96 auto it = detectors_.find(uuid); 97 if (it == detectors_.end()) { 98 return; 99 } 100 101 detectors_.erase(it); 102 } 103 } // namespace IntellVoiceTrigger 104 } // namespace OHOS