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_state_processor.h"
17
18 #include "audio_scene_processor.h"
19
20 #include "telephony_log_wrapper.h"
21
22 namespace OHOS {
23 namespace Telephony {
CallStateProcessor()24 CallStateProcessor::CallStateProcessor() {}
25
~CallStateProcessor()26 CallStateProcessor::~CallStateProcessor()
27 {
28 activeCalls_.clear();
29 alertingCalls_.clear();
30 incomingCalls_.clear();
31 holdingCalls_.clear();
32 dialingCalls_.clear();
33 }
34
AddCall(int32_t callId,TelCallState state)35 void CallStateProcessor::AddCall(int32_t callId, TelCallState state)
36 {
37 std::lock_guard<std::mutex> lock(mutex_);
38 switch (state) {
39 case TelCallState::CALL_STATUS_DIALING:
40 if (dialingCalls_.count(callId) == EMPTY_VALUE) {
41 TELEPHONY_LOGI("add call , state : dialing");
42 dialingCalls_.insert(callId);
43 }
44 break;
45 case TelCallState::CALL_STATUS_ALERTING:
46 if (alertingCalls_.count(callId) == EMPTY_VALUE) {
47 TELEPHONY_LOGI("add call , state : alerting");
48 alertingCalls_.insert(callId);
49 }
50 break;
51 case TelCallState::CALL_STATUS_INCOMING:
52 if (incomingCalls_.count(callId) == EMPTY_VALUE) {
53 TELEPHONY_LOGI("add call , state : incoming");
54 incomingCalls_.insert(callId);
55 }
56 break;
57 case TelCallState::CALL_STATUS_ACTIVE:
58 if (activeCalls_.count(callId) == EMPTY_VALUE) {
59 TELEPHONY_LOGI("add call , state : active");
60 activeCalls_.insert(callId);
61 }
62 break;
63 case TelCallState::CALL_STATUS_HOLDING:
64 if (holdingCalls_.count(callId) == EMPTY_VALUE) {
65 TELEPHONY_LOGI("add call , state : holding");
66 holdingCalls_.insert(callId);
67 }
68 break;
69 default:
70 break;
71 }
72 }
73
DeleteCall(int32_t callId,TelCallState state)74 void CallStateProcessor::DeleteCall(int32_t callId, TelCallState state)
75 {
76 std::lock_guard<std::mutex> lock(mutex_);
77 switch (state) {
78 case TelCallState::CALL_STATUS_DIALING:
79 if (dialingCalls_.count(callId) > EMPTY_VALUE) {
80 TELEPHONY_LOGI("erase call , state : dialing");
81 dialingCalls_.erase(callId);
82 }
83 break;
84 case TelCallState::CALL_STATUS_ALERTING:
85 if (alertingCalls_.count(callId) > EMPTY_VALUE) {
86 TELEPHONY_LOGI("erase call , state : alerting");
87 alertingCalls_.erase(callId);
88 }
89 break;
90 case TelCallState::CALL_STATUS_INCOMING:
91 if (incomingCalls_.count(callId) > EMPTY_VALUE) {
92 TELEPHONY_LOGI("erase call , state : incoming");
93 incomingCalls_.erase(callId);
94 }
95 break;
96 case TelCallState::CALL_STATUS_ACTIVE:
97 if (activeCalls_.count(callId) > EMPTY_VALUE) {
98 TELEPHONY_LOGI("erase call , state : active");
99 activeCalls_.erase(callId);
100 }
101 break;
102 case TelCallState::CALL_STATUS_HOLDING:
103 if (holdingCalls_.count(callId) > EMPTY_VALUE) {
104 TELEPHONY_LOGI("erase call , state : holding");
105 holdingCalls_.erase(callId);
106 }
107 break;
108 default:
109 break;
110 }
111 }
112
GetCallNumber(TelCallState state)113 int32_t CallStateProcessor::GetCallNumber(TelCallState state)
114 {
115 std::lock_guard<std::mutex> lock(mutex_);
116 int32_t number = EMPTY_VALUE;
117 switch (state) {
118 case TelCallState::CALL_STATUS_DIALING:
119 number = static_cast<int32_t>(dialingCalls_.size());
120 break;
121 case TelCallState::CALL_STATUS_ALERTING:
122 number = static_cast<int32_t>(alertingCalls_.size());
123 break;
124 case TelCallState::CALL_STATUS_INCOMING:
125 number = static_cast<int32_t>(incomingCalls_.size());
126 break;
127 case TelCallState::CALL_STATUS_ACTIVE:
128 number = static_cast<int32_t>(activeCalls_.size());
129 break;
130 case TelCallState::CALL_STATUS_HOLDING:
131 number = static_cast<int32_t>(holdingCalls_.size());
132 break;
133 default:
134 break;
135 }
136 return number;
137 }
138
ShouldSwitchState(TelCallState callState)139 bool CallStateProcessor::ShouldSwitchState(TelCallState callState)
140 {
141 bool shouldSwitch = false;
142 switch (callState) {
143 case TelCallState::CALL_STATUS_DIALING:
144 shouldSwitch = (dialingCalls_.size() == EXIST_ONLY_ONE_CALL && activeCalls_.empty() &&
145 incomingCalls_.empty() && alertingCalls_.empty());
146 break;
147 case TelCallState::CALL_STATUS_ALERTING:
148 shouldSwitch = (alertingCalls_.size() == EXIST_ONLY_ONE_CALL && activeCalls_.empty() &&
149 incomingCalls_.empty() && dialingCalls_.empty());
150 break;
151 case TelCallState::CALL_STATUS_INCOMING:
152 shouldSwitch = (incomingCalls_.size() == EXIST_ONLY_ONE_CALL && activeCalls_.empty() &&
153 dialingCalls_.empty() && alertingCalls_.empty());
154 break;
155 case TelCallState::CALL_STATUS_ACTIVE:
156 shouldSwitch = (activeCalls_.size() == EXIST_ONLY_ONE_CALL);
157 break;
158 default:
159 break;
160 }
161 return shouldSwitch;
162 }
163
UpdateCurrentCallState()164 bool CallStateProcessor::UpdateCurrentCallState()
165 {
166 if (activeCalls_.size() > EMPTY_VALUE) {
167 // no need to update call state while active calls exists
168 return false;
169 }
170 AudioEvent event = AudioEvent::UNKNOWN_EVENT;
171 if (holdingCalls_.size() > EMPTY_VALUE) {
172 event = AudioEvent::SWITCH_HOLDING_STATE;
173 } else if (incomingCalls_.size() > EMPTY_VALUE) {
174 event = AudioEvent::SWITCH_INCOMING_STATE;
175 } else if (dialingCalls_.size() > EMPTY_VALUE) {
176 event = AudioEvent::SWITCH_DIALING_STATE;
177 } else if (alertingCalls_.size() > EMPTY_VALUE) {
178 event = AudioEvent::SWITCH_ALERTING_STATE;
179 } else {
180 event = AudioEvent::SWITCH_AUDIO_INACTIVE_STATE;
181 }
182 return DelayedSingleton<AudioSceneProcessor>::GetInstance()->ProcessEvent(event);
183 }
184
GetCurrentActiveCall()185 int32_t CallStateProcessor::GetCurrentActiveCall()
186 {
187 std::lock_guard<std::mutex> lock(mutex_);
188 if (activeCalls_.size() > EMPTY_VALUE) {
189 return (*activeCalls_.begin());
190 }
191 return INVALID_CALLID;
192 }
193 } // namespace Telephony
194 } // namespace OHOS