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_base.h"
19 #include "call_manager_errors.h"
20 #include "call_manager_inner_type.h"
21 #include "ffrt.h"
22 #include "call_number_utils.h"
23
24 namespace OHOS {
25 namespace Telephony {
CallRecordsHandler()26 CallRecordsHandler::CallRecordsHandler() : callDataPtr_(nullptr)
27 {
28 callDataPtr_ = DelayedSingleton<CallDataBaseHelper>::GetInstance();
29 if (callDataPtr_ == nullptr) {
30 TELEPHONY_LOGE("callDataPtr_ is nullptr!");
31 }
32 }
33
AddCallLogInfo(const CallRecordInfo & info)34 int32_t CallRecordsHandler::AddCallLogInfo(const CallRecordInfo &info)
35 {
36 if (callDataPtr_ == nullptr) {
37 TELEPHONY_LOGE("callDataPtr is nullptr!");
38 return TELEPHONY_ERR_LOCAL_PTR_NULL;
39 }
40 std::string numberLocation = CheckNumberLocationInfo(info);
41 std::string displayName = "";
42 if (info.numberMarkInfo.markType == MarkType::MARK_TYPE_YELLOW_PAGE && !info.numberMarkInfo.isCloud) {
43 displayName = std::string(info.numberMarkInfo.markContent);
44 } else {
45 displayName = info.name;
46 }
47
48 DataShare::DataShareValuesBucket bucket;
49 TELEPHONY_LOGI("callLog Insert begin.");
50 MakeCallLogInsertBucket(bucket, info, displayName, numberLocation);
51 bool ret = callDataPtr_->Insert(bucket);
52 if (!ret) {
53 TELEPHONY_LOGE("add call log database fail!");
54 return TELEPHONY_ERR_DATABASE_WRITE_FAIL;
55 }
56 TELEPHONY_LOGI("callLog Insert success.");
57 DeleteCallLogForLimit(info);
58 return TELEPHONY_SUCCESS;
59 }
60
DeleteCallLogForLimit(const CallRecordInfo & info)61 void CallRecordsHandler::DeleteCallLogForLimit(const CallRecordInfo &info)
62 {
63 DataShare::DataSharePredicates queryPredicates;
64 if (info.answerType == CallAnswerType::CALL_ANSWER_BLOCKED) {
65 queryPredicates.EqualTo(CALL_ANSWER_STATE, static_cast<int32_t>(CallAnswerType::CALL_ANSWER_BLOCKED));
66 } else {
67 queryPredicates.NotEqualTo(CALL_ANSWER_STATE, static_cast<int32_t>(CallAnswerType::CALL_ANSWER_BLOCKED));
68 }
69 queryPredicates.OrderByDesc(CALL_CREATE_TIME);
70 queryPredicates.Limit(-1, LOG_LIMIT_NUM);
71 callDataPtr_->QueryAndDeleteLimitedIds(queryPredicates);
72 }
73
MakeCallLogInsertBucket(DataShare::DataShareValuesBucket & bucket,const CallRecordInfo & info,std::string displayName,std::string numberLocation)74 void CallRecordsHandler::MakeCallLogInsertBucket(DataShare::DataShareValuesBucket &bucket,
75 const CallRecordInfo &info, std::string displayName, std::string numberLocation)
76 {
77 bucket.Put(CALL_PHONE_NUMBER, std::string(info.phoneNumber));
78 bucket.Put(CALL_DISPLAY_NAME, displayName);
79 bucket.Put(CALL_DIRECTION, static_cast<int32_t>(info.directionType));
80 bucket.Put(CALL_VOICEMAIL_URI, std::string(""));
81 bucket.Put(CALL_SIM_TYPE, 0);
82 bucket.Put(CALL_IS_HD, static_cast<int32_t>(info.callType));
83 bucket.Put(CALL_IS_READ, 0);
84 bucket.Put(CALL_RING_DURATION, static_cast<int32_t>(info.ringDuration));
85 bucket.Put(CALL_TALK_DURATION, static_cast<int32_t>(info.callDuration));
86 bucket.Put(CALL_FORMAT_NUMBER, std::string(info.formattedNumber));
87 bucket.Put(CALL_QUICKSEARCH_KEY, std::string(""));
88 bucket.Put(CALL_NUMBER_TYPE, 0);
89 bucket.Put(CALL_NUMBER_TYPE_NAME, std::string(""));
90 bucket.Put(CALL_BEGIN_TIME, info.callBeginTime);
91 bucket.Put(CALL_END_TIME, info.callEndTime);
92 bucket.Put(CALL_ANSWER_STATE, static_cast<int32_t>(info.answerType));
93 bucket.Put(CALL_CREATE_TIME, info.callCreateTime);
94 bucket.Put(CALL_NUMBER_LOCATION, numberLocation);
95 bucket.Put(CALL_PHOTO_ID, 0);
96 bucket.Put(CALL_SLOT_ID, info.slotId);
97 bucket.Put(CALL_FEATURES, info.features);
98 bucket.Put(CALL_IS_CNAP, info.namePresentation);
99 }
100
CheckNumberLocationInfo(const CallRecordInfo & info)101 std::string CallRecordsHandler::CheckNumberLocationInfo(const CallRecordInfo &info)
102 {
103 std::string str(info.numberLocation);
104 if (str == "default") {
105 TELEPHONY_LOGI("AddCallLogInfo, number location is default");
106 str = "";
107 DelayedSingleton<CallNumberUtils>::GetInstance()->QueryNumberLocationInfo(str, std::string(info.phoneNumber));
108 }
109 if (str == "") {
110 str = "N";
111 }
112 return str;
113 }
114
QueryAndNotifyUnReadMissedCall()115 int32_t CallRecordsHandler::QueryAndNotifyUnReadMissedCall()
116 {
117 if (callDataPtr_ == nullptr) {
118 TELEPHONY_LOGE("callDataPtr is nullptr!");
119 return TELEPHONY_ERR_LOCAL_PTR_NULL;
120 }
121 missedCallNotification_ = std::make_shared<MissedCallNotification>();
122 if (missedCallNotification_ == nullptr) {
123 TELEPHONY_LOGE("missedCallNotification_ is null!");
124 return TELEPHONY_ERR_LOCAL_PTR_NULL;
125 }
126 DataShare::DataSharePredicates predicates;
127 std::map<std::string, int32_t> phoneNumAndUnreadCountMap;
128 predicates.EqualTo(CALL_IS_READ, static_cast<int32_t>(CallLogReadState::CALL_IS_UNREAD));
129 predicates.And();
130 predicates.EqualTo(CALL_DIRECTION, static_cast<int32_t>(CallDirection::CALL_DIRECTION_IN));
131 predicates.And();
132 predicates.EqualTo(CALL_ANSWER_STATE, static_cast<int32_t>(CallAnswerType::CALL_ANSWER_MISSED));
133 bool ret = callDataPtr_->QueryCallLog(phoneNumAndUnreadCountMap, predicates);
134 if (phoneNumAndUnreadCountMap.empty() || !ret) {
135 TELEPHONY_LOGE("Don't have unread missed call in call log!");
136 return TELEPHONY_ERR_DATABASE_READ_FAIL;
137 }
138 int32_t result = missedCallNotification_->NotifyUnReadMissedCall(phoneNumAndUnreadCountMap);
139 if (result != TELEPHONY_SUCCESS) {
140 TELEPHONY_LOGE("Notify unread missed call error!");
141 return TELEPHONY_ERR_PUBLISH_BROADCAST_FAIL;
142 }
143 return TELEPHONY_SUCCESS;
144 }
145
CallRecordsHandlerService()146 CallRecordsHandlerService::CallRecordsHandlerService() : handler_(nullptr) {}
147
~CallRecordsHandlerService()148 CallRecordsHandlerService::~CallRecordsHandlerService() {}
149
Start()150 void CallRecordsHandlerService::Start()
151 {
152 handler_ = std::make_shared<CallRecordsHandler>();
153 return;
154 }
155
StoreCallRecord(const CallRecordInfo & info)156 int32_t CallRecordsHandlerService::StoreCallRecord(const CallRecordInfo &info)
157 {
158 if (handler_.get() == nullptr) {
159 TELEPHONY_LOGE("handler_ is nullptr");
160 return TELEPHONY_ERR_LOCAL_PTR_NULL;
161 }
162 ffrt::submit([=]() { handler_->AddCallLogInfo(info); });
163 return TELEPHONY_SUCCESS;
164 }
165
RemoveMissedIncomingCallNotification()166 int32_t CallRecordsHandlerService::RemoveMissedIncomingCallNotification()
167 {
168 std::shared_ptr<CallDataBaseHelper> callDataPtr = DelayedSingleton<CallDataBaseHelper>::GetInstance();
169 if (callDataPtr == nullptr) {
170 TELEPHONY_LOGE("callDataPtr is nullptr!");
171 return TELEPHONY_ERR_LOCAL_PTR_NULL;
172 }
173 DataShare::DataSharePredicates predicates;
174 DataShare::DataShareValuesBucket bucket;
175 bucket.Put(CALL_IS_READ, static_cast<int32_t>(CallLogReadState::CALL_IS_READ));
176 predicates.EqualTo(CALL_IS_READ, static_cast<int32_t>(CallLogReadState::CALL_IS_UNREAD));
177 predicates.And();
178 predicates.EqualTo(CALL_DIRECTION, static_cast<int32_t>(CallDirection::CALL_DIRECTION_IN));
179 predicates.And();
180 predicates.EqualTo(CALL_ANSWER_STATE, static_cast<int32_t>(CallAnswerType::CALL_ANSWER_MISSED));
181 bool ret = callDataPtr->Update(predicates, bucket);
182 if (ret) {
183 TELEPHONY_LOGE("Update call log database fail!");
184 return TELEPHONY_ERR_DATABASE_WRITE_FAIL;
185 }
186 TELEPHONY_LOGI("Update call log database success!");
187 return TELEPHONY_SUCCESS;
188 }
189
QueryUnReadMissedCallLog()190 int32_t CallRecordsHandlerService::QueryUnReadMissedCallLog()
191 {
192 if (handler_.get() == nullptr) {
193 TELEPHONY_LOGE("handler_ is nullptr");
194 return TELEPHONY_ERR_LOCAL_PTR_NULL;
195 }
196 ffrt::submit([=]() { handler_->QueryAndNotifyUnReadMissedCall(); });
197 return TELEPHONY_SUCCESS;
198 }
199 } // namespace Telephony
200 } // namespace OHOS
201