• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "call_object_manager.h"
17 
18 #include "call_connect_ability.h"
19 #include "call_manager_errors.h"
20 #include "call_number_utils.h"
21 #include "report_call_info_handler.h"
22 #include "telephony_log_wrapper.h"
23 
24 namespace OHOS {
25 namespace Telephony {
26 std::list<sptr<CallBase>> CallObjectManager::callObjectPtrList_;
27 std::mutex CallObjectManager::listMutex_;
28 int32_t CallObjectManager::callId_ = CALL_START_ID;
29 std::condition_variable CallObjectManager::cv_;
30 bool CallObjectManager::isFirstDialCallAdded_ = false;
31 
CallObjectManager()32 CallObjectManager::CallObjectManager()
33 {
34 }
35 
~CallObjectManager()36 CallObjectManager::~CallObjectManager()
37 {
38     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
39     while (it != callObjectPtrList_.end()) {
40         (*it) = nullptr;
41         callObjectPtrList_.erase(it++);
42     }
43 }
44 
AddOneCallObject(sptr<CallBase> & call)45 int32_t CallObjectManager::AddOneCallObject(sptr<CallBase> &call)
46 {
47     if (call == nullptr) {
48         return TELEPHONY_ERR_LOCAL_PTR_NULL;
49     }
50     std::lock_guard<std::mutex> lock(listMutex_);
51     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
52     for (; it != callObjectPtrList_.end(); ++it) {
53         if ((*it)->GetCallID() == call->GetCallID()) {
54             TELEPHONY_LOGE("this call has existed yet!");
55             return CALL_ERR_PHONE_CALL_ALREADY_EXISTS;
56         }
57     }
58     if (callObjectPtrList_.size() == NO_CALL_EXIST) {
59         CallAttributeInfo info;
60         call->GetCallAttributeInfo(info);
61         DelayedSingleton<CallConnectAbility>::GetInstance()->ConnectAbility(info);
62     }
63     callObjectPtrList_.emplace_back(call);
64     if (callObjectPtrList_.size() == ONE_CALL_EXIST &&
65         callObjectPtrList_.front()->GetTelCallState() == TelCallState::CALL_STATUS_DIALING) {
66         isFirstDialCallAdded_ = true;
67         cv_.notify_all();
68     }
69     TELEPHONY_LOGI("AddOneCallObject success! callId:%{public}d,call list size:%{public}zu", call->GetCallID(),
70         callObjectPtrList_.size());
71     return TELEPHONY_SUCCESS;
72 }
73 
DeleteOneCallObject(int32_t callId)74 int32_t CallObjectManager::DeleteOneCallObject(int32_t callId)
75 {
76     std::lock_guard<std::mutex> lock(listMutex_);
77     std::list<sptr<CallBase>>::iterator it;
78     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
79         if ((*it)->GetCallID() == callId) {
80             callObjectPtrList_.erase(it);
81             TELEPHONY_LOGI("DeleteOneCallObject success! call list size:%{public}zu", callObjectPtrList_.size());
82             break;
83         }
84     }
85     if (callObjectPtrList_.size() == NO_CALL_EXIST) {
86         DelayedSingleton<CallConnectAbility>::GetInstance()->DisconnectAbility();
87     }
88     return TELEPHONY_SUCCESS;
89 }
90 
DeleteOneCallObject(sptr<CallBase> & call)91 void CallObjectManager::DeleteOneCallObject(sptr<CallBase> &call)
92 {
93     if (call == nullptr) {
94         TELEPHONY_LOGE("call is null!");
95         return;
96     }
97     std::lock_guard<std::mutex> lock(listMutex_);
98     callObjectPtrList_.remove(call);
99     if (callObjectPtrList_.size() == 0) {
100         DelayedSingleton<CallConnectAbility>::GetInstance()->DisconnectAbility();
101     }
102     TELEPHONY_LOGI("DeleteOneCallObject success! callList size:%{public}zu", callObjectPtrList_.size());
103 }
104 
GetOneCallObject(int32_t callId)105 sptr<CallBase> CallObjectManager::GetOneCallObject(int32_t callId)
106 {
107     sptr<CallBase> retPtr = nullptr;
108     std::lock_guard<std::mutex> lock(listMutex_);
109     std::list<sptr<CallBase>>::iterator it = CallObjectManager::callObjectPtrList_.begin();
110     for (; it != callObjectPtrList_.end(); ++it) {
111         if ((*it)->GetCallID() == callId) {
112             retPtr = *it;
113             break;
114         }
115     }
116     return retPtr;
117 }
118 
GetOneCallObject(std::string & phoneNumber)119 sptr<CallBase> CallObjectManager::GetOneCallObject(std::string &phoneNumber)
120 {
121     if (phoneNumber.empty()) {
122         TELEPHONY_LOGE("call is null!");
123         return nullptr;
124     }
125     sptr<CallBase> retPtr = nullptr;
126     std::lock_guard<std::mutex> lock(listMutex_);
127     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
128     for (; it != callObjectPtrList_.end(); ++it) {
129         std::string networkAddress = DelayedSingleton<CallNumberUtils>::GetInstance()->RemovePostDailPhoneNumber(
130             (*it)->GetAccountNumber());
131         if (networkAddress == phoneNumber) {
132             TELEPHONY_LOGI("GetOneCallObject success!");
133             retPtr = *it;
134             break;
135         }
136     }
137     return retPtr;
138 }
139 
HasNewCall()140 int32_t CallObjectManager::HasNewCall()
141 {
142     std::lock_guard<std::mutex> lock(listMutex_);
143     std::list<sptr<CallBase>>::iterator it;
144     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
145         if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CREATE ||
146             (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CONNECTING ||
147             (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_DIALING) {
148             TELEPHONY_LOGE("there is already a new call[callId:%{public}d,state:%{public}d], please redial later",
149                 (*it)->GetCallID(), (*it)->GetCallRunningState());
150             return CALL_ERR_DIAL_IS_BUSY;
151         }
152     }
153     return TELEPHONY_SUCCESS;
154 }
155 
IsNewCallAllowedCreate(bool & enabled)156 int32_t CallObjectManager::IsNewCallAllowedCreate(bool &enabled)
157 {
158     enabled = true;
159     std::lock_guard<std::mutex> lock(listMutex_);
160     std::list<sptr<CallBase>>::iterator it;
161     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
162         if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CREATE ||
163             (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CONNECTING ||
164             (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_DIALING ||
165             (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
166             TELEPHONY_LOGE("there is already a new call, please redial later");
167             enabled = false;
168             break;
169         }
170     }
171 
172     return TELEPHONY_ERR_SUCCESS;
173 }
174 
GetCarrierCallList(std::list<int32_t> & list)175 int32_t CallObjectManager::GetCarrierCallList(std::list<int32_t> &list)
176 {
177     list.clear();
178     std::lock_guard<std::mutex> lock(listMutex_);
179     std::list<sptr<CallBase>>::iterator it;
180     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
181         if ((*it)->GetCallType() == CallType::TYPE_CS || (*it)->GetCallType() == CallType::TYPE_IMS) {
182             list.emplace_back((*it)->GetCallID());
183         }
184     }
185     return TELEPHONY_SUCCESS;
186 }
187 
HasRingingMaximum()188 bool CallObjectManager::HasRingingMaximum()
189 {
190     int32_t ringingCount = 0;
191     std::lock_guard<std::mutex> lock(listMutex_);
192     std::list<sptr<CallBase>>::iterator it;
193     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
194         // Count the number of calls in the ringing state
195         if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
196             ringingCount++;
197         }
198     }
199     if (ringingCount >= RINGING_CALL_NUMBER_LEN) {
200         return true;
201     }
202     return false;
203 }
204 
HasDialingMaximum()205 bool CallObjectManager::HasDialingMaximum()
206 {
207     int32_t dialingCount = 0;
208     std::lock_guard<std::mutex> lock(listMutex_);
209     std::list<sptr<CallBase>>::iterator it;
210     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
211         // Count the number of calls in the active state
212         if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_ACTIVE) {
213             dialingCount++;
214         }
215     }
216     if (dialingCount >= DIALING_CALL_NUMBER_LEN) {
217         return true;
218     }
219     return false;
220 }
221 
HasEmergencyCall(bool & enabled)222 int32_t CallObjectManager::HasEmergencyCall(bool &enabled)
223 {
224     enabled = false;
225     std::lock_guard<std::mutex> lock(listMutex_);
226     std::list<sptr<CallBase>>::iterator it;
227     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
228         if ((*it)->GetEmergencyState()) {
229             enabled = true;
230         }
231     }
232     return TELEPHONY_ERR_SUCCESS;
233 }
234 
GetNewCallId()235 int32_t CallObjectManager::GetNewCallId()
236 {
237     int32_t ret = 0;
238     std::lock_guard<std::mutex> lock(listMutex_);
239     ret = ++callId_;
240     return ret;
241 }
242 
IsCallExist(int32_t callId)243 bool CallObjectManager::IsCallExist(int32_t callId)
244 {
245     std::lock_guard<std::mutex> lock(listMutex_);
246     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
247     for (; it != callObjectPtrList_.end(); ++it) {
248         if ((*it)->GetCallID() == callId) {
249             TELEPHONY_LOGW("the call is exist.");
250             return true;
251         }
252     }
253     return false;
254 }
255 
IsCallExist(std::string & phoneNumber)256 bool CallObjectManager::IsCallExist(std::string &phoneNumber)
257 {
258     if (phoneNumber.empty()) {
259         return false;
260     }
261     std::lock_guard<std::mutex> lock(listMutex_);
262     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
263     for (; it != callObjectPtrList_.end(); ++it) {
264         std::string networkAddress = DelayedSingleton<CallNumberUtils>::GetInstance()->RemovePostDailPhoneNumber(
265             (*it)->GetAccountNumber());
266         if (networkAddress == phoneNumber) {
267             return true;
268         }
269     }
270     TELEPHONY_LOGI("the call is does not exist.");
271     return false;
272 }
273 
HasCallExist()274 bool CallObjectManager::HasCallExist()
275 {
276     std::lock_guard<std::mutex> lock(listMutex_);
277     if (callObjectPtrList_.empty()) {
278         TELEPHONY_LOGI("call list size:%{public}zu", callObjectPtrList_.size());
279         return false;
280     }
281     return true;
282 }
283 
HasRingingCall(bool & enabled)284 int32_t CallObjectManager::HasRingingCall(bool &enabled)
285 {
286     enabled = false;
287     std::lock_guard<std::mutex> lock(listMutex_);
288     std::list<sptr<CallBase>>::iterator it;
289     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
290         // Count the number of calls in the ringing state
291         if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
292             enabled = true;
293             break;
294         }
295     }
296     return TELEPHONY_ERR_SUCCESS;
297 }
298 
GetCallState(int32_t callId)299 TelCallState CallObjectManager::GetCallState(int32_t callId)
300 {
301     TelCallState retState = TelCallState::CALL_STATUS_IDLE;
302     std::lock_guard<std::mutex> lock(listMutex_);
303     std::list<sptr<CallBase>>::iterator it = CallObjectManager::callObjectPtrList_.begin();
304     for (; it != callObjectPtrList_.end(); ++it) {
305         if ((*it)->GetCallID() == callId) {
306             retState = (*it)->GetTelCallState();
307             break;
308         }
309     }
310     return retState;
311 }
312 
GetOneCallObject(CallRunningState callState)313 sptr<CallBase> CallObjectManager::GetOneCallObject(CallRunningState callState)
314 {
315     std::lock_guard<std::mutex> lock(listMutex_);
316     std::list<sptr<CallBase>>::reverse_iterator it;
317     for (it = callObjectPtrList_.rbegin(); it != callObjectPtrList_.rend(); ++it) {
318         if ((*it)->GetCallRunningState() == callState) {
319             return (*it);
320         }
321     }
322     return nullptr;
323 }
324 
GetOneCallObjectByIndex(int32_t index)325 sptr<CallBase> CallObjectManager::GetOneCallObjectByIndex(int32_t index)
326 {
327     std::lock_guard<std::mutex> lock(listMutex_);
328     std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
329     for (; it != callObjectPtrList_.end(); ++it) {
330         if ((*it)->GetCallIndex() == index) {
331             return (*it);
332         }
333     }
334     return nullptr;
335 }
336 
IsCallExist(CallType callType,TelCallState callState)337 bool CallObjectManager::IsCallExist(CallType callType, TelCallState callState)
338 {
339     std::lock_guard<std::mutex> lock(listMutex_);
340     std::list<sptr<CallBase>>::iterator it;
341     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
342         if ((*it)->GetCallType() == callType && (*it)->GetTelCallState() == callState) {
343             return true;
344         }
345     }
346     TELEPHONY_LOGI("the call is does not exist.");
347     return false;
348 }
349 
IsCallExist(TelCallState callState)350 bool CallObjectManager::IsCallExist(TelCallState callState)
351 {
352     std::lock_guard<std::mutex> lock(listMutex_);
353     std::list<sptr<CallBase>>::iterator it;
354     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
355         if ((*it)->GetTelCallState() == callState) {
356             return true;
357         }
358     }
359     TELEPHONY_LOGI("the call is does not exist.");
360     return false;
361 }
362 
IsCallExist(TelCallState callState,int32_t & callId)363 bool CallObjectManager::IsCallExist(TelCallState callState, int32_t &callId)
364 {
365     std::lock_guard<std::mutex> lock(listMutex_);
366     std::list<sptr<CallBase>>::iterator it;
367     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
368         if ((*it)->GetTelCallState() == callState) {
369             callId = (*it)->GetCallID();
370             return true;
371         }
372     }
373     TELEPHONY_LOGI("the call is does not exist.");
374     return false;
375 }
376 
IsConferenceCallExist(TelConferenceState state,int32_t & callId)377 bool CallObjectManager::IsConferenceCallExist(TelConferenceState state, int32_t &callId)
378 {
379     std::lock_guard<std::mutex> lock(listMutex_);
380     std::list<sptr<CallBase>>::iterator it;
381     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
382         if ((*it)->GetTelConferenceState() == state) {
383             callId = (*it)->GetCallID();
384             return true;
385         }
386     }
387     TELEPHONY_LOGI("the call is does not exist.");
388     return false;
389 }
390 
GetCallNum(TelCallState callState)391 int32_t CallObjectManager::GetCallNum(TelCallState callState)
392 {
393     int32_t num = 0;
394     std::lock_guard<std::mutex> lock(listMutex_);
395     std::list<sptr<CallBase>>::iterator it;
396     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
397         if ((*it)->GetTelCallState() == callState) {
398             ++num;
399             continue;
400         }
401     }
402     TELEPHONY_LOGI("callState:%{public}d, num:%{public}d", callState, num);
403     return num;
404 }
405 
GetCallNumber(TelCallState callState)406 std::string CallObjectManager::GetCallNumber(TelCallState callState)
407 {
408     std::string number = "";
409     std::lock_guard<std::mutex> lock(listMutex_);
410     std::list<sptr<CallBase>>::iterator it;
411     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
412         if ((*it)->GetTelCallState() == callState) {
413             number = (*it)->GetAccountNumber();
414             break;
415         }
416     }
417     return number;
418 }
419 
GetCallInfoList(int32_t slotId)420 std::vector<CallAttributeInfo> CallObjectManager::GetCallInfoList(int32_t slotId)
421 {
422     std::vector<CallAttributeInfo> callVec;
423     CallAttributeInfo info;
424     callVec.clear();
425     std::lock_guard<std::mutex> lock(listMutex_);
426     std::list<sptr<CallBase>>::iterator it;
427     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
428         (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
429         (*it)->GetCallAttributeInfo(info);
430         if (info.accountId == slotId && info.callType != CallType::TYPE_OTT) {
431             callVec.emplace_back(info);
432         }
433     }
434     return callVec;
435 }
436 
GetForegroundLiveCall()437 sptr<CallBase> CallObjectManager::GetForegroundLiveCall()
438 {
439     std::lock_guard<std::mutex> lock(listMutex_);
440     sptr<CallBase> liveCall = nullptr;
441     for (std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
442         TelCallState telCallState = (*it)->GetTelCallState();
443         if (telCallState == TelCallState::CALL_STATUS_WAITING ||
444             telCallState == TelCallState::CALL_STATUS_INCOMING) {
445             liveCall = (*it);
446             break;
447         }
448         if (telCallState == TelCallState::CALL_STATUS_ALERTING ||
449             telCallState == TelCallState::CALL_STATUS_DIALING) {
450             liveCall = (*it);
451             continue;
452         }
453         if (telCallState == TelCallState::CALL_STATUS_ACTIVE) {
454             liveCall = (*it);
455             continue;
456         }
457         if (telCallState == TelCallState::CALL_STATUS_HOLDING) {
458             liveCall = (*it);
459             continue;
460         }
461     }
462     return liveCall;
463 }
464 
DealFailDial(sptr<CallBase> call)465 int32_t CallObjectManager::DealFailDial(sptr<CallBase> call)
466 {
467     CallDetailInfo callDetatilInfo;
468     if (memset_s(&callDetatilInfo, sizeof(CallDetailInfo), 0, sizeof(CallDetailInfo)) != EOK) {
469         TELEPHONY_LOGE("memset_s callDetatilInfo fail");
470         return TELEPHONY_ERR_MEMSET_FAIL;
471     }
472     std::string number = call->GetAccountNumber();
473     callDetatilInfo.callType = call->GetCallType();
474     callDetatilInfo.accountId = call->GetSlotId();
475     callDetatilInfo.state = TelCallState::CALL_STATUS_DISCONNECTED;
476     callDetatilInfo.callMode = call->GetVideoStateType();
477     callDetatilInfo.voiceDomain = static_cast<int32_t>(call->GetCallType());
478     if (number.length() > kMaxNumberLen) {
479         TELEPHONY_LOGE("numbser length out of range");
480         return CALL_ERR_NUMBER_OUT_OF_RANGE;
481     }
482     if (memcpy_s(&callDetatilInfo.phoneNum, kMaxNumberLen, number.c_str(), number.length()) != EOK) {
483         TELEPHONY_LOGE("memcpy_s number failed!");
484         return TELEPHONY_ERR_MEMCPY_FAIL;
485     }
486 
487     return DelayedSingleton<ReportCallInfoHandler>::GetInstance()->UpdateCallReportInfo(callDetatilInfo);
488 }
489 
GetCarrierCallInfoList()490 std::vector<CallAttributeInfo> CallObjectManager::GetCarrierCallInfoList()
491 {
492     std::vector<CallAttributeInfo> callVec;
493     callVec.clear();
494     std::lock_guard<std::mutex> lock(listMutex_);
495     std::list<sptr<CallBase>>::iterator it;
496     for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
497         CallAttributeInfo info;
498         if ((*it) == nullptr) {
499             TELEPHONY_LOGE("call is nullptr");
500             continue;
501         }
502         (*it)->GetCallAttributeInfo(info);
503         if (info.callType != CallType::TYPE_OTT) {
504             callVec.emplace_back(info);
505         }
506     }
507     return callVec;
508 }
509 } // namespace Telephony
510 } // namespace OHOS
511