• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "intell_voice_manager.h"
16 
17 #include <chrono>
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 #include "intell_voice_log.h"
21 #include "intell_voice_load_callback.h"
22 #include "i_intell_voice_service.h"
23 #include "intell_voice_service_proxy.h"
24 
25 using namespace std;
26 using namespace OHOS::IntellVoiceEngine;
27 #define LOG_TAG "IntellVoiceManager"
28 
29 namespace OHOS {
30 namespace IntellVoice {
31 static sptr<IIntellVoiceService> g_sProxy = nullptr;
32 constexpr int32_t LOADSA_TIMEOUT_MS = 5000;
33 std::mutex IntellVoiceManager::mutex_;
34 std::condition_variable IntellVoiceManager::proxyConVar_;
35 
IntellVoiceManager()36 IntellVoiceManager::IntellVoiceManager()
37 {
38     INTELL_VOICE_LOG_INFO("enter");
39 }
40 
~IntellVoiceManager()41 IntellVoiceManager::~IntellVoiceManager()
42 {
43     INTELL_VOICE_LOG_INFO("enter");
44 }
45 
GetInstance()46 IntellVoiceManager *IntellVoiceManager::GetInstance()
47 {
48     static IntellVoiceManager manager;
49     if (!manager.Init()) {
50         return nullptr;
51     }
52     return &manager;
53 }
54 
Init()55 bool IntellVoiceManager::Init()
56 {
57     std::unique_lock<std::mutex> lock(mutex_);
58     INTELL_VOICE_LOG_INFO("init start");
59 
60     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61     if (samgr == nullptr) {
62         INTELL_VOICE_LOG_ERROR("get sa manager failed");
63         return false;
64     }
65 
66     sptr<IntellVoiceLoadCallback> loadCallback = new (std::nothrow) IntellVoiceLoadCallback(
67         std::bind(&IntellVoiceManager::LoadSystemAbilitySuccess, this, std::placeholders::_1),
68         std::bind(&IntellVoiceManager::LoadSystemAbilityFail, this));
69     if (loadCallback == nullptr) {
70         INTELL_VOICE_LOG_ERROR("loadCallback is nullptr");
71         return false;
72     }
73 
74     int32_t ret = samgr->LoadSystemAbility(INTELL_VOICE_SERVICE_ID, loadCallback);
75     if (ret != ERR_OK) {
76         INTELL_VOICE_LOG_ERROR("Failed to load systemAbility");
77         return false;
78     }
79 
80     auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(LOADSA_TIMEOUT_MS));
81     if (waitStatus == std::cv_status::no_timeout) {
82         INTELL_VOICE_LOG_INFO("Load systemAbility success");
83         return true;
84     }
85 
86     INTELL_VOICE_LOG_ERROR("Load systemAbility timeout");
87     return false;
88 }
89 
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & object)90 void IntellVoiceManager::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &object)
91 {
92     std::unique_lock<std::mutex> lock(mutex_);
93     INTELL_VOICE_LOG_INFO("IntellVoiceManager finish start systemAbility");
94     if (object == nullptr) {
95         INTELL_VOICE_LOG_ERROR("get system ability failed");
96         return;
97     }
98     g_sProxy = iface_cast<IIntellVoiceService>(object);
99     if (g_sProxy != nullptr) {
100         INTELL_VOICE_LOG_INFO("init Service Proxy success");
101     }
102     proxyConVar_.notify_one();
103 }
104 
LoadSystemAbilityFail()105 void IntellVoiceManager::LoadSystemAbilityFail()
106 {
107     std::unique_lock<std::mutex> lock(mutex_);
108     g_sProxy = nullptr;
109     proxyConVar_.notify_one();
110 }
111 
CreateIntellVoiceEngine(IntellVoiceEngineType type,sptr<IIntellVoiceEngine> & inst)112 int32_t IntellVoiceManager::CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst)
113 {
114     INTELL_VOICE_LOG_INFO("enter");
115     if (g_sProxy == nullptr) {
116         INTELL_VOICE_LOG_ERROR("IntellVoiceService Proxy is null");
117         return -1;
118     }
119     return g_sProxy->CreateIntellVoiceEngine(type, inst);
120 }
121 
ReleaseIntellVoiceEngine(IntellVoiceEngineType type)122 int32_t IntellVoiceManager::ReleaseIntellVoiceEngine(IntellVoiceEngineType type)
123 {
124     INTELL_VOICE_LOG_INFO("enter");
125     if (g_sProxy == nullptr) {
126         INTELL_VOICE_LOG_ERROR("IntellVoiceService Proxy is null");
127         return -1;
128     }
129     return g_sProxy->ReleaseIntellVoiceEngine(type);
130 }
131 }
132 }