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 #ifndef SERVICE_MANAGER_H 16 #define SERVICE_MANAGER_H 17 #include <mutex> 18 #include <map> 19 #include <atomic> 20 #include "engine_base.h" 21 #include "trigger_detector.h" 22 #include "switch_observer.h" 23 #include "switch_provider.h" 24 #include "history_info_mgr.h" 25 26 namespace OHOS { 27 namespace IntellVoiceEngine { 28 enum EngineEvent { 29 ENGINE_EVENT_CREATE = 0, 30 ENGINE_EVENT_START, 31 }; 32 33 enum ArbitrationResult { 34 ARBITRATION_OK = 0, 35 ARBITRATION_REJECT, 36 }; 37 38 class IntellVoiceServiceManager { 39 public: 40 ~IntellVoiceServiceManager(); 41 static std::unique_ptr<IntellVoiceServiceManager> &GetInstance(); GetEnrollModelUuid()42 static int32_t GetEnrollModelUuid() 43 { 44 return g_enrollModelUuid; 45 } SetEnrollResult(bool result)46 static void SetEnrollResult(bool result) 47 { 48 enrollResult_.store(result); 49 } GetEnrollResult()50 static bool GetEnrollResult() 51 { 52 return enrollResult_.load(); 53 } 54 sptr<IIntellVoiceEngine> CreateEngine(IntellVoiceEngineType type); 55 int32_t ReleaseEngine(IntellVoiceEngineType type); GetHistoryInfoMgr()56 const std::unique_ptr<HistoryInfoMgr> &GetHistoryInfoMgr() const 57 { 58 return historyInfoMgr_; 59 } 60 61 void OnServiceStart(); 62 void CreateSwitchProvider(); 63 void ReleaseSwitchProvider(); 64 void StartDetection(); 65 void StopDetection(); 66 bool QuerySwitchStatus(); 67 void UnloadIntellVoiceService(); 68 69 int32_t ApplyArbitration(IntellVoiceEngineType type, EngineEvent event); 70 71 private: 72 IntellVoiceServiceManager(); 73 void OnSwitchChange(); 74 void OnDetected(); 75 void CreateDetector(); 76 77 int32_t CreateArbitration(IntellVoiceEngineType type); 78 int32_t StartArbitration(IntellVoiceEngineType type); 79 void HandlePreemption(sptr<IIntellVoiceEngine> currentEngine); 80 void HandleReplace(sptr<IIntellVoiceEngine> currentEngine); 81 82 sptr<IIntellVoiceEngine> CreateEngineInner(IntellVoiceEngineType type); 83 int32_t ReleaseEngineInner(IntellVoiceEngineType type); 84 85 private: 86 static const int32_t g_enrollModelUuid; 87 static std::unique_ptr<IntellVoiceServiceManager> g_intellVoiceServiceMgr; 88 static std::atomic<bool> enrollResult_; 89 std::mutex engineMutex_; 90 std::mutex detectorMutex_; 91 std::map<IntellVoiceEngineType, sptr<EngineBase>> engines_; 92 std::shared_ptr<IntellVoiceTrigger::TriggerDetector> detector_ = nullptr; 93 sptr<SwitchObserver> switchObserver_ = nullptr; 94 IntellVoiceUtils::UniqueProductType<SwitchProvider> switchProvider_ = 95 IntellVoiceUtils::UniqueProductType<SwitchProvider> {nullptr, nullptr}; 96 std::unique_ptr<HistoryInfoMgr> historyInfoMgr_ = nullptr; 97 }; 98 } // namespace IntellVoice 99 } // namespace OHOS 100 #endif