1 /* 2 * Copyright (c) 2023 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 SYSTEM_SOUND_MANAGER_IMPL_H 17 #define SYSTEM_SOUND_MANAGER_IMPL_H 18 19 #include <array> 20 21 #include "datashare_helper.h" 22 #include "data_ability_helper.h" 23 #include "ability_runtime/context/context.h" 24 #include "uri.h" 25 #include "want.h" 26 27 #include <iostream> 28 #include "system_ability_definition.h" 29 #include "ringtone_db_const.h" 30 #include "ringtone_asset.h" 31 #include "ringtone_fetch_result.h" 32 #include "iservice_registry.h" 33 #include <unistd.h> 34 #include <fcntl.h> 35 #include <cerrno> 36 37 #include "audio_system_manager.h" 38 39 #include "system_sound_manager.h" 40 41 namespace OHOS { 42 namespace Media { 43 class RingerModeCallbackImpl; 44 45 class SystemSoundManagerImpl : public SystemSoundManager { 46 public: 47 SystemSoundManagerImpl(); 48 ~SystemSoundManagerImpl(); 49 50 int32_t SetRingerMode(const AudioStandard::AudioRingerMode &ringerMode); 51 AudioStandard::AudioRingerMode GetRingerMode() const; 52 std::string GetDefaultRingtoneUri(RingtoneType ringtoneType); 53 std::string GetDefaultSystemToneUri(SystemToneType systemToneType); 54 55 // SystemSoundManager override 56 int32_t SetRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri, 57 RingtoneType ringtoneType) override; 58 std::string GetRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &context, 59 RingtoneType ringtoneType) override; 60 std::shared_ptr<RingtonePlayer> GetRingtonePlayer(const std::shared_ptr<AbilityRuntime::Context> &context, 61 RingtoneType ringtoneType) override; 62 63 int32_t SetSystemToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri, 64 SystemToneType systemToneType) override; 65 std::string GetSystemToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, 66 SystemToneType systemToneType) override; 67 std::shared_ptr<SystemTonePlayer> GetSystemTonePlayer(const std::shared_ptr<AbilityRuntime::Context> &context, 68 SystemToneType systemToneType) override; 69 70 std::shared_ptr<ToneAttrs> GetDefaultRingtoneAttrs(const std::shared_ptr<AbilityRuntime::Context> &context, 71 RingtoneType ringtoneType) override; 72 std::vector<std::shared_ptr<ToneAttrs>> GetRingtoneAttrList(const std::shared_ptr<AbilityRuntime::Context> &context, 73 RingtoneType ringtoneType) override; 74 std::shared_ptr<ToneAttrs> GetDefaultSystemToneAttrs(const std::shared_ptr<AbilityRuntime::Context> &context, 75 SystemToneType systemToneType) override; 76 std::vector<std::shared_ptr<ToneAttrs>> GetSystemToneAttrList( 77 const std::shared_ptr<AbilityRuntime::Context> &context, SystemToneType systemToneType) override; 78 std::shared_ptr<ToneAttrs> GetDefaultAlarmToneAttrs( 79 const std::shared_ptr<AbilityRuntime::Context> &context) override; 80 std::vector<std::shared_ptr<ToneAttrs>> GetAlarmToneAttrList( 81 const std::shared_ptr<AbilityRuntime::Context> &context) override; 82 std::string GetAlarmToneUri(const std::shared_ptr<AbilityRuntime::Context> &context) override; 83 int32_t SetAlarmToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri) override; 84 int32_t OpenAlarmTone(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri) override; 85 int32_t Close(const int32_t &fd) override; 86 std::string AddCustomizedToneByExternalUri(const std::shared_ptr<AbilityRuntime::Context> &context, 87 const std::shared_ptr<ToneAttrs> &toneAttrs, const std::string &externalUri) override; 88 std::string AddCustomizedToneByFd(const std::shared_ptr<AbilityRuntime::Context> &context, 89 const std::shared_ptr<ToneAttrs> &toneAttrs, const int32_t &fd) override; 90 std::string AddCustomizedToneByFdAndOffset(const std::shared_ptr<AbilityRuntime::Context> &context, 91 const std::shared_ptr<ToneAttrs> &toneAttrs, const int32_t &fd, const int32_t &offset, 92 const int32_t &length) override; 93 int32_t RemoveCustomizedTone(const std::shared_ptr<AbilityRuntime::Context> &context, 94 const std::string &uri) override; 95 std::string GetRingtoneTitle(const std::string &ringtoneUri); 96 97 private: 98 void InitDefaultUriMap(); 99 void InitDefaultRingtoneUriMap(const std::string &ringtoneJsonPath); 100 void InitDefaultSystemToneUriMap(const std::string &systemToneJsonPath); 101 std::string GetFullPath(const std::string &originalUri); 102 std::string GetJsonValue(const std::string &jsonPath); 103 104 int32_t AddCustomizedTone(const std::shared_ptr<DataShare::DataShareHelper> &dataShareHelper, 105 const std::shared_ptr<ToneAttrs> &toneAttrs); 106 int32_t WriteUriToDatabase(const std::string &key, const std::string &uri); 107 std::string GetUriFromDatabase(const std::string &key); 108 std::string GetKeyForDatabase(const std::string &systemSoundType, int32_t type); 109 void InitRingerMode(void); 110 void GetCustomizedTone(const std::shared_ptr<ToneAttrs> &toneAttrs); 111 void InitMap(); 112 std::string GetRingtoneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 113 const std::string &type); 114 int32_t UpdateRingtoneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, const int32_t &toneId, 115 RingtoneType ringtoneType, const int32_t &num); 116 std::string GetShotToneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 117 const std::string &type); 118 std::string GetNotificationToneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper); 119 std::string GetPresetShotToneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 120 const std::string &type); 121 std::string GetPresetNotificationToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper); 122 int32_t UpdateShotToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, const int32_t &toneId, 123 SystemToneType systemToneType, const int32_t &num); 124 int32_t UpdateNotificatioToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 125 const int32_t &toneId); 126 int32_t SetNoSystemToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 127 SystemToneType systemToneType); 128 int32_t RemoveSourceTypeForSystemTone(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 129 SystemToneType systemToneType, SourceType sourceType); 130 131 bool isRingtoneTypeValid(RingtoneType ringtongType); 132 bool isSystemToneTypeValid(SystemToneType systemToneType); 133 134 std::string systemSoundPath_ = ""; 135 std::mutex uriMutex_; 136 std::mutex playerMutex_; 137 std::string mimeType_ = ""; 138 std::string displayName_ = ""; 139 std::unordered_map<RingtoneType, std::string> defaultRingtoneUriMap_; 140 std::unordered_map<SystemToneType, std::string> defaultSystemToneUriMap_; 141 std::shared_ptr<ToneAttrs> ringtoneAttrs_; 142 std::shared_ptr<ToneAttrs> systemtoneAttrs_; 143 std::shared_ptr<ToneAttrs> alarmtoneAttrs_; 144 145 std::atomic<AudioStandard::AudioRingerMode> ringerMode_ = AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL; 146 std::shared_ptr<AudioStandard::AudioGroupManager> audioGroupManager_ = nullptr; 147 std::shared_ptr<AudioStandard::AudioRingerModeCallback> ringerModeCallback_ = nullptr; 148 std::vector<std::shared_ptr<ToneAttrs>> ringtoneAttrsArray_; 149 std::vector<std::shared_ptr<ToneAttrs>> systemtoneAttrsArray_; 150 std::vector<std::shared_ptr<ToneAttrs>> alarmtoneAttrsArray_; 151 }; 152 153 class RingerModeCallbackImpl : public AudioStandard::AudioRingerModeCallback { 154 public: 155 explicit RingerModeCallbackImpl(SystemSoundManagerImpl &systemSoundManagerImpl); 156 virtual ~RingerModeCallbackImpl() = default; 157 void OnRingerModeUpdated(const AudioStandard::AudioRingerMode &ringerMode) override; 158 159 private: 160 SystemSoundManagerImpl &sysSoundMgr_; 161 AudioStandard::AudioRingerMode ringerMode_ { AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL }; 162 }; 163 } // namespace Media 164 } // namespace OHOS 165 #endif // SYSTEM_SOUND_MANAGER_IMPL_H 166