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