• 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_manager.h"
17 
18 #include "call_manager_inner_type.h"
19 #include "call_number_utils.h"
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 #include "os_account_manager_wrapper.h"
23 #include "parameter.h"
24 #include "securec.h"
25 #include "call_earthquake_alarm_subscriber.h"
26 #include <regex>
27 #include "telephony_cust_wrapper.h"
28 #include "settings_datashare_helper.h"
29 #include "call_manager_utils.h"
30 #ifdef SUPPORT_MUTE_BY_DATABASE
31 #include "interoperable_settings_handler.h"
32 #endif
33 
34 namespace OHOS {
35 namespace Telephony {
36 constexpr const char *PROP_NETWORK_COUNTRY_ISO = "telephony.operator.iso-country";
37 constexpr const char *DEFAULT_NETWORK_COUNTRY = "CN";
38 constexpr int16_t DEFAULT_COUNTRY_CODE = 0;
39 constexpr int16_t DEFAULT_TIME = 0;
40 const int32_t ACTIVE_USER_ID = 100;
41 const uint32_t FEATURES_VIDEO = 1 << 0;
42 const int32_t PROP_SYSPARA_SIZE = 128;
43 const char *FORMAT_PATTERN = ",|;";
44 const char *MARK_SOURCE_OF_ANTIFRAUT_CENTER = "5";
45 const char *MARK_SOURCE_OF_OTHERS = "3";
46 const std::string SETTINGS_ANTIFRAUD_CENTER_SWITCH = "";
47 const std::string TELEPHONY_IDENTITY_SWITCH = "";
CallRecordsManager()48 CallRecordsManager::CallRecordsManager() : callRecordsHandlerServerPtr_(nullptr) {}
49 
~CallRecordsManager()50 CallRecordsManager::~CallRecordsManager()
51 {
52     if (statusChangeListener_ != nullptr) {
53         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54         if (samgrProxy != nullptr) {
55             samgrProxy->UnSubscribeSystemAbility(OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener_);
56             statusChangeListener_ = nullptr;
57         }
58     }
59     if (dataShareReadySubscriber_ != nullptr) {
60         bool subRet = CommonEventManager::UnSubscribeCommonEvent(dataShareReadySubscriber_);
61         if (!subRet) {
62             TELEPHONY_LOGE("UnSubscribe data share ready event failed!");
63         }
64         dataShareReadySubscriber_ = nullptr;
65     }
66 }
67 
Init()68 void CallRecordsManager::Init()
69 {
70     callRecordsHandlerServerPtr_ = DelayedSingleton<CallRecordsHandlerService>::GetInstance();
71     if (callRecordsHandlerServerPtr_ == nullptr) {
72         TELEPHONY_LOGE("call record manager init failure.");
73         return;
74     }
75     callRecordsHandlerServerPtr_->Start();
76     statusChangeListener_ = new (std::nothrow) AccountSystemAbilityListener();
77     if (statusChangeListener_ == nullptr) {
78         TELEPHONY_LOGE("failed to create statusChangeListener");
79         return;
80     }
81     RegisterDataShareReadySubscriber();
82     auto managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
83     if (managerPtr == nullptr) {
84         TELEPHONY_LOGE("get system ability manager error");
85         return;
86     }
87     int32_t ret = managerPtr->SubscribeSystemAbility(OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener_);
88     if (ret != TELEPHONY_SUCCESS) {
89         TELEPHONY_LOGE("failed to subscribe account manager service SA!");
90         return;
91     }
92 }
93 
RegisterDataShareReadySubscriber()94 void CallRecordsManager::RegisterDataShareReadySubscriber()
95 {
96     MatchingSkills matchingSkills;
97     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY);
98     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
99     subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
100     dataShareReadySubscriber_ = std::make_shared<DataShareReadyEventSubscriber>(subscriberInfo);
101     bool subRet = CommonEventManager::SubscribeCommonEvent(dataShareReadySubscriber_);
102     if (!subRet) {
103         TELEPHONY_LOGE("Subscribe data share ready event failed!");
104     }
105 }
106 
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)107 void CallRecordsManager::CallStateUpdated(
108     sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
109 {
110     if (callObjectPtr != nullptr && callObjectPtr->GetCallType() == CallType::TYPE_VOIP) {
111         TELEPHONY_LOGI("Voip call should not save cellular call records");
112         return;
113     }
114     CallAttributeInfo info;
115     if (nextState != TelCallState::CALL_STATUS_DISCONNECTED) {
116         return;
117     }
118     if (callObjectPtr == nullptr) {
119         TELEPHONY_LOGE("call object is nullptr");
120         return;
121     }
122     (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
123     callObjectPtr->GetCallAttributeBaseInfo(info);
124     ContactInfo contactInfo = callObjectPtr->GetCallerInfo();
125     if (contactInfo.name.empty() && !info.name.empty()) {
126         info.namePresentation = 1;
127     } else {
128         info.namePresentation = 0;
129     }
130     if (info.numberMarkInfo.markType == MarkType::MARK_TYPE_DEFAULT) {
131         TELEPHONY_LOGI("markType is default.");
132         info.numberMarkInfo.markType = MarkType::MARK_TYPE_NONE;
133     }
134     AddOneCallRecord(info);
135 }
136 
AddOneCallRecord(sptr<CallBase> call,CallAnswerType answerType)137 void CallRecordsManager::AddOneCallRecord(sptr<CallBase> call, CallAnswerType answerType)
138 {
139     CallAttributeInfo info;
140     (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
141     call->GetCallAttributeBaseInfo(info);
142     if (info.numberMarkInfo.markType == MarkType::MARK_TYPE_DEFAULT) {
143         TELEPHONY_LOGI("markType is default.");
144         info.numberMarkInfo.markType = MarkType::MARK_TYPE_NONE;
145     }
146     AddOneCallRecord(info);
147 }
148 
AddOneCallRecord(CallAttributeInfo & info)149 void CallRecordsManager::AddOneCallRecord(CallAttributeInfo &info)
150 {
151     CallRecordInfo data;
152     (void)memset_s(&data, sizeof(CallRecordInfo), 0, sizeof(CallRecordInfo));
153     if (callRecordsHandlerServerPtr_ == nullptr) {
154         TELEPHONY_LOGE("callRecordsHandlerServerPtr_ is nullptr");
155         return;
156     }
157     if (strlen(info.accountNumber) > static_cast<size_t>(kMaxNumberLen)) {
158         TELEPHONY_LOGE("Number out of limit!");
159         return;
160     }
161     if (TELEPHONY_CUST_WRAPPER.isHideViolenceOrEmcNumbersInCallLog_ != nullptr
162         && TELEPHONY_CUST_WRAPPER.isHideViolenceOrEmcNumbersInCallLog_(info)) {
163         TELEPHONY_LOGE("hide violence or emergency nums in call log!");
164         return;
165     }
166     errno_t result = memcpy_s(data.phoneNumber, kMaxNumberLen, info.accountNumber, strlen(info.accountNumber));
167     if (result != EOK) {
168         TELEPHONY_LOGE("memcpy_s failed!");
169         return;
170     }
171     if (strlen(info.numberLocation) > static_cast<size_t>(kMaxNumberLen)) {
172         TELEPHONY_LOGE("Location out of limit!");
173         return;
174     }
175     result = memcpy_s(data.numberLocation, kMaxNumberLen, info.numberLocation, strlen(info.numberLocation));
176     if (result != EOK) {
177         TELEPHONY_LOGE("memcpy_s failed!");
178         return;
179     }
180     if (memcpy_s(data.detectDetails, sizeof(data.detectDetails), info.detectDetails, strlen(info.detectDetails))
181         != EOK) {
182         TELEPHONY_LOGE("memcpy_s detectDetails failed!");
183         return;
184     }
185     CopyCallInfoToRecord(info, data);
186     std::string countryIso = GetCountryIso();
187     int32_t formatRet = CopyFormatNumberToRecord(countryIso, data);
188     if (formatRet != TELEPHONY_SUCCESS) {
189         TELEPHONY_LOGE("CopyFormatNumberToRecord failed!");
190         return;
191     }
192     int32_t formatToE164Ret = CopyFormatNumberToE164ToRecord(countryIso, data);
193     if (formatToE164Ret != TELEPHONY_SUCCESS) {
194         TELEPHONY_LOGE("CopyFormatNumberToE164ToRecord failed!");
195         return;
196     }
197     callRecordsHandlerServerPtr_->StoreCallRecord(data);
198 }
199 
GetCountryIso()200 std::string CallRecordsManager::GetCountryIso()
201 {
202     char valueStr[PROP_SYSPARA_SIZE] = {0};
203     GetParameter(PROP_NETWORK_COUNTRY_ISO, "", valueStr, PROP_SYSPARA_SIZE);
204     std::string countryIso = valueStr;
205     if (countryIso == "") {
206         TELEPHONY_LOGI("GetParameter is null, default network countryIso cn");
207         countryIso = DEFAULT_NETWORK_COUNTRY;
208     } else {
209         TELEPHONY_LOGI("GetParameter network countryIso is %{public}s", countryIso.c_str());
210     }
211     return countryIso;
212 }
213 
CopyFormatNumberToRecord(std::string & countryIso,CallRecordInfo & data)214 int32_t CallRecordsManager::CopyFormatNumberToRecord(std::string &countryIso, CallRecordInfo &data)
215 {
216     std::string tmpStr("");
217     if (std::regex_search(std::string(data.phoneNumber), std::regex(FORMAT_PATTERN))) {
218         (void)DelayedSingleton<CallNumberUtils>::GetInstance()->FormatPhoneNumberAsYouType(
219             std::string(data.phoneNumber), countryIso, tmpStr);
220     } else {
221         (void)DelayedSingleton<CallNumberUtils>::GetInstance()->FormatPhoneNumber(
222             std::string(data.phoneNumber), countryIso, tmpStr);
223     }
224 
225     if (tmpStr.length() > static_cast<size_t>(kMaxNumberLen)) {
226         TELEPHONY_LOGE("Number out of limit!");
227         return TELEPHONY_ERR_ARGUMENT_INVALID;
228     }
229     errno_t result = memcpy_s(data.formattedNumber, kMaxNumberLen, tmpStr.c_str(), tmpStr.length());
230     if (result != EOK) {
231         TELEPHONY_LOGE("memcpy_s failed!");
232         return result;
233     }
234     return TELEPHONY_SUCCESS;
235 }
236 
CopyFormatNumberToE164ToRecord(std::string & countryIso,CallRecordInfo & data)237 int32_t CallRecordsManager::CopyFormatNumberToE164ToRecord(std::string &countryIso, CallRecordInfo &data)
238 {
239     std::string tmpStr("");
240     (void)DelayedSingleton<CallNumberUtils>::GetInstance()->FormatPhoneNumberToE164(
241         std::string(data.phoneNumber), countryIso, tmpStr);
242     if (tmpStr.length() > static_cast<size_t>(kMaxNumberLen)) {
243         TELEPHONY_LOGE("Number out of limit!");
244         return TELEPHONY_ERR_ARGUMENT_INVALID;
245     }
246     errno_t result = memcpy_s(data.formattedNumberToE164, kMaxNumberLen, tmpStr.c_str(), tmpStr.length());
247     if (result != EOK) {
248         TELEPHONY_LOGE("memcpy_s failed!");
249         return result;
250     }
251     return TELEPHONY_SUCCESS;
252 }
253 
CopyCallInfoToRecord(CallAttributeInfo & info,CallRecordInfo & data)254 void CallRecordsManager::CopyCallInfoToRecord(CallAttributeInfo &info, CallRecordInfo &data)
255 {
256     if ((info.callBeginTime == DEFAULT_TIME) || (info.callEndTime == DEFAULT_TIME)) {
257         data.callDuration = DEFAULT_TIME;
258     } else {
259         data.callDuration = difftime(info.callEndTime, info.callBeginTime);
260     }
261     if ((info.ringBeginTime == DEFAULT_TIME) || (info.ringEndTime == DEFAULT_TIME)) {
262         data.ringDuration = DEFAULT_TIME;
263     } else {
264         data.ringDuration = difftime(info.ringEndTime, info.ringBeginTime);
265     }
266     data.callId = info.callId;
267     data.callBeginTime = info.callBeginTime;
268     data.callCreateTime = info.callCreateTime;
269     data.callEndTime = info.callEndTime;
270     data.directionType = info.callDirection;
271     data.answerType = info.answerType;
272     data.countryCode = DEFAULT_COUNTRY_CODE;
273     data.slotId = info.accountId;
274     data.callType = info.callType;
275     // use original call type for video call record
276     int32_t callFeatures = GetCallFeatures(info.originalCallType);
277     data.features = callFeatures;
278     data.numberMarkInfo = info.numberMarkInfo;
279     if (strcmp(data.numberMarkInfo.markSource, MARK_SOURCE_OF_ANTIFRAUT_CENTER) == 0) {
280         int32_t userId = 0;
281         AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
282         GetNumberMarkSource(userId, data.numberMarkInfo.markSource, kMaxNumberLen + 1);
283     }
284     data.blockReason = info.blockReason;
285     data.celiaCallType = info.celiaCallType;
286     data.name = info.name;
287     data.namePresentation = info.namePresentation;
288 }
289 
GetNumberMarkSource(int32_t userId,char * source,unsigned int size)290 void CallRecordsManager::GetNumberMarkSource(int32_t userId, char *source, unsigned int size)
291 {
292     std::string isAntifraudSwitchOn = "0";
293     std::string isTelephonyIdentityOn = "0";
294     auto settingHelper = SettingsDataShareHelper::GetInstance();
295     if (settingHelper == nullptr) {
296         return;
297     }
298     if (size <= strlen(MARK_SOURCE_OF_ANTIFRAUT_CENTER) && size <= strlen(MARK_SOURCE_OF_OTHERS)) {
299         return;
300     }
301     OHOS::Uri settingUri(SettingsDataShareHelper::SETTINGS_DATASHARE_URI);
302     settingHelper->Query(settingUri, SETTINGS_ANTIFRAUD_CENTER_SWITCH, isAntifraudSwitchOn);
303     if (isAntifraudSwitchOn == "0") {
304         strcpy_s(source, size, MARK_SOURCE_OF_OTHERS);
305         return;
306     }
307     settingHelper->Query(settingUri, TELEPHONY_IDENTITY_SWITCH, isTelephonyIdentityOn);
308     if (isTelephonyIdentityOn == "0") {
309         strcpy_s(source, size, MARK_SOURCE_OF_OTHERS);
310         return;
311     }
312     if (!CallManagerUtils::IsBundleInstalled("", userId)) {
313         strcpy_s(source, size, MARK_SOURCE_OF_OTHERS);
314         return;
315     }
316 }
317 
RemoveMissedIncomingCallNotification()318 int32_t CallRecordsManager::RemoveMissedIncomingCallNotification()
319 {
320     if (callRecordsHandlerServerPtr_ == nullptr) {
321         TELEPHONY_LOGE("callRecordsHandlerServerPtr_ is nullptr");
322         return TELEPHONY_ERR_LOCAL_PTR_NULL;
323     }
324     int32_t ret = callRecordsHandlerServerPtr_->RemoveMissedIncomingCallNotification();
325     if (ret != TELEPHONY_SUCCESS) {
326         TELEPHONY_LOGE("RemoveMissedIncomingCallNotification failed!");
327         return ret;
328     }
329     return TELEPHONY_SUCCESS;
330 }
331 
GetCallFeatures(int32_t videoState)332 int32_t CallRecordsManager::GetCallFeatures(int32_t videoState)
333 {
334     uint32_t features = 0;
335     if (IsVideoCall(videoState)) {
336         features |= FEATURES_VIDEO;
337     }
338     return static_cast<int32_t>(features);
339 }
340 
IsVideoCall(int32_t videoState)341 bool CallRecordsManager::IsVideoCall(int32_t videoState)
342 {
343     if (static_cast<VideoStateType>(videoState) == VideoStateType::TYPE_SEND_ONLY ||
344         static_cast<VideoStateType>(videoState) == VideoStateType::TYPE_VIDEO) {
345         return true;
346     }
347     return false;
348 }
349 
SetDataShareReady(bool isDataShareReady)350 void CallRecordsManager::SetDataShareReady(bool isDataShareReady)
351 {
352     isDataShareReady_ = isDataShareReady;
353 }
354 
SetSystemAbilityAdd(bool isSystemAbilityAdd)355 void CallRecordsManager::SetSystemAbilityAdd(bool isSystemAbilityAdd)
356 {
357     isSystemAbilityAdd_ = isSystemAbilityAdd;
358 }
359 
QueryUnReadMissedCallLog(int32_t userId)360 void CallRecordsManager::QueryUnReadMissedCallLog(int32_t userId)
361 {
362     if (!isDataShareReady_ || !isSystemAbilityAdd_ || isUnReadMissedCallLogQuery_) {
363         return;
364     }
365     TELEPHONY_LOGI("the user id is :%{public}d", userId);
366     if (userId == ACTIVE_USER_ID) {
367         int32_t ret = DelayedSingleton<CallRecordsHandlerService>::GetInstance()->QueryUnReadMissedCallLog();
368         if (ret != TELEPHONY_SUCCESS) {
369             TELEPHONY_LOGE("Query unread missed call log failed!");
370             isUnReadMissedCallLogQuery_ = false;
371         } else {
372             isUnReadMissedCallLogQuery_ = true;
373         }
374     }
375 }
376 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)377 void AccountSystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
378 {
379     TELEPHONY_LOGI("SA:%{public}d is added!", systemAbilityId);
380     if (!CheckInputSysAbilityId(systemAbilityId)) {
381         TELEPHONY_LOGE("added SA is invalid!");
382         return;
383     }
384     if (systemAbilityId != OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
385         TELEPHONY_LOGE("added SA is not accoubt manager service, ignored.");
386         return;
387     }
388     DelayedSingleton<CallRecordsManager>::GetInstance()->SetSystemAbilityAdd(true);
389     std::vector<int32_t> activeList = { 0 };
390     DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance()->QueryActiveOsAccountIds(activeList);
391     TELEPHONY_LOGI("current active user id is :%{public}d", activeList[0]);
392     if (activeList[0] == ACTIVE_USER_ID) {
393         DelayedSingleton<CallRecordsManager>::GetInstance()->QueryUnReadMissedCallLog(activeList[0]);
394     } else {
395         MatchingSkills matchingSkills;
396         matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
397         CommonEventSubscribeInfo subscriberInfo(matchingSkills);
398         subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
399         userSwitchSubscriber_ = std::make_shared<UserSwitchEventSubscriber>(subscriberInfo);
400         bool subRet = CommonEventManager::SubscribeCommonEvent(userSwitchSubscriber_);
401         if (!subRet) {
402             TELEPHONY_LOGE("Subscribe user switched event failed!");
403         }
404     }
405 }
406 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)407 void AccountSystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
408 {
409     TELEPHONY_LOGI("SA:%{public}d is removed!", systemAbilityId);
410     if (!CheckInputSysAbilityId(systemAbilityId)) {
411         TELEPHONY_LOGE("removed SA is invalid!");
412         return;
413     }
414     if (systemAbilityId != OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
415         TELEPHONY_LOGE("removed SA is not account manager service,, ignored.");
416         return;
417     }
418     if (userSwitchSubscriber_ != nullptr) {
419         bool subRet = CommonEventManager::UnSubscribeCommonEvent(userSwitchSubscriber_);
420         if (!subRet) {
421             TELEPHONY_LOGE("UnSubscribe user switched event failed!");
422         }
423         userSwitchSubscriber_ = nullptr;
424     }
425 }
426 
OnReceiveEvent(const CommonEventData & data)427 void UserSwitchEventSubscriber::OnReceiveEvent(const CommonEventData &data)
428 {
429     OHOS::EventFwk::Want want = data.GetWant();
430     std::string action = data.GetWant().GetAction();
431     TELEPHONY_LOGI("action = %{public}s", action.c_str());
432     if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
433         int32_t userId = data.GetCode();
434         DelayedSingleton<CallRecordsManager>::GetInstance()->QueryUnReadMissedCallLog(userId);
435     }
436 }
437 
OnReceiveEvent(const CommonEventData & data)438 void DataShareReadyEventSubscriber::OnReceiveEvent(const CommonEventData &data)
439 {
440     OHOS::EventFwk::Want want = data.GetWant();
441     std::string action = data.GetWant().GetAction();
442     TELEPHONY_LOGI("action = %{public}s", action.c_str());
443     if (action == CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY) {
444         DelayedSingleton<CallRecordsManager>::GetInstance()->SetDataShareReady(true);
445         std::vector<int32_t> activeList = { 0 };
446         DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance()->QueryActiveOsAccountIds(activeList);
447         DelayedSingleton<CallRecordsManager>::GetInstance()->QueryUnReadMissedCallLog(activeList[0]);
448         LocationSystemAbilityListener::SystemAbilitySubscriber();
449 #ifdef SUPPORT_MUTE_BY_DATABASE
450         InteroperableSettingsHandler::RegisterObserver();
451 #endif
452     }
453 }
454 } // namespace Telephony
455 } // namespace OHOS