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