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 "iservice_registry.h"
21 #include "system_ability.h"
22 #include "system_ability_definition.h"
23
24 #include "call_manager_errors.h"
25 #include "telephony_log_wrapper.h"
26
27 namespace OHOS {
28 namespace Telephony {
CallAbilityReportProxy()29 CallAbilityReportProxy::CallAbilityReportProxy()
30 {
31 callbackPtrList_.clear();
32 }
33
~CallAbilityReportProxy()34 CallAbilityReportProxy::~CallAbilityReportProxy()
35 {
36 std::lock_guard<std::mutex> lock(mutex_);
37 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
38 while (it != callbackPtrList_.end()) {
39 if ((*it)) {
40 (*it).clear();
41 (*it) = nullptr;
42 }
43 callbackPtrList_.erase(it++);
44 }
45 }
46
RegisterCallBack(sptr<ICallAbilityCallback> callAbilityCallbackPtr,const std::string & bundleName)47 int32_t CallAbilityReportProxy::RegisterCallBack(
48 sptr<ICallAbilityCallback> callAbilityCallbackPtr, const std::string &bundleName)
49 {
50 if (callAbilityCallbackPtr == nullptr) {
51 TELEPHONY_LOGE("callAbilityCallbackPtr is null");
52 return TELEPHONY_ERR_LOCAL_PTR_NULL;
53 }
54 callAbilityCallbackPtr->SetBundleName(bundleName);
55 std::lock_guard<std::mutex> lock(mutex_);
56 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
57 for (; it != callbackPtrList_.end(); ++it) {
58 if ((*it)->GetBundleName() == bundleName) {
59 (*it).clear();
60 (*it) = callAbilityCallbackPtr;
61 TELEPHONY_LOGI("%{public}s RegisterCallBack success", bundleName.c_str());
62 return TELEPHONY_SUCCESS;
63 }
64 }
65 callbackPtrList_.emplace_back(callAbilityCallbackPtr);
66 TELEPHONY_LOGI("%{public}s successfully registered the callback for the first time!", bundleName.c_str());
67 return TELEPHONY_SUCCESS;
68 }
69
UnRegisterCallBack(const std::string & bundleName)70 int32_t CallAbilityReportProxy::UnRegisterCallBack(const std::string &bundleName)
71 {
72 if (callbackPtrList_.empty()) {
73 TELEPHONY_LOGE("callbackPtrList_ is null! %{public}s UnRegisterCallBack failed", bundleName.c_str());
74 return TELEPHONY_ERR_LOCAL_PTR_NULL;
75 }
76 std::lock_guard<std::mutex> lock(mutex_);
77 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
78 for (; it != callbackPtrList_.end(); ++it) {
79 if ((*it)->GetBundleName() == bundleName) {
80 callbackPtrList_.erase(it);
81 TELEPHONY_LOGI("%{public}s UnRegisterCallBack success", bundleName.c_str());
82 return TELEPHONY_SUCCESS;
83 }
84 }
85 TELEPHONY_LOGE("UnRegisterCallBack failure!");
86 return TELEPHONY_ERROR;
87 }
88
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)89 void CallAbilityReportProxy::CallStateUpdated(
90 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
91 {
92 if (callObjectPtr == nullptr) {
93 TELEPHONY_LOGE("callObjectPtr is nullptr!");
94 return;
95 }
96 CallAttributeInfo info;
97 callObjectPtr->GetCallAttributeInfo(info);
98 ReportCallStateInfo(info);
99 }
100
CallEventUpdated(CallEventInfo & info)101 void CallAbilityReportProxy::CallEventUpdated(CallEventInfo &info)
102 {
103 ReportCallEvent(info);
104 }
105
CallDestroyed(const DisconnectedDetails & details)106 void CallAbilityReportProxy::CallDestroyed(const DisconnectedDetails &details)
107 {
108 int32_t ret = TELEPHONY_ERR_FAIL;
109 std::lock_guard<std::mutex> lock(mutex_);
110 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
111 for (; it != callbackPtrList_.end(); ++it) {
112 if ((*it)) {
113 ret = (*it)->OnCallDisconnectedCause(details);
114 if (ret != TELEPHONY_SUCCESS) {
115 TELEPHONY_LOGW("OnCallDisconnectedCause failed, errcode:%{public}d, bundleName:%{public}s", ret,
116 ((*it)->GetBundleName()).c_str());
117 continue;
118 }
119 }
120 }
121 TELEPHONY_LOGI("report call disconnected cause[%{public}d] success", details.reason);
122 }
123
ReportCallStateInfo(const CallAttributeInfo & info)124 int32_t CallAbilityReportProxy::ReportCallStateInfo(const CallAttributeInfo &info)
125 {
126 int32_t ret = TELEPHONY_ERR_FAIL;
127 std::string bundleName = "";
128 std::lock_guard<std::mutex> lock(mutex_);
129 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
130 for (; it != callbackPtrList_.end(); ++it) {
131 if ((*it)) {
132 bundleName = (*it)->GetBundleName();
133 ret = (*it)->OnCallDetailsChange(info);
134 if (ret != TELEPHONY_SUCCESS) {
135 TELEPHONY_LOGW("OnCallDetailsChange failed, errcode:%{public}d, bundleName:%{public}s", ret,
136 bundleName.c_str());
137 continue;
138 }
139 }
140 }
141 TELEPHONY_LOGI("report call state[%{public}d] conferenceState[%{public}d] info success", info.callState,
142 info.conferenceState);
143 return ret;
144 }
145
ReportCallEvent(const CallEventInfo & info)146 int32_t CallAbilityReportProxy::ReportCallEvent(const CallEventInfo &info)
147 {
148 int32_t ret = TELEPHONY_ERR_FAIL;
149 TELEPHONY_LOGI("report call event, eventId:%{public}d", info.eventId);
150 std::lock_guard<std::mutex> lock(mutex_);
151 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
152 for (; it != callbackPtrList_.end(); ++it) {
153 if ((*it)) {
154 ret = (*it)->OnCallEventChange(info);
155 if (ret != TELEPHONY_SUCCESS) {
156 TELEPHONY_LOGW("OnCallEventChange failed, errcode:%{public}d, bundleName:%{public}s", ret,
157 ((*it)->GetBundleName()).c_str());
158 continue;
159 }
160 }
161 }
162 TELEPHONY_LOGI("report call event[%{public}d] info success", info.eventId);
163 return ret;
164 }
165
ReportAsyncResults(const CallResultReportId reportId,AppExecFwk::PacMap & resultInfo)166 int32_t CallAbilityReportProxy::ReportAsyncResults(
167 const CallResultReportId reportId, AppExecFwk::PacMap &resultInfo)
168 {
169 int32_t ret = TELEPHONY_ERR_FAIL;
170 std::lock_guard<std::mutex> lock(mutex_);
171 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
172 for (; it != callbackPtrList_.end(); ++it) {
173 if ((*it)) {
174 ret = (*it)->OnReportAsyncResults(reportId, resultInfo);
175 if (ret != TELEPHONY_SUCCESS) {
176 TELEPHONY_LOGW("ReportAsyncResults failed, errcode:%{public}d, bundleName:%{public}s", ret,
177 ((*it)->GetBundleName()).c_str());
178 continue;
179 }
180 }
181 }
182 TELEPHONY_LOGI("ReportAsyncResults success, reportId:%{public}d", reportId);
183 return ret;
184 }
185
ReportMmiCodeResult(const MmiCodeInfo & info)186 int32_t CallAbilityReportProxy::ReportMmiCodeResult(const MmiCodeInfo &info)
187 {
188 int32_t ret = TELEPHONY_ERR_FAIL;
189 std::lock_guard<std::mutex> lock(mutex_);
190 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
191 for (; it != callbackPtrList_.end(); ++it) {
192 if ((*it)) {
193 ret = (*it)->OnReportMmiCodeResult(info);
194 if (ret != TELEPHONY_SUCCESS) {
195 TELEPHONY_LOGW("ReportMmiCodeResult failed, errcode:%{public}d, bundleName:%{public}s", ret,
196 ((*it)->GetBundleName()).c_str());
197 continue;
198 }
199 }
200 }
201 TELEPHONY_LOGI("ReportMmiCodeResult success");
202 return ret;
203 }
204
OttCallRequest(OttCallRequestId requestId,AppExecFwk::PacMap & info)205 int32_t CallAbilityReportProxy::OttCallRequest(OttCallRequestId requestId, AppExecFwk::PacMap &info)
206 {
207 int32_t ret = TELEPHONY_ERR_FAIL;
208 std::lock_guard<std::mutex> lock(mutex_);
209 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
210 for (; it != callbackPtrList_.end(); ++it) {
211 std::string name = (*it)->GetBundleName();
212 if (name == "com.ohos.callservice") {
213 ret = (*it)->OnOttCallRequest(requestId, info);
214 if (ret != TELEPHONY_SUCCESS) {
215 TELEPHONY_LOGW(
216 "OttCallRequest failed, errcode:%{public}d, bundleName:%{public}s", ret, name.c_str());
217 break;
218 }
219 }
220 }
221 TELEPHONY_LOGI("OttCallRequest success, requestId:%{public}d", requestId);
222 return ret;
223 }
224
ReportAudioDeviceChange(const AudioDeviceInfo & info)225 int32_t CallAbilityReportProxy::ReportAudioDeviceChange(const AudioDeviceInfo &info)
226 {
227 int32_t ret = TELEPHONY_ERR_FAIL;
228 std::lock_guard<std::mutex> lock(mutex_);
229 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
230 for (; it != callbackPtrList_.end(); ++it) {
231 if ((*it)) {
232 ret = (*it)->OnReportAudioDeviceChange(info);
233 if (ret != TELEPHONY_SUCCESS) {
234 TELEPHONY_LOGW("ReportAudioDeviceChange failed, errcode:%{public}d, bundleName:%{public}s", ret,
235 ((*it)->GetBundleName()).c_str());
236 continue;
237 }
238 }
239 }
240 TELEPHONY_LOGI("ReportAudioDeviceChange success");
241 return ret;
242 }
243
ReportPostDialDelay(const std::string & str)244 int32_t CallAbilityReportProxy::ReportPostDialDelay(const std::string &str)
245 {
246 int32_t ret = TELEPHONY_ERR_FAIL;
247 std::lock_guard<std::mutex> lock(mutex_);
248 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
249 for (; it != callbackPtrList_.end(); ++it) {
250 if ((*it)) {
251 ret = (*it)->OnReportPostDialDelay(str);
252 if (ret != TELEPHONY_SUCCESS) {
253 TELEPHONY_LOGW("ReportPostDialDelay failed, errcode:%{public}d, bundleName:%{public}s", ret,
254 ((*it)->GetBundleName()).c_str());
255 continue;
256 }
257 }
258 }
259 TELEPHONY_LOGI("ReportPostDialDelay success");
260 return ret;
261 }
262 } // namespace Telephony
263 } // namespace OHOS
264