1 /*
2  * Copyright (C) 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 "audio_device_manager.h"
17 #include "call_ability_connect_callback.h"
18 #include "call_ability_report_proxy.h"
19 #include "call_connect_ability.h"
20 #include "call_object_manager.h"
21 #include "telephony_log_wrapper.h"
22 #include "call_superprivacy_control_manager.h"
23 #include "call_voice_assistant_manager.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 const int32_t CONNECT_ABILITY_SUCCESS = 0;
28 const int32_t UNEXPECT_DISCONNECT_CODE = -1;
29 
OnAbilityConnectDone( const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)30 void CallAbilityConnectCallback::OnAbilityConnectDone(
31     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
32 {
33     TELEPHONY_LOGW("connect callui result code: %{public}d", resultCode);
34     if (resultCode == CONNECT_ABILITY_SUCCESS) {
35         DelayedSingleton<CallConnectAbility>::GetInstance()->SetConnectFlag(true);
36         DelayedSingleton<CallConnectAbility>::GetInstance()->SetConnectingFlag(false);
37         DelayedSingleton<CallConnectAbility>::GetInstance()->NotifyAll();
38 
39         CallEventInfo eventInfo;
40         (void)memset_s(&eventInfo, sizeof(CallEventInfo), 0, sizeof(CallEventInfo));
41         bool isSuperPrivacyMode = DelayedSingleton<CallSuperPrivacyControlManager>::GetInstance()->
42             GetCurrentIsSuperPrivacyMode();
43         TELEPHONY_LOGI("OnAbilityConnectDone SuperPrivacyMode:%{public}d", isSuperPrivacyMode);
44         if (isSuperPrivacyMode) {
45             eventInfo.eventId = CallAbilityEventId::EVENT_IS_SUPER_PRIVACY_MODE_ON;
46         } else {
47             eventInfo.eventId = CallAbilityEventId::EVENT_IS_SUPER_PRIVACY_MODE_OFF;
48         }
49         DelayedSingleton<CallAbilityReportProxy>::GetInstance()->CallEventUpdated(eventInfo);
50         DelayedSingleton<AudioDeviceManager>::GetInstance()->UpdateEarpieceDevice();
51         CallVoiceAssistantManager::GetInstance()->PublishCommonEvent(true, std::string("call_ui_connect_done"));
52     }
53 }
54 
OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)55 void CallAbilityConnectCallback::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
56 {
57     TELEPHONY_LOGW("disconnect callui result code: %{public}d", resultCode);
58     DelayedSingleton<CallConnectAbility>::GetInstance()->SetConnectFlag(false);
59     DelayedSingleton<CallConnectAbility>::GetInstance()->SetDisconnectingFlag(false);
60     if (resultCode == UNEXPECT_DISCONNECT_CODE) {
61         ReConnectAbility();
62     }
63 }
64 
ReConnectAbility()65 void CallAbilityConnectCallback::ReConnectAbility()
66 {
67     TELEPHONY_LOGI("ReConnectAbility");
68     if (!CallObjectManager::HasCallExist()) {
69         TELEPHONY_LOGE("callObjectPtrList_ is empty, no need to report");
70         return;
71     }
72     std::vector<CallAttributeInfo> callAttributeInfo = CallObjectManager::GetAllCallInfoList();
73     std::vector<CallAttributeInfo>::iterator it = callAttributeInfo.begin();
74     while (it != callAttributeInfo.end()) {
75         CallAttributeInfo info = (*it);
76         TelCallState callState = info.callState;
77         it++;
78         if (callState == TelCallState::CALL_STATUS_DISCONNECTING ||
79             callState == TelCallState::CALL_STATUS_DISCONNECTED ||
80             callState == TelCallState::CALL_STATUS_UNKNOWN || callState == TelCallState::CALL_STATUS_IDLE) {
81             TELEPHONY_LOGE("no need to report");
82             continue;
83         }
84         DelayedSingleton<CallConnectAbility>::GetInstance()->ConnectAbility();
85         return;
86     }
87 }
88 } // namespace Telephony
89 } // namespace OHOS
90