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 "simcard_setting_asset.h" 32 #include "vibrate_asset.h" 33 #include "ringtone_fetch_result.h" 34 #include "iservice_registry.h" 35 #include <unistd.h> 36 #include <fcntl.h> 37 #include <cerrno> 38 39 #include "audio_system_manager.h" 40 41 #include "system_sound_manager.h" 42 43 namespace OHOS { 44 namespace Media { 45 class RingerModeCallbackImpl; 46 47 enum HapticsStyle { 48 HAPTICS_STYLE_STANDARD = 1, 49 HAPTICS_STYLE_GENTLE, 50 }; 51 52 class SystemSoundManagerImpl : public SystemSoundManager { 53 public: 54 SystemSoundManagerImpl(); 55 ~SystemSoundManagerImpl(); 56 57 int32_t SetRingerMode(const AudioStandard::AudioRingerMode &ringerMode); 58 AudioStandard::AudioRingerMode GetRingerMode() const; 59 std::string GetDefaultRingtoneUri(RingtoneType ringtoneType); 60 std::string GetDefaultSystemToneUri(SystemToneType systemToneType); 61 62 // SystemSoundManager override 63 int32_t SetRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri, 64 RingtoneType ringtoneType) override; 65 std::string GetRingtoneUri(const std::shared_ptr<AbilityRuntime::Context> &context, 66 RingtoneType ringtoneType) override; 67 std::shared_ptr<RingtonePlayer> GetRingtonePlayer(const std::shared_ptr<AbilityRuntime::Context> &context, 68 RingtoneType ringtoneType) override; 69 70 int32_t SetSystemToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri, 71 SystemToneType systemToneType) override; 72 std::string GetSystemToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, 73 SystemToneType systemToneType) override; 74 std::shared_ptr<SystemTonePlayer> GetSystemTonePlayer(const std::shared_ptr<AbilityRuntime::Context> &context, 75 SystemToneType systemToneType) override; 76 77 std::shared_ptr<ToneAttrs> GetDefaultRingtoneAttrs(const std::shared_ptr<AbilityRuntime::Context> &context, 78 RingtoneType ringtoneType) override; 79 std::vector<std::shared_ptr<ToneAttrs>> GetRingtoneAttrList(const std::shared_ptr<AbilityRuntime::Context> &context, 80 RingtoneType ringtoneType) override; 81 std::shared_ptr<ToneAttrs> GetDefaultSystemToneAttrs(const std::shared_ptr<AbilityRuntime::Context> &context, 82 SystemToneType systemToneType) override; 83 std::vector<std::shared_ptr<ToneAttrs>> GetSystemToneAttrList( 84 const std::shared_ptr<AbilityRuntime::Context> &context, SystemToneType systemToneType) override; 85 std::shared_ptr<ToneAttrs> GetDefaultAlarmToneAttrs( 86 const std::shared_ptr<AbilityRuntime::Context> &context) override; 87 std::vector<std::shared_ptr<ToneAttrs>> GetAlarmToneAttrList( 88 const std::shared_ptr<AbilityRuntime::Context> &context) override; 89 std::string GetAlarmToneUri(const std::shared_ptr<AbilityRuntime::Context> &context) override; 90 int32_t SetAlarmToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri) override; 91 int32_t OpenAlarmTone(const std::shared_ptr<AbilityRuntime::Context> &context, const std::string &uri) override; 92 int32_t Close(const int32_t &fd) override; 93 std::string AddCustomizedToneByExternalUri(const std::shared_ptr<AbilityRuntime::Context> &context, 94 const std::shared_ptr<ToneAttrs> &toneAttrs, const std::string &externalUri) override; 95 std::string AddCustomizedToneByFd(const std::shared_ptr<AbilityRuntime::Context> &context, 96 const std::shared_ptr<ToneAttrs> &toneAttrs, const int32_t &fd) override; 97 std::string AddCustomizedToneByFdAndOffset(const std::shared_ptr<AbilityRuntime::Context> &context, 98 const std::shared_ptr<ToneAttrs> &toneAttrs, const int32_t &fd, const int32_t &offset, 99 const int32_t &length) override; 100 int32_t RemoveCustomizedTone(const std::shared_ptr<AbilityRuntime::Context> &context, 101 const std::string &uri) override; 102 std::string GetRingtoneTitle(const std::string &ringtoneUri); 103 104 int32_t GetToneHapticsSettings(const std::shared_ptr<AbilityRuntime::Context> &context, 105 ToneHapticsType toneHapticsType, ToneHapticsSettings &settings) override; 106 int32_t SetToneHapticsSettings(const std::shared_ptr<AbilityRuntime::Context> &context, 107 ToneHapticsType toneHapticsType, const ToneHapticsSettings &settings) override; 108 int32_t GetToneHapticsList(const std::shared_ptr<AbilityRuntime::Context> &context, 109 bool isSynced, std::vector<std::shared_ptr<ToneHapticsAttrs>> &toneHapticsAttrsArray) override; 110 int32_t GetHapticsAttrsSyncedWithTone(const std::shared_ptr<AbilityRuntime::Context> &context, 111 const std::string &toneUri, std::shared_ptr<ToneHapticsAttrs> &toneHapticsAttrs) override; 112 int32_t OpenToneHaptics(const std::shared_ptr<AbilityRuntime::Context> &context, 113 const std::string &hapticsUri) override; 114 115 std::string GetHapticsUriByStyle(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 116 const std::string &standardHapticsUri, HapticsStyle hapticsStyle); 117 int32_t GetToneHapticsSettings(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 118 const std::string &toneUri, ToneHapticsType toneHapticsType, ToneHapticsSettings &settings); 119 int32_t GetHapticsAttrsSyncedWithTone(const std::string &toneUri, 120 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 121 std::shared_ptr<ToneHapticsAttrs> &toneHapticsAttrs); 122 int32_t SetToneHapticsSettings(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 123 const std::string &toneUri, ToneHapticsType toneHapticsType, const ToneHapticsSettings &settings); 124 bool CheckVibrateSwitchStatus(); 125 126 private: 127 void InitDefaultUriMap(); 128 void InitDefaultRingtoneUriMap(const std::string &ringtoneJsonPath); 129 void InitDefaultSystemToneUriMap(const std::string &systemToneJsonPath); 130 void InitDefaultToneHapticsMap(); 131 void ReadDefaultToneHaptics(const char *paramName, ToneHapticsType toneHapticsType); 132 std::string GetFullPath(const std::string &originalUri); 133 std::string GetJsonValue(const std::string &jsonPath); 134 135 int32_t AddCustomizedTone(const std::shared_ptr<DataShare::DataShareHelper> &dataShareHelper, 136 const std::shared_ptr<ToneAttrs> &toneAttrs); 137 int32_t WriteUriToDatabase(const std::string &key, const std::string &uri); 138 std::string GetUriFromDatabase(const std::string &key); 139 std::string GetKeyForDatabase(const std::string &systemSoundType, int32_t type); 140 void InitRingerMode(void); 141 void GetCustomizedTone(const std::shared_ptr<ToneAttrs> &toneAttrs); 142 void InitMap(); 143 std::string GetRingtoneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 144 const std::string &type); 145 int32_t UpdateRingtoneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, const int32_t &toneId, 146 RingtoneType ringtoneType, const int32_t &num); 147 std::string GetShotToneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 148 const std::string &type); 149 std::string GetNotificationToneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper); 150 std::string GetPresetShotToneUriByType(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 151 const std::string &type); 152 std::string GetPresetNotificationToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper); 153 int32_t UpdateShotToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, const int32_t &toneId, 154 SystemToneType systemToneType, const int32_t &num); 155 int32_t UpdateNotificatioToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 156 const int32_t &toneId); 157 int32_t SetNoSystemToneUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 158 SystemToneType systemToneType); 159 int32_t RemoveSourceTypeForSystemTone(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 160 SystemToneType systemToneType, SourceType sourceType); 161 162 bool ConvertToRingtoneType(ToneHapticsType toneHapticsType, RingtoneType &ringtoneType); 163 bool ConvertToSystemToneType(ToneHapticsType toneHapticsType, SystemToneType &systemToneType); 164 std::string ConvertToHapticsFileName(const std::string &fileName); 165 ToneHapticsMode IntToToneHapticsMode(int32_t value); 166 std::string GetCurrentToneUri(const std::shared_ptr<AbilityRuntime::Context> &context, 167 ToneHapticsType toneHapticsType); 168 std::unique_ptr<SimcardSettingAsset> GetSimcardSettingAssetByToneHapticsType( 169 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, ToneHapticsType toneHapticsType); 170 171 std::string GetToneSyncedHapticsUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 172 const std::string &toneUri); 173 std::string GetDefaultNonSyncedHapticsUri(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 174 ToneHapticsType toneHapticsType); 175 std::string GetFirstNonSyncedHapticsUri(); 176 int32_t GetDefaultToneHapticsSettings(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 177 const std::string ¤tToneUri, ToneHapticsType toneHapticsType, ToneHapticsSettings &settings); 178 179 int32_t UpdateToneHapticsSettings(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 180 const std::string &toneUri, ToneHapticsType toneHapticsType, const ToneHapticsSettings &settings); 181 bool GetVibrateTypeByStyle(int standardVibrateType, HapticsStyle hapticsStyle, int &vibrateType); 182 std::unique_ptr<RingtoneAsset> IsPresetRingtone(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, 183 const std::string &toneUri); 184 int GetStandardVibrateType(int toneType); 185 186 bool IsRingtoneTypeValid(RingtoneType ringtongType); 187 bool IsSystemToneTypeValid(SystemToneType systemToneType); 188 bool IsSystemToneType(const std::unique_ptr<RingtoneAsset> &ringtoneAsset, 189 const SystemToneType &systemToneType); 190 bool IsToneHapticsTypeValid(ToneHapticsType toneHapticsType); 191 192 static int32_t GetCurrentUserId(); 193 static Uri AssembleUri(const std::string &key, std::string tableType = ""); 194 static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelperProxy(std::string tableType = ""); 195 int32_t GetStringValue(const std::string &key, std::string &value, std::string tableType = ""); 196 197 std::string systemSoundPath_ = ""; 198 std::mutex uriMutex_; 199 std::mutex playerMutex_; 200 #ifdef SUPPORT_VIBRATOR 201 std::mutex toneHapticsMutex_; 202 #endif 203 std::string mimeType_ = ""; 204 std::string displayName_ = ""; 205 std::unordered_map<RingtoneType, std::string> defaultRingtoneUriMap_; 206 std::unordered_map<SystemToneType, std::string> defaultSystemToneUriMap_; 207 std::unordered_map<ToneHapticsType, std::string> defaultToneHapticsUriMap_; 208 std::shared_ptr<ToneAttrs> ringtoneAttrs_; 209 std::shared_ptr<ToneAttrs> systemtoneAttrs_; 210 std::shared_ptr<ToneAttrs> alarmtoneAttrs_; 211 212 std::atomic<AudioStandard::AudioRingerMode> ringerMode_ = AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL; 213 std::shared_ptr<AudioStandard::AudioGroupManager> audioGroupManager_ = nullptr; 214 std::shared_ptr<AudioStandard::AudioRingerModeCallback> ringerModeCallback_ = nullptr; 215 std::vector<std::shared_ptr<ToneAttrs>> ringtoneAttrsArray_; 216 std::vector<std::shared_ptr<ToneAttrs>> systemtoneAttrsArray_; 217 std::vector<std::shared_ptr<ToneAttrs>> alarmtoneAttrsArray_; 218 }; 219 220 class RingerModeCallbackImpl : public AudioStandard::AudioRingerModeCallback { 221 public: 222 explicit RingerModeCallbackImpl(SystemSoundManagerImpl &systemSoundManagerImpl); 223 virtual ~RingerModeCallbackImpl() = default; 224 void OnRingerModeUpdated(const AudioStandard::AudioRingerMode &ringerMode) override; 225 226 private: 227 SystemSoundManagerImpl &sysSoundMgr_; 228 AudioStandard::AudioRingerMode ringerMode_ { AudioStandard::AudioRingerMode::RINGER_MODE_NORMAL }; 229 }; 230 } // namespace Media 231 } // namespace OHOS 232 #endif // SYSTEM_SOUND_MANAGER_IMPL_H 233