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