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_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,std::string & bundleName)47 int32_t CallAbilityReportProxy::RegisterCallBack(
48 sptr<ICallAbilityCallback> callAbilityCallbackPtr, 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(std::string & bundleName)70 int32_t CallAbilityReportProxy::UnRegisterCallBack(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(int32_t cause)106 void CallAbilityReportProxy::CallDestroyed(int32_t cause)
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((DisconnectedDetails)cause);
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", cause);
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 callbackPtrList_.erase(it++);
136 --it;
137 TELEPHONY_LOGW("OnCallDetailsChange failed, errcode:%{public}d, bundleName:%{public}s", ret,
138 bundleName.c_str());
139 continue;
140 }
141 }
142 }
143 TELEPHONY_LOGI("report call state[%{public}d] conferenceState[%{public}d] info success", info.callState,
144 info.conferenceState);
145 return ret;
146 }
147
ReportCallEvent(const CallEventInfo & info)148 int32_t CallAbilityReportProxy::ReportCallEvent(const CallEventInfo &info)
149 {
150 int32_t ret = TELEPHONY_ERR_FAIL;
151 TELEPHONY_LOGI("report call event, eventId:%{public}d", info.eventId);
152 std::lock_guard<std::mutex> lock(mutex_);
153 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
154 for (; it != callbackPtrList_.end(); ++it) {
155 if ((*it)) {
156 ret = (*it)->OnCallEventChange(info);
157 if (ret != TELEPHONY_SUCCESS) {
158 TELEPHONY_LOGW("OnCallEventChange failed, errcode:%{public}d, bundleName:%{public}s", ret,
159 ((*it)->GetBundleName()).c_str());
160 continue;
161 }
162 }
163 }
164 TELEPHONY_LOGI("report call event[%{public}d] info success", info.eventId);
165 return ret;
166 }
167
ReportAsyncResults(const CallResultReportId reportId,AppExecFwk::PacMap & resultInfo)168 int32_t CallAbilityReportProxy::ReportAsyncResults(
169 const CallResultReportId reportId, AppExecFwk::PacMap &resultInfo)
170 {
171 int32_t ret = TELEPHONY_ERR_FAIL;
172 std::lock_guard<std::mutex> lock(mutex_);
173 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
174 for (; it != callbackPtrList_.end(); ++it) {
175 if ((*it)) {
176 ret = (*it)->OnReportAsyncResults(reportId, resultInfo);
177 if (ret != TELEPHONY_SUCCESS) {
178 TELEPHONY_LOGW("ReportAsyncResults failed, errcode:%{public}d, bundleName:%{public}s", ret,
179 ((*it)->GetBundleName()).c_str());
180 continue;
181 }
182 }
183 }
184 TELEPHONY_LOGI("ReportAsyncResults success, reportId:%{public}d", reportId);
185 return ret;
186 }
187
OttCallRequest(OttCallRequestId requestId,AppExecFwk::PacMap & info)188 int32_t CallAbilityReportProxy::OttCallRequest(OttCallRequestId requestId, AppExecFwk::PacMap &info)
189 {
190 int32_t ret = TELEPHONY_ERR_FAIL;
191 std::string name = "";
192 std::lock_guard<std::mutex> lock(mutex_);
193 std::list<sptr<ICallAbilityCallback>>::iterator it = callbackPtrList_.begin();
194 for (; it != callbackPtrList_.end(); ++it) {
195 name = (*it)->GetBundleName();
196 if (name == "com.ohos.callservice") {
197 ret = (*it)->OnOttCallRequest(requestId, info);
198 if (ret != TELEPHONY_SUCCESS) {
199 TELEPHONY_LOGW(
200 "OttCallRequest failed, errcode:%{public}d, bundleName:%{public}s", ret, name.c_str());
201 break;
202 }
203 }
204 }
205 TELEPHONY_LOGI("OttCallRequest success, requestId:%{public}d", requestId);
206 return ret;
207 }
208 } // namespace Telephony
209 } // namespace OHOS
210