• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "call_status_manager.h"
17 
18 #include <securec.h>
19 
20 #include "audio_control_manager.h"
21 #include "bluetooth_call_service.h"
22 #include "call_control_manager.h"
23 #include "call_manager_errors.h"
24 #include "call_manager_hisysevent.h"
25 #include "cs_call.h"
26 #include "datashare_predicates.h"
27 #include "hitrace_meter.h"
28 #include "ims_call.h"
29 #include "ott_call.h"
30 #include "report_call_info_handler.h"
31 #include "telephony_log_wrapper.h"
32 
33 namespace OHOS {
34 namespace Telephony {
35 constexpr int32_t INIT_INDEX = 0;
CallStatusManager()36 CallStatusManager::CallStatusManager()
37 {
38     (void)memset_s(&callReportInfo_, sizeof(CallDetailInfo), 0, sizeof(CallDetailInfo));
39     (void)memset_s(&callDetailsInfo_, sizeof(CallDetailsInfo), 0, sizeof(CallDetailsInfo));
40 }
41 
~CallStatusManager()42 CallStatusManager::~CallStatusManager()
43 {
44     UnInit();
45 }
46 
Init()47 int32_t CallStatusManager::Init()
48 {
49     callDetailsInfo_.callVec.clear();
50     mEventIdTransferMap_.clear();
51     mOttEventIdTransferMap_.clear();
52     InitCallBaseEvent();
53     CallIncomingFilterManagerPtr_ = (std::make_unique<CallIncomingFilterManager>()).release();
54     return TELEPHONY_SUCCESS;
55 }
56 
InitCallBaseEvent()57 void CallStatusManager::InitCallBaseEvent()
58 {
59     mEventIdTransferMap_[RequestResultEventId::RESULT_DIAL_NO_CARRIER] = CallAbilityEventId::EVENT_DIAL_NO_CARRIER;
60     mOttEventIdTransferMap_[OttCallEventId::OTT_CALL_EVENT_FUNCTION_UNSUPPORTED] =
61         CallAbilityEventId::EVENT_OTT_FUNCTION_UNSUPPORTED;
62 }
63 
UnInit()64 int32_t CallStatusManager::UnInit()
65 {
66     callDetailsInfo_.callVec.clear();
67     mEventIdTransferMap_.clear();
68     mOttEventIdTransferMap_.clear();
69     return TELEPHONY_SUCCESS;
70 }
71 
HandleCallReportInfo(const CallDetailInfo & info)72 int32_t CallStatusManager::HandleCallReportInfo(const CallDetailInfo &info)
73 {
74     int32_t ret = TELEPHONY_ERR_FAIL;
75     callReportInfo_ = info;
76     switch (info.state) {
77         case TelCallState::CALL_STATUS_ACTIVE:
78             ret = ActiveHandle(info);
79             break;
80         case TelCallState::CALL_STATUS_HOLDING:
81             ret = HoldingHandle(info);
82             break;
83         case TelCallState::CALL_STATUS_DIALING: {
84             ret = DialingHandle(info);
85             FinishAsyncTrace(HITRACE_TAG_OHOS, "DialCall", getpid());
86             DelayedSingleton<CallManagerHisysevent>::GetInstance()->JudgingDialTimeOut(
87                 info.accountId, static_cast<int32_t>(info.callType), static_cast<int32_t>(info.callMode));
88             break;
89         }
90         case TelCallState::CALL_STATUS_ALERTING:
91             ret = AlertHandle(info);
92             break;
93         case TelCallState::CALL_STATUS_INCOMING: {
94             ret = IncomingHandle(info);
95             FinishAsyncTrace(HITRACE_TAG_OHOS, "InComingCall", getpid());
96             DelayedSingleton<CallManagerHisysevent>::GetInstance()->JudgingIncomingTimeOut(
97                 info.accountId, static_cast<int32_t>(info.callType), static_cast<int32_t>(info.callMode));
98             break;
99         }
100         case TelCallState::CALL_STATUS_WAITING:
101             ret = WaitingHandle(info);
102             break;
103         case TelCallState::CALL_STATUS_DISCONNECTED:
104             ret = DisconnectedHandle(info);
105             break;
106         case TelCallState::CALL_STATUS_DISCONNECTING:
107             ret = DisconnectingHandle(info);
108             break;
109         default:
110             TELEPHONY_LOGE("Invalid call state!");
111             break;
112     }
113     TELEPHONY_LOGI("Entry CallStatusManager HandleCallReportInfo");
114     DelayedSingleton<BluetoothCallService>::GetInstance()->GetCallState();
115     return ret;
116 }
117 
118 // handle call state changes, incoming call, outgoing call.
HandleCallsReportInfo(const CallDetailsInfo & info)119 int32_t CallStatusManager::HandleCallsReportInfo(const CallDetailsInfo &info)
120 {
121     bool flag = false;
122     TELEPHONY_LOGI("call list size:%{public}zu,slotId:%{public}d", info.callVec.size(), info.slotId);
123     for (auto &it : info.callVec) {
124         for (const auto &it1 : callDetailsInfo_.callVec) {
125             if (it.index == it1.index) {
126                 // call state changes
127                 if (it.state != it1.state || it.mpty != it1.mpty) {
128                     TELEPHONY_LOGI("handle updated call state:%{public}d", it.state);
129                     HandleCallReportInfo(it);
130                 }
131                 flag = true;
132                 break;
133             }
134         }
135         // incoming/outgoing call handle
136         if (!flag || callDetailsInfo_.callVec.empty()) {
137             TELEPHONY_LOGI("handle new call state:%{public}d", it.state);
138             HandleCallReportInfo(it);
139         }
140         flag = false;
141     }
142     // disconnected calls handle
143     for (auto &it2 : callDetailsInfo_.callVec) {
144         for (const auto &it3 : info.callVec) {
145             if (it2.index == it3.index) {
146                 TELEPHONY_LOGI("state:%{public}d", it2.state);
147                 flag = true;
148                 break;
149             }
150         }
151         if (!flag) {
152             it2.state = TelCallState::CALL_STATUS_DISCONNECTED;
153             HandleCallReportInfo(it2);
154         }
155         flag = false;
156     }
157     callDetailsInfo_.callVec.clear();
158     callDetailsInfo_ = info;
159     return TELEPHONY_SUCCESS;
160 }
161 
HandleDisconnectedCause(const DisconnectedDetails & details)162 int32_t CallStatusManager::HandleDisconnectedCause(const DisconnectedDetails &details)
163 {
164     bool ret = DelayedSingleton<CallControlManager>::GetInstance()->NotifyCallDestroyed(details);
165     if (!ret) {
166         TELEPHONY_LOGI("NotifyCallDestroyed failed!");
167         return CALL_ERR_PHONE_CALLSTATE_NOTIFY_FAILED;
168     }
169     return TELEPHONY_SUCCESS;
170 }
171 
HandleEventResultReportInfo(const CellularCallEventInfo & info)172 int32_t CallStatusManager::HandleEventResultReportInfo(const CellularCallEventInfo &info)
173 {
174     if (info.eventType != CellularCallEventType::EVENT_REQUEST_RESULT_TYPE) {
175         TELEPHONY_LOGE("unexpected type event occurs, eventId:%{public}d", info.eventId);
176         return CALL_ERR_PHONE_TYPE_UNEXPECTED;
177     }
178     TELEPHONY_LOGI("recv one Event, eventId:%{public}d", info.eventId);
179     sptr<CallBase> call = GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_DIALING);
180     if (call != nullptr) {
181         int32_t ret = DealFailDial(call);
182         TELEPHONY_LOGI("DealFailDial ret:%{public}d", ret);
183     }
184     CallEventInfo eventInfo;
185     (void)memset_s(&eventInfo, sizeof(CallEventInfo), 0, sizeof(CallEventInfo));
186     if (mEventIdTransferMap_.find(info.eventId) != mEventIdTransferMap_.end()) {
187         eventInfo.eventId = mEventIdTransferMap_[info.eventId];
188         DialParaInfo dialInfo;
189         if (eventInfo.eventId == CallAbilityEventId::EVENT_DIAL_NO_CARRIER) {
190             DelayedSingleton<CallControlManager>::GetInstance()->GetDialParaInfo(dialInfo);
191             if (dialInfo.number.length() > static_cast<size_t>(kMaxNumberLen)) {
192                 TELEPHONY_LOGE("Number out of limit!");
193                 return CALL_ERR_NUMBER_OUT_OF_RANGE;
194             }
195             if (memcpy_s(eventInfo.phoneNum, kMaxNumberLen, dialInfo.number.c_str(), dialInfo.number.length()) != EOK) {
196                 TELEPHONY_LOGE("memcpy_s failed!");
197                 return TELEPHONY_ERR_MEMCPY_FAIL;
198             }
199         }
200         DelayedSingleton<CallControlManager>::GetInstance()->NotifyCallEventUpdated(eventInfo);
201     } else {
202         TELEPHONY_LOGW("unknown type Event, eventid %{public}d", info.eventId);
203     }
204     return TELEPHONY_SUCCESS;
205 }
206 
HandleOttEventReportInfo(const OttCallEventInfo & info)207 int32_t CallStatusManager::HandleOttEventReportInfo(const OttCallEventInfo &info)
208 {
209     TELEPHONY_LOGI("recv one Event, eventId:%{public}d", info.ottCallEventId);
210     CallEventInfo eventInfo;
211     (void)memset_s(&eventInfo, sizeof(CallEventInfo), 0, sizeof(CallEventInfo));
212     if (mOttEventIdTransferMap_.find(info.ottCallEventId) != mOttEventIdTransferMap_.end()) {
213         eventInfo.eventId = mOttEventIdTransferMap_[info.ottCallEventId];
214         if (strlen(info.bundleName) > static_cast<size_t>(kMaxNumberLen)) {
215             TELEPHONY_LOGE("Number out of limit!");
216             return CALL_ERR_NUMBER_OUT_OF_RANGE;
217         }
218         if (memcpy_s(eventInfo.bundleName, kMaxNumberLen, info.bundleName, strlen(info.bundleName)) != EOK) {
219             TELEPHONY_LOGE("memcpy_s failed!");
220             return TELEPHONY_ERR_MEMCPY_FAIL;
221         }
222         DelayedSingleton<CallControlManager>::GetInstance()->NotifyCallEventUpdated(eventInfo);
223     } else {
224         TELEPHONY_LOGW("unknown type Event, eventid %{public}d", info.ottCallEventId);
225     }
226     return TELEPHONY_SUCCESS;
227 }
228 
IncomingHandle(const CallDetailInfo & info)229 int32_t CallStatusManager::IncomingHandle(const CallDetailInfo &info)
230 {
231     int32_t ret = IncomingHandlePolicy(info);
232     if (ret != TELEPHONY_SUCCESS) {
233         TELEPHONY_LOGE("IncomingHandlePolicy failed!");
234         if (info.state == TelCallState::CALL_STATUS_INCOMING) {
235             CallManagerHisysevent::WriteIncomingCallFaultEvent(info.accountId, static_cast<int32_t>(info.callType),
236                 static_cast<int32_t>(info.callMode), ret, "IncomingHandlePolicy failed");
237         }
238         return ret;
239     }
240     if (info.callType == CallType::TYPE_CS || info.callType == CallType::TYPE_IMS) {
241         ret = IncomingFilterPolicy(info);
242         if (ret != TELEPHONY_SUCCESS) {
243             TELEPHONY_LOGE("IncomingFilterPolicy failed!");
244             return ret;
245         }
246     }
247     sptr<CallBase> call = CreateNewCall(info, CallDirection::CALL_DIRECTION_IN);
248     if (call == nullptr) {
249         TELEPHONY_LOGE("CreateNewCall failed!");
250         return CALL_ERR_CALL_OBJECT_IS_NULL;
251     }
252 
253     // allow list filtering
254     // Get the contact data from the database
255     ContactInfo contactInfo = {
256         .name = "",
257         .number = "",
258         .isContacterExists = false,
259         .ringtonePath = "",
260         .isSendToVoicemail = false,
261         .isEcc = false,
262         .isVoiceMail = false,
263     };
264     QueryCallerInfo(contactInfo, std::string(info.phoneNum));
265     call->SetCallerInfo(contactInfo);
266 
267     DelayedSingleton<CallControlManager>::GetInstance()->NotifyNewCallCreated(call);
268     ret = UpdateCallState(call, info.state);
269     if (ret != TELEPHONY_SUCCESS) {
270         TELEPHONY_LOGE("UpdateCallState failed!");
271         return ret;
272     }
273     ret = FilterResultsDispose(call);
274     if (ret != TELEPHONY_SUCCESS) {
275         TELEPHONY_LOGE("FilterResultsDispose failed!");
276     }
277     return ret;
278 }
279 
QueryCallerInfo(ContactInfo & contactInfo,std::string phoneNum)280 void CallStatusManager::QueryCallerInfo(ContactInfo &contactInfo, std::string phoneNum)
281 {
282     TELEPHONY_LOGI("Entry CallStatusManager QueryCallerInfo");
283     std::shared_ptr<CallDataBaseHelper> callDataPtr = DelayedSingleton<CallDataBaseHelper>::GetInstance();
284     if (callDataPtr == nullptr) {
285         TELEPHONY_LOGE("callDataPtr is nullptr!");
286         return;
287     }
288     DataShare::DataSharePredicates predicates;
289     predicates.EqualTo(DETAIL_INFO, phoneNum);
290     predicates.And();
291     predicates.EqualTo(CONTENT_TYPE, PHONE);
292     bool ret = callDataPtr->Query(contactInfo, predicates);
293     if (!ret) {
294         TELEPHONY_LOGE("Query contact database fail!");
295     }
296 }
297 
IncomingFilterPolicy(const CallDetailInfo & info)298 int32_t CallStatusManager::IncomingFilterPolicy(const CallDetailInfo &info)
299 {
300     if (CallIncomingFilterManagerPtr_ == nullptr) {
301         TELEPHONY_LOGE("CallIncomingFilterManagerPtr_ is null");
302         return TELEPHONY_ERR_LOCAL_PTR_NULL;
303     }
304     if (CallIncomingFilterManagerPtr_->IsFirstIncoming()) {
305         CallIncomingFilterManagerPtr_->UpdateIncomingFilterData();
306     }
307     return CallIncomingFilterManagerPtr_->doIncomingFilter(info);
308 }
309 
CallFilterCompleteResult(const CallDetailInfo & info)310 void CallStatusManager::CallFilterCompleteResult(const CallDetailInfo &info)
311 {
312     int32_t ret = TELEPHONY_ERR_FAIL;
313     sptr<CallBase> call = CreateNewCall(info, CallDirection::CALL_DIRECTION_IN);
314     if (call == nullptr) {
315         TELEPHONY_LOGE("CreateNewCall failed!");
316         return;
317     }
318 #ifdef ABILITY_DATABASE_SUPPORT
319     // allow list filtering
320     // Get the contact data from the database
321     GetCallerInfoDate(ContactInfo);
322     SetCallerInfo(contactInfo);
323 #endif
324     DelayedSingleton<CallControlManager>::GetInstance()->NotifyNewCallCreated(call);
325     ret = UpdateCallState(call, info.state);
326     if (ret != TELEPHONY_SUCCESS) {
327         TELEPHONY_LOGE("UpdateCallState failed!");
328         return;
329     }
330     ret = FilterResultsDispose(call);
331     if (ret != TELEPHONY_SUCCESS) {
332         TELEPHONY_LOGE("FilterResultsDispose failed!");
333         return;
334     }
335 }
336 
UpdateDialingCallInfo(const CallDetailInfo & info)337 int32_t CallStatusManager::UpdateDialingCallInfo(const CallDetailInfo &info)
338 {
339     sptr<CallBase> call = GetOneCallObjectByIndex(INIT_INDEX);
340     if (call == nullptr) {
341         TELEPHONY_LOGE("call is nullptr");
342         return TELEPHONY_ERR_LOCAL_PTR_NULL;
343     }
344 
345     std::string phoneNum(info.phoneNum);
346     std::string oriNum = call->GetAccountNumber();
347     if (oriNum.length() > phoneNum.length()) {
348         oriNum = oriNum.substr(INIT_INDEX, phoneNum.length());
349     }
350     if (oriNum != phoneNum) {
351         TELEPHONY_LOGE("phoneNum is not match");
352         return CALL_ERR_DIAL_FAILED;
353     }
354     call = RefreshCallIfNecessary(call, info);
355     call->SetCallIndex(info.index);
356     call->SetBundleName(info.bundleName);
357     call->SetSlotId(info.accountId);
358     call->SetTelCallState(info.state);
359     call->SetVideoStateType(info.callMode);
360     call->SetCallType(info.callType);
361     return TELEPHONY_SUCCESS;
362 }
363 
DialingHandle(const CallDetailInfo & info)364 int32_t CallStatusManager::DialingHandle(const CallDetailInfo &info)
365 {
366     TELEPHONY_LOGI("handle dialing state");
367     if (info.index > 0) {
368         TELEPHONY_LOGI("need update call info");
369         return UpdateDialingCallInfo(info);
370     }
371     sptr<CallBase> call = CreateNewCall(info, CallDirection::CALL_DIRECTION_OUT);
372     if (call == nullptr) {
373         TELEPHONY_LOGE("CreateNewCall failed!");
374         return TELEPHONY_ERR_LOCAL_PTR_NULL;
375     }
376     int32_t ret = call->DialingProcess();
377     if (ret != TELEPHONY_SUCCESS) {
378         return ret;
379     }
380     DelayedSingleton<CallControlManager>::GetInstance()->NotifyNewCallCreated(call);
381     ret = UpdateCallState(call, TelCallState::CALL_STATUS_DIALING);
382     if (ret != TELEPHONY_SUCCESS) {
383         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
384     }
385     return ret;
386 }
387 
ActiveHandle(const CallDetailInfo & info)388 int32_t CallStatusManager::ActiveHandle(const CallDetailInfo &info)
389 {
390     TELEPHONY_LOGI("handle active state");
391     sptr<CallBase> call = GetOneCallObjectByIndex(info.index);
392     if (call == nullptr) {
393         TELEPHONY_LOGE("Call is NULL");
394         return TELEPHONY_ERR_LOCAL_PTR_NULL;
395     }
396     call = RefreshCallIfNecessary(call, info);
397     // call state change active, need to judge if launching a conference
398     if (info.mpty == 1) {
399         int32_t mainCallId = ERR_ID;
400         call->LaunchConference();
401         call->GetMainCallId(mainCallId);
402         sptr<CallBase> mainCall = GetOneCallObject(mainCallId);
403         if (mainCall != nullptr) {
404             mainCall->SetTelConferenceState(TelConferenceState::TEL_CONFERENCE_ACTIVE);
405         }
406     } else if (call->ExitConference() == TELEPHONY_SUCCESS) {
407         TELEPHONY_LOGI("SubCallSeparateFromConference success!");
408     } else {
409         TELEPHONY_LOGI("SubCallSeparateFromConference fail!");
410     }
411     int32_t ret = UpdateCallState(call, TelCallState::CALL_STATUS_ACTIVE);
412     if (ret != TELEPHONY_SUCCESS) {
413         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
414         return ret;
415     }
416 #ifdef AUDIO_SUPPORT
417     ToSpeakerPhone(call);
418     DelayedSingleton<AudioControlManager>::GetInstance()->SetVolumeAudible();
419 #endif
420     TELEPHONY_LOGI("handle active state success");
421     return ret;
422 }
423 
HoldingHandle(const CallDetailInfo & info)424 int32_t CallStatusManager::HoldingHandle(const CallDetailInfo &info)
425 {
426     TELEPHONY_LOGI("handle holding state");
427     std::string tmpStr(info.phoneNum);
428     sptr<CallBase> call = GetOneCallObjectByIndex(info.index);
429     if (call == nullptr) {
430         TELEPHONY_LOGE("Call is NULL");
431         return TELEPHONY_ERR_LOCAL_PTR_NULL;
432     }
433     // if the call is in a conference, it will exit, otherwise just set it holding
434     call = RefreshCallIfNecessary(call, info);
435     int32_t ret = call->HoldConference();
436     if (ret == TELEPHONY_SUCCESS) {
437         TELEPHONY_LOGI("HoldConference success");
438     }
439     ret = UpdateCallState(call, TelCallState::CALL_STATUS_HOLDING);
440     if (ret != TELEPHONY_SUCCESS) {
441         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
442     }
443     return ret;
444 }
445 
WaitingHandle(const CallDetailInfo & info)446 int32_t CallStatusManager::WaitingHandle(const CallDetailInfo &info)
447 {
448     return IncomingHandle(info);
449 }
450 
AlertHandle(const CallDetailInfo & info)451 int32_t CallStatusManager::AlertHandle(const CallDetailInfo &info)
452 {
453     TELEPHONY_LOGI("handle alerting state");
454     std::string tmpStr(info.phoneNum);
455     sptr<CallBase> call = GetOneCallObjectByIndex(info.index);
456     if (call == nullptr) {
457         TELEPHONY_LOGE("Call is NULL");
458         return TELEPHONY_ERR_LOCAL_PTR_NULL;
459     }
460     call = RefreshCallIfNecessary(call, info);
461     int32_t ret = UpdateCallState(call, TelCallState::CALL_STATUS_ALERTING);
462     if (ret != TELEPHONY_SUCCESS) {
463         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
464         return ret;
465     }
466 #ifdef AUDIO_SUPPORT
467     ToSpeakerPhone(call);
468     TurnOffMute(call);
469     DelayedSingleton<AudioControlManager>::GetInstance()->SetVolumeAudible();
470 #endif
471     return ret;
472 }
473 
DisconnectingHandle(const CallDetailInfo & info)474 int32_t CallStatusManager::DisconnectingHandle(const CallDetailInfo &info)
475 {
476     TELEPHONY_LOGI("handle disconnecting state");
477     std::string tmpStr(info.phoneNum);
478     sptr<CallBase> call = GetOneCallObjectByIndex(info.index);
479     if (call == nullptr) {
480         TELEPHONY_LOGE("Call is NULL");
481         return TELEPHONY_ERR_LOCAL_PTR_NULL;
482     }
483     call = RefreshCallIfNecessary(call, info);
484     int32_t ret = UpdateCallState(call, TelCallState::CALL_STATUS_DISCONNECTING);
485     if (ret != TELEPHONY_SUCCESS) {
486         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
487     }
488     return ret;
489 }
490 
DisconnectedHandle(const CallDetailInfo & info)491 int32_t CallStatusManager::DisconnectedHandle(const CallDetailInfo &info)
492 {
493     TELEPHONY_LOGI("handle disconnected state");
494     std::string tmpStr(info.phoneNum);
495     sptr<CallBase> call = GetOneCallObjectByIndex(info.index);
496     if (call == nullptr) {
497         TELEPHONY_LOGE("Call is NULL");
498         return TELEPHONY_ERR_LOCAL_PTR_NULL;
499     }
500     call = RefreshCallIfNecessary(call, info);
501     bool isNeedUnHold = false;
502     std::vector<std::u16string> callIdList;
503     call->GetSubCallIdList(callIdList);
504     if (callIdList.size() == 0 || callIdList.size() == 1) {
505         isNeedUnHold = true;
506     }
507     sptr<CallBase> holdCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_HOLD);
508     if (call->GetCallRunningState() != CallRunningState::CALL_RUNNING_STATE_HOLD) {
509         if (holdCall != nullptr && isNeedUnHold) {
510             TELEPHONY_LOGI("release the call and recover the held call");
511             holdCall->UnHoldCall();
512         }
513     }
514     int32_t ret = call->ExitConference();
515     if (ret == TELEPHONY_SUCCESS) {
516         TELEPHONY_LOGI("SubCallSeparateFromConference success");
517     }
518     ret = UpdateCallState(call, TelCallState::CALL_STATUS_DISCONNECTED);
519     if (ret != TELEPHONY_SUCCESS) {
520         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
521         return ret;
522     }
523     DeleteOneCallObject(call->GetCallID());
524     return ret;
525 }
526 
UpdateCallState(sptr<CallBase> & call,TelCallState nextState)527 int32_t CallStatusManager::UpdateCallState(sptr<CallBase> &call, TelCallState nextState)
528 {
529     TELEPHONY_LOGI("UpdateCallState start");
530     if (call == nullptr) {
531         TELEPHONY_LOGE("Call is NULL");
532         return TELEPHONY_ERR_LOCAL_PTR_NULL;
533     }
534     TelCallState priorState = call->GetTelCallState();
535     TELEPHONY_LOGI("priorState:%{public}d, nextState:%{public}d", priorState, nextState);
536     if (priorState == TelCallState::CALL_STATUS_INCOMING && nextState == TelCallState::CALL_STATUS_ACTIVE) {
537         DelayedSingleton<CallManagerHisysevent>::GetInstance()->JudgingAnswerTimeOut(
538             call->GetSlotId(), call->GetCallID(), static_cast<int32_t>(call->GetVideoStateType()));
539     }
540     // need DTMF judge
541     int32_t ret = call->SetTelCallState(nextState);
542     if (ret != TELEPHONY_SUCCESS && ret != CALL_ERR_NOT_NEW_STATE) {
543         TELEPHONY_LOGE("SetTelCallState failed");
544         return TELEPHONY_ERR_LOCAL_PTR_NULL;
545     }
546     // notify state changed
547     if (!DelayedSingleton<CallControlManager>::GetInstance()->NotifyCallStateUpdated(call, priorState, nextState)) {
548         TELEPHONY_LOGE(
549             "NotifyCallStateUpdated failed! priorState:%{public}d,nextState:%{public}d", priorState, nextState);
550         if (nextState == TelCallState::CALL_STATUS_INCOMING) {
551             CallManagerHisysevent::WriteIncomingCallFaultEvent(call->GetSlotId(),
552                 static_cast<int32_t>(call->GetCallType()), static_cast<int32_t>(call->GetVideoStateType()), ret,
553                 "NotifyCallStateUpdated failed");
554         }
555         return CALL_ERR_PHONE_CALLSTATE_NOTIFY_FAILED;
556     }
557     return TELEPHONY_SUCCESS;
558 }
559 
RefreshCallIfNecessary(const sptr<CallBase> & call,const CallDetailInfo & info)560 sptr<CallBase> CallStatusManager::RefreshCallIfNecessary(const sptr<CallBase> &call, const CallDetailInfo &info)
561 {
562     TELEPHONY_LOGI("RefreshCallIfNecessary");
563     if (call->GetCallType() == info.callType) {
564         TELEPHONY_LOGI("RefreshCallIfNecessary not need Refresh");
565         return call;
566     }
567     TelCallState priorState = call->GetTelCallState();
568     CallAttributeInfo attrInfo;
569     (void)memset_s(&attrInfo, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
570     call->GetCallAttributeBaseInfo(attrInfo);
571     sptr<CallBase> newCall = CreateNewCall(info, attrInfo.callDirection);
572     if (newCall == nullptr) {
573         TELEPHONY_LOGE("RefreshCallIfNecessary createCallFail");
574         return call;
575     }
576     newCall->SetCallRunningState(call->GetCallRunningState());
577     newCall->SetTelConferenceState(call->GetTelConferenceState());
578     newCall->SetStartTime(attrInfo.startTime);
579     newCall->SetPolicyFlag(PolicyFlag(call->GetPolicyFlag()));
580     newCall->SetSpeakerphoneOn(call->IsSpeakerphoneOn());
581     newCall->SetCallEndedType(call->GetCallEndedType());
582     newCall->SetCallBeginTime(attrInfo.callBeginTime);
583     newCall->SetCallEndTime(attrInfo.callEndTime);
584     newCall->SetRingBeginTime(attrInfo.ringBeginTime);
585     newCall->SetRingEndTime(attrInfo.ringEndTime);
586     newCall->SetAnswerType(attrInfo.answerType);
587     DeleteOneCallObject(call->GetCallID());
588     newCall->SetCallId(call->GetCallID());
589     newCall->SetTelCallState(priorState);
590     return newCall;
591 }
592 
ToSpeakerPhone(sptr<CallBase> & call)593 int32_t CallStatusManager::ToSpeakerPhone(sptr<CallBase> &call)
594 {
595     int32_t ret = TELEPHONY_ERR_FAIL;
596     if (call == nullptr) {
597         TELEPHONY_LOGE("Call is NULL");
598         return TELEPHONY_ERR_LOCAL_PTR_NULL;
599     }
600     if (call->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_DIALING) {
601         TELEPHONY_LOGI("Call is CALL_STATUS_DIALING");
602         return ret;
603     }
604     if (call->IsSpeakerphoneOn()) {
605         AudioDevice device = {
606             .deviceType = AudioDeviceType::DEVICE_SPEAKER,
607             .address = { 0 },
608         };
609         DelayedSingleton<AudioControlManager>::GetInstance()->SetAudioDevice(device);
610         ret = call->SetSpeakerphoneOn(false);
611     }
612     return ret;
613 }
614 
TurnOffMute(sptr<CallBase> & call)615 int32_t CallStatusManager::TurnOffMute(sptr<CallBase> &call)
616 {
617     bool enabled = true;
618     if (HasEmergencyCall(enabled) != TELEPHONY_SUCCESS) {
619         TELEPHONY_LOGI("CallStatusManager::TurnOffMute HasEmergencyCall failed.");
620     }
621     if (call->GetEmergencyState() || enabled) {
622         DelayedSingleton<AudioControlManager>::GetInstance()->SetMute(false);
623     } else {
624         DelayedSingleton<AudioControlManager>::GetInstance()->SetMute(true);
625     }
626     return TELEPHONY_SUCCESS;
627 }
628 
CreateNewCall(const CallDetailInfo & info,CallDirection dir)629 sptr<CallBase> CallStatusManager::CreateNewCall(const CallDetailInfo &info, CallDirection dir)
630 {
631     sptr<CallBase> callPtr = nullptr;
632     DialParaInfo paraInfo;
633     AppExecFwk::PacMap extras;
634     extras.Clear();
635     PackParaInfo(paraInfo, info, dir, extras);
636     switch (info.callType) {
637         case CallType::TYPE_CS: {
638             if (dir == CallDirection::CALL_DIRECTION_OUT) {
639                 callPtr = (std::make_unique<CSCall>(paraInfo, extras)).release();
640             } else {
641                 callPtr = (std::make_unique<CSCall>(paraInfo)).release();
642             }
643             break;
644         }
645         case CallType::TYPE_IMS: {
646             if (dir == CallDirection::CALL_DIRECTION_OUT) {
647                 callPtr = (std::make_unique<IMSCall>(paraInfo, extras)).release();
648             } else {
649                 callPtr = (std::make_unique<IMSCall>(paraInfo)).release();
650             }
651             break;
652         }
653         case CallType::TYPE_OTT: {
654             if (dir == CallDirection::CALL_DIRECTION_OUT) {
655                 callPtr = (std::make_unique<OTTCall>(paraInfo, extras)).release();
656             } else {
657                 callPtr = (std::make_unique<OTTCall>(paraInfo)).release();
658             }
659             break;
660         }
661         default:
662             return nullptr;
663     }
664     if (callPtr == nullptr) {
665         TELEPHONY_LOGE("CreateNewCall failed!");
666         return nullptr;
667     }
668     AddOneCallObject(callPtr);
669     return callPtr;
670 }
671 
PackParaInfo(DialParaInfo & paraInfo,const CallDetailInfo & info,CallDirection dir,AppExecFwk::PacMap & extras)672 void CallStatusManager::PackParaInfo(
673     DialParaInfo &paraInfo, const CallDetailInfo &info, CallDirection dir, AppExecFwk::PacMap &extras)
674 {
675     paraInfo.isEcc = false;
676     paraInfo.dialType = DialType::DIAL_CARRIER_TYPE;
677     if (dir == CallDirection::CALL_DIRECTION_OUT) {
678         DelayedSingleton<CallControlManager>::GetInstance()->GetDialParaInfo(paraInfo, extras);
679     }
680     paraInfo.number = info.phoneNum;
681     paraInfo.callId = GetNewCallId();
682     paraInfo.index = info.index;
683     paraInfo.videoState = VideoStateType::TYPE_VOICE;
684     paraInfo.accountId = info.accountId;
685     paraInfo.callType = info.callType;
686     paraInfo.callState = info.state;
687     paraInfo.bundleName = info.bundleName;
688 }
689 } // namespace Telephony
690 } // namespace OHOS
691