1 /*
2 * Copyright (C) 2024 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_object_manager.h"
17 #include "call_wired_headset.h"
18
19 #include "input_manager.h"
20 #include "audio_device_manager.h"
21 #include "audio_control_manager.h"
22 #include "call_manager_base.h"
23 #include "system_ability_definition.h"
24 #include "call_control_manager.h"
25 #include "audio_proxy.h"
26 #include "call_manager_base.h"
27 #include "ims_conference.h"
28
29 namespace OHOS {
30 namespace Telephony {
31
32 using WiredHeadSetCallback = std::function<void(const std::shared_ptr<OHOS::MMI::KeyEvent>)>;
33
34 constexpr int32_t INVALID_VALUE = -1;
35 constexpr int32_t FINAL_KEY_DOWN_DURATION_TWO = 2000;
CallWiredHeadSet()36 CallWiredHeadSet::CallWiredHeadSet()
37 {
38 isProcessed_ = false;
39 downFirstTime_ = 0;
40 subscribeIdForPressedUp_ = INVALID_VALUE;
41 subscribeIdForPressedDown_ = INVALID_VALUE;
42 }
43
~CallWiredHeadSet()44 CallWiredHeadSet::~CallWiredHeadSet()
45 {
46 DeInit();
47 }
48
Init()49 bool CallWiredHeadSet::Init()
50 {
51 bool pressedUp = RegistKeyMutePressedUp();
52 bool pressedDown = RegistKeyMutePressedDown();
53 if (pressedUp && pressedDown) {
54 TELEPHONY_LOGI("CallWiredHeadSet Registed KeyMute Pressed callback succeed");
55 return true;
56 } else {
57 TELEPHONY_LOGE("CallWiredHeadSet Registed KeyMute Pressed callback failed,"
58 "pressedUp is %{public}s, pressedDown is %{public}s",
59 pressedUp ? "true" : "false", pressedDown ? "true" : "false");
60 return false;
61 }
62 }
63
DeInit()64 void CallWiredHeadSet::DeInit()
65 {
66 UnregistKeyMutePressedUp();
67 UnregistKeyMutePressedDown();
68 TELEPHONY_LOGI("CallWiredHeadSet UnRegisted KeyMute Pressed callback succeed");
69 }
70
InitOption(const std::set<int32_t> & preKeys,int32_t finalKey,bool isFinalKeyDown,int32_t duration)71 std::shared_ptr<MMI::KeyOption> CallWiredHeadSet::InitOption(
72 const std::set<int32_t> &preKeys, int32_t finalKey, bool isFinalKeyDown, int32_t duration)
73 {
74 std::shared_ptr<MMI::KeyOption> keyOption = std::make_shared<MMI::KeyOption>();
75 keyOption->SetFinalKeyDown(isFinalKeyDown);
76 keyOption->SetFinalKey(finalKey);
77 keyOption->SetPreKeys(preKeys);
78 keyOption->SetFinalKeyDownDuration(duration);
79 return keyOption;
80 }
81
RegistKeyMutePressedUp()82 bool CallWiredHeadSet::RegistKeyMutePressedUp()
83 {
84 WiredHeadSetCallback pressedFunc = [this](const std::shared_ptr<OHOS::MMI::KeyEvent> event) {
85 this->DealKeyMutePressedUp(event);
86 };
87
88 std::set<int32_t> preKeys;
89 std::shared_ptr<MMI::KeyOption> keyOption = InitOption(preKeys, MMI::KeyEvent::KEYCODE_HEADSETHOOK, false, 0);
90 subscribeIdForPressedUp_ = MMI::InputManager::GetInstance()->SubscribeKeyEvent(keyOption, pressedFunc);
91 return subscribeIdForPressedUp_ < 0 ? false : true;
92 }
93
RegistKeyMutePressedDown()94 bool CallWiredHeadSet::RegistKeyMutePressedDown()
95 {
96 WiredHeadSetCallback pressedFunc = [this](const std::shared_ptr<OHOS::MMI::KeyEvent> event) {
97 this->DealKeyMutePressedDown(event);
98 };
99
100 std::set<int32_t> preKeys;
101 std::shared_ptr<MMI::KeyOption> keyOption = InitOption(preKeys, MMI::KeyEvent::KEYCODE_HEADSETHOOK, true, 0);
102 subscribeIdForPressedDown_ = MMI::InputManager::GetInstance()->SubscribeKeyEvent(keyOption, pressedFunc);
103 return subscribeIdForPressedDown_ < 0 ? false : true;
104 }
105
UnregistKeyMutePressedUp()106 void CallWiredHeadSet::UnregistKeyMutePressedUp()
107 {
108 if (subscribeIdForPressedUp_ > 0) {
109 MMI::InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeIdForPressedUp_);
110 }
111 }
112
UnregistKeyMutePressedDown()113 void CallWiredHeadSet::UnregistKeyMutePressedDown()
114 {
115 if (subscribeIdForPressedDown_ > 0) {
116 MMI::InputManager::GetInstance()->UnsubscribeKeyEvent(subscribeIdForPressedDown_);
117 }
118 }
119
120 // wired headset mute key pressed callback method
DealKeyMutePressedUp(std::shared_ptr<MMI::KeyEvent> event)121 void CallWiredHeadSet::DealKeyMutePressedUp(std::shared_ptr<MMI::KeyEvent> event)
122 {
123 TELEPHONY_LOGI("received KeyMute Pressed Up event");
124 if (DelayedSingleton<AudioDeviceManager>::GetInstance()->IsWiredHeadsetConnected() == true) {
125 time_t currentTime = GetCurrentTimeMS();
126 if ((currentTime - downFirstTime_) < FINAL_KEY_DOWN_DURATION_TWO) {
127 DealKeyMuteShortPressed();
128 } else if (!isProcessed_) {
129 DealKeyMuteLongPressed();
130 }
131 }
132 downFirstTime_ = 0;
133 isProcessed_ = false;
134 }
135
DealKeyMutePressedDown(std::shared_ptr<MMI::KeyEvent> event)136 void CallWiredHeadSet::DealKeyMutePressedDown(std::shared_ptr<MMI::KeyEvent> event)
137 {
138 TELEPHONY_LOGI("received KeyMute Pressed Down event");
139 if (isProcessed_) {
140 return;
141 }
142 if (DelayedSingleton<AudioDeviceManager>::GetInstance()->IsWiredHeadsetConnected() == true) {
143 time_t currentTime = GetCurrentTimeMS();
144 if (downFirstTime_ == 0) {
145 downFirstTime_ = GetCurrentTimeMS();
146 return;
147 }
148 if (!isProcessed_ && (currentTime - downFirstTime_ >= FINAL_KEY_DOWN_DURATION_TWO)) {
149 DealKeyMuteLongPressed();
150 isProcessed_ = true;
151 }
152 }
153 }
154
155 // wired headset mute key short pressed callback method
DealKeyMuteShortPressed()156 void CallWiredHeadSet::DealKeyMuteShortPressed()
157 {
158 sptr<CallBase> ringingCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_RINGING);
159 if (ringingCall == nullptr) {
160 sptr<CallBase> holdCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_HOLD);
161 sptr<CallBase> activeCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_ACTIVE);
162 if (activeCall != nullptr && holdCall != nullptr) {
163 TELEPHONY_LOGI("DealKeyMuteShortPressed UnHoldCall callid(%{public}d)", holdCall->GetCallID());
164 DelayedSingleton<CallControlManager>::GetInstance()->UnHoldCall(holdCall->GetCallID());
165 return;
166 }
167 sptr<CallBase> call = CallObjectManager::GetAudioLiveCall();
168 if (call != nullptr) {
169 bool isMuted = DelayedSingleton<AudioProxy>::GetInstance()->IsMicrophoneMute();
170 TELEPHONY_LOGI("DealKeyMuteShortPressed SetMuted isMuted((%{public}d))", (!isMuted));
171 DelayedSingleton<CallControlManager>::GetInstance()->SetMuted(!isMuted);
172 }
173 } else {
174 TELEPHONY_LOGI("DealKeyMuteShortPressed AnswerCall callid(%{public}d)", ringingCall->GetCallID());
175 VideoStateType videoState = ringingCall->GetVideoStateType();
176 if (videoState != VideoStateType::TYPE_VOICE && videoState != VideoStateType::TYPE_VIDEO) {
177 TELEPHONY_LOGI("DealKeyMuteShortPressed get original call type");
178 videoState = static_cast<VideoStateType>(ringingCall->GetOriginalCallType());
179 }
180 DelayedSingleton<CallControlManager>::GetInstance()->AnswerCall(
181 ringingCall->GetCallID(), static_cast<int32_t>(videoState));
182 }
183 }
184
185 // wired headset mute key long pressed callback method
DealKeyMuteLongPressed()186 void CallWiredHeadSet::DealKeyMuteLongPressed()
187 {
188 sptr<CallBase> ringingCall = CallObjectManager::GetOneCallObject(CallRunningState::CALL_RUNNING_STATE_RINGING);
189 if (ringingCall != nullptr) {
190 TELEPHONY_LOGI("DealKeyMuteLongPressed RejectCall callid(%{public}d)", ringingCall->GetCallID());
191 ringingCall->RejectCall();
192 } else {
193 sptr<CallBase> foregroundCall = CallObjectManager::GetForegroundCall();
194 if (foregroundCall != nullptr) {
195 TelConferenceState confState = foregroundCall->GetTelConferenceState();
196 if (confState != TelConferenceState::TEL_CONFERENCE_IDLE) {
197 int32_t conferenceId = DelayedSingleton<ImsConference>::GetInstance()->GetMainCall();
198 TELEPHONY_LOGI("DealKeyMuteLongPressed HangUpCall conferenceId(%{public}d)", conferenceId);
199 DelayedSingleton<CallControlManager>::GetInstance()->HangUpCall(conferenceId);
200 } else {
201 TELEPHONY_LOGI("DealKeyMuteLongPressed HangUpCall callid(%{public}d)", foregroundCall->GetCallID());
202 foregroundCall->HangUpCall();
203 }
204 }
205 }
206 }
207
GetCurrentTimeMS()208 time_t CallWiredHeadSet::GetCurrentTimeMS()
209 {
210 std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tpMicro
211 = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
212 return tpMicro.time_since_epoch().count();
213 }
214 } // namespace Telephony
215 } // namespace OHOS