• 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 
16 #include "voip_call_connection.h"
17 
18 #include "call_manager_hisysevent.h"
19 #include "i_voip_call_manager_callback.h"
20 #include "i_voip_call_manager_service.h"
21 #include "iservice_registry.h"
22 #include "system_ability.h"
23 #include "system_ability_definition.h"
24 #include "telephony_log_wrapper.h"
25 #include "voip_call_manager_proxy.h"
26 
27 namespace OHOS {
28 namespace Telephony {
VoipCallConnection()29 VoipCallConnection::VoipCallConnection()
30     : systemAbilityId_(TELEPHONY_VOIP_CALL_MANAGER_SYS_ABILITY_ID), connectCallManagerState_(false)
31 {}
32 
~VoipCallConnection()33 VoipCallConnection::~VoipCallConnection()
34 {
35     UnInit();
36 }
37 
Init(int32_t systemAbilityId)38 void VoipCallConnection::Init(int32_t systemAbilityId)
39 {
40     std::lock_guard<std::mutex> lock(mutex_);
41     if (connectCallManagerState_) {
42         TELEPHONY_LOGE("Init, connectState is true");
43         return;
44     }
45     systemAbilityId_ = systemAbilityId;
46     TELEPHONY_LOGI("systemAbilityId_ = %{public}d", systemAbilityId);
47     if (GetCallManagerProxy() == TELEPHONY_SUCCESS) {
48         connectCallManagerState_ = true;
49     }
50     statusChangeListener_ = new (std::nothrow) SystemAbilityListener();
51     if (statusChangeListener_ == nullptr) {
52         TELEPHONY_LOGE("Init, failed to create statusChangeListener.");
53         return;
54     }
55     sptr<ISystemAbilityManager> managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
56     if (managerPtr == nullptr) {
57         TELEPHONY_LOGE("voipconnect managerPtr is null");
58         return;
59     }
60     int32_t ret = managerPtr->SubscribeSystemAbility(systemAbilityId_, statusChangeListener_);
61     if (ret != TELEPHONY_SUCCESS) {
62         TELEPHONY_LOGE("Init, failed to subscribe sa:%{public}d", systemAbilityId_);
63         return;
64     }
65     TELEPHONY_LOGI("subscribe voip call manager successfully!");
66 }
67 
UnInit()68 void VoipCallConnection::UnInit()
69 {
70     std::lock_guard<std::mutex> lock(mutex_);
71     voipCallManagerInterfacePtr_ = nullptr;
72     connectCallManagerState_ = false;
73     TELEPHONY_LOGI("voip call connection uninit");
74 }
75 
GetCallManagerProxy()76 int32_t VoipCallConnection::GetCallManagerProxy()
77 {
78     TELEPHONY_LOGI("Voipconnect GetCallManagerProxy start");
79     if (voipCallManagerInterfacePtr_ != nullptr) {
80         return TELEPHONY_SUCCESS;
81     }
82     sptr<ISystemAbilityManager> managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
83     if (managerPtr == nullptr) {
84         TELEPHONY_LOGI("Voipconnect managerPtr is null");
85         return TELEPHONY_ERR_LOCAL_PTR_NULL;
86     }
87     sptr<IVoipCallManagerService> voipCallManagerInterfacePtr = nullptr;
88     sptr<IRemoteObject> iRemoteObjectPtr = managerPtr->GetSystemAbility(systemAbilityId_);
89     if (iRemoteObjectPtr == nullptr) {
90         TELEPHONY_LOGI("Voipconnect iRemoteObjectPtr is null");
91         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
92     }
93     voipCallManagerInterfacePtr = iface_cast<IVoipCallManagerService>(iRemoteObjectPtr);
94     if (!voipCallManagerInterfacePtr) {
95         TELEPHONY_LOGI("Voipconnect GetCallManagerProxy voipCallManagerInterfacePtr is null");
96         return TELEPHONY_ERR_LOCAL_PTR_NULL;
97     }
98 
99     voipCallManagerInterfacePtr_ = voipCallManagerInterfacePtr;
100     return TELEPHONY_SUCCESS;
101 }
102 
AnswerCall(const VoipCallEventInfo & events,int32_t videoState)103 int32_t VoipCallConnection::AnswerCall(const VoipCallEventInfo &events, int32_t videoState)
104 {
105     std::lock_guard<std::mutex> lock(mutex_);
106     GetCallManagerProxy();
107     if (voipCallManagerInterfacePtr_ == nullptr) {
108         TELEPHONY_LOGI("Voipconnect AnswerCall voipCallManagerInterfacePtr_ is null");
109         return TELEPHONY_ERROR;
110     }
111     return voipCallManagerInterfacePtr_->Answer(events, videoState);
112 }
113 
RejectCall(const VoipCallEventInfo & events)114 int32_t VoipCallConnection::RejectCall(const VoipCallEventInfo &events)
115 {
116     std::lock_guard<std::mutex> lock(mutex_);
117     GetCallManagerProxy();
118     if (voipCallManagerInterfacePtr_ == nullptr) {
119         TELEPHONY_LOGI("Voipconnect RejectCall voipCallManagerInterfacePtr_ is null");
120         return TELEPHONY_ERROR;
121     }
122     return voipCallManagerInterfacePtr_->Reject(events);
123 }
124 
HangUpCall(const VoipCallEventInfo & events)125 int32_t VoipCallConnection::HangUpCall(const VoipCallEventInfo &events)
126 {
127     std::lock_guard<std::mutex> lock(mutex_);
128     GetCallManagerProxy();
129     if (voipCallManagerInterfacePtr_ == nullptr) {
130         TELEPHONY_LOGI("Voipconnect HangUpCall voipCallManagerInterfacePtr_ is null");
131         return TELEPHONY_ERROR;
132     }
133     CallManagerHisysevent::WriteVoipCallStatisticalEvent(events.voipCallId, events.bundleName,
134         events.uid, "HungupByCallmanager");
135     return voipCallManagerInterfacePtr_->HangUp(events);
136 }
137 
RegisterCallManagerCallBack(const sptr<ICallStatusCallback> & callback)138 int32_t VoipCallConnection::RegisterCallManagerCallBack(const sptr<ICallStatusCallback> &callback)
139 {
140     std::lock_guard<std::mutex> lock(mutex_);
141     GetCallManagerProxy();
142     if (voipCallManagerInterfacePtr_ == nullptr) {
143         TELEPHONY_LOGI("Voipconnect RegisterCallManagerCallBack voipCallManagerInterfacePtr_ is null");
144         return TELEPHONY_ERROR;
145     }
146     voipCallCallbackPtr_ = callback;
147     return voipCallManagerInterfacePtr_->RegisterCallManagerCallBack(callback);
148 }
149 
UnRegisterCallManagerCallBack()150 int32_t VoipCallConnection::UnRegisterCallManagerCallBack()
151 {
152     int32_t ret;
153     {
154         std::lock_guard<std::mutex> lock(mutex_);
155         GetCallManagerProxy();
156         if (voipCallManagerInterfacePtr_ == nullptr) {
157             TELEPHONY_LOGI("Voipconnect UnRegisterCallManagerCallBack voipCallManagerInterfacePtr_ is null");
158             return TELEPHONY_ERROR;
159         }
160         voipCallCallbackPtr_ = nullptr;
161         ret = voipCallManagerInterfacePtr_->UnRegisterCallManagerCallBack();
162     }
163     UnInit();
164     return ret;
165 }
166 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)167 void VoipCallConnection::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
168 {
169     auto voipCallConnection = DelayedSingleton<VoipCallConnection>::GetInstance();
170     if (voipCallConnection == nullptr) {
171         TELEPHONY_LOGE("voipCallConnection is nullptr");
172         return;
173     }
174     voipCallConnection->Init(systemAbilityId);
175 }
176 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)177 void VoipCallConnection::SystemAbilityListener::OnRemoveSystemAbility(
178     int32_t systemAbilityId, const std::string &deviceId)
179 {
180     auto voipCallConnection = DelayedSingleton<VoipCallConnection>::GetInstance();
181     if (voipCallConnection == nullptr) {
182         TELEPHONY_LOGE("voipCallConnection is nullptr");
183         return;
184     }
185     voipCallConnection->ClearVoipCall();
186     voipCallConnection->UnInit();
187 }
188 
BuildDisconnectedCallInfo(CallReportInfo & callReportInfo,const VoipCallReportInfo & voipInfo)189 void VoipCallConnection::BuildDisconnectedCallInfo(CallReportInfo &callReportInfo, const VoipCallReportInfo &voipInfo)
190 {
191     callReportInfo.callType = CallType::TYPE_VOIP;
192     callReportInfo.state = TelCallState::CALL_STATUS_DISCONNECTED;
193     callReportInfo.voipCallInfo.voipCallId = voipInfo.voipCallId;
194     callReportInfo.voipCallInfo.extensionId = voipInfo.extensionId;
195     callReportInfo.voipCallInfo.userName = voipInfo.userName;
196     (callReportInfo.voipCallInfo.userProfile).assign(voipInfo.userProfile.begin(), voipInfo.userProfile.end());
197     callReportInfo.voipCallInfo.abilityName = voipInfo.abilityName;
198     callReportInfo.voipCallInfo.voipBundleName = voipInfo.voipBundleName;
199     callReportInfo.voipCallInfo.showBannerForIncomingCall = voipInfo.showBannerForIncomingCall;
200     callReportInfo.voipCallInfo.isConferenceCall = voipInfo.isConferenceCall;
201     callReportInfo.voipCallInfo.isVoiceAnswerSupported = voipInfo.isVoiceAnswerSupported;
202     callReportInfo.voipCallInfo.hasMicPermission = voipInfo.hasMicPermission;
203     callReportInfo.voipCallInfo.isCapsuleSticky = voipInfo.isCapsuleSticky;
204     callReportInfo.voipCallInfo.uid = voipInfo.uid;
205 }
206 
ClearVoipCall()207 void VoipCallConnection::ClearVoipCall()
208 {
209     if (!CallObjectManager::HasVoipCallExist()) {
210         TELEPHONY_LOGI("no voip call exist, no need to clear");
211         return;
212     }
213     std::list<sptr<CallBase>> allCallList = CallObjectManager::GetAllCallList();
214     for (auto call : allCallList) {
215         if (call != nullptr && call->GetCallType() == CallType::TYPE_VOIP) {
216             //再上报一次,防止界面未销毁,上报DISCONNECTED后,会删除CallObject对象
217             std::lock_guard<std::mutex> lock(mutex_);
218             if (voipCallCallbackPtr_ != nullptr) {
219                 TELEPHONY_LOGI("Report disconnected voip call again!");
220                 CallAttributeInfo info;
221                 call->GetCallAttributeInfo(info);
222                 CallReportInfo callReportInfo;
223                 BuildDisconnectedCallInfo(callReportInfo, info.voipCallInfo);
224                 callReportInfo.callMode = call->GetVideoStateType();
225                 voipCallCallbackPtr_->UpdateCallReportInfo(callReportInfo);
226             } else {
227                 CallObjectManager::DeleteOneCallObject(call);
228             }
229         }
230     }
231 }
232 
SendCallUiEvent(std::string voipCallId,const CallAudioEvent & callAudioEvent)233 int32_t VoipCallConnection::SendCallUiEvent(std::string voipCallId, const CallAudioEvent &callAudioEvent)
234 {
235     std::lock_guard<std::mutex> lock(mutex_);
236     GetCallManagerProxy();
237     if (voipCallManagerInterfacePtr_ == nullptr) {
238         TELEPHONY_LOGE("voipCallManagerInterfacePtr_ is nullptr");
239         return TELEPHONY_ERROR;
240     }
241     return voipCallManagerInterfacePtr_->SendCallUiEvent(voipCallId, callAudioEvent);
242 }
243 
SendCallUiEventForWindow(AppExecFwk::PacMap & extras)244 int32_t VoipCallConnection::SendCallUiEventForWindow(AppExecFwk::PacMap &extras)
245 {
246     std::lock_guard<std::mutex> lock(mutex_);
247     GetCallManagerProxy();
248     if (voipCallManagerInterfacePtr_ == nullptr) {
249         TELEPHONY_LOGE("voipCallManagerInterfacePtr_ is nullptr");
250         return TELEPHONY_ERROR;
251     }
252     return voipCallManagerInterfacePtr_->SendCallUiEventForWindow(extras);
253 }
254 } // namespace Telephony
255 } // namespace OHOS
256