1 /*
2  * Copyright (C) 2021-2022 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_ability_report_proxy.h"
17 
18 #include <string_ex.h>
19 
20 #include "app_mgr_interface.h"
21 #include "app_state_observer.h"
22 #include "bluetooth_call_manager.h"
23 #include "call_ability_callback_death_recipient.h"
24 #include "call_control_manager.h"
25 #include "call_manager_errors.h"
26 #include "iservice_registry.h"
27 #include "system_ability.h"
28 #include "system_ability_definition.h"
29 #include "telephony_log_wrapper.h"
30 
31 namespace OHOS {
32 namespace Telephony {
CallAbilityReportProxy()33 CallAbilityReportProxy::CallAbilityReportProxy()
34 {
35     callbackPtrList_.clear();
36 }
37 
~CallAbilityReportProxy()38 CallAbilityReportProxy::~CallAbilityReportProxy()
39 {
40     std::lock_guard<std::mutex> lock(mutex_);
41     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
42     while (it != callbackPtrList_.end()) {
43         if ((*it)) {
44             (*it).clear();
45             (*it) = nullptr;
46         }
47         callbackPtrList_.erase(it++);
48     }
49 }
50 
RegisterCallBack(sptr<ICallAbilityCallback> callAbilityCallbackPtr,const std::string & bundleInfo)51 int32_t CallAbilityReportProxy::RegisterCallBack(
52     sptr<ICallAbilityCallback> callAbilityCallbackPtr, const std::string &bundleInfo)
53 {
54     if (callAbilityCallbackPtr == nullptr) {
55         TELEPHONY_LOGE("callAbilityCallbackPtr is null");
56         return TELEPHONY_ERR_LOCAL_PTR_NULL;
57     }
58     callAbilityCallbackPtr->SetBundleInfo(bundleInfo);
59     std::lock_guard<std::mutex> lock(mutex_);
60     callbackPtrList_.emplace_back(callAbilityCallbackPtr);
61     TELEPHONY_LOGI("%{public}s successfully registered the callback!", bundleInfo.c_str());
62     if (callAbilityCallbackPtr->AsObject() != nullptr) {
63         sptr<CallAbilityCallbackDeathRecipient> deathRecipient =
64             new (std::nothrow) CallAbilityCallbackDeathRecipient();
65         if (deathRecipient == nullptr) {
66             TELEPHONY_LOGW("deathRecipient is nullptr");
67             return false;
68         }
69         callAbilityCallbackPtr->AsObject()->AddDeathRecipient(deathRecipient);
70     }
71     return TELEPHONY_SUCCESS;
72 }
73 
UnRegisterCallBack(const std::string & bundleInfo)74 int32_t CallAbilityReportProxy::UnRegisterCallBack(const std::string &bundleInfo)
75 {
76     std::lock_guard<std::mutex> lock(mutex_);
77     if (callbackPtrList_.empty()) {
78         TELEPHONY_LOGE("callbackPtrList_ is null! %{public}s UnRegisterCallBack failed", bundleInfo.c_str());
79         return TELEPHONY_ERR_LOCAL_PTR_NULL;
80     }
81     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
82     for (; it != callbackPtrList_.end(); ++it) {
83         if ((*it)->GetBundleInfo() == bundleInfo) {
84             callbackPtrList_.erase(it);
85             TELEPHONY_LOGI("%{public}s UnRegisterCallBack success", bundleInfo.c_str());
86             break;
87         }
88     }
89     return TELEPHONY_SUCCESS;
90 }
91 
UnRegisterCallBack(sptr<IRemoteObject> object)92 int32_t CallAbilityReportProxy::UnRegisterCallBack(sptr<IRemoteObject> object)
93 {
94     std::lock_guard<std::mutex> lock(mutex_);
95     if (callbackPtrList_.empty()) {
96         TELEPHONY_LOGE("callbackPtrList_ is null!");
97         return TELEPHONY_ERR_LOCAL_PTR_NULL;
98     }
99     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
100     for (; it != callbackPtrList_.end(); ++it) {
101         if ((*it)->AsObject() == object) {
102             TELEPHONY_LOGI("%{public}s UnRegisterCallBack success", (*it)->GetBundleInfo().c_str());
103             callbackPtrList_.erase(it);
104             break;
105         }
106     }
107     return TELEPHONY_SUCCESS;
108 }
109 
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)110 void CallAbilityReportProxy::CallStateUpdated(
111     sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
112 {
113     if (callObjectPtr == nullptr) {
114         TELEPHONY_LOGE("callObjectPtr is nullptr!");
115         return;
116     }
117     CallAttributeInfo info;
118     callObjectPtr->GetCallAttributeInfo(info);
119     size_t accountLen = strlen(info.accountNumber);
120     if (accountLen > static_cast<size_t>(kMaxNumberLen)) {
121         accountLen = kMaxNumberLen;
122     }
123     for (size_t i = 0; i < accountLen; i++) {
124         if (info.accountNumber[i] == ',' || info.accountNumber[i] == ';') {
125             info.accountNumber[i] = '\0';
126             break;
127         }
128     }
129     if (nextState == TelCallState::CALL_STATUS_ANSWERED) {
130         TELEPHONY_LOGI("report answered state");
131         info.callState = TelCallState::CALL_STATUS_ANSWERED;
132     }
133     ReportCallStateInfo(info);
134 }
135 
CallEventUpdated(CallEventInfo & info)136 void CallAbilityReportProxy::CallEventUpdated(CallEventInfo &info)
137 {
138     ReportCallEvent(info);
139 }
140 
CallDestroyed(const DisconnectedDetails & details)141 void CallAbilityReportProxy::CallDestroyed(const DisconnectedDetails &details)
142 {
143     int32_t ret = TELEPHONY_ERR_FAIL;
144     std::lock_guard<std::mutex> lock(mutex_);
145     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
146     for (; it != callbackPtrList_.end(); ++it) {
147         if ((*it)) {
148             ret = (*it)->OnCallDisconnectedCause(details);
149             if (ret != TELEPHONY_SUCCESS) {
150                 TELEPHONY_LOGW("OnCallDisconnectedCause failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
151                     ((*it)->GetBundleInfo()).c_str());
152                 continue;
153             }
154         }
155     }
156     TELEPHONY_LOGI("report call disconnected cause[%{public}d] success", details.reason);
157 }
158 
ReportCallStateInfo(const CallAttributeInfo & info)159 int32_t CallAbilityReportProxy::ReportCallStateInfo(const CallAttributeInfo &info)
160 {
161     int32_t ret = TELEPHONY_ERR_FAIL;
162     std::string bundleInfo = "";
163     std::lock_guard<std::mutex> lock(mutex_);
164     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
165     for (; it != callbackPtrList_.end(); ++it) {
166         if ((*it)) {
167             bundleInfo = (*it)->GetBundleInfo();
168             ret = (*it)->OnCallDetailsChange(info);
169             if (ret != TELEPHONY_SUCCESS) {
170                 TELEPHONY_LOGD(
171                     "OnCallDetailsChange failed, errcode:%{public}d, bundleInfo:%{public}s", ret, bundleInfo.c_str());
172                 continue;
173             }
174         }
175     }
176     DelayedSingleton<BluetoothCallManager>::GetInstance()->SendCallDetailsChange(static_cast<int32_t>(info.callId),
177         static_cast<int32_t>(info.callState));
178     TELEPHONY_LOGI("report call state info success, callId[%{public}d] state[%{public}d] conferenceState[%{public}d] "
179                    "videoState[%{public}d]",
180         info.callId, info.callState, info.conferenceState, info.videoState);
181     return ret;
182 }
183 
ReportCallStateInfo(const CallAttributeInfo & info,std::string bundleInfo)184 int32_t CallAbilityReportProxy::ReportCallStateInfo(const CallAttributeInfo &info, std::string bundleInfo)
185 {
186     int32_t ret = TELEPHONY_ERROR;
187     std::lock_guard<std::mutex> lock(mutex_);
188     for (auto callback : callbackPtrList_) {
189         if (callback->GetBundleInfo() == bundleInfo) {
190             ret = callback->OnCallDetailsChange(info);
191             break;
192         }
193     }
194     if (ret != TELEPHONY_SUCCESS) {
195         TELEPHONY_LOGE(
196             "OnCallDetailsChange failed, errcode:%{public}d, bundleInfo:%{public}s", ret, bundleInfo.c_str());
197     } else {
198         TELEPHONY_LOGI("callId[%{public}d] state[%{public}d] conferenceState[%{public}d] "
199                        "videoState[%{public}d], report bundleInfo %{public}s success",
200             info.callId, info.callState, info.conferenceState, info.videoState, bundleInfo.c_str());
201     }
202     return ret;
203 }
204 
ReportCallEvent(const CallEventInfo & info)205 int32_t CallAbilityReportProxy::ReportCallEvent(const CallEventInfo &info)
206 {
207     int32_t ret = TELEPHONY_ERR_FAIL;
208     TELEPHONY_LOGI("report call event, eventId:%{public}d", info.eventId);
209     std::lock_guard<std::mutex> lock(mutex_);
210     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
211     for (; it != callbackPtrList_.end(); ++it) {
212         if ((*it)) {
213             ret = (*it)->OnCallEventChange(info);
214             if (ret != TELEPHONY_SUCCESS) {
215                 TELEPHONY_LOGW("OnCallEventChange failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
216                     ((*it)->GetBundleInfo()).c_str());
217                 continue;
218             }
219         }
220     }
221     TELEPHONY_LOGI("report call event[%{public}d] info success", info.eventId);
222     return ret;
223 }
224 
ReportAsyncResults(const CallResultReportId reportId,AppExecFwk::PacMap & resultInfo)225 int32_t CallAbilityReportProxy::ReportAsyncResults(
226     const CallResultReportId reportId, AppExecFwk::PacMap &resultInfo)
227 {
228     int32_t ret = TELEPHONY_ERR_FAIL;
229     std::lock_guard<std::mutex> lock(mutex_);
230     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
231     for (; it != callbackPtrList_.end(); ++it) {
232         if ((*it)) {
233             ret = (*it)->OnReportAsyncResults(reportId, resultInfo);
234             if (ret != TELEPHONY_SUCCESS) {
235                 TELEPHONY_LOGW("ReportAsyncResults failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
236                     ((*it)->GetBundleInfo()).c_str());
237                 continue;
238             }
239         }
240     }
241     TELEPHONY_LOGI("ReportAsyncResults success, reportId:%{public}d", reportId);
242     return ret;
243 }
244 
ReportMmiCodeResult(const MmiCodeInfo & info)245 int32_t CallAbilityReportProxy::ReportMmiCodeResult(const MmiCodeInfo &info)
246 {
247     int32_t ret = TELEPHONY_ERR_FAIL;
248     std::lock_guard<std::mutex> lock(mutex_);
249     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
250     for (; it != callbackPtrList_.end(); ++it) {
251         if ((*it)) {
252             ret = (*it)->OnReportMmiCodeResult(info);
253             if (ret != TELEPHONY_SUCCESS) {
254                 TELEPHONY_LOGW("ReportMmiCodeResult failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
255                     ((*it)->GetBundleInfo()).c_str());
256                 continue;
257             }
258         }
259     }
260     TELEPHONY_LOGI("ReportMmiCodeResult success");
261     return ret;
262 }
263 
OttCallRequest(OttCallRequestId requestId,AppExecFwk::PacMap & info)264 int32_t CallAbilityReportProxy::OttCallRequest(OttCallRequestId requestId, AppExecFwk::PacMap &info)
265 {
266     int32_t ret = TELEPHONY_ERR_FAIL;
267     std::lock_guard<std::mutex> lock(mutex_);
268     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
269     for (; it != callbackPtrList_.end(); ++it) {
270         std::string bundleInfo = (*it)->GetBundleInfo();
271         if (bundleInfo == "com.ohos.callservice") {
272             ret = (*it)->OnOttCallRequest(requestId, info);
273             if (ret != TELEPHONY_SUCCESS) {
274                 TELEPHONY_LOGW(
275                     "OttCallRequest failed, errcode:%{public}d, bundleInfo:%{public}s", ret, bundleInfo.c_str());
276                 break;
277             }
278         }
279     }
280     TELEPHONY_LOGI("OttCallRequest success, requestId:%{public}d", requestId);
281     return ret;
282 }
283 
ReportAudioDeviceChange(const AudioDeviceInfo & info)284 int32_t CallAbilityReportProxy::ReportAudioDeviceChange(const AudioDeviceInfo &info)
285 {
286     int32_t ret = TELEPHONY_ERR_FAIL;
287     std::lock_guard<std::mutex> lock(mutex_);
288     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
289     for (; it != callbackPtrList_.end(); ++it) {
290         if ((*it)) {
291             ret = (*it)->OnReportAudioDeviceChange(info);
292             if (ret != TELEPHONY_SUCCESS) {
293                 TELEPHONY_LOGD("ReportAudioDeviceChange failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
294                     ((*it)->GetBundleInfo()).c_str());
295                 continue;
296             }
297         }
298     }
299     TELEPHONY_LOGI("ReportAudioDeviceChange success");
300     return ret;
301 }
302 
ReportPostDialDelay(const std::string & str)303 int32_t CallAbilityReportProxy::ReportPostDialDelay(const std::string &str)
304 {
305     int32_t ret = TELEPHONY_ERR_FAIL;
306     std::lock_guard<std::mutex> lock(mutex_);
307     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
308     for (; it != callbackPtrList_.end(); ++it) {
309         if ((*it)) {
310             ret = (*it)->OnReportPostDialDelay(str);
311             if (ret != TELEPHONY_SUCCESS) {
312                 TELEPHONY_LOGW("ReportPostDialDelay failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
313                     ((*it)->GetBundleInfo()).c_str());
314                 continue;
315             }
316         }
317     }
318     TELEPHONY_LOGI("ReportPostDialDelay success");
319     return ret;
320 }
321 
ReportImsCallModeChange(const CallMediaModeInfo & imsCallModeInfo)322 int32_t CallAbilityReportProxy::ReportImsCallModeChange(const CallMediaModeInfo &imsCallModeInfo)
323 {
324     int32_t ret = TELEPHONY_ERR_FAIL;
325     std::lock_guard<std::mutex> lock(mutex_);
326     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
327     for (; it != callbackPtrList_.end(); ++it) {
328         if ((*it)) {
329             ret = (*it)->OnReportImsCallModeChange(imsCallModeInfo);
330             if (ret != TELEPHONY_SUCCESS) {
331                 TELEPHONY_LOGW("ReportImsCallModeReceive failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
332                     ((*it)->GetBundleInfo()).c_str());
333                 continue;
334             }
335         }
336     }
337     TELEPHONY_LOGI("ReportImsCallModeReceive success");
338     return ret;
339 }
340 
ReportCallSessionEventChange(const CallSessionEvent & callSessionEventOptions)341 int32_t CallAbilityReportProxy::ReportCallSessionEventChange(
342     const CallSessionEvent &callSessionEventOptions)
343 {
344     int32_t ret = TELEPHONY_ERR_FAIL;
345     std::lock_guard<std::mutex> lock(mutex_);
346     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
347     for (; it != callbackPtrList_.end(); ++it) {
348         if ((*it)) {
349             ret = (*it)->OnReportCallSessionEventChange(callSessionEventOptions);
350             if (ret != TELEPHONY_SUCCESS) {
351                 TELEPHONY_LOGW("ReportCallSessionEventChange failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
352                     ((*it)->GetBundleInfo()).c_str());
353                 continue;
354             }
355         }
356     }
357     TELEPHONY_LOGI("ReportCallSessionEventChange success");
358     return ret;
359 }
360 
ReportPeerDimensionsChange(const PeerDimensionsDetail & peerDimensionsDetail)361 int32_t CallAbilityReportProxy::ReportPeerDimensionsChange(const PeerDimensionsDetail &peerDimensionsDetail)
362 {
363     int32_t ret = TELEPHONY_ERR_FAIL;
364     std::lock_guard<std::mutex> lock(mutex_);
365     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
366     for (; it != callbackPtrList_.end(); ++it) {
367         if ((*it)) {
368             ret = (*it)->OnReportPeerDimensionsChange(peerDimensionsDetail);
369             if (ret != TELEPHONY_SUCCESS) {
370                 TELEPHONY_LOGW("ReportPeerDimensionsChange failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
371                     ((*it)->GetBundleInfo()).c_str());
372                 continue;
373             }
374         }
375     }
376     TELEPHONY_LOGI("ReportPeerDimensionsChange success");
377     return ret;
378 }
379 
ReportCallDataUsageChange(const int64_t dataUsage)380 int32_t CallAbilityReportProxy::ReportCallDataUsageChange(const int64_t dataUsage)
381 {
382     int32_t ret = TELEPHONY_ERR_FAIL;
383     std::lock_guard<std::mutex> lock(mutex_);
384     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
385     for (; it != callbackPtrList_.end(); ++it) {
386         if ((*it)) {
387             ret = (*it)->OnReportCallDataUsageChange(dataUsage);
388             if (ret != TELEPHONY_SUCCESS) {
389                 TELEPHONY_LOGW("ReportCallDataUsageChange failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
390                     ((*it)->GetBundleInfo()).c_str());
391                 continue;
392             }
393         }
394     }
395     TELEPHONY_LOGI("ReportCallDataUsageChange success");
396     return ret;
397 }
398 
ReportCameraCapabilities(const CameraCapabilities & cameraCapabilities)399 int32_t CallAbilityReportProxy::ReportCameraCapabilities(const CameraCapabilities &cameraCapabilities)
400 {
401     int32_t ret = TELEPHONY_ERR_FAIL;
402     std::lock_guard<std::mutex> lock(mutex_);
403     std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
404     for (; it != callbackPtrList_.end(); ++it) {
405         if ((*it)) {
406             ret = (*it)->OnReportCameraCapabilities(cameraCapabilities);
407             if (ret != TELEPHONY_SUCCESS) {
408                 TELEPHONY_LOGW("ReportCameraCapabilities failed, errcode:%{public}d, bundleInfo:%{public}s", ret,
409                     ((*it)->GetBundleInfo()).c_str());
410                 continue;
411             }
412         }
413     }
414     TELEPHONY_LOGI("ReportCameraCapabilities success");
415     return ret;
416 }
417 } // namespace Telephony
418 } // namespace OHOS
419