• 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_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         NativeRdb::ValuesBucket bucket;
49         TELEPHONY_LOGI("callLog Insert begin");
50         bucket.PutString(CALL_PHONE_NUMBER, std::string(info.phoneNumber));
51         bucket.PutString(CALL_DISPLAY_NAME, std::string(""));
52         bucket.PutInt(CALL_DIRECTION, static_cast<int32_t>(info.directionType));
53         bucket.PutString(CALL_VOICEMAIL_URI, std::string(""));
54         bucket.PutInt(CALL_SIM_TYPE, 0);
55         bucket.PutInt(CALL_IS_HD, 0);
56         bucket.PutInt(CALL_IS_READ, 0);
57         bucket.PutInt(CALL_RING_DURATION, info.ringDuration);
58         bucket.PutInt(CALL_TALK_DURATION, info.callDuration);
59         bucket.PutString(CALL_FORMAT_NUMBER, std::string(info.formattedPhoneNumber));
60         bucket.PutString(CALL_QUICKSEARCH_KEY, std::string(""));
61         bucket.PutInt(CALL_NUMBER_TYPE, 0);
62         bucket.PutString(CALL_NUMBER_TYPE_NAME, std::string(""));
63         bucket.PutInt(CALL_BEGIN_TIME, info.callBeginTime);
64         bucket.PutInt(CALL_END_TIME, info.callEndTime);
65         bucket.PutInt(CALL_ANSWER_STATE, static_cast<int32_t>(info.answerType));
66         uint64_t timeStamp = time(0);
67         bucket.PutInt(CALL_CREATE_TIME, timeStamp);
68         bucket.PutString(CALL_NUMBER_LOCATION, std::string(""));
69         bucket.PutInt(CALL_PHOTO_ID, 0);
70         callDataPtr_->Insert(bucket);
71         return;
72     }
73 }
74 
CallRecordsHandlerService()75 CallRecordsHandlerService::CallRecordsHandlerService() : eventLoop_(nullptr), handler_(nullptr) {}
76 
~CallRecordsHandlerService()77 CallRecordsHandlerService::~CallRecordsHandlerService() {}
78 
Start()79 void CallRecordsHandlerService::Start()
80 {
81     eventLoop_ = AppExecFwk::EventRunner::Create("CallRecordsHandlerService");
82     if (eventLoop_.get() == nullptr) {
83         TELEPHONY_LOGE("failed to create EventRunner");
84         return;
85     }
86     handler_ = std::make_shared<CallRecordsHandler>(eventLoop_);
87     if (handler_.get() == nullptr) {
88         TELEPHONY_LOGE("failed to create CallRecordsHandler");
89         return;
90     }
91     eventLoop_->Run();
92     return;
93 }
94 
StoreCallRecord(const CallRecordInfo & info)95 int32_t CallRecordsHandlerService::StoreCallRecord(const CallRecordInfo &info)
96 {
97     if (handler_.get() == nullptr) {
98         TELEPHONY_LOGE("handler_ is nullptr");
99         return TELEPHONY_ERR_LOCAL_PTR_NULL;
100     }
101     std::unique_ptr<CallRecordInfo> para = std::make_unique<CallRecordInfo>();
102     if (para.get() == nullptr) {
103         TELEPHONY_LOGE("make_unique CallRecordInfo failed!");
104         return TELEPHONY_ERR_LOCAL_PTR_NULL;
105     }
106     *para = info;
107     handler_->SendEvent(HANDLER_ADD_CALL_RECORD_INFO, std::move(para));
108     return TELEPHONY_SUCCESS;
109 }
110 } // namespace Telephony
111 } // namespace OHOS
112