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 #include "ffrt.h" 29 30 namespace OHOS { 31 namespace Telephony { 32 33 struct IncomingContactInformation { 34 std::string dialOrCome = ""; 35 std::string phoneNumber = ""; 36 std::string incomingName = ""; 37 std::string isContact = "0"; 38 std::string numberLocation = ""; 39 int32_t accountId = -1; 40 int32_t callId = -1; 41 int32_t stopBroadcasting = 0; 42 int32_t call_status = -1; 43 sptr<CallBase> call = nullptr; 44 bool isQueryComplete = false; 45 }; 46 47 class CallVoiceAssistantManager : public CallStateListenerBase { 48 public: 49 CallVoiceAssistantManager() = default; 50 virtual ~CallVoiceAssistantManager(); 51 std::shared_ptr<DataShare::DataShareHelper> Initial(); 52 void Release(); 53 int QueryValue(const std::string& key, std::string& value); 54 bool IsSwitchOn(const std::string& switchState); 55 bool RegisterListenSwitchState(); 56 bool UnRegisterListenSwitchState(); 57 bool ConnectAbility(int32_t callId); 58 bool DisconnectAbility(); 59 void PublishCommonEvent(bool isConnect, std::string publisher); 60 void OnStartService(const std::string& isDial, const int32_t& callId); 61 void OnStopService(bool isDestructor = false); 62 void UpdateReplyData(const std::string& str); 63 void CallStateUpdated(sptr<CallBase> &callObjectPtr, TelCallState priorState, TelCallState nextState) override; 64 void CallStatusIncoming(const int32_t& callId, const int32_t& accountId); 65 void CallStatusDialing(const int32_t& callId, const int32_t& accountId); 66 void CallStatusActive(const int32_t& callId, const int32_t& accountId); 67 void CallStatusDisconnected(const int32_t& callId, const int32_t& accountId); 68 void MuteRinger(); 69 std::shared_ptr<IncomingContactInformation> GetContactInfo(int32_t callId); 70 void SendRequest(const std::shared_ptr<IncomingContactInformation> info, bool isNeed); 71 void UpdateRemoteObject(const sptr<IRemoteObject> &object, int32_t callId, 72 const sptr<AAFwk::IAbilityConnection> callback); 73 std::u16string GetSendString(const std::shared_ptr<IncomingContactInformation> nowInfo); 74 void SetIsControlSwitchOn(bool state); 75 bool GetIsControlSwitchOn(); 76 bool GetIsPlayRing(); 77 void UpdateVoipCallState(int32_t state); 78 bool IsStartVoiceBroadcast(); 79 int32_t CheckTelCallState(TelCallState state); 80 void UpdateNumberLocation(const std::string& location, int32_t callId); 81 void UpdateContactInfo(const ContactInfo &info, int32_t callId); 82 static std::shared_ptr<CallVoiceAssistantManager> GetInstance(); 83 84 private: 85 std::string broadcastCheck = "0"; 86 std::string controlCheck = "0"; 87 bool isConnectService = false; 88 std::string isplay = "0"; 89 bool isControlSwitchOn = false; 90 bool isBroadcastSwitchOn = false; 91 bool isQueryedBroadcastSwitch = false; 92 sptr<AAFwk::IAbilityConnection> connectCallback_ = nullptr; 93 sptr<AAFwk::IDataAbilityObserver> settingsCallback_ = nullptr; 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 HICAR_BUNDLE_NAME = ""; 119 const std::string ABILITY_NAME = "CallVoiceControlAbility"; 120 const std::string CONTROL_CHECK_RESULT = "incomingCallVoiceControlCheckResult"; 121 const std::string BROADCAST_CHECK_RESULT = "incomingCallVoiceBroadcastCheckResult"; 122 const int CHECK_CODE = 1006; 123 const int FAIL_CODE = -1; 124 }; 125 126 class VoiceAssistantConnectCallback : public IRemoteStub<AAFwk::IAbilityConnection> { 127 public: VoiceAssistantConnectCallback(int32_t id)128 VoiceAssistantConnectCallback(int32_t id) : startId(id) {}; 129 virtual ~VoiceAssistantConnectCallback() = default; 130 void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, 131 int resultCode) override; 132 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; 133 134 private: 135 int32_t startId = -1; 136 }; 137 138 class VoiceAssistantSwitchObserver : public AAFwk::DataAbilityObserverStub { 139 public: 140 VoiceAssistantSwitchObserver() = default; 141 virtual ~VoiceAssistantSwitchObserver() = default; 142 void OnChange() override; 143 }; 144 145 class VoiceAssistantRingSubscriber : public EventFwk::CommonEventSubscriber { 146 public: 147 VoiceAssistantRingSubscriber() = default; 148 explicit VoiceAssistantRingSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo); 149 virtual ~VoiceAssistantRingSubscriber() = default; 150 virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; 151 static bool Initial(); 152 static void Release(); 153 154 private: 155 static std::shared_ptr<VoiceAssistantRingSubscriber> subscriber_; 156 }; 157 158 } 159 } 160 #endif