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 "trigger_detector.h"
18 #include "intell_voice_log.h"
19 #include "memory_guard.h"
20 #include "trigger_detector_callback.h"
21
22 #define LOG_TAG "TriggerManager"
23
24 namespace OHOS {
25 namespace IntellVoiceTrigger {
TriggerManager()26 TriggerManager::TriggerManager()
27 {
28 OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
29 service_ = std::make_shared<TriggerService>();
30 if (service_ == nullptr) {
31 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
32 }
33 }
34
~TriggerManager()35 TriggerManager::~TriggerManager()
36 {
37 service_ = nullptr;
38 }
39
UpdateModel(std::vector<uint8_t> buffer,int32_t uuid,TriggerModelType type)40 void TriggerManager::UpdateModel(std::vector<uint8_t> buffer, int32_t uuid, TriggerModelType type)
41 {
42 if (service_ == nullptr) {
43 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
44 return;
45 }
46 std::shared_ptr<GenericTriggerModel> model = std::make_shared<GenericTriggerModel>(uuid,
47 TriggerModel::TriggerModelVersion::MODLE_VERSION_2, type);
48 if (model == nullptr) {
49 INTELL_VOICE_LOG_ERROR("model is null");
50 return;
51 }
52 model->SetData(buffer);
53 service_->UpdateGenericTriggerModel(model);
54 }
55
DeleteModel(int32_t uuid)56 void TriggerManager::DeleteModel(int32_t uuid)
57 {
58 if (service_ == nullptr) {
59 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
60 return;
61 }
62 service_->DeleteGenericTriggerModel(uuid);
63 }
64
IsModelExist(int32_t uuid)65 bool TriggerManager::IsModelExist(int32_t uuid)
66 {
67 if (service_ == nullptr) {
68 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
69 return false;
70 }
71 if (service_->GetGenericTriggerModel(uuid) == nullptr) {
72 return false;
73 }
74 return true;
75 }
76
GetModel(int32_t uuid)77 std::shared_ptr<GenericTriggerModel> TriggerManager::GetModel(int32_t uuid)
78 {
79 if (service_ == nullptr) {
80 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
81 return nullptr;
82 }
83 return service_->GetGenericTriggerModel(uuid);
84 }
85
ReleaseTriggerDetector(int32_t uuid)86 void TriggerManager::ReleaseTriggerDetector(int32_t uuid)
87 {
88 std::lock_guard<std::mutex> lock(detectorMutex_);
89 OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
90 auto it = detectors_.find(uuid);
91 if (it != detectors_.end()) {
92 if (it->second != nullptr) {
93 it->second->UnloadTriggerModel();
94 }
95 detectors_.erase(it);
96 }
97 }
98
SetParameter(const std::string & key,const std::string & value)99 int32_t TriggerManager::SetParameter(const std::string &key, const std::string &value)
100 {
101 if (service_ == nullptr) {
102 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
103 return -1;
104 }
105 return service_->SetParameter(key, value);
106 }
107
GetParameter(const std::string & key)108 std::string TriggerManager::GetParameter(const std::string &key)
109 {
110 if (service_ == nullptr) {
111 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
112 return "";
113 }
114 return service_->GetParameter(key);
115 }
116
117
AttachTelephonyObserver()118 void TriggerManager::AttachTelephonyObserver()
119 {
120 #ifdef SUPPORT_TELEPHONY_SERVICE
121 if (service_ == nullptr) {
122 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
123 return;
124 }
125 return service_->AttachTelephonyObserver();
126 #endif
127 }
128
DetachTelephonyObserver()129 void TriggerManager::DetachTelephonyObserver()
130 {
131 #ifdef SUPPORT_TELEPHONY_SERVICE
132
133 if (service_ == nullptr) {
134 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
135 return;
136 }
137 return service_->DetachTelephonyObserver();
138 #endif
139 }
140
AttachAudioCaptureListener()141 void TriggerManager::AttachAudioCaptureListener()
142 {
143 if (service_ == nullptr) {
144 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
145 return;
146 }
147 return service_->AttachAudioCaptureListener();
148 }
149
DetachAudioCaptureListener()150 void TriggerManager::DetachAudioCaptureListener()
151 {
152 if (service_ == nullptr) {
153 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
154 return;
155 }
156 return service_->DetachAudioCaptureListener();
157 }
158
AttachAudioRendererEventListener()159 void TriggerManager::AttachAudioRendererEventListener()
160 {
161 if (service_ == nullptr) {
162 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
163 return;
164 }
165 return service_->AttachAudioRendererEventListener();
166 }
167
DetachAudioRendererEventListener()168 void TriggerManager::DetachAudioRendererEventListener()
169 {
170 if (service_ == nullptr) {
171 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
172 return;
173 }
174 return service_->DetachAudioRendererEventListener();
175 }
176
AttachAudioSceneEventListener()177 void TriggerManager::AttachAudioSceneEventListener()
178 {
179 if (service_ == nullptr) {
180 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
181 return;
182 }
183 return service_->AttachAudioSceneEventListener();
184 }
185
DetachAudioSceneEventListener()186 void TriggerManager::DetachAudioSceneEventListener()
187 {
188 if (service_ == nullptr) {
189 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
190 return;
191 }
192 return service_->DetachAudioSceneEventListener();
193 }
194
195 #ifdef POWER_MANAGER_ENABLE
AttachHibernateObserver()196 void TriggerManager::AttachHibernateObserver()
197 {
198 if (service_ == nullptr) {
199 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
200 return;
201 }
202 return service_->AttachHibernateObserver();
203 }
204
DetachHibernateObserver()205 void TriggerManager::DetachHibernateObserver()
206 {
207 if (service_ == nullptr) {
208 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
209 return;
210 }
211 return service_->DetachHibernateObserver();
212 }
213 #endif
214
AttachFoldStatusListener()215 void TriggerManager::AttachFoldStatusListener()
216 {
217 #ifdef SUPPORT_WINDOW_MANAGER
218 if (service_ == nullptr) {
219 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
220 return;
221 }
222 return service_->AttachFoldStatusListener();
223 #endif
224 }
225
DetachFoldStatusListener()226 void TriggerManager::DetachFoldStatusListener()
227 {
228 #ifdef SUPPORT_WINDOW_MANAGER
229 if (service_ == nullptr) {
230 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
231 return;
232 }
233 return service_->DetachFoldStatusListener();
234 #endif
235 }
236
StartDetection(int32_t uuid)237 int32_t TriggerManager::StartDetection(int32_t uuid)
238 {
239 std::lock_guard<std::mutex> lock(detectorMutex_);
240 if ((detectors_.count(uuid) == 0) || (detectors_[uuid] == nullptr)) {
241 INTELL_VOICE_LOG_INFO("detector is not existed, uuid:%{public}d", uuid);
242 return -1;
243 }
244
245 if (service_->GetParameter("audio_hal_status") == "true") {
246 INTELL_VOICE_LOG_INFO("audio hal is ready");
247 detectors_[uuid]->StartRecognition();
248 return 1;
249 }
250 return 0;
251 }
252
StopDetection(int32_t uuid)253 void TriggerManager::StopDetection(int32_t uuid)
254 {
255 std::lock_guard<std::mutex> lock(detectorMutex_);
256 if ((detectors_.count(uuid) == 0) || (detectors_[uuid] == nullptr)) {
257 INTELL_VOICE_LOG_INFO("detector is not existed, uuid:%{public}d", uuid);
258 return;
259 }
260 detectors_[uuid]->StopRecognition();
261 }
262
CreateDetector(int32_t uuid,std::function<void ()> onDetected)263 void TriggerManager::CreateDetector(int32_t uuid, std::function<void()> onDetected)
264 {
265 std::lock_guard<std::mutex> lock(detectorMutex_);
266 if (detectors_.count(uuid) != 0 && detectors_[uuid] != nullptr) {
267 INTELL_VOICE_LOG_INFO("detector is already existed, no need to create, uuid:%{public}d", uuid);
268 return;
269 }
270
271 auto cb = std::make_shared<TriggerDetectorCallback>(onDetected);
272 if (cb == nullptr) {
273 INTELL_VOICE_LOG_ERROR("cb is nullptr");
274 return;
275 }
276
277 OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
278 std::shared_ptr<TriggerDetector> detector = std::make_shared<TriggerDetector>(uuid, service_, cb);
279 if (detector == nullptr) {
280 INTELL_VOICE_LOG_ERROR("detector is nullptr");
281 return;
282 }
283
284 detectors_[uuid] = detector;
285 }
286
OnServiceStart()287 void TriggerManager::OnServiceStart()
288 {
289 }
290
OnServiceStop()291 void TriggerManager::OnServiceStop()
292 {
293 DetachTelephonyObserver();
294 DetachAudioCaptureListener();
295 DetachAudioRendererEventListener();
296 #ifdef POWER_MANAGER_ENABLE
297 DetachHibernateObserver();
298 #endif
299 DetachFoldStatusListener();
300 DetachAudioSceneEventListener();
301 }
302
OnTelephonyStateRegistryServiceChange(bool isAdded)303 void TriggerManager::OnTelephonyStateRegistryServiceChange(bool isAdded)
304 {
305 #ifdef SUPPORT_TELEPHONY_SERVICE
306 if (isAdded) {
307 INTELL_VOICE_LOG_INFO("telephony state registry service is added");
308 AttachTelephonyObserver();
309 } else {
310 INTELL_VOICE_LOG_INFO("telephony state registry service is removed");
311 }
312 #endif
313 }
314
OnAudioDistributedServiceChange(bool isAdded)315 void TriggerManager::OnAudioDistributedServiceChange(bool isAdded)
316 {
317 if (isAdded) {
318 INTELL_VOICE_LOG_INFO("audio distributed service is added");
319 AttachAudioCaptureListener();
320 } else {
321 INTELL_VOICE_LOG_INFO("audio distributed service is removed");
322 }
323 }
324
OnAudioPolicyServiceChange(bool isAdded)325 void TriggerManager::OnAudioPolicyServiceChange(bool isAdded)
326 {
327 if (isAdded) {
328 INTELL_VOICE_LOG_INFO("audio policy service is added");
329 AttachAudioRendererEventListener();
330 AttachAudioSceneEventListener();
331 } else {
332 INTELL_VOICE_LOG_INFO("audio policy service is removed");
333 }
334 }
335
336 #ifdef POWER_MANAGER_ENABLE
OnPowerManagerServiceChange(bool isAdded)337 void TriggerManager::OnPowerManagerServiceChange(bool isAdded)
338 {
339 if (isAdded) {
340 INTELL_VOICE_LOG_INFO("power manager service is added");
341 AttachHibernateObserver();
342 } else {
343 INTELL_VOICE_LOG_INFO("power manager service is removed");
344 }
345 }
346 #endif
347
OnDisplayManagerServiceChange(bool isAdded)348 void TriggerManager::OnDisplayManagerServiceChange(bool isAdded)
349 {
350 #ifdef SUPPORT_WINDOW_MANAGER
351 if (isAdded) {
352 INTELL_VOICE_LOG_INFO("fold status service is added");
353 AttachFoldStatusListener();
354 } else {
355 INTELL_VOICE_LOG_INFO("fold status service is removed");
356 }
357 #endif
358 }
359 } // namespace IntellVoiceTrigger
360 } // namespace OHOS
361