• 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 "intell_voice_service_proxy.h"
23 
24 using namespace std;
25 using namespace OHOS::IntellVoiceEngine;
26 #define LOG_TAG "IntellVoiceManager"
27 
28 namespace OHOS {
29 namespace IntellVoice {
30 constexpr int32_t LOADSA_TIMEOUT_MS = 5000;
31 
IntellVoiceManager()32 IntellVoiceManager::IntellVoiceManager()
33 {
34     INTELL_VOICE_LOG_INFO("enter");
35 }
36 
~IntellVoiceManager()37 IntellVoiceManager::~IntellVoiceManager()
38 {
39     INTELL_VOICE_LOG_INFO("enter");
40 }
41 
GetInstance()42 IntellVoiceManager *IntellVoiceManager::GetInstance()
43 {
44     static IntellVoiceManager manager;
45     if (!manager.Init()) {
46         return nullptr;
47     }
48     return &manager;
49 }
50 
Init()51 bool IntellVoiceManager::Init()
52 {
53     std::unique_lock<std::mutex> lock(mutex_);
54     INTELL_VOICE_LOG_INFO("enter");
55 
56     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57     if (samgr == nullptr) {
58         INTELL_VOICE_LOG_ERROR("get sa manager failed");
59         return false;
60     }
61 
62     sptr<IntellVoiceLoadCallback> loadCallback = new (std::nothrow)
63         IntellVoiceLoadCallback(std::bind(&IntellVoiceManager::LoadSystemAbilitySuccess, this, std::placeholders::_1),
64             std::bind(&IntellVoiceManager::LoadSystemAbilityFail, this));
65     if (loadCallback == nullptr) {
66         INTELL_VOICE_LOG_ERROR("loadCallback is nullptr");
67         return false;
68     }
69 
70     int32_t ret = samgr->LoadSystemAbility(INTELL_VOICE_SERVICE_ID, loadCallback);
71     if (ret != ERR_OK) {
72         INTELL_VOICE_LOG_ERROR("Failed to load systemAbility");
73         return false;
74     }
75 
76     auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(LOADSA_TIMEOUT_MS));
77     if (waitStatus != std::cv_status::no_timeout) {
78         INTELL_VOICE_LOG_ERROR("Load systemAbility timeout");
79         return false;
80     }
81     INTELL_VOICE_LOG_INFO("Load systemAbility success");
82     return true;
83 }
84 
LoadSystemAbilitySuccess(const sptr<IRemoteObject> & object)85 void IntellVoiceManager::LoadSystemAbilitySuccess(const sptr<IRemoteObject> &object)
86 {
87     std::unique_lock<std::mutex> lock(mutex_);
88     INTELL_VOICE_LOG_INFO("IntellVoiceManager finish start systemAbility");
89     if (object == nullptr) {
90         INTELL_VOICE_LOG_ERROR("get system ability failed");
91         return;
92     }
93     g_sProxy = iface_cast<IIntellVoiceService>(object);
94     if (g_sProxy != nullptr) {
95         INTELL_VOICE_LOG_INFO("init Service Proxy success");
96     }
97     proxyConVar_.notify_one();
98 }
99 
LoadSystemAbilityFail()100 void IntellVoiceManager::LoadSystemAbilityFail()
101 {
102     std::unique_lock<std::mutex> lock(mutex_);
103     g_sProxy = nullptr;
104     proxyConVar_.notify_one();
105 }
106 
CreateIntellVoiceEngine(IntellVoiceEngineType type,sptr<IIntellVoiceEngine> & inst)107 int32_t IntellVoiceManager::CreateIntellVoiceEngine(IntellVoiceEngineType type, sptr<IIntellVoiceEngine> &inst)
108 {
109     INTELL_VOICE_LOG_INFO("enter");
110     if (g_sProxy == nullptr) {
111         INTELL_VOICE_LOG_ERROR("IntellVoiceService Proxy is null");
112         return -1;
113     }
114     return g_sProxy->CreateIntellVoiceEngine(type, inst);
115 }
116 
ReleaseIntellVoiceEngine(IntellVoiceEngineType type)117 int32_t IntellVoiceManager::ReleaseIntellVoiceEngine(IntellVoiceEngineType type)
118 {
119     INTELL_VOICE_LOG_INFO("enter");
120     if (g_sProxy == nullptr) {
121         INTELL_VOICE_LOG_ERROR("IntellVoiceService Proxy is null");
122         return -1;
123     }
124     return g_sProxy->ReleaseIntellVoiceEngine(type);
125 }
126 
RegisterServiceDeathRecipient(sptr<OHOS::IRemoteObject::DeathRecipient> callback)127 int32_t IntellVoiceManager::RegisterServiceDeathRecipient(sptr<OHOS::IRemoteObject::DeathRecipient> callback)
128 {
129     INTELL_VOICE_LOG_INFO("enter");
130     if (g_sProxy == nullptr) {
131         INTELL_VOICE_LOG_ERROR("IntellVoiceService Proxy is null");
132         return -1;
133     }
134 
135     if (callback == nullptr) {
136         INTELL_VOICE_LOG_ERROR("service death recipient is null");
137         return -1;
138     }
139 
140     bool ret = g_sProxy->AsObject()->AddDeathRecipient(callback);
141     if (!ret) {
142         INTELL_VOICE_LOG_ERROR("failed to add death recipient");
143         return -1;
144     }
145     return 0;
146 }
147 
DeregisterServiceDeathRecipient(sptr<OHOS::IRemoteObject::DeathRecipient> callback)148 int32_t IntellVoiceManager::DeregisterServiceDeathRecipient(sptr<OHOS::IRemoteObject::DeathRecipient> callback)
149 {
150     INTELL_VOICE_LOG_INFO("enter");
151     if (g_sProxy == nullptr) {
152         INTELL_VOICE_LOG_ERROR("IntellVoiceService Proxy is null");
153         return -1;
154     }
155 
156     if (callback == nullptr) {
157         INTELL_VOICE_LOG_ERROR("service death recipient is null");
158         return -1;
159     }
160 
161     bool ret = g_sProxy->AsObject()->RemoveDeathRecipient(callback);
162     if (!ret) {
163         INTELL_VOICE_LOG_ERROR("failed to remove death recipient");
164         return -1;
165     }
166     return 0;
167 }
168 }  // namespace IntellVoice
169 }  // namespace OHOS