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_handler.h"
17
18 #include "call_manager_errors.h"
19
20 namespace OHOS {
21 namespace Telephony {
CallRecordsHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)22 CallRecordsHandler::CallRecordsHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner)
23 : AppExecFwk::EventHandler(runner), callDataPtr_(nullptr)
24 {
25 callDataPtr_ = DelayedSingleton<CallDataBaseHelper>::GetInstance();
26 if (callDataPtr_ == nullptr) {
27 TELEPHONY_LOGE("callDataPtr_ is nullptr!");
28 }
29 }
30
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)31 void CallRecordsHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
32 {
33 if (event == nullptr) {
34 TELEPHONY_LOGE("CallRecordsHandler::ProcessEvent parameter error");
35 return;
36 }
37 if (event->GetInnerEventId() == CallRecordsHandlerService::HANDLER_ADD_CALL_RECORD_INFO) {
38 auto object = event->GetUniqueObject<CallRecordInfo>();
39 if (object == nullptr) {
40 TELEPHONY_LOGE("object is nullptr!");
41 return;
42 }
43 CallRecordInfo info = *object;
44 if (callDataPtr_ == nullptr) {
45 TELEPHONY_LOGE("callDataPtr_ is nullptr!");
46 return;
47 }
48 ContactInfo contactInfo = {
49 .name = "",
50 .number = "",
51 .isContacterExists = false,
52 .ringtonePath = "",
53 .isSendToVoicemail = false,
54 .isEcc = false,
55 .isVoiceMail = false,
56 };
57 QueryCallerInfo(contactInfo, std::string(info.phoneNumber));
58
59 DataShare::DataShareValuesBucket bucket;
60 TELEPHONY_LOGI("callLog Insert begin");
61 bucket.Put(CALL_PHONE_NUMBER, std::string(info.phoneNumber));
62 bucket.Put(CALL_DISPLAY_NAME, std::string(contactInfo.name));
63 bucket.Put(CALL_DIRECTION, static_cast<int32_t>(info.directionType));
64 bucket.Put(CALL_VOICEMAIL_URI, std::string(""));
65 bucket.Put(CALL_SIM_TYPE, 0);
66 bucket.Put(CALL_IS_HD, 0);
67 bucket.Put(CALL_IS_READ, 0);
68 bucket.Put(CALL_RING_DURATION, static_cast<int32_t>(info.ringDuration));
69 bucket.Put(CALL_TALK_DURATION, static_cast<int32_t>(info.callDuration));
70 bucket.Put(CALL_FORMAT_NUMBER, std::string(info.formattedPhoneNumber));
71 bucket.Put(CALL_QUICKSEARCH_KEY, std::string(""));
72 bucket.Put(CALL_NUMBER_TYPE, 0);
73 bucket.Put(CALL_NUMBER_TYPE_NAME, std::string(""));
74 bucket.Put(CALL_BEGIN_TIME, info.callBeginTime);
75 bucket.Put(CALL_END_TIME, info.callEndTime);
76 bucket.Put(CALL_ANSWER_STATE, static_cast<int32_t>(info.answerType));
77 time_t timeStamp = time(0);
78 bucket.Put(CALL_CREATE_TIME, timeStamp);
79 bucket.Put(CALL_NUMBER_LOCATION, std::string(""));
80 bucket.Put(CALL_PHOTO_ID, 0);
81 bucket.Put(CALL_SLOT_ID, info.slotId);
82 callDataPtr_->Insert(bucket);
83 return;
84 }
85 }
86
QueryCallerInfo(ContactInfo & contactInfo,std::string phoneNumber)87 void CallRecordsHandler::QueryCallerInfo(ContactInfo &contactInfo, std::string phoneNumber)
88 {
89 std::shared_ptr<CallDataBaseHelper> callDataPtr = DelayedSingleton<CallDataBaseHelper>::GetInstance();
90 if (callDataPtr == nullptr) {
91 TELEPHONY_LOGE("callDataPtr is nullptr!");
92 return;
93 }
94 DataShare::DataSharePredicates predicates;
95 predicates.EqualTo(CALL_DETAIL_INFO, phoneNumber);
96 predicates.And();
97 predicates.EqualTo(CALL_CONTENT_TYPE, CALL_PHONE);
98 bool ret = callDataPtr->Query(contactInfo, predicates);
99 if (!ret) {
100 TELEPHONY_LOGE("Query contact database fail!");
101 }
102 }
103
CallRecordsHandlerService()104 CallRecordsHandlerService::CallRecordsHandlerService() : eventLoop_(nullptr), handler_(nullptr) {}
105
~CallRecordsHandlerService()106 CallRecordsHandlerService::~CallRecordsHandlerService() {}
107
Start()108 void CallRecordsHandlerService::Start()
109 {
110 eventLoop_ = AppExecFwk::EventRunner::Create("CallRecordsHandlerService");
111 if (eventLoop_.get() == nullptr) {
112 TELEPHONY_LOGE("failed to create EventRunner");
113 return;
114 }
115 handler_ = std::make_shared<CallRecordsHandler>(eventLoop_);
116 if (handler_.get() == nullptr) {
117 TELEPHONY_LOGE("failed to create CallRecordsHandler");
118 return;
119 }
120 eventLoop_->Run();
121 return;
122 }
123
StoreCallRecord(const CallRecordInfo & info)124 int32_t CallRecordsHandlerService::StoreCallRecord(const CallRecordInfo &info)
125 {
126 if (handler_.get() == nullptr) {
127 TELEPHONY_LOGE("handler_ is nullptr");
128 return TELEPHONY_ERR_LOCAL_PTR_NULL;
129 }
130 std::unique_ptr<CallRecordInfo> para = std::make_unique<CallRecordInfo>();
131 if (para.get() == nullptr) {
132 TELEPHONY_LOGE("make_unique CallRecordInfo failed!");
133 return TELEPHONY_ERR_LOCAL_PTR_NULL;
134 }
135 *para = info;
136 handler_->SendEvent(HANDLER_ADD_CALL_RECORD_INFO, std::move(para));
137 return TELEPHONY_SUCCESS;
138 }
139 } // namespace Telephony
140 } // namespace OHOS
141