1 /*
2  * Copyright (C) 2021 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_object_manager.h"
17 
18 #include "call_connect_ability.h"
19 #include "call_control_manager.h"
20 #include "call_manager_errors.h"
21 #include "call_number_utils.h"
22 #include "conference_base.h"
23 #include "ims_conference.h"
24 #include "report_call_info_handler.h"
25 #include "telephony_log_wrapper.h"
26 #include "voip_call.h"
27 
28 namespace OHOS {
29 namespace Telephony {
30 std::list<sptr<CallBase>> CallObjectManager::callObjectPtrList_;
31 std::mutex CallObjectManager::listMutex_;
32 int32_t CallObjectManager::callId_ = CALL_START_ID;
33 std::condition_variable CallObjectManager::cv_;
34 bool CallObjectManager::isFirstDialCallAdded_ = false;
35 bool CallObjectManager::needWaitHold_ = false;
36 CellularCallInfo CallObjectManager::dialCallInfo_;
37 constexpr int32_t CRS_TYPE = 2;
38 constexpr uint64_t DISCONNECT_DELAY_TIME = 2000000;
39 
CallObjectManager()40 CallObjectManager::CallObjectManager()
41 {
42 }
43 
~CallObjectManager()44 CallObjectManager::~CallObjectManager()
45 {
46     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
47     while (it != callObjectPtrList_.end()) {
48         (*it) = nullptr;
49         callObjectPtrList_.erase(it++);
50     }
51 }
52 
AddOneCallObject(sptr<CallBase> & call)53 int32_t CallObjectManager::AddOneCallObject(sptr<CallBase> &call)
54 {
55     if (call == nullptr) {
56         return TELEPHONY_ERR_LOCAL_PTR_NULL;
57     }
58     std::lock_guard<std::mutex> lock(listMutex_);
59     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
60     for (; it != callObjectPtrList_.end(); ++it) {
61         if ((*it)->GetCallID() == call->GetCallID()) {
62             TELEPHONY_LOGE("this call has existed yet!");
63             return CALL_ERR_PHONE_CALL_ALREADY_EXISTS;
64         }
65     }
66     CallAttributeInfo info;
67     call->GetCallAttributeInfo(info);
68     int32_t state;
69     bool isVoIPCallExists = false;
70     DelayedSingleton<CallControlManager>::GetInstance()->GetVoIPCallState(state);
71     if (state == (int32_t)CallStateToApp::CALL_STATE_RINGING) {
72         isVoIPCallExists = true;
73     }
74     if (callObjectPtrList_.size() == NO_CALL_EXIST && (!isVoIPCallExists || info.isEcc)) {
75         DelayedSingleton<CallConnectAbility>::GetInstance()->ConnectAbility();
76     }
77     callObjectPtrList_.emplace_back(call);
78     if (callObjectPtrList_.size() == ONE_CALL_EXIST &&
79         callObjectPtrList_.front()->GetTelCallState() == TelCallState::CALL_STATUS_DIALING) {
80         isFirstDialCallAdded_ = true;
81         cv_.notify_all();
82     }
83     TELEPHONY_LOGI("AddOneCallObject success! callId:%{public}d,call list size:%{public}zu", call->GetCallID(),
84         callObjectPtrList_.size());
85     return TELEPHONY_SUCCESS;
86 }
87 
DelayedDisconnectCallConnectAbility()88 void CallObjectManager::DelayedDisconnectCallConnectAbility()
89 {
90     ffrt::submit_h(
91         []() {
92             std::lock_guard<std::mutex> lock(listMutex_);
93             TELEPHONY_LOGI("delayed disconnect callback begin");
94             auto controlManager = DelayedSingleton<CallControlManager>::GetInstance();
95             if (callObjectPtrList_.size() == NO_CALL_EXIST && controlManager->ShouldDisconnectService()) {
96                 auto callConnectAbility = DelayedSingleton<CallConnectAbility>::GetInstance();
97                 callConnectAbility->DisconnectAbility();
98                 TELEPHONY_LOGI("delayed disconnect done");
99             }
100         },
101         {}, {}, ffrt::task_attr().delay(DISCONNECT_DELAY_TIME));
102 }
103 
DeleteOneCallObject(int32_t callId)104 int32_t CallObjectManager::DeleteOneCallObject(int32_t callId)
105 {
106     std::unique_lock<std::mutex> lock(listMutex_);
107     std::list<sptr<CallBase>>::iterator it;
108     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
109         if ((*it)->GetCallID() == callId) {
110             callObjectPtrList_.erase(it);
111             TELEPHONY_LOGI("DeleteOneCallObject success! call list size:%{public}zu", callObjectPtrList_.size());
112             break;
113         }
114     }
115     if (callObjectPtrList_.size() == NO_CALL_EXIST
116         && DelayedSingleton<CallControlManager>::GetInstance()->ShouldDisconnectService()) {
117         lock.unlock();
118         DelayedDisconnectCallConnectAbility();
119     }
120     return TELEPHONY_SUCCESS;
121 }
122 
DeleteOneCallObject(sptr<CallBase> & call)123 void CallObjectManager::DeleteOneCallObject(sptr<CallBase> &call)
124 {
125     if (call == nullptr) {
126         TELEPHONY_LOGE("call is null!");
127         return;
128     }
129     std::unique_lock<std::mutex> lock(listMutex_);
130     callObjectPtrList_.remove(call);
131     if (callObjectPtrList_.size() == 0
132         && DelayedSingleton<CallControlManager>::GetInstance()->ShouldDisconnectService()) {
133         lock.unlock();
134         DelayedDisconnectCallConnectAbility();
135     }
136     TELEPHONY_LOGI("DeleteOneCallObject success! callList size:%{public}zu", callObjectPtrList_.size());
137 }
138 
GetOneCallObject(int32_t callId)139 sptr<CallBase> CallObjectManager::GetOneCallObject(int32_t callId)
140 {
141     sptr<CallBase> retPtr = nullptr;
142     std::lock_guard<std::mutex> lock(listMutex_);
143     std::list<sptr<CallBase>>::iterator it = CallObjectManager::callObjectPtrList_.begin();
144     for (; it != callObjectPtrList_.end(); ++it) {
145         if ((*it)->GetCallID() == callId) {
146             retPtr = *it;
147             break;
148         }
149     }
150     return retPtr;
151 }
152 
GetOneCallObject(std::string & phoneNumber)153 sptr<CallBase> CallObjectManager::GetOneCallObject(std::string &phoneNumber)
154 {
155     if (phoneNumber.empty()) {
156         TELEPHONY_LOGE("call is null!");
157         return nullptr;
158     }
159     sptr<CallBase> retPtr = nullptr;
160     std::lock_guard<std::mutex> lock(listMutex_);
161     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
162     for (; it != callObjectPtrList_.end(); ++it) {
163         std::string networkAddress =
164             DelayedSingleton<CallNumberUtils>::GetInstance()->RemovePostDialPhoneNumber((*it)->GetAccountNumber());
165         if (networkAddress == phoneNumber) {
166             TELEPHONY_LOGI("GetOneCallObject success!");
167             retPtr = *it;
168             break;
169         }
170     }
171     return retPtr;
172 }
173 
HasNewCall()174 int32_t CallObjectManager::HasNewCall()
175 {
176     std::lock_guard<std::mutex> lock(listMutex_);
177     std::list<sptr<CallBase>>::iterator it;
178     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
179         if ((*it)->GetCallType() != CallType::TYPE_VOIP &&
180             ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CREATE ||
181             (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CONNECTING ||
182             (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_DIALING ||
183             (*it)->GetCallType() == CallType::TYPE_SATELLITE)) {
184             TELEPHONY_LOGE("there is already a new call[callId:%{public}d,state:%{public}d], please redial later",
185                 (*it)->GetCallID(), (*it)->GetCallRunningState());
186             return CALL_ERR_CALL_COUNTS_EXCEED_LIMIT;
187         }
188     }
189     return TELEPHONY_SUCCESS;
190 }
191 
IsNewCallAllowedCreate(bool & enabled)192 int32_t CallObjectManager::IsNewCallAllowedCreate(bool &enabled)
193 {
194     enabled = true;
195     std::list<sptr<CallBase>>::iterator it;
196     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
197         if ((*it)->GetCallType() != CallType::TYPE_VOIP &&
198             ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CREATE ||
199             (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CONNECTING ||
200             (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_DIALING ||
201             (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING)) {
202             TELEPHONY_LOGE("there is already a new call, please redial later");
203             enabled = false;
204             return TELEPHONY_ERR_SUCCESS;
205         }
206     }
207     int32_t count = 0;
208     int32_t callNum = 2;
209     std::list<int32_t> callIdList;
210     GetCarrierCallList(callIdList);
211     for (int32_t otherCallId : callIdList) {
212         sptr<CallBase> call = GetOneCallObject(otherCallId);
213         if (call != nullptr) {
214             TelConferenceState confState = call->GetTelConferenceState();
215             int32_t conferenceId = DelayedSingleton<ImsConference>::GetInstance()->GetMainCall();
216             if (confState != TelConferenceState::TEL_CONFERENCE_IDLE && conferenceId == otherCallId) {
217                 TELEPHONY_LOGI("there is conference call");
218                 count++;
219             } else if (confState == TelConferenceState::TEL_CONFERENCE_IDLE) {
220                 count++;
221             }
222         }
223     }
224     TELEPHONY_LOGI("the count is:%{public}d", count);
225     if (count >= callNum) {
226         enabled = false;
227     }
228     return TELEPHONY_ERR_SUCCESS;
229 }
230 
GetCurrentCallNum()231 int32_t CallObjectManager::GetCurrentCallNum()
232 {
233     int32_t count = 0;
234     std::list<int32_t> callIdList;
235     GetCarrierCallList(callIdList);
236     for (int32_t otherCallId : callIdList) {
237         sptr<CallBase> call = GetOneCallObject(otherCallId);
238         if (call != nullptr) {
239             TelConferenceState confState = call->GetTelConferenceState();
240             int32_t conferenceId = DelayedSingleton<ImsConference>::GetInstance()->GetMainCall();
241             if (confState != TelConferenceState::TEL_CONFERENCE_IDLE && conferenceId == otherCallId) {
242                 TELEPHONY_LOGI("there is conference call");
243                 count++;
244             } else if (confState == TelConferenceState::TEL_CONFERENCE_IDLE) {
245                 count++;
246             }
247         }
248     }
249     TELEPHONY_LOGI("the count is %{public}d", count);
250     return count;
251 }
252 
GetCarrierCallList(std::list<int32_t> & list)253 int32_t CallObjectManager::GetCarrierCallList(std::list<int32_t> &list)
254 {
255     list.clear();
256     std::lock_guard<std::mutex> lock(listMutex_);
257     std::list<sptr<CallBase>>::iterator it;
258     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
259         if ((*it)->GetCallType() == CallType::TYPE_CS || (*it)->GetCallType() == CallType::TYPE_IMS ||
260             (*it)->GetCallType() == CallType::TYPE_SATELLITE) {
261             list.emplace_back((*it)->GetCallID());
262         }
263     }
264     return TELEPHONY_SUCCESS;
265 }
266 
GetVoipCallNum()267 int32_t CallObjectManager::GetVoipCallNum()
268 {
269     int32_t count = 0;
270     std::lock_guard<std::mutex> lock(listMutex_);
271     std::list<sptr<CallBase>>::iterator it;
272     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
273         if ((*it)->GetCallType() == CallType::TYPE_VOIP) {
274             count++;
275         }
276     }
277     return count;
278 }
279 
GetVoipCallList(std::list<int32_t> & list)280 int32_t CallObjectManager::GetVoipCallList(std::list<int32_t> &list)
281 {
282     list.clear();
283     std::lock_guard<std::mutex> lock(listMutex_);
284     std::list<sptr<CallBase>>::iterator it;
285     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
286         if ((*it)->GetCallType() == CallType::TYPE_VOIP) {
287             list.emplace_back((*it)->GetCallID());
288         }
289     }
290     return TELEPHONY_SUCCESS;
291 }
292 
HasRingingMaximum()293 bool CallObjectManager::HasRingingMaximum()
294 {
295     int32_t ringingCount = 0;
296     std::lock_guard<std::mutex> lock(listMutex_);
297     std::list<sptr<CallBase>>::iterator it;
298     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
299         // Count the number of calls in the ringing state
300         if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
301             ringingCount++;
302         }
303     }
304     if (ringingCount >= RINGING_CALL_NUMBER_LEN) {
305         return true;
306     }
307     return false;
308 }
309 
HasDialingMaximum()310 bool CallObjectManager::HasDialingMaximum()
311 {
312     int32_t dialingCount = 0;
313     std::lock_guard<std::mutex> lock(listMutex_);
314     std::list<sptr<CallBase>>::iterator it;
315     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
316         // Count the number of calls in the active state
317         if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_ACTIVE) {
318             dialingCount++;
319         }
320     }
321     if (dialingCount >= DIALING_CALL_NUMBER_LEN) {
322         return true;
323     }
324     return false;
325 }
326 
HasEmergencyCall(bool & enabled)327 int32_t CallObjectManager::HasEmergencyCall(bool &enabled)
328 {
329     enabled = false;
330     std::lock_guard<std::mutex> lock(listMutex_);
331     std::list<sptr<CallBase>>::iterator it;
332     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
333         if ((*it)->GetEmergencyState()) {
334             enabled = true;
335         }
336     }
337     return TELEPHONY_ERR_SUCCESS;
338 }
339 
GetNewCallId()340 int32_t CallObjectManager::GetNewCallId()
341 {
342     int32_t ret = 0;
343     std::lock_guard<std::mutex> lock(listMutex_);
344     ret = ++callId_;
345     return ret;
346 }
347 
IsCallExist(int32_t callId)348 bool CallObjectManager::IsCallExist(int32_t callId)
349 {
350     std::lock_guard<std::mutex> lock(listMutex_);
351     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
352     for (; it != callObjectPtrList_.end(); ++it) {
353         if ((*it)->GetCallID() == callId) {
354             TELEPHONY_LOGW("the call is exist.");
355             return true;
356         }
357     }
358     return false;
359 }
360 
IsCallExist(std::string & phoneNumber)361 bool CallObjectManager::IsCallExist(std::string &phoneNumber)
362 {
363     if (phoneNumber.empty()) {
364         return false;
365     }
366     std::lock_guard<std::mutex> lock(listMutex_);
367     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
368     for (; it != callObjectPtrList_.end(); ++it) {
369         std::string networkAddress =
370             DelayedSingleton<CallNumberUtils>::GetInstance()->RemovePostDialPhoneNumber((*it)->GetAccountNumber());
371         if (networkAddress == phoneNumber) {
372             return true;
373         }
374     }
375     TELEPHONY_LOGI("the call is does not exist.");
376     return false;
377 }
378 
HasCallExist()379 bool CallObjectManager::HasCallExist()
380 {
381     std::lock_guard<std::mutex> lock(listMutex_);
382     if (callObjectPtrList_.empty()) {
383         TELEPHONY_LOGI("call list size:%{public}zu", callObjectPtrList_.size());
384         return false;
385     }
386     return true;
387 }
388 
GetAllCallList()389 std::list<sptr<CallBase>> CallObjectManager::GetAllCallList()
390 {
391     std::lock_guard<std::mutex> lock(listMutex_);
392     return callObjectPtrList_;
393 }
394 
HasCellularCallExist()395 bool CallObjectManager::HasCellularCallExist()
396 {
397     std::lock_guard<std::mutex> lock(listMutex_);
398     std::list<sptr<CallBase>>::iterator it;
399     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
400         if ((*it)->GetCallType() == CallType::TYPE_CS || (*it)->GetCallType() == CallType::TYPE_IMS ||
401             (*it)->GetCallType() == CallType::TYPE_SATELLITE) {
402             if ((*it)->GetTelCallState() != TelCallState::CALL_STATUS_DISCONNECTED &&
403                 (*it)->GetTelCallState() != TelCallState::CALL_STATUS_DISCONNECTING) {
404                 return true;
405             }
406         }
407     }
408     return false;
409 }
410 
HasVoipCallExist()411 bool CallObjectManager::HasVoipCallExist()
412 {
413     std::lock_guard<std::mutex> lock(listMutex_);
414     std::list<sptr<CallBase>>::iterator it;
415     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
416         if ((*it)->GetCallType() == CallType::TYPE_VOIP) {
417             return true;
418         }
419     }
420     return false;
421 }
422 
HasIncomingCallCrsType()423 bool CallObjectManager::HasIncomingCallCrsType()
424 {
425     std::lock_guard<std::mutex> lock(listMutex_);
426     std::list<sptr<CallBase>>::iterator it;
427     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
428         if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING &&
429             (*it)->GetCrsType() == CRS_TYPE) {
430             return true;
431         }
432     }
433     return false;
434 }
435 
HasVideoCall()436 bool CallObjectManager::HasVideoCall()
437 {
438     std::lock_guard<std::mutex> lock(listMutex_);
439     std::list<sptr<CallBase>>::iterator it;
440     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
441         if ((*it)->GetVideoStateType() == VideoStateType::TYPE_VIDEO && (*it)->GetCallType() != CallType::TYPE_VOIP) {
442             return true;
443         }
444     }
445     return false;
446 }
447 
HasSatelliteCallExist()448 bool CallObjectManager::HasSatelliteCallExist()
449 {
450     std::lock_guard<std::mutex> lock(listMutex_);
451     std::list<sptr<CallBase>>::iterator it;
452     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
453         if ((*it)->GetCallType() == CallType::TYPE_SATELLITE) {
454             return true;
455         }
456     }
457     return false;
458 }
459 
GetSatelliteCallList(std::list<int32_t> & list)460 int32_t CallObjectManager::GetSatelliteCallList(std::list<int32_t> &list)
461 {
462     list.clear();
463     std::lock_guard<std::mutex> lock(listMutex_);
464     std::list<sptr<CallBase>>::iterator it;
465     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
466         if ((*it)->GetCallType() == CallType::TYPE_SATELLITE) {
467             list.emplace_back((*it)->GetCallID());
468         }
469     }
470     return TELEPHONY_SUCCESS;
471 }
472 
HasRingingCall(bool & hasRingingCall)473 int32_t CallObjectManager::HasRingingCall(bool &hasRingingCall)
474 {
475     hasRingingCall = false;
476     std::lock_guard<std::mutex> lock(listMutex_);
477     std::list<sptr<CallBase>>::iterator it;
478     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
479         // Count the number of calls in the ringing state
480         if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
481             hasRingingCall = true;
482             break;
483         }
484     }
485     return TELEPHONY_ERR_SUCCESS;
486 }
487 
HasHoldCall(bool & hasHoldCall)488 int32_t CallObjectManager::HasHoldCall(bool &hasHoldCall)
489 {
490     hasHoldCall = false;
491     std::lock_guard<std::mutex> lock(listMutex_);
492     std::list<sptr<CallBase>>::iterator it;
493     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
494         // Count the number of calls in the hold state
495         if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_HOLD) {
496             hasHoldCall = true;
497             break;
498         }
499     }
500     return TELEPHONY_ERR_SUCCESS;
501 }
502 
GetCallState(int32_t callId)503 TelCallState CallObjectManager::GetCallState(int32_t callId)
504 {
505     TelCallState retState = TelCallState::CALL_STATUS_IDLE;
506     std::lock_guard<std::mutex> lock(listMutex_);
507     std::list<sptr<CallBase>>::iterator it = CallObjectManager::callObjectPtrList_.begin();
508     for (; it != callObjectPtrList_.end(); ++it) {
509         if ((*it)->GetCallID() == callId) {
510             retState = (*it)->GetTelCallState();
511             break;
512         }
513     }
514     return retState;
515 }
516 
GetOneCallObject(CallRunningState callState)517 sptr<CallBase> CallObjectManager::GetOneCallObject(CallRunningState callState)
518 {
519     std::lock_guard<std::mutex> lock(listMutex_);
520     std::list<sptr<CallBase>>::reverse_iterator it;
521     for (it = callObjectPtrList_.rbegin(); it != callObjectPtrList_.rend(); ++it) {
522         if ((*it)->GetCallRunningState() == callState) {
523             return (*it);
524         }
525     }
526     return nullptr;
527 }
528 
GetOneCallObjectByIndex(int32_t index)529 sptr<CallBase> CallObjectManager::GetOneCallObjectByIndex(int32_t index)
530 {
531     std::lock_guard<std::mutex> lock(listMutex_);
532     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
533     for (; it != callObjectPtrList_.end(); ++it) {
534         if ((*it)->GetCallIndex() == index && (*it)->GetCallType() != CallType::TYPE_VOIP) {
535             return (*it);
536         }
537     }
538     return nullptr;
539 }
540 
GetOneCallObjectByIndexAndSlotId(int32_t index,int32_t slotId)541 sptr<CallBase> CallObjectManager::GetOneCallObjectByIndexAndSlotId(int32_t index, int32_t slotId)
542 {
543     std::lock_guard<std::mutex> lock(listMutex_);
544     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
545     for (; it != callObjectPtrList_.end(); ++it) {
546         if ((*it)->GetCallIndex() == index) {
547             if ((*it)->GetSlotId() == slotId && (*it)->GetCallType() != CallType::TYPE_VOIP) {
548                 return (*it);
549             }
550         }
551     }
552     return nullptr;
553 }
554 
GetOneCallObjectByVoipCallId(std::string voipCallId,std::string bundleName,int32_t uid)555 sptr<CallBase> CallObjectManager::GetOneCallObjectByVoipCallId(
556     std::string voipCallId, std::string bundleName, int32_t uid)
557 {
558     std::lock_guard<std::mutex> lock(listMutex_);
559     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
560     for (; it != callObjectPtrList_.end(); ++it) {
561         if ((*it)->GetCallType() == CallType::TYPE_VOIP) {
562             sptr<VoIPCall> voipCall = reinterpret_cast<VoIPCall *>((*it).GetRefPtr());
563             if (voipCall->GetVoipCallId() == voipCallId && voipCall->GetVoipBundleName() == bundleName &&
564                 voipCall->GetVoipUid() == uid) {
565                 return (*it);
566             }
567         }
568     }
569     return nullptr;
570 }
571 
IsCallExist(CallType callType,TelCallState callState)572 bool CallObjectManager::IsCallExist(CallType callType, TelCallState callState)
573 {
574     std::lock_guard<std::mutex> lock(listMutex_);
575     std::list<sptr<CallBase>>::iterator it;
576     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
577         if ((*it)->GetCallType() == callType && (*it)->GetTelCallState() == callState) {
578             return true;
579         }
580     }
581     TELEPHONY_LOGI("the call is does not exist.");
582     return false;
583 }
584 
IsCallExist(TelCallState callState)585 bool CallObjectManager::IsCallExist(TelCallState callState)
586 {
587     std::lock_guard<std::mutex> lock(listMutex_);
588     std::list<sptr<CallBase>>::iterator it;
589     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
590         if ((*it)->GetTelCallState() == callState) {
591             return true;
592         }
593     }
594     TELEPHONY_LOGI("the call is does not exist.");
595     return false;
596 }
597 
IsCallExist(TelCallState callState,int32_t & callId)598 bool CallObjectManager::IsCallExist(TelCallState callState, int32_t &callId)
599 {
600     std::lock_guard<std::mutex> lock(listMutex_);
601     std::list<sptr<CallBase>>::iterator it;
602     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
603         if ((*it)->GetTelCallState() == callState) {
604             callId = (*it)->GetCallID();
605             return true;
606         }
607     }
608     TELEPHONY_LOGI("the call is does not exist.");
609     return false;
610 }
611 
IsConferenceCallExist(TelConferenceState state,int32_t & callId)612 bool CallObjectManager::IsConferenceCallExist(TelConferenceState state, int32_t &callId)
613 {
614     std::lock_guard<std::mutex> lock(listMutex_);
615     std::list<sptr<CallBase>>::iterator it;
616     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
617         if ((*it)->GetTelConferenceState() == state) {
618             callId = (*it)->GetCallID();
619             return true;
620         }
621     }
622     TELEPHONY_LOGI("the call is does not exist.");
623     return false;
624 }
625 
GetCallNum(TelCallState callState,bool isIncludeVoipCall)626 int32_t CallObjectManager::GetCallNum(TelCallState callState, bool isIncludeVoipCall)
627 {
628     int32_t num = 0;
629     std::lock_guard<std::mutex> lock(listMutex_);
630     std::list<sptr<CallBase>>::iterator it;
631     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
632         if ((*it)->GetTelCallState() == callState) {
633             if (!isIncludeVoipCall && (*it)->GetCallType() == CallType::TYPE_VOIP) {
634                 continue;
635             } else {
636                 ++num;
637             }
638         }
639     }
640     TELEPHONY_LOGI("callState:%{public}d, num:%{public}d", callState, num);
641     return num;
642 }
643 
GetCallNumber(TelCallState callState,bool isIncludeVoipCall)644 std::string CallObjectManager::GetCallNumber(TelCallState callState, bool isIncludeVoipCall)
645 {
646     std::string number = "";
647     std::lock_guard<std::mutex> lock(listMutex_);
648     std::list<sptr<CallBase>>::iterator it;
649     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
650         if ((*it)->GetTelCallState() == callState) {
651             if (!isIncludeVoipCall && (*it)->GetCallType() == CallType::TYPE_VOIP) {
652                 continue;
653             } else {
654                 number = (*it)->GetAccountNumber();
655                 break;
656             }
657         }
658     }
659     return number;
660 }
661 
GetCallInfoList(int32_t slotId)662 std::vector<CallAttributeInfo> CallObjectManager::GetCallInfoList(int32_t slotId)
663 {
664     std::vector<CallAttributeInfo> callVec;
665     CallAttributeInfo info;
666     callVec.clear();
667     std::lock_guard<std::mutex> lock(listMutex_);
668     std::list<sptr<CallBase>>::iterator it;
669     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
670         (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
671         (*it)->GetCallAttributeInfo(info);
672         if (info.accountId == slotId && info.callType != CallType::TYPE_OTT) {
673             callVec.emplace_back(info);
674         }
675     }
676     return callVec;
677 }
678 
UpdateOneCallObjectByCallId(int32_t callId,TelCallState nextCallState)679 void CallObjectManager::UpdateOneCallObjectByCallId(int32_t callId, TelCallState nextCallState)
680 {
681     std::lock_guard<std::mutex> lock(listMutex_);
682     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
683     for (; it != callObjectPtrList_.end(); ++it) {
684         if ((*it)->GetCallID() == callId) {
685             (*it)->SetTelCallState(nextCallState);
686         }
687     }
688 }
689 
GetForegroundCall(bool isIncludeVoipCall)690 sptr<CallBase> CallObjectManager::GetForegroundCall(bool isIncludeVoipCall)
691 {
692     std::lock_guard<std::mutex> lock(listMutex_);
693     sptr<CallBase> liveCall = nullptr;
694     for (std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
695         if (!isIncludeVoipCall && (*it)->GetCallType() == CallType::TYPE_VOIP) {
696             continue;
697         }
698         TelCallState telCallState = (*it)->GetTelCallState();
699         if (telCallState == TelCallState::CALL_STATUS_WAITING ||
700             telCallState == TelCallState::CALL_STATUS_INCOMING) {
701             liveCall = (*it);
702             break;
703         }
704         if (telCallState == TelCallState::CALL_STATUS_ALERTING ||
705             telCallState == TelCallState::CALL_STATUS_DIALING) {
706             liveCall = (*it);
707             continue;
708         }
709         if (telCallState == TelCallState::CALL_STATUS_ACTIVE) {
710             liveCall = (*it);
711             continue;
712         }
713         if (telCallState == TelCallState::CALL_STATUS_HOLDING) {
714             liveCall = (*it);
715             continue;
716         }
717     }
718     return liveCall;
719 }
720 
GetForegroundLiveCall()721 sptr<CallBase> CallObjectManager::GetForegroundLiveCall()
722 {
723     std::lock_guard<std::mutex> lock(listMutex_);
724     sptr<CallBase> liveCall = nullptr;
725     for (std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
726         TelCallState telCallState = (*it)->GetTelCallState();
727         if (telCallState == TelCallState::CALL_STATUS_ACTIVE) {
728             liveCall = (*it);
729             break;
730         }
731         if (telCallState == TelCallState::CALL_STATUS_ALERTING ||
732             telCallState == TelCallState::CALL_STATUS_DIALING) {
733             liveCall = (*it);
734             break;
735         }
736         if (telCallState == TelCallState::CALL_STATUS_WAITING ||
737             telCallState == TelCallState::CALL_STATUS_INCOMING) {
738             liveCall = (*it);
739             continue;
740         }
741     }
742     return liveCall;
743 }
744 
GetDialCallInfo()745 CellularCallInfo CallObjectManager::GetDialCallInfo()
746 {
747     return dialCallInfo_;
748 }
749 
DealFailDial(sptr<CallBase> call)750 int32_t CallObjectManager::DealFailDial(sptr<CallBase> call)
751 {
752     CallDetailInfo callDetatilInfo;
753     if (memset_s(&callDetatilInfo, sizeof(CallDetailInfo), 0, sizeof(CallDetailInfo)) != EOK) {
754         TELEPHONY_LOGE("memset_s callDetatilInfo fail");
755         return TELEPHONY_ERR_MEMSET_FAIL;
756     }
757     std::string number = call->GetAccountNumber();
758     callDetatilInfo.callType = call->GetCallType();
759     callDetatilInfo.accountId = call->GetSlotId();
760     callDetatilInfo.state = TelCallState::CALL_STATUS_DISCONNECTED;
761     callDetatilInfo.callMode = call->GetVideoStateType();
762     callDetatilInfo.voiceDomain = static_cast<int32_t>(call->GetCallType());
763     if (number.length() > kMaxNumberLen) {
764         TELEPHONY_LOGE("numbser length out of range");
765         return CALL_ERR_NUMBER_OUT_OF_RANGE;
766     }
767     if (memcpy_s(&callDetatilInfo.phoneNum, kMaxNumberLen, number.c_str(), number.length()) != EOK) {
768         TELEPHONY_LOGE("memcpy_s number failed!");
769         return TELEPHONY_ERR_MEMCPY_FAIL;
770     }
771 
772     return DelayedSingleton<ReportCallInfoHandler>::GetInstance()->UpdateCallReportInfo(callDetatilInfo);
773 }
774 
GetAllCallInfoList()775 std::vector<CallAttributeInfo> CallObjectManager::GetAllCallInfoList()
776 {
777     std::vector<CallAttributeInfo> callVec;
778     callVec.clear();
779     std::lock_guard<std::mutex> lock(listMutex_);
780     std::list<sptr<CallBase>>::iterator it;
781     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
782         CallAttributeInfo info;
783         if ((*it) == nullptr) {
784             TELEPHONY_LOGE("call is nullptr");
785             continue;
786         }
787         (*it)->GetCallAttributeInfo(info);
788         callVec.emplace_back(info);
789     }
790     return callVec;
791 }
792 
GetCallNumByRunningState(CallRunningState callState)793 int32_t CallObjectManager::GetCallNumByRunningState(CallRunningState callState)
794 {
795     int32_t count = 0;
796     std::lock_guard<std::mutex> lock(listMutex_);
797     std::list<sptr<CallBase>>::iterator it;
798     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
799         if ((*it)->GetCallRunningState() == callState) {
800             count++;
801             continue;
802         }
803     }
804     TELEPHONY_LOGI("callState:%{public}d, count:%{public}d", callState, count);
805     return count;
806 }
807 } // namespace Telephony
808 } // namespace OHOS
809