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 "call_number_utils.h"
26 #include "call_request_event_handler_helper.h"
27 #include "core_service_client.h"
28 #include "cs_call.h"
29 #include "datashare_predicates.h"
30 #include "hitrace_meter.h"
31 #include "ims_call.h"
32 #include "os_account_manager.h"
33 #include "ott_call.h"
34 #include "report_call_info_handler.h"
35 #include "satellite_call.h"
36 #include "satellite_call_control.h"
37 #include "settings_datashare_helper.h"
38 #include "telephony_log_wrapper.h"
39 #include "call_number_utils.h"
40 #include "voip_call.h"
41 #include "uri.h"
42 #include "ffrt.h"
43 #include "parameters.h"
44 #include "spam_call_adapter.h"
45 #include "call_superprivacy_control_manager.h"
46 #include "notification_helper.h"
47 #include "call_voice_assistant_manager.h"
48 
49 namespace OHOS {
50 namespace Telephony {
51 constexpr int32_t INIT_INDEX = 0;
CallStatusManager()52 CallStatusManager::CallStatusManager()
53 {
54     (void)memset_s(&callReportInfo_, sizeof(CallDetailInfo), 0, sizeof(CallDetailInfo));
55     for (int32_t i = 0; i < SLOT_NUM; i++) {
56         (void)memset_s(&callDetailsInfo_[i], sizeof(CallDetailsInfo), 0, sizeof(CallDetailsInfo));
57     }
58 }
59 
~CallStatusManager()60 CallStatusManager::~CallStatusManager()
61 {
62     UnInit();
63 }
64 
Init()65 int32_t CallStatusManager::Init()
66 {
67     for (int32_t i = 0; i < SLOT_NUM; i++) {
68         callDetailsInfo_[i].callVec.clear();
69     }
70     for (int32_t i = 0; i < SLOT_NUM; i++) {
71         priorVideoState_[i] = VideoStateType::TYPE_VOICE;
72     }
73     mEventIdTransferMap_.clear();
74     mOttEventIdTransferMap_.clear();
75     InitCallBaseEvent();
76     CallIncomingFilterManagerPtr_ = (std::make_unique<CallIncomingFilterManager>()).release();
77     return TELEPHONY_SUCCESS;
78 }
79 
InitCallBaseEvent()80 void CallStatusManager::InitCallBaseEvent()
81 {
82     mEventIdTransferMap_[RequestResultEventId::RESULT_DIAL_NO_CARRIER] = CallAbilityEventId::EVENT_DIAL_NO_CARRIER;
83     mEventIdTransferMap_[RequestResultEventId::RESULT_HOLD_SEND_FAILED] = CallAbilityEventId::EVENT_HOLD_CALL_FAILED;
84     mEventIdTransferMap_[RequestResultEventId::RESULT_SWAP_SEND_FAILED] = CallAbilityEventId::EVENT_SWAP_CALL_FAILED;
85     mOttEventIdTransferMap_[OttCallEventId::OTT_CALL_EVENT_FUNCTION_UNSUPPORTED] =
86         CallAbilityEventId::EVENT_OTT_FUNCTION_UNSUPPORTED;
87     mEventIdTransferMap_[RequestResultEventId::RESULT_COMBINE_SEND_FAILED] =
88         CallAbilityEventId::EVENT_COMBINE_CALL_FAILED;
89     mEventIdTransferMap_[RequestResultEventId::RESULT_SPLIT_SEND_FAILED] =
90         CallAbilityEventId::EVENT_SPLIT_CALL_FAILED;
91 }
92 
UnInit()93 int32_t CallStatusManager::UnInit()
94 {
95     for (int32_t i = 0; i < SLOT_NUM; i++) {
96         callDetailsInfo_[i].callVec.clear();
97     }
98     mEventIdTransferMap_.clear();
99     mOttEventIdTransferMap_.clear();
100     return TELEPHONY_SUCCESS;
101 }
102 
HandleCallReportInfo(const CallDetailInfo & info)103 int32_t CallStatusManager::HandleCallReportInfo(const CallDetailInfo &info)
104 {
105     int32_t ret = TELEPHONY_ERR_FAIL;
106     callReportInfo_ = info;
107     if (info.callType == CallType::TYPE_VOIP) {
108         ret = HandleVoipCallReportInfo(info);
109         return ret;
110     }
111     switch (info.state) {
112         case TelCallState::CALL_STATUS_ACTIVE:
113             ret = ActiveHandle(info);
114             break;
115         case TelCallState::CALL_STATUS_HOLDING:
116             ret = HoldingHandle(info);
117             break;
118         case TelCallState::CALL_STATUS_DIALING: {
119             ret = DialingHandle(info);
120             FinishAsyncTrace(HITRACE_TAG_OHOS, "DialCall", getpid());
121             DelayedSingleton<CallManagerHisysevent>::GetInstance()->JudgingDialTimeOut(
122                 info.accountId, static_cast<int32_t>(info.callType), static_cast<int32_t>(info.callMode));
123             break;
124         }
125         case TelCallState::CALL_STATUS_ALERTING:
126             ret = AlertHandle(info);
127             break;
128         case TelCallState::CALL_STATUS_INCOMING: {
129             DelayedSingleton<CallControlManager>::GetInstance()->AcquireIncomingLock();
130             ret = IncomingHandle(info);
131             DelayedSingleton<CallControlManager>::GetInstance()->ReleaseIncomingLock();
132             FinishAsyncTrace(HITRACE_TAG_OHOS, "InComingCall", getpid());
133             DelayedSingleton<CallManagerHisysevent>::GetInstance()->JudgingIncomingTimeOut(
134                 info.accountId, static_cast<int32_t>(info.callType), static_cast<int32_t>(info.callMode));
135             break;
136         }
137         case TelCallState::CALL_STATUS_WAITING:
138             ret = WaitingHandle(info);
139             break;
140         case TelCallState::CALL_STATUS_DISCONNECTED:
141             ret = DisconnectedHandle(info);
142             break;
143         case TelCallState::CALL_STATUS_DISCONNECTING:
144             ret = DisconnectingHandle(info);
145             break;
146         default:
147             TELEPHONY_LOGE("Invalid call state!");
148             break;
149     }
150     TELEPHONY_LOGI("Entry CallStatusManager HandleCallReportInfo");
151     HandleDsdaInfo(info.accountId);
152     DelayedSingleton<BluetoothCallService>::GetInstance()->GetCallState();
153     TELEPHONY_LOGI("End CallStatusManager HandleCallReportInfo");
154     return ret;
155 }
156 
HandleDsdaInfo(int32_t slotId)157 void CallStatusManager::HandleDsdaInfo(int32_t slotId)
158 {
159     int32_t dsdsMode = DSDS_MODE_V2;
160     bool noOtherCall = true;
161     std::list<int32_t> callIdList;
162     GetCarrierCallList(callIdList);
163     int32_t currentCallNum = GetCurrentCallNum();
164     DelayedSingleton<CallRequestProcess>::GetInstance()->IsExistCallOtherSlot(callIdList, slotId, noOtherCall);
165     DelayedRefSingleton<CoreServiceClient>::GetInstance().GetDsdsMode(dsdsMode);
166     TELEPHONY_LOGI("dsdsMode:%{public}d", dsdsMode);
167     if ((dsdsMode == static_cast<int32_t>(DsdsMode::DSDS_MODE_V5_DSDA) ||
168             dsdsMode == static_cast<int32_t>(DsdsMode::DSDS_MODE_V5_TDM)) &&
169         !noOtherCall) {
170         TELEPHONY_LOGI("Handle DsdaCallInfo");
171         sptr<CallBase> holdCall = GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_HOLD);
172         if (holdCall != nullptr && currentCallNum > CALL_NUMBER) {
173             holdCall->SetCanUnHoldState(false);
174         }
175     }
176 }
177 
178 // handle call state changes, incoming call, outgoing call.
HandleCallsReportInfo(const CallDetailsInfo & info)179 int32_t CallStatusManager::HandleCallsReportInfo(const CallDetailsInfo &info)
180 {
181     bool flag = false;
182     TELEPHONY_LOGI("call list size:%{public}zu,slotId:%{public}d", info.callVec.size(), info.slotId);
183     int32_t curSlotId = info.slotId;
184     if (!DelayedSingleton<CallNumberUtils>::GetInstance()->IsValidSlotId(curSlotId)) {
185         TELEPHONY_LOGE("invalid slotId!");
186         return CALL_ERR_INVALID_SLOT_ID;
187     }
188     for (auto &it : info.callVec) {
189         for (const auto &it1 : callDetailsInfo_[curSlotId].callVec) {
190             if (it.index == it1.index) {
191                 // call state changes
192                 if (it.state != it1.state || it.mpty != it1.mpty || it.callType != it1.callType
193                     || it.callMode != it1.callMode || it.state == TelCallState::CALL_STATUS_ALERTING) {
194                     TELEPHONY_LOGI("handle updated call state:%{public}d", it.state);
195                     HandleCallReportInfo(it);
196                 }
197                 flag = true;
198                 break;
199             }
200         }
201         // incoming/outgoing call handle
202         if (!flag || callDetailsInfo_[curSlotId].callVec.empty()) {
203             TELEPHONY_LOGI("handle new call state:%{public}d", it.state);
204             HandleCallReportInfo(it);
205         }
206         flag = false;
207     }
208     // disconnected calls handle
209     for (auto &it2 : callDetailsInfo_[curSlotId].callVec) {
210         for (const auto &it3 : info.callVec) {
211             if (it2.index == it3.index) {
212                 TELEPHONY_LOGI("state:%{public}d", it2.state);
213                 flag = true;
214                 break;
215             }
216         }
217         if (!flag) {
218             it2.state = TelCallState::CALL_STATUS_DISCONNECTED;
219             HandleCallReportInfo(it2);
220         }
221         flag = false;
222     }
223     callDetailsInfo_[curSlotId].callVec.clear();
224     callDetailsInfo_[curSlotId] = info;
225     auto condition = [](CallDetailInfo i) { return i.state == TelCallState::CALL_STATUS_DISCONNECTED; };
226     auto it_end = std::remove_if(callDetailsInfo_[curSlotId].callVec.begin(),
227         callDetailsInfo_[curSlotId].callVec.end(), condition);
228     callDetailsInfo_[curSlotId].callVec.erase(it_end, callDetailsInfo_[curSlotId].callVec.end());
229     TELEPHONY_LOGI("End CallStatusManager HandleCallsReportInfo slotId:%{public}d, "
230         "callDetailsInfo_ size:%{public}zu", info.slotId, callDetailsInfo_[curSlotId].callVec.size());
231     return TELEPHONY_SUCCESS;
232 }
233 
HandleVoipCallReportInfo(const CallDetailInfo & info)234 int32_t CallStatusManager::HandleVoipCallReportInfo(const CallDetailInfo &info)
235 {
236     TELEPHONY_LOGI("Entry CallStatusManager HandleVoipCallReportInfo");
237     int32_t ret = TELEPHONY_ERR_FAIL;
238     switch (info.state) {
239         case TelCallState::CALL_STATUS_ACTIVE:
240             ret = ActiveVoipCallHandle(info);
241             break;
242         case TelCallState::CALL_STATUS_INCOMING: {
243             ret = IncomingVoipCallHandle(info);
244             break;
245         }
246         case TelCallState::CALL_STATUS_DISCONNECTED:
247             ret = DisconnectedVoipCallHandle(info);
248             break;
249         case TelCallState::CALL_STATUS_DIALING:
250             ret = OutgoingVoipCallHandle(info);
251             break;
252         case TelCallState::CALL_STATUS_ANSWERED:
253             ret = AnsweredVoipCallHandle(info);
254             break;
255         case TelCallState::CALL_STATUS_DISCONNECTING:
256             ret = DisconnectingVoipCallHandle(info);
257             break;
258         default:
259             TELEPHONY_LOGE("Invalid call state!");
260             break;
261     }
262     return ret;
263 }
264 
HandleDisconnectedCause(const DisconnectedDetails & details)265 int32_t CallStatusManager::HandleDisconnectedCause(const DisconnectedDetails &details)
266 {
267     bool ret = DelayedSingleton<CallControlManager>::GetInstance()->NotifyCallDestroyed(details);
268     if (!ret) {
269         TELEPHONY_LOGI("NotifyCallDestroyed failed!");
270         return CALL_ERR_PHONE_CALLSTATE_NOTIFY_FAILED;
271     }
272     return TELEPHONY_SUCCESS;
273 }
274 
HandleEventResultReportInfo(const CellularCallEventInfo & info)275 int32_t CallStatusManager::HandleEventResultReportInfo(const CellularCallEventInfo &info)
276 {
277     if (info.eventType != CellularCallEventType::EVENT_REQUEST_RESULT_TYPE) {
278         TELEPHONY_LOGE("unexpected type event occurs, eventId:%{public}d", info.eventId);
279         return CALL_ERR_PHONE_TYPE_UNEXPECTED;
280     }
281     TELEPHONY_LOGI("recv one Event, eventId:%{public}d", info.eventId);
282     sptr<CallBase> call = GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_DIALING);
283     if (call != nullptr) {
284         int32_t ret = DealFailDial(call);
285         TELEPHONY_LOGI("DealFailDial ret:%{public}d", ret);
286     }
287     CallEventInfo eventInfo;
288     (void)memset_s(&eventInfo, sizeof(CallEventInfo), 0, sizeof(CallEventInfo));
289     if (mEventIdTransferMap_.find(info.eventId) != mEventIdTransferMap_.end()) {
290         eventInfo.eventId = mEventIdTransferMap_[info.eventId];
291         DialParaInfo dialInfo;
292         if (eventInfo.eventId == CallAbilityEventId::EVENT_DIAL_NO_CARRIER) {
293             DelayedSingleton<CallControlManager>::GetInstance()->GetDialParaInfo(dialInfo);
294             if (dialInfo.number.length() > static_cast<size_t>(kMaxNumberLen)) {
295                 TELEPHONY_LOGE("Number out of limit!");
296                 return CALL_ERR_NUMBER_OUT_OF_RANGE;
297             }
298             if (memcpy_s(eventInfo.phoneNum, kMaxNumberLen, dialInfo.number.c_str(), dialInfo.number.length()) != EOK) {
299                 TELEPHONY_LOGE("memcpy_s failed!");
300                 return TELEPHONY_ERR_MEMCPY_FAIL;
301             }
302         } else if (eventInfo.eventId == CallAbilityEventId::EVENT_COMBINE_CALL_FAILED) {
303             sptr<CallBase> activeCall = GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_ACTIVE);
304             if (activeCall != nullptr) {
305                 activeCall->HandleCombineConferenceFailEvent();
306             }
307         } else if (eventInfo.eventId == CallAbilityEventId::EVENT_HOLD_CALL_FAILED) {
308             needWaitHold_ = false;
309         }
310         DelayedSingleton<CallControlManager>::GetInstance()->NotifyCallEventUpdated(eventInfo);
311     } else {
312         TELEPHONY_LOGW("unknown type Event, eventid %{public}d", info.eventId);
313     }
314     return TELEPHONY_SUCCESS;
315 }
316 
HandleOttEventReportInfo(const OttCallEventInfo & info)317 int32_t CallStatusManager::HandleOttEventReportInfo(const OttCallEventInfo &info)
318 {
319     TELEPHONY_LOGI("recv one Event, eventId:%{public}d", info.ottCallEventId);
320     CallEventInfo eventInfo;
321     (void)memset_s(&eventInfo, sizeof(CallEventInfo), 0, sizeof(CallEventInfo));
322     if (mOttEventIdTransferMap_.find(info.ottCallEventId) != mOttEventIdTransferMap_.end()) {
323         eventInfo.eventId = mOttEventIdTransferMap_[info.ottCallEventId];
324         if (strlen(info.bundleName) > static_cast<size_t>(kMaxNumberLen)) {
325             TELEPHONY_LOGE("Number out of limit!");
326             return CALL_ERR_NUMBER_OUT_OF_RANGE;
327         }
328         if (memcpy_s(eventInfo.bundleName, kMaxNumberLen, info.bundleName, strlen(info.bundleName)) != EOK) {
329             TELEPHONY_LOGE("memcpy_s failed!");
330             return TELEPHONY_ERR_MEMCPY_FAIL;
331         }
332         DelayedSingleton<CallControlManager>::GetInstance()->NotifyCallEventUpdated(eventInfo);
333     } else {
334         TELEPHONY_LOGW("unknown type Event, eventid %{public}d", info.ottCallEventId);
335     }
336     return TELEPHONY_SUCCESS;
337 }
338 
HandleVoipEventReportInfo(const VoipCallEventInfo & info)339 int32_t CallStatusManager::HandleVoipEventReportInfo(const VoipCallEventInfo &info)
340 {
341     TELEPHONY_LOGI("recv one Event, eventId:%{public}d", info.voipCallEvent);
342     sptr<CallBase> call = GetOneCallObjectByVoipCallId(info.voipCallId, info.bundleName, info.uid);
343     if (call == nullptr) {
344         TELEPHONY_LOGE("voip call is null");
345         return TELEPHONY_ERR_LOCAL_PTR_NULL;
346     }
347     if (call->GetCallRunningState() != CallRunningState::CALL_RUNNING_STATE_ACTIVE
348         && call->GetCallRunningState() != CallRunningState::CALL_RUNNING_STATE_DIALING) {
349         return TELEPHONY_ERR_FAIL;
350     }
351     if (info.voipCallEvent == VoipCallEvent::VOIP_CALL_EVENT_MUTED) {
352         call->SetMicPhoneState(true);
353     } else if (info.voipCallEvent == VoipCallEvent::VOIP_CALL_EVENT_UNMUTED) {
354         call->SetMicPhoneState(false);
355     }
356     DelayedSingleton<AudioDeviceManager>::GetInstance()->ReportAudioDeviceInfo(call);
357     return TELEPHONY_SUCCESS;
358 }
359 
IncomingHandle(const CallDetailInfo & info)360 int32_t CallStatusManager::IncomingHandle(const CallDetailInfo &info)
361 {
362     sptr<CallBase> call = GetOneCallObjectByIndexAndSlotId(info.index, info.accountId);
363     if (call != nullptr) {
364         auto oldCallType = call->GetCallType();
365         auto videoState = call->GetVideoStateType();
366         if (oldCallType != info.callType || call->GetTelCallState() != info.state || videoState != info.callMode) {
367             call = RefreshCallIfNecessary(call, info);
368             if (oldCallType != info.callType || videoState != info.callMode) {
369                 return UpdateCallState(call, info.state);
370             }
371         }
372         return TELEPHONY_SUCCESS;
373     }
374     int32_t ret = TELEPHONY_SUCCESS;
375     if (info.callType == CallType::TYPE_CS || info.callType == CallType::TYPE_IMS ||
376         info.callType == CallType::TYPE_SATELLITE) {
377         ret = IncomingFilterPolicy(info);
378         if (ret != TELEPHONY_SUCCESS) {
379             return ret;
380         }
381     }
382     call = CreateNewCall(info, CallDirection::CALL_DIRECTION_IN);
383     if (call == nullptr) {
384         TELEPHONY_LOGE("CreateNewCall failed!");
385         return CALL_ERR_CALL_OBJECT_IS_NULL;
386     }
387     SetContactInfo(call, std::string(info.phoneNum));
388     bool block = false;
389     if (IsRejectCall(call, info, block)) {
390         return HandleRejectCall(call, block);
391     }
392     if (info.callType != CallType::TYPE_VOIP && IsRingOnceCall(call, info)) {
393         return HandleRingOnceCall(call);
394     }
395     AddOneCallObject(call);
396     DelayedSingleton<CallControlManager>::GetInstance()->NotifyNewCallCreated(call);
397     CarrierAndVoipConflictProcess(call->GetCallID());
398     ret = UpdateCallState(call, info.state);
399     if (ret != TELEPHONY_SUCCESS) {
400         TELEPHONY_LOGE("UpdateCallState failed!");
401         return ret;
402     }
403     ret = FilterResultsDispose(call);
404     if (ret != TELEPHONY_SUCCESS) {
405         TELEPHONY_LOGE("FilterResultsDispose failed!");
406     }
407     return ret;
408 }
409 
CarrierAndVoipConflictProcess(int32_t callId)410 void CallStatusManager::CarrierAndVoipConflictProcess(int32_t callId)
411 {
412     DelayedSingleton<CallControlManager>::GetInstance()->CarrierAndVoipConflictProcess(callId,
413         TelCallState::CALL_STATUS_INCOMING);
414 }
415 
SetContactInfo(sptr<CallBase> & call,std::string phoneNum)416 void CallStatusManager::SetContactInfo(sptr<CallBase> &call, std::string phoneNum)
417 {
418     if (call == nullptr) {
419         TELEPHONY_LOGE("CreateVoipCall failed!");
420         return;
421     }
422     ffrt::submit([=, &call]() {
423         sptr<CallBase> callObjectPtr = call;
424         // allow list filtering
425         // Get the contact data from the database
426         ContactInfo contactInfo = {
427             .name = "",
428             .number = phoneNum,
429             .isContacterExists = false,
430             .ringtonePath = "",
431             .isSendToVoicemail = false,
432             .isEcc = false,
433             .isVoiceMail = false,
434             .isQueryComplete = true,
435         };
436         QueryCallerInfo(contactInfo, phoneNum);
437         callObjectPtr->SetCallerInfo(contactInfo);
438         CallVoiceAssistantManager::GetInstance()->UpdateContactInfo(contactInfo, callObjectPtr->GetCallID());
439     });
440 }
441 
HandleRejectCall(sptr<CallBase> & call,bool isBlock)442 int32_t CallStatusManager::HandleRejectCall(sptr<CallBase> &call, bool isBlock)
443 {
444     if (call == nullptr) {
445         TELEPHONY_LOGE("call is nullptr!");
446         return TELEPHONY_ERR_LOCAL_PTR_NULL;
447     }
448     int32_t ret = call->SetTelCallState(TelCallState::CALL_STATUS_INCOMING);
449     if (ret != TELEPHONY_SUCCESS && ret != CALL_ERR_NOT_NEW_STATE) {
450         TELEPHONY_LOGE("Set CallState failed!");
451         return ret;
452     }
453     ret = call->RejectCall();
454     if (ret != TELEPHONY_SUCCESS) {
455         TELEPHONY_LOGE("RejectCall failed!");
456         return ret;
457     }
458     if (isBlock) {
459         return DelayedSingleton<CallControlManager>::GetInstance()->AddBlockLogAndNotification(call);
460     }
461     return DelayedSingleton<CallControlManager>::GetInstance()->AddCallLogAndNotification(call);
462 }
463 
IncomingVoipCallHandle(const CallDetailInfo & info)464 int32_t CallStatusManager::IncomingVoipCallHandle(const CallDetailInfo &info)
465 {
466     int32_t ret = TELEPHONY_ERROR;
467     sptr<CallBase> call = GetOneCallObjectByVoipCallId(
468         info.voipCallInfo.voipCallId, info.voipCallInfo.voipBundleName, info.voipCallInfo.uid);
469     if (call != nullptr) {
470         return TELEPHONY_SUCCESS;
471     }
472     call = CreateNewCall(info, CallDirection::CALL_DIRECTION_IN);
473     if (call == nullptr) {
474         TELEPHONY_LOGE("CreateVoipCall failed!");
475         return CALL_ERR_CALL_OBJECT_IS_NULL;
476     }
477     AddOneCallObject(call);
478     DelayedSingleton<CallControlManager>::GetInstance()->NotifyNewCallCreated(call);
479     ret = UpdateCallState(call, info.state);
480     if (ret != TELEPHONY_SUCCESS) {
481         TELEPHONY_LOGE("UpdateCallState failed!");
482         return ret;
483     }
484     return ret;
485 }
486 
OutgoingVoipCallHandle(const CallDetailInfo & info)487 int32_t CallStatusManager::OutgoingVoipCallHandle(const CallDetailInfo &info)
488 {
489     int32_t ret = TELEPHONY_ERROR;
490     sptr<CallBase> call = GetOneCallObjectByVoipCallId(
491         info.voipCallInfo.voipCallId, info.voipCallInfo.voipBundleName, info.voipCallInfo.uid);
492     if (call != nullptr) {
493         VideoStateType originalType = call->GetVideoStateType();
494         if (originalType != info.callMode) {
495             TELEPHONY_LOGI("change VideoStateType from %{public}d to %{public}d",
496                 static_cast<int32_t>(originalType), static_cast<int32_t>(info.callMode));
497             call->SetVideoStateType(info.callMode);
498             return UpdateCallState(call, info.state);
499         }
500         return TELEPHONY_SUCCESS;
501     }
502     call = CreateNewCall(info, CallDirection::CALL_DIRECTION_OUT);
503     if (call == nullptr) {
504         TELEPHONY_LOGE("CreateVoipCall failed!");
505         return CALL_ERR_CALL_OBJECT_IS_NULL;
506     }
507     AddOneCallObject(call);
508     DelayedSingleton<CallControlManager>::GetInstance()->NotifyNewCallCreated(call);
509     ret = UpdateCallState(call, info.state);
510     if (ret != TELEPHONY_SUCCESS) {
511         TELEPHONY_LOGE("UpdateCallState failed!");
512         return ret;
513     }
514     return ret;
515 }
516 
AnsweredVoipCallHandle(const CallDetailInfo & info)517 int32_t CallStatusManager::AnsweredVoipCallHandle(const CallDetailInfo &info)
518 {
519     int32_t ret = TELEPHONY_ERROR;
520     sptr<CallBase> call = GetOneCallObjectByVoipCallId(
521         info.voipCallInfo.voipCallId, info.voipCallInfo.voipBundleName, info.voipCallInfo.uid);
522     if (call == nullptr) {
523         return ret;
524     }
525     if (DelayedSingleton<CallControlManager>::GetInstance()->NotifyCallStateUpdated(
526         call, TelCallState::CALL_STATUS_INCOMING, TelCallState::CALL_STATUS_ANSWERED)) {
527         return TELEPHONY_SUCCESS;
528     } else {
529         return ret;
530     }
531 }
532 
DisconnectingVoipCallHandle(const CallDetailInfo & info)533 int32_t CallStatusManager::DisconnectingVoipCallHandle(const CallDetailInfo &info)
534 {
535     sptr<CallBase> call = GetOneCallObjectByVoipCallId(
536         info.voipCallInfo.voipCallId, info.voipCallInfo.voipBundleName, info.voipCallInfo.uid);
537     if (call == nullptr) {
538         return TELEPHONY_ERROR;
539     }
540     return UpdateCallState(call, TelCallState::CALL_STATUS_DISCONNECTING);
541 }
542 
QueryCallerInfo(ContactInfo & contactInfo,std::string phoneNum)543 void CallStatusManager::QueryCallerInfo(ContactInfo &contactInfo, std::string phoneNum)
544 {
545     TELEPHONY_LOGI("Entry CallStatusManager QueryCallerInfo");
546     std::shared_ptr<CallDataBaseHelper> callDataPtr = DelayedSingleton<CallDataBaseHelper>::GetInstance();
547     if (callDataPtr == nullptr) {
548         TELEPHONY_LOGE("callDataPtr is nullptr!");
549         return;
550     }
551     DataShare::DataSharePredicates predicates;
552     predicates.EqualTo(DETAIL_INFO, phoneNum);
553     predicates.And();
554     predicates.EqualTo(CONTENT_TYPE, PHONE);
555     bool ret = callDataPtr->Query(contactInfo, predicates);
556     if (!ret) {
557         TELEPHONY_LOGE("Query contact database fail!");
558     }
559 }
560 
IncomingFilterPolicy(const CallDetailInfo & info)561 int32_t CallStatusManager::IncomingFilterPolicy(const CallDetailInfo &info)
562 {
563     if (CallIncomingFilterManagerPtr_ == nullptr) {
564         TELEPHONY_LOGE("CallIncomingFilterManagerPtr_ is null");
565         return TELEPHONY_ERR_LOCAL_PTR_NULL;
566     }
567     return CallIncomingFilterManagerPtr_->DoIncomingFilter(info);
568 }
569 
CallFilterCompleteResult(const CallDetailInfo & info)570 void CallStatusManager::CallFilterCompleteResult(const CallDetailInfo &info)
571 {
572     int32_t ret = TELEPHONY_ERR_FAIL;
573     sptr<CallBase> call = CreateNewCall(info, CallDirection::CALL_DIRECTION_IN);
574     if (call == nullptr) {
575         TELEPHONY_LOGE("CreateNewCall failed!");
576         return;
577     }
578     AddOneCallObject(call);
579 #ifdef ABILITY_DATABASE_SUPPORT
580     // allow list filtering
581     // Get the contact data from the database
582     GetCallerInfoDate(ContactInfo);
583     SetCallerInfo(contactInfo);
584 #endif
585     DelayedSingleton<CallControlManager>::GetInstance()->NotifyNewCallCreated(call);
586     ret = UpdateCallState(call, info.state);
587     if (ret != TELEPHONY_SUCCESS) {
588         TELEPHONY_LOGE("UpdateCallState failed!");
589         return;
590     }
591     ret = FilterResultsDispose(call);
592     if (ret != TELEPHONY_SUCCESS) {
593         TELEPHONY_LOGE("FilterResultsDispose failed!");
594         return;
595     }
596 }
597 
UpdateDialingCallInfo(const CallDetailInfo & info)598 int32_t CallStatusManager::UpdateDialingCallInfo(const CallDetailInfo &info)
599 {
600     sptr<CallBase> call = GetOneCallObjectByIndexAndSlotId(info.index, info.accountId);
601     if (call != nullptr) {
602         call = RefreshCallIfNecessary(call, info);
603         return TELEPHONY_SUCCESS;
604     }
605     call = GetOneCallObjectByIndex(INIT_INDEX);
606     if (call == nullptr) {
607         TELEPHONY_LOGE("call is nullptr");
608         return TELEPHONY_ERR_LOCAL_PTR_NULL;
609     }
610 
611     std::string oriNum = call->GetAccountNumber();
612     call = RefreshCallIfNecessary(call, info);
613     call->SetCallIndex(info.index);
614     call->SetBundleName(info.bundleName);
615     call->SetSlotId(info.accountId);
616     call->SetTelCallState(info.state);
617     call->SetVideoStateType(info.callMode);
618     call->SetCallType(info.callType);
619     call->SetAccountNumber(oriNum);
620     return TELEPHONY_SUCCESS;
621 }
622 
DialingHandle(const CallDetailInfo & info)623 int32_t CallStatusManager::DialingHandle(const CallDetailInfo &info)
624 {
625     TELEPHONY_LOGI("handle dialing state");
626     if (info.index > 0) {
627         sptr<CallBase> call = GetOneCallObjectByIndexAndSlotId(INIT_INDEX, info.accountId);
628         if (call == nullptr) {
629             call = GetOneCallObjectByIndexAndSlotId(info.index, info.accountId);
630         }
631         if (call != nullptr) {
632             TELEPHONY_LOGI("need update call info");
633             return UpdateDialingCallInfo(info);
634         }
635     }
636     sptr<CallBase> call = CreateNewCall(info, CallDirection::CALL_DIRECTION_OUT);
637     if (call == nullptr) {
638         TELEPHONY_LOGE("CreateNewCall failed!");
639         return TELEPHONY_ERR_LOCAL_PTR_NULL;
640     }
641     AddOneCallObject(call);
642     auto callRequestEventHandler = DelayedSingleton<CallRequestEventHandlerHelper>::GetInstance();
643     callRequestEventHandler->RestoreDialingFlag(false);
644     callRequestEventHandler->RemoveEventHandlerTask();
645     int32_t ret = call->DialingProcess();
646     if (ret != TELEPHONY_SUCCESS) {
647         return ret;
648     }
649     DelayedSingleton<CallControlManager>::GetInstance()->NotifyNewCallCreated(call);
650     ret = UpdateCallState(call, TelCallState::CALL_STATUS_DIALING);
651     if (ret != TELEPHONY_SUCCESS) {
652         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
653     }
654     return ret;
655 }
656 
ActiveHandle(const CallDetailInfo & info)657 int32_t CallStatusManager::ActiveHandle(const CallDetailInfo &info)
658 {
659     TELEPHONY_LOGI("handle active state");
660     std::string tmpStr(info.phoneNum);
661     sptr<CallBase> call = GetOneCallObjectByIndexAndSlotId(info.index, info.accountId);
662     if (call == nullptr) {
663         TELEPHONY_LOGE("Call is NULL");
664         return TELEPHONY_ERR_LOCAL_PTR_NULL;
665     }
666     call = RefreshCallIfNecessary(call, info);
667     SetOriginalCallTypeForActiveState(call);
668     // call state change active, need to judge if launching a conference
669     if (info.mpty == 1) {
670         int32_t mainCallId = ERR_ID;
671         call->LaunchConference();
672         call->GetMainCallId(mainCallId);
673         sptr<CallBase> mainCall = GetOneCallObject(mainCallId);
674         if (mainCall != nullptr) {
675             mainCall->SetTelConferenceState(TelConferenceState::TEL_CONFERENCE_ACTIVE);
676         }
677     } else if (call->ExitConference() == TELEPHONY_SUCCESS) {
678         TELEPHONY_LOGI("SubCallSeparateFromConference success!");
679     } else {
680         TELEPHONY_LOGI("SubCallSeparateFromConference fail!");
681     }
682     int32_t ret = UpdateCallState(call, TelCallState::CALL_STATUS_ACTIVE);
683     if (ret != TELEPHONY_SUCCESS) {
684         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
685         return ret;
686     }
687     sptr<CallBase> holdCall = GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_HOLD);
688     if (holdCall != nullptr) {
689         holdCall->SetCanSwitchCallState(true);
690         TELEPHONY_LOGI("holdcall:%{public}d can swap", holdCall->GetCallID());
691     }
692 #ifdef AUDIO_SUPPORT
693     ToSpeakerPhone(call);
694     DelayedSingleton<AudioControlManager>::GetInstance()->SetVolumeAudible();
695 #endif
696     TELEPHONY_LOGI("handle active state success");
697     return ret;
698 }
699 
ActiveVoipCallHandle(const CallDetailInfo & info)700 int32_t CallStatusManager::ActiveVoipCallHandle(const CallDetailInfo &info)
701 {
702     TELEPHONY_LOGI("handle active state");
703     sptr<CallBase> call = GetOneCallObjectByVoipCallId(
704         info.voipCallInfo.voipCallId, info.voipCallInfo.voipBundleName, info.voipCallInfo.uid);
705     if (call == nullptr) {
706         TELEPHONY_LOGE("voip Call is NULL");
707         return TELEPHONY_ERR_LOCAL_PTR_NULL;
708     }
709     VideoStateType originalType = call->GetVideoStateType();
710     if (originalType != info.callMode) {
711         TELEPHONY_LOGI("change VideoStateType from %{public}d to %{public}d",
712             static_cast<int32_t>(originalType), static_cast<int32_t>(info.callMode));
713         call->SetVideoStateType(info.callMode);
714     }
715     int32_t ret = UpdateCallState(call, TelCallState::CALL_STATUS_ACTIVE);
716     if (ret != TELEPHONY_SUCCESS) {
717         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
718         return ret;
719     }
720     TELEPHONY_LOGI("handle active state success");
721     return ret;
722 }
723 
HoldingHandle(const CallDetailInfo & info)724 int32_t CallStatusManager::HoldingHandle(const CallDetailInfo &info)
725 {
726     TELEPHONY_LOGI("handle holding state");
727     std::string tmpStr(info.phoneNum);
728     sptr<CallBase> call = GetOneCallObjectByIndexAndSlotId(info.index, info.accountId);
729     if (call == nullptr) {
730         TELEPHONY_LOGE("Call is NULL");
731         return TELEPHONY_ERR_LOCAL_PTR_NULL;
732     }
733     // if the call is in a conference, it will exit, otherwise just set it holding
734     call = RefreshCallIfNecessary(call, info);
735     if (info.mpty == 1) {
736         int32_t ret = call->HoldConference();
737         if (ret == TELEPHONY_SUCCESS) {
738             TELEPHONY_LOGI("HoldConference success");
739         }
740     }
741     TelCallState priorState = call->GetTelCallState();
742     int32_t ret = UpdateCallState(call, TelCallState::CALL_STATUS_HOLDING);
743     if (ret != TELEPHONY_SUCCESS) {
744         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
745     }
746     int32_t callId = call->GetCallID();
747     int32_t dsdsMode = DSDS_MODE_V2;
748     DelayedRefSingleton<CoreServiceClient>::GetInstance().GetDsdsMode(dsdsMode);
749     TELEPHONY_LOGE("HoldingHandle dsdsMode:%{public}d", dsdsMode);
750     bool canSwitchCallState = call->GetCanSwitchCallState();
751     if (dsdsMode == static_cast<int32_t>(DsdsMode::DSDS_MODE_V5_DSDA) ||
752         dsdsMode == static_cast<int32_t>(DsdsMode::DSDS_MODE_V5_TDM)) {
753         int32_t activeCallNum = GetCallNum(TelCallState::CALL_STATUS_ACTIVE);
754         if (needWaitHold_ && activeCallNum == 0) {
755             needWaitHold_ = false;
756             int32_t result = DelayedSingleton<CellularCallConnection>::GetInstance()->Dial(GetDialCallInfo());
757             sptr<CallBase> dialCall = GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_DIALING);
758             if (result != TELEPHONY_SUCCESS && dialCall != nullptr) {
759                 DealFailDial(call);
760                 TELEPHONY_LOGI("Dial call fail");
761             }
762         } else {
763             TelConferenceState confState = call->GetTelConferenceState();
764             int32_t conferenceId = ERR_ID;
765             call->GetMainCallId(conferenceId);
766             if (confState != TelConferenceState::TEL_CONFERENCE_IDLE && conferenceId == callId) {
767                 AutoHandleForDsda(canSwitchCallState, priorState, activeCallNum, call->GetSlotId(), false);
768             } else if (confState == TelConferenceState::TEL_CONFERENCE_IDLE) {
769                 AutoHandleForDsda(canSwitchCallState, priorState, activeCallNum, call->GetSlotId(), false);
770             }
771         }
772     }
773     return ret;
774 }
775 
WaitingHandle(const CallDetailInfo & info)776 int32_t CallStatusManager::WaitingHandle(const CallDetailInfo &info)
777 {
778     DelayedSingleton<CallControlManager>::GetInstance()->AcquireIncomingLock();
779     int32_t ret = IncomingHandle(info);
780     DelayedSingleton<CallControlManager>::GetInstance()->ReleaseIncomingLock();
781     return ret;
782 }
783 
AlertHandle(const CallDetailInfo & info)784 int32_t CallStatusManager::AlertHandle(const CallDetailInfo &info)
785 {
786     TELEPHONY_LOGI("handle alerting state");
787     std::string tmpStr(info.phoneNum);
788     sptr<CallBase> call = GetOneCallObjectByIndexAndSlotId(info.index, info.accountId);
789     if (call == nullptr) {
790         TELEPHONY_LOGE("Call is NULL");
791         return TELEPHONY_ERR_LOCAL_PTR_NULL;
792     }
793     call = RefreshCallIfNecessary(call, info);
794     int32_t ret = UpdateCallState(call, TelCallState::CALL_STATUS_ALERTING);
795     if (ret != TELEPHONY_SUCCESS) {
796         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
797         return ret;
798     }
799 #ifdef AUDIO_SUPPORT
800     ToSpeakerPhone(call);
801     TurnOffMute(call);
802     DelayedSingleton<AudioControlManager>::GetInstance()->SetVolumeAudible();
803 #endif
804     return ret;
805 }
806 
DisconnectingHandle(const CallDetailInfo & info)807 int32_t CallStatusManager::DisconnectingHandle(const CallDetailInfo &info)
808 {
809     TELEPHONY_LOGI("handle disconnecting state");
810     std::string tmpStr(info.phoneNum);
811     sptr<CallBase> call = GetOneCallObjectByIndexAndSlotId(info.index, info.accountId);
812     if (call == nullptr) {
813         TELEPHONY_LOGE("Call is NULL");
814         return TELEPHONY_ERR_LOCAL_PTR_NULL;
815     }
816     call = RefreshCallIfNecessary(call, info);
817     SetOriginalCallTypeForDisconnectState(call);
818     int32_t ret = UpdateCallState(call, TelCallState::CALL_STATUS_DISCONNECTING);
819     if (ret != TELEPHONY_SUCCESS) {
820         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
821     }
822     return ret;
823 }
824 
DisconnectedVoipCallHandle(const CallDetailInfo & info)825 int32_t CallStatusManager::DisconnectedVoipCallHandle(const CallDetailInfo &info)
826 {
827     TELEPHONY_LOGI("handle disconnected voip call state");
828     sptr<CallBase> call = GetOneCallObjectByVoipCallId(
829         info.voipCallInfo.voipCallId, info.voipCallInfo.voipBundleName, info.voipCallInfo.uid);
830     if (call == nullptr) {
831         TELEPHONY_LOGE("voip Call is NULL");
832         return TELEPHONY_ERR_LOCAL_PTR_NULL;
833     }
834     int32_t ret = UpdateCallState(call, TelCallState::CALL_STATUS_DISCONNECTED);
835     if (ret != TELEPHONY_SUCCESS) {
836         TELEPHONY_LOGE("UpdateCallState failed, errCode:%{public}d", ret);
837         return ret;
838     }
839     DeleteOneCallObject(call->GetCallID());
840     TELEPHONY_LOGI("handle disconnected voip call state success");
841     return ret;
842 }
843 
DisconnectedHandle(const CallDetailInfo & info)844 int32_t CallStatusManager::DisconnectedHandle(const CallDetailInfo &info)
845 {
846     TELEPHONY_LOGI("handle disconnected state");
847     if (timeWaitHelper_ !=  nullptr) {
848         TELEPHONY_LOGI("ringtone once");
849         timeWaitHelper_->NotifyAll();
850         timeWaitHelper_ = nullptr;
851     }
852     std::string tmpStr(info.phoneNum);
853     sptr<CallBase> call = GetOneCallObjectByIndexAndSlotId(info.index, info.accountId);
854     if (call == nullptr) {
855         TELEPHONY_LOGE("call is null");
856         return TELEPHONY_ERR_LOCAL_PTR_NULL;
857     }
858     call = RefreshCallIfNecessary(call, info);
859     SetOriginalCallTypeForDisconnectState(call);
860     std::vector<std::u16string> callIdList;
861     call->GetSubCallIdList(callIdList);
862     CallRunningState previousState = call->GetCallRunningState();
863     call->ExitConference();
864     TelCallState priorState = call->GetTelCallState();
865     UpdateCallState(call, TelCallState::CALL_STATUS_DISCONNECTED);
866     HandleHoldCallOrAutoAnswerCall(call, callIdList, previousState, priorState);
867     int32_t currentCallNum = CallObjectManager::GetCurrentCallNum();
868     if (currentCallNum <= 0) {
869         DelayedSingleton<CallSuperPrivacyControlManager>::GetInstance()->RestoreSuperPrivacyMode();
870     }
871     return TELEPHONY_SUCCESS;
872 }
873 
HandleHoldCallOrAutoAnswerCall(const sptr<CallBase> call,std::vector<std::u16string> callIdList,CallRunningState previousState,TelCallState priorState)874 void CallStatusManager::HandleHoldCallOrAutoAnswerCall(const sptr<CallBase> call,
875     std::vector<std::u16string> callIdList, CallRunningState previousState, TelCallState priorState)
876 {
877     if (call == nullptr) {
878         TELEPHONY_LOGE("call is null");
879         return;
880     }
881     bool canUnHold = false;
882     size_t size = callIdList.size();
883     int32_t activeCallNum = GetCallNum(TelCallState::CALL_STATUS_ACTIVE);
884     int32_t waitingCallNum = GetCallNum(TelCallState::CALL_STATUS_WAITING);
885     IsCanUnHold(activeCallNum, waitingCallNum, size, canUnHold);
886     sptr<CallBase> holdCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_HOLD);
887     if (previousState != CallRunningState::CALL_RUNNING_STATE_HOLD &&
888         previousState != CallRunningState::CALL_RUNNING_STATE_ACTIVE &&
889         priorState == TelCallState::CALL_STATUS_DISCONNECTING) {
890         if (holdCall != nullptr && canUnHold && holdCall->GetCanUnHoldState()) {
891             if (holdCall->GetSlotId() == call->GetSlotId()) {
892                 TELEPHONY_LOGI("release call and recover the held call");
893                 holdCall->UnHoldCall();
894             }
895         }
896     }
897     DeleteOneCallObject(call->GetCallID());
898     int32_t dsdsMode = DSDS_MODE_V2;
899     DelayedRefSingleton<CoreServiceClient>::GetInstance().GetDsdsMode(dsdsMode);
900     if (dsdsMode == DSDS_MODE_V3) {
901         AutoAnswer(activeCallNum, waitingCallNum);
902     } else if (dsdsMode == static_cast<int32_t>(DsdsMode::DSDS_MODE_V5_DSDA) ||
903         dsdsMode == static_cast<int32_t>(DsdsMode::DSDS_MODE_V5_TDM)) {
904         bool canSwitchCallState = call->GetCanSwitchCallState();
905         AutoHandleForDsda(canSwitchCallState, priorState, activeCallNum, call->GetSlotId(), true);
906     }
907 }
908 
IsCanUnHold(int32_t activeCallNum,int32_t waitingCallNum,int32_t size,bool & canUnHold)909 void CallStatusManager::IsCanUnHold(int32_t activeCallNum, int32_t waitingCallNum, int32_t size, bool &canUnHold)
910 {
911     int32_t incomingCallNum = GetCallNum(TelCallState::CALL_STATUS_INCOMING);
912     int32_t answeredCallNum = GetCallNum(TelCallState::CALL_STATUS_ANSWERED);
913     int32_t dialingCallNum = GetCallNum(TelCallState::CALL_STATUS_ALERTING);
914     if (answeredCallNum == 0 && incomingCallNum == 0 && (size == 0 || size == 1) && activeCallNum == 0 &&
915         waitingCallNum == 0 && dialingCallNum == 0) {
916         canUnHold = true;
917     }
918     TELEPHONY_LOGI("CanUnHold state: %{public}d", canUnHold);
919 }
920 
AutoHandleForDsda(bool canSwitchCallState,TelCallState priorState,int32_t activeCallNum,int32_t slotId,bool continueAnswer)921 void CallStatusManager::AutoHandleForDsda(
922     bool canSwitchCallState, TelCallState priorState, int32_t activeCallNum, int32_t slotId, bool continueAnswer)
923 {
924     int32_t dialingCallNum = GetCallNum(TelCallState::CALL_STATUS_DIALING);
925     int32_t alertingCallNum = GetCallNum(TelCallState::CALL_STATUS_ALERTING);
926     std::list<int32_t> callIdList;
927     GetCarrierCallList(callIdList);
928     for (int32_t ringCallId : callIdList) {
929         sptr<CallBase> ringCall = GetOneCallObject(ringCallId);
930         if (ringCall != nullptr && ringCall->GetAutoAnswerState()) {
931             TELEPHONY_LOGI("ringCall is not nullptr");
932             int32_t videoState = static_cast<int32_t>(ringCall->GetVideoStateType());
933             if (videoState == static_cast<int32_t>(VideoStateType::TYPE_VIDEO)) {
934                 TELEPHONY_LOGI("AutoAnswer VideoCall for Dsda");
935                 AutoAnswerForVideoCall(activeCallNum);
936                 AutoUnHoldForDsda(canSwitchCallState, priorState, activeCallNum, slotId);
937                 return;
938             }
939             if (dialingCallNum == 0 && alertingCallNum == 0 && activeCallNum == 0 &&
940                 ringCall->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
941                 TELEPHONY_LOGI("AutoAnswer VoiceCall for Dsda");
942                 AutoAnswerForVoiceCall(ringCall, slotId, continueAnswer);
943                 AutoUnHoldForDsda(canSwitchCallState, priorState, activeCallNum, slotId);
944                 return;
945             }
946         }
947     }
948     AutoUnHoldForDsda(canSwitchCallState, priorState, activeCallNum, slotId);
949 }
950 
AutoUnHoldForDsda(bool canSwitchCallState,TelCallState priorState,int32_t activeCallNum,int32_t slotId)951 void CallStatusManager::AutoUnHoldForDsda(
952     bool canSwitchCallState, TelCallState priorState, int32_t activeCallNum, int32_t slotId)
953 {
954     int32_t dialingCallNum = GetCallNum(TelCallState::CALL_STATUS_DIALING);
955     int32_t waitingCallNum = GetCallNum(TelCallState::CALL_STATUS_WAITING);
956     int32_t callNum = 2;
957     std::list<int32_t> callIdList;
958     GetCarrierCallList(callIdList);
959     int32_t currentCallNum = GetCurrentCallNum();
960     for (int32_t otherCallId : callIdList) {
961         sptr<CallBase> otherCall = GetOneCallObject(otherCallId);
962         if (otherCall == nullptr) {
963             TELEPHONY_LOGE("otherCall is nullptr");
964             continue;
965         }
966         TelCallState state = otherCall->GetTelCallState();
967         TelConferenceState confState = otherCall->GetTelConferenceState();
968         int32_t conferenceId = ERR_ID;
969         otherCall->GetMainCallId(conferenceId);
970         if (slotId != otherCall->GetSlotId() && state == TelCallState::CALL_STATUS_HOLDING &&
971             otherCall->GetCanUnHoldState() && activeCallNum == 0 && waitingCallNum == 0 &&
972             dialingCallNum == 0 &&
973             ((confState != TelConferenceState::TEL_CONFERENCE_IDLE && conferenceId == otherCallId) ||
974                 confState == TelConferenceState::TEL_CONFERENCE_IDLE)) {
975             // Actively hang up the processing unhold state or exchange call
976             if (priorState == TelCallState::CALL_STATUS_DISCONNECTING ||
977                 (!canSwitchCallState && currentCallNum == callNum)) {
978                 otherCall->UnHoldCall();
979                 return;
980             }
981         }
982     }
983     for (int32_t otherCallId : callIdList) {
984         sptr<CallBase> holdCall = GetOneCallObject(otherCallId);
985         if (holdCall != nullptr && holdCall->GetTelCallState() == TelCallState::CALL_STATUS_HOLDING) {
986             if (currentCallNum == callNum) {
987                 holdCall->SetCanUnHoldState(true);
988             }
989         }
990     }
991 }
992 
AutoAnswerForVoiceCall(sptr<CallBase> ringCall,int32_t slotId,bool continueAnswer)993 void CallStatusManager::AutoAnswerForVoiceCall(sptr<CallBase> ringCall, int32_t slotId, bool continueAnswer)
994 {
995     /* Need to check whether the autoAnswer call and the holding call are on the same slotid
996      * To prevent repeated AT command delivery.
997      */
998     if (continueAnswer || slotId != ringCall->GetSlotId()) {
999         DelayedSingleton<CallControlManager>::GetInstance()->NotifyCallStateUpdated(
1000             ringCall, TelCallState::CALL_STATUS_INCOMING, TelCallState::CALL_STATUS_ANSWERED);
1001         int ret = ringCall->AnswerCall(ringCall->GetAnswerVideoState());
1002         if (ret == TELEPHONY_SUCCESS) {
1003             DelayedSingleton<CallControlManager>::GetInstance()->NotifyIncomingCallAnswered(ringCall);
1004         }
1005         TELEPHONY_LOGI("ret = %{public}d", ret);
1006     }
1007     ringCall->SetAutoAnswerState(false);
1008 }
1009 
AutoAnswerForVideoCall(int32_t activeCallNum)1010 void CallStatusManager::AutoAnswerForVideoCall(int32_t activeCallNum)
1011 {
1012     int32_t holdingCallNum = GetCallNum(TelCallState::CALL_STATUS_HOLDING);
1013     int32_t dialingCallNum = GetCallNum(TelCallState::CALL_STATUS_DIALING);
1014     int32_t alertingCallNum = GetCallNum(TelCallState::CALL_STATUS_ALERTING);
1015     if (activeCallNum == 0 && holdingCallNum == 0 && dialingCallNum == 0 && alertingCallNum == 0) {
1016         std::list<int32_t> ringCallIdList;
1017         GetCarrierCallList(ringCallIdList);
1018         for (int32_t ringingCallId : ringCallIdList) {
1019             sptr<CallBase> ringingCall = GetOneCallObject(ringingCallId);
1020             if (ringingCall == nullptr) {
1021                 TELEPHONY_LOGE("ringingCall is nullptr");
1022                 return;
1023             }
1024             CallRunningState ringingCallState = ringingCall->GetCallRunningState();
1025             if ((ringingCallState == CallRunningState::CALL_RUNNING_STATE_RINGING &&
1026                     (ringingCall->GetAutoAnswerState()))) {
1027                 ringingCall->SetAutoAnswerState(false);
1028                 int ret = ringingCall->AnswerCall(ringingCall->GetAnswerVideoState());
1029                 TELEPHONY_LOGI("ret = %{public}d", ret);
1030                 break;
1031             }
1032         }
1033     }
1034 }
1035 
AutoAnswer(int32_t activeCallNum,int32_t waitingCallNum)1036 void CallStatusManager::AutoAnswer(int32_t activeCallNum, int32_t waitingCallNum)
1037 {
1038     int32_t holdingCallNum = GetCallNum(TelCallState::CALL_STATUS_HOLDING);
1039     int32_t dialingCallNum = GetCallNum(TelCallState::CALL_STATUS_DIALING);
1040     int32_t alertingCallNum = GetCallNum(TelCallState::CALL_STATUS_ALERTING);
1041     if (activeCallNum == 0 && waitingCallNum == 0 && holdingCallNum == 0 && dialingCallNum == 0 &&
1042         alertingCallNum == 0) {
1043         std::list<int32_t> ringCallIdList;
1044         GetCarrierCallList(ringCallIdList);
1045         for (int32_t ringingCallId : ringCallIdList) {
1046             sptr<CallBase> ringingCall = GetOneCallObject(ringingCallId);
1047             if (ringingCall == nullptr) {
1048                 TELEPHONY_LOGE("ringingCall is nullptr");
1049                 return;
1050             }
1051             CallRunningState ringingCallState = ringingCall->GetCallRunningState();
1052             if ((ringingCallState == CallRunningState::CALL_RUNNING_STATE_RINGING &&
1053                     (ringingCall->GetAutoAnswerState()))) {
1054                 ringingCall->SetAutoAnswerState(false);
1055                 int ret = ringingCall->AnswerCall(ringingCall->GetAnswerVideoState());
1056                 TELEPHONY_LOGI("ret = %{public}d", ret);
1057                 break;
1058             }
1059         }
1060     }
1061 }
1062 
UpdateCallState(sptr<CallBase> & call,TelCallState nextState)1063 int32_t CallStatusManager::UpdateCallState(sptr<CallBase> &call, TelCallState nextState)
1064 {
1065     TELEPHONY_LOGI("UpdateCallState start");
1066     if (call == nullptr) {
1067         TELEPHONY_LOGE("Call is NULL");
1068         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1069     }
1070     TelCallState priorState = call->GetTelCallState();
1071     VideoStateType videoState = call->GetVideoStateType();
1072     TELEPHONY_LOGI(
1073         "callIndex:%{public}d, callId:%{public}d, priorState:%{public}d, nextState:%{public}d, videoState:%{public}d",
1074         call->GetCallIndex(), call->GetCallID(), priorState, nextState, videoState);
1075     if (call->GetCallType() == CallType::TYPE_SATELLITE) {
1076         DelayedSingleton<SatelliteCallControl>::GetInstance()->
1077             HandleSatelliteCallStateUpdate(call, priorState, nextState);
1078     }
1079     if (priorState == TelCallState::CALL_STATUS_INCOMING && nextState == TelCallState::CALL_STATUS_ACTIVE) {
1080         DelayedSingleton<CallManagerHisysevent>::GetInstance()->JudgingAnswerTimeOut(
1081             call->GetSlotId(), call->GetCallID(), static_cast<int32_t>(call->GetVideoStateType()));
1082     }
1083     int32_t ret = call->SetTelCallState(nextState);
1084     UpdateOneCallObjectByCallId(call->GetCallID(), nextState);
1085     if (ret != TELEPHONY_SUCCESS && ret != CALL_ERR_NOT_NEW_STATE) {
1086         TELEPHONY_LOGE("SetTelCallState failed");
1087         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1088     }
1089     if (!DelayedSingleton<CallControlManager>::GetInstance()->NotifyCallStateUpdated(call, priorState, nextState)) {
1090         TELEPHONY_LOGE(
1091             "NotifyCallStateUpdated failed! priorState:%{public}d,nextState:%{public}d", priorState, nextState);
1092         if (nextState == TelCallState::CALL_STATUS_INCOMING) {
1093             CallManagerHisysevent::WriteIncomingCallFaultEvent(call->GetSlotId(),
1094                 static_cast<int32_t>(call->GetCallType()), static_cast<int32_t>(call->GetVideoStateType()), ret,
1095                 "NotifyCallStateUpdated failed");
1096         }
1097         return CALL_ERR_PHONE_CALLSTATE_NOTIFY_FAILED;
1098     }
1099     SetVideoCallState(call, nextState);
1100     return TELEPHONY_SUCCESS;
1101 }
1102 
SetVideoCallState(sptr<CallBase> & call,TelCallState nextState)1103 void CallStatusManager::SetVideoCallState(sptr<CallBase> &call, TelCallState nextState)
1104 {
1105     if (call == nullptr) {
1106         TELEPHONY_LOGE("Call is NULL");
1107         return;
1108     }
1109     int slotId = call->GetSlotId();
1110     bool isSlotIdValid = false;
1111     if (slotId < SLOT_NUM && slotId >= 0) {
1112         isSlotIdValid = true;
1113     }
1114     VideoStateType videoState = call->GetVideoStateType();
1115     TELEPHONY_LOGI("nextVideoState:%{public}d, priorVideoState:%{public}d, isSlotIdValid:%{public}d", videoState,
1116         priorVideoState_[slotId], isSlotIdValid);
1117     if (isSlotIdValid && (priorVideoState_[slotId] != videoState)) {
1118         DelayedSingleton<AudioControlManager>::GetInstance()->VideoStateUpdated(
1119             call, priorVideoState_[slotId], videoState);
1120         priorVideoState_[slotId] = videoState;
1121     }
1122     if (isSlotIdValid && (nextState == TelCallState::CALL_STATUS_DISCONNECTED)) {
1123         priorVideoState_[slotId] = VideoStateType::TYPE_VOICE;
1124     }
1125 }
1126 
RefreshCallIfNecessary(const sptr<CallBase> & call,const CallDetailInfo & info)1127 sptr<CallBase> CallStatusManager::RefreshCallIfNecessary(const sptr<CallBase> &call, const CallDetailInfo &info)
1128 {
1129     TELEPHONY_LOGI("RefreshCallIfNecessary");
1130     if (call->GetCallType() == CallType::TYPE_IMS && call->GetVideoStateType() != info.callMode) {
1131         call->SetVideoStateType(info.callMode);
1132         sptr<IMSCall> imsCall = reinterpret_cast<IMSCall *>(call.GetRefPtr());
1133         imsCall->InitVideoCall();
1134     }
1135     if (call->GetCallType() == CallType::TYPE_IMS) {
1136         call->SetCrsType(info.crsType);
1137     }
1138     if (call->GetCallType() == info.callType) {
1139         TELEPHONY_LOGI("RefreshCallIfNecessary not need Refresh");
1140         return call;
1141     }
1142     TelCallState priorState = call->GetTelCallState();
1143     CallAttributeInfo attrInfo;
1144     (void)memset_s(&attrInfo, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
1145     call->GetCallAttributeBaseInfo(attrInfo);
1146     sptr<CallBase> newCall = CreateNewCall(info, attrInfo.callDirection);
1147     if (newCall == nullptr) {
1148         TELEPHONY_LOGE("RefreshCallIfNecessary createCallFail");
1149         return call;
1150     }
1151     AddOneCallObject(newCall);
1152     newCall->SetCallRunningState(call->GetCallRunningState());
1153     newCall->SetTelConferenceState(call->GetTelConferenceState());
1154     newCall->SetStartTime(attrInfo.startTime);
1155     newCall->SetPolicyFlag(PolicyFlag(call->GetPolicyFlag()));
1156     newCall->SetSpeakerphoneOn(call->IsSpeakerphoneOn());
1157     newCall->SetCallEndedType(call->GetCallEndedType());
1158     newCall->SetCallBeginTime(attrInfo.callBeginTime);
1159     newCall->SetCallCreateTime(attrInfo.callCreateTime);
1160     newCall->SetCallEndTime(attrInfo.callEndTime);
1161     newCall->SetRingBeginTime(attrInfo.ringBeginTime);
1162     newCall->SetRingEndTime(attrInfo.ringEndTime);
1163     newCall->SetAnswerType(attrInfo.answerType);
1164     newCall->SetMicPhoneState(call->IsMuted());
1165     DeleteOneCallObject(call->GetCallID());
1166     newCall->SetCallId(call->GetCallID());
1167     newCall->SetTelCallState(priorState);
1168     if (call->GetNumberLocation() != "default") {
1169         newCall->SetNumberLocation(call->GetNumberLocation());
1170     }
1171     NumberMarkInfo numberMarkInfo = call->GetNumberMarkInfo();
1172     if (numberMarkInfo.markType != MarkType::MARK_TYPE_NONE) {
1173         newCall->SetNumberMarkInfo(numberMarkInfo);
1174     }
1175     return newCall;
1176 }
1177 
SetOriginalCallTypeForActiveState(sptr<CallBase> & call)1178 void CallStatusManager::SetOriginalCallTypeForActiveState(sptr<CallBase> &call)
1179 {
1180     if (call == nullptr) {
1181         TELEPHONY_LOGE("Call is NULL");
1182         return;
1183     }
1184     TelCallState priorState = call->GetTelCallState();
1185     VideoStateType videoState = call->GetVideoStateType();
1186     int32_t videoStateHistory = call->GetOriginalCallType();
1187     if (priorState == TelCallState::CALL_STATUS_ALERTING || priorState == TelCallState::CALL_STATUS_INCOMING ||
1188         priorState == TelCallState::CALL_STATUS_WAITING) {
1189         // outgoing/incoming video call, but accepted/answered with voice call
1190         if (videoStateHistory != static_cast<int32_t>(videoState)) {
1191             TELEPHONY_LOGD("set videoState:%{public}d as original call type", static_cast<int32_t>(videoState));
1192             call->SetOriginalCallType(static_cast<int32_t>(videoState));
1193         }
1194     } else if (priorState == TelCallState::CALL_STATUS_ACTIVE || priorState == TelCallState::CALL_STATUS_HOLDING) {
1195         int32_t videoStateCurrent =
1196             static_cast<int32_t>(static_cast<uint32_t>(videoStateHistory) | static_cast<uint32_t>(videoState));
1197         TELEPHONY_LOGD("maybe upgrade/downgrade operation, keep video record always, videoStateCurrent:%{public}d",
1198             videoStateCurrent);
1199         call->SetOriginalCallType(videoStateCurrent);
1200     }
1201 }
1202 
SetOriginalCallTypeForDisconnectState(sptr<CallBase> & call)1203 void CallStatusManager::SetOriginalCallTypeForDisconnectState(sptr<CallBase> &call)
1204 {
1205     if (call == nullptr) {
1206         TELEPHONY_LOGE("Call is NULL");
1207         return;
1208     }
1209     TelCallState priorState = call->GetTelCallState();
1210     CallAttributeInfo attrInfo;
1211     (void)memset_s(&attrInfo, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
1212     call->GetCallAttributeBaseInfo(attrInfo);
1213     if (priorState == TelCallState::CALL_STATUS_DIALING || priorState == TelCallState::CALL_STATUS_ALERTING ||
1214         ((priorState == TelCallState::CALL_STATUS_INCOMING || priorState == TelCallState::CALL_STATUS_WAITING) &&
1215         attrInfo.answerType != CallAnswerType::CALL_ANSWER_REJECT)) {
1216         // outgoing/incoming video call, but canceled or missed
1217         TELEPHONY_LOGD("canceled or missed call, set voice type as original call type");
1218         call->SetOriginalCallType(static_cast<int32_t>(VideoStateType::TYPE_VOICE));
1219     }
1220 }
1221 
ToSpeakerPhone(sptr<CallBase> & call)1222 int32_t CallStatusManager::ToSpeakerPhone(sptr<CallBase> &call)
1223 {
1224     int32_t ret = TELEPHONY_ERR_FAIL;
1225     if (call == nullptr) {
1226         TELEPHONY_LOGE("Call is NULL");
1227         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1228     }
1229     if (call->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_DIALING) {
1230         TELEPHONY_LOGI("Call is CALL_STATUS_DIALING");
1231         return ret;
1232     }
1233     if (call->IsSpeakerphoneOn()) {
1234         AudioDevice device = {
1235             .deviceType = AudioDeviceType::DEVICE_SPEAKER,
1236             .address = { 0 },
1237         };
1238         DelayedSingleton<AudioControlManager>::GetInstance()->SetAudioDevice(device);
1239         ret = call->SetSpeakerphoneOn(false);
1240     }
1241     return ret;
1242 }
1243 
TurnOffMute(sptr<CallBase> & call)1244 int32_t CallStatusManager::TurnOffMute(sptr<CallBase> &call)
1245 {
1246     bool enabled = true;
1247     if (HasEmergencyCall(enabled) != TELEPHONY_SUCCESS) {
1248         TELEPHONY_LOGI("CallStatusManager::TurnOffMute HasEmergencyCall failed.");
1249     }
1250     if (call->GetEmergencyState() || enabled) {
1251         DelayedSingleton<AudioControlManager>::GetInstance()->SetMute(false);
1252     } else {
1253         DelayedSingleton<AudioControlManager>::GetInstance()->SetMute(true);
1254     }
1255     return TELEPHONY_SUCCESS;
1256 }
1257 
CreateNewCall(const CallDetailInfo & info,CallDirection dir)1258 sptr<CallBase> CallStatusManager::CreateNewCall(const CallDetailInfo &info, CallDirection dir)
1259 {
1260     TELEPHONY_LOGI("CreateNewCall");
1261     DialParaInfo paraInfo;
1262     AppExecFwk::PacMap extras;
1263     extras.Clear();
1264     PackParaInfo(paraInfo, info, dir, extras);
1265 
1266     sptr<CallBase> callPtr = CreateNewCallByCallType(paraInfo, info, dir, extras);
1267     if (callPtr == nullptr) {
1268         TELEPHONY_LOGE("CreateNewCall failed!");
1269         return nullptr;
1270     }
1271     DialScene dialScene = (DialScene)extras.GetIntValue("dialScene");
1272     if (dialScene == DialScene::CALL_EMERGENCY) {
1273         callPtr->SetIsEccContact(true);
1274     }
1275     callPtr->SetOriginalCallType(info.originalCallType);
1276     TELEPHONY_LOGD("originalCallType:%{public}d", info.originalCallType);
1277     if (info.callType == CallType::TYPE_VOIP) {
1278         return callPtr;
1279     }
1280     if (info.state == TelCallState::CALL_STATUS_INCOMING || info.state == TelCallState::CALL_STATUS_WAITING ||
1281         (info.state == TelCallState::CALL_STATUS_DIALING && info.index == 0)) {
1282         TELEPHONY_LOGI("NumberLocationUpdate start");
1283         ffrt::submit([=]() {
1284             DelayedSingleton<CallNumberUtils>::GetInstance()->NumberLocationUpdate(callPtr);
1285             if (info.state == TelCallState::CALL_STATUS_DIALING) {
1286                 DelayedSingleton<CallNumberUtils>::GetInstance()->YellowPageAndMarkUpdate(callPtr);
1287             }
1288         });
1289     }
1290     time_t createTime = time(nullptr);
1291     if (createTime < 0) {
1292         createTime = 0;
1293     }
1294     callPtr->SetCallCreateTime(createTime);
1295     return callPtr;
1296 }
1297 
CreateNewCallByCallType(DialParaInfo & paraInfo,const CallDetailInfo & info,CallDirection dir,AppExecFwk::PacMap & extras)1298 sptr<CallBase> CallStatusManager::CreateNewCallByCallType(
1299     DialParaInfo &paraInfo, const CallDetailInfo &info, CallDirection dir, AppExecFwk::PacMap &extras)
1300 {
1301     sptr<CallBase> callPtr = nullptr;
1302     switch (info.callType) {
1303         case CallType::TYPE_CS: {
1304             if (dir == CallDirection::CALL_DIRECTION_OUT) {
1305                 callPtr = (std::make_unique<CSCall>(paraInfo, extras)).release();
1306             } else {
1307                 callPtr = (std::make_unique<CSCall>(paraInfo)).release();
1308             }
1309             break;
1310         }
1311         case CallType::TYPE_IMS: {
1312             if (dir == CallDirection::CALL_DIRECTION_OUT) {
1313                 callPtr = (std::make_unique<IMSCall>(paraInfo, extras)).release();
1314             } else {
1315                 callPtr = (std::make_unique<IMSCall>(paraInfo)).release();
1316             }
1317             if (callPtr->GetCallType() == CallType::TYPE_IMS) {
1318                 sptr<IMSCall> imsCall = reinterpret_cast<IMSCall *>(callPtr.GetRefPtr());
1319                 imsCall->InitVideoCall();
1320             }
1321             break;
1322         }
1323         case CallType::TYPE_OTT: {
1324             if (dir == CallDirection::CALL_DIRECTION_OUT) {
1325                 callPtr = (std::make_unique<OTTCall>(paraInfo, extras)).release();
1326             } else {
1327                 callPtr = (std::make_unique<OTTCall>(paraInfo)).release();
1328             }
1329             break;
1330         }
1331         case CallType::TYPE_VOIP: {
1332             callPtr = (std::make_unique<VoIPCall>(paraInfo)).release();
1333             break;
1334         }
1335         case CallType::TYPE_SATELLITE: {
1336             if (dir == CallDirection::CALL_DIRECTION_OUT) {
1337                 callPtr = (std::make_unique<SatelliteCall>(paraInfo, extras)).release();
1338             } else {
1339                 callPtr = (std::make_unique<SatelliteCall>(paraInfo)).release();
1340             }
1341             break;
1342         }
1343         default:
1344             return nullptr;
1345     }
1346     return callPtr;
1347 }
1348 
ShouldRejectIncomingCall()1349 bool CallStatusManager::ShouldRejectIncomingCall()
1350 {
1351     bool hasEcc = false;
1352     if (HasEmergencyCall(hasEcc) == TELEPHONY_SUCCESS && hasEcc) {
1353         TELEPHONY_LOGI("HasEmergencyCall reject incoming call.");
1354         return true;
1355     }
1356     auto datashareHelper = SettingsDataShareHelper::GetInstance();
1357     std::string device_provisioned {"0"};
1358     OHOS::Uri uri(
1359         "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=device_provisioned");
1360     int resp = datashareHelper->Query(uri, "device_provisioned", device_provisioned);
1361     if (resp == TELEPHONY_SUCCESS && (device_provisioned == "0" || device_provisioned.empty())) {
1362         TELEPHONY_LOGI("ShouldRejectIncomingCall: device_provisioned = 0");
1363         return true;
1364     }
1365 
1366     std::string user_setup_complete {"1"};
1367     std::vector<int> activedOsAccountIds;
1368     OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activedOsAccountIds);
1369     if (activedOsAccountIds.empty()) {
1370         TELEPHONY_LOGI("ShouldRejectIncomingCall: activedOsAccountIds is empty");
1371         return false;
1372     }
1373     int userId = activedOsAccountIds[0];
1374     OHOS::Uri uri_setup(
1375         "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_"
1376         + std::to_string(userId) + "?Proxy=true&key=user_setup_complete");
1377     int resp_userSetup = datashareHelper->Query(uri_setup, "user_setup_complete", user_setup_complete);
1378     if (resp_userSetup == TELEPHONY_SUCCESS && (user_setup_complete == "0" || user_setup_complete.empty())) {
1379         TELEPHONY_LOGI("ShouldRejectIncomingCall: user_setup_complete = 0");
1380         return true;
1381     }
1382     return false;
1383 }
1384 
ShouldBlockIncomingCall(const sptr<CallBase> & call,const CallDetailInfo & info)1385 bool CallStatusManager::ShouldBlockIncomingCall(const sptr<CallBase> &call, const CallDetailInfo &info)
1386 {
1387     bool isEcc = false;
1388     DelayedSingleton<CellularCallConnection>::GetInstance()->IsEmergencyPhoneNumber(
1389         info.phoneNum, info.accountId, isEcc);
1390     if (isEcc) {
1391         TELEPHONY_LOGI("incoming phoneNumber is ecc.");
1392         return false;
1393     }
1394     std::shared_ptr<SpamCallAdapter> spamCallAdapterPtr_ = std::make_shared<SpamCallAdapter>();
1395     if (spamCallAdapterPtr_ == nullptr) {
1396         TELEPHONY_LOGE("create SpamCallAdapter object failed!");
1397         return false;
1398     }
1399     bool isSpamCall = spamCallAdapterPtr_->DetectSpamCall(std::string(info.phoneNum), info.accountId);
1400     if (!isSpamCall) {
1401         TELEPHONY_LOGE("DetectSpamCall failed!");
1402         return false;
1403     }
1404     if (spamCallAdapterPtr_->WaitForDetectResult()) {
1405         TELEPHONY_LOGI("DetectSpamCall no time out");
1406         NumberMarkInfo numberMarkInfo = {
1407             .markType = MarkType::MARK_TYPE_NONE,
1408             .markContent = "",
1409             .markCount = -1,
1410             .markSource = "",
1411             .isCloud = false,
1412         };
1413         bool isBlock = false;
1414         int32_t blockReason;
1415         spamCallAdapterPtr_->GetParseResult(isBlock, numberMarkInfo, blockReason);
1416         call->SetNumberMarkInfo(numberMarkInfo);
1417         call->SetBlockReason(blockReason);
1418         if (isBlock) {
1419             return true;
1420         }
1421     }
1422     return false;
1423 }
1424 
IsRingOnceCall(const sptr<CallBase> & call,const CallDetailInfo & info)1425 bool CallStatusManager::IsRingOnceCall(const sptr<CallBase> &call, const CallDetailInfo &info)
1426 {
1427     NumberMarkInfo numberMarkInfo = call->GetNumberMarkInfo();
1428     ContactInfo contactInfo = call->GetCallerInfo();
1429     if (numberMarkInfo.markType == MarkType::MARK_TYPE_YELLOW_PAGE ||
1430         std::string(contactInfo.name) != "") {
1431         return false;
1432     }
1433     auto datashareHelper = SettingsDataShareHelper::GetInstance();
1434     std::string is_check_ring_once {"0"};
1435     std::string key = "spamshield_sim" + std::to_string(info.accountId + 1) + "_phone_switch_ring_once";
1436     OHOS::Uri uri(
1437         "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=" + key);
1438     int32_t ret = datashareHelper->Query(uri, key, is_check_ring_once);
1439     if (ret != TELEPHONY_SUCCESS || is_check_ring_once == "0") {
1440         TELEPHONY_LOGI("is_check_ring_once = 0, not need check ring once call");
1441         return false;
1442     }
1443     if (timeWaitHelper_ == nullptr) {
1444         timeWaitHelper_ = std::make_unique<TimeWaitHelper>(WAIT_TIME_THREE_SECOND);
1445     }
1446     if (!timeWaitHelper_->WaitForResult()) {
1447         TELEPHONY_LOGI("is not ring once");
1448         return false;
1449     }
1450     return true;
1451 }
1452 
HandleRingOnceCall(sptr<CallBase> & call)1453 int32_t CallStatusManager::HandleRingOnceCall(sptr<CallBase> &call)
1454 {
1455     if (call == nullptr) {
1456         TELEPHONY_LOGE("call is nullptr!");
1457         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1458     }
1459     int32_t ret = call->SetTelCallState(TelCallState::CALL_STATUS_INCOMING);
1460     if (ret != TELEPHONY_SUCCESS && ret != CALL_ERR_NOT_NEW_STATE) {
1461         TELEPHONY_LOGE("Set CallState failed!");
1462         return ret;
1463     }
1464     return DelayedSingleton<CallControlManager>::GetInstance()->AddCallLogAndNotification(call);
1465 }
1466 
PackParaInfo(DialParaInfo & paraInfo,const CallDetailInfo & info,CallDirection dir,AppExecFwk::PacMap & extras)1467 void CallStatusManager::PackParaInfo(
1468     DialParaInfo &paraInfo, const CallDetailInfo &info, CallDirection dir, AppExecFwk::PacMap &extras)
1469 {
1470     paraInfo.isEcc = false;
1471     paraInfo.dialType = DialType::DIAL_CARRIER_TYPE;
1472     if (dir == CallDirection::CALL_DIRECTION_OUT) {
1473         DelayedSingleton<CallControlManager>::GetInstance()->GetDialParaInfo(paraInfo, extras);
1474     }
1475     if (info.callType == CallType::TYPE_VOIP) {
1476         paraInfo.voipCallInfo.voipCallId = info.voipCallInfo.voipCallId;
1477         paraInfo.voipCallInfo.userName = info.voipCallInfo.userName;
1478         (paraInfo.voipCallInfo.userProfile).assign(
1479             (info.voipCallInfo.userProfile).begin(), (info.voipCallInfo.userProfile).end());
1480         paraInfo.voipCallInfo.abilityName = info.voipCallInfo.abilityName;
1481         paraInfo.voipCallInfo.extensionId = info.voipCallInfo.extensionId;
1482         paraInfo.voipCallInfo.voipBundleName = info.voipCallInfo.voipBundleName;
1483         paraInfo.voipCallInfo.showBannerForIncomingCall = info.voipCallInfo.showBannerForIncomingCall;
1484         paraInfo.voipCallInfo.isConferenceCall = info.voipCallInfo.isConferenceCall;
1485         paraInfo.voipCallInfo.isVoiceAnswerSupported = info.voipCallInfo.isVoiceAnswerSupported;
1486         paraInfo.voipCallInfo.hasMicPermission = info.voipCallInfo.hasMicPermission;
1487         paraInfo.voipCallInfo.uid = info.voipCallInfo.uid;
1488     }
1489     paraInfo.number = info.phoneNum;
1490     paraInfo.callId = GetNewCallId();
1491     paraInfo.index = info.index;
1492     paraInfo.videoState = info.callMode;
1493     paraInfo.accountId = info.accountId;
1494     paraInfo.callType = info.callType;
1495     paraInfo.callState = info.state;
1496     paraInfo.bundleName = info.bundleName;
1497     paraInfo.crsType = info.crsType;
1498     paraInfo.originalCallType = info.originalCallType;
1499 }
1500 
IsFocusModeOpen()1501 bool CallStatusManager::IsFocusModeOpen()
1502 {
1503     auto datashareHelper = SettingsDataShareHelper::GetInstance();
1504     std::string focusModeEnable {"0"};
1505     std::vector<int> activedOsAccountIds;
1506     OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activedOsAccountIds);
1507     if (activedOsAccountIds.empty()) {
1508         TELEPHONY_LOGW("ShouldRejectIncomingCall: activedOsAccountIds is empty");
1509         return false;
1510     }
1511     int userId = activedOsAccountIds[0];
1512     OHOS::Uri uri(
1513         "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_"
1514         + std::to_string(userId) + "?Proxy=true&key=focus_mode_enable");
1515     int resp = datashareHelper->Query(uri, "focus_mode_enable", focusModeEnable);
1516     if (resp == TELEPHONY_SUCCESS && focusModeEnable == "1") {
1517         TELEPHONY_LOGI("IsFocusModeOpen: focus_mode_enable = 1");
1518         return true;
1519     }
1520     return false;
1521 }
1522 
IsRejectCall(sptr<CallBase> & call,const CallDetailInfo & info,bool & block)1523 bool CallStatusManager::IsRejectCall(sptr<CallBase> &call, const CallDetailInfo &info, bool &block)
1524 {
1525     int32_t state;
1526     DelayedSingleton<CallControlManager>::GetInstance()->GetVoIPCallState(state);
1527     if (ShouldRejectIncomingCall() || state == (int32_t)CallStateToApp::CALL_STATE_RINGING) {
1528         CallManagerHisysevent::HiWriteBehaviorEventPhoneUE(
1529             CALL_INCOMING_REJECT_BY_SYSTEM, PNAMEID_KEY, KEY_CALL_MANAGER, PVERSIONID_KEY, "",
1530             ACTION_TYPE, REJECT_BY_OOBE);
1531         block = false;
1532         return true;
1533     }
1534     if (info.callType != CallType::TYPE_VOIP && ShouldBlockIncomingCall(call, info)) {
1535         CallManagerHisysevent::HiWriteBehaviorEventPhoneUE(
1536             CALL_INCOMING_REJECT_BY_SYSTEM, PNAMEID_KEY, KEY_CALL_MANAGER, PVERSIONID_KEY, "",
1537             ACTION_TYPE, REJECT_BY_NUM_BLOCK);
1538         block = true;
1539         return true;
1540     }
1541     if (IsFocusModeOpen()) {
1542         int ret = Notification::NotificationHelper::IsNeedSilentInDoNotDisturbMode(info.phoneNum, 0);
1543         TELEPHONY_LOGI("IsRejectCall IsNeedSilentInDoNotDisturbMode ret:%{public}d", ret);
1544         if (ret == 0) {
1545             CallManagerHisysevent::HiWriteBehaviorEventPhoneUE(
1546                 CALL_INCOMING_REJECT_BY_SYSTEM, PNAMEID_KEY, KEY_CALL_MANAGER, PVERSIONID_KEY, "",
1547                 ACTION_TYPE, REJECT_IN_FOCUSMODE);
1548             block = false;
1549             return true;
1550         }
1551     }
1552     return false;
1553 }
1554 } // namespace Telephony
1555 } // namespace OHOS
1556