1e1c44949Sopenharmony_ci/*
2e1c44949Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
3e1c44949Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e1c44949Sopenharmony_ci * you may not use this file except in compliance with the License.
5e1c44949Sopenharmony_ci * You may obtain a copy of the License at
6e1c44949Sopenharmony_ci *
7e1c44949Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e1c44949Sopenharmony_ci *
9e1c44949Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e1c44949Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e1c44949Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e1c44949Sopenharmony_ci * See the License for the specific language governing permissions and
13e1c44949Sopenharmony_ci * limitations under the License.
14e1c44949Sopenharmony_ci */
15e1c44949Sopenharmony_ci
16e1c44949Sopenharmony_ci#include "call_broadcast_subscriber.h"
17e1c44949Sopenharmony_ci
18e1c44949Sopenharmony_ci#include <string_ex.h>
19e1c44949Sopenharmony_ci
20e1c44949Sopenharmony_ci#include "audio_device_manager.h"
21e1c44949Sopenharmony_ci#include "call_manager_errors.h"
22e1c44949Sopenharmony_ci#include "telephony_log_wrapper.h"
23e1c44949Sopenharmony_ci#include "call_control_manager.h"
24e1c44949Sopenharmony_ci#include "satellite_call_control.h"
25e1c44949Sopenharmony_ci#include "securec.h"
26e1c44949Sopenharmony_ci#include "call_superprivacy_control_manager.h"
27e1c44949Sopenharmony_ci#include "call_connect_ability.h"
28e1c44949Sopenharmony_ci#include "call_ability_connect_callback.h"
29e1c44949Sopenharmony_ci
30e1c44949Sopenharmony_ci
31e1c44949Sopenharmony_cinamespace OHOS {
32e1c44949Sopenharmony_cinamespace Telephony {
33e1c44949Sopenharmony_ciusing namespace OHOS::EventFwk;
34e1c44949Sopenharmony_ciCallBroadcastSubscriber::CallBroadcastSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo)
35e1c44949Sopenharmony_ci    : CommonEventSubscriber(subscriberInfo)
36e1c44949Sopenharmony_ci{
37e1c44949Sopenharmony_ci    memberFuncMap_[UNKNOWN_BROADCAST_EVENT] =
38e1c44949Sopenharmony_ci        [this](const EventFwk::CommonEventData &data) { UnknownBroadcast(data); };
39e1c44949Sopenharmony_ci    memberFuncMap_[SIM_STATE_BROADCAST_EVENT] =
40e1c44949Sopenharmony_ci        [this](const EventFwk::CommonEventData &data) { SimStateBroadcast(data); };
41e1c44949Sopenharmony_ci    memberFuncMap_[CONNECT_CALLUI_SERVICE] =
42e1c44949Sopenharmony_ci        [this](const EventFwk::CommonEventData &data) { ConnectCallUiServiceBroadcast(data); };
43e1c44949Sopenharmony_ci    memberFuncMap_[HIGH_TEMP_LEVEL_CHANGED] =
44e1c44949Sopenharmony_ci        [this](const EventFwk::CommonEventData &data) { HighTempLevelChangedBroadcast(data); };
45e1c44949Sopenharmony_ci    memberFuncMap_[SUPER_PRIVACY_MODE] =
46e1c44949Sopenharmony_ci        [this](const EventFwk::CommonEventData &data) { ConnectCallUiSuperPrivacyModeBroadcast(data); };
47e1c44949Sopenharmony_ci    memberFuncMap_[BLUETOOTH_REMOTEDEVICE_NAME_UPDATE] =
48e1c44949Sopenharmony_ci        [this](const EventFwk::CommonEventData &data) { UpdateBluetoothDeviceName(data); };
49e1c44949Sopenharmony_ci    memberFuncMap_[USER_SWITCHED] =
50e1c44949Sopenharmony_ci        [this](const EventFwk::CommonEventData &data) { ConnectCallUiUserSwitchedBroadcast(data); };
51e1c44949Sopenharmony_ci}
52e1c44949Sopenharmony_ci
53e1c44949Sopenharmony_civoid CallBroadcastSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
54e1c44949Sopenharmony_ci{
55e1c44949Sopenharmony_ci    uint32_t code = UNKNOWN_BROADCAST_EVENT;
56e1c44949Sopenharmony_ci    OHOS::EventFwk::Want want = data.GetWant();
57e1c44949Sopenharmony_ci    std::string action = data.GetWant().GetAction();
58e1c44949Sopenharmony_ci    TELEPHONY_LOGI("receive one broadcast:%{public}s", action.c_str());
59e1c44949Sopenharmony_ci    if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED) {
60e1c44949Sopenharmony_ci        code = SIM_STATE_BROADCAST_EVENT;
61e1c44949Sopenharmony_ci    } else if (action == "event.custom.contacts.PAGE_STATE_CHANGE") {
62e1c44949Sopenharmony_ci        code = CONNECT_CALLUI_SERVICE;
63e1c44949Sopenharmony_ci    } else if (action == "usual.event.thermal.satcomm.HIGH_TEMP_LEVEL") {
64e1c44949Sopenharmony_ci        code = HIGH_TEMP_LEVEL_CHANGED;
65e1c44949Sopenharmony_ci    } else if (action == "usual.event.SUPER_PRIVACY_MODE") {
66e1c44949Sopenharmony_ci        code = SUPER_PRIVACY_MODE;
67e1c44949Sopenharmony_ci    } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_NAME_UPDATE) {
68e1c44949Sopenharmony_ci        code = BLUETOOTH_REMOTEDEVICE_NAME_UPDATE;
69e1c44949Sopenharmony_ci    } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
70e1c44949Sopenharmony_ci        code = USER_SWITCHED;
71e1c44949Sopenharmony_ci    } else {
72e1c44949Sopenharmony_ci        code = UNKNOWN_BROADCAST_EVENT;
73e1c44949Sopenharmony_ci    }
74e1c44949Sopenharmony_ci    auto itFunc = memberFuncMap_.find(code);
75e1c44949Sopenharmony_ci    if (itFunc != memberFuncMap_.end()) {
76e1c44949Sopenharmony_ci        auto memberFunc = itFunc->second;
77e1c44949Sopenharmony_ci        if (memberFunc != nullptr) {
78e1c44949Sopenharmony_ci            return memberFunc(data);
79e1c44949Sopenharmony_ci        }
80e1c44949Sopenharmony_ci    }
81e1c44949Sopenharmony_ci}
82e1c44949Sopenharmony_ci
83e1c44949Sopenharmony_civoid CallBroadcastSubscriber::UnknownBroadcast(const EventFwk::CommonEventData &data)
84e1c44949Sopenharmony_ci{
85e1c44949Sopenharmony_ci    TELEPHONY_LOGI("you receive one unknown broadcast!");
86e1c44949Sopenharmony_ci}
87e1c44949Sopenharmony_ci
88e1c44949Sopenharmony_civoid CallBroadcastSubscriber::SimStateBroadcast(const EventFwk::CommonEventData &data)
89e1c44949Sopenharmony_ci{
90e1c44949Sopenharmony_ci    TELEPHONY_LOGI("sim state broadcast code:%{public}d", data.GetCode());
91e1c44949Sopenharmony_ci}
92e1c44949Sopenharmony_ci
93e1c44949Sopenharmony_civoid CallBroadcastSubscriber::ConnectCallUiServiceBroadcast(const EventFwk::CommonEventData &data)
94e1c44949Sopenharmony_ci{
95e1c44949Sopenharmony_ci    bool isConnectService = data.GetWant().GetBoolParam("isShouldConnect", false);
96e1c44949Sopenharmony_ci    TELEPHONY_LOGI("isConnectService:%{public}d", isConnectService);
97e1c44949Sopenharmony_ci    DelayedSingleton<CallControlManager>::GetInstance()->ConnectCallUiService(isConnectService);
98e1c44949Sopenharmony_ci}
99e1c44949Sopenharmony_ci
100e1c44949Sopenharmony_civoid CallBroadcastSubscriber::HighTempLevelChangedBroadcast(const EventFwk::CommonEventData &data)
101e1c44949Sopenharmony_ci{
102e1c44949Sopenharmony_ci    int32_t satcommHighTempLevel = data.GetWant().GetIntParam("satcomm_high_temp_level", -1);
103e1c44949Sopenharmony_ci    TELEPHONY_LOGI("satcommHighTempLevel:%{public}d", satcommHighTempLevel);
104e1c44949Sopenharmony_ci    DelayedSingleton<SatelliteCallControl>::GetInstance()->SetSatcommTempLevel(satcommHighTempLevel);
105e1c44949Sopenharmony_ci}
106e1c44949Sopenharmony_ci
107e1c44949Sopenharmony_civoid CallBroadcastSubscriber::ConnectCallUiSuperPrivacyModeBroadcast(const EventFwk::CommonEventData &data)
108e1c44949Sopenharmony_ci{
109e1c44949Sopenharmony_ci    bool isInCall = data.GetWant().GetBoolParam("isInCall", false);
110e1c44949Sopenharmony_ci    bool isHangup = data.GetWant().GetBoolParam("isHangup", false);
111e1c44949Sopenharmony_ci    if (isInCall && isHangup) {
112e1c44949Sopenharmony_ci        DelayedSingleton<CallSuperPrivacyControlManager>::GetInstance()->CloseAllCall();
113e1c44949Sopenharmony_ci        return;
114e1c44949Sopenharmony_ci    }
115e1c44949Sopenharmony_ci    int32_t videoState = data.GetWant().GetIntParam("videoState", -1);
116e1c44949Sopenharmony_ci    bool isAnswer = data.GetWant().GetBoolParam("isAnswer", false);
117e1c44949Sopenharmony_ci    DelayedSingleton<CallSuperPrivacyControlManager>::GetInstance()->SetOldSuperPrivacyMode();
118e1c44949Sopenharmony_ci    DelayedSingleton<CallSuperPrivacyControlManager>::GetInstance()->SetIsChangeSuperPrivacyMode(true);
119e1c44949Sopenharmony_ci    if (isInCall) {
120e1c44949Sopenharmony_ci        DelayedSingleton<CallSuperPrivacyControlManager>::GetInstance()->CloseSuperPrivacyMode();
121e1c44949Sopenharmony_ci        return;
122e1c44949Sopenharmony_ci    }
123e1c44949Sopenharmony_ci    TELEPHONY_LOGI("CallUiSuperPrivacyModeBroadcast isAnswer:%{public}d", isAnswer);
124e1c44949Sopenharmony_ci    if (isAnswer) {
125e1c44949Sopenharmony_ci        int32_t callId = data.GetWant().GetIntParam("callId", -1);
126e1c44949Sopenharmony_ci        TELEPHONY_LOGI("CallUiSuperPrivacyModeBroadcast_Answer callId:%{public}d", callId);
127e1c44949Sopenharmony_ci        DelayedSingleton<CallSuperPrivacyControlManager>::GetInstance()->
128e1c44949Sopenharmony_ci            CloseAnswerSuperPrivacyMode(callId, videoState);
129e1c44949Sopenharmony_ci    } else {
130e1c44949Sopenharmony_ci        std::string phoneNumber = data.GetWant().GetStringParam("phoneNumber");
131e1c44949Sopenharmony_ci        std::u16string phNumber = Str8ToStr16(phoneNumber);
132e1c44949Sopenharmony_ci        int32_t accountId = data.GetWant().GetIntParam("accountId", -1);
133e1c44949Sopenharmony_ci        TELEPHONY_LOGI("CallUiSuperPrivacyModeBroadcast_Answer accountId:%{public}d", accountId);
134e1c44949Sopenharmony_ci        int32_t dialScene = data.GetWant().GetIntParam("dialScene", -1);
135e1c44949Sopenharmony_ci        int32_t dialType = data.GetWant().GetIntParam("dialType", -1);
136e1c44949Sopenharmony_ci        int32_t callType = data.GetWant().GetIntParam("callType", -1);
137e1c44949Sopenharmony_ci        DelayedSingleton<CallSuperPrivacyControlManager>::GetInstance()->CloseCallSuperPrivacyMode(
138e1c44949Sopenharmony_ci            phNumber, accountId, videoState, dialScene, dialType, callType);
139e1c44949Sopenharmony_ci    }
140e1c44949Sopenharmony_ci}
141e1c44949Sopenharmony_ci
142e1c44949Sopenharmony_civoid CallBroadcastSubscriber::UpdateBluetoothDeviceName(const EventFwk::CommonEventData &data)
143e1c44949Sopenharmony_ci{
144e1c44949Sopenharmony_ci    std::string macAddress = data.GetWant().GetStringParam("deviceAddr");
145e1c44949Sopenharmony_ci    std::string deviceName = data.GetWant().GetStringParam("remoteName");
146e1c44949Sopenharmony_ci    DelayedSingleton<AudioDeviceManager>::GetInstance()->UpdateBluetoothDeviceName(macAddress, deviceName);
147e1c44949Sopenharmony_ci}
148e1c44949Sopenharmony_ci
149e1c44949Sopenharmony_civoid CallBroadcastSubscriber::ConnectCallUiUserSwitchedBroadcast(const EventFwk::CommonEventData &data)
150e1c44949Sopenharmony_ci{
151e1c44949Sopenharmony_ci    if (!DelayedSingleton<CallConnectAbility>::GetInstance()->GetConnectFlag()) {
152e1c44949Sopenharmony_ci        TELEPHONY_LOGE("is not connected");
153e1c44949Sopenharmony_ci        return;
154e1c44949Sopenharmony_ci    }
155e1c44949Sopenharmony_ci    TELEPHONY_LOGI("User switched, need reconnect ability");
156e1c44949Sopenharmony_ci    DelayedSingleton<CallConnectAbility>::GetInstance()->DisconnectAbility();
157e1c44949Sopenharmony_ci    sptr<CallAbilityConnectCallback> connectCallback_ = new CallAbilityConnectCallback();
158e1c44949Sopenharmony_ci    connectCallback_->ReConnectAbility();
159e1c44949Sopenharmony_ci}
160e1c44949Sopenharmony_ci} // namespace Telephony
161e1c44949Sopenharmony_ci} // namespace OHOS
162