1 /*
2  * Copyright (C) 2023-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 #ifndef CALL_VOICE_ASSISTANT_MANAGER_H
17 #define CALL_VOICE_ASSISTANT_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string_ex.h>
22 
23 #include "call_earthquake_alarm_locator.h"
24 #include "data_ability_observer_stub.h"
25 #include "call_manager_inner_type.h"
26 #include "call_state_listener_base.h"
27 #include "audio_control_manager.h"
28 
29 namespace OHOS {
30 namespace Telephony {
31 
32 struct IncomingContactInformation {
33     std::string dialOrCome = "";
34     std::string phoneNumber = "";
35     std::string incomingName = "";
36     std::string isContact = "0";
37     std::string numberLocation = "";
38     int32_t accountId = -1;
39     int32_t callId = -1;
40     int32_t stopBroadcasting = 0;
41     int32_t call_status = -1;
42     sptr<CallBase> call = nullptr;
43     bool isQueryComplete = false;
44 };
45 
46 class CallVoiceAssistantManager : public CallStateListenerBase {
47 public:
48     CallVoiceAssistantManager() = default;
49     virtual ~CallVoiceAssistantManager();
50     std::shared_ptr<DataShare::DataShareHelper> Initial();
51     void Release();
52     int QueryValue(const std::string& key, std::string& value);
53     bool IsSwitchOn(const std::string& switchState);
54     bool RegisterListenSwitchState();
55     bool UnRegisterListenSwitchState();
56     bool ConnectAbility(int32_t callId);
57     bool DisconnectAbility();
58     void PublishCommonEvent(bool isConnect, std::string publisher);
59     void OnStartService(const std::string& isDial, const int32_t& callId);
60     void OnStopService();
61     void UpdateReplyData(const std::string& str);
62     void CallStateUpdated(sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState) override;
63     void CallStatusIncoming(const int32_t& callId, const int32_t& accountId);
64     void CallStatusDialing(const int32_t& callId, const int32_t& accountId);
65     void CallStatusActive(const int32_t& callId, const int32_t& accountId);
66     void CallStatusDisconnected(const int32_t& callId, const int32_t& accountId);
67     void MuteRinger();
68     std::shared_ptr<IncomingContactInformation> GetContactInfo(int32_t callId);
69     void SendRequest(const std::shared_ptr<IncomingContactInformation> info, bool isNeed);
70     void UpdateRemoteObject(const sptr<IRemoteObject> &object, int32_t callId,
71         const sptr<AAFwk::IAbilityConnection> callback);
72     std::u16string GetSendString(const std::shared_ptr<IncomingContactInformation> nowInfo);
73     void SetIsControlSwitchOn(bool state);
74     bool GetIsControlSwitchOn();
75     bool GetIsPlayRing();
76     void UpdateVoipCallState(int32_t state);
77     bool IsStartVoiceBroadcast();
78     int32_t CheckTelCallState(TelCallState state);
79     void UpdateNumberLocation(const std::string& location, int32_t callId);
80     void UpdateContactInfo(const ContactInfo &info, int32_t callId);
81     static std::shared_ptr<CallVoiceAssistantManager> GetInstance();
82 
83 private:
84     std::string broadcastCheck = "0";
85     std::string controlCheck = "0";
86     bool isConnectService = false;
87     std::string isplay = "0";
88     bool isControlSwitchOn = false;
89     bool isBroadcastSwitchOn = false;
90     bool isQueryedBroadcastSwitch = false;
91     sptr<AAFwk::IAbilityConnection> connectCallback_ = nullptr;
92     sptr<AAFwk::IDataAbilityObserver> settingsCallback_ = nullptr;
93     std::mutex mutex_;
94     int32_t nowCallId = -1;
95     int32_t nowAccountId = -1;
96     int32_t nowVoipCallState = -1;
97     sptr<IRemoteObject> mRemoteObject = nullptr;
98     std::map<int32_t, std::shared_ptr<IncomingContactInformation>> accountIds = {};
99     static std::shared_ptr<CallVoiceAssistantManager> mInstance_;
100 
101 public:
102     const char *SETTINGS_DATASHARE_URI =
103         "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
104     const char *SETTINGS_DATASHARE_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
105     const std::string SETTINGS_DATASHARE_URI_KEY = std::string(SETTINGS_DATASHARE_URI) + std::string("&key=");
106     const std::string CONTROL_SWITCH = "incoming_call_voice_control_switch";
107     const std::string BROADCAST_SWITCH = "incoming_call_voice_broadcast_switch";
108     const std::string DEFAULT_STRING = "";
109     const std::u16string DEFAULT_U16STRING = u"";
110     const std::string SWITCH_TURN_OFF = "0";
111     const std::string SWITCH_TURN_ON = "1";
112     const std::string INCOMING = "come";
113     const std::string DIALING = "dial";
114     const std::string CONTROL_SWITCH_STATE_CHANGE_EVENT = "usual.event.CALL_UI_REPORT_SWITCH_STATE_CHANGE";
115     const std::string IS_CONNECT_SERVICE = "isConnectVoiceAssistant";
116     const std::string IS_PLAY_RING = "call_manager_play_ring";
117     const std::string BUNDLE_NAME = "";
118     const std::string ABILITY_NAME = "CallVoiceControlAbility";
119     const std::string CONTROL_CHECK_RESULT = "incomingCallVoiceControlCheckResult";
120     const std::string BROADCAST_CHECK_RESULT = "incomingCallVoiceBroadcastCheckResult";
121     const int CHECK_CODE = 1006;
122     const int FAIL_CODE = -1;
123 };
124 
125 class VoiceAssistantConnectCallback : public IRemoteStub<AAFwk::IAbilityConnection> {
126 public:
VoiceAssistantConnectCallback(int32_t id)127     VoiceAssistantConnectCallback(int32_t id) : startId(id) {};
128     virtual ~VoiceAssistantConnectCallback() = default;
129     void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
130         int resultCode) override;
131     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
132 
133 private:
134     int32_t startId = -1;
135 };
136 
137 class VoiceAssistantSwitchObserver : public AAFwk::DataAbilityObserverStub {
138 public:
139     VoiceAssistantSwitchObserver() = default;
140     virtual ~VoiceAssistantSwitchObserver() = default;
141     void OnChange() override;
142 };
143 
144 class VoiceAssistantRingSubscriber : public EventFwk::CommonEventSubscriber {
145 public:
146     VoiceAssistantRingSubscriber() = default;
147     explicit VoiceAssistantRingSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo);
148     virtual ~VoiceAssistantRingSubscriber() = default;
149     virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override;
150     static bool Initial();
151     static void Release();
152 
153 private:
154     static std::shared_ptr<VoiceAssistantRingSubscriber> subscriber_;
155 };
156 
157 }
158 }
159 #endif