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