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_state_listener.h"
17
18 #include "telephony_log_wrapper.h"
19
20 namespace OHOS {
21 namespace Telephony {
CallStateListener()22 CallStateListener::CallStateListener()
23 {
24 listenerSet_.clear();
25 }
26
~CallStateListener()27 CallStateListener::~CallStateListener()
28 {
29 listenerSet_.clear();
30 }
31
AddOneObserver(const std::shared_ptr<CallStateListenerBase> & observer)32 bool CallStateListener::AddOneObserver(const std::shared_ptr<CallStateListenerBase> &observer)
33 {
34 if (observer == nullptr) {
35 TELEPHONY_LOGE("observer is nullptr!");
36 return false;
37 }
38 std::lock_guard<std::mutex> lock(mutex_);
39 if (listenerSet_.count(observer) > 0) {
40 return true;
41 }
42 listenerSet_.insert(observer);
43 return true;
44 }
45
RemoveOneObserver(const std::shared_ptr<CallStateListenerBase> & observer)46 bool CallStateListener::RemoveOneObserver(const std::shared_ptr<CallStateListenerBase> &observer)
47 {
48 if (observer == nullptr) {
49 TELEPHONY_LOGE("observer is nullptr!");
50 return false;
51 }
52 std::lock_guard<std::mutex> lock(mutex_);
53 listenerSet_.erase(observer);
54 return true;
55 }
56
RemoveAllObserver()57 bool CallStateListener::RemoveAllObserver()
58 {
59 std::lock_guard<std::mutex> lock(mutex_);
60 listenerSet_.clear();
61 return true;
62 }
63
NewCallCreated(sptr<CallBase> & callObjectPtr)64 void CallStateListener::NewCallCreated(sptr<CallBase> &callObjectPtr)
65 {
66 if (callObjectPtr == nullptr) {
67 TELEPHONY_LOGE("callObjectPtr is nullptr!");
68 return;
69 }
70 std::lock_guard<std::mutex> lock(mutex_);
71 for (auto &observer : listenerSet_) {
72 observer->NewCallCreated(callObjectPtr);
73 }
74 }
75
CallDestroyed(const DisconnectedDetails & details)76 void CallStateListener::CallDestroyed(const DisconnectedDetails &details)
77 {
78 std::lock_guard<std::mutex> lock(mutex_);
79 for (auto &observer : listenerSet_) {
80 observer->CallDestroyed(details);
81 }
82 }
83
CallStateUpdated(sptr<CallBase> & callObjectPtr,TelCallState priorState,TelCallState nextState)84 void CallStateListener::CallStateUpdated(
85 sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState)
86 {
87 if (callObjectPtr == nullptr) {
88 TELEPHONY_LOGE("callObjectPtr is nullptr");
89 return;
90 }
91 std::lock_guard<std::mutex> lock(mutex_);
92 for (auto &observer : listenerSet_) {
93 observer->CallStateUpdated(callObjectPtr, priorState, nextState);
94 }
95 }
96
IncomingCallHungUp(sptr<CallBase> & callObjectPtr,bool isSendSms,std::string content)97 void CallStateListener::IncomingCallHungUp(sptr<CallBase> &callObjectPtr, bool isSendSms, std::string content)
98 {
99 if (callObjectPtr == nullptr) {
100 TELEPHONY_LOGE("callObjectPtr is nullptr");
101 return;
102 }
103 std::lock_guard<std::mutex> lock(mutex_);
104 for (auto &observer : listenerSet_) {
105 observer->IncomingCallHungUp(callObjectPtr, isSendSms, content);
106 }
107 }
108
IncomingCallActivated(sptr<CallBase> & callObjectPtr)109 void CallStateListener::IncomingCallActivated(sptr<CallBase> &callObjectPtr)
110 {
111 if (callObjectPtr == nullptr) {
112 TELEPHONY_LOGE("callObjectPtr is nullptr");
113 return;
114 }
115 std::lock_guard<std::mutex> lock(mutex_);
116 for (auto &observer : listenerSet_) {
117 observer->IncomingCallActivated(callObjectPtr);
118 }
119 }
120
CallEventUpdated(CallEventInfo & info)121 void CallStateListener::CallEventUpdated(CallEventInfo &info)
122 {
123 std::lock_guard<std::mutex> lock(mutex_);
124 for (auto &observer : listenerSet_) {
125 observer->CallEventUpdated(info);
126 }
127 }
128 } // namespace Telephony
129 } // namespace OHOS