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 "securec.h"
24
25 namespace OHOS {
26 namespace Telephony {
27 constexpr int16_t DEFAULT_COUNTRY_CODE = 0;
28 constexpr int16_t DEFAULT_TIME = 0;
29 const int32_t ACTIVE_USER_ID = 100;
CallRecordsManager()30 CallRecordsManager::CallRecordsManager() : callRecordsHandlerServerPtr_(nullptr) {}
31
~CallRecordsManager()32 CallRecordsManager::~CallRecordsManager()
33 {
34 if (statusChangeListener_ != nullptr) {
35 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
36 if (samgrProxy != nullptr) {
37 samgrProxy->UnSubscribeSystemAbility(OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener_);
38 statusChangeListener_ = nullptr;
39 }
40 }
41 }
42
Init()43 void CallRecordsManager::Init()
44 {
45 callRecordsHandlerServerPtr_ = DelayedSingleton<CallRecordsHandlerService>::GetInstance();
46 if (callRecordsHandlerServerPtr_ == nullptr) {
47 TELEPHONY_LOGE("call record manager init failure.");
48 return;
49 }
50 callRecordsHandlerServerPtr_->Start();
51 statusChangeListener_ = new (std::nothrow) AccountSystemAbilityListener();
52 if (statusChangeListener_ == nullptr) {
53 TELEPHONY_LOGE("failed to create statusChangeListener");
54 return;
55 }
56 auto managerPtr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57 if (managerPtr == nullptr) {
58 TELEPHONY_LOGE("get system ability manager error");
59 return;
60 }
61 int32_t ret = managerPtr->SubscribeSystemAbility(OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN, statusChangeListener_);
62 if (ret != TELEPHONY_SUCCESS) {
63 TELEPHONY_LOGE("failed to subscribe account manager service SA!");
64 return;
65 }
66 }
67
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)68 void CallRecordsManager::CallStateUpdated(
69 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
70 {
71 CallAttributeInfo info;
72 if (nextState != TelCallState::CALL_STATUS_DISCONNECTED) {
73 TELEPHONY_LOGE("nextState not CALL_STATUS_DISCONNECTED");
74 return;
75 }
76 if (callObjectPtr == nullptr) {
77 TELEPHONY_LOGE("call object is nullptr");
78 return;
79 }
80 (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
81 callObjectPtr->GetCallAttributeBaseInfo(info);
82 AddOneCallRecord(info);
83 }
84
AddOneCallRecord(sptr<CallBase> call,CallAnswerType answerType)85 void CallRecordsManager::AddOneCallRecord(sptr<CallBase> call, CallAnswerType answerType)
86 {
87 CallAttributeInfo info;
88 (void)memset_s(&info, sizeof(CallAttributeInfo), 0, sizeof(CallAttributeInfo));
89 call->GetCallAttributeBaseInfo(info);
90 AddOneCallRecord(info);
91 }
92
AddOneCallRecord(CallAttributeInfo & info)93 void CallRecordsManager::AddOneCallRecord(CallAttributeInfo &info)
94 {
95 CallRecordInfo data;
96 (void)memset_s(&data, sizeof(CallRecordInfo), 0, sizeof(CallRecordInfo));
97 if (callRecordsHandlerServerPtr_ == nullptr) {
98 TELEPHONY_LOGE("callRecordsHandlerServerPtr_ is nullptr");
99 return;
100 }
101 if (strlen(info.accountNumber) > static_cast<size_t>(kMaxNumberLen)) {
102 TELEPHONY_LOGE("Number out of limit!");
103 return;
104 }
105 errno_t result = memcpy_s(data.phoneNumber, kMaxNumberLen, info.accountNumber, strlen(info.accountNumber));
106 if (result != EOK) {
107 TELEPHONY_LOGE("memcpy_s failed!");
108 return;
109 }
110 if ((info.callBeginTime == DEFAULT_TIME) || (info.callEndTime == DEFAULT_TIME)) {
111 data.callDuration = DEFAULT_TIME;
112 } else {
113 data.callDuration = difftime(info.callEndTime, info.callBeginTime);
114 }
115 if ((info.ringBeginTime == DEFAULT_TIME) || (info.ringEndTime == DEFAULT_TIME)) {
116 data.ringDuration = DEFAULT_TIME;
117 } else {
118 data.ringDuration = difftime(info.ringEndTime, info.ringBeginTime);
119 }
120 data.callId = info.callId;
121 data.callBeginTime = info.callBeginTime;
122 data.callEndTime = info.callEndTime;
123 data.directionType = info.callDirection;
124 data.answerType = info.answerType;
125 data.countryCode = DEFAULT_COUNTRY_CODE;
126 data.slotId = info.accountId;
127 data.callType = info.callType;
128 std::string tmpStr("");
129 auto callNumberUtils = DelayedSingleton<CallNumberUtils>::GetInstance();
130 std::string numberWithoutPostDial = callNumberUtils->RemovePostDailPhoneNumber(data.phoneNumber);
131 callNumberUtils->FormatPhoneNumber(numberWithoutPostDial, "CN", tmpStr);
132 if (tmpStr.length() > static_cast<size_t>(kMaxNumberLen)) {
133 TELEPHONY_LOGE("Number out of limit!");
134 return;
135 }
136 if (memcpy_s(data.formattedPhoneNumber, kMaxNumberLen, tmpStr.c_str(), tmpStr.length()) != 0) {
137 TELEPHONY_LOGE("memcpy_s failed!");
138 return;
139 }
140 callRecordsHandlerServerPtr_->StoreCallRecord(data);
141 }
142
RemoveMissedIncomingCallNotification()143 int32_t CallRecordsManager::RemoveMissedIncomingCallNotification()
144 {
145 if (callRecordsHandlerServerPtr_ == nullptr) {
146 TELEPHONY_LOGE("callRecordsHandlerServerPtr_ is nullptr");
147 return TELEPHONY_ERR_LOCAL_PTR_NULL;
148 }
149 int32_t ret = callRecordsHandlerServerPtr_->RemoveMissedIncomingCallNotification();
150 if (ret != TELEPHONY_SUCCESS) {
151 TELEPHONY_LOGE("RemoveMissedIncomingCallNotification failed!");
152 return ret;
153 }
154 return TELEPHONY_SUCCESS;
155 }
156
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)157 void AccountSystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
158 {
159 TELEPHONY_LOGI("SA:%{public}d is added!", systemAbilityId);
160 if (!CheckInputSysAbilityId(systemAbilityId)) {
161 TELEPHONY_LOGE("added SA is invalid!");
162 return;
163 }
164 if (systemAbilityId != OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
165 TELEPHONY_LOGE("added SA is not accoubt manager service, ignored.");
166 return;
167 }
168 std::vector<int32_t> activeList = { 0 };
169 DelayedSingleton<AppExecFwk::OsAccountManagerWrapper>::GetInstance()->QueryActiveOsAccountIds(activeList);
170 TELEPHONY_LOGI("current active user id is :%{public}d", activeList[0]);
171 if (activeList[0] == ACTIVE_USER_ID) {
172 int32_t ret = DelayedSingleton<CallRecordsHandlerService>::GetInstance()->QueryUnReadMissedCallLog();
173 if (ret != TELEPHONY_SUCCESS) {
174 TELEPHONY_LOGE("QueryUnReadMissedCallLog failed!");
175 }
176 } else {
177 MatchingSkills matchingSkills;
178 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
179 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
180 subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
181 userSwitchSubscriber_ = std::make_shared<UserSwitchEventSubscriber>(subscriberInfo);
182 bool subRet = CommonEventManager::SubscribeCommonEvent(userSwitchSubscriber_);
183 if (!subRet) {
184 TELEPHONY_LOGE("Subscribe user switched event failed!");
185 }
186 }
187 }
188
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)189 void AccountSystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
190 {
191 TELEPHONY_LOGI("SA:%{public}d is removed!", systemAbilityId);
192 if (!CheckInputSysAbilityId(systemAbilityId)) {
193 TELEPHONY_LOGE("removed SA is invalid!");
194 return;
195 }
196 if (systemAbilityId != OHOS::SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN) {
197 TELEPHONY_LOGE("removed SA is not account manager service,, ignored.");
198 return;
199 }
200 if (userSwitchSubscriber_ != nullptr) {
201 bool subRet = CommonEventManager::UnSubscribeCommonEvent(userSwitchSubscriber_);
202 if (!subRet) {
203 TELEPHONY_LOGE("UnSubscribe user switched event failed!");
204 }
205 userSwitchSubscriber_ = nullptr;
206 }
207 }
208
OnReceiveEvent(const CommonEventData & data)209 void UserSwitchEventSubscriber::OnReceiveEvent(const CommonEventData &data)
210 {
211 OHOS::EventFwk::Want want = data.GetWant();
212 std::string action = data.GetWant().GetAction();
213 TELEPHONY_LOGI("action = %{public}s", action.c_str());
214 if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
215 int32_t userId = data.GetCode();
216 if (userId == ACTIVE_USER_ID) {
217 int32_t ret = DelayedSingleton<CallRecordsHandlerService>::GetInstance()->QueryUnReadMissedCallLog();
218 if (ret != TELEPHONY_SUCCESS) {
219 TELEPHONY_LOGE("Query unread missed call log failed!");
220 }
221 }
222 }
223 }
224 } // namespace Telephony
225 } // namespace OHOS