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_records_manager.h"
17
18 #include "securec.h"
19 #include "call_manager_inner_type.h"
20 #include "call_number_utils.h"
21
22 namespace OHOS {
23 namespace Telephony {
24 constexpr int16_t DEFAULT_COUNTRY_CODE = 0;
25 constexpr int16_t DEFAULT_TIME = 0;
CallRecordsManager()26 CallRecordsManager::CallRecordsManager() : callRecordsHandlerServerPtr_(nullptr) {}
27
~CallRecordsManager()28 CallRecordsManager::~CallRecordsManager() {}
29
Init()30 void CallRecordsManager::Init()
31 {
32 callRecordsHandlerServerPtr_ = DelayedSingleton<CallRecordsHandlerService>::GetInstance();
33 if (callRecordsHandlerServerPtr_ == nullptr) {
34 TELEPHONY_LOGE("call record manager init failure.");
35 return;
36 }
37 callRecordsHandlerServerPtr_->Start();
38 }
39
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)40 void CallRecordsManager::CallStateUpdated(
41 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
42 {
43 CallAttributeInfo info;
44 if (nextState != TelCallState::CALL_STATUS_DISCONNECTED) {
45 TELEPHONY_LOGE("nextState not CALL_STATUS_DISCONNECTED");
46 return;
47 }
48 if (callObjectPtr == nullptr) {
49 TELEPHONY_LOGE("call object is nullptr");
50 return;
51 }
52 (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
53 callObjectPtr->GetCallAttributeBaseInfo(info);
54 AddOneCallRecord(info);
55 }
56
AddOneCallRecord(sptr<CallBase> call,CallAnswerType answerType)57 void CallRecordsManager::AddOneCallRecord(sptr<CallBase> call, CallAnswerType answerType)
58 {
59 CallAttributeInfo info;
60 (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
61 call->GetCallAttributeBaseInfo(info);
62 AddOneCallRecord(info);
63 }
64
AddOneCallRecord(CallAttributeInfo & info)65 void CallRecordsManager::AddOneCallRecord(CallAttributeInfo &info)
66 {
67 CallRecordInfo data;
68 (void)memset_s(&data, sizeof(CallRecordInfo), 0, sizeof(CallRecordInfo));
69 if (callRecordsHandlerServerPtr_ == nullptr) {
70 TELEPHONY_LOGE("callRecordsHandlerServerPtr_ is nullptr");
71 return;
72 }
73 if (strlen(info.accountNumber) > static_cast<size_t>(kMaxNumberLen)) {
74 TELEPHONY_LOGE("Number out of limit!");
75 return;
76 }
77 errno_t result = memcpy_s(data.phoneNumber, kMaxNumberLen, info.accountNumber, strlen(info.accountNumber));
78 if (result != EOK) {
79 TELEPHONY_LOGE("memcpy_s failed!");
80 return;
81 }
82 if ((info.callBeginTime == DEFAULT_TIME) || (info.callEndTime == DEFAULT_TIME)) {
83 data.callDuration = DEFAULT_TIME;
84 } else {
85 data.callDuration = difftime(info.callEndTime, info.callBeginTime);
86 }
87 if ((info.ringBeginTime == DEFAULT_TIME) || (info.ringEndTime == DEFAULT_TIME)) {
88 data.ringDuration = DEFAULT_TIME;
89 } else {
90 data.ringDuration = difftime(info.ringEndTime, info.ringBeginTime);
91 }
92 data.callId = info.callId;
93 data.callBeginTime = info.callBeginTime;
94 data.callEndTime = info.callEndTime;
95 data.directionType = info.callDirection;
96 data.answerType = info.answerType;
97 data.countryCode = DEFAULT_COUNTRY_CODE;
98 data.slotId = info.accountId;
99 std::string tmpStr("");
100 (void)DelayedSingleton<CallNumberUtils>::GetInstance()->FormatPhoneNumber(
101 std::string(data.phoneNumber), "CN", tmpStr);
102 if (tmpStr.length() > static_cast<size_t>(kMaxNumberLen)) {
103 TELEPHONY_LOGE("Number out of limit!");
104 return;
105 }
106 if (memcpy_s(data.formattedPhoneNumber, kMaxNumberLen, tmpStr.c_str(), tmpStr.length()) != 0) {
107 TELEPHONY_LOGE("memcpy_s failed!");
108 return;
109 }
110 callRecordsHandlerServerPtr_->StoreCallRecord(data);
111 }
112 } // namespace Telephony
113 } // namespace OHOS