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_manager_errors.h"
19 #include "telephony_log_wrapper.h"
20 #include "call_connect_ability.h"
21
22 namespace OHOS {
23 namespace Telephony {
24 std::list<sptr<CallBase>> CallObjectManager::callObjectPtrList_;
25 std::mutex CallObjectManager::listMutex_;
26 int32_t CallObjectManager::callId_ = CALL_START_ID;
27
CallObjectManager()28 CallObjectManager::CallObjectManager()
29 {
30 }
31
~CallObjectManager()32 CallObjectManager::~CallObjectManager()
33 {
34 std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
35 while (it != callObjectPtrList_.end()) {
36 (*it) = nullptr;
37 callObjectPtrList_.erase(it++);
38 }
39 }
40
AddOneCallObject(sptr<CallBase> & call)41 int32_t CallObjectManager::AddOneCallObject(sptr<CallBase> &call)
42 {
43 if (call == nullptr) {
44 return TELEPHONY_ERR_LOCAL_PTR_NULL;
45 }
46 std::lock_guard<std::mutex> lock(listMutex_);
47 std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
48 for (; it != callObjectPtrList_.end(); ++it) {
49 if ((*it)->GetCallID() == call->GetCallID()) {
50 TELEPHONY_LOGE("this call has existed yet!");
51 return CALL_ERR_PHONE_CALL_ALREADY_EXISTS;
52 }
53 }
54 if (callObjectPtrList_.size() == 0) {
55 CallAttributeInfo info;
56 call->GetCallAttributeInfo(info);
57 DelayedSingleton<CallConnectAbility>::GetInstance()->ConnectAbility(info);
58 }
59 callObjectPtrList_.emplace_back(call);
60 TELEPHONY_LOGI("AddOneCallObject success! callId:%{public}d,call list size:%{public}zu", call->GetCallID(),
61 callObjectPtrList_.size());
62 return TELEPHONY_SUCCESS;
63 }
64
DeleteOneCallObject(int32_t callId)65 int32_t CallObjectManager::DeleteOneCallObject(int32_t callId)
66 {
67 std::lock_guard<std::mutex> lock(listMutex_);
68 std::list<sptr<CallBase>>::iterator it;
69 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
70 if ((*it)->GetCallID() == callId) {
71 callObjectPtrList_.erase(it);
72 TELEPHONY_LOGI("DeleteOneCallObject success! call list size:%{public}zu", callObjectPtrList_.size());
73 break;
74 }
75 }
76 if (callObjectPtrList_.size() == 0) {
77 DelayedSingleton<CallConnectAbility>::GetInstance()->DisconnectAbility();
78 }
79 return TELEPHONY_SUCCESS;
80 }
81
DeleteOneCallObject(sptr<CallBase> & call)82 void CallObjectManager::DeleteOneCallObject(sptr<CallBase> &call)
83 {
84 if (call == nullptr) {
85 TELEPHONY_LOGE("call is null!");
86 return;
87 }
88 std::lock_guard<std::mutex> lock(listMutex_);
89 callObjectPtrList_.remove(call);
90 if (callObjectPtrList_.size() == 0) {
91 DelayedSingleton<CallConnectAbility>::GetInstance()->DisconnectAbility();
92 }
93 TELEPHONY_LOGI("DeleteOneCallObject success! callList size:%{public}zu", callObjectPtrList_.size());
94 }
95
GetOneCallObject(int32_t callId)96 sptr<CallBase> CallObjectManager::GetOneCallObject(int32_t callId)
97 {
98 sptr<CallBase> retPtr = nullptr;
99 std::lock_guard<std::mutex> lock(listMutex_);
100 std::list<sptr<CallBase>>::iterator it = CallObjectManager::callObjectPtrList_.begin();
101 for (; it != callObjectPtrList_.end(); ++it) {
102 if ((*it)->GetCallID() == callId) {
103 retPtr = *it;
104 break;
105 }
106 }
107 return retPtr;
108 }
109
GetOneCallObject(std::string & phoneNumber)110 sptr<CallBase> CallObjectManager::GetOneCallObject(std::string &phoneNumber)
111 {
112 if (phoneNumber.empty()) {
113 TELEPHONY_LOGE("call is null!");
114 return nullptr;
115 }
116 sptr<CallBase> retPtr = nullptr;
117 std::lock_guard<std::mutex> lock(listMutex_);
118 std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
119 for (; it != callObjectPtrList_.end(); ++it) {
120 if ((*it)->GetAccountNumber() == phoneNumber) {
121 TELEPHONY_LOGI("GetOneCallObject success!");
122 retPtr = *it;
123 break;
124 }
125 }
126 return retPtr;
127 }
128
HasNewCall()129 int32_t CallObjectManager::HasNewCall()
130 {
131 std::lock_guard<std::mutex> lock(listMutex_);
132 std::list<sptr<CallBase>>::iterator it;
133 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
134 if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CREATE ||
135 (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CONNECTING ||
136 (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_DIALING) {
137 TELEPHONY_LOGE("there is already a new call[callId:%{public}d,state:%{public}d], please redial later",
138 (*it)->GetCallID(), (*it)->GetCallRunningState());
139 return CALL_ERR_DIAL_IS_BUSY;
140 }
141 }
142 return TELEPHONY_SUCCESS;
143 }
144
IsNewCallAllowedCreate(bool & enabled)145 int32_t CallObjectManager::IsNewCallAllowedCreate(bool &enabled)
146 {
147 enabled = true;
148 std::lock_guard<std::mutex> lock(listMutex_);
149 std::list<sptr<CallBase>>::iterator it;
150 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
151 if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CREATE ||
152 (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_CONNECTING ||
153 (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_DIALING ||
154 (*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
155 TELEPHONY_LOGE("there is already a new call, please redial later");
156 enabled = false;
157 break;
158 }
159 }
160
161 return TELEPHONY_ERR_SUCCESS;
162 }
163
GetCarrierCallList(std::list<int32_t> & list)164 int32_t CallObjectManager::GetCarrierCallList(std::list<int32_t> &list)
165 {
166 list.clear();
167 std::lock_guard<std::mutex> lock(listMutex_);
168 std::list<sptr<CallBase>>::iterator it;
169 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
170 if ((*it)->GetCallType() == CallType::TYPE_CS || (*it)->GetCallType() == CallType::TYPE_IMS) {
171 list.emplace_back((*it)->GetCallID());
172 }
173 }
174 return TELEPHONY_SUCCESS;
175 }
176
HasRingingMaximum()177 bool CallObjectManager::HasRingingMaximum()
178 {
179 int32_t ringingCount = 0;
180 std::lock_guard<std::mutex> lock(listMutex_);
181 std::list<sptr<CallBase>>::iterator it;
182 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
183 // Count the number of calls in the ringing state
184 if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
185 ringingCount++;
186 }
187 }
188 if (ringingCount >= RINGING_CALL_NUMBER_LEN) {
189 return true;
190 }
191 return false;
192 }
193
HasDialingMaximum()194 bool CallObjectManager::HasDialingMaximum()
195 {
196 int32_t dialingCount = 0;
197 std::lock_guard<std::mutex> lock(listMutex_);
198 std::list<sptr<CallBase>>::iterator it;
199 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
200 // Count the number of calls in the active state
201 if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_ACTIVE) {
202 dialingCount++;
203 }
204 }
205 if (dialingCount >= DIALING_CALL_NUMBER_LEN) {
206 return true;
207 }
208 return false;
209 }
210
HasEmergencyCall(bool & enabled)211 int32_t CallObjectManager::HasEmergencyCall(bool &enabled)
212 {
213 enabled = false;
214 std::lock_guard<std::mutex> lock(listMutex_);
215 std::list<sptr<CallBase>>::iterator it;
216 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
217 if ((*it)->GetEmergencyState()) {
218 enabled = true;
219 }
220 }
221 return TELEPHONY_ERR_SUCCESS;
222 }
223
GetNewCallId()224 int32_t CallObjectManager::GetNewCallId()
225 {
226 int32_t ret = 0;
227 std::lock_guard<std::mutex> lock(listMutex_);
228 ret = ++callId_;
229 return ret;
230 }
231
IsCallExist(int32_t callId)232 bool CallObjectManager::IsCallExist(int32_t callId)
233 {
234 std::lock_guard<std::mutex> lock(listMutex_);
235 std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
236 for (; it != callObjectPtrList_.end(); ++it) {
237 if ((*it)->GetCallID() == callId) {
238 TELEPHONY_LOGW("the call is exist.");
239 return true;
240 }
241 }
242 return false;
243 }
244
IsCallExist(std::string & phoneNumber)245 bool CallObjectManager::IsCallExist(std::string &phoneNumber)
246 {
247 if (phoneNumber.empty()) {
248 return false;
249 }
250 std::lock_guard<std::mutex> lock(listMutex_);
251 std::list<sptr<CallBase>>::iterator it = callObjectPtrList_.begin();
252 for (; it != callObjectPtrList_.end(); ++it) {
253 if ((*it)->GetAccountNumber() == phoneNumber) {
254 return true;
255 }
256 }
257 TELEPHONY_LOGI("the call is does not exist.");
258 return false;
259 }
260
HasCallExist()261 bool CallObjectManager::HasCallExist()
262 {
263 std::lock_guard<std::mutex> lock(listMutex_);
264 if (callObjectPtrList_.empty()) {
265 TELEPHONY_LOGI("call list size:%{public}zu", callObjectPtrList_.size());
266 return false;
267 }
268 return true;
269 }
270
HasRingingCall(bool & enabled)271 int32_t CallObjectManager::HasRingingCall(bool &enabled)
272 {
273 enabled = false;
274 std::lock_guard<std::mutex> lock(listMutex_);
275 std::list<sptr<CallBase>>::iterator it;
276 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
277 // Count the number of calls in the ringing state
278 if ((*it)->GetCallRunningState() == CallRunningState::CALL_RUNNING_STATE_RINGING) {
279 enabled = true;
280 break;
281 }
282 }
283 return TELEPHONY_ERR_SUCCESS;
284 }
285
GetCallState(int32_t callId)286 TelCallState CallObjectManager::GetCallState(int32_t callId)
287 {
288 TelCallState retState = TelCallState::CALL_STATUS_IDLE;
289 std::lock_guard<std::mutex> lock(listMutex_);
290 std::list<sptr<CallBase>>::iterator it = CallObjectManager::callObjectPtrList_.begin();
291 for (; it != callObjectPtrList_.end(); ++it) {
292 if ((*it)->GetCallID() == callId) {
293 retState = (*it)->GetTelCallState();
294 break;
295 }
296 }
297 return retState;
298 }
299
GetOneCallObject(CallRunningState callState)300 sptr<CallBase> CallObjectManager::GetOneCallObject(CallRunningState callState)
301 {
302 std::lock_guard<std::mutex> lock(listMutex_);
303 std::list<sptr<CallBase>>::reverse_iterator it;
304 for (it = callObjectPtrList_.rbegin(); it != callObjectPtrList_.rend(); ++it) {
305 if ((*it)->GetCallRunningState() == callState) {
306 return (*it);
307 }
308 }
309 return nullptr;
310 }
311
IsCallExist(CallType callType,TelCallState callState)312 bool CallObjectManager::IsCallExist(CallType callType, TelCallState callState)
313 {
314 std::lock_guard<std::mutex> lock(listMutex_);
315 std::list<sptr<CallBase>>::iterator it;
316 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
317 if ((*it)->GetCallType() == callType && (*it)->GetTelCallState() == callState) {
318 return true;
319 }
320 }
321 TELEPHONY_LOGI("the call is does not exist.");
322 return false;
323 }
324
IsCallExist(TelCallState callState)325 bool CallObjectManager::IsCallExist(TelCallState callState)
326 {
327 std::lock_guard<std::mutex> lock(listMutex_);
328 std::list<sptr<CallBase>>::iterator it;
329 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
330 if ((*it)->GetTelCallState() == callState) {
331 return true;
332 }
333 }
334 TELEPHONY_LOGI("the call is does not exist.");
335 return false;
336 }
337
IsCallExist(TelCallState callState,int32_t & callId)338 bool CallObjectManager::IsCallExist(TelCallState callState, int32_t &callId)
339 {
340 std::lock_guard<std::mutex> lock(listMutex_);
341 std::list<sptr<CallBase>>::iterator it;
342 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
343 if ((*it)->GetTelCallState() == callState) {
344 callId = (*it)->GetCallID();
345 return true;
346 }
347 }
348 TELEPHONY_LOGI("the call is does not exist.");
349 return false;
350 }
351
IsConferenceCallExist(TelConferenceState state,int32_t & callId)352 bool CallObjectManager::IsConferenceCallExist(TelConferenceState state, int32_t &callId)
353 {
354 std::lock_guard<std::mutex> lock(listMutex_);
355 std::list<sptr<CallBase>>::iterator it;
356 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
357 if ((*it)->GetTelConferenceState() == state) {
358 callId = (*it)->GetCallID();
359 return true;
360 }
361 }
362 TELEPHONY_LOGI("the call is does not exist.");
363 return false;
364 }
365
GetCallNum(TelCallState callState)366 int32_t CallObjectManager::GetCallNum(TelCallState callState)
367 {
368 int32_t num = 0;
369 std::lock_guard<std::mutex> lock(listMutex_);
370 std::list<sptr<CallBase>>::iterator it;
371 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
372 if ((*it)->GetTelCallState() == callState) {
373 ++num;
374 continue;
375 }
376 }
377 TELEPHONY_LOGI("callState:%{public}d, num:%{public}d", callState, num);
378 return num;
379 }
380
GetCallNumber(TelCallState callState)381 std::string CallObjectManager::GetCallNumber(TelCallState callState)
382 {
383 std::string number = "";
384 std::lock_guard<std::mutex> lock(listMutex_);
385 std::list<sptr<CallBase>>::iterator it;
386 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
387 if ((*it)->GetTelCallState() == callState) {
388 number = (*it)->GetAccountNumber();
389 break;
390 }
391 }
392 return number;
393 }
394
GetCallInfoList(int32_t slotId)395 std::vector<CallAttributeInfo> CallObjectManager::GetCallInfoList(int32_t slotId)
396 {
397 std::vector<CallAttributeInfo> callVec;
398 CallAttributeInfo info;
399 callVec.clear();
400 std::lock_guard<std::mutex> lock(listMutex_);
401 std::list<sptr<CallBase>>::iterator it;
402 for (it = callObjectPtrList_.begin(); it != callObjectPtrList_.end(); ++it) {
403 (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
404 (*it)->GetCallAttributeInfo(info);
405 if (info.accountId == slotId && info.callType != CallType::TYPE_OTT) {
406 callVec.emplace_back(info);
407 }
408 }
409 return callVec;
410 }
411 } // namespace Telephony
412 } // namespace OHOS
413